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

Side by Side Diff: content/browser/zygote_host/zygote_host_impl_linux.cc

Issue 258893004: Use RecvMsgWithPid to find zygote PID instead of chrome-sandbox (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Respond to jln feedback Created 6 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | content/zygote/zygote_linux.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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "content/browser/zygote_host/zygote_host_impl_linux.h" 5 #include "content/browser/zygote_host/zygote_host_impl_linux.h"
6 6
7 #include <string.h>
7 #include <sys/socket.h> 8 #include <sys/socket.h>
8 #include <sys/stat.h> 9 #include <sys/stat.h>
9 #include <sys/types.h> 10 #include <sys/types.h>
10 #include <unistd.h> 11 #include <unistd.h>
11 12
12 #include "base/base_switches.h" 13 #include "base/base_switches.h"
13 #include "base/command_line.h" 14 #include "base/command_line.h"
14 #include "base/environment.h" 15 #include "base/environment.h"
15 #include "base/file_util.h" 16 #include "base/file_util.h"
16 #include "base/files/file_enumerator.h" 17 #include "base/files/file_enumerator.h"
17 #include "base/files/scoped_file.h" 18 #include "base/files/scoped_file.h"
18 #include "base/linux_util.h" 19 #include "base/linux_util.h"
19 #include "base/logging.h" 20 #include "base/logging.h"
20 #include "base/memory/linked_ptr.h" 21 #include "base/memory/linked_ptr.h"
21 #include "base/memory/scoped_ptr.h" 22 #include "base/memory/scoped_ptr.h"
22 #include "base/metrics/histogram.h" 23 #include "base/metrics/histogram.h"
23 #include "base/path_service.h" 24 #include "base/path_service.h"
24 #include "base/posix/eintr_wrapper.h" 25 #include "base/posix/eintr_wrapper.h"
25 #include "base/posix/unix_domain_socket_linux.h" 26 #include "base/posix/unix_domain_socket_linux.h"
26 #include "base/process/launch.h" 27 #include "base/process/launch.h"
27 #include "base/process/memory.h" 28 #include "base/process/memory.h"
29 #include "base/process/process_handle.h"
28 #include "base/strings/string_number_conversions.h" 30 #include "base/strings/string_number_conversions.h"
29 #include "base/strings/string_util.h" 31 #include "base/strings/string_util.h"
30 #include "base/strings/utf_string_conversions.h" 32 #include "base/strings/utf_string_conversions.h"
31 #include "base/time/time.h" 33 #include "base/time/time.h"
32 #include "content/browser/renderer_host/render_sandbox_host_linux.h" 34 #include "content/browser/renderer_host/render_sandbox_host_linux.h"
33 #include "content/common/child_process_sandbox_support_impl_linux.h" 35 #include "content/common/child_process_sandbox_support_impl_linux.h"
34 #include "content/common/zygote_commands_linux.h" 36 #include "content/common/zygote_commands_linux.h"
35 #include "content/public/browser/content_browser_client.h" 37 #include "content/public/browser/content_browser_client.h"
36 #include "content/public/common/content_switches.h" 38 #include "content/public/common/content_switches.h"
37 #include "content/public/common/result_codes.h" 39 #include "content/public/common/result_codes.h"
38 #include "sandbox/linux/suid/client/setuid_sandbox_client.h" 40 #include "sandbox/linux/suid/client/setuid_sandbox_client.h"
39 #include "sandbox/linux/suid/common/sandbox.h" 41 #include "sandbox/linux/suid/common/sandbox.h"
40 #include "ui/base/ui_base_switches.h" 42 #include "ui/base/ui_base_switches.h"
41 #include "ui/gfx/switches.h" 43 #include "ui/gfx/switches.h"
42 44
43 #if defined(USE_TCMALLOC) 45 #if defined(USE_TCMALLOC)
44 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h" 46 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
45 #endif 47 #endif
46 48
47 namespace content { 49 namespace content {
48 50
51 // Returns true if |proc| is the same process as or a descendent process of
52 // |ancestor|.
53 static bool SameOrDescendantOf(base::ProcessId proc, base::ProcessId ancestor) {
54 for (unsigned i = 0; i < 100; i++) {
55 if (proc == ancestor)
56 return true;
57
58 // Walk up process tree.
59 base::ProcessHandle handle;
60 CHECK(base::OpenProcessHandle(proc, &handle));
61 proc = base::GetParentProcessId(handle);
62 base::CloseProcessHandle(handle);
63 if (proc <= 0)
64 return false;
65 }
66
67 NOTREACHED();
68 return false;
69 }
70
49 // static 71 // static
50 ZygoteHost* ZygoteHost::GetInstance() { 72 ZygoteHost* ZygoteHost::GetInstance() {
51 return ZygoteHostImpl::GetInstance(); 73 return ZygoteHostImpl::GetInstance();
52 } 74 }
53 75
54 ZygoteHostImpl::ZygoteHostImpl() 76 ZygoteHostImpl::ZygoteHostImpl()
55 : control_fd_(-1), 77 : control_fd_(-1),
56 control_lock_(), 78 control_lock_(),
57 pid_(-1), 79 pid_(-1),
58 init_(false), 80 init_(false),
(...skipping 17 matching lines...) Expand all
76 init_ = true; 98 init_ = true;
77 99
78 base::FilePath chrome_path; 100 base::FilePath chrome_path;
79 CHECK(PathService::Get(base::FILE_EXE, &chrome_path)); 101 CHECK(PathService::Get(base::FILE_EXE, &chrome_path));
80 CommandLine cmd_line(chrome_path); 102 CommandLine cmd_line(chrome_path);
81 103
82 cmd_line.AppendSwitchASCII(switches::kProcessType, switches::kZygoteProcess); 104 cmd_line.AppendSwitchASCII(switches::kProcessType, switches::kZygoteProcess);
83 105
84 int fds[2]; 106 int fds[2];
85 CHECK(socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds) == 0); 107 CHECK(socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds) == 0);
108 CHECK(UnixDomainSocket::EnableReceiveProcessId(fds[0]));
86 base::FileHandleMappingVector fds_to_map; 109 base::FileHandleMappingVector fds_to_map;
87 fds_to_map.push_back(std::make_pair(fds[1], kZygoteSocketPairFd)); 110 fds_to_map.push_back(std::make_pair(fds[1], kZygoteSocketPairFd));
88 111
89 base::LaunchOptions options; 112 base::LaunchOptions options;
90 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess(); 113 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
91 if (browser_command_line.HasSwitch(switches::kZygoteCmdPrefix)) { 114 if (browser_command_line.HasSwitch(switches::kZygoteCmdPrefix)) {
92 cmd_line.PrependWrapper( 115 cmd_line.PrependWrapper(
93 browser_command_line.GetSwitchValueNative(switches::kZygoteCmdPrefix)); 116 browser_command_line.GetSwitchValueNative(switches::kZygoteCmdPrefix));
94 } 117 }
95 // Append any switches from the browser process that need to be forwarded on 118 // Append any switches from the browser process that need to be forwarded on
(...skipping 23 matching lines...) Expand all
119 sandbox_binary_ = sandbox_cmd.c_str(); 142 sandbox_binary_ = sandbox_cmd.c_str();
120 143
121 // A non empty sandbox_cmd means we want a SUID sandbox. 144 // A non empty sandbox_cmd means we want a SUID sandbox.
122 using_suid_sandbox_ = !sandbox_cmd.empty(); 145 using_suid_sandbox_ = !sandbox_cmd.empty();
123 146
124 // Start up the sandbox host process and get the file descriptor for the 147 // Start up the sandbox host process and get the file descriptor for the
125 // renderers to talk to it. 148 // renderers to talk to it.
126 const int sfd = RenderSandboxHostLinux::GetInstance()->GetRendererSocket(); 149 const int sfd = RenderSandboxHostLinux::GetInstance()->GetRendererSocket();
127 fds_to_map.push_back(std::make_pair(sfd, GetSandboxFD())); 150 fds_to_map.push_back(std::make_pair(sfd, GetSandboxFD()));
128 151
129 int dummy_fd = -1; 152 base::ScopedFD dummy_fd;
130 if (using_suid_sandbox_) { 153 if (using_suid_sandbox_) {
131 scoped_ptr<sandbox::SetuidSandboxClient> 154 scoped_ptr<sandbox::SetuidSandboxClient>
132 sandbox_client(sandbox::SetuidSandboxClient::Create()); 155 sandbox_client(sandbox::SetuidSandboxClient::Create());
133 sandbox_client->PrependWrapper(&cmd_line, &options); 156 sandbox_client->PrependWrapper(&cmd_line, &options);
134 sandbox_client->SetupLaunchEnvironment(); 157 sandbox_client->SetupLaunchEnvironment();
135 158
159 // We no longer need this dummy socket for discovering the zygote's PID,
160 // but the sandbox is still hard-coded to expect a file descriptor at
161 // kZygoteIdFd. Fixing this requires a sandbox API change. :(
136 CHECK_EQ(kZygoteIdFd, sandbox_client->GetUniqueToChildFileDescriptor()); 162 CHECK_EQ(kZygoteIdFd, sandbox_client->GetUniqueToChildFileDescriptor());
137 dummy_fd = socket(AF_UNIX, SOCK_DGRAM, 0); 163 dummy_fd.reset(socket(AF_UNIX, SOCK_DGRAM, 0));
138 CHECK(dummy_fd >= 0); 164 CHECK_GE(dummy_fd.get(), 0);
139 fds_to_map.push_back(std::make_pair(dummy_fd, kZygoteIdFd)); 165 fds_to_map.push_back(std::make_pair(dummy_fd.get(), kZygoteIdFd));
140 } 166 }
141 167
142 base::ProcessHandle process = -1; 168 base::ProcessHandle process = -1;
143 options.fds_to_remap = &fds_to_map; 169 options.fds_to_remap = &fds_to_map;
144 base::LaunchProcess(cmd_line.argv(), options, &process); 170 base::LaunchProcess(cmd_line.argv(), options, &process);
145 CHECK(process != -1) << "Failed to launch zygote process"; 171 CHECK(process != -1) << "Failed to launch zygote process";
172 dummy_fd.reset();
146 173
147 if (using_suid_sandbox_) { 174 if (using_suid_sandbox_) {
148 // In the SUID sandbox, the real zygote is forked from the sandbox. 175 // In the SUID sandbox, the real zygote is forked from the sandbox
149 // We need to look for it. 176 // and will be executing in another PID namespace.
150 // But first, wait for the zygote to tell us it's running. 177 // Wait for the zygote to tell us it's running, and receive its PID,
178 // which the kernel will translate to our PID namespace.
151 // The sending code is in content/browser/zygote_main_linux.cc. 179 // The sending code is in content/browser/zygote_main_linux.cc.
152 std::vector<int> fds_vec; 180 std::vector<int> fds_vec;
153 const int kExpectedLength = sizeof(kZygoteHelloMessage); 181 const size_t kExpectedLength = sizeof(kZygoteHelloMessage);
154 char buf[kExpectedLength]; 182 char buf[kExpectedLength];
155 const ssize_t len = UnixDomainSocket::RecvMsg(fds[0], buf, sizeof(buf), 183 const ssize_t len = UnixDomainSocket::RecvMsgWithPid(
156 &fds_vec); 184 fds[0], buf, sizeof(buf), &fds_vec, &pid_);
157 CHECK_EQ(kExpectedLength, len) << "Incorrect zygote magic length"; 185 CHECK_EQ(kExpectedLength, static_cast<size_t>(len))
158 CHECK(0 == strcmp(buf, kZygoteHelloMessage)) << "Incorrect zygote hello"; 186 << "Incorrect zygote magic length";
187 CHECK_EQ(0, memcmp(buf, kZygoteHelloMessage, kExpectedLength))
188 << "Incorrect zygote hello";
189 CHECK_EQ(0U, fds_vec.size())
190 << "Zygote hello should not include file descriptors";
159 191
160 std::string inode_output; 192 if (pid_ <= 0 || !SameOrDescendantOf(pid_, base::GetProcId(process))) {
161 ino_t inode = 0; 193 LOG(FATAL)
162 // Figure out the inode for |dummy_fd|, close |dummy_fd| on our end, 194 << "Received invalid process ID for zygote; kernel might be too old? "
163 // and find the zygote process holding |dummy_fd|. 195 "See crbug.com/357670 or try using --"
164 CHECK(base::FileDescriptorGetInode(&inode, dummy_fd)) 196 << switches::kDisableSetuidSandbox << " to workaround.";
165 << "Cannot get inode for dummy_fd " << dummy_fd; 197 }
166 close(dummy_fd);
167
168 std::vector<std::string> get_inode_cmdline;
169 get_inode_cmdline.push_back(sandbox_binary_);
170 get_inode_cmdline.push_back(base::kFindInodeSwitch);
171 get_inode_cmdline.push_back(base::Int64ToString(inode));
172 CommandLine get_inode_cmd(get_inode_cmdline);
173 CHECK(base::GetAppOutput(get_inode_cmd, &inode_output))
174 << "Find inode command failed for inode " << inode;
175
176 base::TrimWhitespaceASCII(inode_output, base::TRIM_ALL, &inode_output);
177 CHECK(base::StringToInt(inode_output, &pid_))
178 << "Invalid find inode output: " << inode_output;
179 CHECK(pid_ > 0) << "Did not find zygote process (using sandbox binary "
180 << sandbox_binary_ << ")";
181 198
182 if (process != pid_) { 199 if (process != pid_) {
183 // Reap the sandbox. 200 // Reap the sandbox.
184 base::EnsureProcessGetsReaped(process); 201 base::EnsureProcessGetsReaped(process);
185 } 202 }
186 } else { 203 } else {
187 // Not using the SUID sandbox. 204 // Not using the SUID sandbox.
188 pid_ = process; 205 pid_ = process;
189 } 206 }
190 207
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
506 return RenderSandboxHostLinux::GetInstance()->pid(); 523 return RenderSandboxHostLinux::GetInstance()->pid();
507 } 524 }
508 525
509 int ZygoteHostImpl::GetSandboxStatus() const { 526 int ZygoteHostImpl::GetSandboxStatus() const {
510 if (have_read_sandbox_status_word_) 527 if (have_read_sandbox_status_word_)
511 return sandbox_status_; 528 return sandbox_status_;
512 return 0; 529 return 0;
513 } 530 }
514 531
515 } // namespace content 532 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/zygote/zygote_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698