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

Side by Side Diff: chrome/browser/zygote_host_linux.cc

Issue 359001: Revert 30938 - Add support for getting the real process id from within the su... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 11 years, 1 month 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 | « chrome/browser/zygote_host_linux.h ('k') | chrome/browser/zygote_main_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) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 "chrome/browser/zygote_host_linux.h" 5 #include "chrome/browser/zygote_host_linux.h"
6 6
7 #include <unistd.h>
8 #include <sys/types.h>
7 #include <sys/socket.h> 9 #include <sys/socket.h>
8 #include <sys/stat.h> 10 #include <sys/stat.h>
9 #include <sys/types.h>
10 #include <unistd.h>
11 11
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/eintr_wrapper.h" 13 #include "base/eintr_wrapper.h"
14 #include "base/linux_util.h"
15 #include "base/logging.h" 14 #include "base/logging.h"
16 #include "base/path_service.h" 15 #include "base/path_service.h"
17 #include "base/pickle.h" 16 #include "base/pickle.h"
18 #include "base/process_util.h" 17 #include "base/process_util.h"
19 #include "base/string_util.h" 18 #include "base/string_util.h"
20 #include "base/unix_domain_socket_posix.h" 19 #include "base/unix_domain_socket_posix.h"
21 20
22 #include "chrome/browser/renderer_host/render_sandbox_host_linux.h" 21 #include "chrome/browser/renderer_host/render_sandbox_host_linux.h"
23 #include "chrome/common/chrome_constants.h" 22 #include "chrome/common/chrome_constants.h"
24 #include "chrome/common/chrome_switches.h" 23 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/process_watcher.h"
26 24
27 #include "sandbox/linux/suid/suid_unsafe_environment_variables.h" 25 #include "sandbox/linux/suid/suid_unsafe_environment_variables.h"
28 26
29 static void SaveSUIDUnsafeEnvironmentVariables() { 27 static void SaveSUIDUnsafeEnvironmentVariables() {
30 // The ELF loader will clear many environment variables so we save them to 28 // The ELF loader will clear many environment variables so we save them to
31 // different names here so that the SUID sandbox can resolve them for the 29 // different names here so that the SUID sandbox can resolve them for the
32 // renderer. 30 // renderer.
33 31
34 for (unsigned i = 0; kSUIDUnsafeEnvironmentVariables[i]; ++i) { 32 for (unsigned i = 0; kSUIDUnsafeEnvironmentVariables[i]; ++i) {
35 const char* const envvar = kSUIDUnsafeEnvironmentVariables[i]; 33 const char* const envvar = kSUIDUnsafeEnvironmentVariables[i];
36 char* const saved_envvar = SandboxSavedEnvironmentVariable(envvar); 34 char* const saved_envvar = SandboxSavedEnvironmentVariable(envvar);
37 if (!saved_envvar) 35 if (!saved_envvar)
38 continue; 36 continue;
39 37
40 const char* const value = getenv(envvar); 38 const char* const value = getenv(envvar);
41 if (value) 39 if (value)
42 setenv(saved_envvar, value, 1 /* overwrite */); 40 setenv(saved_envvar, value, 1 /* overwrite */);
43 else 41 else
44 unsetenv(saved_envvar); 42 unsetenv(saved_envvar);
45 43
46 free(saved_envvar); 44 free(saved_envvar);
47 } 45 }
48 } 46 }
49 47
50 ZygoteHost::ZygoteHost() 48 ZygoteHost::ZygoteHost() {
51 : pid_(-1),
52 init_(false) {
53 }
54
55 ZygoteHost::~ZygoteHost() {
56 if (init_)
57 close(control_fd_);
58 }
59
60 void ZygoteHost::Init(const std::string& sandbox_cmd) {
61 DCHECK(!init_);
62 init_ = true;
63
64 FilePath chrome_path; 49 FilePath chrome_path;
65 CHECK(PathService::Get(base::FILE_EXE, &chrome_path)); 50 CHECK(PathService::Get(base::FILE_EXE, &chrome_path));
66 CommandLine cmd_line(chrome_path); 51 CommandLine cmd_line(chrome_path);
67 52
68 cmd_line.AppendSwitchWithValue(switches::kProcessType, 53 cmd_line.AppendSwitchWithValue(switches::kProcessType,
69 switches::kZygoteProcess); 54 switches::kZygoteProcess);
70 55
71 int fds[2]; 56 int fds[2];
72 CHECK(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fds) == 0); 57 CHECK(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fds) == 0);
73 base::file_handle_mapping_vector fds_to_map; 58 base::file_handle_mapping_vector fds_to_map;
(...skipping 16 matching lines...) Expand all
90 browser_command_line.GetSwitchValue( 75 browser_command_line.GetSwitchValue(
91 switches::kLoggingLevel)); 76 switches::kLoggingLevel));
92 } 77 }
93 if (browser_command_line.HasSwitch(switches::kEnableLogging)) { 78 if (browser_command_line.HasSwitch(switches::kEnableLogging)) {
94 // Append with value to support --enable-logging=stderr. 79 // Append with value to support --enable-logging=stderr.
95 cmd_line.AppendSwitchWithValue(switches::kEnableLogging, 80 cmd_line.AppendSwitchWithValue(switches::kEnableLogging,
96 browser_command_line.GetSwitchValue( 81 browser_command_line.GetSwitchValue(
97 switches::kEnableLogging)); 82 switches::kEnableLogging));
98 } 83 }
99 84
100 const char* sandbox_binary = sandbox_cmd.c_str(); 85 const char* sandbox_binary = NULL;
101 struct stat st; 86 struct stat st;
102 87
103 bool using_suid_sandbox = false; 88 // In Chromium branded builds, developers can set an environment variable to
104 if (!sandbox_cmd.empty() && stat(sandbox_binary, &st) == 0) { 89 // use the development sandbox. See
90 // http://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment
91 if (stat("/proc/self/exe", &st) == 0 &&
92 st.st_uid == getuid()) {
93 sandbox_binary = getenv("CHROME_DEVEL_SANDBOX");
94 }
95
96 #if defined(LINUX_SANDBOX_PATH)
97 if (!sandbox_binary)
98 sandbox_binary = LINUX_SANDBOX_PATH;
99 #endif
100
101 if (sandbox_binary && stat(sandbox_binary, &st) == 0) {
105 if (access(sandbox_binary, X_OK) == 0 && 102 if (access(sandbox_binary, X_OK) == 0 &&
106 (st.st_mode & S_ISUID) && 103 (st.st_mode & S_ISUID) &&
107 (st.st_mode & S_IXOTH)) { 104 (st.st_mode & S_IXOTH)) {
108 using_suid_sandbox = true;
109 cmd_line.PrependWrapper(ASCIIToWide(sandbox_binary)); 105 cmd_line.PrependWrapper(ASCIIToWide(sandbox_binary));
110 106
111 SaveSUIDUnsafeEnvironmentVariables(); 107 SaveSUIDUnsafeEnvironmentVariables();
112 } else { 108 } else {
113 LOG(FATAL) << "The SUID sandbox helper binary was found, but is not " 109 LOG(FATAL) << "The SUID sandbox helper binary was found, but is not "
114 "configured correctly. Rather than run without sandboxing " 110 "configured correctly. Rather than run without sandboxing "
115 "I'm aborting now. You need to make sure that " 111 "I'm aborting now. You need to make sure that "
116 << sandbox_binary << " is mode 4755."; 112 << sandbox_binary << " is mode 4755.";
117 } 113 }
118 } 114 }
119 115
120 // Start up the sandbox host process and get the file descriptor for the 116 // Start up the sandbox host process and get the file descriptor for the
121 // renderers to talk to it. 117 // renderers to talk to it.
122 const int sfd = Singleton<RenderSandboxHostLinux>()->GetRendererSocket(); 118 const int sfd = Singleton<RenderSandboxHostLinux>()->GetRendererSocket();
123 fds_to_map.push_back(std::make_pair(sfd, 5)); 119 fds_to_map.push_back(std::make_pair(sfd, 5));
124 120
125 int dummy_fd = -1;
126 if (using_suid_sandbox) {
127 dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
128 CHECK(dummy_fd >= 0);
129 fds_to_map.push_back(std::make_pair(dummy_fd, 7));
130 }
131
132 base::ProcessHandle process; 121 base::ProcessHandle process;
133 base::LaunchApp(cmd_line.argv(), fds_to_map, false, &process); 122 base::LaunchApp(cmd_line.argv(), fds_to_map, false, &process);
134 CHECK(process != -1) << "Failed to launch zygote process"; 123 CHECK(process != -1) << "Failed to launch zygote process";
135 124
136 if (using_suid_sandbox) { 125 pid_ = process;
137 // In the SUID sandbox, the real zygote is forked from the sandbox.
138 // We need to look for it.
139 // But first, wait for the zygote to tell us it's running.
140 // The sending code is in chrome/browser/zygote_main_linux.cc.
141 std::vector<int> fds_vec;
142 const int kExpectedLength = sizeof(kZygoteMagic);
143 char buf[kExpectedLength];
144 const ssize_t len = base::RecvMsg(fds[0], buf, sizeof(buf), &fds_vec);
145 CHECK(len == kExpectedLength) << "Incorrect zygote magic length";
146 CHECK(0 == strcmp(buf, kZygoteMagic)) << "Incorrect zygote magic";
147
148 std::string inode_output;
149 ino_t inode = 0;
150 // Figure out the inode for |dummy_fd|, close |dummy_fd| on our end,
151 // and find the zygote process holding |dummy_fd|.
152 if (base::FileDescriptorGetInode(&inode, dummy_fd)) {
153 close(dummy_fd);
154 std::vector<std::string> get_inode_cmdline;
155 get_inode_cmdline.push_back(sandbox_binary);
156 get_inode_cmdline.push_back(base::kFindInodeSwitch);
157 get_inode_cmdline.push_back(IntToString(inode));
158 CommandLine get_inode_cmd(get_inode_cmdline);
159 if (base::GetAppOutput(get_inode_cmd, &inode_output)) {
160 StringToInt(inode_output, &pid_);
161 }
162 }
163 CHECK(pid_ > 0) << "Did not find zygote process";
164
165 if (process != pid_) {
166 // Reap the sandbox.
167 ProcessWatcher::EnsureProcessGetsReaped(process);
168 }
169 } else {
170 // Not using the SUID sandbox.
171 pid_ = process;
172 }
173
174 close(fds[1]); 126 close(fds[1]);
175 control_fd_ = fds[0]; 127 control_fd_ = fds[0];
176 } 128 }
177 129
130 ZygoteHost::~ZygoteHost() {
131 close(control_fd_);
132 }
133
178 pid_t ZygoteHost::ForkRenderer( 134 pid_t ZygoteHost::ForkRenderer(
179 const std::vector<std::string>& argv, 135 const std::vector<std::string>& argv,
180 const base::GlobalDescriptors::Mapping& mapping) { 136 const base::GlobalDescriptors::Mapping& mapping) {
181 DCHECK(init_);
182 Pickle pickle; 137 Pickle pickle;
183 138
184 pickle.WriteInt(kCmdFork); 139 pickle.WriteInt(kCmdFork);
185 pickle.WriteInt(argv.size()); 140 pickle.WriteInt(argv.size());
186 for (std::vector<std::string>::const_iterator 141 for (std::vector<std::string>::const_iterator
187 i = argv.begin(); i != argv.end(); ++i) 142 i = argv.begin(); i != argv.end(); ++i)
188 pickle.WriteString(*i); 143 pickle.WriteString(*i);
189 144
190 pickle.WriteInt(mapping.size()); 145 pickle.WriteInt(mapping.size());
191 146
192 std::vector<int> fds; 147 std::vector<int> fds;
193 for (base::GlobalDescriptors::Mapping::const_iterator 148 for (base::GlobalDescriptors::Mapping::const_iterator
194 i = mapping.begin(); i != mapping.end(); ++i) { 149 i = mapping.begin(); i != mapping.end(); ++i) {
195 pickle.WriteUInt32(i->first); 150 pickle.WriteUInt32(i->first);
196 fds.push_back(i->second); 151 fds.push_back(i->second);
197 } 152 }
198 153
199 if (!base::SendMsg(control_fd_, pickle.data(), pickle.size(), fds)) 154 if (!base::SendMsg(control_fd_, pickle.data(), pickle.size(), fds))
200 return -1; 155 return -1;
201 156
202 pid_t pid; 157 pid_t pid;
203 if (HANDLE_EINTR(read(control_fd_, &pid, sizeof(pid))) != sizeof(pid)) 158 if (HANDLE_EINTR(read(control_fd_, &pid, sizeof(pid))) != sizeof(pid))
204 return -1; 159 return -1;
205 160
206 return pid; 161 return pid;
207 } 162 }
208 163
209 void ZygoteHost::EnsureProcessTerminated(pid_t process) { 164 void ZygoteHost::EnsureProcessTerminated(pid_t process) {
210 DCHECK(init_);
211 Pickle pickle; 165 Pickle pickle;
212 166
213 pickle.WriteInt(kCmdReap); 167 pickle.WriteInt(kCmdReap);
214 pickle.WriteInt(process); 168 pickle.WriteInt(process);
215 169
216 HANDLE_EINTR(write(control_fd_, pickle.data(), pickle.size())); 170 HANDLE_EINTR(write(control_fd_, pickle.data(), pickle.size()));
217 } 171 }
218 172
219 bool ZygoteHost::DidProcessCrash(base::ProcessHandle handle, 173 bool ZygoteHost::DidProcessCrash(base::ProcessHandle handle,
220 bool* child_exited) { 174 bool* child_exited) {
221 DCHECK(init_);
222 Pickle pickle; 175 Pickle pickle;
223 pickle.WriteInt(kCmdDidProcessCrash); 176 pickle.WriteInt(kCmdDidProcessCrash);
224 pickle.WriteInt(handle); 177 pickle.WriteInt(handle);
225 178
226 HANDLE_EINTR(write(control_fd_, pickle.data(), pickle.size())); 179 HANDLE_EINTR(write(control_fd_, pickle.data(), pickle.size()));
227 180
228 static const unsigned kMaxMessageLength = 128; 181 static const unsigned kMaxMessageLength = 128;
229 char buf[kMaxMessageLength]; 182 char buf[kMaxMessageLength];
230 const ssize_t len = HANDLE_EINTR(read(control_fd_, buf, sizeof(buf))); 183 const ssize_t len = HANDLE_EINTR(read(control_fd_, buf, sizeof(buf)));
231 184
(...skipping 12 matching lines...) Expand all
244 !read_pickle.ReadBool(&iter, &tmp_child_exited)) { 197 !read_pickle.ReadBool(&iter, &tmp_child_exited)) {
245 LOG(WARNING) << "Error parsing DidProcessCrash response from zygote."; 198 LOG(WARNING) << "Error parsing DidProcessCrash response from zygote.";
246 return false; 199 return false;
247 } 200 }
248 201
249 if (child_exited) 202 if (child_exited)
250 *child_exited = tmp_child_exited; 203 *child_exited = tmp_child_exited;
251 204
252 return did_crash; 205 return did_crash;
253 } 206 }
OLDNEW
« no previous file with comments | « chrome/browser/zygote_host_linux.h ('k') | chrome/browser/zygote_main_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698