Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

6.3 KB
Raw Blame
// Copyright 2026 The OpenSandbox Authors
//
// 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
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// 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.

// OSEP-0018 §5: opt-in, observation-only audit of exec / connect /
// privilege events, scoped to the sandbox cgroup. Compiles with bpf2go
// (CO-RE); the generated bytecode is embedded in the execd-ebpf build
// variant only.

// vmlinux.h is intentionally NOT used: CO-RE resolves every accessor by
// member name against the target kernel's BTF at load time, so the compile
// time layout does not need to match any kernel. audit_types.h declares the
// handful of kernel structures/members these programs touch (by name), which
// keeps the build hermetic, per-arch-vmlinux-free and small. See the header.
#include "audit_types.h"
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

char LICENSE[] SEC("license") = "GPL";

#define AF_INET 2
#define TCP_SYN_SENT 2

// Events ring buffer, consumed by the execd-ebpf process.
struct {
    __uint(type, BPF_MAP_TYPE_RINGBUF);
    __uint(max_entries, 1 << 20);
} events SEC(".maps");

// Set from userspace to the sandbox cgroup id; events outside it are
// dropped (execd shares the sandbox cgroup with every workload process).
const volatile uint64_t target_cgroup;

struct event_exec {
    uint32_t pid;
    uint32_t ppid;
    char comm[16];
    char filename[64];
} __attribute__((packed));

struct event_connect {
    uint32_t pid;
    char comm[16];
    uint32_t dst_ip[4]; // big-endian; IPv4 is stored in dst_ip[3]
    uint16_t dst_port;  // network byte order
} __attribute__((packed));

struct event_privilege {
    uint32_t pid;
    char comm[16];
    uint32_t old_uid;
    uint32_t new_uid;
    uint32_t old_gid;
    uint32_t new_gid;
    uint64_t cap_added; // caps in new_cred not in old_cred
} __attribute__((packed));

static __always_inline int emit(void *event, uint32_t size)
{
    if (target_cgroup &&
        bpf_get_current_cgroup_id() != target_cgroup)
        return 0;
    return bpf_ringbuf_output(&events, event, size, 0) == 0;
}

SEC("tracepoint/sched/sched_process_exec")
int on_exec(struct trace_event_raw_sched_process_exec *ctx)
{
    struct event_exec ev = {};
    struct task_struct *task;
    struct task_struct *parent;

    ev.pid = bpf_get_current_pid_tgid() >> 32;
    task = (struct task_struct *)bpf_get_current_task();
    bpf_core_read(&parent, sizeof(parent), &task->real_parent);
    bpf_core_read(&ev.ppid, sizeof(ev.ppid), &parent->pid);
    bpf_get_current_comm(&ev.comm, sizeof(ev.comm));

    // The raw trace event layout changed in 5.16: filename moved from an
    // inline 1024-byte array to a __data_loc string. The tracepoint payload
    // does not carry an argv array, so exec events report the filename only.
    if (bpf_core_field_exists(ctx->__data_loc_filename)) {
        uint32_t filename_loc = BPF_CORE_READ(ctx, __data_loc_filename);

        bpf_probe_read_str(&ev.filename, sizeof(ev.filename),
                           (void *)ctx + (filename_loc & 0xffff));
    } else {
        // 5.10-5.15: filename is inline at a fixed offset after
        // trace_entry(8) + pid(4) + old_pid(4).
        bpf_probe_read_str(&ev.filename, sizeof(ev.filename), (void *)ctx + 16);
    }

    emit(&ev, sizeof(ev));
    return 0;
}

SEC("tracepoint/sock/inet_sock_set_state")
int on_connect(struct trace_event_raw_inet_sock_set_state *ctx)
{
    struct event_connect ev = {};
    struct sock *sk;
    struct sock_common *skc;

    // Emit connect attempts (TCP_SYN_SENT) only.
    if (BPF_CORE_READ(ctx, newstate) != TCP_SYN_SENT)
        return 0;

    ev.pid = bpf_get_current_pid_tgid() >> 32;
    bpf_get_current_comm(&ev.comm, sizeof(ev.comm));

    bpf_core_read(&sk, sizeof(sk), &ctx->skaddr);
    skc = &sk->__sk_common;
    if (BPF_CORE_READ(skc, skc_family) == AF_INET) {
        __be32 daddr;

        bpf_core_read(&daddr, sizeof(daddr), &skc->skc_daddr);
        // Store the IPv4-mapped ::ffff:a.b.c.d form so the userspace
        // decoder can format plain dotted IPv4.
        ev.dst_ip[0] = 0;
        ev.dst_ip[1] = 0;
        // On little-endian targets the u32 0xffff0000 occupies bytes
        // 00 00 ff ff, i.e. the ::ffff: prefix at bytes 10-11 the Go
        // decoder looks for.
        ev.dst_ip[2] = 0xffff0000;
        ev.dst_ip[3] = daddr;
    } else {
        bpf_core_read(ev.dst_ip, sizeof(ev.dst_ip),
                      &skc->skc_v6_daddr.in6_u.u6_addr32);
    }
    bpf_core_read(&ev.dst_port, sizeof(ev.dst_port), &skc->skc_dport);

    emit(&ev, sizeof(ev));
    return 0;
}

SEC("kprobe/commit_creds")
int BPF_KPROBE(on_commit_creds, struct cred *new)
{
    struct event_privilege ev = {};
    struct task_struct *task;
    struct cred *old;

    task = (struct task_struct *)bpf_get_current_task();
    bpf_core_read(&old, sizeof(old), &task->real_cred);

    ev.pid = bpf_get_current_pid_tgid() >> 32;
    bpf_get_current_comm(&ev.comm, sizeof(ev.comm));
    bpf_core_read(&ev.old_uid, sizeof(ev.old_uid), &old->uid.val);
    bpf_core_read(&ev.old_gid, sizeof(ev.old_gid), &old->gid.val);
    bpf_core_read(&ev.new_uid, sizeof(ev.new_uid), &new->uid.val);
    bpf_core_read(&ev.new_gid, sizeof(ev.new_gid), &new->gid.val);

    // kernel_cap_t is 8 bytes in every kernel but its shape changed in
    // 6.3 ({u32 cap[2]} -> {u64 val}); reading the field as raw 8 bytes
    // is layout-agnostic and works on both.
    {
        uint64_t old_caps;
        uint64_t new_caps;

        bpf_core_read(&old_caps, sizeof(old_caps), &old->cap_effective);
        bpf_core_read(&new_caps, sizeof(new_caps), &new->cap_effective);
        ev.cap_added = new_caps & ~old_caps;
    }

    // Emit when identity or effective capabilities change (cap-only
    // transitions such as file caps or ambient raises are audit-relevant).
    if (ev.old_uid == ev.new_uid && ev.old_gid == ev.new_gid &&
        ev.cap_added == 0)
        return 0;

    emit(&ev, sizeof(ev));
    return 0;
}