Imported Upstream version 1.8.4p4
[debian/sudo] / src / exec_common.c
1 /*
2  * Copyright (c) 2009-2012 Todd C. Miller <Todd.Miller@courtesan.com>
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 <sys/param.h>
21 #include <stdio.h>
22 #ifdef STDC_HEADERS
23 # include <stdlib.h>
24 # include <stddef.h>
25 #else
26 # ifdef HAVE_STDLIB_H
27 #  include <stdlib.h>
28 # endif
29 #endif /* STDC_HEADERS */
30 #ifdef HAVE_STRING_H
31 # include <string.h>
32 #endif /* HAVE_STRING_H */
33 #ifdef HAVE_STRINGS_H
34 # include <strings.h>
35 #endif /* HAVE_STRINGS_H */
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif /* HAVE_UNISTD_H */
39 #ifdef HAVE_PRIV_SET
40 # include <priv.h>
41 #endif
42 #include <errno.h>
43
44 #include "sudo.h"
45 #include "sudo_exec.h"
46
47 /*
48  * Disable execution of child processes in the command we are about
49  * to run.  On systems with privilege sets, we can remove the exec
50  * privilege.  On other systems we use LD_PRELOAD and the like.
51  */
52 static char * const *
53 disable_execute(char *const envp[])
54 {
55 #ifdef _PATH_SUDO_NOEXEC
56     char * const *ev;
57     char *preload, **nenvp;
58     int env_len = 0, env_size = 128;
59 #endif /* _PATH_SUDO_NOEXEC */
60     debug_decl(disable_execute, SUDO_DEBUG_UTIL)
61
62 #ifdef HAVE_PRIV_SET
63     /* Solaris privileges, remove PRIV_PROC_EXEC post-execve. */
64     if (priv_set(PRIV_OFF, PRIV_LIMIT, "PRIV_PROC_EXEC", NULL) == 0)
65         debug_return_ptr(envp);
66     warning(_("unable to remove PRIV_PROC_EXEC from PRIV_LIMIT"));
67 #endif /* HAVE_PRIV_SET */
68
69 #ifdef _PATH_SUDO_NOEXEC
70     nenvp = emalloc2(env_size, sizeof(char *));
71
72     /*
73      * Preload a noexec file.  For a list of LD_PRELOAD-alikes, see
74      * http://www.fortran-2000.com/ArnaudRecipes/sharedlib.html
75      * XXX - need to support 32-bit and 64-bit variants
76      */
77 # if defined(__darwin__) || defined(__APPLE__)
78     nenvp[env_len++] = "DYLD_FORCE_FLAT_NAMESPACE=";
79     preload = fmt_string("DYLD_INSERT_LIBRARIES", sudo_conf_noexec_path());
80 # elif defined(__osf__) || defined(__sgi)
81     easprintf(&preload, "_RLD_LIST=%s:DEFAULT", sudo_conf_noexec_path());
82 # elif defined(_AIX)
83     preload = fmt_string("LDR_PRELOAD", sudo_conf_noexec_path());
84 # else
85     preload = fmt_string("LD_PRELOAD", sudo_conf_noexec_path());
86 # endif
87     if (preload == NULL)
88         errorx(1, _("unable to allocate memory"));
89     nenvp[env_len++] = preload;
90
91     for (ev = envp; *ev != NULL; ev++) {
92         /*
93          * Prune out existing preloaded libraries.
94          * XXX - should append to new value instead.
95          */
96 # if defined(__darwin__) || defined(__APPLE__)
97         if (strncmp(*ev, "DYLD_INSERT_LIBRARIES=", sizeof("DYLD_INSERT_LIBRARIES=") - 1) == 0)
98             continue;
99         if (strncmp(*ev, "DYLD_FORCE_FLAT_NAMESPACE=", sizeof("DYLD_INSERT_LIBRARIES=") - 1) == 0)
100             continue;
101 # elif defined(__osf__) || defined(__sgi)
102         if (strncmp(*ev, "_RLD_LIST=", sizeof("_RLD_LIST=") - 1) == 0)
103             continue;
104 # elif defined(_AIX)
105         if (strncmp(*ev, "LDR_PRELOAD=", sizeof("LDR_PRELOAD=") - 1) == 0)
106             continue;
107 # else
108         if (strncmp(*ev, "LD_PRELOAD=", sizeof("LD_PRELOAD=") - 1) == 0)
109             continue;
110 # endif
111         /* Need at least 2 slots for current element and a NULL. */
112         if (env_len + 2 > env_size) {
113             env_size += 128;
114             nenvp = erealloc3(nenvp, env_size, sizeof(char *));
115         }
116         nenvp[env_len++] = *ev;
117     }
118     nenvp[env_len] = NULL;
119     envp = nenvp;
120 #endif /* _PATH_SUDO_NOEXEC */
121
122     debug_return_ptr(envp);
123 }
124
125 /*
126  * Like execve(2) but falls back to running through /bin/sh
127  * ala execvp(3) if we get ENOEXEC.
128  */
129 int
130 sudo_execve(const char *path, char *const argv[], char *const envp[], int noexec)
131 {
132     /* Modify the environment as needed to disable further execve(). */
133     if (noexec)
134         envp = disable_execute(envp);
135
136     execve(path, argv, envp);
137     if (errno == ENOEXEC) {
138         int argc;
139         char **nargv;
140
141         for (argc = 0; argv[argc] != NULL; argc++)
142             continue;
143         nargv = emalloc2(argc + 2, sizeof(char *));
144         nargv[0] = "sh";
145         nargv[1] = (char *)path;
146         memcpy(nargv + 2, argv + 1, argc * sizeof(char *));
147         execve(_PATH_BSHELL, nargv, envp);
148         efree(nargv);
149     }
150     return -1;
151 }