2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// A binary that tries x86_64 compat syscalls, ptrace and clone untraced.
|
|
|
|
|
|
|
|
#include <sched.h>
|
|
|
|
#include <sys/ptrace.h>
|
|
|
|
#include <syscall.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-12-03 00:37:55 +08:00
|
|
|
#include <cerrno>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/config.h"
|
2020-09-10 20:47:32 +08:00
|
|
|
|
|
|
|
#ifdef SAPI_X86_64
|
2019-03-19 00:21:48 +08:00
|
|
|
void TestAMD64SyscallMismatch() {
|
|
|
|
int64_t result;
|
|
|
|
|
|
|
|
// exit() is allowed, but not if called via 32-bit syscall.
|
|
|
|
asm("movq $1, %%rax\n" // __NR_exit: 1 in 32-bit (60 in 64-bit)
|
|
|
|
"movq $42, %%rbx\n" // int error_code: 42
|
|
|
|
"int $0x80\n"
|
|
|
|
"movq %%rax, %0\n"
|
|
|
|
: "=r"(result)
|
|
|
|
:
|
|
|
|
: "rax", "rbx");
|
|
|
|
exit(-result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestAMD64SyscallMismatchFs() {
|
|
|
|
int64_t result;
|
|
|
|
char filename[] = "/etc/passwd";
|
|
|
|
|
|
|
|
// access("/etc/passwd") is allowed, but not if called via 32-bit syscall.
|
|
|
|
asm("movq $33, %%rax\n" // __NR_access: 33 in 32-bit (21 in 64-bit)
|
|
|
|
"movq %1, %%rbx\n" // const char* filename: /etc/passwd
|
|
|
|
"movq $0, %%rcx\n" // int mode: F_OK (0), test for existence
|
|
|
|
"int $0x80\n"
|
|
|
|
"movq %%rax, %0\n"
|
|
|
|
: "=r"(result)
|
|
|
|
: "g"(filename)
|
|
|
|
: "rax", "rbx", "rcx");
|
|
|
|
exit(-result);
|
|
|
|
}
|
2020-09-10 20:47:32 +08:00
|
|
|
#endif
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2022-05-27 17:57:03 +08:00
|
|
|
void TestPtraceDenied() {
|
2019-03-19 00:21:48 +08:00
|
|
|
ptrace(PTRACE_SEIZE, getppid(), 0, 0);
|
|
|
|
|
|
|
|
printf("Syscall violation should have been discovered by now\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2022-05-27 17:57:03 +08:00
|
|
|
void TestPtraceBlocked() {
|
|
|
|
int result = ptrace(PTRACE_SEIZE, getppid(), 0, 0);
|
|
|
|
|
|
|
|
if (result != -1 || errno != EPERM) {
|
|
|
|
printf("System call should have been blocked\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
void TestCloneUntraced() {
|
|
|
|
syscall(__NR_clone, static_cast<uintptr_t>(CLONE_UNTRACED), nullptr, nullptr,
|
|
|
|
nullptr, static_cast<uintptr_t>(0));
|
|
|
|
|
|
|
|
printf("Syscall violation should have been discovered by now\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestBpf() {
|
|
|
|
syscall(__NR_bpf, 0, nullptr, 0);
|
|
|
|
|
|
|
|
printf("Syscall violation should have been discovered by now\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2020-12-03 00:37:55 +08:00
|
|
|
void TestBpfError() {
|
|
|
|
exit(syscall(__NR_bpf, 0, nullptr, 0) == -1 ? errno : 0);
|
|
|
|
}
|
|
|
|
|
2019-03-20 19:10:57 +08:00
|
|
|
void TestIsatty() {
|
|
|
|
isatty(0);
|
|
|
|
}
|
|
|
|
|
2022-04-27 15:43:17 +08:00
|
|
|
int main(int argc, char* argv[]) {
|
2019-03-19 00:21:48 +08:00
|
|
|
// Disable buffering.
|
|
|
|
setbuf(stdin, nullptr);
|
|
|
|
setbuf(stdout, nullptr);
|
|
|
|
setbuf(stderr, nullptr);
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
printf("argc < 3\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int testno = atoi(argv[1]); // NOLINT
|
|
|
|
switch (testno) {
|
2020-09-10 20:47:32 +08:00
|
|
|
#ifdef SAPI_X86_64
|
2019-03-19 00:21:48 +08:00
|
|
|
case 1:
|
|
|
|
TestAMD64SyscallMismatch();
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
TestAMD64SyscallMismatchFs();
|
|
|
|
break;
|
2020-09-10 20:47:32 +08:00
|
|
|
#endif
|
2019-03-19 00:21:48 +08:00
|
|
|
case 3:
|
2022-05-27 17:57:03 +08:00
|
|
|
TestPtraceDenied();
|
2019-03-19 00:21:48 +08:00
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
TestCloneUntraced();
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
TestBpf();
|
|
|
|
break;
|
2019-03-20 19:10:57 +08:00
|
|
|
case 6:
|
|
|
|
TestIsatty();
|
|
|
|
break;
|
2020-12-03 00:37:55 +08:00
|
|
|
case 7:
|
|
|
|
TestBpfError();
|
|
|
|
break;
|
2022-05-27 17:57:03 +08:00
|
|
|
case 8:
|
|
|
|
TestPtraceBlocked();
|
|
|
|
break;
|
2019-03-19 00:21:48 +08:00
|
|
|
default:
|
|
|
|
printf("Unknown test: %d\n", testno);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("OK: All tests went OK\n");
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|