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 SANDBOX_LINUX_SUID_SETUID_SANDBOX_CLIENT_H_
6 #define SANDBOX_LINUX_SUID_SETUID_SANDBOX_CLIENT_H_
8 #include "base/environment.h"
9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "sandbox/sandbox_export.h"
15 // Helper class to use the setuid sandbox. This class is to be used
16 // after being executed through the setuid helper.
17 // This class is difficult to use. It has been created by refactoring very old
18 // code scathered through the Chromium code base.
20 // A typical use for "A" launching a sandboxed process "B" would be:
21 // (Steps 1 through 4 are described in setuid_sandbox_host.h.)
22 // 5. B uses CloseDummyFile() to close the dummy file descriptor.
23 // 6. B performs various initializations that require access to the file
25 // 6.b (optional) B uses sandbox::Credentials::HasOpenDirectory() to verify
26 // that no directory is kept open (which would allow bypassing the setuid
28 // 7. B should be prepared to assume the role of init(1). In particular, B
29 // cannot receive any signal from any other process, excluding SIGKILL.
30 // If B dies, all the processes in the namespace will die.
31 // B can fork() and the parent can assume the role of init(1), by using
32 // sandbox::CreateInitProcessReaper().
33 // 8. B requests being chroot-ed through ChrootMe() and
34 // requests other sandboxing status via the status functions.
35 class SANDBOX_EXPORT SetuidSandboxClient
{
37 // All instantation should go through this factory method.
38 static SetuidSandboxClient
* Create();
39 ~SetuidSandboxClient();
41 // Close the dummy file descriptor leftover from the sandbox ABI.
42 void CloseDummyFile();
43 // Ask the setuid helper over the setuid sandbox IPC channel to chroot() us
44 // to an empty directory.
45 // Will only work if we have been launched through the setuid helper.
48 // Did we get launched through an up to date setuid binary ?
49 bool IsSuidSandboxUpToDate() const;
50 // Did we get launched through the setuid helper ?
51 bool IsSuidSandboxChild() const;
52 // Did the setuid helper create a new PID namespace ?
53 bool IsInNewPIDNamespace() const;
54 // Did the setuid helper create a new network namespace ?
55 bool IsInNewNETNamespace() const;
56 // Are we done and fully sandboxed ?
57 bool IsSandboxed() const;
60 explicit SetuidSandboxClient(base::Environment
* env
);
62 // Holds the environment. Will never be NULL.
63 scoped_ptr
<base::Environment
> env_
;
66 DISALLOW_COPY_AND_ASSIGN(SetuidSandboxClient
);
69 } // namespace sandbox
71 #endif // SANDBOX_LINUX_SUID_SETUID_SANDBOX_CLIENT_H_