1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
/*
* environ[] array cleanup code and getenv() wrappers
*
* No copyright is claimed. This code is in the public domain; do with
* it what you wish.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_SYS_PRCTL_H
#include <sys/prctl.h>
#else
#define PR_GET_DUMPABLE 3
#endif
#if (!defined(HAVE_PRCTL) && defined(linux))
#include <sys/syscall.h>
#endif
#include <unistd.h>
#include <sys/types.h>
#include "env.h"
#ifndef HAVE_ENVIRON_DECL
extern char **environ;
#endif
static char * const forbid[] = {
"BASH_ENV=", /* GNU creeping featurism strikes again... */
"ENV=",
"HOME=",
"IFS=",
"KRB_CONF=",
"LD_", /* anything with the LD_ prefix */
"LIBPATH=",
"MAIL=",
"NLSPATH=",
"PATH=",
"SHELL=",
"SHLIB_PATH=",
(char *) 0
};
/* these are allowed, but with no slashes inside
(to work around security problems in GNU gettext) */
static char * const noslash[] = {
"LANG=",
"LANGUAGE=",
"LC_", /* anything with the LC_ prefix */
(char *) 0
};
struct ul_env_list {
char *env;
struct ul_env_list *next;
};
/*
* Saves @name env.variable to @ls, returns pointer to the new head of the list.
*/
static struct ul_env_list *env_list_add(struct ul_env_list *ls0, const char *str)
{
struct ul_env_list *ls;
char *p;
size_t sz = 0;
if (!str || !*str)
return ls0;
sz = strlen(str) + 1;
p = malloc(sizeof(struct ul_env_list) + sz);
if (!p)
return ls0;
ls = (struct ul_env_list *) p;
p += sizeof(struct ul_env_list);
memcpy(p, str, sz);
ls->env = p;
ls->next = ls0;
return ls;
}
/*
* Use setenv() for all stuff in @ls.
*
* It would be possible to use putenv(), but we want to keep @ls free()-able.
*/
int env_list_setenv(struct ul_env_list *ls)
{
int rc = 0;
while (ls && rc == 0) {
if (ls->env) {
char *val = strchr(ls->env, '=');
if (!val)
continue;
*val = '\0';
rc = setenv(ls->env, val + 1, 0);
*val = '=';
}
ls = ls->next;
}
return rc;
}
void env_list_free(struct ul_env_list *ls)
{
while (ls) {
struct ul_env_list *x = ls;
ls = ls->next;
free(x);
}
}
/*
* Removes unwanted variables from environ[]. If @org is not NULL than stores
* unwnated variables to the list.
*/
void __sanitize_env(struct ul_env_list **org)
{
char **envp = environ;
char * const *bad;
char **cur;
int last = 0;
for (cur = envp; *cur; cur++)
last++;
for (cur = envp; *cur; cur++) {
for (bad = forbid; *bad; bad++) {
if (strncmp(*cur, *bad, strlen(*bad)) == 0) {
if (org)
*org = env_list_add(*org, *cur);
last = remote_entry(envp, cur - envp, last);
cur--;
break;
}
}
}
for (cur = envp; *cur; cur++) {
for (bad = noslash; *bad; bad++) {
if (strncmp(*cur, *bad, strlen(*bad)) != 0)
continue;
if (!strchr(*cur, '/'))
continue; /* OK */
if (org)
*org = env_list_add(*org, *cur);
last = remote_entry(envp, cur - envp, last);
cur--;
break;
}
}
}
void sanitize_env(void)
{
__sanitize_env(NULL);
}
char *safe_getenv(const char *arg)
{
uid_t ruid = getuid();
if (ruid != 0 || (ruid != geteuid()) || (getgid() != getegid()))
return NULL;
#ifdef HAVE_PRCTL
if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
return NULL;
#else
#if (defined(linux) && defined(SYS_prctl))
if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
return NULL;
#endif
#endif
#ifdef HAVE_SECURE_GETENV
return secure_getenv(arg);
#elif HAVE___SECURE_GETENV
return __secure_getenv(arg);
#else
return getenv(arg);
#endif
}
#ifdef TEST_PROGRAM
int main(void)
{
char *const *bad;
char copy[32];
char *p;
int retval = EXIT_SUCCESS;
struct ul_env_list *removed = NULL;
for (bad = forbid; *bad; bad++) {
strcpy(copy, *bad);
p = strchr(copy, '=');
if (p)
*p = '\0';
setenv(copy, copy, 1);
}
/* removed */
__sanitize_env(&removed);
/* check removal */
for (bad = forbid; *bad; bad++) {
strcpy(copy, *bad);
p = strchr(copy, '=');
if (p)
*p = '\0';
p = getenv(copy);
if (p) {
warnx("%s was not removed", copy);
retval = EXIT_FAILURE;
}
}
/* restore removed */
env_list_setenv(removed);
/* check restore */
for (bad = forbid; *bad; bad++) {
strcpy(copy, *bad);
p = strchr(copy, '=');
if (p)
*p = '\0';
p = getenv(copy);
if (!p) {
warnx("%s was not restored", copy);
retval = EXIT_FAILURE;
}
}
env_list_free(removed);
return retval;
}
#endif
|