Blame elf/dl-tunables.c

Packit 6c4009
/* The tunable framework.  See the README.tunables to know how to use the
Packit 6c4009
   tunable in a glibc module.
Packit 6c4009
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
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 <startup.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <ldsodefs.h>
Packit 6c4009
Packit 6c4009
#define TUNABLES_INTERNAL 1
Packit 6c4009
#include "dl-tunables.h"
Packit 6c4009
Packit 6c4009
#include <not-errno.h>
Packit 6c4009
Packit 6c4009
#if TUNABLES_FRONTEND == TUNABLES_FRONTEND_valstring
Packit 6c4009
# define GLIBC_TUNABLES "GLIBC_TUNABLES"
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#if TUNABLES_FRONTEND == TUNABLES_FRONTEND_valstring
Packit 6c4009
static char *
Packit 6c4009
tunables_strdup (const char *in)
Packit 6c4009
{
Packit 6c4009
  size_t i = 0;
Packit 6c4009
Packit 6c4009
  while (in[i++] != '\0');
Packit 6c4009
  char *out = __sbrk (i);
Packit 6c4009
Packit 6c4009
  /* FIXME: In reality if the allocation fails, __sbrk will crash attempting to
Packit 6c4009
     set the thread-local errno since the TCB has not yet been set up.  This
Packit 6c4009
     needs to be fixed with an __sbrk implementation that does not set
Packit 6c4009
     errno.  */
Packit 6c4009
  if (out == (void *)-1)
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  i--;
Packit 6c4009
Packit 6c4009
  while (i-- > 0)
Packit 6c4009
    out[i] = in[i];
Packit 6c4009
Packit 6c4009
  return out;
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
static char **
Packit 6c4009
get_next_env (char **envp, char **name, size_t *namelen, char **val,
Packit 6c4009
	      char ***prev_envp)
