Skip to content

Commit 30757db

Browse files
happyCoder92copybara-github
authored andcommitted
Add AllowMmap/AllowMprotect/AllowDynamicStartup variants with executable mappings
PiperOrigin-RevId: 761880262 Change-Id: I9140f3723e9353190f866bcdd4547f34ab636cd5
1 parent 75b6c16 commit 30757db

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed

sandboxed_api/sandbox2/policybuilder.cc

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
#include <memory>
4343
#include <optional>
4444
#include <string>
45-
#include <type_traits>
4645
#include <utility>
4746
#include <vector>
4847

@@ -57,6 +56,7 @@
5756
#include "absl/types/span.h"
5857
#include "sandboxed_api/config.h"
5958
#include "sandboxed_api/sandbox2/allowlists/all_syscalls.h"
59+
#include "sandboxed_api/sandbox2/allowlists/map_exec.h"
6060
#include "sandboxed_api/sandbox2/allowlists/namespaces.h"
6161
#include "sandboxed_api/sandbox2/allowlists/seccomp_speculation.h"
6262
#include "sandboxed_api/sandbox2/allowlists/trace_all_syscalls.h"
@@ -591,9 +591,14 @@ PolicyBuilder& PolicyBuilder::AllowMprotectWithoutExec() {
591591
});
592592
}
593593

594-
std::enable_if_t<builder_internal::is_type_complete_v<MapExec>, PolicyBuilder&>
595-
PolicyBuilder::AllowMmap() {
596-
return AllowSyscalls(kMmapSyscalls);
594+
PolicyBuilder& PolicyBuilder::AllowMprotect(MapExec) {
595+
return Allow(MapExec()).AllowSyscall(__NR_mprotect);
596+
}
597+
598+
PolicyBuilder& PolicyBuilder::AllowMmap() { return AllowMmap(MapExec()); }
599+
600+
PolicyBuilder& PolicyBuilder::AllowMmap(MapExec) {
601+
return AllowSyscalls(kMmapSyscalls).AllowSyscall(__NR_mprotect);
597602
}
598603

599604
PolicyBuilder& PolicyBuilder::AllowMlock() {
@@ -1259,13 +1264,12 @@ PolicyBuilder& PolicyBuilder::AllowStaticStartup() {
12591264
return *this;
12601265
}
12611266

1262-
std::enable_if_t<builder_internal::is_type_complete_v<MapExec>, PolicyBuilder&>
1263-
PolicyBuilder::AllowDynamicStartup() {
1264-
if (!allow_map_exec_) {
1265-
SetError(absl::FailedPreconditionError(
1266-
"Allowing dynamic startup requires Allow(MapExec)."));
1267-
return *this;
1268-
}
1267+
PolicyBuilder& PolicyBuilder::AllowDynamicStartup() {
1268+
return AllowDynamicStartup(MapExec());
1269+
}
1270+
1271+
PolicyBuilder& PolicyBuilder::AllowDynamicStartup(MapExec) {
1272+
Allow(MapExec());
12691273
if (allowed_complex_.dynamic_startup) {
12701274
return *this;
12711275
}

sandboxed_api/sandbox2/policybuilder.h

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <memory>
2424
#include <optional>
2525
#include <string>
26-
#include <type_traits>
2726
#include <utility>
2827
#include <vector>
2928

@@ -55,16 +54,6 @@ class SeccompSpeculation;
5554
class TraceAllSyscalls;
5655
class UnrestrictedNetworking;
5756

58-
namespace builder_internal {
59-
60-
template <typename, typename = void>
61-
constexpr bool is_type_complete_v = false;
62-
63-
template <typename T>
64-
constexpr bool is_type_complete_v<T, std::void_t<decltype(sizeof(T))>> = true;
65-
66-
} // namespace builder_internal
67-
6857
// PolicyBuilder is a helper class to simplify creation of policies. The builder
6958
// uses fluent interface for convenience and increased readability of policies.
7059
//
@@ -336,18 +325,19 @@ class PolicyBuilder final {
336325
// Appends code to unconditionally allow mmap. Specifically this allows mmap
337326
// and mmap2 syscall on architectures where these syscalls exist.
338327
//
339-
// This function requires that targets :map_exec library to be linked
340-
// against. Otherwise, the PolicyBuilder will fail to build the policy.
341-
//
342328
// Prefer using `AllowMmapWithoutExec()` as allowing mapping executable pages
343329
// makes exploitation easier.
344-
std::enable_if_t<builder_internal::is_type_complete_v<MapExec>,
345-
PolicyBuilder&>
346-
AllowMmap();
330+
PolicyBuilder& AllowMmap(MapExec);
331+
332+
ABSL_DEPRECATED("Use AllowMmap(MapExec) or AllowMmapWithoutExec() instead.")
333+
PolicyBuilder& AllowMmap();
347334

348335
// Appends code to allow mmap calls that don't specify PROT_EXEC.
349336
PolicyBuilder& AllowMmapWithoutExec();
350337

338+
// Appends code to allow mprotect (also with PROT_EXEC).
339+
PolicyBuilder& AllowMprotect(MapExec);
340+
351341
// Appends code to allow mprotect calls that don't specify PROT_EXEC.
352342
PolicyBuilder& AllowMprotectWithoutExec();
353343

@@ -708,9 +698,10 @@ class PolicyBuilder final {
708698
//
709699
// In addition to syscalls allowed by `AllowStaticStartup`, also allow
710700
// reading, seeking, mmap()-ing and closing files.
711-
std::enable_if_t<builder_internal::is_type_complete_v<MapExec>,
712-
PolicyBuilder&>
713-
AllowDynamicStartup();
701+
PolicyBuilder& AllowDynamicStartup(MapExec);
702+
703+
ABSL_DEPRECATED("Use AllowDynamicStartup(MapExec) instead.")
704+
PolicyBuilder& AllowDynamicStartup();
714705

715706
// Appends a policy, which will be run on the specified syscall.
716707
//

0 commit comments

Comments
 (0)