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> |
11 #include <link.h> | 11 #include <link.h> |
12 #include <signal.h> | 12 #include <signal.h> |
13 #include <stdio.h> | 13 #include <stdio.h> |
14 #include <stdlib.h> | 14 #include <stdlib.h> |
15 #include <sys/socket.h> | 15 #include <sys/socket.h> |
16 #include <sys/stat.h> | 16 #include <sys/stat.h> |
17 #include <sys/types.h> | 17 #include <sys/types.h> |
18 | 18 |
19 #include <string> | 19 #include <string> |
20 #include <vector> | 20 #include <vector> |
21 | 21 |
22 #include "base/at_exit.h" | 22 #include "base/at_exit.h" |
23 #include "base/command_line.h" | 23 #include "base/command_line.h" |
24 #include "base/logging.h" | 24 #include "base/logging.h" |
25 #include "base/message_loop/message_loop.h" | 25 #include "base/message_loop/message_loop.h" |
26 #include "base/posix/eintr_wrapper.h" | 26 #include "base/posix/eintr_wrapper.h" |
27 #include "base/posix/global_descriptors.h" | 27 #include "base/posix/global_descriptors.h" |
28 #include "base/posix/unix_domain_socket_linux.h" | 28 #include "base/posix/unix_domain_socket_linux.h" |
29 #include "base/process/kill.h" | 29 #include "base/process/kill.h" |
| 30 #include "base/process/process_handle.h" |
30 #include "base/rand_util.h" | 31 #include "base/rand_util.h" |
31 #include "components/nacl/common/nacl_switches.h" | 32 #include "components/nacl/common/nacl_switches.h" |
32 #include "components/nacl/loader/nacl_listener.h" | 33 #include "components/nacl/loader/nacl_listener.h" |
33 #include "components/nacl/loader/nacl_sandbox_linux.h" | 34 #include "components/nacl/loader/nacl_sandbox_linux.h" |
34 #include "content/public/common/zygote_fork_delegate_linux.h" | 35 #include "content/public/common/zygote_fork_delegate_linux.h" |
35 #include "crypto/nss_util.h" | 36 #include "crypto/nss_util.h" |
36 #include "ipc/ipc_descriptors.h" | 37 #include "ipc/ipc_descriptors.h" |
37 #include "ipc/ipc_switches.h" | 38 #include "ipc/ipc_switches.h" |
38 #include "sandbox/linux/services/libc_urandom_override.h" | 39 #include "sandbox/linux/services/libc_urandom_override.h" |
39 | 40 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 listener.set_uses_nonsfi_mode(uses_nonsfi_mode); | 105 listener.set_uses_nonsfi_mode(uses_nonsfi_mode); |
105 listener.set_prereserved_sandbox_size(system_info.prereserved_sandbox_size); | 106 listener.set_prereserved_sandbox_size(system_info.prereserved_sandbox_size); |
106 listener.set_number_of_cores(system_info.number_of_cores); | 107 listener.set_number_of_cores(system_info.number_of_cores); |
107 listener.Listen(); | 108 listener.Listen(); |
108 _exit(0); | 109 _exit(0); |
109 } | 110 } |
110 | 111 |
111 // Start the NaCl loader in a child created by the NaCl loader Zygote. | 112 // Start the NaCl loader in a child created by the NaCl loader Zygote. |
112 void ChildNaClLoaderInit(const std::vector<int>& child_fds, | 113 void ChildNaClLoaderInit(const std::vector<int>& child_fds, |
113 const NaClLoaderSystemInfo& system_info, | 114 const NaClLoaderSystemInfo& system_info, |
114 bool uses_nonsfi_mode) { | 115 bool uses_nonsfi_mode, |
| 116 const std::string& channel_id) { |
115 const int parent_fd = child_fds[content::ZygoteForkDelegate::kParentFDIndex]; | 117 const int parent_fd = child_fds[content::ZygoteForkDelegate::kParentFDIndex]; |
116 const int dummy_fd = child_fds[content::ZygoteForkDelegate::kDummyFDIndex]; | 118 const int dummy_fd = child_fds[content::ZygoteForkDelegate::kDummyFDIndex]; |
| 119 |
117 bool validack = false; | 120 bool validack = false; |
118 const size_t kMaxReadSize = 1024; | 121 base::ProcessId real_pid; |
119 char buffer[kMaxReadSize]; | |
120 // Wait until the parent process has discovered our PID. We | 122 // Wait until the parent process has discovered our PID. We |
121 // should not fork any child processes (which the seccomp | 123 // should not fork any child processes (which the seccomp |
122 // sandbox does) until then, because that can interfere with the | 124 // sandbox does) until then, because that can interfere with the |
123 // parent's discovery of our PID. | 125 // parent's discovery of our PID. |
124 const ssize_t nread = HANDLE_EINTR(read(parent_fd, buffer, kMaxReadSize)); | 126 const ssize_t nread = |
| 127 HANDLE_EINTR(read(parent_fd, &real_pid, sizeof(real_pid))); |
| 128 if (static_cast<size_t>(nread) == sizeof(real_pid)) { |
| 129 // Make sure the parent didn't accidentally send us our real PID. |
| 130 // We don't want it to be discoverable anywhere in our address space |
| 131 // when we start running untrusted code. |
| 132 CHECK(real_pid == 0); |
125 | 133 |
126 if (nread < 0) { | 134 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
127 perror("read"); | 135 switches::kProcessChannelID, channel_id); |
| 136 validack = true; |
| 137 } else { |
| 138 if (nread < 0) |
| 139 perror("read"); |
128 LOG(ERROR) << "read returned " << nread; | 140 LOG(ERROR) << "read returned " << nread; |
129 } else if (nread > 0) { | |
130 VLOG(1) << "NaCl loader is synchronised with Chrome zygote"; | |
131 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
132 switches::kProcessChannelID, std::string(buffer, nread)); | |
133 validack = true; | |
134 } | 141 } |
| 142 |
135 if (IGNORE_EINTR(close(dummy_fd)) != 0) | 143 if (IGNORE_EINTR(close(dummy_fd)) != 0) |
136 LOG(ERROR) << "close(dummy_fd) failed"; | 144 LOG(ERROR) << "close(dummy_fd) failed"; |
137 if (IGNORE_EINTR(close(parent_fd)) != 0) | 145 if (IGNORE_EINTR(close(parent_fd)) != 0) |
138 LOG(ERROR) << "close(parent_fd) failed"; | 146 LOG(ERROR) << "close(parent_fd) failed"; |
139 if (validack) { | 147 if (validack) { |
140 BecomeNaClLoader(child_fds, system_info, uses_nonsfi_mode); | 148 BecomeNaClLoader(child_fds, system_info, uses_nonsfi_mode); |
141 } else { | 149 } else { |
142 LOG(ERROR) << "Failed to synch with zygote"; | 150 LOG(ERROR) << "Failed to synch with zygote"; |
143 } | 151 } |
144 _exit(1); | 152 _exit(1); |
145 } | 153 } |
146 | 154 |
147 // Handle a fork request from the Zygote. | 155 // Handle a fork request from the Zygote. |
148 // Some of this code was lifted from | 156 // Some of this code was lifted from |
149 // content/browser/zygote_main_linux.cc:ForkWithRealPid() | 157 // content/browser/zygote_main_linux.cc:ForkWithRealPid() |
150 bool HandleForkRequest(const std::vector<int>& child_fds, | 158 bool HandleForkRequest(const std::vector<int>& child_fds, |
151 const NaClLoaderSystemInfo& system_info, | 159 const NaClLoaderSystemInfo& system_info, |
152 PickleIterator* input_iter, | 160 PickleIterator* input_iter, |
153 Pickle* output_pickle) { | 161 Pickle* output_pickle) { |
154 bool uses_nonsfi_mode; | 162 bool uses_nonsfi_mode; |
155 if (!input_iter->ReadBool(&uses_nonsfi_mode)) { | 163 if (!input_iter->ReadBool(&uses_nonsfi_mode)) { |
156 LOG(ERROR) << "Could not read uses_nonsfi_mode status"; | 164 LOG(ERROR) << "Could not read uses_nonsfi_mode status"; |
157 return false; | 165 return false; |
158 } | 166 } |
159 | 167 |
| 168 std::string channel_id; |
| 169 if (!input_iter->ReadString(&channel_id)) { |
| 170 LOG(ERROR) << "Could not read channel_id string"; |
| 171 return false; |
| 172 } |
| 173 |
160 if (content::ZygoteForkDelegate::kNumPassedFDs != child_fds.size()) { | 174 if (content::ZygoteForkDelegate::kNumPassedFDs != child_fds.size()) { |
161 LOG(ERROR) << "nacl_helper: unexpected number of fds, got " | 175 LOG(ERROR) << "nacl_helper: unexpected number of fds, got " |
162 << child_fds.size(); | 176 << child_fds.size(); |
163 return false; | 177 return false; |
164 } | 178 } |
165 | 179 |
166 VLOG(1) << "nacl_helper: forking"; | 180 VLOG(1) << "nacl_helper: forking"; |
167 pid_t child_pid = fork(); | 181 pid_t child_pid = fork(); |
168 if (child_pid < 0) { | 182 if (child_pid < 0) { |
169 PLOG(ERROR) << "*** fork() failed."; | 183 PLOG(ERROR) << "*** fork() failed."; |
170 } | 184 } |
171 | 185 |
172 if (child_pid == 0) { | 186 if (child_pid == 0) { |
173 ChildNaClLoaderInit(child_fds, system_info, uses_nonsfi_mode); | 187 ChildNaClLoaderInit(child_fds, system_info, uses_nonsfi_mode, channel_id); |
174 NOTREACHED(); | 188 NOTREACHED(); |
175 } | 189 } |
176 | 190 |
177 // I am the parent. | 191 // I am the parent. |
178 // First, close the dummy_fd so the sandbox won't find me when | 192 // First, close the dummy_fd so the sandbox won't find me when |
179 // looking for the child's pid in /proc. Also close other fds. | 193 // looking for the child's pid in /proc. Also close other fds. |
180 for (size_t i = 0; i < child_fds.size(); i++) { | 194 for (size_t i = 0; i < child_fds.size(); i++) { |
181 if (IGNORE_EINTR(close(child_fds[i])) != 0) | 195 if (IGNORE_EINTR(close(child_fds[i])) != 0) |
182 LOG(ERROR) << "close(child_fds[i]) failed"; | 196 LOG(ERROR) << "close(child_fds[i]) failed"; |
183 } | 197 } |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 // Now handle requests from the Zygote. | 430 // Now handle requests from the Zygote. |
417 while (true) { | 431 while (true) { |
418 bool request_handled = HandleZygoteRequest(kNaClZygoteDescriptor, | 432 bool request_handled = HandleZygoteRequest(kNaClZygoteDescriptor, |
419 system_info); | 433 system_info); |
420 // Do not turn this into a CHECK() without thinking about robustness | 434 // Do not turn this into a CHECK() without thinking about robustness |
421 // against malicious IPC requests. | 435 // against malicious IPC requests. |
422 DCHECK(request_handled); | 436 DCHECK(request_handled); |
423 } | 437 } |
424 NOTREACHED(); | 438 NOTREACHED(); |
425 } | 439 } |
OLD | NEW |