Annotation of embedaddon/sudo/src/exec_common.c, revision 1.1.1.5
1.1 misho 1: /*
1.1.1.3 misho 2: * Copyright (c) 2009-2013 Todd C. Miller <Todd.Miller@courtesan.com>
1.1 misho 3: *
4: * Permission to use, copy, modify, and distribute this software for any
5: * purpose with or without fee is hereby granted, provided that the above
6: * copyright notice and this permission notice appear in all copies.
7: *
8: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15: */
16:
17: #include <config.h>
18:
19: #include <sys/types.h>
20: #include <stdio.h>
21: #ifdef STDC_HEADERS
22: # include <stdlib.h>
23: # include <stddef.h>
24: #else
25: # ifdef HAVE_STDLIB_H
26: # include <stdlib.h>
27: # endif
28: #endif /* STDC_HEADERS */
29: #ifdef HAVE_STRING_H
30: # include <string.h>
31: #endif /* HAVE_STRING_H */
32: #ifdef HAVE_STRINGS_H
33: # include <strings.h>
34: #endif /* HAVE_STRINGS_H */
35: #ifdef HAVE_UNISTD_H
36: # include <unistd.h>
37: #endif /* HAVE_UNISTD_H */
38: #ifdef HAVE_PRIV_SET
39: # include <priv.h>
40: #endif
41: #include <errno.h>
1.1.1.3 misho 42: #include <fcntl.h>
1.1.1.2 misho 43: #include <signal.h>
1.1 misho 44:
45: #include "sudo.h"
46: #include "sudo_exec.h"
47:
48: /*
49: * Disable execution of child processes in the command we are about
50: * to run. On systems with privilege sets, we can remove the exec
51: * privilege. On other systems we use LD_PRELOAD and the like.
52: */
53: static char * const *
54: disable_execute(char *const envp[])
55: {
56: #ifdef _PATH_SUDO_NOEXEC
57: char *preload, **nenvp = NULL;
58: int env_len, env_size;
59: int preload_idx = -1;
60: # ifdef RTLD_PRELOAD_ENABLE_VAR
61: bool enabled = false;
62: # endif
63: #endif /* _PATH_SUDO_NOEXEC */
64: debug_decl(disable_execute, SUDO_DEBUG_UTIL)
65:
66: #ifdef HAVE_PRIV_SET
67: /* Solaris privileges, remove PRIV_PROC_EXEC post-execve. */
1.1.1.5 ! misho 68: (void)priv_set(PRIV_ON, PRIV_INHERITABLE, "PRIV_FILE_DAC_READ", NULL);
! 69: (void)priv_set(PRIV_ON, PRIV_INHERITABLE, "PRIV_FILE_DAC_WRITE", NULL);
! 70: (void)priv_set(PRIV_ON, PRIV_INHERITABLE, "PRIV_FILE_DAC_SEARCH", NULL);
1.1 misho 71: if (priv_set(PRIV_OFF, PRIV_LIMIT, "PRIV_PROC_EXEC", NULL) == 0)
1.1.1.5 ! misho 72: debug_return_const_ptr(envp);
! 73: warning(U_("unable to remove PRIV_PROC_EXEC from PRIV_LIMIT"));
1.1 misho 74: #endif /* HAVE_PRIV_SET */
75:
76: #ifdef _PATH_SUDO_NOEXEC
77: /*
78: * Preload a noexec file. For a list of LD_PRELOAD-alikes, see
79: * http://www.fortran-2000.com/ArnaudRecipes/sharedlib.html
80: * XXX - need to support 32-bit and 64-bit variants
81: */
82:
83: /* Count entries in envp, looking for LD_PRELOAD as we go. */
84: for (env_len = 0; envp[env_len] != NULL; env_len++) {
85: if (strncmp(envp[env_len], RTLD_PRELOAD_VAR "=", sizeof(RTLD_PRELOAD_VAR)) == 0) {
86: preload_idx = env_len;
87: continue;
88: }
89: #ifdef RTLD_PRELOAD_ENABLE_VAR
90: if (strncmp(envp[env_len], RTLD_PRELOAD_ENABLE_VAR "=", sizeof(RTLD_PRELOAD_ENABLE_VAR)) == 0) {
91: enabled = true;
92: continue;
93: }
94: #endif
95: }
96:
97: /* Make a new copy of envp as needed. */
98: env_size = env_len + 1 + (preload_idx == -1);
99: #ifdef RTLD_PRELOAD_ENABLE_VAR
100: if (!enabled)
101: env_size++;
102: #endif
103: nenvp = emalloc2(env_size, sizeof(*envp));
104: memcpy(nenvp, envp, env_len * sizeof(*envp));
105: nenvp[env_len] = NULL;
106:
107: /* Prepend our LD_PRELOAD to existing value or add new entry at the end. */
108: if (preload_idx == -1) {
109: # ifdef RTLD_PRELOAD_DEFAULT
110: easprintf(&preload, "%s=%s%s%s", RTLD_PRELOAD_VAR, sudo_conf_noexec_path(), RTLD_PRELOAD_DELIM, RTLD_PRELOAD_DEFAULT);
111: # else
112: preload = fmt_string(RTLD_PRELOAD_VAR, sudo_conf_noexec_path());
113: # endif
114: if (preload == NULL)
1.1.1.4 misho 115: fatal(NULL);
1.1 misho 116: nenvp[env_len++] = preload;
117: nenvp[env_len] = NULL;
118: } else {
119: easprintf(&preload, "%s=%s%s%s", RTLD_PRELOAD_VAR, sudo_conf_noexec_path(), RTLD_PRELOAD_DELIM, nenvp[preload_idx]);
120: nenvp[preload_idx] = preload;
121: }
122: # ifdef RTLD_PRELOAD_ENABLE_VAR
123: if (!enabled) {
124: nenvp[env_len++] = RTLD_PRELOAD_ENABLE_VAR "=";
125: nenvp[env_len] = NULL;
126: }
127: # endif
128:
129: /* Install new env pointer. */
130: envp = nenvp;
131: #endif /* _PATH_SUDO_NOEXEC */
132:
1.1.1.5 ! misho 133: debug_return_const_ptr(envp);
1.1 misho 134: }
135:
136: /*
137: * Like execve(2) but falls back to running through /bin/sh
138: * ala execvp(3) if we get ENOEXEC.
139: */
140: int
1.1.1.5 ! misho 141: sudo_execve(const char *path, char *const argv[], char *const envp[], bool noexec)
1.1 misho 142: {
143: /* Modify the environment as needed to disable further execve(). */
144: if (noexec)
145: envp = disable_execute(envp);
146:
147: execve(path, argv, envp);
148: if (errno == ENOEXEC) {
149: int argc;
150: char **nargv;
151:
152: for (argc = 0; argv[argc] != NULL; argc++)
153: continue;
154: nargv = emalloc2(argc + 2, sizeof(char *));
155: nargv[0] = "sh";
156: nargv[1] = (char *)path;
157: memcpy(nargv + 2, argv + 1, argc * sizeof(char *));
158: execve(_PATH_BSHELL, nargv, envp);
159: efree(nargv);
160: }
161: return -1;
162: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>