Blame nscd/nscd_getgr_r.c

Packit 6c4009
/* Copyright (C) 1998-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1998.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <alloca.h>
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <grp.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
#include <sys/socket.h>
Packit 6c4009
#include <sys/uio.h>
Packit 6c4009
#include <sys/un.h>
Packit 6c4009
#include <not-cancel.h>
Packit 6c4009
#include <_itoa.h>
Packit 6c4009
#include <scratch_buffer.h>
Packit 6c4009
Packit 6c4009
#include "nscd-client.h"
Packit 6c4009
#include "nscd_proto.h"
Packit 6c4009
Packit 6c4009
int __nss_not_use_nscd_group;
Packit 6c4009
Packit 6c4009
static int nscd_getgr_r (const char *key, size_t keylen, request_type type,
Packit 6c4009
			 struct group *resultbuf, char *buffer,
Packit 6c4009
			 size_t buflen, struct group **result);
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__nscd_getgrnam_r (const char *name, struct group *resultbuf, char *buffer,
Packit 6c4009
		   size_t buflen, struct group **result)
Packit 6c4009
{
Packit 6c4009
  return nscd_getgr_r (name, strlen (name) + 1, GETGRBYNAME, resultbuf,
Packit 6c4009
		       buffer, buflen, result);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__nscd_getgrgid_r (gid_t gid, struct group *resultbuf, char *buffer,
Packit 6c4009
		   size_t buflen, struct group **result)
Packit 6c4009
{
Packit 6c4009
  char buf[3 * sizeof (gid_t)];
Packit 6c4009
  buf[sizeof (buf) - 1] = '\0';
Packit 6c4009
  char *cp = _itoa_word (gid, buf + sizeof (buf) - 1, 10, 0);
Packit 6c4009
Packit 6c4009
  return nscd_getgr_r (cp, buf + sizeof (buf) - cp, GETGRBYGID, resultbuf,
Packit 6c4009
		       buffer, buflen, result);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
libc_locked_map_ptr (,__gr_map_handle) attribute_hidden;
Packit 6c4009
/* Note that we only free the structure if necessary.  The memory
Packit 6c4009
   mapping is not removed since it is not visible to the malloc
Packit 6c4009
   handling.  */
