Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: chrome/app/chrome_exe_main_mac.cc

Issue 2891933005: Plumb sandbox rules through the helper executable. (Closed)
Patch Set: Address review feedback Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/app/chrome_exe_main_mac.c ('k') | sandbox/mac/sandbox_mac_seatbelt_exec_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // The entry point for all Mac Chromium processes, including the outer app 5 // The entry point for all Mac Chromium processes, including the outer app
6 // bundle (browser) and helper app (renderer, plugin, and friends). 6 // bundle (browser) and helper app (renderer, plugin, and friends).
7 7
8 #include <dlfcn.h> 8 #include <dlfcn.h>
9 #include <errno.h> 9 #include <errno.h>
10 #include <libgen.h> 10 #include <libgen.h>
11 #include <mach-o/dyld.h> 11 #include <mach-o/dyld.h>
12 #include <stddef.h> 12 #include <stddef.h>
13 #include <stdint.h> 13 #include <stdint.h>
14 #include <stdio.h> 14 #include <stdio.h>
15 #include <stdlib.h> 15 #include <stdlib.h>
16 #include <string.h> 16 #include <string.h>
17 #include <unistd.h> 17 #include <unistd.h>
18 18
19 #include <string>
20 #include <vector>
21
19 #include "chrome/common/chrome_version.h" 22 #include "chrome/common/chrome_version.h"
20 23
24 #if defined(HELPER_EXECUTABLE)
25 #include "sandbox/mac/seatbelt_exec.h"
26 #endif // defined(HELPER_EXECUTABLE)
27
28 extern char **environ;
29
30 namespace {
31
21 typedef int (*ChromeMainPtr)(int, char**); 32 typedef int (*ChromeMainPtr)(int, char**);
22 33
34 #if defined(HELPER_EXECUTABLE)
35 // The name of the parameter containing the executable path.
36 constexpr char exec_param[] = "EXECUTABLE_PATH";
37 // The name of the parameter containing the PID of Chrome.
38 constexpr char pid_param[] = "CHROMIUM_PID";
39 // The command line parameter to engage the v2 sandbox.
40 constexpr char v2_sandbox_arg[] = "--v2-sandbox";
41 // The command line parameter for the file descriptor used to receive the
42 // sandbox policy.
43 constexpr char fd_mapping_arg[] = "--fd_mapping=";
44
45 int SandboxExec(int argc, char* argv[], int fd_mapping) {
46 char rp[MAXPATHLEN];
47 if (realpath(argv[0], rp) == NULL)
48 abort();
49
50 sandbox::SeatbeltExecServer server(fd_mapping);
51
52 if (!server.SetParameter(exec_param, rp) ||
53 !server.SetParameter(pid_param, std::to_string(getpid()))) {
54 fprintf(stderr, "Failed to set up parameters for sandbox.\n");
55 return -1;
Mark Mentovai 2017/05/23 20:41:28 This isn’t right for something that’s going to be
Mark Mentovai 2017/05/23 20:41:29 I also don’t see any rhyme or reason to when you d
Greg K 2017/05/23 23:31:29 Yeah that was an oversight. Everything should just
56 }
57
58 if (server.InitializeSandbox() != 0)
59 abort();
60
61 std::vector<char*> new_argv;
62 for (int i = 1; i < argc; ++i) {
63 if (strcmp(argv[i], v2_sandbox_arg) != 0 &&
64 strncmp(argv[i], fd_mapping_arg, strlen(fd_mapping_arg)) != 0) {
65 new_argv.push_back(argv[i]);
66 }
67 }
68 new_argv.push_back(nullptr);
69
70 // The helper executable re-executes itself under the sandbox.
71 execve(argv[0], new_argv.data(), environ);
Mark Mentovai 2017/05/23 20:41:29 Instead of declaring environ and calling execve(),
Mark Mentovai 2017/05/23 20:41:29 NSGetExecutablePath() would be better than argv[0]
Greg K 2017/05/23 23:31:29 Done.
Greg K 2017/05/23 23:31:29 Done.
72 perror("execve");
73 return 1;
74 }
75 #endif // defined(HELPER_EXECUTABLE)
76
77 } // namespace
78
23 __attribute__((visibility("default"))) int main(int argc, char* argv[]) { 79 __attribute__((visibility("default"))) int main(int argc, char* argv[]) {
24 #if defined(HELPER_EXECUTABLE) 80 #if defined(HELPER_EXECUTABLE)
81 bool enable_v2_sandbox = false;
82 int fd_mapping = -1;
83 for (int i = 1; i < argc; i++) {
84 if (strcmp(argv[i], v2_sandbox_arg) == 0) {
85 enable_v2_sandbox = true;
86 } else if (strncmp(argv[i], fd_mapping_arg, strlen(fd_mapping_arg)) == 0) {
87 // Parse --fd_mapping=X to get the file descriptor X.
88 std::string arg(argv[i]);
89 std::string fd_str = arg.substr(arg.find("=") + 1, arg.length());
Mark Mentovai 2017/05/23 20:41:28 You don’t need to find the = now. You know where i
Greg K 2017/05/23 23:31:29 Done.
90 fd_mapping = std::stoi(fd_str);
91 }
92 }
93 if (enable_v2_sandbox && fd_mapping == -1) {
94 fprintf(stderr, "Must pass a valid file descriptor to --fd_mapping.\n");
95 return -1;
Mark Mentovai 2017/05/23 20:41:29 -1’s not right here either.
Greg K 2017/05/23 23:31:29 Done.
96 }
97
98 if (enable_v2_sandbox)
99 return SandboxExec(argc, argv, fd_mapping);
100
25 const char* const rel_path = 101 const char* const rel_path =
26 "../../../" PRODUCT_FULLNAME_STRING 102 "../../../" PRODUCT_FULLNAME_STRING
27 " Framework.framework/" PRODUCT_FULLNAME_STRING " Framework"; 103 " Framework.framework/" PRODUCT_FULLNAME_STRING " Framework";
28 #else 104 #else
29 const char* const rel_path = 105 const char* const rel_path =
30 "../Versions/" CHROME_VERSION_STRING "/" PRODUCT_FULLNAME_STRING 106 "../Versions/" CHROME_VERSION_STRING "/" PRODUCT_FULLNAME_STRING
31 " Framework.framework/" PRODUCT_FULLNAME_STRING " Framework"; 107 " Framework.framework/" PRODUCT_FULLNAME_STRING " Framework";
32 #endif // defined(HELPER_EXECUTABLE) 108 #endif // defined(HELPER_EXECUTABLE)
33 109
34 uint32_t exec_path_size = 0; 110 uint32_t exec_path_size = 0;
35 int rv = _NSGetExecutablePath(NULL, &exec_path_size); 111 int rv = _NSGetExecutablePath(NULL, &exec_path_size);
36 if (rv != -1) { 112 if (rv != -1) {
37 fprintf(stderr, "_NSGetExecutablePath: get length failed\n"); 113 fprintf(stderr, "_NSGetExecutablePath: get length failed\n");
38 abort(); 114 abort();
39 } 115 }
40 116
41 char* exec_path = malloc(exec_path_size); 117 char* exec_path = new char[exec_path_size];
42 if (!exec_path) {
43 fprintf(stderr, "malloc %u: %s\n", exec_path_size, strerror(errno));
44 abort();
45 }
46
47 rv = _NSGetExecutablePath(exec_path, &exec_path_size); 118 rv = _NSGetExecutablePath(exec_path, &exec_path_size);
48 if (rv != 0) { 119 if (rv != 0) {
49 fprintf(stderr, "_NSGetExecutablePath: get path failed\n"); 120 fprintf(stderr, "_NSGetExecutablePath: get path failed\n");
50 abort(); 121 abort();
51 } 122 }
52 123
53 // Slice off the last part of the main executable path, and append the 124 // Slice off the last part of the main executable path, and append the
54 // version framework information. 125 // version framework information.
55 const char* parent_dir = dirname(exec_path); 126 const char* parent_dir = dirname(exec_path);
56 if (!parent_dir) { 127 if (!parent_dir) {
57 fprintf(stderr, "dirname %s: %s\n", exec_path, strerror(errno)); 128 fprintf(stderr, "dirname %s: %s\n", exec_path, strerror(errno));
58 abort(); 129 abort();
59 } 130 }
60 free(exec_path); 131 delete[] exec_path;
61 132
62 const size_t parent_path_len = strlen(parent_dir); 133 const size_t parent_path_len = strlen(parent_dir);
63 const size_t rel_path_len = strlen(rel_path); 134 const size_t rel_path_len = strlen(rel_path);
64 // 2 accounts for a trailing NUL byte and the '/' in the middle of the paths. 135 // 2 accounts for a trailing NUL byte and the '/' in the middle of the paths.
65 const size_t framework_path_size = parent_path_len + rel_path_len + 2; 136 const size_t framework_path_size = parent_path_len + rel_path_len + 2;
66 char* framework_path = malloc(framework_path_size); 137 char* framework_path = new char[framework_path_size];
67 if (!framework_path) {
68 fprintf(stderr, "malloc %zu: %s\n", framework_path_size, strerror(errno));
69 abort();
70 }
71 snprintf(framework_path, framework_path_size, "%s/%s", parent_dir, rel_path); 138 snprintf(framework_path, framework_path_size, "%s/%s", parent_dir, rel_path);
72 139
73 void* library = dlopen(framework_path, RTLD_LAZY | RTLD_LOCAL | RTLD_FIRST); 140 void* library = dlopen(framework_path, RTLD_LAZY | RTLD_LOCAL | RTLD_FIRST);
74 if (!library) { 141 if (!library) {
75 fprintf(stderr, "dlopen %s: %s\n", framework_path, dlerror()); 142 fprintf(stderr, "dlopen %s: %s\n", framework_path, dlerror());
76 abort(); 143 abort();
77 } 144 }
78 free(framework_path); 145 delete[] framework_path;
79 146
80 const ChromeMainPtr chrome_main = dlsym(library, "ChromeMain"); 147 const ChromeMainPtr chrome_main =
148 reinterpret_cast<ChromeMainPtr>(dlsym(library, "ChromeMain"));
81 if (!chrome_main) { 149 if (!chrome_main) {
82 fprintf(stderr, "dlsym ChromeMain: %s\n", dlerror()); 150 fprintf(stderr, "dlsym ChromeMain: %s\n", dlerror());
83 abort(); 151 abort();
84 } 152 }
85 rv = chrome_main(argc, argv); 153 rv = chrome_main(argc, argv);
86 154
87 // exit, don't return from main, to avoid the apparent removal of main from 155 // exit, don't return from main, to avoid the apparent removal of main from
Mark Mentovai 2017/05/23 20:41:29 Shouldn’t this apply to the other early returns in
Greg K 2017/05/23 23:31:30 Yes although all the early returns should actually
88 // stack backtraces under tail call optimization. 156 // stack backtraces under tail call optimization.
89 exit(rv); 157 exit(rv);
90 } 158 }
OLDNEW
« no previous file with comments | « chrome/app/chrome_exe_main_mac.c ('k') | sandbox/mac/sandbox_mac_seatbelt_exec_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698