Skip to content

Commit 9109204

Browse files
happyCoder92copybara-github
authored andcommitted
Add a test for AllowSpeculation
PiperOrigin-RevId: 742178834 Change-Id: I9e2006586d101f91281e7bb367fa21a6461d691d
1 parent d49863c commit 9109204

4 files changed

Lines changed: 59 additions & 0 deletions

File tree

sandboxed_api/sandbox2/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,7 @@ cc_test(
959959
":sandbox2",
960960
"//sandboxed_api:config",
961961
"//sandboxed_api:testing",
962+
"//sandboxed_api/sandbox2/allowlists:seccomp_speculation",
962963
"//sandboxed_api/sandbox2/util:bpf_helper",
963964
"//sandboxed_api/util:file_base",
964965
"//sandboxed_api/util:status_matchers",

sandboxed_api/sandbox2/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,7 @@ if(BUILD_TESTING AND SAPI_BUILD_TESTING)
10381038
)
10391039
target_link_libraries(sandbox2_policy_test PRIVATE
10401040
absl::strings
1041+
sandbox2::allowlists_seccomp_speculation
10411042
sandbox2::bpf_helper
10421043
sapi::config
10431044
sandbox2::sandbox2

sandboxed_api/sandbox2/policy_test.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "absl/strings/match.h"
3030
#include "absl/strings/string_view.h"
3131
#include "sandboxed_api/config.h"
32+
#include "sandboxed_api/sandbox2/allowlists/seccomp_speculation.h"
3233
#include "sandboxed_api/sandbox2/executor.h"
3334
#include "sandboxed_api/sandbox2/policybuilder.h"
3435
#include "sandboxed_api/sandbox2/result.h"
@@ -410,6 +411,28 @@ TEST_P(PolicyTest, SecondExecveatNotAllowedByDefault) {
410411
EXPECT_THAT(result.reason_code(), Eq(0));
411412
}
412413

414+
TEST_P(PolicyTest, SpeculationAllowed) {
415+
const std::string path = GetTestSourcePath("sandbox2/testcases/policy");
416+
std::unique_ptr<Sandbox2> s2 = CreateTestSandbox(
417+
{"policy", "11"}, // Calls TestSpeculationAllowed()
418+
CreateDefaultPermissiveTestPolicy(path).Allow(SeccompSpeculation()));
419+
Result result = s2->Run();
420+
421+
ASSERT_THAT(result.final_status(), Eq(Result::OK));
422+
EXPECT_THAT(result.reason_code(), Eq(0));
423+
}
424+
425+
TEST_P(PolicyTest, SpeculationBlockedByDefault) {
426+
const std::string path = GetTestSourcePath("sandbox2/testcases/policy");
427+
std::unique_ptr<Sandbox2> s2 =
428+
CreateTestSandbox({"policy", "12"}, // Calls TestSpeculationBlocked()
429+
CreateDefaultPermissiveTestPolicy(path));
430+
Result result = s2->Run();
431+
432+
ASSERT_THAT(result.final_status(), Eq(Result::OK));
433+
EXPECT_THAT(result.reason_code(), Eq(0));
434+
}
435+
413436
INSTANTIATE_TEST_SUITE_P(Sandbox2, PolicyTest, ::testing::Values(false, true),
414437
[](const ::testing::TestParamInfo<bool>& info) {
415438
return info.param ? "UnotifyMonitor"

sandboxed_api/sandbox2/testcases/policy.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
// A binary that tries x86_64 compat syscalls, ptrace and clone untraced.
1616

17+
#include <linux/prctl.h>
1718
#include <sched.h>
19+
#include <sys/prctl.h>
1820
#include <sys/ptrace.h>
1921
#include <syscall.h>
2022
#include <unistd.h>
@@ -113,6 +115,32 @@ void TestSafeBpf() {
113115

114116
void TestIsatty() { isatty(0); }
115117

118+
void TestSpeculationAllowed() {
119+
int res = prctl(PR_GET_SPECULATION_CTRL, PR_SPEC_STORE_BYPASS, 0, 0, 0);
120+
if (res == PR_SPEC_ENABLE) {
121+
printf("PR_SPEC_STORE_BYPASS enabled when it should not have been\n");
122+
exit(EXIT_FAILURE);
123+
}
124+
res = prctl(PR_GET_SPECULATION_CTRL, PR_SPEC_INDIRECT_BRANCH, 0, 0, 0);
125+
if (res == PR_SPEC_ENABLE) {
126+
printf("PR_SPEC_INDIRECT_BRANCH enabled when it should not have been\n");
127+
exit(EXIT_FAILURE);
128+
}
129+
}
130+
131+
void TestSpeculationBlocked() {
132+
int res = prctl(PR_GET_SPECULATION_CTRL, PR_SPEC_STORE_BYPASS, 0, 0, 0);
133+
if ((res != -1 && errno == EINVAL) && res != PR_SPEC_ENABLE) {
134+
printf("PR_SPEC_STORE_BYPASS disabled when it should not have been\n");
135+
exit(EXIT_FAILURE);
136+
}
137+
res = prctl(PR_GET_SPECULATION_CTRL, PR_SPEC_INDIRECT_BRANCH, 0, 0, 0);
138+
if ((res != -1 && errno == EINVAL) && res != PR_SPEC_ENABLE) {
139+
printf("PR_SPEC_INDIRECT_BRANCH disabled when it should not have been\n");
140+
exit(EXIT_FAILURE);
141+
}
142+
}
143+
116144
int main(int argc, char* argv[]) {
117145
// Disable buffering.
118146
setbuf(stdin, nullptr);
@@ -155,6 +183,12 @@ int main(int argc, char* argv[]) {
155183
case 9:
156184
TestSafeBpf();
157185
break;
186+
case 11:
187+
TestSpeculationAllowed();
188+
break;
189+
case 12:
190+
TestSpeculationBlocked();
191+
break;
158192
default:
159193
printf("Unknown test: %d\n", testno);
160194
return EXIT_FAILURE;

0 commit comments

Comments
 (0)