Annotation of embedaddon/sudo/src/exec_common.c, revision 1.1.1.3

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. */
                     68:     if (priv_set(PRIV_OFF, PRIV_LIMIT, "PRIV_PROC_EXEC", NULL) == 0)
                     69:        debug_return_ptr(envp);
                     70:     warning(_("unable to remove PRIV_PROC_EXEC from PRIV_LIMIT"));
                     71: #endif /* HAVE_PRIV_SET */
                     72: 
                     73: #ifdef _PATH_SUDO_NOEXEC
                     74:     /*
                     75:      * Preload a noexec file.  For a list of LD_PRELOAD-alikes, see
                     76:      * http://www.fortran-2000.com/ArnaudRecipes/sharedlib.html
                     77:      * XXX - need to support 32-bit and 64-bit variants
                     78:      */
                     79: 
                     80:     /* Count entries in envp, looking for LD_PRELOAD as we go. */
                     81:     for (env_len = 0; envp[env_len] != NULL; env_len++) {
                     82:        if (strncmp(envp[env_len], RTLD_PRELOAD_VAR "=", sizeof(RTLD_PRELOAD_VAR)) == 0) {
                     83:            preload_idx = env_len;
                     84:            continue;
                     85:        }
                     86: #ifdef RTLD_PRELOAD_ENABLE_VAR
                     87:        if (strncmp(envp[env_len], RTLD_PRELOAD_ENABLE_VAR "=", sizeof(RTLD_PRELOAD_ENABLE_VAR)) == 0) {
                     88:            enabled = true;
                     89:            continue;
                     90:        }
                     91: #endif
                     92:     }
                     93: 
                     94:     /* Make a new copy of envp as needed. */
                     95:     env_size = env_len + 1 + (preload_idx == -1);
                     96: #ifdef RTLD_PRELOAD_ENABLE_VAR
                     97:     if (!enabled)
                     98:        env_size++;
                     99: #endif
                    100:     nenvp = emalloc2(env_size, sizeof(*envp));
                    101:     memcpy(nenvp, envp, env_len * sizeof(*envp));
                    102:     nenvp[env_len] = NULL;
                    103: 
                    104:     /* Prepend our LD_PRELOAD to existing value or add new entry at the end. */
                    105:     if (preload_idx == -1) {
                    106: # ifdef RTLD_PRELOAD_DEFAULT
                    107:        easprintf(&preload, "%s=%s%s%s", RTLD_PRELOAD_VAR, sudo_conf_noexec_path(), RTLD_PRELOAD_DELIM, RTLD_PRELOAD_DEFAULT);
                    108: # else
                    109:        preload = fmt_string(RTLD_PRELOAD_VAR, sudo_conf_noexec_path());
                    110: # endif
                    111:        if (preload == NULL)
1.1.1.3 ! misho     112:            fatalx(NULL);
1.1       misho     113:        nenvp[env_len++] = preload;
                    114:        nenvp[env_len] = NULL;
                    115:     } else {
                    116:        easprintf(&preload, "%s=%s%s%s", RTLD_PRELOAD_VAR, sudo_conf_noexec_path(), RTLD_PRELOAD_DELIM, nenvp[preload_idx]);
                    117:        nenvp[preload_idx] = preload;
                    118:     }
                    119: # ifdef RTLD_PRELOAD_ENABLE_VAR
                    120:     if (!enabled) {
                    121:        nenvp[env_len++] = RTLD_PRELOAD_ENABLE_VAR "=";
                    122:        nenvp[env_len] = NULL;
                    123:     }
                    124: # endif
                    125: 
                    126:     /* Install new env pointer. */
                    127:     envp = nenvp;
                    128: #endif /* _PATH_SUDO_NOEXEC */
                    129: 
                    130:     debug_return_ptr(envp);
                    131: }
                    132: 
                    133: /*
                    134:  * Like execve(2) but falls back to running through /bin/sh
                    135:  * ala execvp(3) if we get ENOEXEC.
                    136:  */
                    137: int
                    138: sudo_execve(const char *path, char *const argv[], char *const envp[], int noexec)
                    139: {
                    140:     /* Modify the environment as needed to disable further execve(). */
                    141:     if (noexec)
                    142:        envp = disable_execute(envp);
                    143: 
                    144:     execve(path, argv, envp);
                    145:     if (errno == ENOEXEC) {
                    146:        int argc;
                    147:        char **nargv;
                    148: 
                    149:        for (argc = 0; argv[argc] != NULL; argc++)
                    150:            continue;
                    151:        nargv = emalloc2(argc + 2, sizeof(char *));
                    152:        nargv[0] = "sh";
                    153:        nargv[1] = (char *)path;
                    154:        memcpy(nargv + 2, argv + 1, argc * sizeof(char *));
                    155:        execve(_PATH_BSHELL, nargv, envp);
                    156:        efree(nargv);
                    157:     }
                    158:     return -1;
                    159: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>