Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/claybin/bpf/emit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#pragma once

#include <cstdint>
#include <optional>
#include <vector>

#include "claybin/core/error.hpp"
Expand Down Expand Up @@ -78,6 +79,7 @@ inline constexpr std::uint32_t kRetAllow = 0x7fff0000u;

// audit arch tokens, needed for the arch guard.
inline constexpr std::uint32_t kAuditArchX86_64 = 0xc000003eu;
inline constexpr std::uint32_t kAuditArchI386 = 0x40000003u;
inline constexpr std::uint32_t kAuditArchAarch64 = 0xc00000b7u;
inline constexpr std::uint32_t kAuditArchRiscv64 = 0xc00000f3u;

Expand Down Expand Up @@ -136,6 +138,11 @@ std::vector<Interval> fold_intervals(const SyscallPolicy& policy);
// asked for.
Result<Program> compile(const SyscallPolicy& policy, std::uint32_t arch = native_arch());

// Explicit compatibility policy: supported only with an x86_64 native policy.
// No policy means the compatibility ABI is killed. i386 notification is unsupported.
Result<Program> compile(const SyscallPolicy& native, const std::optional<SyscallPolicy>& i386,
std::uint32_t arch = native_arch());

// tiny reference interpreter over struct seccomp_data, used by tests to
// differential-check the tree against a linear lookup. also handy for `explain`.
std::uint32_t evaluate(const Program& prog, std::uint32_t nr, std::uint32_t arch);
Expand Down
16 changes: 15 additions & 1 deletion include/claybin/policy/policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma once

#include <cstdint>
#include <optional>
#include <string>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -69,6 +70,8 @@ struct PolicyData {
NetAuthority net{};
ResourceLimits resources{};
SyscallPolicy syscalls{};
// Absent means the i386 ABI is killed, never inferred from native numbers.
std::optional<SyscallPolicy> syscalls_i386{};
ProcOps proc{};
Isolation isolation{Isolation::process};

