BPF typed arenas
Subsystems (8)
Kumar Kartikeya Dwivedi
maintainercover BPF typed arenas
NOTE: Patch marked as RFC, since this not ready for proper review. For now, reviewers are advised to ignore concrete details, but instead focus on the general approach, design decisions, and tradeoffs made in the sandboxing approach to support objects that can hold trusted kernel objects.
This set adds typed arenas: kernel-only memory next to an arena map that holds objects of a program's own struct types with special fields, kptrs for now, at fixed slots. Programs reach the objects through native pointers, the verifier trusts the special fields in them, and it does not track the lifetime of an object, because the memory behind a typed arena pointer is always valid.
Today, a struct with a kptr cannot be placed in arena memory. The arena is mapped into user space, and access arbitrarily by the BPF program with no constraints, which can write anything into it, so the verifier cannot trust a pointer it loads from arena memory, and a reference kept in such a field could be overwritten, duplicated or dropped.
A program that builds its data structures in the arena and wants to attach a task, a socket or an allocated object to a node keeps the reference in a separate map and looks it up by key every time. sched-ext has introduced id based indirection for task structs to work around similar limitations.
The other way to get typed objects with kptrs, bpf_obj_new(), spills the lifetime of every object into the static analysis: the pointer is owned by whoever holds it, it must be dropped or moved into a map, a list or an rbtree before the program exits, sharing it needs bpf_refcount, and reading a shared object needs RCU or a lock. Every program that touches such an object is verified against those rules, and data structures built from allocated objects are written around them. This has proven pretty inflexible in practice, and requires kernel changes for each new data structure.
A typed arena is arena memory that is only mapped in the kernel, so that only the program writes it and under the verifier's rules, and that is always valid: every address in it resolves to an object of its struct, allocated or not. A pointer to a typed arena object therefore does not dangle, does not have to be released and does not have to be checked for being alive, so there is no ownership, no reference and no RCU state for the verifier to track.
The verifier still has to ensure is that a value used as a pointer to struct T points at the start of an object of T. It does not do that by tracking where the value came from, but by construction: a new instruction, typed_arena_cast, masks any 64-bit value into T's slice of the typed arena and rounds it to an object boundary. The instruction lowers to a mask with an immediate and an add of the slice's base, four instructions, with no branch and no bounds check, and the result cannot point outside the slice or into the middle of an object. Any value goes through it, a pointer loaded from the raw arena that user space corrupted, an integer, a pointer of another struct, and comes out as a trusted pointer to a whole object. Pointers are stored as they are, in the raw arena, in maps, on the stack or in typed objects; only their use as an address is sanitized.
The typed arena region is sparsely mapped, and populated on demand at runtime using new allocation functions mirroring arena allocation kfuncs. In case of faults, scratch pages are used to supply underlying memory to allow such objects to be accessed freely from both the BPF program and the kernel without issues.
Layout
------
Each arena map reserves a 4 GiB kernel-only region ahead of its user-mappable window, which user space cannot map. A new mm helper aligns the vm area to the region, so that every power-of-two slice in it is aligned to its own size and the cast needs only a mask. A struct with special fields gets one slice per program BTF and type in the program's arena map, registered at program load when the verifier first meets the type in a cast, in a load from a typed pointer field or in an allocation. The slice's size comes from a "typed_arena_size:<size>" declaration tag on the struct, 128 MiB by default, a power of two of at least a page and at most 2 GiB. Objects sit at slots of the struct's size rounded up to a power of two. The unit of backing is a chunk, the larger of a slot and a page: an object larger than a page is one contiguous block, and an object never spans two allocations. The page tables of a slice are populated at registration and charged to the map, together with two bitmaps, because a fault on an unbacked typed page is recovered in atomic context.
A chunk nobody allocated reads as zeroed dummy objects: a fault on it maps in the slice's scratch chunk, one per slice, at the same position, so that a scalar field of one dummy object does not alias another. Two kfuncs back a range with real objects and release it, in page counts like the raw arena kfuncs, rounded up to whole chunks, with the granted count written back:
void *bpf_typed_arena_alloc_pages(void *map, __u64 type_id, void *addr,
__u32 *page_cnt, int node_id);
void bpf_typed_arena_free_pages(void *map, __u64 type_id, void *ptr,
__u32 page_cnt);A release is deferred behind an RCU and an RCU-tasks-trace grace period, so that every invocation that started before it keeps valid objects, and a range is released once, since a second release queued behind the first would take the range away from its next owner. The kptrs of released objects are dropped once the chunk is unmapped. A pointer to a released object that a program kept still names a whole object of the type, of whoever claims the chunk next; that is program logic, like a stale index into an array, not a safety problem.
Access
------
A pointer member of a typed struct whose pointee is itself a typed struct is a typed pointer field, found from BTF alone, without annotation. The verifier lets nothing but a trusted pointer to the pointee at offset zero, or the constant 0, be stored into it, the memory is kernel-only, and a reused chunk comes back zeroed, so a load from the field is trusted to be an object or 0 without a cast. The loaded pointer is canonicalized in place only where it is used as an address; a comparison, a copy or a store of it as a value sees the raw value, so a list is walked with plain loads and ends on the usual test against NULL. A program that uses the value without checking for 0 lands on object 0 of the slice, as a cast of 0 would. Whether to check is left to the program, since nothing it can do with the value reaches outside the slice; a maybe-NULL type that must be checked before every use would cost a check on every hop that the sanitization makes unnecessary.
Scalar fields are loaded and stored natively, atomics included, with no probe mode and no exception table entry. kptr fields go through bpf_kptr_xchg(), as in map values.
Compiler Changes
----------------
Programs do not write the cast. Behind the typed-arena target feature, Clang inserts it where a pointer to a struct with special fields is used as an address, and where a value of another type is converted to such a pointer, which is where a value loaded from the raw arena or returned by the allocator becomes a typed pointer. The same struct also lives in allocated objects, map values and on the stack, and the compiler cannot tell which, so the verifier decides per path what a cast costs: a value it does not trust is sanitized, a pointer it already trusts is copied, and a program that only uses bpf_obj_new() objects of the struct loads as before. The builtin, __builtin_bpf_typed_arena_cast(), remains for turning a scalar or a raw arena pointer into a typed pointer on purpose. The feature defines __BPF_FEATURE_TYPED_ARENA_CAST; libbpf relocates the instruction and provides bpf_typed_arena_cast(); the selftests probe for the feature and are skipped without it.
The compiler changes are on the typed-arena branch of
https://github.com/kkdwvd/llvm-project, two commits on top of main:
4f5f6b0db95c BPF: Add the typed_arena_cast builtin and instruction cb29a13b73bd BPF: Insert typed_arena_cast at uses of typed record pointers
Notes
-----
* BPF kptrs are the only special fields a typed arena object may have. Timers, workqueues, task work and RCU heads keep kernel state that a release or a fault could pull from under a kfunc, so they are refused inline for now, this will be made to work in subsequent revisions.
Pages can be unmapped while a kernel is operating on an object embedded inline a typed arena object sitting in a page that was requested to be freed. Some protocol to signal when operating on such objects from the kernel side will be necessary to ensure that new operations cannot begin on such fields, and whether existing operations are finished.
Spin locks, lists, rbtrees, etc. are unnecessary to support as special fields, since they have their own analogues in arenas.
* A slot is a power of two, so an object is padded by up to half its slot. This is the trade the slab allocator makes for its size classes, and it is what keeps the cast at a mask and an add. Denser packing can be added behind the same interface, since programs never see the slot, but it is likely not worth it.
* Slices are fixed at registration, neither grown nor moved, and a map's slices share its 4 GiB region. Objects larger than 4 MiB are refused.
* The scratch chunk is shared by every unallocated chunk of a slice, so a store through a stray pointer is visible through every unallocated chunk, and a kptr stored there is dropped with the map.
* The sanitizing sequence runs on every value that enters from untrusted memory and, for now, before every use of a loaded typed pointer as an address.
* The page tables of a slice cost 8 bytes per page, 256 KiB for the default size, charged to the map.
* Nothing in the set is UAPI: the kfuncs, the declaration tag and the instruction encoding may change.
* Only x86-64 has been tested. No JIT changes are needed: the cast is lowered by the verifier, accesses are native loads and stores, and the fault path lives in the arena map.
Follow-ups
----------
* Elide the sanitization on loads through a typed pointer that nothing writes to, with a probed load, so that a chain of reads costs nothing beyond the loads.
Patch 1 adds an mm helper for an aligned sparse vm area. Patches 2 and 3 add the region, the registry and the scratch backing. Patches 4 to 8 add the cast instruction, object access, special fields, typed pointer fields and their canonicalization at use. Patch 9 adds the kfuncs and patch 10 the free cast on trusted pointers. Patch 11 adds the libbpf support, patch 12 the selftests' compiler probe, and patches 13 to 16 the selftests.
Kumar Kartikeya Dwivedi (16):
mm/vmalloc: Add get_vm_area_align()
bpf: Introduce BPF typed arenas
bpf: Back typed arena chunks with scratch on demand
bpf: Add the typed_arena_cast instruction
bpf: Allow scalar and atomic access to typed arena objects
bpf: Support special fields in typed arena objects
bpf: Trust typed pointer fields of typed arena objects
bpf: Canonicalize loaded typed arena pointers where they are used
bpf: Add typed arena page allocation and release kfuncs
bpf: Let typed_arena_cast copy pointers the verifier already trusts
libbpf: Support the typed_arena_cast instruction selftests/bpf: Build BPF objects with compiler-inserted typed arena
casts
selftests/bpf: Test typed arena casts and registration selftests/bpf: Test typed arena object access, kptrs and typed pointer
fields
selftests/bpf: Test typed arena page allocation and release selftests/bpf: Exercise typed arenas at run time
include/linux/bpf.h | 91 ++ include/linux/bpf_verifier.h | 35 + include/linux/vmalloc.h | 2 + include/uapi/linux/bpf.h | 6 + kernel/bpf/arena.c | 772 +++++++++- kernel/bpf/backtrack.c | 8 +- kernel/bpf/btf.c | 140 +- kernel/bpf/core.c | 2 + kernel/bpf/disasm.c | 9 + kernel/bpf/fixups.c | 64 + kernel/bpf/log.c | 6 +- kernel/bpf/syscall.c | 3 + kernel/bpf/verifier.c | 498 ++++++- mm/vmalloc.c | 19 + tools/include/uapi/linux/bpf.h | 6 + tools/lib/bpf/bpf_helpers.h | 13 + tools/lib/bpf/relo_core.c | 19 +- tools/testing/selftests/bpf/Makefile | 13 +- .../testing/selftests/bpf/bpf_experimental.h | 18 + .../selftests/bpf/prog_tests/typed_arena.c | 489 +++++++ .../selftests/bpf/prog_tests/verifier.c | 22 + .../testing/selftests/bpf/progs/typed_arena.c | 354 +++++ .../bpf/progs/verifier_typed_arena.c | 1304 +++++++++++++++++ 23 files changed, 3852 insertions(+), 41 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/typed_arena.c create mode 100644 tools/testing/selftests/bpf/progs/typed_arena.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_typed_arena.c
base-commit: ea9358e1270ab2c3ba6f36bd9bdda68617665516
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 1/16 mm/vmalloc: Add get_vm_area_align()
Add a variant of get_vm_area() that takes the alignment of the area's start. The internal allocator has supported an alignment all along, through __get_vm_area_node(), but every caller of the public API passes one page, so a user that needs a naturally aligned region has to over-reserve and skip the slack in front of the aligned address, wasting up to the alignment in virtual space per area.
BPF arena maps are about to reserve a 4 GiB kernel-only region next to the existing arena window, holding one power-of-two slice per program struct type. Each slice is placed at a multiple of its own size inside the region, which makes the region's alignment the largest slice size, so that a BPF program can turn any 64-bit value into an object address in a slice with a mask and an add. Reserving the region aligned avoids carrying 4 GiB of slack in every arena map.
--- include/linux/vmalloc.h | 2 ++ mm/vmalloc.c | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+)
include/linux/vmalloc.h +2 -0
@@ -246,6 +246,8 @@ static inline size_t get_vm_area_size(const struct vm_struct *area)extern struct vm_struct *get_vm_area (unsigned long size,unsigned long flags);extern struct vm_struct *get_vm_area_caller (unsigned long size,unsigned long flags,const void *caller); +extern struct vm_struct *get_vm_area_align (unsigned long size, +unsigned long align,unsigned long flags); extern struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags, unsigned long start, unsigned long end,
mm/vmalloc.c +19 -0
@@ -3301,6 +3301,25 @@ struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, NUMA_NO_NODE, GFP_KERNEL, caller); } +/** +* get_vm_area_align - reserve a contiguous kernel virtual area at an aligned start +* @size: size of the area +* @align: alignment of the start of the area, a power of two +* @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC +* +* Like get_vm_area(), with the start of the area aligned to @align. +* +* Return: the area descriptor on success or %NULL on failure. +*/ +struct vm_struct *get_vm_area_align (unsigned long size,unsigned long align, +unsigned long flags) +{ +return __get_vm_area_node (size, align,PAGE_SHIFT , flags, +VMALLOC_START ,VMALLOC_END , +NUMA_NO_NODE ,GFP_KERNEL , +__builtin_return_address (0 )); +} +/** * find_vm_area - find a continuous kernel virtual area * @addr: base address
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 2/16 bpf: Introduce BPF typed arenas
In reply to Kumar Kartikeya DwivediObjects with special fields cannot live in arena memory: user space maps the arena and programs store arbitrary bytes into it, so the verifier trusts nothing loaded from there, and a kptr field is a kernel pointer the kernel must be able to trust. Give such objects a home next to the arena instead.
Reserve a typed arena region ahead of the arena's raw window, a kernel-only 4 GiB of virtual space user space never maps, and hand out one typed arena per program-BTF struct with special fields: a power-of-two slice of the region holding one object per power-of-two slot. Objects are reached through native kernel pointers that the verifier constructs, so the region's memory carries no user-visible representation and objects need no translation on load or store; a later patch adds the instruction that turns any 64-bit value into such a pointer, and another the fields the verifier trusts inside them.
The slice is aligned to its own size, which is what makes that cast a mask and an add: masking a value with the slice's size less its slot lands on a slot of the slice once the base is added, with no bounds check, no branch and no subtraction. Alignment inside the region comes from placing a slice at a multiple of its size, first-fit over the registry kept in address order, and alignment of the region itself comes from reserving the arena's vm_area aligned to the region with get_vm_area_align(), instead of over-reserving and skipping slack in front. A slice is at most half the region, so that the mask is a positive 32-bit immediate; the raw window and its guards keep their layout and offset math, shifted behind the region.
The slot is the object's size rounded up to a power of two. That pads an object by up to half its slot, the same trade the slab allocator makes for its size classes, and it buys the four-instruction cast above: any denser packing needs a reciprocal multiply to find an object boundary and a scratch layout that repeats with the least common multiple of slot and page. Both can be added later behind the same interface, since nothing about the slot is visible to programs. The typed arena's size comes from the struct's "typed_arena_size:" declaration tag, parsed by the verifier, with a 128 MiB default; it decides how many objects the type can hold.
A typed arena is keyed by the BTF object and the type ID, so two programs loaded from different objects get distinct slices even for structurally equal structs, and it is registered at program load, where the verifier first meets the type. A program whose load fails drops its references; a program that loaded keeps the typed arena alive for the map's lifetime, so that objects survive program reloads. The page tables of the whole slice are populated at registration, because a fault on an unbacked typed page is recovered in atomic context, which cannot allocate them then; they cost eight bytes per page of the slice, 256 KiB for the default size, and are charged to the map together with the two chunk bitmaps as memory fixed for the map's lifetime.
The chunk, the larger of the slot and a page, is the unit at which the slice is backed: a chunk holds whole objects, so an object is never split between a real page and a page backed some other way, and a chunk larger than a page is one allocation of its order, so that a multi-page object is contiguous in the direct map. Map teardown walks every slice, drops the special fields of the objects of each real chunk through the arena mapping, still in place, and frees the chunk's allocation.
--- include/linux/bpf.h | 62 ++++++++++ kernel/bpf/arena.c | 274 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 331 insertions(+), 5 deletions(-)
include/linux/bpf.h +62 -0
@@ -45,6 +45,7 @@ struct bpf_prog;struct bpf_prog_aux ;struct bpf_map ;struct bpf_arena ; +struct bpf_typed_arena ;struct sock ;struct seq_file ;struct btf ;
@@ -275,6 +276,67 @@ struct btf_record {struct btf_field fields[]; }; +/* +* Typed arenas: kernel-only object storage next to an arena map. A program-BTF +* struct with special fields that a program casts to, or allocates, gets a +* typed arena: a power-of-two slice of the map's typed region, naturally +* aligned, holding one object per power-of-two slot. Objects are reached +* through native kernel pointers that the verifier constructs; a cast masks +* any 64-bit value into a slot of the slice, so every value names an object +* of the type. The chunk, max(slot, page), is the unit of backing: a chunk no +* program allocated reads as the zeroed scratch chunk, one dummy object per +* slot, once an access faults it in. The chunks bitmap marks every chunk that +* holds a mapping, real or scratch; pending marks the chunks whose release is +* queued, which stay mapped and marked until it has run. record is the +* struct's own special-field record, from the BTF that is retained here. +*/ +#define BPF_TYPED_ARENA_SIZE_TAG "typed_arena_size:" +#define BPF_TYPED_ARENA_DEFAULT_SIZE SZ_128M + +struct bpf_typed_arena { +struct list_head node; +struct bpf_map *map; +refcount_t refcnt; +struct btf *btf; +u32 btf_id; +u8 slot_shift; +u8 chunk_shift; +u8 size_shift; +void *base; +void *scratch; +struct page **scratch_pages; +unsigned long *chunks; +unsigned long *pending; +const struct btf_record *record; +}; + +static inline u64 bpf_typed_arena_size (const struct bpf_typed_arena *ta) +{ +return 1ull << ta->size_shift; +} + +static inline u32 bpf_typed_arena_slot (const struct bpf_typed_arena *ta) +{ +return 1u << ta->slot_shift; +} + +static inline u32 bpf_typed_arena_chunk (const struct bpf_typed_arena *ta) +{ +return 1u << ta->chunk_shift; +} + +/* The cast mask keeps the slot index and drops the offset within the slot and the bits above the slice. */ +static inline u64 bpf_typed_arena_mask (const struct bpf_typed_arena *ta) +{ +return bpf_typed_arena_size (ta) -bpf_typed_arena_slot (ta); +} + +struct bpf_typed_arena *bpf_typed_arena_get (struct bpf_map *map,struct btf *btf,u32 btf_id, +const struct btf_record *record,u64 size); +void bpf_typed_arena_put (struct bpf_map *map,struct bpf_typed_arena *ta); +int bpf_typed_arena_cast_insns (const struct bpf_typed_arena *ta,u8 dst,u8 src, +struct bpf_insn *buf); +/* Non-opaque version of bpf_rb_node in uapi/linux/bpf.h */ struct bpf_rb_node_kern {struct rb_node rb_node;
kernel/bpf/arena.c +269 -5
328 lines not on this page · raw message
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 3/16 bpf: Back typed arena chunks with scratch on demand
In reply to Kumar Kartikeya DwivediThe cast that produces a typed arena pointer never fails: it masks any value into a slot of the slice, so a program can reach every object the slice could hold, whether or not anything allocated it. Every such pointer must still denote a valid object of the type, or the native accesses through it would fault, and a kptr exchange would write a kernel pointer into unbacked memory. So back the slice on demand: an access to an unbacked page of the slice faults, and the kernel fault recovery of the arena, reached through bpf_arena_handle_page_fault(), installs scratch memory there and resumes the access, reporting it on the program's stderr stream as a use of an unallocated object.
The scratch memory is one zeroed chunk per typed arena, and the page of the scratch chunk installed at a faulting page is the one at the same position in the chunk. A chunk holds whole objects laid out from its start, so the dummy objects a program sees are laid out exactly like real ones: a kptr field of one dummy object never aliases a scalar field of another, which matters because a kptr stored through one pointer must not be readable as a scalar through another, and the special fields of the dummy objects can be dropped at map teardown by walking the scratch chunk like a real one. This is why the chunk, rather than the page, is the unit of backing, and why the slot is a power of two: with objects straddling pages at arbitrary offsets the layout would differ from page to page, and the scratch memory would have to grow to the period at which it repeats. The scratch chunk is shared by every unallocated chunk of the slice, so the dummy objects behave as one sink: a program that writes through a stray pointer sees its writes through every other stray pointer to the same position, and a kptr it leaves there lives until the map is freed.
The fault path marks the chunk taken in the bitmap before installing the page, with an atomic bit set because a program can fault in any context it runs in, including NMI, and the allocator, added next, only backs chunks that are not marked. A real page never replaces a scratch page either: a program may be in the middle of using the dummy object, nothing can wait for that use to end because the next stray access faults the scratch page right back in, and the effect of the mapping changing under a kernel operation on the object has not been reasoned through. The raw arena can replace its scratch page because its contents are raw data. A chunk the fault path took is given back with the release kfunc like any other, after which it can be allocated or faulted again.
Refuse to register typed arenas on architectures that do not provide the atomic PTE installer, since those do not wire up the arena fault path and a typed access there would oops. The guard between the region and the raw window stays outside recovery: a typed access is bounded by its slice and cannot reach it, so a fault there is still a bug to oops on.
--- kernel/bpf/arena.c | 115 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 109 insertions(+), 6 deletions(-)
kernel/bpf/arena.c +109 -6
@@ -67,6 +67,16 @@#define TYPED_ARENA_REGION_SZ SZ_4G#define TYPED_ARENA_MAX_SZ SZ_2G#define KERN_VM_SZ (TYPED_ARENA_REGION_SZ + SZ_4G + GUARD_SZ) +/* +* Typed accesses are native loads and stores, so a fault on an unbacked typed +* page can only be recovered where the arena's kernel fault path is wired up, +* which is where the architecture provides an atomic PTE installer. +*/ +#ifdef ptep_try_set +#define TYPED_ARENA_SUPPORTED true +#else +#define TYPED_ARENA_SUPPORTED false +#endif static void arena_free_pages (struct bpf_arena *arena,long uaddr,long page_cnt,bool sleepable);
@@ -305,13 +315,28 @@ static unsigned int typed_arena_chunk_order(const struct bpf_typed_arena *ta)return ta->chunk_shift -PAGE_SHIFT ; } +static u32 typed_arena_chunk_pages (const struct bpf_typed_arena *ta) +{ +return bpf_typed_arena_chunk (ta) >>PAGE_SHIFT ; +} + +/* The page of the scratch chunk that backs the page at @addr while its chunk is unallocated. */ +static struct page *typed_arena_scratch_page (const struct bpf_typed_arena *ta,unsigned long addr) +{ +unsigned long off = addr - (unsigned long )ta->base; + +return ta->scratch_pages[(off & (bpf_typed_arena_chunk (ta) -1 )) >>PAGE_SHIFT ]; +} +/* * The memory a typed arena takes for the map's lifetime: a page table entry -* per page of the slice and the two chunk bitmaps. +* per page of the slice, the scratch chunk with its page array, and the two +* chunk bitmaps. */ static u64 typed_arena_static_mem (const struct bpf_typed_arena *ta) {return (bpf_typed_arena_size (ta) >>PAGE_SHIFT ) *sizeof (pte_t) + +bpf_typed_arena_chunk (ta) +typed_arena_chunk_pages (ta) *sizeof (struct page *) +2 *BITS_TO_LONGS (typed_arena_nr_chunks (ta)) *sizeof (long ); }
@@ -371,7 +396,7 @@ static int typed_arena_teardown_cb(pte_t *ptep, unsigned long addr, void *data)if ((addr - (unsigned long )ta->base) & (bpf_typed_arena_chunk (ta) -1 ))return 0 ; -if (!pte_present (pte)) +if (!pte_present (pte) ||pte_page (pte) ==typed_arena_scratch_page (ta, addr))return 0 ;typed_arena_free_objects (ta, (void *)addr);__free_pages (pte_page (pte),typed_arena_chunk_order (ta));
@@ -383,6 +408,10 @@ static void typed_arena_free(struct bpf_arena *arena, struct bpf_typed_arena *taWRITE_ONCE (arena->typed_arena_mem, arena->typed_arena_mem -typed_arena_static_mem (ta));apply_to_existing_page_range (&init_mm, (unsigned long )ta->base,bpf_typed_arena_size (ta), typed_arena_teardown_cb, ta); +/* Programs may have stored kptrs into the dummy objects. */ +typed_arena_free_objects (ta, ta->scratch); +vfree (ta->scratch); +kfree (ta->scratch_pages);bitmap_free (ta->chunks);bitmap_free (ta->pending);btf_put (ta->btf);
@@ -408,11 +437,14 @@ struct bpf_typed_arena *bpf_typed_arena_get(struct bpf_map *map, struct btf *btf { struct bpf_arena *arena = container_of(map, struct bpf_arena, map);const struct btf_type *t =btf_type_by_id (btf, btf_id); +struct mem_cgroup *new_memcg, *old_memcg;struct bpf_typed_arena *ta;u64 slot, chunk;s64 off; -int err; +int err, i; +if (!TYPED_ARENA_SUPPORTED ) +return ERR_PTR (-EOPNOTSUPP );if (!t || !t->size || !record)return ERR_PTR (-EINVAL );if (!is_power_of_2 (size) || size <PAGE_SIZE || size >TYPED_ARENA_MAX_SZ )
@@ -444,10 +476,17 @@ struct bpf_typed_arena *bpf_typed_arena_get(struct bpf_map *map, struct btf *btf ta->chunk_shift =ilog2 (chunk); ta->chunks =bitmap_zalloc (typed_arena_nr_chunks (ta),GFP_KERNEL_ACCOUNT ); ta->pending =bitmap_zalloc (typed_arena_nr_chunks (ta),GFP_KERNEL_ACCOUNT ); -if (!ta->chunks || !ta->pending) { + ta->scratch_pages =kcalloc (typed_arena_chunk_pages (ta),sizeof (*ta->scratch_pages), +GFP_KERNEL_ACCOUNT ); +bpf_map_memcg_enter (map, &old_memcg, &new_memcg); + ta->scratch =__vmalloc (chunk,GFP_KERNEL_ACCOUNT | __GFP_ZERO); +bpf_map_memcg_exit (old_memcg, new_memcg); +if (!ta->chunks || !ta->pending || !ta->scratch_pages || !ta->scratch) { err = -ENOMEM ; goto free; } +for (i =0 ; i <typed_arena_chunk_pages (ta); i++) + ta->scratch_pages[i] =vmalloc_to_page (ta->scratch + i *PAGE_SIZE ); err =apply_to_page_range (&init_mm, (unsigned long )ta->base, size, apply_range_set_cb,NULL );if (err) goto free;
@@ -462,6 +501,8 @@ struct bpf_typed_arena *bpf_typed_arena_get(struct bpf_map *map, struct btf *btfreturn ta; free: +vfree (ta->scratch); +kfree (ta->scratch_pages);bitmap_free (ta->chunks);bitmap_free (ta->pending);kfree (ta);
@@ -1460,6 +1501,64 @@ static void __bpf_prog_report_arena_violation(struct bpf_prog *prog, bool write, })); } +static struct bpf_typed_arena *typed_arena_lookup (struct bpf_arena *arena,unsigned long addr) +{ +struct bpf_typed_arena *ta; + +list_for_each_entry_rcu (ta, &arena->typed_arenas, node) +if (addr - (unsigned long )ta->base <bpf_typed_arena_size (ta)) +return ta; +return NULL ; +} + +static void __bpf_prog_report_typed_arena_violation (struct bpf_prog *prog, +const struct bpf_typed_arena *ta, +bool write,unsigned long addr) +{ +const struct btf_type *t =btf_type_by_id (ta->btf, ta->btf_id); +struct bpf_stream_stage ss; + +/* Use main prog for stream access */ + prog = prog->aux->main_prog_aux->prog; + +bpf_stream_stage (ss, prog,BPF_STDERR , ({ +bpf_stream_printk (ss,"ERROR: Typed arena %s access to unallocated struct %s at 0x%lx\n" , + write ?"WRITE" :"READ" ,btf_name_by_offset (ta->btf, t->name_off), + addr & ~((unsigned long )bpf_typed_arena_slot (ta) -1 )); +bpf_stream_dump_stack (ss); + })); +} + +/* +* A typed access reached an unbacked page, so the program used a pointer to an +* object nobody allocated, which the cast permits by design. The pointer must +* still denote an object of the type: back the page with the page of the +* scratch chunk at the same position, so that the dummy objects the program +* sees are laid out like real ones and a kptr field of one never aliases a +* scalar field of another. Mark the chunk taken first, so that the allocator +* never puts real pages where a program may be in the middle of using the +* dummy object; nothing could wait for that use to end, since the next stray +* access would fault the scratch page right back in. The mark is an atomic +* bit because the fault can happen in any context a program runs in. +*/ +static bool typed_arena_handle_page_fault (struct bpf_arena *arena,struct bpf_prog *prog, +unsigned long addr,bool is_write) +{ +unsigned long page_addr = addr &PAGE_MASK ; +struct bpf_typed_arena *ta; + +guard (rcu)(); + ta =typed_arena_lookup (arena, page_addr); +if (!ta) +return false; +set_bit ((page_addr - (unsigned long )ta->base) >> ta->chunk_shift, ta->chunks); +apply_to_page_range (&init_mm, page_addr,PAGE_SIZE , apply_range_set_scratch_cb, +typed_arena_scratch_page (ta, page_addr)); +flush_vmap_cache (page_addr,PAGE_SIZE ); +__bpf_prog_report_typed_arena_violation (prog, ta, is_write, addr); +return true; +} +bool bpf_arena_handle_page_fault (unsigned long addr,bool is_write,unsigned long fault_ip) {struct bpf_arena *arena;
@@ -1476,12 +1575,16 @@ bool bpf_arena_handle_page_fault(unsigned long addr, bool is_write, unsigned lonif (!arena)return false; +if (page_addr -typed_arena_region (arena) <TYPED_ARENA_REGION_SZ ) +return typed_arena_handle_page_fault (arena, prog, addr, is_write); + kbase =bpf_arena_get_kern_vm_start (arena);/* * Recovery covers the 4 GiB mappable band plus the upper half-guard. -* Lower guard is unreachable from kfuncs; an address there indicates -* a different bug class - leave it to the regular kernel oops path. +* Lower guard is unreachable from typed accesses and from kfuncs; an +* address there indicates a different bug class - leave it to the +* regular kernel oops path. */ if (page_addr < kbase || page_addr >= kbase +SZ_4G +GUARD_SZ /2 )return false;
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 4/16 bpf: Add the typed_arena_cast instruction
In reply to Kumar Kartikeya DwivediAdd the instruction that turns an untrusted 64-bit value into a verifier-trusted pointer to a typed arena object:
dst = typed_arena_cast(src, imm)
encoded as a 64-bit BPF_MOV with a reserved off value, the value in src, the program-local BTF type ID of the struct in imm, and the pointer in dst, which may be src. The compiler emits the instruction with a CO-RE type ID relocation landing in imm, so the type is part of the instruction and a cast names one typed arena on every path. The verifier registers the typed arena for the struct on first sight, records it in the instruction's aux data, and lowers the instruction after verification to the sanitizing sequence of that typed arena, provided by the registry: the value is masked with the typed arena's size less its slot size and the base is added. That keeps the slot index bits and drops everything else, so any value lands on the start of an object of the type. The result is trusted and never NULL.
Any value casts. A typed arena holds objects of one struct at every slot, and a chunk nobody allocated reads as the zeroed scratch chunk, so the verifier need not know where a value came from: a pointer loaded from the raw arena that user space corrupted, a pointer of another type, an integer, or a pointer to this type that arithmetic moved inside an object, which the cast rounds back to the object. This is the whole trust model for values that enter from untrusted memory, in four instructions on every entry, and the reason no NULL check follows a cast.
Pointers to typed objects are stored as-is, in the raw arena, in maps, on the stack and in typed objects. There is no handle form and no translation on the way to memory: a load gives back the 64-bit value, and a cast makes a pointer of it wherever the value is not trusted. A 32-bit view of a typed pointer is what it is for any kernel pointer, a truncated scalar under the leak rules, and storing the pointer in memory user space can read is a pointer leak the arena's privilege already permits. The alternative, a 32-bit slot offset as the stored form, needs a conversion on every store and a second instruction to obtain it, for no gain in what the cast can trust.
The registration validates the struct once per program: it must be a struct with special fields, since a struct without them belongs in the raw arena, and every special field must be one a typed arena supports, which for now is a kptr; locks, timers, workqueues, lists, rbtrees, refcounts and uptrs are refused as not supported. The typed arena's size comes from the struct's "typed_arena_size:<bytes>" decl tag with the usual suffixes, 128 MiB by default, and must be a power of two of at least a page, because the cast bounds the value with one AND and a page is the smallest unit of backing. The registry's answer, a slice of the typed arena region, is logged with its slot, chunk, object count and size, so the padding a power-of-two slot costs is visible. A program keeps a reference on each typed arena it registers until its load fails, at which point the references are dropped before the arena map's; a loaded program leaves the typed arena to the map for the map's lifetime, so objects survive program reloads.
Object access, kptr fields and pointer fields come in the following patches; until then dereferencing a typed pointer is refused.
--- include/linux/bpf.h | 17 +++ include/linux/bpf_verifier.h | 19 +++ include/uapi/linux/bpf.h | 6 + kernel/bpf/backtrack.c | 8 +- kernel/bpf/core.c | 2 + kernel/bpf/disasm.c | 9 ++ kernel/bpf/fixups.c | 33 ++++++ kernel/bpf/log.c | 5 +- kernel/bpf/verifier.c | 207 ++++++++++++++++++++++++++++++++- tools/include/uapi/linux/bpf.h | 6 + 10 files changed, 306 insertions(+), 6 deletions(-)
include/linux/bpf.h +17 -0
@@ -945,6 +945,9 @@ enum bpf_type_flag {/* DYNPTR points to file */ DYNPTR_TYPE_FILE =BIT (20 +BPF_BASE_TYPE_BITS ), +/* MEM is an object in a typed arena, reached through a native pointer. */ +MEM_ARENA =BIT (21 +BPF_BASE_TYPE_BITS ), + __BPF_TYPE_FLAG_MAX, __BPF_TYPE_LAST_FLAG = __BPF_TYPE_FLAG_MAX - 1, };
@@ -1916,6 +1919,9 @@ struct bpf_prog_aux {u64 prog_array_member_cnt;/* counts how many times as member of prog_array */ struct mutex ext_mutex;/* mutex for freplace_link_cnt and prog_array_member_cnt */ struct bpf_arena *arena; +/* typed arenas this program casts to or allocates from; referenced until the load fails */ +struct bpf_typed_arena **typed_arenas; +u32 typed_arena_cnt;void (*recursion_detected)(struct bpf_prog *prog);/* callback if recursion is detected */ /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */ const struct btf_type *attach_func_proto;
@@ -1991,6 +1997,17 @@ struct bpf_prog_aux {#define BPF_NR_CONTEXTS 4/* normal, softirq, hardirq, NMI */ +/* The typed arena of a program-BTF struct this program registered, or NULL. */ +static inline struct bpf_typed_arena *bpf_prog_typed_arena (const struct bpf_prog_aux *aux,u32 btf_id) +{ +u32 i; + +for (i =0 ; i < aux->typed_arena_cnt; i++) +if (aux->typed_arenas[i]->btf_id == btf_id) +return aux->typed_arenas[i]; +return NULL ; +} +struct bpf_prog {u16 pages;/* Number of allocated pages */ u32 jited:1, /* Is our filter JIT'ed? */
include/linux/bpf_verifier.h +19 -0
@@ -661,6 +661,7 @@ struct bpf_insn_aux_data {u64 insert_off; };struct btf_struct_meta *kptr_struct_meta; +struct bpf_typed_arena *typed_arena;/* named by a cast or a typed arena kfunc call */ u64 map_key_state;/* constant (32 bit) key tracking for maps */ int ctx_field_size;/* the ctx field size for load insn, maybe 0 */ u32 seen;/* this insn was processed by the verifier at env->pass_cnt */
@@ -1462,6 +1463,23 @@ static inline bool type_is_ptr_alloc_obj(u32 type) !(type_flag (type) &PTR_UNTRUSTED ); } +/* A pointer to an object in a typed arena: trusted, never NULL once checked, offset within the object. */ +static inline bool type_is_typed_arena_obj (u32 type) +{ +return base_type (type) ==PTR_TO_BTF_ID &&type_flag (type) &MEM_ARENA ; +} + +/* An object of a program-BTF struct: allocated by the program, or in a typed arena. */ +static inline bool type_is_local_obj (u32 type) +{ +return type & (MEM_ALLOC |MEM_ARENA ); +} + +static inline bool insn_is_typed_arena_cast (const struct bpf_insn *insn) +{ +return insn->code == (BPF_ALU64 |BPF_MOV |BPF_X ) && insn->off ==BPF_TYPED_ARENA_CAST ; +} +static inline bool type_is_non_owning_ref (u32 type) {return type_is_ptr_alloc_obj (type) &&type_flag (type) &NON_OWN_REF ;
@@ -1824,6 +1842,7 @@ int bpf_optimize_bpf_loop(struct bpf_verifier_env *env);void bpf_opt_hard_wire_dead_code_branches (struct bpf_verifier_env *env);int bpf_opt_remove_dead_code (struct bpf_verifier_env *env);int bpf_opt_remove_nops (struct bpf_verifier_env *env); +int bpf_lower_typed_arena_insns (struct bpf_verifier_env *env);int bpf_opt_subreg_zext_lo32_rnd_hi32 (struct bpf_verifier_env *env,const union bpf_attr *attr);int bpf_convert_ctx_accesses (struct bpf_verifier_env *env);int bpf_jit_subprogs (struct bpf_verifier_env *env);
include/uapi/linux/bpf.h +6 -0
@@ -1423,6 +1423,12 @@ enum {enum bpf_addr_space_cast {BPF_ADDR_SPACE_CAST =1 , +/* +* dst = typed_arena_cast(src, imm): src holds any 64-bit value, dst +* becomes a pointer to the object it names in the typed arena of the +* struct whose program-BTF type ID is imm. dst may be src. +*/ +BPF_TYPED_ARENA_CAST =2 , };/* flags for BPF_MAP_UPDATE_ELEM command */
kernel/bpf/backtrack.c +5 -3
@@ -319,7 +319,7 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,*/ return 0 ; }else if (opcode ==BPF_MOV ) { -if (BPF_SRC (insn->code) ==BPF_X ) { +if (BPF_SRC (insn->code) ==BPF_X && insn->off !=BPF_TYPED_ARENA_CAST ) {/* dreg = sreg or dreg = (s8, s16, s32)sreg * dreg needs precision after this insn * sreg needs precision before this insn
@@ -328,11 +328,13 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,if (sreg !=BPF_REG_FP )bt_set_reg (bt, sreg); }else { -/* dreg = K +/* dreg = K, or dreg = typed_arena_cast(sreg, imm) * dreg needs precision after this insn. * Corresponding register is already marked * as precise=true in this verifier state. -* No further markings in parent are necessary +* No further markings in parent are necessary; +* a cast yields a pointer that is safe for any +* value of sreg, which needs no precision. */ bt_clear_reg (bt, dreg); }
kernel/bpf/core.c +2 -0
@@ -3085,6 +3085,8 @@ static void bpf_prog_free_deferred(struct work_struct *work) aux = container_of(work, struct bpf_prog_aux, work);#ifdef CONFIG_BPF_SYSCALL bpf_free_kfunc_btf_tab (aux->kfunc_btf_tab); +/* The typed arenas outlive the program; only the load's failure retracts them. */ +kfree (aux->typed_arenas);#endif #ifdef CONFIG_CGROUP_BPF if (aux->cgroup_atype !=CGROUP_BPF_ATTACH_TYPE_INVALID )
kernel/bpf/disasm.c +9 -0
@@ -175,6 +175,12 @@ static bool is_addr_space_cast(const struct bpf_insn *insn) insn->off ==BPF_ADDR_SPACE_CAST ; } +static bool is_typed_arena_cast (const struct bpf_insn *insn) +{ +return insn->code == (BPF_ALU64 |BPF_MOV |BPF_X ) && + insn->off ==BPF_TYPED_ARENA_CAST ; +} +/* Special (internal-only) form of mov, used to resolve per-CPU addrs: * dst_reg = src_reg + <percpu_base_off> * BPF_ADDR_PERCPU is used as a special insn->off value.
@@ -208,6 +214,9 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs,verbose (cbs->private_data,"(%02x) r%d = addr_space_cast(r%d, %u, %u)" , insn->code, insn->dst_reg, insn->src_reg, ((u32 )insn->imm) >>16 , (u16 )insn->imm); + }else if (is_typed_arena_cast (insn)) { +verbose (cbs->private_data,"(%02x) r%d = typed_arena_cast(r%d, %d)" , + insn->code, insn->dst_reg, insn->src_reg, insn->imm); }else if (is_mov_percpu_addr (insn)) {verbose (cbs->private_data,"(%02x) r%d = &(void __percpu *)(r%d)" , insn->code, insn->dst_reg, insn->src_reg);
kernel/bpf/fixups.c +33 -0
@@ -876,6 +876,39 @@ int bpf_opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env,* struct __sk_buff -> struct sk_buff * struct bpf_sock_ops -> struct sock */ +/* +* Replace every typed_arena_cast with the sanitizing sequence of the typed +* arena the verifier registered for it. The type is part of the instruction, +* so a cast has exactly one typed arena by the time it gets here. +*/ +int bpf_lower_typed_arena_insns (struct bpf_verifier_env *env) +{ +struct bpf_insn *insn = env->prog->insnsi; +int i, cnt, delta =0 , insn_cnt = env->prog->len; +const struct bpf_typed_arena *ta; +struct bpf_insn insn_buf[8 ]; +struct bpf_prog *new_prog; + +for (i =0 ; i < insn_cnt; i++, insn++) { +if (!insn_is_typed_arena_cast (insn)) +continue ; + ta = env->insn_aux_data[i + delta].typed_arena; +if (!ta) { +verifier_bug (env,"typed_arena_cast at insn %d has no typed arena" , i); +return -EFAULT ; + } + cnt =bpf_typed_arena_cast_insns (ta, insn->dst_reg, insn->src_reg, insn_buf); + new_prog =bpf_patch_insn_data (env, i + delta, insn_buf, cnt); +if (!new_prog) +return -ENOMEM ; + delta += cnt -1 ; + env->prog = new_prog; + insn = new_prog->insnsi + i + delta; + } + +return 0 ; +} +int bpf_convert_ctx_accesses (struct bpf_verifier_env *env) {struct bpf_subprog_info *subprogs = env->subprog_info;
kernel/bpf/log.c +3 -2
@@ -432,14 +432,15 @@ const char *reg_type_str(struct bpf_verifier_env *env, enum bpf_reg_type type)strscpy (postfix,"_or_null" ); } -snprintf (prefix,sizeof (prefix),"%s%s%s%s%s%s%s" , +snprintf (prefix,sizeof (prefix),"%s%s%s%s%s%s%s%s" , type &MEM_RDONLY ?"rdonly_" :"" , type &MEM_RINGBUF ?"ringbuf_" :"" , type &MEM_USER ?"user_" :"" , type &MEM_PERCPU ?"percpu_" :"" , type &MEM_RCU ?"rcu_" :"" , type &PTR_UNTRUSTED ?"untrusted_" :"" , - type &PTR_TRUSTED ?"trusted_" :"" + type &PTR_TRUSTED ?"trusted_" :"" , + type &MEM_ARENA ?"typed_arena_" :"" ); snprintf(env->tmp_str_buf, TMP_STR_BUF_LEN, "%s%s%s",
kernel/bpf/verifier.c +206 -1
245 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +6 -0
@@ -1423,6 +1423,12 @@ enum {enum bpf_addr_space_cast {BPF_ADDR_SPACE_CAST =1 , +/* +* dst = typed_arena_cast(src, imm): src holds any 64-bit value, dst +* becomes a pointer to the object it names in the typed arena of the +* struct whose program-BTF type ID is imm. dst may be src. +*/ +BPF_TYPED_ARENA_CAST =2 , };/* flags for BPF_MAP_UPDATE_ELEM command */
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 5/16 bpf: Allow scalar and atomic access to typed arena objects
In reply to Kumar Kartikeya DwivediLet a program read and write the ordinary fields of a typed arena object, and run atomics on them, through the native pointer the cast produced. Every byte the pointer can reach is mapped, either with real objects or with the type's scratch chunk, so the accesses are plain loads and stores:
no probe mode, no exception table entry, and no address check at the access. The verifier's bounds are the whole check. It already keeps a BTF pointer's offset constant and inside the object, and the object is inside its slot by construction, so a typed pointer never leaves its typed arena. This is what the sanitizing cast buys: the cost of trust is paid once, where a value enters, and every access after it is as cheap as a load from the stack.
Reuse the rules for program-allocated objects. A typed arena object is a program-BTF struct with a special-field record, as an allocated object is, so give both the same treatment where the code asked whether a pointer is
allocated: reads and writes of scalar fields are permitted, an access that overlaps a special field is rejected, a pointer field loads as a scalar rather than as a pointer, and flexible arrays are not walked. Writes are permitted because the memory under the object is never unmapped while a program that saw it can still run, which is what allocated objects need a reference for. The fault-prone dereference predicate does not match the class, so the context conversion pass leaves the accesses alone and load-acquire is allowed on it. Helpers and kfuncs keep rejecting the class as plain memory, since none of the argument type tables lists it; a kfunc that wants a typed arena pointer asks for it by type, as the page kfuncs and the kptr exchange do.
--- kernel/bpf/btf.c | 9 +++++---- kernel/bpf/verifier.c | 18 ++++++++---------- 2 files changed, 13 insertions(+), 14 deletions(-)
kernel/bpf/btf.c +5 -4
@@ -7782,7 +7782,7 @@ int btf_struct_access(struct bpf_verifier_log *log,u32 id = reg->btf_id;int err; -while (type_is_alloc (reg->type)) { +while (type_is_local_obj (reg->type)) {struct btf_struct_meta *meta;struct btf_record *rec;int i;
@@ -7807,14 +7807,15 @@ int btf_struct_access(struct bpf_verifier_log *log, t =btf_type_by_id (btf, id); do { err = btf_struct_walk(log, btf, t, off, size, &id, &tmp_flag, - field_name, !type_is_alloc(reg->type)); + field_name, !type_is_local_obj(reg->type)); switch (err) { case WALK_PTR: - /* For local types, the destination register cannot + /* + * For local types, the destination register cannot * become a pointer again. */ - if (type_is_alloc(reg->type)) + if (type_is_local_obj(reg->type)) return SCALAR_VALUE; /* If we found the pointer or scalar on t+off, * we're done.
kernel/bpf/verifier.c +8 -10
@@ -6341,12 +6341,6 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,u32 btf_id =0 ;int ret; -/* The access rules for typed arena objects come with a later patch. */ -if (type_is_typed_arena_obj (reg->type)) { -verbose (env,"typed arena access is not supported yet\n" ); -return -EACCES ; - } -if (!env->allow_ptr_leaks) { verbose(env, "'struct %s' access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n",
@@ -6398,7 +6392,7 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,return -EACCES ; } -if (env->ops->btf_struct_access && !type_is_alloc (reg->type) && atype ==BPF_WRITE ) { +if (env->ops->btf_struct_access && !type_is_local_obj (reg->type) && atype ==BPF_WRITE ) {if (!btf_is_kernel (reg->btf)) {verifier_bug (env,"reg->btf must be kernel btf" );return -EFAULT ;
@@ -6409,10 +6403,14 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,"%s cannot write into ptr_%s at off=%d size=%d\n" , reg_arg_name(env, argno), tname, off, size); }else { -/* Writes are permitted with default btf_struct_access for -* program allocated objects (which always have id > 0). +/* +* Writes are permitted with default btf_struct_access for +* program allocated objects (which always have id > 0) and +* for typed arena objects, whose memory is never unmapped +* under a program. */ -if (atype !=BPF_READ && !type_is_ptr_alloc_obj (reg->type)) { +if (atype !=BPF_READ && !type_is_ptr_alloc_obj (reg->type) && + !type_is_typed_arena_obj (reg->type)) {verbose (env,"only read is supported\n" );return -EACCES ; }
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 6/16 bpf: Support special fields in typed arena objects
In reply to Kumar Kartikeya DwivediLet bpf_kptr_xchg() take a pointer to a referenced or percpu kptr field of a typed arena object, so that objects in arena memory can own references to kernel objects and to program-allocated objects. The exchange is a single atomic word swap on a native address, so it needs nothing from the arena:
the field's record comes from the struct's BTF as it does for an allocated object, the destination may carry the field's offset, and the value is matched against the field's type as before. Direct loads and stores of the field stay rejected, as they are for allocated objects; the exchange is the only access. A reference left in an object is dropped when its chunk is released or the map is destroyed, including one stored into a dummy object of the scratch chunk by a program that used a pointer nobody allocated.
Kptrs are the only special fields a typed arena object may hold, and the reason is how its memory behaves. A chunk is released after a grace period that covers the invocations that started before the release was requested, not the ones that start after it and cast into the range, and a fault in the middle of a kfunc swaps the page under the object for the scratch chunk, so the rest of the kfunc runs on the dummy object that every unallocated slot shares. A field survives that only if each operation on it is one atomic instruction, so that it lands on the real page or on the dummy but is never split, and if the kernel keeps no pointer into the object, so that nothing dangles once the memory is gone or reused. A kptr is exactly that: the exchange is the only operation, and the referenced object is held through the pointer value alone.
Timers, workqueues, task work and RCU heads are not. Their kfuncs take a lock in the field and touch it several times, the async callback object records the object's address for the callback, and an RCU head is linked into the RCU callback list in place. A release or a fault in the middle leaves a lock taken on a real page and dropped on the dummy, a callback running on memory that now holds another object, or the dummy's RCU head queued twice. Locks, lists, rbtrees and refcounts stay refused as well; the memory is native, and a program builds those from typed pointer fields and atomics without kernel help.
The way to give a typed object a timer, a workqueue, task work or an RCU head is to allocate an object holding them with bpf_obj_new() and keep it in a kptr field of the typed object. Allocated memory is owned by the allocator and freed only by bpf_obj_drop() after its fields are cancelled, so nothing swaps it or reuses it under a kfunc or a callback. That needs those fields to be allowed in allocated objects, which a later patch adds.
--- kernel/bpf/verifier.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +4 -2
@@ -386,7 +386,7 @@ static struct btf_record *reg_btf_record(const struct bpf_reg_state *reg)if (reg->type ==PTR_TO_MAP_VALUE ) { rec = reg->map_ptr->record; - }else if (type_is_ptr_alloc_obj (reg->type)) { + }else if (type_is_ptr_alloc_obj (reg->type) ||type_is_typed_arena_obj (reg->type)) { meta =btf_find_struct_meta (reg->btf, reg->btf_id);if (meta) rec = meta->record;
@@ -8063,7 +8063,7 @@ static int process_kptr_func(struct bpf_verifier_env *env, int regno,struct btf_record *rec;u32 kptr_off; -if (type_is_ptr_alloc_obj (reg->type)) { +if (type_is_ptr_alloc_obj (reg->type) ||type_is_typed_arena_obj (reg->type)) { rec =reg_btf_record (reg); }else {/* PTR_TO_MAP_VALUE */ map_ptr = reg->map_ptr;
@@ -8834,6 +8834,7 @@ static const struct bpf_reg_types kptr_xchg_dest_types = {PTR_TO_BTF_ID |MEM_ALLOC ,PTR_TO_BTF_ID |MEM_ALLOC |NON_OWN_REF ,PTR_TO_BTF_ID |MEM_ALLOC |NON_OWN_REF |MEM_RCU , + PTR_TO_BTF_ID | MEM_ARENA, } }; static const struct bpf_reg_types dynptr_types = {
@@ -9154,6 +9155,7 @@ static int check_func_arg_reg_off(struct bpf_verifier_env *env,case PTR_TO_BTF_ID |MEM_RCU :case PTR_TO_BTF_ID |MEM_ALLOC |NON_OWN_REF :case PTR_TO_BTF_ID |MEM_ALLOC |NON_OWN_REF |MEM_RCU : +case PTR_TO_BTF_ID |MEM_ARENA :/* When referenced PTR_TO_BTF_ID is passed to release function, * its fixed offset must be 0. bpf_refcount_acquire() returns the * pointer it was given while incrementing the refcount at the
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 7/16 bpf: Trust typed pointer fields of typed arena objects
In reply to Kumar Kartikeya DwivediA pointer member of a typed arena object whose pointee is itself a struct with a typed arena is a typed pointer field: it holds a pointer to an object of that struct, or 0, and nothing else, because the verifier lets nothing else be written there. Typed objects live in memory only programs can write, every store into such a field is a 64-bit store of a typed arena pointer to the pointee struct at offset zero or of the constant 0, and a chunk comes back zeroed when it is reused. A stale pointer, to an object whose chunk was released and claimed again, still names a whole object of the type. So a 64-bit load from the field gives back a value that is an object of the pointee type or 0 without a check, and a linked structure in a typed arena is walked with plain loads: no cast per hop, no annotation.
Detect the fields from BTF alone. A struct is typed when it has a special field, a kptr today, or a pointer member to a typed struct, taken to the
closure: the closure runs when the program's BTF is parsed, before the struct records, and is kept in the BTF, so that parsing a record can ask whether a pointee is typed. A struct whose only pointers are to itself and that has no special field of its own never joins, and pointer members tagged as arena pointers are user addresses and stay scalars. The field becomes a new record kind, BPF_TYPED_PTR, eight bytes aligned to eight like a kptr, allowed in arrays and nested structs, with nothing to acquire, release or free. It counts as a special field, so a struct holding only a pointer to a typed struct, a list head for instance, gets a typed arena of its own. The same member of a struct allocated with bpf_obj_new() keeps its old meaning, a plain pointer that loads as a scalar, and so does the same shape in a map value or in the raw arena, whose memory is not disciplined.
The load yields a new form of typed arena pointer, PTR_UNSANITIZED: the value is trusted to be an object or 0 but is not canonical, since nothing masked it into its slot, and it may be 0, which no NULL check is required to exclude. It survives 64-bit copies, spills and fills, and 64-bit compares on the raw value, so a loop ends with the usual test against 0. It may be stored back into a typed pointer field or anywhere a pointer store is allowed. What it cannot do yet is serve as an address: a load or store through it, arithmetic on it, or passing it to a call is refused until the next patch teaches the lowering to sanitize it in place at that point.
Narrower loads and stores of a typed pointer field, stores of anything but a typed pointer to the pointee or 0, and atomics on the field are rejected, as the field's invariant depends on them. A struct whose special fields are only inherited from a nested struct has no record of its own and is refused at registration rather than treated as a verifier bug.
--- include/linux/bpf.h | 12 ++++ include/linux/bpf_verifier.h | 6 ++ kernel/bpf/btf.c | 131 +++++++++++++++++++++++++++++++---- kernel/bpf/log.c | 3 +- kernel/bpf/syscall.c | 3 + kernel/bpf/verifier.c | 124 +++++++++++++++++++++++++++++++-- 6 files changed, 260 insertions(+), 19 deletions(-)
include/linux/bpf.h +12 -0
@@ -218,6 +218,7 @@ enum btf_field_type {BPF_RES_SPIN_LOCK = (1 <<12 ),BPF_TASK_WORK = (1 <<13 ),BPF_RCU_HEAD = (1 <<14 ), + BPF_TYPED_PTR = (1 << 15), }; enum bpf_cgroup_storage_type {
@@ -451,6 +452,8 @@ static inline const char *btf_field_type_name(enum btf_field_type type)return "bpf_task_work" ;case BPF_RCU_HEAD :return "bpf_rcu_head" ; +case BPF_TYPED_PTR : +return "typed_ptr" ;default :WARN_ON_ONCE (1 );return "unknown" ;
@@ -478,6 +481,7 @@ static inline u32 btf_field_type_size(enum btf_field_type type)case BPF_KPTR_REF :case BPF_KPTR_PERCPU :case BPF_UPTR : +case BPF_TYPED_PTR :return sizeof (u64);case BPF_LIST_HEAD :return sizeof (struct bpf_list_head );
@@ -514,6 +518,7 @@ static inline u32 btf_field_type_align(enum btf_field_type type)case BPF_KPTR_REF :case BPF_KPTR_PERCPU :case BPF_UPTR : +case BPF_TYPED_PTR :return __alignof__(u64 );case BPF_LIST_HEAD :return __alignof__(struct bpf_list_head );
@@ -562,6 +567,7 @@ static inline void bpf_obj_init_field(const struct btf_field *field, void *addr)case BPF_UPTR :case BPF_TASK_WORK :case BPF_RCU_HEAD : +case BPF_TYPED_PTR :break ;default :WARN_ON_ONCE (1 );
@@ -948,6 +954,12 @@ enum bpf_type_flag {/* MEM is an object in a typed arena, reached through a native pointer. */ MEM_ARENA =BIT (21 +BPF_BASE_TYPE_BITS ), +/* +* MEM_ARENA pointer as loaded from a typed pointer field: an object of +* the type or 0, not yet masked into its slot. +*/ +PTR_UNSANITIZED =BIT (22 +BPF_BASE_TYPE_BITS ), + __BPF_TYPE_FLAG_MAX, __BPF_TYPE_LAST_FLAG = __BPF_TYPE_FLAG_MAX - 1, };
include/linux/bpf_verifier.h +6 -0
@@ -1475,6 +1475,12 @@ static inline bool type_is_local_obj(u32 type)return type & (MEM_ALLOC |MEM_ARENA ); } +/* A typed arena pointer as a typed pointer field holds it, not yet rounded into its slot. */ +static inline bool type_is_unsanitized_arena_obj (u32 type) +{ +return type_is_typed_arena_obj (type) &&type_flag (type) &PTR_UNSANITIZED ; +} +static inline bool insn_is_typed_arena_cast (const struct bpf_insn *insn) {return insn->code == (BPF_ALU64 |BPF_MOV |BPF_X ) && insn->off ==BPF_TYPED_ARENA_CAST ;
kernel/bpf/btf.c +118 -13
252 lines not on this page · raw message
kernel/bpf/log.c +2 -1
@@ -432,7 +432,7 @@ const char *reg_type_str(struct bpf_verifier_env *env, enum bpf_reg_type type)strscpy (postfix,"_or_null" ); } - snprintf(prefix, sizeof(prefix), "%s%s%s%s%s%s%s%s", + snprintf(prefix, sizeof(prefix), "%s%s%s%s%s%s%s%s%s", type & MEM_RDONLY ? "rdonly_" : "", type & MEM_RINGBUF ? "ringbuf_" : "", type & MEM_USER ? "user_" : "",
@@ -440,6 +440,7 @@ const char *reg_type_str(struct bpf_verifier_env *env, enum bpf_reg_type type) type &MEM_RCU ?"rcu_" :"" , type &PTR_UNTRUSTED ?"untrusted_" :"" , type &PTR_TRUSTED ?"trusted_" :"" , + type &PTR_UNSANITIZED ?"unsanitized_" :"" , type &MEM_ARENA ?"typed_arena_" :"" );
kernel/bpf/syscall.c +3 -0
@@ -688,6 +688,7 @@ void btf_record_free(struct btf_record *rec)case BPF_WORKQUEUE :case BPF_TASK_WORK :case BPF_RCU_HEAD : +case BPF_TYPED_PTR :/* Nothing to release */ break ;default :
@@ -743,6 +744,7 @@ struct btf_record *btf_record_dup(const struct btf_record *rec)case BPF_WORKQUEUE :case BPF_TASK_WORK :case BPF_RCU_HEAD : +case BPF_TYPED_PTR :/* Nothing to acquire */ break ;default :
@@ -877,6 +879,7 @@ void bpf_obj_free_fields(const struct btf_record *rec, void *obj)case BPF_RB_NODE :case BPF_REFCOUNT :case BPF_RCU_HEAD : +case BPF_TYPED_PTR :break ;default :WARN_ON_ONCE (1 );
kernel/bpf/verifier.c +119 -5
198 lines not on this page · raw message
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 8/16 bpf: Canonicalize loaded typed arena pointers where they are used
In reply to Kumar Kartikeya DwivediA typed arena pointer loaded from a typed pointer field is an object of its type or 0, but it is not canonical: nothing masked it into its slot, and it may be 0. The previous patch refused to use it as an address. Sanitize it instead where it is used, in place: when an instruction loads or stores through such a pointer, or an atomic runs on it, when arithmetic moves it, or when it goes to a helper or kfunc, the lowering prepends the typed arena's cast sequence to that instruction, on that register, and on that path the register is canonical from then on. Compares, copies, spills and stores of the value as a value need nothing, so a list is walked with plain loads and ends with the usual test against 0, and a pointer that is only passed along is never touched.
This is the whole of the NULL question. The field's discipline says the value is an object or 0. A program that tests for 0 sees the raw value and takes its branch. A program that does not test, or tests wrongly, uses the value as an address, the sequence maps 0 to object 0 of the slice, and the access lands on a whole object of the type, the same outcome as feeding 0 to a cast. Nothing the program does with the value can reach outside the slice, so the verifier asks for no annotation and no check: the SFI scheme decides the result, not the type system, and a NULL check is a matter of program logic alone. The alternative, a maybe-NULL pointer type that must be checked before every use, would put a check on every hop of a traversal that the sanitization makes unnecessary.
The sequence is lowered once per instruction and runs on every path through it, so it must be harmless on every path. It is idempotent on a canonical pointer of the same typed arena at offset zero, which is what a path that got its pointer from a cast or from an earlier sanitization brings. It is wrong on a pointer with an offset into its object, since the mask rounds to the object base, on a pointer of another typed arena, whose base and mask differ, and on any other value. So the verifier records the register and its typed arena in the instruction's aux data when a path needs the sequence, and rejects the program when another path brings that register in a form the sequence would corrupt, or when a second register needs it at the same instruction, which no compiler-generated code does. Arithmetic sanitizes before it moves the pointer for the same reason: the offset must be added to the object base, not masked away later.
The later optimization phase builds on the unsanitized form: a load through such a pointer that nothing writes to can be a probed load instead of a sanitized one, and a chain of reads then costs nothing beyond the loads. A store of a typed pointer into a typed object always sanitizes its destination, since the field's discipline depends on the store landing in a real object.
--- include/linux/bpf_verifier.h | 10 +++++ kernel/bpf/fixups.c | 38 +++++++++++++------ kernel/bpf/verifier.c | 71 ++++++++++++++++++++++++++++++++---- 3 files changed, 99 insertions(+), 20 deletions(-)
include/linux/bpf_verifier.h +10 -0
@@ -662,6 +662,16 @@ struct bpf_insn_aux_data { };struct btf_struct_meta *kptr_struct_meta;struct bpf_typed_arena *typed_arena;/* named by a cast or a typed arena kfunc call */ +/* +* Lazy sanitization of a typed arena pointer used as an address here: +* the register and its typed arena, whether a path brought the pointer +* unsanitized, and the registers some path brought in a form the +* sanitizing sequence would corrupt. +*/ +struct bpf_typed_arena *sanitize_arena; +u16 sanitize_plain; +u8 sanitize_reg; +bool sanitize_needed;u64 map_key_state;/* constant (32 bit) key tracking for maps */ int ctx_field_size;/* the ctx field size for load insn, maybe 0 */ u32 seen;/* this insn was processed by the verifier at env->pass_cnt */
kernel/bpf/fixups.c +26 -12
@@ -877,27 +877,41 @@ int bpf_opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env,* struct bpf_sock_ops -> struct sock */ /* -* Replace every typed_arena_cast with the sanitizing sequence of the typed -* arena the verifier registered for it. The type is part of the instruction, -* so a cast has exactly one typed arena by the time it gets here. +* Lower the instructions the verifier tied to a typed arena. A typed_arena_cast +* becomes the sanitizing sequence of the typed arena the verifier registered +* for it; the type is part of the instruction, so a cast has exactly one. An +* instruction that uses a typed pointer loaded from a typed pointer field as +* an address gets the same sequence prepended, in place on that register: the +* verifier checked that every path brings that register here as an +* unsanitized pointer of that typed arena, or as a canonical one at offset +* zero, on which the sequence is a no-op. */ int bpf_lower_typed_arena_insns (struct bpf_verifier_env *env) {struct bpf_insn *insn = env->prog->insnsi;int i, cnt, delta =0 , insn_cnt = env->prog->len; -const struct bpf_typed_arena *ta; -struct bpf_insn insn_buf[8 ]; +struct bpf_insn insn_buf[16 ]; +struct bpf_insn_aux_data *aux;struct bpf_prog *new_prog;for (i =0 ; i < insn_cnt; i++, insn++) { -if (!insn_is_typed_arena_cast (insn)) -continue ; - ta = env->insn_aux_data[i + delta].typed_arena; -if (!ta) { -verifier_bug (env,"typed_arena_cast at insn %d has no typed arena" , i); -return -EFAULT ; + aux = &env->insn_aux_data[i + delta]; + cnt =0 ; +if (aux->sanitize_needed) + cnt =bpf_typed_arena_cast_insns (aux->sanitize_arena, aux->sanitize_reg, + aux->sanitize_reg, insn_buf); +if (insn_is_typed_arena_cast (insn)) { +if (!aux->typed_arena) { +verifier_bug (env,"typed_arena_cast at insn %d has no typed arena" , i); +return -EFAULT ; + } + cnt +=bpf_typed_arena_cast_insns (aux->typed_arena, insn->dst_reg, + insn->src_reg, insn_buf + cnt); + }else if (cnt) { + insn_buf[cnt++] = *insn; } - cnt =bpf_typed_arena_cast_insns (ta, insn->dst_reg, insn->src_reg, insn_buf); +if (!cnt) +continue ; new_prog =bpf_patch_insn_data (env, i + delta, insn_buf, cnt);if (!new_prog)return -ENOMEM ;
kernel/bpf/verifier.c +63 -8
83 lines not on this page · raw message
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 9/16 bpf: Add typed arena page allocation and release kfuncs
In reply to Kumar Kartikeya DwivediGive programs control over the memory behind a typed arena. So far every chunk of one reads as the dummy objects of the scratch chunk once touched; now a program can back a range with real objects and give it back:
void *bpf_typed_arena_alloc_pages(void *map, u64 local_type_id__k,
void *addr, u32 *page_cnt, int node_id);
void bpf_typed_arena_free_pages(void *map, u64 local_type_id__k,
void *ptr, u32 page_cnt);The prototypes follow the raw page kfuncs, with the struct's local BTF type ID naming the typed arena inside the map as a verifier-known constant, as bpf_obj_new() takes its type. The verifier requires the program to have an associated arena map and BTF, registers the typed arena for the struct as a cast does, records it in the call's aux data, and refuses a call site that two paths reach with two type IDs, since the fixup replaces the type ID register with the typed arena once per call site: the kfunc receives the registered typed arena and only checks that it belongs to the map it was given. The address is a typed pointer naming the chunk of the first object, NULL for any range, ignored by the verifier and validated by the kfunc; a loaded, unsanitized pointer passed there is sanitized before the call like any call argument. The request is in pages, like the raw allocator's, but the unit of backing is the chunk, so the request is rounded up to whole chunks and the count granted is written back through the pointer: an object larger than a page is never half allocated, and a program learns how many objects it got. The generic kfunc argument rules classify a pointer to a scalar as writable fixed-size memory, so a spilled constant in that slot does not survive the call as known. The allocator returns a typed pointer to the first object of the range, or NULL, checked once and then used natively, and the objects follow at slot strides. Both kfuncs are safe under a spin
lock: the allocator takes the arena's resilient spinlock and uses the lock-free allocators, and the release only queues work. The flags argument of the raw allocator is left out to stay within five arguments; a variant can add it, kfuncs not being ABI.
Allocation claims the chunk range in the type's bitmap under the spinlock and installs each chunk as one allocation of its order into empty entries
only: a chunk the fault path faulted to scratch is skipped by a search or fails a fixed request, because a scratch page is never replaced by a real one. The fault path marks its chunk without the lock, so it can still slip in between the claim and the install; the chunks installed so far are then backed out under the lock, the chunk it took keeps its mark, and a search retries a few times while a fixed request fails. The bits are set one at a time so that a word-wide update cannot undo the fault path's atomic mark. One allocation per chunk also removes the array of pages the raw allocator sizes against the lock-free kmalloc limit, and makes a multi-page object contiguous in the direct map, which is what lets its special fields be dropped after its mapping is gone.
Release is always deferred. The chunks stay mapped and marked until a worker has waited for an RCU and an RCU-tasks-trace grace period, so every invocation that started before the release keeps valid objects, sleepable ones included. The worker then clears the entries and the marks under the spinlock, flushes the TLB, drops the special fields of every object of each real chunk from its block, and frees it. A range is released once: the release marks its chunks pending under the spinlock, and a further release of any of them is refused until the worker has cleared the marks, since a second release queued behind the first would run after the first has let the range be claimed again and take it away from its new owner. An invocation that casts into the range after the release was requested is racing with it: it stays memory safe, and it sees the real objects before the clear and the dummy object after. Releasing a chunk that the fault path took only clears its entries, after which it can be claimed or faulted again. A release that cannot queue its work in a non-sleepable context leaves the chunks allocated until the map is freed. Map teardown flushes the worker.
--- kernel/bpf/arena.c | 391 ++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 60 +++++++ 2 files changed, 451 insertions(+)
kernel/bpf/arena.c +391 -0
@@ -11,6 +11,7 @@#include <linux/vmalloc.h> #include <linux/pagemap.h> #include <asm/tlbflush.h> +#include <linux/rcupdate_wait.h> #include "range_tree.h" /*
@@ -103,6 +104,9 @@ struct bpf_arena {struct irq_work free_irq;struct work_struct free_work;struct llist_head free_spans; +struct irq_work typed_free_irq; +struct work_struct typed_free_work; +struct llist_head typed_free_spans; };static void arena_free_worker (struct work_struct *work);
@@ -114,6 +118,17 @@ struct arena_free_span {u32 page_cnt; }; +static void typed_arena_free_worker (struct work_struct *work); +static void typed_arena_free_irq (struct irq_work *iw); + +/* A chunk range of a typed arena whose release is queued. */ +struct typed_arena_free_span { +struct llist_node node; +struct bpf_typed_arena *ta; +u32 coff; +u32 chunk_cnt; +}; +static unsigned long typed_arena_region (struct bpf_arena *arena) {return (unsigned long )arena->kern_vm->addr;
@@ -565,6 +580,347 @@ static void typed_arenas_free(struct bpf_arena *arena) } } +static bool typed_arena_chunks_free (const struct bpf_typed_arena *ta,unsigned long coff, +unsigned long cnt) +{ +return find_next_bit (ta->chunks, coff + cnt, coff) >= coff + cnt; +} + +static bool typed_arena_chunks_taken (const struct bpf_typed_arena *ta,unsigned long coff, +unsigned long cnt) +{ +return find_next_zero_bit (ta->chunks, coff + cnt, coff) >= coff + cnt; +} + +static bool typed_arena_chunks_pending (const struct bpf_typed_arena *ta,unsigned long coff, +unsigned long cnt) +{ +return find_next_bit (ta->pending, coff + cnt, coff) < coff + cnt; +} + +/* +* The bits are set one at a time: the fault path marks a chunk it faults to +* scratch with an atomic set from any context, and a word-wide update here +* could undo that mark. +*/ +static void typed_arena_chunks_mark (struct bpf_typed_arena *ta,unsigned long coff, +unsigned long cnt,bool taken) +{ +unsigned long i; + +for (i = coff; i < coff + cnt; i++) { +if (taken) +set_bit (i, ta->chunks); +else +clear_bit (i, ta->chunks); + } +} + +struct typed_install_data { +struct bpf_arena *arena; +struct page *head; +unsigned long start; +int i; +}; + +/* +* The typed arena installer maps the pages of one chunk's allocation and only +* fills empty entries: a scratch page is never replaced, see +* typed_arena_handle_page_fault(). Pairs with the atomic scratch installer. +*/ +static int apply_range_set_typed_cb (pte_t *pte,unsigned long addr,void *data) +{ +struct typed_install_data *d = data; +struct page *page = d->head + ((addr - d->start) >>PAGE_SHIFT ); + +if (!ptep_try_set (pte,mk_pte (page,PAGE_KERNEL ))) +return -EBUSY ; + d->i++; +WRITE_ONCE (d->arena->nr_pages, d->arena->nr_pages +1 ); +return 0 ; +} + +struct typed_clear_data { +struct bpf_arena *arena; +const struct bpf_typed_arena *ta; +struct page *head; +}; + +/* +* Clear the entries of one chunk. A real chunk is one allocation, so the page +* at the chunk's first entry is its head, which is reported for freeing once +* the mapping is gone; scratch pages are left to the scratch chunk. +*/ +static int apply_range_clear_typed_cb (pte_t *pte,unsigned long addr,void *data) +{ +struct typed_clear_data *d = data; +pte_t old_pte; +struct page *page; + + old_pte =ptep_get_and_clear (&init_mm, addr, pte); +if (pte_none (old_pte) || !pte_present (old_pte)) +return 0 ; + page =pte_page (old_pte); +if (page ==typed_arena_scratch_page (d->ta, addr)) +return 0 ; +if (!((addr - (unsigned long )d->ta->base) & (bpf_typed_arena_chunk (d->ta) -1 ))) + d->head = page; +WRITE_ONCE (d->arena->nr_pages, d->arena->nr_pages -1 ); +return 0 ; +} + +/* +* Back a chunk range of a typed arena with zeroed objects and return the +* address of its first object, or 0. @addr names the chunk of the first +* object, or is 0 for any range; *@page_cnt is the request in pages, rounded +* up to whole chunks, and receives the count granted. The range is claimed in +* the chunk bitmap under the arena's spinlock before anything is installed, so +* a chunk the fault path faulted to scratch is skipped by a search, or fails a +* fixed request. The fault path marks its chunk without the lock, so it can +* still win the race between the claim and the install; the chunks installed +* so far are then backed out, the chunk it took stays taken, and a search is +* retried a few times while a fixed request fails. Each chunk is one +* allocation of its order from the lock-free allocator, which the kfunc needs +* because it may run under a spin lock; there is no array of pages to size. +*/ +static unsigned long typed_arena_alloc_pages (struct bpf_typed_arena *ta,unsigned long addr, +u32 *page_cnt,int node_id) +{ +unsigned long base = (unsigned long )ta->base, nr_chunks =typed_arena_nr_chunks (ta); + struct bpf_arena *arena = container_of(ta->map, struct bpf_arena, map); +unsigned int order =typed_arena_chunk_order (ta); +unsigned long chunk =bpf_typed_arena_chunk (ta); +struct mem_cgroup *new_memcg, *old_memcg; +unsigned long chunk_cnt, coff =0 , start, done, i; +struct typed_install_data data; +struct typed_clear_data cdata; +struct llist_node *pos, *t; +struct llist_head freed; +unsigned long flags; +struct page *head; +int ret, attempt =0 ; + +if (node_id !=NUMA_NO_NODE && + ((unsigned int )node_id >= nr_node_ids || !node_online (node_id))) +return 0 ; +if (!*page_cnt) +return 0 ; + chunk_cnt =DIV_ROUND_UP ((unsigned long )*page_cnt,typed_arena_chunk_pages (ta)); +if (chunk_cnt > nr_chunks) +return 0 ; +if (addr) { +if (addr - base >=bpf_typed_arena_size (ta)) +return 0 ; + coff = (addr - base) >> ta->chunk_shift; +if (chunk_cnt > nr_chunks - coff) +return 0 ; + } + +bpf_map_memcg_enter (&arena->map, &old_memcg, &new_memcg); +retry: +if (raw_res_spin_lock_irqsave (&arena->spinlock, flags)) + goto out; + +if (addr) { +if (!typed_arena_chunks_free (ta, coff, chunk_cnt)) + goto out_unlock; + }else { + coff =bitmap_find_next_zero_area (ta->chunks, nr_chunks,0 , chunk_cnt,0 ); +if (coff >= nr_chunks) + goto out_unlock; + } +typed_arena_chunks_mark (ta, coff, chunk_cnt, true); + start = base + (coff << ta->chunk_shift); + + data.arena = arena; +for (done =0 ; done < chunk_cnt; done++) { + head =alloc_pages_nolock (__GFP_ACCOUNT, node_id, order); +if (!head) { + ret = -ENOMEM ; + goto back_out; + } + data.head = head; + data.start = start + (done << ta->chunk_shift); + data.i =0 ; + ret =apply_to_page_range (&init_mm, data.start, chunk, apply_range_set_typed_cb, + &data); +if (ret) { +/* The fault path took this chunk: give back the allocation whole. */ + cdata.arena = arena; + cdata.ta = ta; +if (data.i) +apply_to_existing_page_range (&init_mm, data.start, + (unsigned long )data.i <<PAGE_SHIFT , + apply_range_clear_typed_cb, &cdata); +free_pages_nolock (head, order); + goto back_out; + } + } +flush_vmap_cache (start, chunk_cnt << ta->chunk_shift); +raw_res_spin_unlock_irqrestore (&arena->spinlock, flags); +bpf_map_memcg_exit (old_memcg, new_memcg); + *page_cnt = chunk_cnt *typed_arena_chunk_pages (ta); +return start; + +back_out: +/* +* Back out the chunks installed before the one that failed. On -EBUSY +* that chunk is the one the fault path took, and it keeps its mark. +*/ +init_llist_head (&freed); + cdata.arena = arena; + cdata.ta = ta; +for (i =0 ; i < chunk_cnt; i++) +if (ret != -EBUSY || i != done) +clear_bit (coff + i, ta->chunks); +for (i =0 ; i < done; i++) { + cdata.head =NULL ; +apply_to_existing_page_range (&init_mm, start + (i << ta->chunk_shift), chunk, + apply_range_clear_typed_cb, &cdata); +if (cdata.head) +__llist_add (&cdata.head->pcp_llist, &freed); + } +raw_res_spin_unlock_irqrestore (&arena->spinlock, flags); +flush_tlb_kernel_range (start, start + (chunk_cnt << ta->chunk_shift)); +llist_for_each_safe (pos, t,__llist_del_all (&freed)) + free_pages_nolock(llist_entry(pos, struct page, pcp_llist), order); +if (ret == -EBUSY && !addr && ++attempt <3 ) + goto retry; + goto out; + +out_unlock: +raw_res_spin_unlock_irqrestore (&arena->spinlock, flags); +out: +bpf_map_memcg_exit (old_memcg, new_memcg); +return 0 ; +} + +/* +* Queue the release of the chunks covering a page range of a typed arena. The +* chunks stay mapped and marked until the worker has waited for the grace +* periods, so that every invocation that started before this call keeps its +* objects. Every chunk of the range must be taken, by real pages or by the +* fault path, and none of them may have a release queued already: a second +* release of a chunk would run after the first has let it be claimed again, +* and take it away from its new owner. Releasing a chunk the fault path took +* clears its entries, after which it can be claimed or faulted again. +*/ +static void typed_arena_free_pages (struct bpf_typed_arena *ta,unsigned long addr,u32 page_cnt) +{ +unsigned long base = (unsigned long )ta->base, size =bpf_typed_arena_size (ta); + struct bpf_arena *arena = container_of(ta->map, struct bpf_arena, map); +unsigned long off, first, last, flags; +struct typed_arena_free_span *s; + +if (!page_cnt || addr - base >= size) +return ; + off = addr - base; +if (page_cnt > (size - off) >>PAGE_SHIFT ) +return ; + first = off >> ta->chunk_shift; + last = (off + ((unsigned long )page_cnt <<PAGE_SHIFT ) -1 ) >> ta->chunk_shift; + + s =kmalloc_nolock (sizeof (*s), __GFP_ACCOUNT,NUMA_NO_NODE ); +if (!s) +/* +* The chunks stay allocated until the map is freed; nothing can +* be retried from here. +*/ +return ; +if (raw_res_spin_lock_irqsave (&arena->spinlock, flags)) + goto free_span; +if (!typed_arena_chunks_taken (ta, first, last - first +1 ) || +typed_arena_chunks_pending (ta, first, last - first +1 )) { +raw_res_spin_unlock_irqrestore (&arena->spinlock, flags); + goto free_span; + } +/* Only this path and the worker touch the pending bits, both under the lock. */ +bitmap_set (ta->pending, first, last - first +1 ); +raw_res_spin_unlock_irqrestore (&arena->spinlock, flags); + + s->ta = ta; + s->coff = first; + s->chunk_cnt = last - first +1 ; +llist_add (&s->node, &arena->typed_free_spans); +irq_work_queue (&arena->typed_free_irq); +return ; + +free_span: +kfree_nolock (s); +} + +/* +* Release the queued chunk ranges once every invocation that started before +* a release was requested is done with its objects: after an RCU and an RCU +* tasks trace grace period, since sleepable programs run under the latter. +* The entries are cleared and the marks dropped under the spinlock, chunk by +* chunk, then the TLB is flushed and each real chunk's allocation, one +* contiguous block, has the special fields of its objects dropped and is +* freed. An invocation that casts into the range after the release was +* requested races with it: it stays memory safe, and sees the real objects +* before the clear and the dummy object after. +*/ +static void typed_arena_free_worker (struct work_struct *work) +{ + struct bpf_arena *arena = container_of(work, struct bpf_arena, typed_free_work); +struct mem_cgroup *new_memcg, *old_memcg; +struct llist_node *list, *pos, *t, *p, *pn; +struct typed_arena_free_span *s; +struct typed_clear_data cdata; +struct bpf_typed_arena *ta; +struct llist_head heads; +unsigned long flags, start, i; +struct page *head; + + list =llist_del_all (&arena->typed_free_spans); +if (!list) +return ; + +synchronize_rcu_mult (call_rcu, call_rcu_tasks_trace); + +bpf_map_memcg_enter (&arena->map, &old_memcg, &new_memcg); +llist_for_each_safe (pos, t, list) { + s = llist_entry(pos, struct typed_arena_free_span, node); + ta = s->ta; + start = (unsigned long )ta->base + ((unsigned long )s->coff << ta->chunk_shift); + +init_llist_head (&heads); + cdata.arena = arena; + cdata.ta = ta; + +while (raw_res_spin_lock_irqsave (&arena->spinlock, flags)) +cpu_relax (); +for (i =0 ; i < s->chunk_cnt; i++) { + cdata.head =NULL ; +apply_to_existing_page_range (&init_mm, start + (i << ta->chunk_shift), +bpf_typed_arena_chunk (ta), + apply_range_clear_typed_cb, &cdata); +if (cdata.head) +__llist_add (&cdata.head->pcp_llist, &heads); + } +typed_arena_chunks_mark (ta, s->coff, s->chunk_cnt, false); +bitmap_clear (ta->pending, s->coff, s->chunk_cnt); +raw_res_spin_unlock_irqrestore (&arena->spinlock, flags); + +flush_tlb_kernel_range (start, + start + ((unsigned long )s->chunk_cnt << ta->chunk_shift)); +llist_for_each_safe (p, pn,__llist_del_all (&heads)) { + head = llist_entry(p, struct page, pcp_llist); +typed_arena_free_objects (ta,page_address (head)); +__free_pages (head,typed_arena_chunk_order (ta)); + } +kfree_nolock (s); + } +bpf_map_memcg_exit (old_memcg, new_memcg); +} + +static void typed_arena_free_irq (struct irq_work *iw) +{ + struct bpf_arena *arena = container_of(iw, struct bpf_arena, typed_free_irq); + +schedule_work (&arena->typed_free_work); +} +static struct bpf_map *arena_map_alloc (union bpf_attr *attr) {struct vm_struct *kern_vm;
@@ -613,6 +969,9 @@ static struct bpf_map *arena_map_alloc(union bpf_attr *attr)init_llist_head (&arena->free_spans);init_irq_work (&arena->free_irq, arena_free_irq);INIT_WORK (&arena->free_work, arena_free_worker); +init_llist_head (&arena->typed_free_spans); +init_irq_work (&arena->typed_free_irq, typed_arena_free_irq); +INIT_WORK (&arena->typed_free_work, typed_arena_free_worker);bpf_map_init_from_attr (&arena->map, attr); err =bpf_map_alloc_pages (&arena->map,NUMA_NO_NODE ,1 , &arena->scratch_page);
@@ -686,6 +1045,8 @@ static void arena_map_free(struct bpf_map *map)/* Ensure no pending deferred frees */ irq_work_sync (&arena->free_irq);flush_work (&arena->free_work); +irq_work_sync (&arena->typed_free_irq); +flush_work (&arena->typed_free_work);/* * free_vm_area() calls remove_vm_area() that calls free_unmap_vmap_area().
@@ -1463,12 +1824,42 @@ __bpf_kfunc int bpf_arena_reserve_pages(void *p__map, void *ptr__ign, u32 page_creturn arena_reserve_pages (arena, (long )ptr__ign, page_cnt); } + +/* +* The verifier registers the typed arena of local_type_id__k at load, checks +* that the map is the program's arena, and replaces the type ID register with +* the registered typed arena before the call. addr__ign is a typed pointer +* into that arena naming the chunk of the first object, or NULL for any +* range; *page_cnt is the request in pages and receives the count granted, +* rounded up to whole objects. +*/ +__bpf_kfunc void *bpf_typed_arena_alloc_pages(void *p__map, u64 local_type_id__k, void *addr__ign, +u32 *page_cnt,int node_id) +{ +struct bpf_typed_arena *ta = (struct bpf_typed_arena *)local_type_id__k; + +if (ta->map != p__map) +return NULL ; +return (void *)typed_arena_alloc_pages (ta, (unsigned long )addr__ign, page_cnt, node_id); +} + +__bpf_kfunc void bpf_typed_arena_free_pages(void *p__map, u64 local_type_id__k, void *ptr__ign, +u32 page_cnt) +{ +struct bpf_typed_arena *ta = (struct bpf_typed_arena *)local_type_id__k; + +if (ta->map != p__map) +return ; +typed_arena_free_pages (ta, (unsigned long )ptr__ign, page_cnt); +}__bpf_kfunc_end_defs (); BTF_KFUNCS_START(arena_kfuncs) BTF_ID_FLAGS(func, bpf_arena_alloc_pages, KF_ARENA_RET | KF_ARENA_ARG2 | KF_SPINLOCK_SAFE) BTF_ID_FLAGS(func, bpf_arena_free_pages, KF_ARENA_ARG2 | KF_SPINLOCK_SAFE) BTF_ID_FLAGS(func, bpf_arena_reserve_pages, KF_ARENA_ARG2 | KF_SPINLOCK_SAFE) +BTF_ID_FLAGS(func, bpf_typed_arena_alloc_pages, KF_RET_NULL | KF_SPINLOCK_SAFE) +BTF_ID_FLAGS(func, bpf_typed_arena_free_pages, KF_SPINLOCK_SAFE) BTF_KFUNCS_END(arena_kfuncs) static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/bpf/verifier.c +60 -0
102 lines not on this page · raw message
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 10/16 bpf: Let typed_arena_cast copy pointers the verifier already trusts
In reply to Kumar Kartikeya DwivediThe compiler inserts the cast at every use of a pointer to a struct with special fields, so that programs never write one, and it cannot tell what the pointer is: the same struct lives in typed arenas, in allocated objects, in map values and on the stack. So the verifier decides per path what the cast takes. A scalar, a raw arena pointer, a typed arena pointer loaded from a field and not yet sanitized, one of another struct, or one that arithmetic moved inside its object is sanitized as before. A sanitized pointer of the struct's own typed arena at offset zero is already what the cast produces, so it is copied, and since the sequence is a no-op on it, such a path may share the instruction with sanitizing ones. Any other verified pointer, to an allocated object, a map value, the stack or anything else, is copied with its state, offset and references: the cast has nothing to add to what the verifier knows, so it needs neither an arena map nor a registration, and a program that only allocates objects of the struct loads as before. The lowering is per instruction and runs on every path through it, so a path that copies such a pointer cannot share the instruction with one that sanitizes, since the sequence would mask a pointer that is not in the slice; that program is rejected. An instruction that only copied lowers to a move, or to nothing when dst is src, which takes removing the instruction, as the nop removal pass has already run.
--- kernel/bpf/fixups.c | 35 ++++++++++++++++++++++-------- kernel/bpf/verifier.c | 50 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 9 deletions(-)
kernel/bpf/fixups.c +26 -9
@@ -897,17 +897,34 @@ int bpf_lower_typed_arena_insns(struct bpf_verifier_env *env)for (i =0 ; i < insn_cnt; i++, insn++) { aux = &env->insn_aux_data[i + delta]; cnt =0 ; -if (aux->sanitize_needed) - cnt =bpf_typed_arena_cast_insns (aux->sanitize_arena, aux->sanitize_reg, - aux->sanitize_reg, insn_buf);if (insn_is_typed_arena_cast (insn)) { -if (!aux->typed_arena) { -verifier_bug (env,"typed_arena_cast at insn %d has no typed arena" , i); -return -EFAULT ; +/* +* A cast that sanitized on some path lowers to the +* sequence, a no-op on the paths that brought a sanitized +* pointer of the same typed arena. One that only copied +* verified pointers is a move, or nothing at all. +*/ +if (aux->sanitize_needed) { +if (!aux->typed_arena) { +verifier_bug (env,"typed_arena_cast at insn %d has no typed arena" , i); +return -EFAULT ; + } + cnt =bpf_typed_arena_cast_insns (aux->typed_arena, insn->dst_reg, + insn->src_reg, insn_buf); + }else if (insn->dst_reg != insn->src_reg) { + insn_buf[cnt++] =BPF_MOV64_REG (insn->dst_reg, insn->src_reg); + }else { +int err =verifier_remove_insns (env, i + delta,1 ); + +if (err) +return err; + delta--; + insn = env->prog->insnsi + i + delta; +continue ; } - cnt +=bpf_typed_arena_cast_insns (aux->typed_arena, insn->dst_reg, - insn->src_reg, insn_buf + cnt); - }else if (cnt) { + }else if (aux->sanitize_needed) { + cnt =bpf_typed_arena_cast_insns (aux->sanitize_arena, aux->sanitize_reg, + aux->sanitize_reg, insn_buf); insn_buf[cnt++] = *insn; }if (!cnt)
kernel/bpf/verifier.c +50 -0
69 lines not on this page · raw message
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 11/16 libbpf: Support the typed_arena_cast instruction
In reply to Kumar Kartikeya DwivediThe typed_arena_cast instruction turns any 64-bit value into a pointer to an object of a program-BTF struct in the struct's typed arena, the kernel-only region of an arena map. It is encoded as a 64-bit register move with an off of BPF_TYPED_ARENA_CAST and the struct's local BTF type ID in imm, and the kernel takes the ID from there, so a compiler that emits the instruction records a BPF_CORE_TYPE_ID_LOCAL relocation against it, the way it does for the ld_imm64 behind __builtin_btf_type_id().
Let the relocation patcher write a local type ID into that instruction. It is the one ALU form with a register source that carries a relocation, and it takes no other relocation kind; nothing else about the patching of ALU immediates changes.
Add bpf_typed_arena_cast() to bpf_helpers.h, wrapping the compiler builtin under its feature macro, so that programs cast through one name whatever the toolchain.
--- tools/lib/bpf/bpf_helpers.h | 13 +++++++++++++ tools/lib/bpf/relo_core.c | 19 +++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-)
tools/lib/bpf/bpf_helpers.h +13 -0
@@ -340,6 +340,19 @@ enum libbpf_tristate {/* Helper macro to print out debug messages */ #define bpf_printk(fmt, args...) ___bpf_pick_printk(args)(fmt, ##args) +/* +* Turn any 64-bit value into a pointer to an object of the given struct type +* in its typed arena, with the typed_arena_cast instruction: the kernel masks +* the value into the struct's slice of the arena map's typed region, so the +* result names an object of that type whatever the value was, and the +* verifier trusts it. The struct's local BTF type ID is relocated into the +* instruction. Requires compiler support. +*/ +#ifdef __BPF_FEATURE_TYPED_ARENA_CAST +#define bpf_typed_arena_cast (v, type) \ + ((typeof(type) *)__builtin_bpf_typed_arena_cast((v), *(typeof(type) *)0)) +#endif +struct bpf_iter_num ; extern int bpf_iter_num_new(struct bpf_iter_num *it, int start, int end) __weak __ksym;
tools/lib/bpf/relo_core.c +17 -2
@@ -1028,6 +1028,12 @@ static int insn_bytes_to_bpf_size(__u32 sz) } } +/* rX = typed_arena_cast(rY, <local type ID in imm>) */ +static bool is_typed_arena_cast_insn (struct bpf_insn *insn) +{ +return insn->code == (BPF_ALU64 |BPF_MOV |BPF_X ) && insn->off ==BPF_TYPED_ARENA_CAST ; +} +/* * Patch relocatable BPF instruction. *
@@ -1043,7 +1049,8 @@ static int insn_bytes_to_bpf_size(__u32 sz)* 3. rX = <imm64> (load with 64-bit immediate value); * 4. rX = *(T *)(rY + <off>), where T is one of {u8, u16, u32, u64}; * 5. *(T *)(rX + <off>) = rY, where T is one of {u8, u16, u32, u64}; -* 6. *(T *)(rX + <off>) = <imm>, where T is one of {u8, u16, u32, u64}. +* 6. *(T *)(rX + <off>) = <imm>, where T is one of {u8, u16, u32, u64}; +* 7. rX = typed_arena_cast(rY, <imm>), for a local type ID relocation only. */ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn, int insn_idx, const struct bpf_core_relo *relo,
@@ -1060,8 +1067,16 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,switch (class) {case BPF_ALU :case BPF_ALU64 : -if (BPF_SRC (insn->code) !=BPF_K ) +/* +* The typed arena cast is a register move whose imm names the +* struct by its local type ID; the kernel takes the ID from there. +*/ +if (is_typed_arena_cast_insn (insn)) { +if (relo->kind !=BPF_CORE_TYPE_ID_LOCAL ) + goto bad_insn; + }else if (BPF_SRC (insn->code) !=BPF_K ) { goto bad_insn; + }if (res->poison)return bpf_core_poison_insn (prog_name, relo_idx, insn, insn_idx);if (res->validate && insn->imm != orig_val) {
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 12/16 selftests/bpf: Build BPF objects with compiler-inserted typed arena casts
In reply to Kumar Kartikeya DwivediClang inserts typed_arena_cast where a pointer to a typed record is used, but only with the +typed-arena target feature, and it defines __BPF_FEATURE_TYPED_ARENA_CAST only then: a struct with kptr fields lives in map values of programs that run on kernels without the instruction, so the insertion has to be opted into. Probe the feature the way cpu v4 is probed, by asking the compiler for the macro, and pass it to every flavor when the compiler has it. The typed arena tests are gated on the macro and skip with a compiler that lacks the feature.
--- tools/testing/selftests/bpf/Makefile | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/Makefile +10 -3
@@ -15,6 +15,13 @@ ifneq ($(shell $(CLANG) --target=bpf -mcpu=help 2>&1 | grep 'v4'),) CLANG_CPUV4 := 1 endif +# Check whether clang inserts typed arena casts; the feature defines the macro +# the typed arena tests are gated on, so they skip with a clang without it +ifneq ($(shell echo | $(CLANG) --target=bpf -Xclang -target-feature -Xclang +typed-arena \ + -dM -E - 2>/dev/null | grep __BPF_FEATURE_TYPED_ARENA_CAST),) +CLANG_TYPED_ARENA := -Xclang -target-feature -Xclang +typed-arena +endif + # Order correspond to 'make run_tests' order TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_progs \ test_sockmap \
@@ -510,7 +517,7 @@ BINARY := test_progs BPF_CC := $(CLANG) BPF_CC_MSG := CLNG-BPF BPF_SYS_INCLUDES := $(CLANG_SYS_INCLUDES) -BPF_CC_FLAGS := -O2 $(BPF_TARGET_ENDIAN) -mcpu=v3 +BPF_CC_FLAGS := -O2 $(BPF_TARGET_ENDIAN) -mcpu=v3 $(CLANG_TYPED_ARENA) BPF_DEFINES := -DENABLE_ATOMICS_TESTS include Makefile.skel
@@ -526,12 +533,12 @@ $(OUTPUT)/test_progs: $(RUNNER_PREREQS) $(BPF_OBJS) $(ALL_SKELS) FORCE $(OUTPUT)/test_progs-no_alu32: $(RUNNER_PREREQS) FORCE +$(Q)$(RUNNER_MAKE) RUNNER=test_progs FLAVOR=no_alu32 TESTS_DIR=prog_tests \ - $(CLANG_RUNNER_ARGS) BPF_CC_FLAGS='-O2 $(BPF_TARGET_ENDIAN) -mcpu=v2' + $(CLANG_RUNNER_ARGS) BPF_CC_FLAGS='-O2 $(BPF_TARGET_ENDIAN) -mcpu=v2 $(CLANG_TYPED_ARENA)' ifneq ($(CLANG_CPUV4),) $(OUTPUT)/test_progs-cpuv4: $(RUNNER_PREREQS) FORCE +$(Q)$(RUNNER_MAKE) RUNNER=test_progs FLAVOR=cpuv4 TESTS_DIR=prog_tests \ - $(CLANG_RUNNER_ARGS) BPF_CC_FLAGS='-O2 $(BPF_TARGET_ENDIAN) -mcpu=v4' \ + $(CLANG_RUNNER_ARGS) BPF_CC_FLAGS='-O2 $(BPF_TARGET_ENDIAN) -mcpu=v4 $(CLANG_TYPED_ARENA)' \ BPF_DEFINES=-DENABLE_ATOMICS_TESTS endif
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 13/16 selftests/bpf: Test typed arena casts and registration
In reply to Kumar Kartikeya DwivediThe compiler inserts a typed_arena_cast where a value is converted to a pointer to a typed struct and before every use of such a pointer as an address, so the programs write no cast: an object is whatever value user space hands in, a raw arena pointer, or a pointer of another type, and the casts the verifier sees are all the compiler's.
Convert a value user space hands in and see it lowered to the mask, the base load and the add, with the pointer typed as the struct's object; convert a raw arena pointer, and a typed pointer passed through an opaque value and to another type, which land on objects like any other value. See the registration logged with the slot size. A cast of a pointer the verifier already trusts is the identity: an allocated object, a typed struct inside a map value and one on the stack keep their type and state through the cast the compiler inserts, and such programs need no arena.
Reject a cast in a program without an arena map, and structs with the special fields a typed arena does not carry: spin locks, resilient spin locks, timers, refcounts, list heads and rbtree roots. A struct without special fields is not typed to the compiler and no cast names it; the page kfuncs, which take a type ID, reject one in their own tests. Reject typed arena sizes that are not a power of two, below a page, above the region, or too small for one object, and two size declarations on one struct; accept sizes with a suffix and see the object count follow, take an object larger than a page in a slot of whole pages, and see the region refuse a third type once two 2 GiB ones have filled it.
A typed pointer is a kernel pointer: a 32-bit copy is the scalar a privileged program may take of any pointer, and a narrow store to the stack is an invalid spill.
The values user space would hand in stay unset: the verifier does not care where a value lands, and at run time an unset one names object 0 of its slice. The tests exist only when the compiler emits the cast; the runner checks a flag the object exports and skips otherwise. Add the typed arena size tag macro to bpf_experimental.h.
--- .../testing/selftests/bpf/bpf_experimental.h | 6 + .../selftests/bpf/prog_tests/verifier.c | 22 + .../bpf/progs/verifier_typed_arena.c | 528 ++++++++++++++++++ 3 files changed, 556 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_typed_arena.c
tools/testing/selftests/bpf/bpf_experimental.h +6 -0
@@ -9,6 +9,12 @@#define __contains (name, node) __attribute__((btf_decl_tag("contains:" #name ":" #node))) +/* +* Size of a struct's typed arena in bytes: a power of two with an optional K, +* M or G suffix, 128M unless declared. +*/ +#define __typed_arena_size (sz) __attribute__((btf_decl_tag("typed_arena_size:" #sz))) +/* Convenience macro to wrap over bpf_obj_new */ #define bpf_obj_new (type) ((type *)bpf_obj_new(bpf_core_type_id_local(type)))
tools/testing/selftests/bpf/prog_tests/verifier.c +22 -0
@@ -120,6 +120,7 @@#include "verifier_subreg.skel.h" #include "verifier_tailcall.skel.h" #include "verifier_tailcall_jit.skel.h" +#include "verifier_typed_arena.skel.h" #include "verifier_typedef.skel.h" #include "verifier_uninit.skel.h" #include "verifier_unpriv.skel.h"
@@ -303,6 +304,27 @@ void test_verifier_subprog_topo(void) { RUN(verifier_subprog_topo); }void test_verifier_subreg (void ) {RUN (verifier_subreg); }void test_verifier_tailcall (void ) {RUN (verifier_tailcall); }void test_verifier_tailcall_jit (void ) {RUN (verifier_tailcall_jit); } + +/* +* The typed arena tests need a compiler that emits the cast; without one the +* object holds no programs. +*/ +void test_verifier_typed_arena (void ) +{ +struct verifier_typed_arena *skel; +bool supported; + + skel =verifier_typed_arena__open (); +if (!ASSERT_OK_PTR (skel,"open" )) +return ; + supported = skel->rodata->typed_arena_supported; +verifier_typed_arena__destroy (skel); +if (!supported) { +test__skip (); +return ; + } +RUN (verifier_typed_arena); +}void test_verifier_typedef (void ) {RUN (verifier_typedef); }void test_verifier_uninit (void ) {RUN (verifier_uninit); }void test_verifier_unpriv (void ) {RUN (verifier_unpriv); }
tools/testing/selftests/bpf/progs/verifier_typed_arena.c new file +528 -0
528 lines not on this page · raw message
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 14/16 selftests/bpf: Test typed arena object access, kptrs and typed pointer fields
In reply to Kumar Kartikeya DwivediWrite and read a scalar field natively on a chunk faulted to scratch, run an atomic on it, and move the pointer within the object; reject an access beyond the object, a negative offset and a variable one. Walk a nested struct and see a pointer to a kernel struct load as a scalar. See helpers refuse the pointer as memory.
Exchange a kernel kptr into an object and out again, leave a local kptr in a dummy object for the map to drop, and reject direct loads and stores of a kptr field, an exchange on a scalar field and one with the wrong type.
Load a typed pointer field and see the pointer stay unsanitized until it is used as an address: the dereference carries the mask, base load and add in place, a compare of the value does not, a store of the raw value written by hand is accepted, a second dereference through the same register does not sanitize again, and pointer arithmetic sanitizes before it adds. A NULL dereferences object 0 of the slice, whether held directly or loaded from the field. The field takes a typed pointer, a loaded pointer or NULL, and the compiler casts what a program assigns to it, so a scalar and a pointer to another typed struct are accepted from C and rejected only when stored by hand; reject a narrow load or store and an atomic. The same member in raw arena memory is data: a stored pointer loads as a scalar and casts back to its object where it is used. A struct whose only special field is a pointer to a typed struct gets a typed arena of its own. One instruction sanitizes one typed arena: reject it when two paths bring pointers of different types, or a typed pointer on one path only, and reject the cast the compiler inserts when one path brings a loaded arena pointer and another an allocated object.
--- .../bpf/progs/verifier_typed_arena.c | 634 ++++++++++++++++++ 1 file changed, 634 insertions(+)
tools/testing/selftests/bpf/progs/verifier_typed_arena.c +634 -0
@@ -523,6 +523,640 @@ int narrow_store_is_invalid_spill(void *ctx)return 0 ; } +SEC("syscall") +__description("a scalar field is written and read natively, on a chunk faulted to scratch") +__success __retval(7) +__stderr("ERROR: Typed arena WRITE access to unallocated struct typed_obj at 0x{{[0-9a-f]+}}") +int access_scalar_field (void *ctx) +{ +struct typed_obj *obj; + +arena_bind (); + obj = ptr; + obj->value =7 ; +return obj->value; +} + +SEC("syscall") +__description("atomics run natively on a scalar field") +__success __retval(3) +int access_atomic(void *ctx) +{ +struct typed_obj *obj; + +arena_bind (); + obj = ptr; + obj->value =1 ; +__sync_fetch_and_add (&obj->value,2 ); +return obj->value; +} + +SEC("syscall") +__description("pointer arithmetic stays inside the object") +__success __retval(9) +int access_after_arithmetic(void *ctx) +{ +struct typed_obj *obj; +void *p; + +arena_bind (); + obj = ptr; + p = obj; + asmvolatile ("%[p] += 8" : [p]"+r" (p)); + *(__u64 *)p =9 ; +return obj->value; +} + +SEC("syscall") +__description("an access beyond the object is rejected") +__failure __msg("access beyond struct typed_obj") +int access_beyond_object(void *ctx) +{ +struct typed_obj *obj; + +arena_bind (); + obj = ptr; + ((__u64 *)obj)[2 ] =1 ; +return 0 ; +} + +SEC("syscall") +__description("a negative offset is rejected") +__failure __msg("invalid negative access") +int access_negative_offset(void *ctx) +{ +struct typed_obj *obj; +void *p; + +arena_bind (); + obj = ptr; + p = (void *)obj -8 ; +return *(__u64 *)p; +} + +SEC("syscall") +__description("a variable offset is rejected") +__failure __msg("{{variable (offset|typed_arena_ptr_ access)}}") +int access_variable_offset(void *ctx) +{ +struct typed_obj *obj; +void *p; + +arena_bind (); + obj = ptr; + p = (void *)obj + (bpf_get_prandom_u32 () &8 ); +return *(__u64 *)p; +} + +struct mixed_obj { + struct task_struct __kptr *task; +struct { +__u32 a; +__u32 b; + } inner; +struct task_struct *ptr; +}; + +SEC("syscall") +__description("nested structs are walked and pointers to kernel structs load as scalars") +__success __retval(3) +int access_nested_and_kernel_pointer_fields(void *ctx) +{ +struct mixed_obj *obj; + +arena_bind (); + obj = ptr; + obj->inner.b =3 ; +if (obj->ptr) +return 1 ; +return obj->inner.b; +} + +SEC("syscall") +__description("helpers do not take typed arena pointers as memory") +__failure __msg("R1 type=typed_arena_ptr_ expected=") +int helper_rejects_typed_pointer(void *ctx) +{ +struct typed_obj *obj; +__u64 src =0 ; + +arena_bind (); + obj = ptr; +bpf_probe_read_kernel (&obj->value,sizeof (obj->value), &src); +return 0 ; +} + +struct arena_node { +__u64 v; +}; + +struct kptr_obj { + struct task_struct __kptr *task; + struct arena_node __kptr *node; +__u64 value; +}; + +SEC("syscall") +__description("a kernel kptr is exchanged into and out of an object") +__success __retval(0) +int kptr_xchg_task(void *ctx) +{ +struct task_struct *task, *old; +struct kptr_obj *obj; + +arena_bind (); + obj = ptr; + task =bpf_task_acquire (bpf_get_current_task_btf ()); +if (!task) +return 2 ; + old =bpf_kptr_xchg (&obj->task, task); +if (old) +bpf_task_release (old); + old =bpf_kptr_xchg (&obj->task,NULL ); +if (!old) +return 3 ; +bpf_task_release (old); +return 0 ; +} + +SEC("syscall") +__description("a local kptr left in a dummy object is dropped with the map") +__success __retval(0) +int kptr_xchg_local(void *ctx) +{ +struct arena_node *n, *old; +struct kptr_obj *obj; + +arena_bind (); + obj = ptr; + n = bpf_obj_new(struct arena_node); +if (!n) +return 2 ; + n->v =42 ; + old =bpf_kptr_xchg (&obj->node, n); +if (old) +bpf_obj_drop (old); +return 0 ; +} + +SEC("syscall") +__description("a kptr field is not read directly") +__failure __msg("direct access to kptr is disallowed") +int kptr_read_directly(void *ctx) +{ +struct kptr_obj *obj; + +arena_bind (); + obj = ptr; +return obj->task !=NULL ; +} + +SEC("syscall") +__description("a kptr field is not written directly") +__failure __msg("direct access to kptr is disallowed") +int kptr_write_directly(void *ctx) +{ +struct kptr_obj *obj; + +arena_bind (); + obj = ptr; + obj->task =NULL ; +return 0 ; +} + +SEC("syscall") +__description("an exchange needs a kptr field") +__failure __msg("off=16 doesn't point to kptr") +int kptr_xchg_scalar_field(void *ctx) +{ +struct kptr_obj *obj; + +arena_bind (); + obj = ptr; +bpf_kptr_xchg (&obj->value,NULL ); +return 0 ; +} + +SEC("syscall") +__description("an exchange checks the value against the field's type") +__failure __msg("invalid kptr access, R2 type=ptr_arena_node expected=ptr_task_struct") +int kptr_xchg_wrong_type(void *ctx) +{ +struct kptr_obj *obj; +struct arena_node *n; + +arena_bind (); + obj = ptr; + n = bpf_obj_new(struct arena_node); +if (!n) +return 2 ; + n =bpf_kptr_xchg (&obj->task, n); +if (n) +bpf_obj_drop (n); +return 0 ; +} + +/* 32-byte slot, linked through a typed pointer field: the sanitize mask is 134217696 */ +struct node_obj { + struct task_struct __kptr *task; +struct node_obj *next; +__u64 value; +}; + +/* The same layout as node_obj, a different typed arena */ +struct pair_obj { + struct task_struct __kptr *task; +struct pair_obj *next; +__u64 value; +}; + +/* 8-byte slot, typed only by its pointer to a typed struct: the cast mask is 134217720 */ +struct head_obj { +struct node_obj *first; +}; + +SEC("syscall") +__description("a typed pointer field loads unsanitized, and a dereference sanitizes it in place") +__success __retval(0) __log_level(2) +__msg("R{{[0-9]}}=unsanitized_typed_arena_ptr_node_obj(") +__xlated("r{{[0-9]}} &= 134217720") +__xlated("r12 = 0x{{[0-9a-f]+}}") +__xlated("r{{[0-9]}} += r12") +__xlated("...") +__xlated("r{{[0-9]}} = *(u64 *)(r{{[0-9]}} +0)") +__xlated("r{{[0-9]}} &= 134217696") +__xlated("r12 = 0x{{[0-9a-f]+}}") +__xlated("r{{[0-9]}} += r12") +__xlated("r{{[0-9]}} = *(u64 *)(r{{[0-9]}} +16)") +int ptr_field_deref_sanitizes (void *ctx) +{ +struct head_obj *h; +struct node_obj *n; + +arena_bind (); + h = ptr; + n = h->first; +return n->value; +} + +SEC("syscall") +__description("a compare and a store of a loaded typed pointer use the raw value") +__success __retval(0) __log_level(2) +__msg("R{{[0-9]}}=unsanitized_typed_arena_ptr_node_obj(") +__msg("if r{{[0-9]}} == 0x0 goto") +__msg("R{{[0-9]}}=unsanitized_typed_arena_ptr_node_obj(") +__msg("*(u64 *)(r1 +8) = r2") +int ptr_field_compare_and_store_stay_raw (void *ctx) +{ +struct node_obj *n, *m; + +arena_bind (); + n = ptr; + m = n->next; +/* Opaque to the compiler, so that the compare is emitted. */ +barrier_var (m); +if (!m) +return 0 ; +/* The store is written by hand: the compiler would cast the value first. */ + asmvolatile ("r1 = %[n];" +"r2 = %[m];" +"*(u64 *)(r1 + 8) = r2;" + :: [n]"r" (n), [m]"r" (m) + :"r1" ,"r2" ,"memory" ); +return 0 ; +} + +SEC("syscall") +__description("a register sanitized by one dereference is not sanitized again by the next") +__success __retval(5) +__xlated("r1 = *(u64 *)(r1 +8)") +__xlated("r2 = 5") +__xlated("r1 &= 134217696") +__xlated("r12 = 0x{{[0-9a-f]+}}") +__xlated("r1 += r12") +__xlated("*(u64 *)(r1 +16) = r2") +__xlated("r0 = *(u64 *)(r1 +16)") +int ptr_field_second_deref_not_sanitized_again (void *ctx) +{ +struct node_obj *n; +__u64 ret; + +arena_bind (); + n = ptr; +/* Written by hand: the compiler would cast copies rather than reuse the register. */ + asmvolatile ("r1 = %[n];" +"r1 = *(u64 *)(r1 + 8);" +"r2 = 5;" +"*(u64 *)(r1 + 16) = r2;" +"r0 = *(u64 *)(r1 + 16);" +"%[ret] = r0;" + : [ret]"=r" (ret) : [n]"r" (n) + :"r0" ,"r1" ,"r2" ,"memory" ); +return ret; +} + +SEC("syscall") +__description("pointer arithmetic on a loaded typed pointer sanitizes it first") +__success __retval(0) +__xlated("r{{[0-9]}} = *(u64 *)(r{{[0-9]}} +8)") +__xlated("...") +__xlated("r1 &= 134217696") +__xlated("r12 = 0x{{[0-9a-f]+}}") +__xlated("r1 += r12") +__xlated("r1 += 16") +__xlated("*(u64 *)(r1 +0) = r2") +int ptr_field_arithmetic_sanitizes (void *ctx) +{ +struct node_obj *n, *m; + +arena_bind (); + n = ptr; + m = n->next; + asmvolatile ("r1 = %[m];" +"r2 = 0;" +"r1 += 16;" +"*(u64 *)(r1 + 0) = r2;" + :: [m]"r" (m) + :"r1" ,"r2" ,"memory" ); +return 0 ; +} + +SEC("syscall") +__description("a NULL typed pointer dereferences object 0 of the slice, held directly or loaded from a field") +__success __retval(42) +int ptr_field_null_lands_on_object_zero(void *ctx) +{ +struct node_obj *zero =NULL , *n, *m; + +arena_bind (); +/* +* The compiler treats a NULL dereference as undefined and would drop +* the store, fold the stored NULL into the load and the load into +* nothing; the barriers keep each value opaque so that the accesses +* are emitted. +*/ +barrier_var (zero); + zero->value =42 ; + n = ptr; + n->next =NULL ; +barrier_var (n); + m = n->next; +barrier_var (m); +return m->value; +} + +SEC("syscall") +__description("a typed pointer field takes a typed pointer, a loaded one, or NULL") +__success __retval(0) +int ptr_field_store_accepted(void *ctx) +{ +struct node_obj *n, *m, *p; + +arena_bind (); + n = ptr; + m = ptr2; + n->next = m; + p = m->next; + n->next = p; + n->next =NULL ; +return 0 ; +} + +SEC("syscall") +__description("a typed pointer field does not take a scalar") +__failure __msg("store into typed pointer field of struct node_obj expects a typed arena pointer to struct node_obj or NULL") +int ptr_field_store_scalar_rejected(void *ctx) +{ +struct node_obj *n; + +arena_bind (); + n = ptr; +/* Written by hand: the compiler would cast the value before the store. */ + asmvolatile ("r6 = %[n];" +"call %[bpf_get_prandom_u32];" +"r1 = r6;" +"*(u64 *)(r1 + 8) = r0;" + :: [n] "r"(n), __imm(bpf_get_prandom_u32) + :"r0" ,"r1" ,"r2" ,"r3" ,"r4" ,"r5" ,"r6" ,"memory" ); +return 0 ; +} + +SEC("syscall") +__description("a scalar assigned to a typed pointer field is cast by the compiler first") +__success __retval(0) +__xlated("call unknown") +__xlated("...") +__xlated("r{{[0-9]}} &= 134217696") +__xlated("r12 = 0x{{[0-9a-f]+}}") +__xlated("r{{[0-9]}} += r12") +__xlated("*(u64 *)(r{{[0-9]}} +8) = r{{[0-9]}}") +int ptr_field_store_scalar_cast_by_compiler (void *ctx) +{ +struct node_obj *n; + +arena_bind (); + n = ptr; + n->next = (void *)(long )bpf_get_prandom_u32 (); +return 0 ; +} + +SEC("syscall") +__description("a typed pointer field does not take a pointer to another typed struct") +__failure __msg("store into typed pointer field of struct node_obj expects a typed arena pointer to struct node_obj or NULL") +int ptr_field_store_other_type_rejected(void *ctx) +{ +struct typed_obj *other; +struct node_obj *n; + +arena_bind (); + n = ptr; + other = ptr; +/* Written by hand: the compiler would re-cast the value to node_obj first. */ + asmvolatile ("r1 = %[n];" +"r2 = %[o];" +"*(u64 *)(r1 + 8) = r2;" + :: [n]"r" (n), [o]"r" (other) + :"r1" ,"r2" ,"memory" ); +return 0 ; +} + +SEC("syscall") +__description("a pointer to another typed struct assigned to a typed pointer field is re-cast by the compiler") +__success __retval(0) +int ptr_field_store_other_type_recast(void *ctx) +{ +struct typed_obj *other; +struct node_obj *n; + +arena_bind (); + n = ptr; + other = ptr; + n->next = (struct node_obj *)other; +return n->next ==NULL ; +} + +SEC("syscall") +__description("a typed pointer field is loaded whole") +__failure __msg("typed pointer field of struct node_obj must be accessed with a 64-bit load or store") +int ptr_field_narrow_load_rejected(void *ctx) +{ +struct node_obj *n; + +arena_bind (); + n = ptr; + asmvolatile ("r1 = %[n];" +"w2 = *(u32 *)(r1 + 8);" + :: [n]"r" (n) + :"r1" ,"r2" ); +return 0 ; +} + +SEC("syscall") +__description("a typed pointer field is stored whole") +__failure __msg("typed pointer field of struct node_obj must be accessed with a 64-bit load or store") +int ptr_field_narrow_store_rejected(void *ctx) +{ +struct node_obj *n; + +arena_bind (); + n = ptr; + asmvolatile ("r1 = %[n];" +"w2 = 0;" +"*(u32 *)(r1 + 8) = w2;" + :: [n]"r" (n) + :"r1" ,"r2" ,"memory" ); +return 0 ; +} + +SEC("syscall") +__description("a typed pointer field takes no atomic operation") +__failure __msg("typed pointer field of struct node_obj") +int ptr_field_atomic_rejected(void *ctx) +{ +struct node_obj *n, *m; + +arena_bind (); + n = ptr; + m = ptr2; +__sync_val_compare_and_swap ((__u64 *)&n->next,0 , (__u64 )m); +return 0 ; +} + +/* The same member outside a typed object is data */ +struct raw_holder { +struct node_obj *n; +__u64 v; +}; + +SEC("syscall") +__description("a pointer stored in raw arena memory loads as a scalar and casts back to its object") +__success __retval(0) +int ptr_field_in_raw_memory_is_scalar(void *ctx) +{ + struct raw_holder __arena *h; +struct node_obj *n, *again; + + h =bpf_arena_alloc_pages (&arena,NULL ,1 ,NUMA_NO_NODE ,0 ); +if (!h) +return 1 ; + n = ptr; + n->value =7 ; + h->n = n; + again = h->n; +return again != n || again->value !=7 ; +} + +SEC("syscall") +__description("a struct typed only by a pointer to a typed struct gets a typed arena of its own") +__success __retval(0) __log_level(2) +__msg("typed arena for struct head_obj: slot 8 bytes") +int ptr_field_makes_struct_typed (void *ctx) +{ +struct head_obj *h; +struct node_obj *n; + +arena_bind (); + h = ptr; + n = ptr2; + h->first = n; +return h->first != n; +} + +/* +* The cast the compiler inserts is one instruction on every path through it: +* a loaded typed pointer needs the sanitizing sequence there, an allocated +* object needs the identity, and the two cannot share a lowering. +*/ +SEC("syscall") +__description("one cast cannot both sanitize an arena pointer and pass an allocated object") +__failure __msg("casts values that need different treatment on different paths") +int cast_conflicts_between_arena_and_allocated(void *ctx) +{ +struct node_obj *n, *a; +struct head_obj *h; + +arena_bind (); + a = bpf_obj_new(struct node_obj); +if (!a) +return 1 ; + h = ptr; +if (bpf_get_prandom_u32 () &1 ) + n = h->first; +else + n = a; + n->value =1 ; +bpf_obj_drop (a); +return 0 ; +} + +SEC("syscall") +__description("one instruction sanitizes one typed arena: two types on two paths are rejected") +__failure __msg("sanitizes typed arena pointers of different types on different paths") +int ptr_field_sanitize_two_types_at_one_insn(void *ctx) +{ +struct node_obj *n; +struct pair_obj *p; + +arena_bind (); + n = ptr; + p = ptr; + asmvolatile ("call %[bpf_get_prandom_u32];" +"if r0 == 0 goto 1f;" +"r1 = %[n];" +"r1 = *(u64 *)(r1 + 8);" +"goto 2f;" +"1: r1 = %[p];" +"r1 = *(u64 *)(r1 + 8);" +"2: r2 = *(u64 *)(r1 + 16);" + :: [n] "r"(n), [p] "r"(p), __imm(bpf_get_prandom_u32) + :"r0" ,"r1" ,"r2" ,"r3" ,"r4" ,"r5" ,"memory" ); +return 0 ; +} + +SEC("syscall") +__description("one instruction sanitizes one typed arena: a typed pointer on one path only is rejected") +__failure __msg("sanitizes a typed arena pointer only on some paths") +int ptr_field_sanitize_on_one_path(void *ctx) +{ +struct node_obj *n; + +arena_bind (); + n = ptr; + asmvolatile ("r2 = 0;" +"*(u64 *)(r10 - 16) = r2;" +"call %[bpf_get_prandom_u32];" +"if r0 == 0 goto 1f;" +"r1 = %[n];" +"r1 = *(u64 *)(r1 + 8);" +"goto 2f;" +"1: r1 = r10;" +"r1 += -32;" +"2: r2 = *(u64 *)(r1 + 16);" + :: [n] "r"(n), __imm(bpf_get_prandom_u32) + :"r0" ,"r1" ,"r2" ,"r3" ,"r4" ,"r5" ,"memory" ); +return 0 ; +} +#endif /* __BPF_FEATURE_TYPED_ARENA_CAST */ char _license[] SEC("license") = "GPL";
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 15/16 selftests/bpf: Test typed arena page allocation and release
In reply to Kumar Kartikeya DwivediAllocate a page of typed arena objects, write one, reach it again through an opaque copy of its address and its neighbour by arithmetic on that copy, and check that the kfunc receives the registered typed arena in place of the type ID. Take the chunk a value names and see the same chunk refused twice, once for the chunk and once for a page inside it. Ask for one page of an object that spans several and see the request rounded up to the object's chunk, with the granted count written back. Release a chunk and see it keep its objects and stay taken within the same invocation, since the release is deferred behind a grace period.
Reject a use of the returned pointer without a NULL check, a struct without special fields, and a map that is not the program's arena. Declare the two kfuncs in bpf_experimental.h.
--- .../testing/selftests/bpf/bpf_experimental.h | 12 ++ .../bpf/progs/verifier_typed_arena.c | 142 ++++++++++++++++++ 2 files changed, 154 insertions(+)
tools/testing/selftests/bpf/bpf_experimental.h +12 -0
@@ -15,6 +15,18 @@*/ #define __typed_arena_size (sz) __attribute__((btf_decl_tag("typed_arena_size:" #sz))) +/* +* Back the chunks covering *page_cnt pages of the typed arena of the struct +* whose local BTF type ID is type_id with zeroed objects, at addr, a typed +* pointer naming the first chunk, or anywhere for NULL; round the count up +* to whole chunks and write it back; return a pointer to the first object, +* or NULL. Release such a range after a grace period, after which its +* objects read as the dummy object. Both are safe under a spin lock. +*/ +extern void *bpf_typed_arena_alloc_pages (void *map,__u64 type_id,void *addr,__u32 *page_cnt, +int node_id) __ksym; +extern void bpf_typed_arena_free_pages(void *map, __u64 type_id, void *ptr, __u32 page_cnt) __ksym; +/* Convenience macro to wrap over bpf_obj_new */ #define bpf_obj_new (type) ((type *)bpf_obj_new(bpf_core_type_id_local(type)))
tools/testing/selftests/bpf/progs/verifier_typed_arena.c +142 -0
148 lines not on this page · raw message
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 16/16 selftests/bpf: Exercise typed arenas at run time
In reply to Kumar Kartikeya DwivediDrive typed arenas from user space through a skeleton. Check that registration at load is accounted in the map's memory usage, that an allocated chunk adds its pages to it and a released one takes them away, that a value written through one invocation is read by another, and that a chunk is refused to a fixed request while it is taken. Leave a reference to the test module's object in a kptr field, release the chunk, and poll the object's reference count until the deferred release has dropped it, which proves the grace period and the field teardown ran; then claim the same chunk again and see it come back zeroed. Read an object nobody allocated and see the dummy object, see the chunk it faulted in refused to the allocator, release it, and poll until it can be claimed. Load the same object twice on one arena map and check that the two program BTFs get distinct typed arenas for the same struct.
Release the pages of one allocation one at a time and see them all come back, since the worker walks every span of a batch. Fill a typed arena, ask to release one page more than it holds past the address, and see the request refused while a release of the last chunk alone goes through. Release a chunk twice, the second time while the worker is waiting out the grace period of the first, claim the chunk again as soon as it is free, and see an object written to it survive the repeated request. Allocate more pages in one request than the allocator's batch holds and see the whole range served and returned. Allocate an object of two pages with a request for one and see the whole object granted, both pages usable, and the chunk returned.
Carve objects out of an allocated page, link them into a list through a typed pointer field and walk it with no cast: the sum of the values only reads, a second pass writes through every pointer, and the NULL that ends the list, dereferenced on purpose, reads object 0 of the slice.
User space hands objects around as opaque 64-bit values; converting one to a typed pointer casts it, and any value casts to an object, so a small offset names one as well as a pointer does. Programs that only convert values they hold reference the arena map explicitly, since that is what associates the arena with a program. The tests need a compiler that emits the cast and are skipped otherwise.
--- .../selftests/bpf/prog_tests/typed_arena.c | 489 ++++++++++++++++++ .../testing/selftests/bpf/progs/typed_arena.c | 354 +++++++++++++ 2 files changed, 843 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/typed_arena.c create mode 100644 tools/testing/selftests/bpf/progs/typed_arena.c
tools/testing/selftests/bpf/prog_tests/typed_arena.c new file +489 -0
@@ -0,0 +1,489 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ +#include <test_progs.h> +#include <sys/user.h> +#ifndef PAGE_SIZE /* on some archs it comes in sys/user.h */ +#include <unistd.h> +#define PAGE_SIZE getpagesize() +#endif + +#include "typed_arena.skel.h" + +/* The sizes progs/typed_arena.c declares */ +#define OBJ_ARENA_SIZE (256 * 1024) +#define WIDE_ARENA_SIZE (8 * 1024 * 1024) +#define BIG_OBJ_SIZE 8192 +#define LIST_LEN 64 + +/* The map's memory usage as the "memlock:" line of its fdinfo. */ +static long map_memlock (int map_fd) +{ +char path[64 ], line[128 ]; +long memlock =-1 ; +FILE *f; + +snprintf (path,sizeof (path),"/proc/self/fdinfo/%d" , map_fd); + f =fopen (path,"r" ); +if (!ASSERT_OK_PTR (f,"open_fdinfo" )) +return -1 ; +while (fgets (line,sizeof (line), f)) { +if (sscanf (line,"memlock:\t%ld" , &memlock) ==1 ) +break ; + } +fclose (f); +ASSERT_NEQ (memlock,-1 ,"parse_memlock" ); +return memlock; +} + +static int run_ret (struct bpf_program *prog,const char *name) +{ +LIBBPF_OPTS (bpf_test_run_opts, opts); +int err =bpf_prog_test_run_opts (bpf_program__fd (prog), &opts); + +if (!ASSERT_OK (err, name)) +return -1 ; +return opts.retval; +} + +static int run (struct bpf_program *prog,const char *name) +{ +int ret =run_ret (prog, name); + +if (!ASSERT_OK (ret, name)) +return -1 ; +return 0 ; +} + +/* Poll the module object's reference count until a deferred release has run. */ +static long wait_ref_cnt (struct typed_arena *skel,long want) +{ +int i; + +for (i =0 ; i <500 ; i++) { +if (run (skel->progs.read_ref_cnt,"read_ref_cnt" )) +return -1 ; +if (skel->bss->ref_cnt == want) +break ; +usleep (10000 ); + } +return skel->bss->ref_cnt; +} + +/* Poll the map's memory usage until it reaches the expected value. */ +static long wait_memlock (int map_fd,long want) +{ +long memlock =-1 ; +int i; + +for (i =0 ; i <500 ; i++) { + memlock =map_memlock (map_fd); +if (memlock == want) +break ; +usleep (10000 ); + } +return memlock; +} + +/* Poll a fixed allocation until the release of the chunk it names has run. */ +static int wait_alloc_at (struct typed_arena *skel) +{ +int i, ret =-1 ; + +for (i =0 ; i <500 ; i++) { + ret =run_ret (skel->progs.alloc_at,"alloc_at" ); +if (ret <=0 ) +break ; +usleep (10000 ); + } +return ret; +} + +/* Objects persist across invocations, and chunks come and go around them. */ +static void test_pages (void ) +{ +long ps =PAGE_SIZE , base, base_cnt; +struct typed_arena *skel; +void *p; +int fd; + + skel =typed_arena__open_and_load (); +if (!ASSERT_OK_PTR (skel,"open_load" )) +return ; + fd =bpf_map__fd (skel->maps.arena); + +/* Registration at load accounts the page tables and the scratch chunks. */ + base =map_memlock (fd); +ASSERT_GE (base, ps,"registered" ); +if (run (skel->progs.read_ref_cnt,"base_ref_cnt" )) + goto out; + base_cnt = skel->bss->ref_cnt; + +if (run (skel->progs.alloc,"alloc" )) + goto out; +ASSERT_EQ (skel->data->page_cnt,1 ,"granted" ); + p = skel->bss->ptr; +ASSERT_EQ (map_memlock (fd), base + ps,"after_alloc" ); + + skel->bss->value =42 ; +if (run (skel->progs.write_value,"write_value" )) + goto out; + skel->bss->value =0 ; +if (run (skel->progs.read_value,"read_value" )) + goto out; +ASSERT_EQ (skel->bss->value,42 ,"persisted" ); + +/* The chunk is taken until released. */ +ASSERT_EQ (run_ret (skel->progs.alloc_at,"alloc_at" ),1 ,"taken" ); + +if (run (skel->progs.stash_ref,"stash_ref" )) + goto out; +ASSERT_EQ (wait_ref_cnt (skel, base_cnt +1 ), base_cnt +1 ,"ref_stashed" ); + +/* Release drops the reference after the grace period and returns the memory. */ +if (run (skel->progs.free_pages,"free_pages" )) + goto out; +ASSERT_EQ (wait_ref_cnt (skel, base_cnt), base_cnt,"ref_dropped" ); +ASSERT_EQ (map_memlock (fd), base,"after_free" ); + +/* The same chunk can be claimed again, and comes back zeroed. */ +ASSERT_EQ (wait_alloc_at (skel),0 ,"reclaimed" ); +ASSERT_EQ (skel->bss->ptr, p,"same_object" ); +if (run (skel->progs.read_value,"read_fresh" )) + goto out; +ASSERT_EQ (skel->bss->value,0 ,"fresh" ); +if (run (skel->progs.free_pages,"free_again" )) + goto out; + +/* +* An object nobody allocated reads as the dummy object, and the chunk +* it faults in is taken until released in turn. Any value casts to an +* object, so a small offset names one as well as a pointer does. +*/ + skel->bss->ptr = (void *)(7 * ps); + skel->bss->value =1 ; +if (run (skel->progs.read_value,"read_unallocated" )) + goto out; +ASSERT_EQ (skel->bss->value,0 ,"dummy" ); +ASSERT_EQ (run_ret (skel->progs.alloc_at,"alloc_scratch_chunk" ),1 ,"scratch_taken" ); +if (run (skel->progs.free_pages,"free_scratch_chunk" )) + goto out; +ASSERT_EQ (wait_alloc_at (skel),0 ,"scratch_released" ); +out: +typed_arena__destroy (skel); +} + +/* +* A typed arena belongs to the program BTF that registered it. Two loads of +* the same object sharing one arena map carry two BTF objects, so a pointer +* of one names an object in a different typed arena when the other casts it. +*/ +static void test_identity (void ) +{ +struct typed_arena *skel1, *skel2 =NULL ; + + skel1 =typed_arena__open_and_load (); +if (!ASSERT_OK_PTR (skel1,"open_load1" )) +return ; + skel2 =typed_arena__open (); +if (!ASSERT_OK_PTR (skel2,"open2" )) + goto out; +if (!ASSERT_OK (bpf_map__reuse_fd (skel2->maps.arena,bpf_map__fd (skel1->maps.arena)), +"reuse_fd" )) + goto out; +if (!ASSERT_OK (typed_arena__load (skel2),"load2" )) + goto out; + +if (run (skel1->progs.alloc,"alloc1" )) + goto out; + skel1->bss->value =7 ; +if (run (skel1->progs.write_value,"write1" )) + goto out; + skel2->bss->ptr = skel1->bss->ptr; +if (run (skel2->progs.read_value,"read2" )) + goto out; +ASSERT_EQ (skel2->bss->value,0 ,"distinct_typed_arena" ); +if (run (skel1->progs.read_value,"read1" )) + goto out; +ASSERT_EQ (skel1->bss->value,7 ,"own_typed_arena" ); +out: +typed_arena__destroy (skel2); +typed_arena__destroy (skel1); +} + +/* Every span of a batch of releases is processed, not only the first one with real pages. */ +static void test_release_spans (void ) +{ +struct typed_arena *skel; +long ps =PAGE_SIZE , base; +void *p; +int fd, i; + + skel =typed_arena__open_and_load (); +if (!ASSERT_OK_PTR (skel,"open_load" )) +return ; + fd =bpf_map__fd (skel->maps.arena); + base =map_memlock (fd); + + skel->data->page_cnt =8 ; +if (run (skel->progs.alloc,"alloc" )) + goto out; +ASSERT_EQ (skel->data->page_cnt,8 ,"granted" ); + p = skel->bss->ptr; +ASSERT_EQ (map_memlock (fd), base +8 * ps,"after_alloc" ); + +/* Release the pages one at a time, so that the worker sees a span per page. */ + skel->data->page_cnt =1 ; +for (i =0 ; i <8 ; i++) { + skel->bss->ptr = p + i * ps; +if (run (skel->progs.free_pages,"free_pages" )) + goto out; + } +ASSERT_EQ (wait_memlock (fd, base), base,"all_released" ); +out: +typed_arena__destroy (skel); +} + +/* A release of more pages than the typed arena holds past the address is refused. */ +static void test_release_bounds (void ) +{ +long ps =PAGE_SIZE , base, nr =OBJ_ARENA_SIZE /PAGE_SIZE ; +struct typed_arena *skel; +int fd; + + skel =typed_arena__open_and_load (); +if (!ASSERT_OK_PTR (skel,"open_load" )) +return ; + fd =bpf_map__fd (skel->maps.arena); + base =map_memlock (fd); + +/* Fill the typed arena, so that a scan of its bitmap for a free chunk runs to the end. */ + skel->bss->ptr =NULL ; + skel->data->page_cnt = nr; +if (run (skel->progs.alloc_at,"fill" )) + goto out; +ASSERT_EQ (map_memlock (fd), base + nr * ps,"full" ); + + skel->data->page_cnt = nr +1 ; +if (run (skel->progs.free_pages,"free_oversized" )) + goto out; + +/* +* A release of the last chunk alone goes through. Once it has run, +* anything queued before it has run too. +*/ + skel->bss->ptr = (void *)((nr -1 ) * ps); + skel->data->page_cnt =1 ; +if (run (skel->progs.free_pages,"free_last" )) + goto out; +ASSERT_EQ (wait_alloc_at (skel),0 ,"last_released" ); +ASSERT_EQ (map_memlock (fd), base + nr * ps,"still_full" ); + skel->bss->ptr =NULL ; +ASSERT_EQ (run_ret (skel->progs.alloc_at,"alloc_at" ),1 ,"first_still_taken" ); + + skel->data->page_cnt = nr; +if (run (skel->progs.free_pages,"free_all" )) + goto out; +ASSERT_EQ (wait_memlock (fd, base), base,"released" ); +out: +typed_arena__destroy (skel); +} + +/* +* A chunk is released once: a second release of a chunk whose release is +* queued is refused, so that it cannot take away whatever claims the chunk +* after the first release has run. The repeated request is made while the +* worker is waiting out the grace period of the first, which puts it in a +* later batch; a request that arrives before the worker starts joins the +* first batch instead and is harmless either way. +*/ +static void test_release_once (void ) +{ +struct typed_arena *skel; +void *p1, *p2; +long base; +int fd; + + skel =typed_arena__open_and_load (); +if (!ASSERT_OK_PTR (skel,"open_load" )) +return ; + fd =bpf_map__fd (skel->maps.arena); + base =map_memlock (fd); + +if (run (skel->progs.alloc,"alloc1" )) + goto out; + p1 = skel->bss->ptr; +if (run (skel->progs.alloc,"alloc2" )) + goto out; + p2 = skel->bss->ptr; + + skel->bss->ptr = p1; +if (run (skel->progs.free_pages,"free1" )) + goto out; +usleep (1000 ); +if (run (skel->progs.free_pages,"free1_again" )) + goto out; + skel->bss->ptr = p2; +if (run (skel->progs.free_pages,"free2" )) + goto out; + +/* Claim the first chunk again as soon as its release has run, and write to it. */ + skel->bss->ptr = p1; +ASSERT_EQ (wait_alloc_at (skel),0 ,"reclaimed" ); + skel->bss->value =42 ; +if (run (skel->progs.write_value,"write_value" )) + goto out; + +/* +* The second chunk's release was queued after the repeated one. Once it +* has run, so has the repeated one, if it was accepted. +*/ + skel->bss->ptr = p2; +ASSERT_EQ (wait_alloc_at (skel),0 ,"marker_released" ); + + skel->bss->ptr = p1; + skel->bss->value =0 ; +if (run (skel->progs.read_value,"read_value" )) + goto out; +ASSERT_EQ (skel->bss->value,42 ,"kept" ); + +if (run (skel->progs.free_pages,"free1_final" )) + goto out; + skel->bss->ptr = p2; +if (run (skel->progs.free_pages,"free2_final" )) + goto out; +ASSERT_EQ (wait_memlock (fd, base), base,"released" ); +out: +typed_arena__destroy (skel); +} + +/* A request larger than one batch of the allocator is served whole. */ +static void test_batch_alloc (void ) +{ +long ps =PAGE_SIZE , base, nr =WIDE_ARENA_SIZE /PAGE_SIZE ; +struct typed_arena *skel; +void *p; +int fd; + + skel =typed_arena__open_and_load (); +if (!ASSERT_OK_PTR (skel,"open_load" )) +return ; + fd =bpf_map__fd (skel->maps.arena); + base =map_memlock (fd); + + skel->data->page_cnt = nr; +if (run (skel->progs.wide_alloc,"wide_alloc" )) + goto out; +ASSERT_EQ (skel->data->page_cnt, nr,"granted" ); + p = skel->bss->ptr; +ASSERT_EQ (map_memlock (fd), base + nr * ps,"after_alloc" ); + +/* The last page of the range is usable. */ + skel->bss->ptr = p + (nr -1 ) * ps; + skel->bss->value =42 ; +if (run (skel->progs.wide_touch,"wide_touch" )) + goto out; + + skel->bss->ptr = p; +if (run (skel->progs.wide_free,"wide_free" )) + goto out; +ASSERT_EQ (wait_memlock (fd, base), base,"released" ); +out: +typed_arena__destroy (skel); +} + +/* An object of more than a page is backed and released as one chunk. */ +static void test_multipage (void ) +{ +long ps =PAGE_SIZE , base, granted; +struct typed_arena *skel; +int fd; + + granted =BIG_OBJ_SIZE > ps ?BIG_OBJ_SIZE / ps :1 ; + + skel =typed_arena__open_and_load (); +if (!ASSERT_OK_PTR (skel,"open_load" )) +return ; + fd =bpf_map__fd (skel->maps.arena); + base =map_memlock (fd); + +/* One page requested, the whole object granted. */ + skel->data->page_cnt =1 ; +if (run (skel->progs.big_alloc,"big_alloc" )) + goto out; +ASSERT_EQ (skel->data->page_cnt, granted,"granted" ); +ASSERT_EQ (map_memlock (fd), base + granted * ps,"after_alloc" ); + + skel->bss->value =42 ; +if (run (skel->progs.big_touch,"big_touch" )) + goto out; + +if (run (skel->progs.big_free,"big_free" )) + goto out; +ASSERT_EQ (wait_memlock (fd, base), base,"released" ); +out: +typed_arena__destroy (skel); +} + +/* +* A list linked through typed pointer fields is walked with no cast, read +* and written, and its NULL end dereferences object 0 of the slice. +*/ +static void test_fields (void ) +{ +struct typed_arena *skel; + + skel =typed_arena__open_and_load (); +if (!ASSERT_OK_PTR (skel,"open_load" )) +return ; +if (run (skel->progs.list_build,"list_build" )) + goto out; +if (run (skel->progs.list_sum_values,"list_sum" )) + goto out; +ASSERT_EQ (skel->bss->list_sum,LIST_LEN * (LIST_LEN +1 ) /2 ,"sum" ); +if (run (skel->progs.list_bump_values,"list_bump" )) + goto out; +if (run (skel->progs.list_sum_values,"list_sum_bumped" )) + goto out; +ASSERT_EQ (skel->bss->list_sum,LIST_LEN * (LIST_LEN +1 ) /2 +LIST_LEN ,"sum_bumped" ); + + skel->bss->value =4242 ; +if (run (skel->progs.list_deref_end,"list_deref_end" )) + goto out; +ASSERT_EQ (skel->bss->list_sum,4242 ,"null_is_object_zero" ); +out: +typed_arena__destroy (skel); +} + +void test_typed_arena (void ) +{ +struct typed_arena *skel; +bool supported; + +/* The programs need a compiler that emits the cast. */ + skel =typed_arena__open (); +if (!ASSERT_OK_PTR (skel,"open" )) +return ; + supported = skel->rodata->typed_arena_supported; +typed_arena__destroy (skel); +if (!supported) { +test__skip (); +return ; + } + +if (test__start_subtest ("pages" )) +test_pages (); +if (test__start_subtest ("identity" )) +test_identity (); +if (test__start_subtest ("release_spans" )) +test_release_spans (); +if (test__start_subtest ("release_bounds" )) +test_release_bounds (); +if (test__start_subtest ("release_once" )) +test_release_once (); +if (test__start_subtest ("batch_alloc" )) +test_batch_alloc (); +if (test__start_subtest ("multipage" )) +test_multipage (); +if (test__start_subtest ("fields" )) +test_fields (); +}
tools/testing/selftests/bpf/progs/typed_arena.c new file +354 -0
354 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [RFC PATCH bpf-next v1 01/16] mm/vmalloc: Add get_vm_area_align()
In reply to Kumar Kartikeya DwivediThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] mm: get_vm_area_align() silently ignores align if VM_IOREMAP is passed --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] mm: get_vm_area_align() silently ignores align if VM_IOREMAP is passed --
commit 60a153047292d8f40c9a75614c11c4a932d74c1f
Author: Kumar Kartikeya Dwivedi <[email protected]>
mm/vmalloc: Add get_vm_area_align()
This commit introduces get_vm_area_align(), a new variant of get_vm_area() that allows callers to specify a custom alignment for the start of the virtual memory area. This avoids wasting virtual address space when naturally aligned regions are required, such as for BPF arena maps.
Quoted 4 lines
mm/vmalloc.c +0
-0
[ ... ]
mm/vmalloc.c struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, · line 3308
Quoted 9 lines
mm/vmalloc.c +5
-0@@ -3301,6 +3301,25 @@ struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, NUMA_NO_NODE, GFP_KERNEL, caller); } +/** +* get_vm_area_align - reserve a contiguous kernel virtual area at an aligned start +* @size: size of the area +* @align: alignment of the start of the area, a power of two +* @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
[Severity: Medium]
Does this API work as intended if a caller passes VM_IOREMAP?
The documentation explicitly invites the caller to pass VM_IOREMAP, but __get_vm_area_node() unconditionally overwrites the requested alignment when this flag is set:
mm/vmalloc.c:__get_vm_area_node() {
...
if (flags & VM_IOREMAP)
align = 1ul << clamp_t(int, get_count_order_long(size),
PAGE_SHIFT, IOREMAP_MAX_ORDER);
...
}Will this establish a broken contract where the caller's explicitly requested alignment is silently ignored and overridden based on the size of the area?
Quoted 14 lines
mm/vmalloc.c +14
-0+* +* Like get_vm_area(), with the start of the area aligned to @align. +* +* Return: the area descriptor on success or %NULL on failure. +*/ +struct vm_struct *get_vm_area_align (unsigned long size,unsigned long align, +unsigned long flags) +{ +return __get_vm_area_node (size, align,PAGE_SHIFT , flags, +VMALLOC_START ,VMALLOC_END , +NUMA_NO_NODE ,GFP_KERNEL , +__builtin_return_address (0 )); +} +
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [RFC PATCH bpf-next v1 12/16] selftests/bpf: Build BPF objects with compiler-inserted typed arena casts
In reply to Kumar Kartikeya DwivediThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] bpf: missing typed-arena feature flag propagation to libarena sub-make --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] bpf: missing typed-arena feature flag propagation to libarena sub-make --
commit b75aa4c5a8315082b286ae6d14b710be82785867
Author: Kumar Kartikeya Dwivedi <[email protected]>
selftests/bpf: Build BPF objects with compiler-inserted typed arena casts
This commit probes clang for typed arena cast support and defines CLANG_TYPED_ARENA if available. It then appends this new flag to BPF_CC_FLAGS when building test_progs and its various flavors.
Quoted 4 lines
tools/testing/selftests/bpf/Makefile +0
-0
[ ... ]
tools/testing/selftests/bpf/Makefile BINARY := test_progs · line 520
Quoted 6 lines
tools/testing/selftests/bpf/Makefile +1
-1@@ -510,7 +517,7 @@ BINARY := test_progs BPF_CC := $(CLANG) BPF_CC_MSG := CLNG-BPF BPF_SYS_INCLUDES := $(CLANG_SYS_INCLUDES) -BPF_CC_FLAGS := -O2 $(BPF_TARGET_ENDIAN) -mcpu=v3 +BPF_CC_FLAGS := -O2 $(BPF_TARGET_ENDIAN) -mcpu=v3 $(CLANG_TYPED_ARENA)
[Severity: Low]
Should the newly introduced CLANG_TYPED_ARENA flag also be propagated to the libarena sub-make?
When building libarena, BPF compilation flags are explicitly passed down without the new compiler feature flag:
LIBARENA_MAKE_ARGS = \
...
CLANG="$(CLANG)" \ BPF_CFLAGS="$(BPF_CFLAGS) $(CLANG_CFLAGS)" \
...
While there are currently no typed arena tests within libarena, omitting this flag means any future typed arena programs added to libarena might fail to compile or get unexpectedly skipped. Does it make sense to include it here for consistency?
Quoted 2 lines
tools/testing/selftests/bpf/Makefile +0
-0BPF_DEFINES := -DENABLE_ATOMICS_TESTS include Makefile.skel
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
[email protected]
AI reviewRe: [RFC PATCH bpf-next v1 15/16] selftests/bpf: Test typed arena page allocation and release
In reply to Kumar Kartikeya DwivediThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] bpf: physical pages freed before TLB flush in typed_arena_alloc_pages() - [High] bpf…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] bpf: physical pages freed before TLB flush in typed_arena_alloc_pages()
- [High] bpf: bpf_typed_arena_free_pages() frees adjacent chunks due to unaligned addr --
commit 45f79d175e13ad1f2540fd16c264cce33991c839
Author: Kumar Kartikeya Dwivedi <[email protected]>
selftests/bpf: Test typed arena page allocation and release
Allocate a page of typed arena objects, write one, reach it again through an opaque copy of its address and its neighbour by arithmetic on that copy, and check that the kfunc receives the registered typed arena in place of the type ID. Take the chunk a value names and see the same chunk refused twice, once for the chunk and once for a page inside it. Ask for one page of an object that spans several and see the request rounded up to the object's chunk, with the granted count written back. Release a chunk and see it keep its objects and stay taken within the same invocation, since the release is deferred behind a grace period.
Reject a use of the returned pointer without a NULL check, a struct without special fields, and a map that is not the program's arena. Declare the two kfuncs in bpf_experimental.h.
Quoted 13 lines
tools/testing/selftests/bpf/progs/verifier_typed_arena.c +5
-0@@ -1157,6 +1157,148 @@ int ptr_field_sanitize_on_one_path(void *ctx)return 0 ; } +#define TYPE_ID (T ) bpf_core_type_id_local(T) + +SEC("syscall") +__description("allocated pages hold real objects, reachable through any value that lands in them") +__success __retval(0)
tools/testing/selftests/bpf/progs/verifier_typed_arena.c int ptr_field_sanitize_on_one_path(void *ctx) · line 1174
Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_typed_arena.c +10
-0+__xlated("r2 = 0x{{[0-9a-f]+[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]}}") +__xlated("call kernel-function") +int pages_alloc (void *ctx) +{ +struct typed_obj *obj, *again, *next; +void *opaque; +__u32 cnt =1 ; + + obj = bpf_typed_arena_alloc_pages(&arena, TYPE_ID(struct typed_obj), NULL, &cnt, +NUMA_NO_NODE );
[Severity: Critical]
Is it possible for physical pages to be freed before the TLB flush occurs?
Looking at typed_arena_alloc_pages() in kernel/bpf/arena.c, when an allocation fails midway:
kernel/bpf/arena.c:typed_arena_alloc_pages() {
...
if (data.i)
apply_to_existing_page_range(&init_mm, data.start,
(unsigned long)data.i << PAGE_SHIFT,
apply_range_clear_typed_cb, &cdata);
free_pages_nolock(head, order);
goto back_out;
...
}If a concurrent page fault occurs, can this result in the physical pages being returned to the allocator before the TLB flush in the back_out path?
Quoted 2 lines
tools/testing/selftests/bpf/progs/verifier_typed_arena.c +2
-0+if (!obj) +return 1 ;
[ ... ]
Quoted 4 lines
tools/testing/selftests/bpf/progs/verifier_typed_arena.c +4
-0+SEC("syscall") +__description("a released chunk keeps its objects and stays taken until the grace period has passed") +__success __retval(0) +int pages_free(void *ctx)
tools/testing/selftests/bpf/progs/verifier_typed_arena.c int ptr_field_sanitize_on_one_path(void *ctx) · line 1253
Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_typed_arena.c +10
-0+{ +struct typed_obj *obj; +__u32 cnt =1 ; + + obj = bpf_typed_arena_alloc_pages(&arena, TYPE_ID(struct typed_obj), NULL, &cnt, +NUMA_NO_NODE ); +if (!obj) +return 1 ; + obj->value =7 ; + bpf_typed_arena_free_pages(&arena, TYPE_ID(struct typed_obj), obj, 1);
[Severity: High]
Does this code handle unaligned offset values correctly?
Looking at typed_arena_free_pages() in kernel/bpf/arena.c, when an unaligned addr is passed, the calculation for the last chunk might spill over into the next chunk:
kernel/bpf/arena.c:typed_arena_free_pages() {
off = addr - base;
if (page_cnt > (size - off) >> PAGE_SHIFT)
return;
first = off >> ta->chunk_shift;
last = (off + ((unsigned long)page_cnt << PAGE_SHIFT) - 1) >> ta->chunk_shift;
...
}Can this lead to unintentional freeing of adjacent chunks?
Quoted 7 lines
tools/testing/selftests/bpf/progs/verifier_typed_arena.c +7
-0+if (obj->value !=7 ) +return 2 ; + cnt =1 ; + if (bpf_typed_arena_alloc_pages(&arena, TYPE_ID(struct typed_obj), obj, &cnt, NUMA_NO_NODE)) +return 3 ; +return 0 ; +}
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
[email protected]
AI reviewRe: [RFC PATCH bpf-next v1 10/16] bpf: Let typed_arena_cast copy pointers the verifier already trusts
In reply to Kumar Kartikeya DwivediThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] bpf: check_typed_arena_cast() misses src->off check causing state mismatch --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf: check_typed_arena_cast() misses src->off check causing state mismatch --
commit e8c7f600f2244019888ad241fa81b63bc3bccf3d
Author: Kumar Kartikeya Dwivedi <[email protected]>
bpf: Let typed_arena_cast copy pointers the verifier already trusts
The compiler inserts the cast at every use of a pointer to a struct with special fields, so that programs never write one, and it cannot tell what the pointer is: the same struct lives in typed arenas, in allocated objects, in map values and on the stack. So the verifier decides per path what the cast takes.
Quoted 4 lines
kernel/bpf/verifier.c +0
-0
[ ... ]
kernel/bpf/verifier.c static int check_typed_arena_cast(struct bpf_verifier_env *env, struct bpf_insn · line 17362
Quoted 6 lines
kernel/bpf/verifier.c +2
-0@@ -17320,6 +17358,18 @@ static int check_typed_arena_cast(struct bpf_verifier_env *env, struct bpf_insnreturn PTR_ERR (ta); aux->typed_arena = ta; +if (src->type == (PTR_TO_BTF_ID |MEM_ARENA ) && src->btf_id == ta->btf_id && +tnum_is_const (src->var_off) && !src->var_off.value) {
[Severity: High]
Does check_typed_arena_cast() need to ensure src->off == 0 here?
If a BPF program uses typed_arena_cast on a control flow path where one branch provides a SCALAR_VALUE (setting aux->sanitize_needed = true) and the other branch provides a PTR_TO_BTF_ID | MEM_ARENA with a non-zero fixed offset (src->off > 0), the runtime sanitization sequence drops the offset by masking it to 0, but the verifier retains the non-zero offset.
Could this state mismatch allow a program to subtract the offset and access memory before the arena object, bypassing verifier bounds checks?
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ *dst = *src; +return 0 ; + } +if (aux->sanitize_plain) { +verbose (env,"insn %d casts values that need different treatment on different paths\n" , + env->insn_idx); +return -EINVAL ; + } + aux->sanitize_needed = true; +
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
[email protected]
AI reviewRe: [RFC PATCH bpf-next v1 16/16] selftests/bpf: Exercise typed arenas at run time
In reply to Kumar Kartikeya DwivediThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] selftests/bpf: test_release_bounds unconditionally fails due to logic error in alloc_at…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] selftests/bpf: test_release_bounds unconditionally fails due to logic error in alloc_at expectation --
commit a5c65698dd674c11891a999b72347849ae68ce84
Author: Kumar Kartikeya Dwivedi <[email protected]>
selftests/bpf: Exercise typed arenas at run time
Drive typed arenas from user space through a skeleton, testing map memory usage, chunk allocation, release, multipage allocations, bounds checking, and pointer fields.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/typed_arena.c new file +0
-0
[ ... ]
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/typed_arena.c new file +7
-0+static void test_release_bounds (void ) +{ +long ps =PAGE_SIZE , base, nr =OBJ_ARENA_SIZE /PAGE_SIZE ; +struct typed_arena *skel; +int fd; + + skel =typed_arena__open_and_load ();
tools/testing/selftests/bpf/prog_tests/typed_arena.c line 263
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/typed_arena.c new file +10
-0+if (!ASSERT_OK_PTR (skel,"open_load" )) +return ; + fd =bpf_map__fd (skel->maps.arena); + base =map_memlock (fd); + +/* Fill the typed arena, so that a scan of its bitmap for a free chunk runs to the end. */ + skel->bss->ptr =NULL ; + skel->data->page_cnt = nr; +if (run (skel->progs.alloc_at,"fill" )) + goto out;
[Severity: Medium]
Will this unconditionally abort the test?
When skel->bss->ptr is set to NULL in test_release_bounds(), the BPF program alloc_at() executes with its hint variable as NULL. If the allocation is successful, o will be non-NULL, which makes the condition o != hint evaluate to 1 (true):
tools/testing/selftests/bpf/progs/typed_arena.c:alloc_at() {
...
hint = ptr;
o = bpf_typed_arena_alloc_pages(&arena, TYPE_ID(struct obj), hint, &page_cnt, NUMA_NO_NODE);
if (!o)
return 1;
return o != hint;
}The run() helper wraps ASSERT_OK(ret), which expects a return value of 0. Since it receives 1, the assertion fails and the test executes goto out, leaving the rest of test_release_bounds() unexecuted.
Could the return logic in alloc_at() be adjusted when hint is NULL, or should this test use a different program or helper to fill the arena?
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/typed_arena.c new file +5
-0+ASSERT_EQ (map_memlock (fd), base + nr * ps,"full" ); + + skel->data->page_cnt = nr +1 ; +if (run (skel->progs.free_pages,"free_oversized" )) + goto out;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
[email protected]
AI reviewRe: [RFC PATCH bpf-next v1 09/16] bpf: Add typed arena page allocation and release kfuncs
In reply to Kumar Kartikeya DwivediThank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider: - [Critical] bpf: stale TLB use-after-free window in allocation error path - [High] bpf: uninitialized…
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
- [Critical] bpf: stale TLB use-after-free window in allocation error path
- [High] bpf: uninitialized kernel memory leak via arena page allocation
- [High] bpf: TLB shootdown race due to premature state publication
- [High] bpf: TOCTOU vulnerability via double fetch of user-provided page_cnt
- [High] bpf: state machine corruption race between worker and page fault handler
- [Medium] bpf: memory leak when resilient spinlock acquisition fails
- [Medium] bpf: incorrect memcg charging in tracking struct allocation
--
commit fcd33d7dbae3719eae8ea173b7e1a7f8cbe911c9
Author: Kumar Kartikeya Dwivedi <[email protected]>
bpf: Add typed arena page allocation and release kfuncs
This patch introduces typed arenas for BPF programs, allowing them to allocate and release contiguous memory regions mapped as real objects rather than scratch pages. It exposes two new kfuncs, bpf_typed_arena_alloc_pages() and bpf_typed_arena_free_pages(), which interface with a lock-free page allocator and defer the release via RCU workers.
kernel/bpf/arena.c +0 -0
@@ -686,6 +1045,8 @@ static unsigned long typed_arena_alloc_pages(struct bpf_typed_arena *ta, unsigne u32 *page_cnt, int node_id) {
[ ... ]
Review
Quoted 6 lines
(quoted diff) +6
-0+ if (node_id != NUMA_NO_NODE && + ((unsigned int)node_id >= nr_node_ids || !node_online(node_id))) + return 0; + if (!*page_cnt) + return 0; + chunk_cnt = DIV_ROUND_UP((unsigned long)*page_cnt, typed_arena_chunk_pages(ta));
[Severity: High]
Does this code introduce a TOCTOU vulnerability? Because page_cnt is a pointer to memory that can be shared with BPF programs or userspace, it could be modified concurrently. If the value is changed to zero between the if (!*page_cnt) check and the DIV_ROUND_UP() calculation, chunk_cnt will evaluate to 0. This would cause bitmap_find_next_zero_area() to return 0 and grant the BPF program a valid pointer to the arena's base address without actually allocating any pages, potentially leading to memory corruption.
[ ... ]
Review
Quoted 4 lines
(quoted diff) +4
-0+ data.arena = arena; + for (done = 0; done < chunk_cnt; done++) { + head = alloc_pages_nolock(__GFP_ACCOUNT, node_id, order); + if (!head) {
[Severity: High]
Does this allocation leak uninitialized buddy allocator memory? The call to alloc_pages_nolock() omits the __GFP_ZERO flag. Since arena memory is exposed to BPF programs (and potentially mapped to userspace), untrusted code could read stale kernel data. Furthermore, during teardown, the kernel might invoke bpf_obj_free_fields() on this uninitialized garbage, which could lead to panics if random memory is treated as valid kptrs.
Quoted 9 lines
(quoted diff) +9
-0+ ret = -ENOMEM; + goto back_out; + } + data.head = head; + data.start = start + (done << ta->chunk_shift); + data.i = 0; + ret = apply_to_page_range(&init_mm, data.start, chunk, apply_range_set_typed_cb, + &data); + if (ret) {
Review
Quoted 10 lines
(quoted diff) +10
-0+ /* The fault path took this chunk: give back the allocation whole. */ + cdata.arena = arena; + cdata.ta = ta; + if (data.i) + apply_to_existing_page_range(&init_mm, data.start, + (unsigned long)data.i << PAGE_SHIFT, + apply_range_clear_typed_cb, &cdata); + free_pages_nolock(head, order); + goto back_out; + }
[Severity: Critical]
Does this code clear PTEs and free physical pages before flushing the TLB? When apply_to_page_range() returns -EBUSY, the partially installed PTEs are cleared and the pages are immediately freed to the buddy allocator via free_pages_nolock(). The actual TLB flush happens later at the back_out label. This creates a window where freed pages can be reallocated elsewhere in the kernel, while stale, writable VMA translations remain cached in the TLB, allowing arbitrary memory overwrite.
[ ... ]
Quoted 10 lines
(quoted diff) +10
-0+back_out: + /* + * Back out the chunks installed before the one that failed. On -EBUSY + * that chunk is the one the fault path took, and it keeps its mark. + */ + init_llist_head(&freed); + cdata.arena = arena; + cdata.ta = ta; + for (i = 0; i < chunk_cnt; i++) + if (ret != -EBUSY || i != done)
Review
Quoted 10 lines
(quoted diff) +10
-0+ clear_bit(coff + i, ta->chunks); + for (i = 0; i < done; i++) { + cdata.head = NULL; + apply_to_existing_page_range(&init_mm, start + (i << ta->chunk_shift), chunk, + apply_range_clear_typed_cb, &cdata); + if (cdata.head) + __llist_add(&cdata.head->pcp_llist, &freed); + } + raw_res_spin_unlock_irqrestore(&arena->spinlock, flags); + flush_tlb_kernel_range(start, start + (chunk_cnt << ta->chunk_shift));
[Severity: High]
Does this sequence create a TLB shootdown race? By clearing the allocation state bits (ta->chunks) and dropping the spinlock before calling flush_tlb_kernel_range(), it creates a window where concurrent allocations can immediately claim the chunk and expose new mappings. Other CPUs might retain stale TLB translations to the old pages that are about to be freed, leading to silent data corruption or use-after-free conditions.
[ ... ]
Quoted 2 lines
(quoted diff) +2
-0+static void typed_arena_free_pages(struct bpf_typed_arena *ta, unsigned long addr, u32 page_cnt) +{
[ ... ]
Review
Quoted 4 lines
(quoted diff) +4
-0+ first = off >> ta->chunk_shift; + last = (off + ((unsigned long)page_cnt << PAGE_SHIFT) - 1) >> ta->chunk_shift; + + s = kmalloc_nolock(sizeof(*s), __GFP_ACCOUNT, NUMA_NO_NODE);
[Severity: Medium]
Does this structure allocation incorrectly charge the current context's memory cgroup? Because this is a generic kfunc that can be invoked from arbitrary execution contexts (like tracepoints or network packets), __GFP_ACCOUNT will charge whichever random user task happens to be interrupted. Shouldn't this be wrapped with bpf_map_memcg_enter() and bpf_map_memcg_exit() to charge the map's memcg?
Quoted 8 lines
(quoted diff) +8
-0+ if (!s) + /* + * The chunks stay allocated until the map is freed; nothing can + * be retried from here. + */ + return; + if (raw_res_spin_lock_irqsave(&arena->spinlock, flags)) + goto free_span;
[ ... ]
Review
Quoted 6 lines
(quoted diff) +6
-0+ irq_work_queue(&arena->typed_free_irq); + return; + +free_span: + kfree_nolock(s); +}
[Severity: Medium]
Does failing to acquire the resilient spinlock cause a silent memory leak? If raw_res_spin_lock_irqsave() fails (e.g., due to lock contention), the tracking struct is freed and the function returns without error. The requested chunks remain marked as taken indefinitely, but the BPF program assumes they were successfully queued for release, leaking arena chunks until map teardown.
[ ... ]
Quoted 2 lines
(quoted diff) +2
-0+static void typed_arena_free_worker(struct work_struct *work) +{
[ ... ]
Review
Quoted 11 lines
(quoted diff) +11
-0+ while (raw_res_spin_lock_irqsave(&arena->spinlock, flags)) + cpu_relax(); + for (i = 0; i < s->chunk_cnt; i++) { + cdata.head = NULL; + apply_to_existing_page_range(&init_mm, start + (i << ta->chunk_shift), + bpf_typed_arena_chunk(ta), + apply_range_clear_typed_cb, &cdata); + if (cdata.head) + __llist_add(&cdata.head->pcp_llist, &heads); + } + typed_arena_chunks_mark(ta, s->coff, s->chunk_cnt, false);
[Severity: High]
Does this create a destructive state machine race with the page fault handler? The worker clears the PTE mappings under the spinlock, but the page fault handler (typed_arena_handle_page_fault()) runs locklessly. If a page fault triggers on a cleared chunk just before typed_arena_chunks_mark(..., false) is called, the fault handler will install a scratch page and set the chunk bit. The worker will then blindly force the bit to 0. The chunk becomes marked as 'free' but contains a scratch page, causing all future allocations of this chunk to permanently fail with -EBUSY.
Review
Quoted 5 lines
(quoted diff) +5
-0+ bitmap_clear(ta->pending, s->coff, s->chunk_cnt); + raw_res_spin_unlock_irqrestore(&arena->spinlock, flags); + + flush_tlb_kernel_range(start, + start + ((unsigned long)s->chunk_cnt << ta->chunk_shift));
[Severity: High]
Can this sequence lead to a TLB shootdown race? Similar to the allocation error path, clearing the allocation state bits and dropping the spinlock before flushing the TLB allows concurrent allocations to claim the chunks while stale translations remain cached, introducing memory corruption risks.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
[email protected]
AI reviewRe: [RFC PATCH bpf-next v1 04/16] bpf: Add the typed_arena_cast instruction
In reply to Kumar Kartikeya DwivediThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] bpf: typed arenas leak on late verification failure - [Medium] bpf: diagnostic log state leak in…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] bpf: typed arenas leak on late verification failure
- [Medium] bpf: diagnostic log state leak in check_alu_op() on typed arena cast
- [Low] bpf: comment style violation in backtrack_insn()
--
commit 1039fff673a440b6fdcfc452a1630f899213bc0c
Author: Kumar Kartikeya Dwivedi <[email protected]>
bpf: Add the typed_arena_cast instruction
This commit adds the typed_arena_cast instruction, which turns an untrusted 64-bit value into a verifier-trusted pointer to a typed arena object. The verifier registers the typed arena on first sight, logs it, and lowers the instruction after verification.
Quoted 8 lines
kernel/bpf/backtrack.c +0
-0@@ -328,11 +328,13 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,if (sreg !=BPF_REG_FP )bt_set_reg (bt, sreg); }else {
kernel/bpf/backtrack.c static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx, · line 338
Quoted 10 lines
kernel/bpf/backtrack.c +4
-2-/* dreg = K +/* dreg = K, or dreg = typed_arena_cast(sreg, imm) * dreg needs precision after this insn. * Corresponding register is already marked * as precise=true in this verifier state. -* No further markings in parent are necessary +* No further markings in parent are necessary; +* a cast yields a pointer that is safe for any +* value of sreg, which needs no precision. */
^^^
[Severity: Low]
Does this multi-line comment violate the BPF subsystem comment formatting rules? The opening /* should typically be on its own line for multi-line comments in BPF subsystem code.
[ ... ]
Quoted 13 lines
kernel/bpf/core.c +2
-0@@ -3085,6 +3085,8 @@ static void bpf_prog_free_deferred(struct work_struct *work) aux = container_of(work, struct bpf_prog_aux, work);#ifdef CONFIG_BPF_SYSCALL bpf_free_kfunc_btf_tab (aux->kfunc_btf_tab); +/* The typed arenas outlive the program; only the load's failure retracts them. */ +kfree (aux->typed_arenas);#endif #ifdef CONFIG_CGROUP_BPF if (aux->cgroup_atype !=CGROUP_BPF_ATTACH_TYPE_INVALID )
[ ... ]
kernel/bpf/verifier.c static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) · line 17177
Quoted 11 lines
kernel/bpf/verifier.c +2
-1@@ -16993,7 +17173,9 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)struct bpf_reg_state *dst_reg = regs + insn->dst_reg;if (BPF_CLASS (insn->code) ==BPF_ALU64 ) { -if (insn->imm) { +if (insn->off ==BPF_TYPED_ARENA_CAST ) { +return check_typed_arena_cast (env, insn);
^^^^
[Severity: Medium]
Does this early return bypass the paired bpf_diag_mod_end(env) cleanup at the end of check_alu_op()? Leaving the diagnostic state active here fails to record the register modification in the diagnostic event log.
Quoted 4 lines
kernel/bpf/verifier.c +1
-0+ }else if (insn->imm) {/* off == BPF_ADDR_SPACE_CAST */ mark_reg_unknown (env, regs, insn->dst_reg);if (insn->imm ==1 )/* cast from as(1) to as(0) */
[ ... ]
Quoted 14 lines
kernel/bpf/verifier.c +10
-0@@ -20580,8 +20764,26 @@ static int resolve_func_ptrs(struct bpf_verifier_env *env) }/* drop refcnt of maps used by the rejected program */ +/* +* Drop the rejected program's typed arena references before its arena map +* reference. A typed arena nobody else registered is retracted; one that a +* loaded program registered lives on for the map's lifetime. +*/ +static void release_typed_arenas (struct bpf_verifier_env *env) +{ +struct bpf_prog_aux *aux = env->prog->aux; +u32 i; +
kernel/bpf/verifier.c static int resolve_func_ptrs(struct bpf_verifier_env *env) · line 20786
Quoted 10 lines
kernel/bpf/verifier.c +8
-0+for (i =0 ; i < aux->typed_arena_cnt; i++) +bpf_typed_arena_put (bpf_prog_arena (env->prog), aux->typed_arenas[i]); +kfree (aux->typed_arenas); + aux->typed_arenas =NULL ; + aux->typed_arena_cnt =0 ; +} +static void release_maps (struct bpf_verifier_env *env) { +release_typed_arenas (env);
^^^^
[Severity: High]
Can this leak the typed arena references?
In bpf_check(), if verification fails after env->prog->aux->used_maps is populated, the err_release_maps path bypasses calling release_maps(env):
err_release_maps:
if (ret) release_insn_arrays(env); if (!env->prog->aux->used_maps) release_maps(env);
If release_maps(env) is skipped, release_typed_arenas(env) will not be called. Then, bpf_prog_free_deferred() only does kfree(aux->typed_arenas) without dropping the actual bpf_typed_arena_put() refcounts, permanently leaking the typed arenas and their backing resources.
Quoted 3 lines
kernel/bpf/verifier.c +0
-0__bpf_free_used_maps (env->prog->aux, env->used_maps, env->used_map_cnt); }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewRe: [RFC PATCH bpf-next v1 03/16] bpf: Back typed arena chunks with scratch on demand
In reply to Kumar Kartikeya DwivediThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] bpf: Use-After-Free due to missing PTE cleanup in typed_arena_teardown_cb() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] bpf: Use-After-Free due to missing PTE cleanup in typed_arena_teardown_cb() --
commit 5ad8213f9e65607de0b6ea3647cc93fae67f349a
Author: Kumar Kartikeya Dwivedi <[email protected]>
bpf: Back typed arena chunks with scratch on demand
This patch introduces demand-paging for typed BPF arenas, where unbacked pages fault and are populated with scratch memory. It handles the fault recovery, marks chunks in a bitmap, and adds allocation and freeing mechanisms for these chunks during the map's lifecycle.
Quoted 4 lines
kernel/bpf/arena.c +0
-0
[ ... ]
kernel/bpf/arena.c static int typed_arena_teardown_cb(pte_t *ptep, unsigned long addr, void *data) · line 402
Quoted 9 lines
kernel/bpf/arena.c +1
-1@@ -371,7 +396,7 @@ static int typed_arena_teardown_cb(pte_t *ptep, unsigned long addr, void *data)if ((addr - (unsigned long )ta->base) & (bpf_typed_arena_chunk (ta) -1 ))return 0 ; -if (!pte_present (pte)) +if (!pte_present (pte) ||pte_page (pte) ==typed_arena_scratch_page (ta, addr))return 0 ;typed_arena_free_objects (ta, (void *)addr);__free_pages (pte_page (pte),typed_arena_chunk_order (ta));
[Severity: Critical]
Does this leave the kernel leaf PTEs pointing to freed memory?
This callback frees the physical pages for allocated chunks using __free_pages(), but it doesn't appear to clear the PTEs using pte_clear() or ptep_get_and_clear().
If a typed arena is destroyed (for example, if a BPF program load fails after allocation), bpf_typed_arena_put() calls typed_arena_free(), which then uses this callback. The underlying memory is freed, but the page tables remain populated with valid mappings.
Because typed_arena_find_slice() searches for and reuses freed address gaps, a subsequently loaded BPF program could allocate an arena of the same size, inherit the same virtual address slice, and access these stale PTEs to manipulate the freed memory.
kernel/bpf/arena.c static void typed_arena_free(struct bpf_arena *arena, struct bpf_typed_arena *ta · line 413
Quoted 7 lines
kernel/bpf/arena.c +3
-0@@ -383,6 +408,10 @@ static void typed_arena_free(struct bpf_arena *arena, struct bpf_typed_arena *taWRITE_ONCE (arena->typed_arena_mem, arena->typed_arena_mem -typed_arena_static_mem (ta));apply_to_existing_page_range (&init_mm, (unsigned long )ta->base,bpf_typed_arena_size (ta), typed_arena_teardown_cb, ta); +/* Programs may have stored kptrs into the dummy objects. */ +typed_arena_free_objects (ta, ta->scratch); +vfree (ta->scratch);
[Severity: Critical]
Could this similarly leave stale mappings to freed scratch space?
The scratch pages are freed via vfree(), but the aliased PTEs within the arena's kernel VM area that point to these scratch pages are never cleared during the teardown process.
Quoted 4 lines
kernel/bpf/arena.c +1
-0+kfree (ta->scratch_pages);bitmap_free (ta->chunks);bitmap_free (ta->pending);btf_put (ta->btf);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [RFC PATCH bpf-next v1 06/16] bpf: Support special fields in typed arena objects
In reply to Kumar Kartikeya DwivediThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] bpf: bpf_kptr_xchg allows user-space to forge kernel pointers via typed arena memory - [High]…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] bpf: bpf_kptr_xchg allows user-space to forge kernel pointers via typed arena memory
- [High] bpf: bpf_kptr_xchg on arena pointers causes kernel panic on archs lacking inline support --
commit 2d0dbe5c6fd18e1e3096285d4ab48687f876b4ce
Author: Kumar Kartikeya Dwivedi <[email protected]>
bpf: Support special fields in typed arena objects
This commit allows bpf_kptr_xchg() to take a pointer to a kptr field of a typed arena object. It modifies the verifier to permit objects in arena memory to own references to kernel objects and program-allocated objects.
Quoted 4 lines
kernel/bpf/verifier.c +0
-0
[ ... ]
kernel/bpf/verifier.c static int process_kptr_func(struct bpf_verifier_env *env, int regno, · line 8067
Quoted 7 lines
kernel/bpf/verifier.c +1
-1@@ -8063,7 +8063,7 @@ static int process_kptr_func(struct bpf_verifier_env *env, int regno,struct btf_record *rec;u32 kptr_off; -if (type_is_ptr_alloc_obj (reg->type)) { +if (type_is_ptr_alloc_obj (reg->type) ||type_is_typed_arena_obj (reg->type)) { rec =reg_btf_record (reg);
[Severity: Critical]
Does this allow user-space to forge kernel pointers?
Because arena memory (BPF_MAP_TYPE_ARENA) is inherently shared between the kernel and user-space, it can be mmapped with PROT_READ and PROT_WRITE. Furthermore, arena maps lack a map-level btf_record (it is NULL), which bypasses the standard check in bpf_map_mmap() that forbids mmapping maps containing special fields.
Could a malicious user-space process mmap the arena, write an arbitrary 64-bit value to the offset of the kptr field, and then have the BPF program call bpf_kptr_xchg() to read this user-supplied value? The verifier would unconditionally track the returned value as a trusted kernel pointer.
Could this forged pointer then be dereferenced or passed to bpf_obj_drop(), leading to memory corruption or KASLR bypass?
Quoted 2 lines
kernel/bpf/verifier.c +0
-0}else {/* PTR_TO_MAP_VALUE */ map_ptr = reg->map_ptr;
[ ... ]
kernel/bpf/verifier.c static const struct bpf_reg_types kptr_xchg_dest_types = { · line 8837
Quoted 5 lines
kernel/bpf/verifier.c +1
-0@@ -8834,6 +8834,7 @@ static const struct bpf_reg_types kptr_xchg_dest_types = {PTR_TO_BTF_ID |MEM_ALLOC ,PTR_TO_BTF_ID |MEM_ALLOC |NON_OWN_REF ,PTR_TO_BTF_ID |MEM_ALLOC |NON_OWN_REF |MEM_RCU , + PTR_TO_BTF_ID | MEM_ARENA,
[Severity: High]
Could this cause a kernel panic on architectures that lack inline support for ptr_xchg?
On architectures that support arenas but do not override bpf_jit_supports_ptr_xchg() to return true (e.g., PowerPC and s390x), the JIT will fall back to calling the bpf_kptr_xchg() C kernel helper.
The bpf_kptr_xchg() helper uses the raw xchg() macro on the provided pointer, which resides in demand-paged arena memory:
kernel/bpf/helpers.c:bpf_kptr_xchg() {
...
return xchg(kptr, (unsigned long)ptr);
}If the accessed arena page is unmapped, a kernel page fault occurs. Because bpf_arena_handle_page_fault() is only wired up in the architecture-specific fault handlers for arm64 and x86, it is completely ignored on PowerPC and s390x.
Since the xchg() macro lacks an exception table entry inside the helper, wouldn't an unmapped arena page result in an unhandled fault and a direct kernel panic?
Quoted 2 lines
kernel/bpf/verifier.c +0
-0} };
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
[email protected]
AI reviewRe: [RFC PATCH bpf-next v1 07/16] bpf: Trust typed pointer fields of typed arena objects
In reply to Kumar Kartikeya DwivediThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] bpf: refcount leak in bpf_typed_arena causes saturation and WARN --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] bpf: refcount leak in bpf_typed_arena causes saturation and WARN --
commit 824870737e5e6082cced029879731300754bd906
Author: Kumar Kartikeya Dwivedi <[email protected]>
Subject: bpf: Trust typed pointer fields of typed arena objects
This commit introduces logic to track and trust typed pointer fields within typed arena objects. A load from these fields returns an unsanitized pointer which is considered trustworthy without additional checks, and writes ensure only valid typed arena pointers or null are stored.
Quoted 3 lines
kernel/bpf/verifier.c +0
-0
[ ... ]
Quoted 1 line
kernel/bpf/verifier.c +0
-0@@ -6328,6 +6348,61 @@ static bool type_is_trusted_or_null(struct bpf_verifier_env *env,
[ ... ]
Quoted 13 lines
kernel/bpf/verifier.c +13
-0+static int check_typed_ptr_field_access (struct bpf_verifier_env *env,struct bpf_reg_state *regs, +struct bpf_reg_state *reg,const char *tname,u32 btf_id, +enum bpf_access_type atype,int value_regno) +{ +struct bpf_insn *insn = &env->prog->insnsi[env->insn_idx]; +u8 class =BPF_CLASS (insn->code); +struct bpf_reg_state *val; +struct bpf_typed_arena *ta; + +if (BPF_MODE (insn->code) !=BPF_MEM ||BPF_SIZE (insn->code) !=BPF_DW || + (class !=BPF_LDX && class !=BPF_STX && class !=BPF_ST )) { +verbose (env,"typed pointer field of struct %s must be accessed with a 64-bit load or store\n" , + tname);
kernel/bpf/verifier.c static bool type_is_trusted_or_null(struct bpf_verifier_env *env, · line 6381
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+return -EACCES ; + } +if (atype ==BPF_READ ) { +/* +* The pointer leads into the pointee's typed arena, which the +* program may never cast to or allocate from: register it here, +* as a cast would, so that the sanitization has a slice to mask +* into and the slice exists for the map's lifetime. +*/ + ta =typed_arena_register (env, btf_id);
[Severity: Medium]
Could this lead to a refcount leak for the typed arena when the program is unloaded?
When typed_arena_register() is called here, it acquires a reference to the typed arena via bpf_typed_arena_get() which increments ta->refcnt. However, the program unload path in bpf_prog_free_deferred() skips releasing the reference acquired during load:
kernel/bpf/core.c:bpf_prog_free_deferred() {
...
/* The typed arenas outlive the program; only the load's failure retracts them. */
kfree(aux->typed_arenas);
...
}Because the reference is never decremented after a successful load, the refcount will monotonically increase by one per load and unload cycle.
Since this path is reachable by any user capable of loading BPF programs, is it possible for a user to repeatedly load and unload a program to saturate the refcount and trigger a kernel WARN?
Quoted 5 lines
kernel/bpf/verifier.c +5
-0+if (IS_ERR (ta)) +return PTR_ERR (ta); +return mark_btf_ld_reg (env, regs, value_regno,PTR_TO_BTF_ID , reg->btf, btf_id, +MEM_ARENA |PTR_UNSANITIZED ); + }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7