OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <dirent.h> | |
6 #include <fcntl.h> | |
7 #include <sys/resource.h> | |
8 #include <sys/stat.h> | |
9 #include <sys/time.h> | |
10 #include <sys/types.h> | |
11 | |
12 #include <limits> | |
13 | |
14 #include "base/bind.h" | |
15 #include "base/callback_helpers.h" | |
16 #include "base/command_line.h" | |
17 #include "base/logging.h" | |
18 #include "base/memory/singleton.h" | |
19 #include "base/posix/eintr_wrapper.h" | |
20 #include "base/strings/string_number_conversions.h" | |
21 #include "base/time/time.h" | |
22 #include "content/common/sandbox_linux.h" | |
23 #include "content/common/sandbox_seccomp_bpf_linux.h" | |
24 #include "content/public/common/content_switches.h" | |
25 #include "content/public/common/sandbox_linux.h" | |
26 #include "sandbox/linux/services/credentials.h" | |
27 #include "sandbox/linux/suid/client/setuid_sandbox_client.h" | |
28 | |
29 namespace { | |
30 | |
31 void LogSandboxStarted(const std::string& sandbox_name) { | |
32 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
33 const std::string process_type = | |
34 command_line.GetSwitchValueASCII(switches::kProcessType); | |
35 const std::string activated_sandbox = | |
36 "Activated " + sandbox_name + " sandbox for process type: " + | |
37 process_type + "."; | |
38 #if defined(OS_CHROMEOS) | |
39 LOG(WARNING) << activated_sandbox; | |
40 #else | |
41 VLOG(1) << activated_sandbox; | |
42 #endif | |
43 } | |
44 | |
45 bool AddResourceLimit(int resource, rlim_t limit) { | |
46 struct rlimit old_rlimit; | |
47 if (getrlimit(resource, &old_rlimit)) | |
48 return false; | |
49 // Make sure we don't raise the existing limit. | |
50 const struct rlimit new_rlimit = { | |
51 std::min(old_rlimit.rlim_cur, limit), | |
52 std::min(old_rlimit.rlim_max, limit) | |
53 }; | |
54 int rc = setrlimit(resource, &new_rlimit); | |
55 return rc == 0; | |
56 } | |
57 | |
58 bool IsRunningTSAN() { | |
59 #if defined(THREAD_SANITIZER) | |
60 return true; | |
61 #else | |
62 return false; | |
63 #endif | |
64 } | |
65 | |
66 } // namespace | |
67 | |
68 namespace content { | |
69 | |
70 LinuxSandbox::LinuxSandbox() | |
71 : proc_fd_(-1), | |
72 seccomp_bpf_started_(false), | |
73 pre_initialized_(false), | |
74 seccomp_bpf_supported_(false), | |
75 setuid_sandbox_client_(sandbox::SetuidSandboxClient::Create()) { | |
76 if (setuid_sandbox_client_ == NULL) { | |
77 LOG(FATAL) << "Failed to instantiate the setuid sandbox client."; | |
78 } | |
79 } | |
80 | |
81 LinuxSandbox::~LinuxSandbox() { | |
82 } | |
83 | |
84 LinuxSandbox* LinuxSandbox::GetInstance() { | |
85 LinuxSandbox* instance = Singleton<LinuxSandbox>::get(); | |
86 CHECK(instance); | |
87 return instance; | |
88 } | |
89 | |
90 #if defined(ADDRESS_SANITIZER) && defined(OS_LINUX) | |
91 // ASan API call to notify the tool the sandbox is going to be turned on. | |
92 extern "C" void __sanitizer_sandbox_on_notify(void *reserved); | |
93 #endif | |
94 | |
95 void LinuxSandbox::PreinitializeSandbox() { | |
96 CHECK(!pre_initialized_); | |
97 seccomp_bpf_supported_ = false; | |
98 #if defined(ADDRESS_SANITIZER) && defined(OS_LINUX) | |
99 // ASan needs to open some resources before the sandbox is enabled. | |
100 // This should not fork, not launch threads, not open a directory. | |
101 __sanitizer_sandbox_on_notify(/*reserved*/NULL); | |
102 #endif | |
103 | |
104 #if !defined(NDEBUG) | |
105 // Open proc_fd_ only in Debug mode so that forgetting to close it doesn't | |
106 // produce a sandbox escape in Release mode. | |
107 proc_fd_ = open("/proc", O_DIRECTORY | O_RDONLY); | |
108 CHECK_GE(proc_fd_, 0); | |
109 #endif // !defined(NDEBUG) | |
110 // We "pre-warm" the code that detects supports for seccomp BPF. | |
111 if (SandboxSeccompBPF::IsSeccompBPFDesired()) { | |
112 if (!SandboxSeccompBPF::SupportsSandbox()) { | |
113 VLOG(1) << "Lacking support for seccomp-bpf sandbox."; | |
114 } else { | |
115 seccomp_bpf_supported_ = true; | |
116 } | |
117 } | |
118 pre_initialized_ = true; | |
119 } | |
120 | |
121 bool LinuxSandbox::InitializeSandbox() { | |
122 bool seccomp_bpf_started = false; | |
123 LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance(); | |
124 // We need to make absolutely sure that our sandbox is "sealed" before | |
125 // InitializeSandbox does exit. | |
126 base::ScopedClosureRunner sandbox_sealer( | |
127 base::Bind(&LinuxSandbox::SealSandbox, base::Unretained(linux_sandbox))); | |
128 const std::string process_type = | |
129 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
130 switches::kProcessType); | |
131 | |
132 // No matter what, it's always an error to call InitializeSandbox() after | |
133 // threads have been created. | |
134 if (!linux_sandbox->IsSingleThreaded()) { | |
135 std::string error_message = "InitializeSandbox() called with multiple " | |
136 "threads in process " + process_type; | |
137 // TSAN starts a helper thread. So we don't start the sandbox and don't | |
138 // even report an error about it. | |
139 if (IsRunningTSAN()) | |
140 return false; | |
141 // The GPU process is allowed to call InitializeSandbox() with threads for | |
142 // now, because it loads third party libraries. | |
143 if (process_type != switches::kGpuProcess) | |
144 CHECK(false) << error_message; | |
145 LOG(ERROR) << error_message; | |
146 return false; | |
147 } | |
148 | |
149 DCHECK(!linux_sandbox->HasOpenDirectories()) << | |
150 "InitializeSandbox() called after unexpected directories have been " << | |
151 "opened. This breaks the security of the setuid sandbox."; | |
152 | |
153 // Attempt to limit the future size of the address space of the process. | |
154 linux_sandbox->LimitAddressSpace(process_type); | |
155 | |
156 // First, try to enable seccomp-bpf. | |
157 seccomp_bpf_started = linux_sandbox->StartSeccompBPF(process_type); | |
158 | |
159 return seccomp_bpf_started; | |
160 } | |
161 | |
162 int LinuxSandbox::GetStatus() const { | |
163 CHECK(pre_initialized_); | |
164 int sandbox_flags = 0; | |
165 if (setuid_sandbox_client_->IsSandboxed()) { | |
166 sandbox_flags |= kSandboxLinuxSUID; | |
167 if (setuid_sandbox_client_->IsInNewPIDNamespace()) | |
168 sandbox_flags |= kSandboxLinuxPIDNS; | |
169 if (setuid_sandbox_client_->IsInNewNETNamespace()) | |
170 sandbox_flags |= kSandboxLinuxNetNS; | |
171 } | |
172 | |
173 if (seccomp_bpf_supported() && | |
174 SandboxSeccompBPF::ShouldEnableSeccompBPF(switches::kRendererProcess)) { | |
175 // We report whether the sandbox will be activated when renderers go | |
176 // through sandbox initialization. | |
177 sandbox_flags |= kSandboxLinuxSeccompBPF; | |
178 } | |
179 | |
180 return sandbox_flags; | |
181 } | |
182 | |
183 // Threads are counted via /proc/self/task. This is a little hairy because of | |
184 // PID namespaces and existing sandboxes, so "self" must really be used instead | |
185 // of using the pid. | |
186 bool LinuxSandbox::IsSingleThreaded() const { | |
187 struct stat task_stat; | |
188 int fstat_ret; | |
189 if (proc_fd_ >= 0) { | |
190 // If a handle to /proc is available, use it. This allows to bypass file | |
191 // system restrictions. | |
192 fstat_ret = fstatat(proc_fd_, "self/task/", &task_stat, 0); | |
193 } else { | |
194 // Otherwise, make an attempt to access the file system directly. | |
195 fstat_ret = fstatat(AT_FDCWD, "/proc/self/task/", &task_stat, 0); | |
196 } | |
197 // In Debug mode, it's mandatory to be able to count threads to catch bugs. | |
198 #if !defined(NDEBUG) | |
199 // Using DCHECK here would be incorrect. DCHECK can be enabled in non | |
200 // official release mode. | |
201 CHECK_EQ(0, fstat_ret) << "Could not count threads, the sandbox was not " | |
202 << "pre-initialized properly."; | |
203 #endif // !defined(NDEBUG) | |
204 if (fstat_ret) { | |
205 // Pretend to be monothreaded if it can't be determined (for instance the | |
206 // setuid sandbox is already engaged but no proc_fd_ is available). | |
207 return true; | |
208 } | |
209 | |
210 // At least "..", "." and the current thread should be present. | |
211 CHECK_LE(3UL, task_stat.st_nlink); | |
212 // Counting threads via /proc/self/task could be racy. For the purpose of | |
213 // determining if the current proces is monothreaded it works: if at any | |
214 // time it becomes monothreaded, it'll stay so. | |
215 return task_stat.st_nlink == 3; | |
216 } | |
217 | |
218 bool LinuxSandbox::seccomp_bpf_started() const { | |
219 return seccomp_bpf_started_; | |
220 } | |
221 | |
222 sandbox::SetuidSandboxClient* | |
223 LinuxSandbox::setuid_sandbox_client() const { | |
224 return setuid_sandbox_client_.get(); | |
225 } | |
226 | |
227 // For seccomp-bpf, we use the SandboxSeccompBPF class. | |
228 bool LinuxSandbox::StartSeccompBPF(const std::string& process_type) { | |
229 CHECK(!seccomp_bpf_started_); | |
230 if (!pre_initialized_) | |
231 PreinitializeSandbox(); | |
232 if (seccomp_bpf_supported()) | |
233 seccomp_bpf_started_ = SandboxSeccompBPF::StartSandbox(process_type); | |
234 | |
235 if (seccomp_bpf_started_) | |
236 LogSandboxStarted("seccomp-bpf"); | |
237 | |
238 return seccomp_bpf_started_; | |
239 } | |
240 | |
241 bool LinuxSandbox::seccomp_bpf_supported() const { | |
242 CHECK(pre_initialized_); | |
243 return seccomp_bpf_supported_; | |
244 } | |
245 | |
246 bool LinuxSandbox::LimitAddressSpace(const std::string& process_type) { | |
247 (void) process_type; | |
248 #if !defined(ADDRESS_SANITIZER) | |
249 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
250 if (command_line->HasSwitch(switches::kNoSandbox)) { | |
251 return false; | |
252 } | |
253 | |
254 // Limit the address space to 4GB. | |
255 // This is in the hope of making some kernel exploits more complex and less | |
256 // reliable. It also limits sprays a little on 64-bit. | |
257 rlim_t address_space_limit = std::numeric_limits<uint32_t>::max(); | |
258 #if defined(__LP64__) | |
259 // On 64 bits, V8 and possibly others will reserve massive memory ranges and | |
260 // rely on on-demand paging for allocation. Unfortunately, even | |
261 // MADV_DONTNEED ranges count towards RLIMIT_AS so this is not an option. | |
262 // See crbug.com/169327 for a discussion. | |
263 // On the GPU process, irrespective of V8, we can exhaust a 4GB address space | |
264 // under normal usage, see crbug.com/271119 | |
265 // For now, increase limit to 16GB for renderer and worker and gpu processes | |
266 // to accomodate. | |
267 if (process_type == switches::kRendererProcess || | |
268 process_type == switches::kWorkerProcess || | |
269 process_type == switches::kGpuProcess) { | |
270 address_space_limit = 1L << 34; | |
271 } | |
272 #endif // defined(__LP64__) | |
273 | |
274 // On all platforms, add a limit to the brk() heap that would prevent | |
275 // allocations that can't be index by an int. | |
276 const rlim_t kNewDataSegmentMaxSize = std::numeric_limits<int>::max(); | |
277 | |
278 bool limited_as = AddResourceLimit(RLIMIT_AS, address_space_limit); | |
279 bool limited_data = AddResourceLimit(RLIMIT_DATA, kNewDataSegmentMaxSize); | |
280 return limited_as && limited_data; | |
281 #else | |
282 return false; | |
283 #endif // !defined(ADDRESS_SANITIZER) | |
284 } | |
285 | |
286 bool LinuxSandbox::HasOpenDirectories() { | |
287 return sandbox::Credentials().HasOpenDirectory(proc_fd_); | |
288 } | |
289 | |
290 void LinuxSandbox::SealSandbox() { | |
291 if (proc_fd_ >= 0) { | |
292 int ret = IGNORE_EINTR(close(proc_fd_)); | |
293 CHECK_EQ(0, ret); | |
294 proc_fd_ = -1; | |
295 } | |
296 } | |
297 | |
298 } // namespace content | |
299 | |
OLD | NEW |