Skip to content

eBPF-based security platform

bpf: restrict userspace map creation

John Fastabendc8a34bc0b557
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rst
index f48ce03c..00e31350 100644
--- a/Documentation/architecture.rst
+++ b/Documentation/architecture.rst
@@ -219,7 +219,9 @@ allowed; they come from a loader the kernel has already verified.
Userspace ``BPF_PROG_LOAD`` is allowed only for ``BPF_PROG_TYPE_SYSCALL``
with a non-zero ``signature_size``, so the only program userspace can
load is a signed loader. Allowed without a signature are the commands
-that cannot introduce code: map create, update and freeze (the
+that cannot introduce code: map create only from the executable
+``init`` recorded as shirudo (signed-loader map creation runs in kernel
+context and is already allowed), map update and freeze (the
loader-data pattern), object pin, link create and raw tracepoint open
(a program fd is only obtainable from a signed load or from an
already-vetted program), and the read-only commands (``OBJ_GET``,
diff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rst
index ae5ddf73..073eddcb 100644
--- a/Documentation/threat-model.rst
+++ b/Documentation/threat-model.rst
@@ -112,7 +112,9 @@ T1 Loading unsigned BPF
and ``lsm/bpf_prog_load`` then requires the verdict
``BPF_SIG_VERIFIED`` from exactly that keyring kind and, while
staging, that serial (the kernel records none for its own bpf
- keyring). The payload is hash-locked to the loader by
+ keyring). Userspace ``BPF_MAP_CREATE`` is restricted to the
+ executable ``init`` recorded as shirudo; signed loader map creation
+ runs in kernel context. The payload is hash-locked to the loader by
``excl_prog_hash``. Root without the key gets ``-EACCES`` or
``-ENOKEY``; a certificate planted in any other keyring is ignored,
and a foreign-signed artifact fails with ``ENOKEY``.
diff --git a/bpf/bpf2.c b/bpf/bpf2.c
index 85c3c174..29cc074b 100644
--- a/bpf/bpf2.c
+++ b/bpf/bpf2.c
@@ -118,11 +118,16 @@ struct {
__type(value, struct shirudo_sb_id);
} shirudo_bpffs_sb SEC(".maps");
+static __always_inline bool
+shirudo_task_is_self(const struct shirudo_sb_id *id);
+
SEC("lsm/bpf")
int BPF_PROG(shirudo_lsm_bpf, int cmd, union bpf_attr *attr,
unsigned int size, bool kernel)
{
+ struct shirudo_sb_id *id;
__u32 prog_type, sig_sz;
+ __u32 zero = 0;
if (kernel)
return 0;
@@ -157,6 +162,11 @@ int BPF_PROG(shirudo_lsm_bpf, int cmd, union bpf_attr *attr,
* already-vetted program grants no new code-execution capability.
*/
case BPF_MAP_CREATE:
+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+ if (!id || !shirudo_task_is_self(id))
+ return -EACCES;
+ return 0;
+
case BPF_MAP_UPDATE_ELEM:
case BPF_MAP_FREEZE:
case BPF_OBJ_PIN: