blob: 6d15c0f30bd6fa5649f9e7f962bd376acaf9c938 [file] [log] [blame]
[email protected]cc8f1462009-06-12 17:36:551// 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
3// found in the LICENSE file.
4
5#include <unistd.h>
6#include <sys/epoll.h>
7#include <sys/types.h>
8#include <sys/socket.h>
9#include <sys/signal.h>
[email protected]abe3ad92009-06-15 18:15:0810#include <sys/prctl.h>
[email protected]cc8f1462009-06-12 17:36:5511
12#include "base/command_line.h"
13#include "base/eintr_wrapper.h"
14#include "base/global_descriptors_posix.h"
15#include "base/pickle.h"
16#include "base/unix_domain_socket_posix.h"
17
18#include "chrome/browser/zygote_host_linux.h"
19#include "chrome/common/chrome_descriptors.h"
20#include "chrome/common/main_function_params.h"
21#include "chrome/common/process_watcher.h"
22
[email protected]abe3ad92009-06-15 18:15:0823#include "skia/ext/SkFontHost_fontconfig_control.h"
24
[email protected]cc8f1462009-06-12 17:36:5525// http://code.google.com/p/chromium/wiki/LinuxZygote
26
27// This is the object which implements the zygote. The ZygoteMain function,
28// which is called from ChromeMain, at the the bottom and simple constructs one
29// of these objects and runs it.
30class Zygote {
31 public:
32 bool ProcessRequests() {
33 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the
34 // browser on it.
[email protected]abe3ad92009-06-15 18:15:0835 // A SOCK_DGRAM is installed in fd 4. This is the sandbox IPC channel.
36 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
[email protected]cc8f1462009-06-12 17:36:5537
38 // We need to accept SIGCHLD, even though our handler is a no-op because
39 // otherwise we cannot wait on children. (According to POSIX 2001.)
40 struct sigaction action;
41 memset(&action, 0, sizeof(action));
42 action.sa_handler = SIGCHLDHandler;
43 CHECK(sigaction(SIGCHLD, &action, NULL) == 0);
44
45 for (;;) {
46 if (HandleRequestFromBrowser(3))
47 return true;
48 }
49 }
50
51 private:
52 // See comment below, where sigaction is called.
53 static void SIGCHLDHandler(int signal) { }
54
55 // ---------------------------------------------------------------------------
56 // Requests from the browser...
57
58 // Read and process a request from the browser. Returns true if we are in a
59 // new process and thus need to unwind back into ChromeMain.
60 bool HandleRequestFromBrowser(int fd) {
61 std::vector<int> fds;
[email protected]abe3ad92009-06-15 18:15:0862 static const unsigned kMaxMessageLength = 1024;
[email protected]cc8f1462009-06-12 17:36:5563 char buf[kMaxMessageLength];
64 const ssize_t len = base::RecvMsg(fd, buf, sizeof(buf), &fds);
65 if (len == -1) {
66 LOG(WARNING) << "Error reading message from browser: " << errno;
67 return false;
68 }
69
70 if (len == 0) {
71 // EOF from the browser. We should die.
72 _exit(0);
73 return false;
74 }
75
76 Pickle pickle(buf, len);
77 void* iter = NULL;
78
79 int kind;
80 if (!pickle.ReadInt(&iter, &kind))
81 goto error;
82
83 if (kind == ZygoteHost::kCmdFork) {
84 return HandleForkRequest(fd, pickle, iter, fds);
85 } else if (kind == ZygoteHost::kCmdReap) {
86 if (fds.size())
87 goto error;
88 return HandleReapRequest(fd, pickle, iter);
89 }
90
91 error:
92 LOG(WARNING) << "Error parsing message from browser";
93 for (std::vector<int>::const_iterator
94 i = fds.begin(); i != fds.end(); ++i)
95 close(*i);
96 return false;
97 }
98
99 bool HandleReapRequest(int fd, Pickle& pickle, void* iter) {
100 pid_t child;
101
102 if (!pickle.ReadInt(&iter, &child)) {
103 LOG(WARNING) << "Error parsing reap request from browser";
104 return false;
105 }
106
107 ProcessWatcher::EnsureProcessTerminated(child);
108
109 return false;
110 }
111
112 // Handle a 'fork' request from the browser: this means that the browser
113 // wishes to start a new renderer.
114 bool HandleForkRequest(int fd, Pickle& pickle, void* iter,
115 std::vector<int>& fds) {
116 std::vector<std::string> args;
117 int argc, numfds;
118 base::GlobalDescriptors::Mapping mapping;
119 pid_t child;
120
121 if (!pickle.ReadInt(&iter, &argc))
122 goto error;
123
124 for (int i = 0; i < argc; ++i) {
125 std::string arg;
126 if (!pickle.ReadString(&iter, &arg))
127 goto error;
128 args.push_back(arg);
129 }
130
131 if (!pickle.ReadInt(&iter, &numfds))
132 goto error;
133 if (numfds != static_cast<int>(fds.size()))
134 goto error;
135
136 for (int i = 0; i < numfds; ++i) {
137 base::GlobalDescriptors::Key key;
138 if (!pickle.ReadUInt32(&iter, &key))
139 goto error;
140 mapping.push_back(std::make_pair(key, fds[i]));
141 }
142
[email protected]abe3ad92009-06-15 18:15:08143 mapping.push_back(std::make_pair(
144 static_cast<uint32_t>(kSandboxIPCChannel), 4));
145
[email protected]cc8f1462009-06-12 17:36:55146 child = fork();
147
148 if (!child) {
149 close(3); // our socket from the browser is in fd 3
150 Singleton<base::GlobalDescriptors>()->Reset(mapping);
151 CommandLine::Reset();
152 CommandLine::Init(args);
153 return true;
154 }
155
156 for (std::vector<int>::const_iterator
157 i = fds.begin(); i != fds.end(); ++i)
158 close(*i);
159
160 HANDLE_EINTR(write(fd, &child, sizeof(child)));
161 return false;
162
163 error:
164 LOG(WARNING) << "Error parsing fork request from browser";
165 for (std::vector<int>::const_iterator
166 i = fds.begin(); i != fds.end(); ++i)
167 close(*i);
168 return false;
169 }
[email protected]cc8f1462009-06-12 17:36:55170};
171
[email protected]abe3ad92009-06-15 18:15:08172static bool MaybeEnterChroot() {
173 const char* const sandbox_fd_string = getenv("SBX_D");
174 if (sandbox_fd_string) {
175 // The SUID sandbox sets this environment variable to a file descriptor
176 // over which we can signal that we have completed our startup and can be
177 // chrooted.
178
179 char* endptr;
180 const long fd_long = strtol(sandbox_fd_string, &endptr, 10);
181 if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX)
182 return false;
183 const int fd = fd_long;
184
185 static const char kChrootMe = 'C';
186 static const char kChrootMeSuccess = 'O';
187
188 if (HANDLE_EINTR(write(fd, &kChrootMe, 1)) != 1)
189 return false;
190
191 char reply;
192 if (HANDLE_EINTR(read(fd, &reply, 1)) != 1)
193 return false;
194 if (reply != kChrootMeSuccess)
195 return false;
196 if (chdir("/") == -1)
197 return false;
198
199 static const int kMagicSandboxIPCDescriptor = 4;
200 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
201
202 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
203 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0))
204 return false;
205 } else {
206 SkiaFontConfigUseDirectImplementation();
207 }
208
209 return true;
210}
211
[email protected]cc8f1462009-06-12 17:36:55212bool ZygoteMain(const MainFunctionParams& params) {
[email protected]abe3ad92009-06-15 18:15:08213 if (!MaybeEnterChroot()) {
214 LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: "
215 << errno << ")";
216 return false;
217 }
218
[email protected]cc8f1462009-06-12 17:36:55219 Zygote zygote;
220 return zygote.ProcessRequests();
221}