Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame^] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <errno.h> |
| 6 | #include <stdarg.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | #include <sys/mount.h> |
| 10 | #include <syslog.h> |
| 11 | #include <unistd.h> |
| 12 | |
| 13 | #include <chromeos/libminijail.h> |
| 14 | |
| 15 | #include "debugdaemon.h" |
| 16 | |
| 17 | const char *kHelpers[] = { |
| 18 | NULL, |
| 19 | }; |
| 20 | |
| 21 | void die(const char *fmt, ...) { |
| 22 | va_list ap; |
| 23 | va_start(ap, fmt); |
| 24 | vsyslog(LOG_ERR, fmt, ap); |
| 25 | va_end(ap); |
| 26 | exit(1); |
| 27 | } |
| 28 | |
| 29 | // @brief Enter a minijail. |
| 30 | // |
| 31 | // We need to be in a vfs namespace so that our tmpfs is only visible to us and |
| 32 | // our descendants, and we don't want to be root. Note that minijail_enter() |
| 33 | // exits the process if it can't succeed. |
| 34 | void enter_sandbox() { |
| 35 | static const char *kDebugdUser = "debugd"; |
| 36 | static const char *kDebugdGroup = "debugd"; |
| 37 | struct minijail *j = minijail_new(); |
| 38 | minijail_namespace_vfs(j); |
| 39 | minijail_change_user(j, kDebugdUser); |
| 40 | minijail_change_group(j, kDebugdGroup); |
| 41 | minijail_enter(j); |
| 42 | minijail_destroy(j); |
| 43 | } |
| 44 | |
| 45 | // @brief Sets up a tmpfs visible to this program and its descendants. |
| 46 | // |
| 47 | // The created tmpfs is mounted at /debugd. |
| 48 | void make_tmpfs() { |
| 49 | int r = mount("none", "/debugd", "tmpfs", MS_NODEV | MS_NOSUID | MS_NOEXEC, |
| 50 | NULL); |
| 51 | if (r < 0) |
| 52 | die("mount() failed: %s", strerror(errno)); |
| 53 | } |
| 54 | |
| 55 | // @brief Launch a single helper program. |
| 56 | // |
| 57 | // Helper programs are launched with no arguments. |
| 58 | void launch_one_helper(const char *progname) { |
| 59 | // execve() fails to declare the pointers inside its argument array as const, |
| 60 | // so we have to cast away constness here, even though argv does not touch |
| 61 | // them. |
| 62 | char *const argv[] = { const_cast<char *>(progname), NULL }; |
| 63 | int r = fork(); |
| 64 | if (r < 0) |
| 65 | die("forking helper %s failed: %s", progname, strerror(errno)); |
| 66 | if (r > 0) { |
| 67 | syslog(LOG_NOTICE, "forked helper %d for %s", r, progname); |
| 68 | return; |
| 69 | } |
| 70 | r = execve(progname, argv, environ); |
| 71 | syslog(LOG_ERR, "execing helper %s failed: %s", progname, strerror(errno)); |
| 72 | exit(r); |
| 73 | } |
| 74 | |
| 75 | // @brief Launch all our helper programs. |
| 76 | void launch_helpers() { |
| 77 | for (int i = 0; kHelpers[i]; i++) { |
| 78 | launch_one_helper(kHelpers[i]); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void start_dbus() { |
| 83 | // TODO(ellyjones): Implement this |
| 84 | } |
| 85 | |
| 86 | int __attribute__((visibility("default"))) main(int argc, char *argv[]) { |
| 87 | enter_sandbox(); |
| 88 | make_tmpfs(); |
| 89 | launch_helpers(); |
| 90 | start_dbus(); |
| 91 | return 0; |
| 92 | } |