Skip to content

eBPF-based security platform

bpf: make the identity map unwritable from BPF programs

Tobias Klausere552a8b4e71a
diff --git a/AGENTS.md b/AGENTS.md
index d9e2d9b3..276768ac 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -216,6 +216,16 @@ one usually still compiles and passes tests.
value the target wrote about itself is not a check.
- **Pin the bpf_link before anything else fallible** in `shirudo init`,
or a crash leaves an attached, unowned link that the kernel detaches.
+- **A map no BPF program writes is created with `BPF_F_RDONLY_PROG`.**
+ `BPF_MAP_FREEZE` only blocks the syscall side; without the flag a BPF
+ program could still write the map's value through a lookup's return
+ pointer, and the verifier would allow it. `shirudo_bpffs_sb` carries
+ the flag for that reason. A map a BPF program writes, even once
+ (`shirudo_mnt_rec`, the task, socket and inode storage maps,
+ `shirudo_tamper`, `shirudo_devices`, upatch state), does not take it.
+ A map added later that fits the no-BPF-write criterion takes the flag
+ from the commit that adds it; a map that stops fitting it drops the
+ flag in the same commit that adds the write.
- **init runs twice in production.** The authenticated systemd initrd binds
persistent state writable, verifies runtime and config, enrolls and seals the
`.bpf` and `.dm-verity` keyrings, labels state, attaches all guards, restores
diff --git a/Documentation/internals/enforcement.rst b/Documentation/internals/enforcement.rst
index 63f4c54c..f1cd05f9 100644
--- a/Documentation/internals/enforcement.rst
+++ b/Documentation/internals/enforcement.rst
@@ -59,7 +59,10 @@ The objects share seven maps, which ``init`` hands from ``bpf2`` to
keyring serial and kind, the serials of the enrolled keys, the
``.dm-verity`` keyring and key serials, the NV index, the NV
writer's public modulus and seed, and the ECC endorsement key
- name used to salt authenticated sessions.
+ name used to salt authenticated sessions. Created with
+ ``BPF_F_RDONLY_PROG``, so the verifier refuses any BPF program
+ that writes it; only ``init``'s one update before the freeze
+ populates it.
* - ``shirudo_tasks``
- ``task_authority``
- Task storage with each task's mark and exec candidate.
@@ -87,6 +90,17 @@ mapping. ``bpf2`` also keeps ``shirudo_mnt_rec`` for the bpffs
mount it records, and ``seal`` keeps its scratch buffer and the inode
storage of directory marks.
+A map no BPF program ever writes, such as ``shirudo_bpffs_sb``, is
+created with ``BPF_F_RDONLY_PROG`` as well as frozen: freezing
+(``BPF_MAP_FREEZE``) blocks only the syscall side, so without the flag
+a BPF program could still write the map's value through a lookup's
+return pointer and the verifier would allow it. A map a BPF program
+does write, even once, such as ``shirudo_mnt_rec`` or the task, socket
+and inode storage maps above, does not take the flag. The same applies
+to a map a future change adds: set ``BPF_F_RDONLY_PROG`` at creation
+the moment no BPF program writes it, and drop the flag the moment one
+does.
+
Telling shirudo from root
-------------------------
diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rst
index 6de46bc9..dc8cf7a9 100644
--- a/Documentation/security/threat-model.rst
+++ b/Documentation/security/threat-model.rst
@@ -307,9 +307,11 @@ Assets
- The bpffs inode and mount guards, the descriptor checks (T2).
* - Identity map
- ``shirudo_bpffs_sb``, pinned as ``identity``
- - Frozen by ``init``; opened only by tasks with runtime authority
- (T13). It holds the keyring serials, the runtime root, the EK
- name, the NV writer seed and public key, and the NV index.
+ - Frozen by ``init`` and created with ``BPF_F_RDONLY_PROG``, so
+ neither userspace nor a BPF program can write it; opened only by
+ tasks with runtime authority (T13). It holds the keyring
+ serials, the runtime root, the EK name, the NV writer seed and
+ public key, and the NV index.
* - Task marks
- ``shirudo_tasks`` task storage, pinned as ``task_authority``
- Frozen against ``bpf()`` writes; only the seal's programs set a
diff --git a/bpf/lib/identity.h b/bpf/lib/identity.h
index f601ec5a..cc8ae95c 100644
--- a/bpf/lib/identity.h
+++ b/bpf/lib/identity.h
@@ -29,6 +29,7 @@ struct sb_id {
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 1);
+ __uint(map_flags, BPF_F_RDONLY_PROG);
__type(key, __u32);
__type(value, struct sb_id);
} shirudo_bpffs_sb SEC(".maps");
diff --git a/util/image_guards.c b/util/image_guards.c
index 17ceab1c..2319f08d 100644
--- a/util/image_guards.c
+++ b/util/image_guards.c
@@ -144,7 +144,7 @@ const struct guard_map shirudo_guard_maps[GUARD_MAP_COUNT] = {
[GUARD_IDENTITY] = {
"shirudo_bpffs_sb", IDENTITY_PIN,
BPF_MAP_TYPE_ARRAY, sizeof(__u32),
- sizeof(struct identity), 1, 0,
+ sizeof(struct identity), 1, BPF_F_RDONLY_PROG,
},
[GUARD_TASKS] = {
"shirudo_tasks", BPFFS_DIR "/task_authority",