Packit 6c4009
{
Packit 6c4009
  while (envp != NULL && *envp != NULL)
Packit 6c4009
    {
Packit 6c4009
      char **prev = envp;
Packit 6c4009
      char *envline = *envp++;
Packit 6c4009
      int len = 0;
Packit 6c4009
Packit 6c4009
      while (envline[len] != '\0' && envline[len] != '=')
Packit 6c4009
	len++;
Packit 6c4009
Packit 6c4009
      /* Just the name and no value, go to the next one.  */
Packit 6c4009
      if (envline[len] == '\0')
Packit 6c4009
	continue;
Packit 6c4009
Packit 6c4009
      *name = envline;
Packit 6c4009
      *namelen = len;
Packit 6c4009
      *val = &envline[len + 1];
Packit 6c4009
      *prev_envp = prev;
Packit 6c4009
Packit 6c4009
      return envp;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TUNABLE_SET_VAL_IF_VALID_RANGE(__cur, __val, __type)		      \
Packit 6c4009
({									      \
Packit 6c4009
  __type min = (__cur)->type.min;					      \
Packit 6c4009
  __type max = (__cur)->type.max;					      \
Packit 6c4009
									      \
Packit 6c4009
  if ((__type) (__val) >= min && (__type) (val) <= max)			      \
Packit 6c4009
    {									      \
Packit 6c4009
      (__cur)->val.numval = val;					      \
Packit 6c4009
      (__cur)->initialized = true;					      \
Packit 6c4009
    }									      \
Packit 6c4009
})
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_tunable_update_val (tunable_t *cur, const void *valp)
Packit 6c4009
{
Packit 6c4009
  uint64_t val;
Packit 6c4009
Packit 6c4009
  if (cur->type.type_code != TUNABLE_TYPE_STRING)
Packit 6c4009
    val = *((int64_t *) valp);
Packit 6c4009
Packit 6c4009
  switch (cur->type.type_code)
Packit 6c4009
    {
Packit 6c4009
    case TUNABLE_TYPE_INT_32:
Packit 6c4009
	{
Packit 6c4009
	  TUNABLE_SET_VAL_IF_VALID_RANGE (cur, val, int64_t);
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
    case TUNABLE_TYPE_UINT_64:
Packit 6c4009
	{
Packit 6c4009
	  TUNABLE_SET_VAL_IF_VALID_RANGE (cur, val, uint64_t);
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
    case TUNABLE_TYPE_SIZE_T:
Packit 6c4009
	{
Packit 6c4009
	  TUNABLE_SET_VAL_IF_VALID_RANGE (cur, val, uint64_t);
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
    case TUNABLE_TYPE_STRING:
Packit 6c4009
	{
Packit 6c4009
	  cur->val.strval = valp;
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
    default:
Packit 6c4009
      __builtin_unreachable ();
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Validate range of the input value and initialize the tunable CUR if it looks
Packit 6c4009
   good.  */
Packit 6c4009
static void
Packit 6c4009
tunable_initialize (tunable_t *cur, const char *strval)
Packit 6c4009
{
Packit 6c4009
  uint64_t val;
Packit 6c4009
  const void *valp;
Packit 6c4009
Packit 6c4009
  if (cur->type.type_code != TUNABLE_TYPE_STRING)
Packit 6c4009
    {
Packit 6c4009
      val = _dl_strtoul (strval, NULL);
Packit 6c4009
      valp = &val;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      cur->initialized = true;
Packit 6c4009
      valp = strval;
Packit 6c4009
    }
Packit 6c4009
  do_tunable_update_val (cur, valp);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
__tunable_set_val (tunable_id_t id, void *valp)
Packit 6c4009
{
Packit 6c4009
  tunable_t *cur = &tunable_list[id];
Packit 6c4009
Packit 6c4009
  do_tunable_update_val (cur, valp);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#if TUNABLES_FRONTEND == TUNABLES_FRONTEND_valstring
Packit 6c4009
/* Parse the tunable string TUNESTR and adjust it to drop any tunables that may
Packit 6c4009
   be unsafe for AT_SECURE processes so that it can be used as the new
Packit 6c4009
   environment variable value for GLIBC_TUNABLES.  VALSTRING is the original
Packit 6c4009
   environment variable string which we use to make NULL terminated values so
Packit 6c4009
   that we don't have to allocate memory again for it.  */
Packit 6c4009
static void
Packit 6c4009
parse_tunables (char *tunestr, char *valstring)
Packit 6c4009
{
Packit 6c4009
  if (tunestr == NULL || *tunestr == '\0')
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  char *p = tunestr;
Packit 6c4009
Packit 6c4009
  while (true)
Packit 6c4009
    {
Packit 6c4009
      char *name = p;
Packit 6c4009
      size_t len = 0;
Packit 6c4009
Packit 6c4009
      /* First, find where the name ends.  */
Packit 6c4009
      while (p[len] != '=' && p[len] != ':' && p[len] != '\0')
Packit 6c4009
	len++;
Packit 6c4009
Packit 6c4009
      /* If we reach the end of the string before getting a valid name-value
Packit 6c4009
	 pair, bail out.  */
Packit 6c4009
      if (p[len] == '\0')
Packit 6c4009
	return;
Packit 6c4009
Packit 6c4009
      /* We did not find a valid name-value pair before encountering the
Packit 6c4009
	 colon.  */
Packit 6c4009
      if (p[len]== ':')
Packit 6c4009
	{
Packit 6c4009
	  p += len + 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      p += len + 1;
Packit 6c4009
Packit 6c4009
      /* Take the value from the valstring since we need to NULL terminate it.  */
Packit 6c4009
      char *value = &valstring[p - tunestr];
Packit 6c4009
      len = 0;
Packit 6c4009
Packit 6c4009
      while (p[len] != ':' && p[len] != '\0')
Packit 6c4009
	len++;
Packit 6c4009
Packit 6c4009
      /* Add the tunable if it exists.  */
Packit 6c4009
      for (size_t i = 0; i < sizeof (tunable_list) / sizeof (tunable_t); i++)
Packit 6c4009
	{
Packit 6c4009
	  tunable_t *cur = &tunable_list[i];
Packit 6c4009
Packit 6c4009
	  if (tunable_is_name (cur->name, name))
Packit 6c4009
	    {
Packit 6c4009
	      /* If we are in a secure context (AT_SECURE) then ignore the tunable
Packit 6c4009
		 unless it is explicitly marked as secure.  Tunable values take
Packit 6c4009
		 precendence over their envvar aliases.  */
Packit 6c4009
	      if (__libc_enable_secure)
Packit 6c4009
		{
Packit 6c4009
		  if (cur->security_level == TUNABLE_SECLEVEL_SXID_ERASE)
Packit 6c4009
		    {
Packit 6c4009
		      if (p[len] == '\0')
Packit 6c4009
			{
Packit 6c4009
			  /* Last tunable in the valstring.  Null-terminate and
Packit 6c4009
			     return.  */
Packit 6c4009
			  *name = '\0';
Packit 6c4009
			  return;
Packit 6c4009
			}
Packit 6c4009
		      else
Packit 6c4009
			{
Packit 6c4009
			  /* Remove the current tunable from the string.  We do
Packit 6c4009
			     this by overwriting the string starting from NAME
Packit 6c4009
			     (which is where the current tunable begins) with
Packit 6c4009
			     the remainder of the string.  We then have P point
Packit 6c4009
			     to NAME so that we continue in the correct
Packit 6c4009
			     position in the valstring.  */
Packit 6c4009
			  char *q = &p[len + 1];
Packit 6c4009
			  p = name;
Packit 6c4009
			  while (*q != '\0')
Packit 6c4009
			    *name++ = *q++;
Packit 6c4009
			  name[0] = '\0';
Packit 6c4009
			  len = 0;
Packit 6c4009
			}
Packit 6c4009
		    }
Packit 6c4009
Packit 6c4009
		  if (cur->security_level != TUNABLE_SECLEVEL_NONE)
Packit 6c4009
		    break;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      value[len] = '\0';
Packit 6c4009
	      tunable_initialize (cur, value);
Packit 6c4009
	      break;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (p[len] == '\0')
Packit 6c4009
	return;
Packit 6c4009
      else
Packit 6c4009
	p += len + 1;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Enable the glibc.malloc.check tunable in SETUID/SETGID programs only when
Packit 6c4009
   the system administrator has created the /etc/suid-debug file.  This is a
Packit 6c4009
   special case where we want to conditionally enable/disable a tunable even
Packit 6c4009
   for setuid binaries.  We use the special version of access() to avoid
Packit 6c4009
   setting ERRNO, which is a TLS variable since TLS has not yet been set
Packit 6c4009
   up.  */
Packit 6c4009
static inline void
Packit 6c4009
__always_inline
Packit 6c4009
maybe_enable_malloc_check (void)
Packit 6c4009
{
Packit 6c4009
  tunable_id_t id = TUNABLE_ENUM_NAME (glibc, malloc, check);
Packit 6c4009
  if (__libc_enable_secure && __access_noerrno ("/etc/suid-debug", F_OK) == 0)
Packit 6c4009
    tunable_list[id].security_level = TUNABLE_SECLEVEL_NONE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Initialize the tunables list from the environment.  For now we only use the
Packit 6c4009
   ENV_ALIAS to find values.  Later we will also use the tunable names to find
Packit 6c4009
   values.  */
Packit 6c4009
void
Packit 6c4009
__tunables_init (char **envp)
Packit 6c4009
{
Packit 6c4009
  char *envname = NULL;
Packit 6c4009
  char *envval = NULL;
Packit 6c4009
  size_t len = 0;
Packit 6c4009
  char **prev_envp = envp;
Packit 6c4009
Packit 6c4009
  maybe_enable_malloc_check ();
Packit 6c4009
Packit 6c4009
  while ((envp = get_next_env (envp, &envname, &len, &envval,
Packit 6c4009
			       &prev_envp)) != NULL)
Packit 6c4009
    {
Packit 6c4009
#if TUNABLES_FRONTEND == TUNABLES_FRONTEND_valstring
Packit 6c4009
      if (tunable_is_name (GLIBC_TUNABLES, envname))
Packit 6c4009
	{
Packit 6c4009
	  char *new_env = tunables_strdup (envname);
Packit 6c4009
	  if (new_env != NULL)
Packit 6c4009
	    parse_tunables (new_env + len + 1, envval);
Packit 6c4009
	  /* Put in the updated envval.  */
Packit 6c4009
	  *prev_envp = new_env;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
      for (int i = 0; i < sizeof (tunable_list) / sizeof (tunable_t); i++)
Packit 6c4009
	{
Packit 6c4009
	  tunable_t *cur = &tunable_list[i];
Packit 6c4009
Packit 6c4009
	  /* Skip over tunables that have either been set already or should be
Packit 6c4009
	     skipped.  */
Packit 6c4009
	  if (cur->initialized || cur->env_alias == NULL)
Packit 6c4009
	    continue;
Packit 6c4009
Packit 6c4009
	  const char *name = cur->env_alias;
Packit 6c4009
Packit 6c4009
	  /* We have a match.  Initialize and move on to the next line.  */
Packit 6c4009
	  if (tunable_is_name (name, envname))
Packit 6c4009
	    {
Packit 6c4009
	      /* For AT_SECURE binaries, we need to check the security settings of
Packit 6c4009
		 the tunable and decide whether we read the value and also whether
Packit 6c4009
		 we erase the value so that child processes don't inherit them in
Packit 6c4009
		 the environment.  */
Packit 6c4009
	      if (__libc_enable_secure)
Packit 6c4009
		{
Packit 6c4009
		  if (cur->security_level == TUNABLE_SECLEVEL_SXID_ERASE)
Packit 6c4009
		    {
Packit 6c4009
		      /* Erase the environment variable.  */
Packit 6c4009
		      char **ep = prev_envp;
Packit 6c4009
Packit 6c4009
		      while (*ep != NULL)
Packit 6c4009
			{
Packit 6c4009
			  if (tunable_is_name (name, *ep))
Packit 6c4009
			    {
Packit 6c4009
			      char **dp = ep;
Packit 6c4009
Packit 6c4009
			      do
Packit 6c4009
				dp[0] = dp[1];
Packit 6c4009
			      while (*dp++);
Packit 6c4009
			    }
Packit 6c4009
			  else
Packit 6c4009
			    ++ep;
Packit 6c4009
			}
Packit 6c4009
		      /* Reset the iterator so that we read the environment again
Packit 6c4009
			 from the point we erased.  */
Packit 6c4009
		      envp = prev_envp;
Packit 6c4009
		    }
Packit 6c4009
Packit 6c4009
		  if (cur->security_level != TUNABLE_SECLEVEL_NONE)
Packit 6c4009
		    continue;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      tunable_initialize (cur, envval);
Packit 6c4009
	      break;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Set the tunable value.  This is called by the module that the tunable exists
Packit 6c4009
   in. */
Packit 6c4009
void
Packit 6c4009
__tunable_get_val (tunable_id_t id, void *valp, tunable_callback_t callback)
Packit 6c4009
{
Packit 6c4009
  tunable_t *cur = &tunable_list[id];
Packit 6c4009
Packit 6c4009
  switch (cur->type.type_code)
Packit 6c4009
    {
Packit 6c4009
    case TUNABLE_TYPE_UINT_64:
Packit 6c4009
	{
Packit 6c4009
	  *((uint64_t *) valp) = (uint64_t) cur->val.numval;
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
    case TUNABLE_TYPE_INT_32:
Packit 6c4009
	{
Packit 6c4009
	  *((int32_t *) valp) = (int32_t) cur->val.numval;
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
    case TUNABLE_TYPE_SIZE_T:
Packit 6c4009
	{
Packit 6c4009
	  *((size_t *) valp) = (size_t) cur->val.numval;
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
    case TUNABLE_TYPE_STRING:
Packit 6c4009
	{
Packit 6c4009
	  *((const char **)valp) = cur->val.strval;
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
    default:
Packit 6c4009
      __builtin_unreachable ();
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (cur->initialized && callback != NULL)
Packit 6c4009
    callback (&cur->val);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
rtld_hidden_def (__tunable_get_val)