Skip to content

Commit 31a4fcf

Browse files
okunzcopybara-github
authored andcommitted
allowlists: Create allowlist for DisableNamespaces.
This allows to control who is able to disable Linux namespaces via the policybuilder. Namespaces are a core element of Sandbox2's security posture. We're therefore implementing a similar mechanism as existing for allowing to call all syscalls. We also introduce a new location for all allowlist tokens to better manage the code concerned with this functionality. PiperOrigin-RevId: 721816939 Change-Id: Ic00b1ff9754afc779c4c5155d1ec3c059c3ff5c9
1 parent 02b329d commit 31a4fcf

File tree

7 files changed

+117
-12
lines changed

7 files changed

+117
-12
lines changed

sandboxed_api/sandbox2/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ cc_library(
631631
"@com_google_absl//absl/types:optional",
632632
"@com_google_absl//absl/types:span",
633633
"@com_google_sandboxed_api//sandboxed_api:config",
634+
"@com_google_sandboxed_api//sandboxed_api/sandbox2/allowlists:namespaces",
634635
"@com_google_sandboxed_api//sandboxed_api/sandbox2/network_proxy:filtering",
635636
"@com_google_sandboxed_api//sandboxed_api/sandbox2/util:bpf_helper",
636637
"@com_google_sandboxed_api//sandboxed_api/util:file_base",

sandboxed_api/sandbox2/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
add_subdirectory(allowlists)
1516
add_subdirectory(examples)
1617
add_subdirectory(unwind)
1718
add_subdirectory(util)
@@ -559,6 +560,7 @@ target_link_libraries(sandbox2_policybuilder
559560
sandbox2::bpf_helper
560561
sandbox2::namespace
561562
sandbox2::syscall
563+
sandbox2::allowlists_namespaces
562564
sapi::file_base
563565
sapi::fileops
564566
sapi::status
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2025 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+
# Description: Collection of allowlist tokens that are used to visibility
16+
# restrict features in Sandbox2.
17+
18+
load("@com_google_sandboxed_api//sandboxed_api/bazel:build_defs.bzl", "sapi_platform_copts")
19+
20+
licenses(["notice"])
21+
22+
cc_library(
23+
name = "namespaces",
24+
hdrs = ["namespaces.h"],
25+
copts = sapi_platform_copts(),
26+
visibility = ["//visibility:public"],
27+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2025 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+
# Description: Collection of allowlist tokens that are used to visibility
16+
# restrict features in Sandbox2.
17+
18+
# sandboxed_api/sandbox2/allowlists:namespaces
19+
add_library(sandbox2_allowlists_namespaces ${SAPI_LIB_TYPE}
20+
namespaces.h
21+
)
22+
add_library(sandbox2::allowlists_namespaces ALIAS sandbox2_allowlists_namespaces)
23+
target_link_libraries(sandbox2_allowlists_namespaces PRIVATE
24+
sapi::base
25+
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2025 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+
#ifndef SANDBOXED_API_SANDBOX2_ALLOWLISTS_NAMESPACES_H_
16+
#define SANDBOXED_API_SANDBOX2_ALLOWLISTS_NAMESPACES_H_
17+
18+
namespace sandbox2 {
19+
20+
class NamespacesToken {
21+
public:
22+
explicit NamespacesToken() = default;
23+
};
24+
25+
} // namespace sandbox2
26+
27+
#endif // SANDBOXED_API_SANDBOX2_ALLOWLISTS_NAMESPACES_H_

sandboxed_api/sandbox2/policybuilder.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include "sandboxed_api/sandbox2/allow_all_syscalls.h"
6060
#include "sandboxed_api/sandbox2/allow_seccomp_speculation.h"
6161
#include "sandboxed_api/sandbox2/allow_unrestricted_networking.h"
62+
#include "sandboxed_api/sandbox2/allowlists/namespaces.h"
6263
#include "sandboxed_api/sandbox2/forkserver.pb.h"
6364
#include "sandboxed_api/sandbox2/namespace.h"
6465
#include "sandboxed_api/sandbox2/network_proxy/filtering.h"
@@ -165,6 +166,23 @@ bool IsOnReadOnlyDev(const std::string& path) {
165166

166167
} // namespace
167168

169+
PolicyBuilder& PolicyBuilder::DisableNamespaces(NamespacesToken) {
170+
if (requires_namespaces_) {
171+
SetError(absl::FailedPreconditionError(
172+
"Namespaces cannot be both disabled and enabled. You're probably "
173+
"using features that implicitly enable namespaces (SetHostname, "
174+
"AddFile, AddDirectory, AddDataDependency, AddLibrariesForBinary "
175+
"or similar)"));
176+
return *this;
177+
}
178+
use_namespaces_ = false;
179+
return *this;
180+
}
181+
182+
PolicyBuilder& PolicyBuilder::DisableNamespaces() {
183+
return DisableNamespaces(NamespacesToken());
184+
}
185+
168186
PolicyBuilder& PolicyBuilder::Allow(MapExec) {
169187
allow_map_exec_ = true;
170188
return *this;

sandboxed_api/sandbox2/policybuilder.h

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct bpf_labels;
4646
namespace sandbox2 {
4747

4848
class AllowAllSyscalls;
49+
class NamespacesToken;
4950
class LoadUserBpfCodeFromFile;
5051
class MapExec;
5152
class SeccompSpeculation;
@@ -155,6 +156,20 @@ class PolicyBuilder final {
155156
return (Allow(tags), ...);
156157
}
157158

159+
// Disables the use of namespaces.
160+
//
161+
// The default security posture of Sandbox2 depends on the use of namespaces
162+
// and syscall filters. By disabling namespaces, the default security posture
163+
// is weakened.
164+
//
165+
// The consequence of disabling namespaces is that the sandboxee will be able
166+
// to access the host's file system, network, and other resources if the
167+
// appropriate syscalls are also allowed.
168+
//
169+
// Disabling namespaces is not recommended and should only be done if
170+
// absolutely necessary.
171+
PolicyBuilder& DisableNamespaces(NamespacesToken);
172+
158173
// Allows the use of memory mappings that are marked as executable.
159174
//
160175
// This applies to the mmap and mprotect syscalls and by default, mapped
@@ -845,18 +860,8 @@ class PolicyBuilder final {
845860
// This will disable *all* namespaces.
846861
//
847862
// IMPORTANT: This is not recommended.
848-
PolicyBuilder& DisableNamespaces() {
849-
if (requires_namespaces_) {
850-
SetError(absl::FailedPreconditionError(
851-
"Namespaces cannot be both disabled and enabled. You're probably "
852-
"using features that implicitly enable namespaces (SetHostname, "
853-
"AddFile, AddDirectory, AddDataDependency, AddLibrariesForBinary "
854-
"or similar)"));
855-
return *this;
856-
}
857-
use_namespaces_ = false;
858-
return *this;
859-
}
863+
ABSL_DEPRECATED("Use DisableNamespaces(NamespacesToken()) instead.")
864+
PolicyBuilder& DisableNamespaces();
860865

861866
// Set hostname in the network namespace.
862867
//

0 commit comments

Comments
 (0)