• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // The sandbox2::Namespace class defines ways of inserting the sandboxed process
16 // into Linux namespaces.
17 
18 #ifndef SANDBOXED_API_SANDBOX2_NAMESPACE_H_
19 #define SANDBOXED_API_SANDBOX2_NAMESPACE_H_
20 
21 #include <sched.h>
22 #include <sys/types.h>
23 
24 #include <cstdint>
25 #include <string>
26 
27 #include "sandboxed_api/sandbox2/forkserver.pb.h"
28 #include "sandboxed_api/sandbox2/mounts.h"
29 
30 namespace sandbox2 {
31 
32 class Namespace final {
33  public:
34   // Performs the namespace setup (mounts, write the uid_map, etc.).
35   static void InitializeNamespaces(uid_t uid, gid_t gid, int32_t clone_flags,
36                                    const Mounts& mounts,
37                                    const std::string& hostname,
38                                    bool avoid_pivot_root,
39                                    bool allow_mount_propagation);
40   static void InitializeInitialNamespaces(uid_t uid, gid_t gid);
41 
42   Namespace(Mounts mounts, std::string hostname, NetNsMode netns_config,
43             bool allow_mount_propagation = false);
44 
netns_config()45   NetNsMode netns_config() const { return netns_config_; }
46 
clone_flags()47   int32_t clone_flags() const { return clone_flags_; }
48 
mounts()49   Mounts& mounts() { return mounts_; }
mounts()50   const Mounts& mounts() const { return mounts_; }
51 
hostname()52   const std::string& hostname() const { return hostname_; }
53 
allow_mount_propagation()54   bool allow_mount_propagation() const { return allow_mount_propagation_; }
55 
56  private:
57   int32_t clone_flags_ = CLONE_NEWUSER | CLONE_NEWNS | CLONE_NEWUTS |
58                          CLONE_NEWPID | CLONE_NEWIPC | CLONE_NEWNET;
59   Mounts mounts_;
60   std::string hostname_;
61   bool allow_mount_propagation_;
62   NetNsMode netns_config_;
63 };
64 
65 }  // namespace sandbox2
66 
67 #endif  // SANDBOXED_API_SANDBOX2_NAMESPACE_H_
68