Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1 | /* libminijailpreload.c - preload hack library |
| 2 | * Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | * |
| 6 | * This library is preloaded into every program launched by minijail_run(). |
| 7 | * DO NOT EXPORT ANY SYMBOLS FROM THIS LIBRARY. They will replace other symbols |
| 8 | * in the programs it is preloaded into and cause impossible-to-debug failures. |
| 9 | * See the minijail0.1 for a design explanation. */ |
| 10 | |
| 11 | #include "libminijail.h" |
| 12 | #include "libminijail-private.h" |
| 13 | |
| 14 | #include <dlfcn.h> |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 15 | #include <stdio.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <syslog.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | static int (*real_main)(int, char **, char **) = NULL; |
| 23 | static void *libc_handle = NULL; |
| 24 | |
| 25 | static void die(const char *failed) { |
| 26 | syslog(LOG_ERR, "libminijail: %s", failed); |
| 27 | abort(); |
| 28 | } |
| 29 | |
| 30 | static void unset_in_env(char **envp, const char *name) { |
| 31 | int i; |
| 32 | for (i = 0; envp[i]; i++) |
| 33 | if (!strncmp(envp[i], name, strlen(name))) |
| 34 | envp[i][0] = '\0'; |
| 35 | } |
| 36 | |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 37 | /** @brief Fake main(), spliced in before the real call to main() by |
| 38 | * __libc_start_main (see below). |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 39 | * We get serialized commands from our invoking process over an fd specified |
| 40 | * by an environment variable (kFdEnvVar). The environment variable is a list |
| 41 | * of key=value pairs (see move_commands_to_env); we use them to construct a |
| 42 | * jail, then enter it. |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 43 | */ |
| 44 | static int fake_main(int argc, char **argv, char **envp) { |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 45 | char *fd_name = getenv(kFdEnvVar); |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 46 | int fd = -1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 47 | struct minijail *j; |
| 48 | if (geteuid() != getuid() || getegid() != getgid()) |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 49 | /* If we didn't do this check, an attacker could set kFdEnvVar for |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 50 | * any setuid program that uses libminijail to cause it to get capabilities |
| 51 | * or a uid it did not expect. */ |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 52 | /* TODO(wad) why would libminijail interact here? */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 53 | return MINIJAIL_ERR_PRELOAD; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 54 | if (!fd_name) |
| 55 | return MINIJAIL_ERR_PRELOAD; |
| 56 | fd = atoi(fd_name); |
| 57 | if (fd < 0) |
| 58 | return MINIJAIL_ERR_PRELOAD; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 59 | |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 60 | j = minijail_new(); |
| 61 | if (!j) |
| 62 | die("preload: out of memory"); |
Will Drewry | fe4a372 | 2011-09-16 14:50:50 -0500 | [diff] [blame^] | 63 | if (minijail_from_fd(fd, j)) |
| 64 | die("preload: failed to parse minijail from parent"); |
| 65 | close(fd); |
| 66 | |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 67 | /* TODO(ellyjones): this trashes existing preloads, so one can't do: |
| 68 | * LD_PRELOAD="/tmp/test.so libminijailpreload.so" prog; the descendants of |
| 69 | * prog will have no LD_PRELOAD set at all. */ |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 70 | unset_in_env(envp, kLdPreloadEnvVar); |
Will Drewry | fe4a372 | 2011-09-16 14:50:50 -0500 | [diff] [blame^] | 71 | /* Strip out flags meant for the parent. */ |
| 72 | minijail_preenter(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 73 | minijail_enter(j); |
| 74 | minijail_destroy(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 75 | dlclose(libc_handle); |
| 76 | return real_main(argc, argv, envp); |
| 77 | } |
| 78 | |
| 79 | /** @brief LD_PRELOAD override of __libc_start_main. |
| 80 | * |
| 81 | * It is really best if you do not look too closely at this function. |
| 82 | * We need to ensure that some of our code runs before the target program (see |
| 83 | * the minijail0.1 file in this directory for high-level details about this), and |
| 84 | * the only available place to hook is this function, which is normally |
| 85 | * responsible for calling main(). Our LD_PRELOAD will overwrite the real |
| 86 | * __libc_start_main with this one, so we have to look up the real one from |
| 87 | * libc and invoke it with a pointer to the fake main() we'd like to run before |
| 88 | * the real main(). We can't just run our setup code *here* because |
| 89 | * __libc_start_main is responsible for setting up the C runtime environment, |
| 90 | * so we can't rely on things like malloc() being available yet. |
| 91 | */ |
| 92 | |
| 93 | int __libc_start_main(int (*main) (int, char **, char **), |
| 94 | int argc, char ** ubp_av, void (*init) (void), |
| 95 | void (*fini) (void), void (*rtld_fini) (void), |
| 96 | void (* stack_end)) { |
| 97 | void *sym; |
| 98 | /* This hack is unfortunately required by C99 - casting directly from void* to |
| 99 | * function pointers is left undefined. See POSIX.1-2003, the Rationale for |
| 100 | * the specification of dlsym(), and dlsym(3). This deliberately violates |
| 101 | * strict-aliasing rules, but gcc can't tell. */ |
| 102 | union { |
| 103 | int (*fn)(int (*main) (int, char **, char **), int argc, |
| 104 | char **ubp_av, void (*init) (void), void (*fini) (void), |
| 105 | void (*rtld_fini) (void), void (* stack_end)); |
| 106 | void *symval; |
| 107 | } real_libc_start_main; |
| 108 | |
| 109 | /* We hold this handle for the duration of the real __libc_start_main() and |
| 110 | * drop it just before calling the real main(). */ |
| 111 | libc_handle = dlopen("libc.so.6", RTLD_NOW); |
| 112 | |
| 113 | if (!libc_handle) { |
| 114 | syslog(LOG_ERR, "can't dlopen() libc"); |
| 115 | /* We dare not use abort() here because it will run atexit() handlers and |
| 116 | * try to flush stdio. */ |
| 117 | _exit(1); |
| 118 | } |
| 119 | sym = dlsym(libc_handle, "__libc_start_main"); |
| 120 | if (!sym) { |
| 121 | syslog(LOG_ERR, "can't find the real __libc_start_main()"); |
| 122 | _exit(1); |
| 123 | } |
| 124 | real_libc_start_main.symval = sym; |
| 125 | real_main = main; |
| 126 | |
| 127 | /* Note that we swap fake_main in for main - fake_main knows that it should |
| 128 | * call real_main after it's done. */ |
| 129 | return real_libc_start_main.fn(fake_main, argc, ubp_av, init, fini, rtld_fini, |
| 130 | stack_end); |
| 131 | } |