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.
5 #ifndef CONTENT_COMMON_SANDBOX_LINUX_SANDBOX_LINUX_H_
6 #define CONTENT_COMMON_SANDBOX_LINUX_SANDBOX_LINUX_H_
11 #include "base/basictypes.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "content/public/common/sandbox_linux.h"
16 #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
17 defined(THREAD_SANITIZER) || defined(LEAK_SANITIZER) || \
18 defined(UNDEFINED_SANITIZER) || defined(SANITIZER_COVERAGE)
19 #include <sanitizer/common_interface_defs.h>
20 #define ANY_OF_AMTLU_SANITIZER 1
23 template <typename T
> struct DefaultSingletonTraits
;
27 namespace sandbox
{ class SetuidSandboxClient
; }
31 // A singleton class to represent and change our sandboxing state for the
32 // three main Linux sandboxes.
33 // The sandboxing model allows using two layers of sandboxing. The first layer
34 // can be implemented either with unprivileged namespaces or with the setuid
35 // sandbox. This class provides a way to engage the namespace sandbox, but does
36 // not deal with the legacy setuid sandbox directly.
37 // The second layer is mainly based on seccomp-bpf and is engaged with
38 // InitializeSandbox(). InitializeSandbox() is also responsible for "sealing"
39 // the first layer of sandboxing. That is, InitializeSandbox must always be
40 // called to have any meaningful sandboxing at all.
43 // This is a list of sandbox IPC methods which the renderer may send to the
44 // sandbox host. See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
45 // This isn't the full list, values < 32 are reserved for methods called from
47 enum LinuxSandboxIPCMethods
{
48 METHOD_GET_FALLBACK_FONT_FOR_CHAR
= 32,
49 METHOD_LOCALTIME
= 33,
50 DEPRECATED_METHOD_GET_CHILD_WITH_INODE
= 34,
51 METHOD_GET_STYLE_FOR_STRIKE
= 35,
52 METHOD_MAKE_SHARED_MEMORY_SEGMENT
= 36,
53 METHOD_MATCH_WITH_FALLBACK
= 37,
56 // Get our singleton instance.
57 static LinuxSandbox
* GetInstance();
59 // Do some initialization that can only be done before any of the sandboxes
60 // are enabled. If using the setuid sandbox, this should be called manually
61 // before the setuid sandbox is engaged.
62 // Security: When this runs, it is imperative that either InitializeSandbox()
63 // runs as well or that all file descriptors returned in
64 // GetFileDescriptorsToClose() get closed.
65 // Otherwise file descriptors that bypass the security of the setuid sandbox
66 // would be kept open. One must be particularly careful if a process performs
68 void PreinitializeSandbox();
70 // Check that the current process is the init process of a new PID
71 // namespace and then proceed to drop access to the file system by using
72 // a new unprivileged namespace. This is a layer-1 sandbox.
73 // In order for this sandbox to be effective, it must be "sealed" by calling
74 // InitializeSandbox().
75 void EngageNamespaceSandbox();
77 // Return a list of file descriptors to close if PreinitializeSandbox() ran
78 // but InitializeSandbox() won't. Avoid using.
79 // TODO(jln): get rid of this hack.
80 std::vector
<int> GetFileDescriptorsToClose();
82 // Seal an eventual layer-1 sandbox and initialize the layer-2 sandbox with
83 // an adequate policy depending on the process type and command line
85 // Currently the layer-2 sandbox is composed of seccomp-bpf and address space
86 // limitations. This will instantiate the LinuxSandbox singleton if it
87 // doesn't already exist.
88 // This function should only be called without any thread running.
89 static bool InitializeSandbox();
91 // Stop |thread| in a way that can be trusted by the sandbox.
92 static void StopThread(base::Thread
* thread
);
94 // Returns the status of the renderer, worker and ppapi sandbox. Can only
95 // be queried after going through PreinitializeSandbox(). This is a bitmask
96 // and uses the constants defined in "enum LinuxSandboxStatus". Since the
97 // status needs to be provided before the sandboxes are actually started,
98 // this returns what will actually happen once InitializeSandbox()
99 // is called from inside these processes.
101 // Returns true if the current process is single-threaded or if the number
102 // of threads cannot be determined.
103 bool IsSingleThreaded() const;
104 // Did we start Seccomp BPF?
105 bool seccomp_bpf_started() const;
107 // Simple accessor for our instance of the setuid sandbox. Will never return
109 // There is no StartSetuidSandbox(), the SetuidSandboxClient instance should
111 sandbox::SetuidSandboxClient
* setuid_sandbox_client() const;
113 // Check the policy and eventually start the seccomp-bpf sandbox. This should
114 // never be called with threads started. If we detect that threads have
115 // started we will crash.
116 bool StartSeccompBPF(const std::string
& process_type
);
118 // Limit the address space of the current process (and its children).
119 // to make some vulnerabilities harder to exploit.
120 bool LimitAddressSpace(const std::string
& process_type
);
122 // Returns a file descriptor to proc. The file descriptor is no longer valid
123 // after the sandbox has been sealed.
124 int proc_fd() const {
125 DCHECK_NE(-1, proc_fd_
);
129 #if defined(ANY_OF_AMTLU_SANITIZER)
130 __sanitizer_sandbox_arguments
* sanitizer_args() const {
131 return sanitizer_args_
.get();
136 friend struct DefaultSingletonTraits
<LinuxSandbox
>;
141 // Some methods are static and get an instance of the Singleton. These
142 // are the non-static implementations.
143 bool InitializeSandboxImpl();
144 void StopThreadImpl(base::Thread
* thread
);
145 // We must have been pre_initialized_ before using these.
146 bool seccomp_bpf_supported() const;
147 bool seccomp_bpf_with_tsync_supported() const;
148 // Returns true if it can be determined that the current process has open
149 // directories that are not managed by the LinuxSandbox class. This would
150 // be a vulnerability as it would allow to bypass the setuid sandbox.
151 bool HasOpenDirectories() const;
152 // The last part of the initialization is to make sure any temporary "hole"
153 // in the sandbox is closed. For now, this consists of closing proc_fd_.
155 // GetStatus() makes promises as to how the sandbox will behave. This
156 // checks that no promises have been broken.
157 void CheckForBrokenPromises(const std::string
& process_type
);
158 // Stop |thread| and make sure it does not appear in /proc/self/tasks/
160 void StopThreadAndEnsureNotCounted(base::Thread
* thread
) const;
162 // A file descriptor to /proc. It's dangerous to have it around as it could
163 // allow for sandbox bypasses. It needs to be closed before we consider
164 // ourselves sandboxed.
166 bool seccomp_bpf_started_
;
167 // The value returned by GetStatus(). Gets computed once and then cached.
168 int sandbox_status_flags_
;
169 // Did PreinitializeSandbox() run?
170 bool pre_initialized_
;
171 bool seccomp_bpf_supported_
; // Accurate if pre_initialized_.
172 bool seccomp_bpf_with_tsync_supported_
; // Accurate if pre_initialized_.
173 bool yama_is_enforcing_
; // Accurate if pre_initialized_.
174 bool initialize_sandbox_ran_
; // InitializeSandbox() was called.
175 scoped_ptr
<sandbox::SetuidSandboxClient
> setuid_sandbox_client_
;
176 #if defined(ANY_OF_AMTLU_SANITIZER)
177 scoped_ptr
<__sanitizer_sandbox_arguments
> sanitizer_args_
;
180 DISALLOW_COPY_AND_ASSIGN(LinuxSandbox
);
183 } // namespace content
185 #endif // CONTENT_COMMON_SANDBOX_LINUX_SANDBOX_LINUX_H_