10f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
20f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
30f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// found in the LICENSE file.
40f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
50f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#ifndef SANDBOX_LINUX_SERVICES_CREDENTIALS_H_
60f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#define SANDBOX_LINUX_SERVICES_CREDENTIALS_H_
70f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
80f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "build/build_config.h"
90f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Link errors are tedious to track, raise a compile-time error instead.
100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#if defined(OS_ANDROID)
110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#error "Android is not supported."
120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#endif  // defined(OS_ANDROID).
130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include <string>
150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "base/basictypes.h"
170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "sandbox/sandbox_export.h"
190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)namespace sandbox {
210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// This class should be used to manipulate the current process' credentials.
230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// It is currently a stub used to manipulate POSIX.1e capabilities as
240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// implemented by the Linux kernel.
25c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochclass SANDBOX_EXPORT Credentials {
260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) public:
270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  Credentials();
280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  ~Credentials();
290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Returns the number of file descriptors in the current process's FD
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // table, excluding |proc_fd|, which should be a file descriptor for
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // /proc.
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  int CountOpenFds(int proc_fd);
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Checks whether the current process has any directory file descriptor open.
36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Directory file descriptors are "capabilities" that would let a process use
37f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // system calls such as openat() to bypass restrictions such as
38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // DropFileSystemAccess().
39f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Sometimes it's useful to call HasOpenDirectory() after file system access
40f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // has been dropped. In this case, |proc_fd| should be a file descriptor to
41f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // /proc. The file descriptor in |proc_fd| will be ignored by
42f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // HasOpenDirectory() and remains owned by the caller. It is very important
43f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // for the caller to close it.
44f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // If /proc is available, |proc_fd| can be passed as -1.
45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // If |proc_fd| is -1 and /proc is not available, this function will return
46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // false.
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool HasOpenDirectory(int proc_fd);
48f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Drop all capabilities in the effective, inheritable and permitted sets for
500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // the current process.
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool DropAllCapabilities();
520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Return true iff there is any capability in any of the capabilities sets
530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // of the current process.
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool HasAnyCapability() const;
550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Returns the capabilities of the current process in textual form, as
560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // documented in libcap2's cap_to_text(3). This is mostly useful for
570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // debugging and tests.
58f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_ptr<std::string> GetCurrentCapString() const;
59f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Returns whether the kernel supports CLONE_NEWUSER and whether it would be
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // possible to immediately move to a new user namespace. There is no point
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // in using this method right before calling MoveToNewUserNS(), simply call
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // MoveToNewUserNS() immediately. This method is only useful to test kernel
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // support ahead of time.
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static bool SupportsNewUserNS();
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
67f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Move the current process to a new "user namespace" as supported by Linux
68f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // 3.8+ (CLONE_NEWUSER).
69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The uid map will be set-up so that the perceived uid and gid will not
70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // change.
71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // If this call succeeds, the current process will be granted a full set of
72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // capabilities in the new namespace.
73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool MoveToNewUserNS();
74f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
75f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Remove the ability of the process to access the file system. File
76f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // descriptors which are already open prior to calling this API remain
77f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // available.
78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The implementation currently uses chroot(2) and requires CAP_SYS_CHROOT.
79f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // CAP_SYS_CHROOT can be acquired by using the MoveToNewUserNS() API.
80f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Make sure to call DropAllCapabilities() after this call to prevent
81f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // escapes.
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // To be secure, it's very important for this API to not be called while the
83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // process has any directory file descriptor open.
84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool DropFileSystemAccess();
850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) private:
870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(Credentials);
880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)};
890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}  // namespace sandbox.
910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#endif  // SANDBOX_LINUX_SERVICES_CREDENTIALS_H_
93