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

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

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