OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 // A mini-zygote specifically for Native Client. | 5 // A mini-zygote specifically for Native Client. |
6 | 6 |
7 #include "components/nacl/loader/nacl_helper_linux.h" | 7 #include "components/nacl/loader/nacl_helper_linux.h" |
8 | 8 |
9 #include <errno.h> | 9 #include <errno.h> |
10 #include <fcntl.h> | 10 #include <fcntl.h> |
(...skipping 29 matching lines...) Expand all Loading... |
40 | 40 |
41 struct NaClLoaderSystemInfo { | 41 struct NaClLoaderSystemInfo { |
42 size_t prereserved_sandbox_size; | 42 size_t prereserved_sandbox_size; |
43 long number_of_cores; | 43 long number_of_cores; |
44 }; | 44 }; |
45 | 45 |
46 // The child must mimic the behavior of zygote_main_linux.cc on the child | 46 // The child must mimic the behavior of zygote_main_linux.cc on the child |
47 // side of the fork. See zygote_main_linux.cc:HandleForkRequest from | 47 // side of the fork. See zygote_main_linux.cc:HandleForkRequest from |
48 // if (!child) { | 48 // if (!child) { |
49 void BecomeNaClLoader(const std::vector<int>& child_fds, | 49 void BecomeNaClLoader(const std::vector<int>& child_fds, |
50 const NaClLoaderSystemInfo& system_info) { | 50 const NaClLoaderSystemInfo& system_info, |
| 51 bool uses_nonsfi_mode) { |
51 VLOG(1) << "NaCl loader: setting up IPC descriptor"; | 52 VLOG(1) << "NaCl loader: setting up IPC descriptor"; |
52 // don't need zygote FD any more | 53 // don't need zygote FD any more |
53 if (IGNORE_EINTR(close(kNaClZygoteDescriptor)) != 0) | 54 if (IGNORE_EINTR(close(kNaClZygoteDescriptor)) != 0) |
54 LOG(ERROR) << "close(kNaClZygoteDescriptor) failed."; | 55 LOG(ERROR) << "close(kNaClZygoteDescriptor) failed."; |
55 bool sandbox_initialized = InitializeBPFSandbox(); | 56 bool sandbox_initialized = InitializeBPFSandbox(); |
56 if (!sandbox_initialized) { | 57 if (!sandbox_initialized) { |
57 LOG(ERROR) << "Could not initialize NaCl's second " | 58 LOG(ERROR) << "Could not initialize NaCl's second " |
58 << "layer sandbox (seccomp-bpf)."; | 59 << "layer sandbox (seccomp-bpf)."; |
59 } | 60 } |
60 base::GlobalDescriptors::GetInstance()->Set( | 61 base::GlobalDescriptors::GetInstance()->Set( |
61 kPrimaryIPCChannel, | 62 kPrimaryIPCChannel, |
62 child_fds[content::ZygoteForkDelegate::kBrowserFDIndex]); | 63 child_fds[content::ZygoteForkDelegate::kBrowserFDIndex]); |
63 | 64 |
64 base::MessageLoopForIO main_message_loop; | 65 base::MessageLoopForIO main_message_loop; |
65 NaClListener listener; | 66 NaClListener listener; |
| 67 listener.set_uses_nonsfi_mode(uses_nonsfi_mode); |
66 listener.set_prereserved_sandbox_size(system_info.prereserved_sandbox_size); | 68 listener.set_prereserved_sandbox_size(system_info.prereserved_sandbox_size); |
67 listener.set_number_of_cores(system_info.number_of_cores); | 69 listener.set_number_of_cores(system_info.number_of_cores); |
68 listener.Listen(); | 70 listener.Listen(); |
69 _exit(0); | 71 _exit(0); |
70 } | 72 } |
71 | 73 |
72 // Start the NaCl loader in a child created by the NaCl loader Zygote. | 74 // Start the NaCl loader in a child created by the NaCl loader Zygote. |
73 void ChildNaClLoaderInit(const std::vector<int>& child_fds, | 75 void ChildNaClLoaderInit(const std::vector<int>& child_fds, |
74 const NaClLoaderSystemInfo& system_info) { | 76 const NaClLoaderSystemInfo& system_info, |
| 77 bool uses_nonsfi_mode) { |
75 const int parent_fd = child_fds[content::ZygoteForkDelegate::kParentFDIndex]; | 78 const int parent_fd = child_fds[content::ZygoteForkDelegate::kParentFDIndex]; |
76 const int dummy_fd = child_fds[content::ZygoteForkDelegate::kDummyFDIndex]; | 79 const int dummy_fd = child_fds[content::ZygoteForkDelegate::kDummyFDIndex]; |
77 bool validack = false; | 80 bool validack = false; |
78 const size_t kMaxReadSize = 1024; | 81 const size_t kMaxReadSize = 1024; |
79 char buffer[kMaxReadSize]; | 82 char buffer[kMaxReadSize]; |
80 // Wait until the parent process has discovered our PID. We | 83 // Wait until the parent process has discovered our PID. We |
81 // should not fork any child processes (which the seccomp | 84 // should not fork any child processes (which the seccomp |
82 // sandbox does) until then, because that can interfere with the | 85 // sandbox does) until then, because that can interfere with the |
83 // parent's discovery of our PID. | 86 // parent's discovery of our PID. |
84 const int nread = HANDLE_EINTR(read(parent_fd, buffer, kMaxReadSize)); | 87 const int nread = HANDLE_EINTR(read(parent_fd, buffer, kMaxReadSize)); |
(...skipping 11 matching lines...) Expand all Loading... |
96 switches::kProcessChannelID, | 99 switches::kProcessChannelID, |
97 std::string(&buffer[len], nread - len)); | 100 std::string(&buffer[len], nread - len)); |
98 validack = true; | 101 validack = true; |
99 } | 102 } |
100 } | 103 } |
101 if (IGNORE_EINTR(close(dummy_fd)) != 0) | 104 if (IGNORE_EINTR(close(dummy_fd)) != 0) |
102 LOG(ERROR) << "close(dummy_fd) failed"; | 105 LOG(ERROR) << "close(dummy_fd) failed"; |
103 if (IGNORE_EINTR(close(parent_fd)) != 0) | 106 if (IGNORE_EINTR(close(parent_fd)) != 0) |
104 LOG(ERROR) << "close(parent_fd) failed"; | 107 LOG(ERROR) << "close(parent_fd) failed"; |
105 if (validack) { | 108 if (validack) { |
106 BecomeNaClLoader(child_fds, system_info); | 109 BecomeNaClLoader(child_fds, system_info, uses_nonsfi_mode); |
107 } else { | 110 } else { |
108 LOG(ERROR) << "Failed to synch with zygote"; | 111 LOG(ERROR) << "Failed to synch with zygote"; |
109 } | 112 } |
110 _exit(1); | 113 _exit(1); |
111 } | 114 } |
112 | 115 |
113 // Handle a fork request from the Zygote. | 116 // Handle a fork request from the Zygote. |
114 // Some of this code was lifted from | 117 // Some of this code was lifted from |
115 // content/browser/zygote_main_linux.cc:ForkWithRealPid() | 118 // content/browser/zygote_main_linux.cc:ForkWithRealPid() |
116 bool HandleForkRequest(const std::vector<int>& child_fds, | 119 bool HandleForkRequest(const std::vector<int>& child_fds, |
117 const NaClLoaderSystemInfo& system_info, | 120 const NaClLoaderSystemInfo& system_info, |
| 121 PickleIterator* input_iter, |
118 Pickle* output_pickle) { | 122 Pickle* output_pickle) { |
| 123 bool uses_nonsfi_mode; |
| 124 if (!input_iter->ReadBool(&uses_nonsfi_mode)) { |
| 125 LOG(ERROR) << "Could not read uses_nonsfi_mode status"; |
| 126 return false; |
| 127 } |
| 128 |
119 if (content::ZygoteForkDelegate::kNumPassedFDs != child_fds.size()) { | 129 if (content::ZygoteForkDelegate::kNumPassedFDs != child_fds.size()) { |
120 LOG(ERROR) << "nacl_helper: unexpected number of fds, got " | 130 LOG(ERROR) << "nacl_helper: unexpected number of fds, got " |
121 << child_fds.size(); | 131 << child_fds.size(); |
122 return false; | 132 return false; |
123 } | 133 } |
124 | 134 |
125 VLOG(1) << "nacl_helper: forking"; | 135 VLOG(1) << "nacl_helper: forking"; |
126 pid_t child_pid = fork(); | 136 pid_t child_pid = fork(); |
127 if (child_pid < 0) { | 137 if (child_pid < 0) { |
128 PLOG(ERROR) << "*** fork() failed."; | 138 PLOG(ERROR) << "*** fork() failed."; |
129 } | 139 } |
130 | 140 |
131 if (child_pid == 0) { | 141 if (child_pid == 0) { |
132 ChildNaClLoaderInit(child_fds, system_info); | 142 ChildNaClLoaderInit(child_fds, system_info, uses_nonsfi_mode); |
133 NOTREACHED(); | 143 NOTREACHED(); |
134 } | 144 } |
135 | 145 |
136 // I am the parent. | 146 // I am the parent. |
137 // First, close the dummy_fd so the sandbox won't find me when | 147 // First, close the dummy_fd so the sandbox won't find me when |
138 // looking for the child's pid in /proc. Also close other fds. | 148 // looking for the child's pid in /proc. Also close other fds. |
139 for (size_t i = 0; i < child_fds.size(); i++) { | 149 for (size_t i = 0; i < child_fds.size(); i++) { |
140 if (IGNORE_EINTR(close(child_fds[i])) != 0) | 150 if (IGNORE_EINTR(close(child_fds[i])) != 0) |
141 LOG(ERROR) << "close(child_fds[i]) failed"; | 151 LOG(ERROR) << "close(child_fds[i]) failed"; |
142 } | 152 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 int command_type, | 204 int command_type, |
195 const std::vector<int>& attached_fds, | 205 const std::vector<int>& attached_fds, |
196 const NaClLoaderSystemInfo& system_info, | 206 const NaClLoaderSystemInfo& system_info, |
197 PickleIterator* input_iter) { | 207 PickleIterator* input_iter) { |
198 Pickle write_pickle; | 208 Pickle write_pickle; |
199 bool have_to_reply = false; | 209 bool have_to_reply = false; |
200 // Commands must write anything to send back to |write_pickle|. | 210 // Commands must write anything to send back to |write_pickle|. |
201 switch (command_type) { | 211 switch (command_type) { |
202 case nacl::kNaClForkRequest: | 212 case nacl::kNaClForkRequest: |
203 have_to_reply = HandleForkRequest(attached_fds, system_info, | 213 have_to_reply = HandleForkRequest(attached_fds, system_info, |
204 &write_pickle); | 214 input_iter, &write_pickle); |
205 break; | 215 break; |
206 case nacl::kNaClGetTerminationStatusRequest: | 216 case nacl::kNaClGetTerminationStatusRequest: |
207 have_to_reply = | 217 have_to_reply = |
208 HandleGetTerminationStatusRequest(input_iter, &write_pickle); | 218 HandleGetTerminationStatusRequest(input_iter, &write_pickle); |
209 break; | 219 break; |
210 default: | 220 default: |
211 LOG(ERROR) << "Unsupported command from Zygote"; | 221 LOG(ERROR) << "Unsupported command from Zygote"; |
212 return false; | 222 return false; |
213 } | 223 } |
214 if (!have_to_reply) | 224 if (!have_to_reply) |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 // Now handle requests from the Zygote. | 391 // Now handle requests from the Zygote. |
382 while (true) { | 392 while (true) { |
383 bool request_handled = HandleZygoteRequest(kNaClZygoteDescriptor, | 393 bool request_handled = HandleZygoteRequest(kNaClZygoteDescriptor, |
384 system_info); | 394 system_info); |
385 // Do not turn this into a CHECK() without thinking about robustness | 395 // Do not turn this into a CHECK() without thinking about robustness |
386 // against malicious IPC requests. | 396 // against malicious IPC requests. |
387 DCHECK(request_handled); | 397 DCHECK(request_handled); |
388 } | 398 } |
389 NOTREACHED(); | 399 NOTREACHED(); |
390 } | 400 } |
OLD | NEW |