rust: alloc: reject offline NUMA nodes in allocator
Subsystems (5)
PATCH 1/1 rust: alloc: reject offline NUMA nodes in allocator
NumaNode::new() only checks that the id is in [0, MAX_NUMNODES). Safe Allocator APIs can still forward an offline node into k[v]realloc_node_align(), which may dereference NODE_DATA(nid) == NULL and panic (GPF).
Reject offline nodes at the shared ReallocFunc boundary for nonzero allocations. Leave NUMA_NO_NODE and zero-sized free/realloc paths unchanged. A residual hotplug TOCTOU remains if a node goes offline after the check; this matches practical C usage for rejecting statically missing nodes from safe Rust.
--- rust/helpers/mm.c | 6 ++++++ rust/kernel/alloc/allocator.rs | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+)
rust/helpers/mm.c +6 -0
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/mm.h> +#include <linux/nodemask.h> #include <linux/sched/mm.h> __rust_helper void rust_helper_mmgrab(struct mm_struct *mm)
@@ -38,6 +39,11 @@ __rust_helper void rust_helper_mmap_read_unlock(struct mm_struct *mm)mmap_read_unlock (mm); } +__rust_helper bool rust_helper_node_online(int nid) +{ +return node_online (nid); +} + __rust_helper struct vm_area_struct *rust_helper_vma_lookup (struct mm_struct *mm,unsigned long addr) {
rust/kernel/alloc/allocator.rs +18 -0
@@ -101,6 +101,23 @@ unsafe fn call( nid: NumaNode, ) -> Result<NonNull<[u8]>, AllocError> {let size = layout.size (); + +// `NumaNode::new` validates the ID range, not whether the node is online. +// The C allocation paths require an online node for nonzero allocations. +// Keep `NUMA_NO_NODE` and zero-sized deallocations unchanged. +// +// Residual TOCTOU remains if a node goes offline after this check and +// before the allocator runs; that matches practical C usage for rejecting +// statically missing/offline nodes from safe Rust. +if size !=0 && nid !=NumaNode ::NO_NODE { +// SAFETY: The helper only reads the kernel's online-node mask. `nid` +// is either `NUMA_NO_NODE` (excluded above) or a value in +// `[0, MAX_NUMNODES)` from `NumaNode::new`. +if !unsafe { bindings::node_online (nid.0 ) } { +return Err (AllocError ); + } + } +let ptr =match ptr {Some (ptr) => {if old_layout.size () ==0 {
@@ -116,6 +133,7 @@ unsafe fn call(// - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc` and thus only requires that // `ptr` is NULL or valid. // - `ptr` is either NULL or valid by the safety requirements of this function. +// - For nonzero allocations with a specific node, `nid` was checked online above. // // GUARANTEE: // - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc`.
Signature · 1 line
2.55.0
Vlastimil Babka (SUSE)
reviewerQuoted 10 lines · Franklin-Qi
NumaNode::new()only checks that the id is in [0,MAX_NUMNODES). Safe Allocator APIs can still forward an offline node into k[v]realloc_node_align(), which may dereferenceNODE_DATA(nid)== NULL and panic (GPF).Reject offline nodes at the shared ReallocFunc boundary for nonzero allocations. Leave
NUMA_NO_NODEand zero-sized free/realloc paths unchanged. A residual hotplug TOCTOU remains if a node goes offline after the check; this matches practical C usage for rejecting statically missing nodes from safe Rust.
IIRC we however don't remove node data on offline, so the NULL dereference won't happen?
Quoted 71 lines
Fixes 7760b6421b6c ("rust: add support for NUMA ids in allocations")Reported-by Georgios Androutsopoulos <[email protected]>Signed-off-by Franklin-Qi <[email protected]>--- rust/helpers/mm.c | 6 ++++++ rust/kernel/alloc/allocator.rs | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+)rust/helpers/mm.c +6
-0@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/mm.h> +#include <linux/nodemask.h> #include <linux/sched/mm.h> __rust_helper void rust_helper_mmgrab(struct mm_struct *mm)@@ -38,6 +39,11 @@ __rust_helper void rust_helper_mmap_read_unlock(struct mm_struct *mm)mmap_read_unlock (mm); } +__rust_helper bool rust_helper_node_online(int nid) +{ +return node_online (nid); +} + __rust_helper struct vm_area_struct *rust_helper_vma_lookup (struct mm_struct *mm,unsigned long addr) {rust/kernel/alloc/allocator.rs +18
-0@@ -101,6 +101,23 @@ unsafe fn call( nid: NumaNode, ) -> Result<NonNull<[u8]>, AllocError> {let size = layout.size (); + +// `NumaNode::new` validates the ID range, not whether the node is online. +// The C allocation paths require an online node for nonzero allocations. +// Keep `NUMA_NO_NODE` and zero-sized deallocations unchanged. +// +// Residual TOCTOU remains if a node goes offline after this check and +// before the allocator runs; that matches practical C usage for rejecting +// statically missing/offline nodes from safe Rust. +if size !=0 && nid !=NumaNode ::NO_NODE { +// SAFETY: The helper only reads the kernel's online-node mask. `nid` +// is either `NUMA_NO_NODE` (excluded above) or a value in +// `[0, MAX_NUMNODES)` from `NumaNode::new`. +if !unsafe { bindings::node_online (nid.0 ) } { +return Err (AllocError ); + } + } +let ptr =match ptr {Some (ptr) => {if old_layout.size () ==0 {@@ -116,6 +133,7 @@ unsafe fn call(// - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc` and thus only requires that // `ptr` is NULL or valid. // - `ptr` is either NULL or valid by the safety requirements of this function. +// - For nonzero allocations with a specific node, `nid` was checked online above. // // GUARANTEE: // - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc`.