Expand Down Expand Up @@ -182,6 +185,8 @@ struct PolicyData {
r.net = net.meet(o.net);
r.resources = resources.meet(o.resources);
r.syscalls = syscalls.meet(o.syscalls);
if (syscalls_i386 && o.syscalls_i386)
r.syscalls_i386 = syscalls_i386->meet(*o.syscalls_i386);
r.proc = proc.meet(o.proc);
// stronger isolation wins, and stronger is the larger enum value.
r.isolation = isolation > o.isolation ? isolation : o.isolation;
Expand Down Expand Up @@ -233,7 +238,9 @@ struct PolicyData {

bool subsumes(const PolicyData& o) const {
return fs.subsumes(o.fs) && net.subsumes(o.net) && resources.subsumes(o.resources) &&
syscalls.subsumes(o.syscalls) && proc.subsumes(o.proc) &&
syscalls.subsumes(o.syscalls) &&
(!o.syscalls_i386 || (syscalls_i386 && syscalls_i386->subsumes(*o.syscalls_i386))) &&
proc.subsumes(o.proc) &&
keep_caps.subsumes(o.keep_caps) && isolation <= o.isolation;
}

Expand Down Expand Up @@ -475,6 +482,10 @@ class Policy<Draft> {
data_.syscalls = std::move(p);
return std::move(*this);
}
Policy&& syscall_profile_i386(SyscallPolicy p) && {
data_.syscalls_i386 = std::move(p);
return std::move(*this);
}

// -- process ------------------------------------------------------------
Policy&& allow_fork() && {
Expand Down Expand Up @@ -642,6 +653,9 @@ class Policy<Sealed> {
d.net = NetAuthority::everything();
d.resources = ResourceLimits::everything();
d.syscalls = SyscallPolicy::everything();
#if defined(__x86_64__)
d.syscalls_i386 = SyscallPolicy::everything();
#endif
d.proc = ProcOps::everything();
d.isolation = Isolation::process;
d.env_cleared = false;
Expand Down
111 changes: 111 additions & 0 deletions include/claybin/policy/profiles_i386.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Native Linux i386 syscall profiles. Numbers from Linux asm/unistd_32.h.
// Deliberately independent of the build host and of the x86_64 syscall table.
#pragma once
#include "claybin/policy/syscalls.hpp"

namespace clay::profiles_i386 {
inline SyscallPolicy base() {
SyscallPolicy p;
p.set_default(SysAction::errno_, 1);
static constexpr SysNr calls[] = {
0, 1, // restart_syscall,exit
3, 4, 6, // read, write, close
19, 140, // lseek, _llseek
20, 24, 47, 49, 50, 64, // pid/uid/gid/euid/egid/ppid
199, 200, 201, 202, // 32-bit uid/gid variants
33, // access
13, 27, 104, 105, // time,alarm,setitimer,getitimer
29, // pause
43, // times
57, 65, 66, 132, 147, // setpgid,getpgrp,setsid,getpgid,getsid
45, // brk
54, 55, 221, // ioctl, fcntl, fcntl64
77, 78, // getrusage,gettimeofday
91, 125, 163, 192, 218, 219, // munmap,mprotect,mremap,mmap2,mincore,madvise
106, 107, 108, 195, 196, 197, // stat/lstat/fstat and stat64 variants
119, 173, 174, 175, 176, 177, 179, 186, // signal handling
122, // uname
158, 162, // sched_yield,nanosleep
180, 181, 183, // pread64,pwrite64,getcwd
184, // capget
191, // ugetrlimit
224, 240, 242, 243, 244, // gettid,futex,affinity,set/get_thread_area
252, 258, // exit_group,set_tid_address
265, 266, 267, // clock_gettime/getres/nanosleep
311, 312, // set/get_robust_list
42,41,63,330,331, // pipe,dup,dup2,dup3,pipe2
254,329,256,319,255, // epoll_create/create1/wait/pwait/ctl
142,168,309,308, // _newselect,poll,ppoll,pselect6
340,116,99,100, // prlimit64,sysinfo,statfs,fstatfs
414,413, // ppoll_time64,pselect6_time64
355, // getrandom
375, 380, 381, 382, // membarrier,pkey_mprotect/alloc/free
386, // rseq
403, 406, 407, 422, // clock_gettime64/getres64/nanosleep64,futex_time64
436, // close_range
};
p.allow(std::span<const SysNr>{calls});
// The old mmap takes a pointer: seccomp cannot inspect its prot field.
p.deny(90, 38);
p.deny(435, 38); // clone3: force inspectable clone fallback
for (SysNr nr : {192u, 125u, 380u})
p.deny_arg_all(nr, 2, 6, SysAction::errno_, 1);
static constexpr std::uint32_t prctl[] = {
1,2,3,4,15,16,21,23,27,29,30,36,37,38,39,40,41,42,0x53564d41};
p.allow(172);
p.allow_arg32_only(172, 0, std::span<const std::uint32_t>{prctl}, SysAction::errno_, 1);
static constexpr std::uint32_t personality[] = {0, 0xffffffffu};
p.allow(136);
p.allow_arg32_only(136, 0, std::span<const std::uint32_t>{personality}, SysAction::errno_, 1);
static constexpr std::uint32_t ioctl[] = {
0x5401,0x5402,0x5403,0x5404,0x5405,0x5409,0x540a,0x540b,
0x5413,0x5414,0x540f,0x5410,0x541b,0x5421,0x5450,0x5451,0x5411};
p.allow_arg32_only(54, 1, std::span<const std::uint32_t>{ioctl}, SysAction::errno_, 1);
for (SysNr nr : {26u /* ptrace */, 21u /* mount */, 52u /* umount2 */,
217u /* pivot_root */, 61u /* chroot */, 310u /* unshare */,
346u /* setns */, 357u /* bpf */, 336u /* perf_event_open */,
128u,129u,350u /* modules */, 283u, /* kexec_load */
101u,110u, /* ioperm,iopl */ 286u,287u,288u, /* keyring */
374u /* userfaultfd */, 425u,426u,427u /* io_uring */,
428u,429u,430u,431u,432u,433u,442u /* mount APIs */})
p.kill(nr);
return p;
}

inline SyscallPolicy compiler() {
auto p = base();
static constexpr SysNr calls[] = {
2, 11, 114, 120, 190, 284, // fork,execve,wait4,clone,vfork,waitid
5,8,9,10,12,14,15,16,38,39,40,41,42,60,63,83,85,92,93,94,95,
118,133,141,145,146,148,193,194,220,229,230,231,232,233,234,
239,250,254,255,256,268,269,271,272,295,296,297,298,299,300,
301,302,303,304,305,306,307,308,309,319,324,328,329,330,331,
333,334,340,353,358,378,379,383,439,
116,99,100,168,142, // sysinfo,statfs,fstatfs,poll,_newselect
187, // sendfile
414,413, // ppoll_time64,pselect6_time64
};
p.allow(std::span<const SysNr>{calls});
// No process profile grants keyring access.
p.deny_arg_any(120, 0, 0x00020000ull | 0x02000000ull | 0x04000000ull |
0x08000000ull | 0x10000000ull | 0x20000000ull | 0x40000000ull,
SysAction::errno_, 1);
// kill/tkill/pidfd_send_signal remain EPERM; no compatibility notify broker.
return p;
}

inline SyscallPolicy compiler_with_network() {
auto p = compiler();
// socketcall's pointer arguments cannot be checked for an allowed address
// family. Use the direct i386 socket syscalls instead, matching native
// argument filtering; old multiplexed socket creation stays fail-closed.
p.deny(102, 38);
static constexpr SysNr calls[] = {
345,337, // sendmmsg,recvmmsg
359,360,361,362,363,364,365,366,367,368,369,370,371,372,373,
417, // recvmmsg_time64
};
p.allow(std::span<const SysNr>{calls});
return p;
}
} // namespace clay::profiles_i386
35 changes: 34 additions & 1 deletion src/bpf/emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,17 @@ Result<Program> compile(const SyscallPolicy& policy, std::uint32_t arch) {
out.push_back(jeq(0xffffffffu, 0, 1));
out.push_back(ret(kRetAllow));

if (arch == kAuditArchX86_64) {
if (arch == kAuditArchX86_64 || arch == kAuditArchI386) {
// reject the x32 range outright rather than trying to reason about it.
out.push_back(jge(kX32SyscallBit, 0, 1));
out.push_back(ret(kRetKillProcess));
}
if (arch == kAuditArchX86_64) {
// Before Linux 5.4 these untagged numbers entered x32 handlers too.
out.push_back(jge(512, 0, 2));
out.push_back(jgt(547, 1, 0));
out.push_back(ret(kRetKillProcess));
}

// ---- argument rules, before the interval tree ------------------------
//
Expand Down Expand Up @@ -355,6 +361,33 @@ Result<Program> compile(const SyscallPolicy& policy, std::uint32_t arch) {
return prog;
}

Result<Program> compile(const SyscallPolicy& native, const std::optional<SyscallPolicy>& i386,
std::uint32_t arch) {
if (!i386) return compile(native, arch);
if (arch != kAuditArchX86_64)
return std::unexpected(Error{Errc::unsupported, "seccomp: i386 requires x86_64 native ABI"});
if (i386->default_action() == SysAction::notify ||
std::any_of(i386->rules().begin(), i386->rules().end(),
[](const auto& r) { return r.action == SysAction::notify; }) ||
std::any_of(i386->arg_rules().begin(), i386->arg_rules().end(),
[](const auto& r) { return r.action == SysAction::notify; }))
return std::unexpected(Error{Errc::unsupported, "seccomp: i386 notification unsupported"});
auto n = compile(native, arch);
if (!n) return std::unexpected(n.error());
auto c = compile(*i386, kAuditArchI386);
if (!c) return std::unexpected(c.error());
// Use long jumps: policy bodies routinely exceed conditional jump reach.
std::vector<Insn> out{ld_abs(kOffArch), jeq(arch, 0, 1), ja(3),
jeq(kAuditArchI386, 1, 0), ret(kRetKillProcess),
ja(static_cast<std::uint32_t>(n->insns.size()))};
out.insert(out.end(), n->insns.begin(), n->insns.end());
out.insert(out.end(), c->insns.begin(), c->insns.end());
if (out.size() > 4096)
return std::unexpected(Error{Errc::unsupported, "seccomp: combined filter too large"});
n->insns = std::move(out);
return n;
}

std::uint32_t evaluate(const Program& prog, std::uint32_t nr, std::uint32_t arch) {
return evaluate_with_args(prog, nr, arch, nullptr);
}
Expand Down
24 changes: 19 additions & 5 deletions src/plan/compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,16 @@ Result<Compiled> compile(const Policy<Sealed>& policy, const HostCapabilities& h
// -- phase: seccomp (last wall) ---------------------------------------
if (host.seccomp) {
SyscallPolicy sys = d.syscalls;
if (d.syscalls_i386) {
const auto& compat = *d.syscalls_i386;
const auto notifies = [](const auto& rule) { return rule.action == SysAction::notify; };
if (compat.default_action() == SysAction::notify ||
std::any_of(compat.rules().begin(), compat.rules().end(), notifies) ||
std::any_of(compat.arg_rules().begin(), compat.arg_rules().end(), notifies) ||
std::any_of(compat.arg_allow_sets().begin(), compat.arg_allow_sets().end(), notifies))
return std::unexpected(Error{Errc::unsupported,
"i386 seccomp notifications require an architecture-aware broker"});
}
if (sys.default_action() == SysAction::kill_process && sys.rules().empty()) {
// a policy that never named a syscall would kill the child before it
// could exec. that is a policy bug, not something to paper over.
Expand Down Expand Up @@ -1239,7 +1249,7 @@ Result<Compiled> compile(const Policy<Sealed>& policy, const HostCapabilities& h
// grant: it has no descriptor to send on once the handshake fd is closed.
if (wants_notify) sys.allow(46 /* sendmsg */);

auto prog = bpf::compile(sys);
auto prog = bpf::compile(sys, d.syscalls_i386);
if (!prog) return std::unexpected(prog.error());

auto blob = std::span<const std::byte>{
Expand All @@ -1259,12 +1269,16 @@ Result<Compiled> compile(const Policy<Sealed>& policy, const HostCapabilities& h
// compiled to SyscallPolicy::everything() and the report cheerfully
// said `strong syscall.filter`, which would have put a green wall on
// screen for a filter the user had just turned off.
if (sys.default_action() == SysAction::allow && sys.rules().empty() &&
sys.arg_rules().empty() && sys.arg_allow_sets().empty()) {
const auto unfiltered = [](const SyscallPolicy& p) {
return p.default_action() == SysAction::allow && p.rules().empty() &&
p.arg_rules().empty() && p.arg_allow_sets().empty();
};
if (unfiltered(sys) || (d.syscalls_i386 && unfiltered(*d.syscalls_i386))) {
report.record(CapId::syscall_filter, Enforcement::partial,
"seccomp-bpf: arch guard only, all syscalls permitted");
"seccomp-bpf: arch guard only for an enabled ABI, all its syscalls permitted");
} else {
report.record(CapId::syscall_filter, Enforcement::strong, "seccomp-bpf");
report.record(CapId::syscall_filter, Enforcement::strong,
d.syscalls_i386 ? "seccomp-bpf: x86_64+i386 (x32 denied)" : "seccomp-bpf");
}
out.brokers_syscalls = wants_notify;
} else {
Expand Down
71 changes: 70 additions & 1 deletion tests/bpf_emit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "claybin/bpf/emit.hpp"
#include "claybin/policy/profiles.hpp"
#include "claybin/policy/profiles_i386.hpp"
#include "claybin/policy/policy.hpp"

using namespace clay;
using namespace clay::bpf;
Expand All @@ -25,7 +27,7 @@ void differential(const SyscallPolicy& p, std::uint32_t max_nr = 600) {
if (!prog) return;
for (SysNr nr = 0; nr < max_nr; ++nr) {
std::uint32_t got = evaluate(*prog, nr, kAuditArchX86_64);
std::uint32_t want = reference(p, nr);
std::uint32_t want = nr >= 512 && nr <= 547 ? kRetKillProcess : reference(p, nr);
if (got != want) {
std::fprintf(stderr, " nr=%u got=%#x want=%#x\n", nr, got, want);
CHECK_EQ(got, want);
Expand All @@ -38,6 +40,73 @@ void differential(const SyscallPolicy& p, std::uint32_t max_nr = 600) {
} // namespace

int main() {
{
auto native = SyscallPolicy::everything();
auto compat = profiles_i386::compiler();
auto prog = compile(native, std::optional{compat}, kAuditArchX86_64);
CHECK(prog.has_value());
if (prog) {
CHECK_EQ(evaluate(*prog, 11, kAuditArchI386), kRetAllow); // execve
for (auto nr : {13u, 27u, 104u, 105u, 29u, 57u, 65u, 66u, 132u, 147u})
CHECK_EQ(evaluate(*prog, nr, kAuditArchI386), kRetAllow);
CHECK_EQ(evaluate(*prog, 3, 0u), kRetKillProcess); // unknown audit arch
// Upstream's tracer-cancelled syscall exception applies only after
// architecture validation, and must survive multi-ABI dispatch.
CHECK_EQ(evaluate(*prog, 0xffffffffu, kAuditArchX86_64), kRetAllow);
CHECK_EQ(evaluate(*prog, 0xffffffffu, kAuditArchI386), kRetAllow);
CHECK_EQ(evaluate(*prog, 0xffffffffu, 0u), kRetKillProcess);
CHECK_EQ(evaluate(*prog, 59, kAuditArchI386), kRetErrno | 1u);
CHECK_EQ(evaluate(*prog, 59, kAuditArchX86_64), kRetAllow);
CHECK_EQ(evaluate(*prog, 11, kAuditArchAarch64), kRetKillProcess);
for (auto nr : {512u, 513u, 547u, 0x40000000u, 0x40000200u})
CHECK_EQ(evaluate(*prog, nr, kAuditArchX86_64), kRetKillProcess);
for (auto nr : {511u, 548u})
CHECK_EQ(evaluate(*prog, nr, kAuditArchX86_64), kRetAllow);
CHECK_EQ(evaluate(*prog, 0x40000003u, kAuditArchI386), kRetKillProcess);
CHECK_EQ(evaluate(*prog, 90, kAuditArchI386), kRetErrno | 38u);
for (auto nr : {192u,125u,380u}) {
std::uint64_t args[6]{};
args[2] = 6;
CHECK_EQ(evaluate_with_args(*prog, nr, kAuditArchI386, args), kRetErrno | 1u);
args[2] = 5;
CHECK_EQ(evaluate_with_args(*prog, nr, kAuditArchI386, args), kRetAllow);
}
std::uint64_t args[6]{0x10000000u};
CHECK_EQ(evaluate_with_args(*prog, 120, kAuditArchI386, args), kRetErrno | 1u);
args[0] = 0x100;
CHECK_EQ(evaluate_with_args(*prog, 120, kAuditArchI386, args), kRetAllow);
CHECK_EQ(evaluate(*prog, 37, kAuditArchI386), kRetErrno | 1u); // kill
CHECK_EQ(evaluate(*prog, 26, kAuditArchI386), kRetKillProcess); // ptrace
CHECK_EQ(evaluate(*prog, 288, kAuditArchI386), kRetKillProcess); // keyctl
args[1] = 0x100005401ull; // ioctl ignores upper register bits
CHECK_EQ(evaluate_with_args(*prog, 54, kAuditArchI386, args), kRetAllow);
args[1] = 0x5412; // TIOCSTI
CHECK_EQ(evaluate_with_args(*prog, 54, kAuditArchI386, args), kRetErrno | 1u);
}
auto only_native = compile(native, std::optional<SyscallPolicy>{}, kAuditArchX86_64);
CHECK(only_native.has_value());
CHECK_EQ(evaluate(*only_native, 11, kAuditArchI386), kRetKillProcess);
compat.notify(3);
CHECK(!compile(native, std::optional{compat}, kAuditArchX86_64));
auto network = profiles_i386::compiler_with_network();
CHECK_EQ(network.action_for(102), SysAction::errno_);
CHECK_EQ(network.errno_for(102), std::uint16_t{38});
auto network_prog = compile(native, std::optional{network}, kAuditArchX86_64);
CHECK(network_prog.has_value());
if (network_prog) {
CHECK_EQ(evaluate(*network_prog, 102, kAuditArchI386), kRetErrno | 38u);
CHECK_EQ(evaluate(*network_prog, 359, kAuditArchI386), kRetAllow); // socket
}
PolicyData a, b;
a.syscalls_i386 = profiles_i386::compiler();
CHECK(a.subsumes(b));
CHECK(!b.subsumes(a));
CHECK(!a.meet(b).syscalls_i386);
CHECK(!b.meet(a).syscalls_i386);
b.syscalls_i386 = SyscallPolicy::everything();
CHECK(b.subsumes(a));
CHECK(a.meet(b).syscalls_i386.has_value());
}
// -- arch guard --------------------------------------------------------
{
SyscallPolicy p;
Expand Down
Loading