Packit 6c4009
libc_freeres_fn (gr_map_free)
Packit 6c4009
{
Packit 6c4009
  if (__gr_map_handle.mapped != NO_MAPPING)
Packit 6c4009
    {
Packit 6c4009
      void *p = __gr_map_handle.mapped;
Packit 6c4009
      __gr_map_handle.mapped = NO_MAPPING;
Packit 6c4009
      free (p);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
nscd_getgr_r (const char *key, size_t keylen, request_type type,
Packit 6c4009
	      struct group *resultbuf, char *buffer, size_t buflen,
Packit 6c4009
	      struct group **result)
Packit 6c4009
{
Packit 6c4009
  int gc_cycle;
Packit 6c4009
  int nretries = 0;
Packit 6c4009
  const uint32_t *len = NULL;
Packit 6c4009
  struct scratch_buffer lenbuf;
Packit 6c4009
  scratch_buffer_init (&lenbuf);
Packit 6c4009
Packit 6c4009
  /* If the mapping is available, try to search there instead of
Packit 6c4009
     communicating with the nscd.  */
Packit 6c4009
  struct mapped_database *mapped = __nscd_get_map_ref (GETFDGR, "group",
Packit 6c4009
						       &__gr_map_handle,
Packit 6c4009
						       &gc_cycle);
Packit 6c4009
 retry:;
Packit 6c4009
  const char *gr_name = NULL;
Packit 6c4009
  size_t gr_name_len = 0;
Packit 6c4009
  int retval = -1;
Packit 6c4009
  const char *recend = (const char *) ~UINTMAX_C (0);
Packit 6c4009
  gr_response_header gr_resp;
Packit 6c4009
Packit 6c4009
  if (mapped != NO_MAPPING)
Packit 6c4009
    {
Packit 6c4009
      struct datahead *found = __nscd_cache_search (type, key, keylen, mapped,
Packit 6c4009
						    sizeof gr_resp);
Packit 6c4009
      if (found != NULL)
Packit 6c4009
	{
Packit 6c4009
	  len = (const uint32_t *) (&found->data[0].grdata + 1);
Packit 6c4009
	  gr_resp = found->data[0].grdata;
Packit 6c4009
	  gr_name = ((const char *) len
Packit 6c4009
		     + gr_resp.gr_mem_cnt * sizeof (uint32_t));
Packit 6c4009
	  gr_name_len = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
Packit 6c4009
	  recend = (const char *) found->data + found->recsize;
Packit 6c4009
	  /* Now check if we can trust gr_resp fields.  If GC is
Packit 6c4009
	     in progress, it can contain anything.  */
Packit 6c4009
	  if (mapped->head->gc_cycle != gc_cycle)
Packit 6c4009
	    {
Packit 6c4009
	      retval = -2;
Packit 6c4009
	      goto out;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  /* The alignment is always sufficient, unless GC is in progress.  */
Packit 6c4009
	  assert (((uintptr_t) len & (__alignof__ (*len) - 1)) == 0);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int sock = -1;
Packit 6c4009
  if (gr_name == NULL)
Packit 6c4009
    {
Packit 6c4009
      sock = __nscd_open_socket (key, keylen, type, &gr_resp,
Packit 6c4009
				 sizeof (gr_resp));
Packit 6c4009
      if (sock == -1)
Packit 6c4009
	{
Packit 6c4009
	  __nss_not_use_nscd_group = 1;
Packit 6c4009
	  goto out;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* No value found so far.  */
Packit 6c4009
  *result = NULL;
Packit 6c4009
Packit 6c4009
  if (__glibc_unlikely (gr_resp.found == -1))
Packit 6c4009
    {
Packit 6c4009
      /* The daemon does not cache this database.  */
Packit 6c4009
      __nss_not_use_nscd_group = 1;
Packit 6c4009
      goto out_close;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (gr_resp.found == 1)
Packit 6c4009
    {
Packit 6c4009
      struct iovec vec[2];
Packit 6c4009
      char *p = buffer;
Packit 6c4009
      size_t total_len;
Packit 6c4009
      uintptr_t align;
Packit 6c4009
      nscd_ssize_t cnt;
Packit 6c4009
Packit 6c4009
      /* Now allocate the buffer the array for the group members.  We must
Packit 6c4009
	 align the pointer.  */
Packit 6c4009
      align = ((__alignof__ (char *) - (p - ((char *) 0)))
Packit 6c4009
	       & (__alignof__ (char *) - 1));
Packit 6c4009
      total_len = (align + (1 + gr_resp.gr_mem_cnt) * sizeof (char *)
Packit 6c4009
		   + gr_resp.gr_name_len + gr_resp.gr_passwd_len);
Packit 6c4009
      if (__glibc_unlikely (buflen < total_len))
Packit 6c4009
	{
Packit 6c4009
	no_room:
Packit 6c4009
	  __set_errno (ERANGE);
Packit 6c4009
	  retval = ERANGE;
Packit 6c4009
	  goto out_close;
Packit 6c4009
	}
Packit 6c4009
      buflen -= total_len;
Packit 6c4009
Packit 6c4009
      p += align;
Packit 6c4009
      resultbuf->gr_mem = (char **) p;
Packit 6c4009
      p += (1 + gr_resp.gr_mem_cnt) * sizeof (char *);
Packit 6c4009
Packit 6c4009
      /* Set pointers for strings.  */
Packit 6c4009
      resultbuf->gr_name = p;
Packit 6c4009
      p += gr_resp.gr_name_len;
Packit 6c4009
      resultbuf->gr_passwd = p;
Packit 6c4009
      p += gr_resp.gr_passwd_len;
Packit 6c4009
Packit 6c4009
      /* Fill in what we know now.  */
Packit 6c4009
      resultbuf->gr_gid = gr_resp.gr_gid;
Packit 6c4009
Packit 6c4009
      /* Read the length information, group name, and password.  */
Packit 6c4009
      if (gr_name == NULL)
Packit 6c4009
	{
Packit 6c4009
	  /* Handle a simple, usual case: no group members.  */
Packit 6c4009
	  if (__glibc_likely (gr_resp.gr_mem_cnt == 0))
Packit 6c4009
	    {
Packit 6c4009
	      size_t n = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
Packit 6c4009
	      if (__builtin_expect (__readall (sock, resultbuf->gr_name, n)
Packit 6c4009
				    != (ssize_t) n, 0))
Packit 6c4009
		goto out_close;
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      /* Allocate array to store lengths.  */
Packit 6c4009
	      if (!scratch_buffer_set_array_size
Packit 6c4009
		  (&lenbuf, gr_resp.gr_mem_cnt, sizeof (uint32_t)))
Packit 6c4009
		goto out_close;
Packit 6c4009
	      len = lenbuf.data;
Packit 6c4009
Packit 6c4009
	      vec[0].iov_base = (void *) len;
Packit 6c4009
	      vec[0].iov_len = gr_resp.gr_mem_cnt * sizeof (uint32_t);
Packit 6c4009
	      vec[1].iov_base = resultbuf->gr_name;
Packit 6c4009
	      vec[1].iov_len = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
Packit 6c4009
	      total_len = vec[0].iov_len + vec[1].iov_len;
Packit 6c4009
Packit 6c4009
	      /* Get this data.  */
Packit 6c4009
	      size_t n = __readvall (sock, vec, 2);
Packit 6c4009
	      if (__glibc_unlikely (n != total_len))
Packit 6c4009
		goto out_close;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	/* We already have the data.  Just copy the group name and
Packit 6c4009
	   password.  */
Packit 6c4009
	memcpy (resultbuf->gr_name, gr_name,
Packit 6c4009
		gr_resp.gr_name_len + gr_resp.gr_passwd_len);
Packit 6c4009
Packit 6c4009
      /* Clear the terminating entry.  */
Packit 6c4009
      resultbuf->gr_mem[gr_resp.gr_mem_cnt] = NULL;
Packit 6c4009
Packit 6c4009
      /* Prepare reading the group members.  */
Packit 6c4009
      total_len = 0;
Packit 6c4009
      for (cnt = 0; cnt < gr_resp.gr_mem_cnt; ++cnt)
Packit 6c4009
	{
Packit 6c4009
	  resultbuf->gr_mem[cnt] = p;
Packit 6c4009
	  total_len += len[cnt];
Packit 6c4009
	  p += len[cnt];
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (__glibc_unlikely (gr_name + gr_name_len + total_len > recend))
Packit 6c4009
	{
Packit 6c4009
	  /* len array might contain garbage during nscd GC cycle,
Packit 6c4009
	     retry rather than fail in that case.  */
Packit 6c4009
	  if (gr_name != NULL && mapped->head->gc_cycle != gc_cycle)
Packit 6c4009
	    retval = -2;
Packit 6c4009
	  goto out_close;
Packit 6c4009
	}
Packit 6c4009
      if (__glibc_unlikely (total_len > buflen))
Packit 6c4009
	{
Packit 6c4009
	  /* len array might contain garbage during nscd GC cycle,
Packit 6c4009
	     retry rather than fail in that case.  */
Packit 6c4009
	  if (gr_name != NULL && mapped->head->gc_cycle != gc_cycle)
Packit 6c4009
	    {
Packit 6c4009
	      retval = -2;
Packit 6c4009
	      goto out_close;
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    goto no_room;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      retval = 0;
Packit 6c4009
Packit 6c4009
      /* If there are no group members TOTAL_LEN is zero.  */
Packit 6c4009
      if (gr_name == NULL)
Packit 6c4009
	{
Packit 6c4009
	  if (total_len > 0
Packit 6c4009
	      && __builtin_expect (__readall (sock, resultbuf->gr_mem[0],
Packit 6c4009
					      total_len) != total_len, 0))
Packit 6c4009
	    {
Packit 6c4009
	      /* The `errno' to some value != ERANGE.  */
Packit 6c4009
	      __set_errno (ENOENT);
Packit 6c4009
	      retval = ENOENT;
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    *result = resultbuf;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* Copy the group member names.  */
Packit 6c4009
	  memcpy (resultbuf->gr_mem[0], gr_name + gr_name_len, total_len);
Packit 6c4009
Packit 6c4009
	  /* Try to detect corrupt databases.  */
Packit 6c4009
	  if (resultbuf->gr_name[gr_name_len - 1] != '\0'
Packit 6c4009
	      || resultbuf->gr_passwd[gr_resp.gr_passwd_len - 1] != '\0'
Packit 6c4009
	      || ({for (cnt = 0; cnt < gr_resp.gr_mem_cnt; ++cnt)
Packit 6c4009
		    if (resultbuf->gr_mem[cnt][len[cnt] - 1] != '\0')
Packit 6c4009
		      break;
Packit 6c4009
		  cnt < gr_resp.gr_mem_cnt; }))
Packit 6c4009
	    {
Packit 6c4009
	      /* We cannot use the database.  */
Packit 6c4009
	      retval = mapped->head->gc_cycle != gc_cycle ? -2 : -1;
Packit 6c4009
	      goto out_close;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  *result = resultbuf;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* Set errno to 0 to indicate no error, just no found record.  */
Packit 6c4009
      __set_errno (0);
Packit 6c4009
      /* Even though we have not found anything, the result is zero.  */
Packit 6c4009
      retval = 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
 out_close:
Packit 6c4009
  if (sock != -1)
Packit 6c4009
    __close_nocancel_nostatus (sock);
Packit 6c4009
 out:
Packit 6c4009
  if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
Packit 6c4009
    {
Packit 6c4009
      /* When we come here this means there has been a GC cycle while we
Packit 6c4009
	 were looking for the data.  This means the data might have been
Packit 6c4009
	 inconsistent.  Retry if possible.  */
Packit 6c4009
      if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
Packit 6c4009
	{
Packit 6c4009
	  /* nscd is just running gc now.  Disable using the mapping.  */
Packit 6c4009
	  if (atomic_decrement_val (&mapped->counter) == 0)
Packit 6c4009
	    __nscd_unmap (mapped);
Packit 6c4009
	  mapped = NO_MAPPING;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (retval != -1)
Packit 6c4009
	goto retry;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  scratch_buffer_free (&lenbuf);
Packit 6c4009
Packit 6c4009
  return retval;
Packit 6c4009
}