@@ -75,15 +75,16 @@ namespace sandbox2 {
75
75
// 1. default policy (GetDefaultPolicy, private),
76
76
// 2. user policy (user_policy_, public),
77
77
// 3. default KILL action (avoid failing open if user policy did not do it).
78
- std::vector<sock_filter> Policy::GetPolicy (bool user_notif) const {
78
+ std::vector<sock_filter> Policy::GetPolicy (
79
+ bool user_notif, bool enable_sandboxing_pre_execve) const {
79
80
if (absl::GetFlag (FLAGS_sandbox2_danger_danger_permit_all) ||
80
81
!absl::GetFlag (FLAGS_sandbox2_danger_danger_permit_all_and_log).empty ()) {
81
82
return GetTrackingPolicy ();
82
83
}
83
84
84
85
// Now we can start building the policy.
85
86
// 1. Start with the default policy (e.g. syscall architecture checks).
86
- auto policy = GetDefaultPolicy (user_notif);
87
+ auto policy = GetDefaultPolicy (user_notif, enable_sandboxing_pre_execve );
87
88
VLOG (3 ) << " Default policy:\n " << bpf::Disasm (policy);
88
89
89
90
// 2. Append user policy.
@@ -105,7 +106,8 @@ std::vector<sock_filter> Policy::GetPolicy(bool user_notif) const {
105
106
// Produces a policy which returns SECCOMP_RET_TRACE instead of SECCOMP_RET_KILL
106
107
// for the __NR_execve syscall, so the tracer can make a decision to allow or
107
108
// disallow it depending on which occurrence of __NR_execve it was.
108
- std::vector<sock_filter> Policy::GetDefaultPolicy (bool user_notif) const {
109
+ std::vector<sock_filter> Policy::GetDefaultPolicy (
110
+ bool user_notif, bool enable_sandboxing_pre_execve) const {
109
111
bpf_labels l = {0 };
110
112
111
113
std::vector<sock_filter> policy;
@@ -122,16 +124,21 @@ std::vector<sock_filter> Policy::GetDefaultPolicy(bool user_notif) const {
122
124
ALLOW,
123
125
LABEL (&l, past_seccomp_l),
124
126
LOAD_SYSCALL_NR,
125
- JNE32 (__NR_execveat, JUMP (&l, past_execveat_l)),
126
- ARG_32 (4 ),
127
- JNE32 (AT_EMPTY_PATH, JUMP (&l, past_execveat_l)),
128
- ARG_32 (5 ),
129
- JNE32 (internal::kExecveMagic , JUMP (&l, past_execveat_l)),
130
- ALLOW,
131
- LABEL (&l, past_execveat_l),
132
-
133
- LOAD_SYSCALL_NR,
134
127
};
128
+ if (enable_sandboxing_pre_execve) {
129
+ policy.insert (
130
+ policy.end (),
131
+ {
132
+ JNE32 (__NR_execveat, JUMP (&l, past_execveat_l)),
133
+ ARG_32 (4 ),
134
+ JNE32 (AT_EMPTY_PATH, JUMP (&l, past_execveat_l)),
135
+ ARG_32 (5 ),
136
+ JNE32 (internal::kExecveMagic , JUMP (&l, past_execveat_l)),
137
+ ALLOW,
138
+ LABEL (&l, past_execveat_l),
139
+ LOAD_SYSCALL_NR,
140
+ });
141
+ }
135
142
} else {
136
143
policy = {
137
144
// If compiled arch is different from the runtime one, inform the
@@ -144,23 +151,28 @@ std::vector<sock_filter> Policy::GetDefaultPolicy(bool user_notif) const {
144
151
TRACE (sapi::cpu::kUnknown ),
145
152
LABEL (&l, past_arch_check_l),
146
153
147
- // After the policy is uploaded, forkserver will execve the sandboxee.
148
- // We need to allow this execve but not others. Since BPF does not have
149
- // state, we need to inform the Monitor to decide, and for that we use a
150
- // magic value in syscall args 5. Note that this value is not supposed
151
- // to be secret, but just an optimization so that the monitor is not
152
- // triggered on every call to execveat.
153
- LOAD_SYSCALL_NR,
154
- JNE32 (__NR_execveat, JUMP (&l, past_execveat_l)),
155
- ARG_32 (4 ),
156
- JNE32 (AT_EMPTY_PATH, JUMP (&l, past_execveat_l)),
157
- ARG_32 (5 ),
158
- JNE32 (internal::kExecveMagic , JUMP (&l, past_execveat_l)),
159
- SANDBOX2_TRACE,
160
- LABEL (&l, past_execveat_l),
161
-
162
154
LOAD_SYSCALL_NR,
163
155
};
156
+ if (enable_sandboxing_pre_execve) {
157
+ // After the policy is uploaded, forkserver will execve the sandboxee.
158
+ // We need to allow this execve but not others. Since BPF does not have
159
+ // state, we need to inform the Monitor to decide, and for that we use a
160
+ // magic value in syscall args 5. Note that this value is not supposed
161
+ // to be secret, but just an optimization so that the monitor is not
162
+ // triggered on every call to execveat.
163
+ policy.insert (
164
+ policy.end (),
165
+ {
166
+ JNE32 (__NR_execveat, JUMP (&l, past_execveat_l)),
167
+ ARG_32 (4 ),
168
+ JNE32 (AT_EMPTY_PATH, JUMP (&l, past_execveat_l)),
169
+ ARG_32 (5 ),
170
+ JNE32 (internal::kExecveMagic , JUMP (&l, past_execveat_l)),
171
+ SANDBOX2_TRACE,
172
+ LABEL (&l, past_execveat_l),
173
+ LOAD_SYSCALL_NR,
174
+ });
175
+ }
164
176
}
165
177
166
178
// Insert a custom syscall to signal the sandboxee it's running inside a
0 commit comments