blob: a98b736d08e07bda9be67c5ef237149cf1146fd2 [file] [log] [blame]
Elly Jonescd7a9042011-07-22 13:56:51 -04001/* 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.
Elly Jonese1749eb2011-10-07 13:54:59 -04009 * See the minijail0.1 for a design explanation.
10 */
Elly Jonescd7a9042011-07-22 13:56:51 -040011
12#include "libminijail.h"
13#include "libminijail-private.h"
14
15#include <dlfcn.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050016#include <stdio.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040017#include <stdlib.h>
18#include <string.h>
19#include <sys/types.h>
20#include <syslog.h>
21#include <unistd.h>
22
Elly Jonese1749eb2011-10-07 13:54:59 -040023static int (*real_main) (int, char **, char **);
24static void *libc_handle;
Elly Jonescd7a9042011-07-22 13:56:51 -040025
Elly Jonese1749eb2011-10-07 13:54:59 -040026static void die(const char *failed)
27{
28 syslog(LOG_ERR, "libminijail: %s", failed);
29 abort();
Elly Jonescd7a9042011-07-22 13:56:51 -040030}
31
Elly Jonese1749eb2011-10-07 13:54:59 -040032static void unset_in_env(char **envp, const char *name)
33{
34 int i;
35 for (i = 0; envp[i]; i++)
36 if (!strncmp(envp[i], name, strlen(name)))
37 envp[i][0] = '\0';
Elly Jonescd7a9042011-07-22 13:56:51 -040038}
39
Elly Jonescd7a9042011-07-22 13:56:51 -040040/** @brief Fake main(), spliced in before the real call to main() by
41 * __libc_start_main (see below).
Will Drewry2f54b6a2011-09-16 13:45:31 -050042 * We get serialized commands from our invoking process over an fd specified
43 * by an environment variable (kFdEnvVar). The environment variable is a list
44 * of key=value pairs (see move_commands_to_env); we use them to construct a
45 * jail, then enter it.
Elly Jonescd7a9042011-07-22 13:56:51 -040046 */
Elly Jonese1749eb2011-10-07 13:54:59 -040047static int fake_main(int argc, char **argv, char **envp)
48{
49 char *fd_name = getenv(kFdEnvVar);
50 int fd = -1;
51 struct minijail *j;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -040052 if (geteuid() != getuid() || getegid() != getgid()) {
53 /*
54 * If we didn't do this check, an attacker could set kFdEnvVar
Elly Jonese1749eb2011-10-07 13:54:59 -040055 * for any setuid program that uses libminijail to cause it to
56 * get capabilities or a uid it did not expect.
57 */
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -040058 /* TODO(wad): why would libminijail interact here? */
Elly Jonese1749eb2011-10-07 13:54:59 -040059 return MINIJAIL_ERR_PRELOAD;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -040060 }
Elly Jonese1749eb2011-10-07 13:54:59 -040061 if (!fd_name)
62 return MINIJAIL_ERR_PRELOAD;
63 fd = atoi(fd_name);
64 if (fd < 0)
65 return MINIJAIL_ERR_PRELOAD;
Will Drewry2f54b6a2011-09-16 13:45:31 -050066
Elly Jonese1749eb2011-10-07 13:54:59 -040067 j = minijail_new();
68 if (!j)
69 die("preload: out of memory");
70 if (minijail_from_fd(fd, j))
71 die("preload: failed to parse minijail from parent");
72 close(fd);
Will Drewryfe4a3722011-09-16 14:50:50 -050073
Mike Frysinger35b95962020-08-22 02:59:12 -040074 unset_in_env(envp, kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -040075 /* TODO(ellyjones): this trashes existing preloads, so one can't do:
76 * LD_PRELOAD="/tmp/test.so libminijailpreload.so" prog; the
77 * descendants of prog will have no LD_PRELOAD set at all.
78 */
79 unset_in_env(envp, kLdPreloadEnvVar);
80 /* Strip out flags meant for the parent. */
81 minijail_preenter(j);
82 minijail_enter(j);
83 minijail_destroy(j);
84 dlclose(libc_handle);
85 return real_main(argc, argv, envp);
Elly Jonescd7a9042011-07-22 13:56:51 -040086}
87
88/** @brief LD_PRELOAD override of __libc_start_main.
89 *
Elly Jonese1749eb2011-10-07 13:54:59 -040090 * It is really best if you do not look too closely at this function. We need
91 * to ensure that some of our code runs before the target program (see the
92 * minijail0.1 file in this directory for high-level details about this), and
Elly Jonescd7a9042011-07-22 13:56:51 -040093 * the only available place to hook is this function, which is normally
94 * responsible for calling main(). Our LD_PRELOAD will overwrite the real
95 * __libc_start_main with this one, so we have to look up the real one from
96 * libc and invoke it with a pointer to the fake main() we'd like to run before
97 * the real main(). We can't just run our setup code *here* because
98 * __libc_start_main is responsible for setting up the C runtime environment,
99 * so we can't rely on things like malloc() being available yet.
100 */
101
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -0400102int API __libc_start_main(int (*main)(int, char **, char **), int argc,
103 char **ubp_av, void (*init)(void), void (*fini)(void),
104 void (*rtld_fini)(void), void(*stack_end))
Elly Jonese1749eb2011-10-07 13:54:59 -0400105{
106 void *sym;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -0400107 /*
108 * This hack is unfortunately required by C99 - casting directly from
Elly Jonese1749eb2011-10-07 13:54:59 -0400109 * void* to function pointers is left undefined. See POSIX.1-2003, the
110 * Rationale for the specification of dlsym(), and dlsym(3). This
111 * deliberately violates strict-aliasing rules, but gcc can't tell.
112 */
113 union {
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -0400114 int (*fn)(int (*main)(int, char **, char **), int argc,
115 char **ubp_av, void (*init)(void), void (*fini)(void),
116 void (*rtld_fini)(void), void(*stack_end));
Elly Jonese1749eb2011-10-07 13:54:59 -0400117 void *symval;
118 } real_libc_start_main;
Elly Jonescd7a9042011-07-22 13:56:51 -0400119
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -0400120 /*
121 * We hold this handle for the duration of the real __libc_start_main()
Elly Jonese1749eb2011-10-07 13:54:59 -0400122 * and drop it just before calling the real main().
123 */
124 libc_handle = dlopen("libc.so.6", RTLD_NOW);
Elly Jonescd7a9042011-07-22 13:56:51 -0400125
Elly Jonese1749eb2011-10-07 13:54:59 -0400126 if (!libc_handle) {
127 syslog(LOG_ERR, "can't dlopen() libc");
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -0400128 /*
129 * We dare not use abort() here because it will run atexit()
Elly Jonese1749eb2011-10-07 13:54:59 -0400130 * handlers and try to flush stdio.
131 */
132 _exit(1);
133 }
134 sym = dlsym(libc_handle, "__libc_start_main");
135 if (!sym) {
136 syslog(LOG_ERR, "can't find the real __libc_start_main()");
137 _exit(1);
138 }
139 real_libc_start_main.symval = sym;
140 real_main = main;
Elly Jonescd7a9042011-07-22 13:56:51 -0400141
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -0400142 /*
143 * Note that we swap fake_main in for main - fake_main knows that it
Elly Jonese1749eb2011-10-07 13:54:59 -0400144 * should call real_main after it's done.
145 */
146 return real_libc_start_main.fn(fake_main, argc, ubp_av, init, fini,
147 rtld_fini, stack_end);
Elly Jonescd7a9042011-07-22 13:56:51 -0400148}