net: skb: isolate skb data area allocations into a separate bucket
Subsystems (3)
PATCH v3 1/1 net: skb: isolate skb data area allocations into a separate bucket
SKB data area allocations (as done from alloc_skb()) use kmalloc(). These allocations can be variably sized and their contents can be more or less controlled from userspace, which makes them useful for attackers that want to overwrite a use-after-free'd object from the same kmalloc slab (which often just requires the sizes to roughly match into the same kmalloc bucket). [0] is an easy example of an exploit that uses netlink skb allocation to target another similarly-sized accidentally freed object.
While other mitigations like CONFIG_RANDOM_KMALLOC_CACHES exist, these are probabilistic. Use the existing kmem buckets API to further isolate these allocations in a guaranteed fashion, when CONFIG_SLAB_BUCKETS=y.
---
v3:
- rebase on net-next and resend
net/core/skbuff.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
net/core/skbuff.c +6 -2
@@ -586,6 +586,8 @@ struct sk_buff *napi_build_skb(void *data, unsigned int frag_size) }EXPORT_SYMBOL (napi_build_skb); +static kmem_buckets *skb_data_buckets__ro_after_init ; +static void *kmalloc_pfmemalloc (size_t obj_size,gfp_t flags,int node) {if (!gfp_pfmemalloc_allowed (flags))
@@ -593,7 +595,8 @@ static void *kmalloc_pfmemalloc(size_t obj_size, gfp_t flags, int node)if (!obj_size)return kmem_cache_alloc_node (net_hotdata.skb_small_head_cache, flags, node); -return kmalloc_node_track_caller (obj_size, flags, node); +return kmem_buckets_alloc_node_track_caller (skb_data_buckets, obj_size, + flags, node); }/*
@@ -634,7 +637,7 @@ static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node,* Try a regular allocation, when that fails and we're not entitled * to the reserves, fail. */ - obj =kmalloc_node_track_caller (obj_size, + obj =kmem_buckets_alloc_node_track_caller (skb_data_buckets, obj_size, flags | __GFP_NOMEMALLOC | __GFP_NOWARN, node);if (likely (obj))
@@ -5215,6 +5218,7 @@ void __init skb_init(void)0 ,SKB_SMALL_HEAD_HEADROOM , NULL); + skb_data_buckets =kmem_buckets_create ("skb_data" ,SLAB_PANIC ,0 ,INT_MAX ,NULL );skb_extensions_init (); }
Signature · 1 line
2.54.0
Paolo Abeni
maintainerOn 7/2/26 7:07 PM, Pedro Falcato wrote:> @@ -586,6 +586,8 @@ struct sk_buff *napi_build_skb(void *data, unsigned int frag_size)
Quoted 9 lines
}
EXPORT_SYMBOL(napi_build_skb);net/core/skbuff.c +2
-0+static kmem_buckets *skb_data_buckets__ro_after_init ; +static void *kmalloc_pfmemalloc (size_t obj_size,gfp_t flags,int node) {if (!gfp_pfmemalloc_allowed (flags))
net/core/skbuff.c static void *kmalloc_pfmemalloc(size_t obj_size, gfp_t flags, int node) · line 599
Quoted 6 lines
net/core/skbuff.c +2
-1@@ -593,7 +595,8 @@ static void *kmalloc_pfmemalloc(size_t obj_size, gfp_t flags, int node)if (!obj_size)return kmem_cache_alloc_node (net_hotdata.skb_small_head_cache, flags, node); -return kmalloc_node_track_caller (obj_size, flags, node); +return kmem_buckets_alloc_node_track_caller (skb_data_buckets, obj_size, + flags, node);
Sashiko noted that some drivers may require GFP_DMA buckets, and the above may break them:
https://sashiko.dev/#/patchset/20260702170728.168755-1-pfalcato%40suse.de
net/core/skbuff.c static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node, · line 642
Quoted 11 lines
net/core/skbuff.c +1
-1}/* @@ -634,7 +637,7 @@ static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node,* Try a regular allocation, when that fails and we're not entitled * to the reserves, fail. */ - obj =kmalloc_node_track_caller (obj_size, + obj =kmem_buckets_alloc_node_track_caller (skb_data_buckets, obj_size, flags | __GFP_NOMEMALLOC | __GFP_NOWARN, node);
Minor nit: checkpatch laments WRT brackets alignment.
/P
Quoted 21 lines · Paolo Abeni
On 7/2/26 7:07 PM, Pedro Falcato wrote:> @@ -586,6 +586,8 @@ struct
sk_buff*napi_build_skb(void *data, unsigned intfrag_size)Quoted 9 lines
}
EXPORT_SYMBOL(napi_build_skb);net/core/skbuff.c +2
-0+static kmem_buckets *skb_data_buckets__ro_after_init ; +static void *kmalloc_pfmemalloc (size_t obj_size,gfp_t flags,int node) {if (!gfp_pfmemalloc_allowed (flags))net/core/skbuff.c static void *kmalloc_pfmemalloc(size_t obj_size, gfp_t flags, int node) · line 599
Quoted 6 lines
net/core/skbuff.c +2
-1@@ -593,7 +595,8 @@ static void *kmalloc_pfmemalloc(size_t obj_size, gfp_t flags, int node)if (!obj_size)return kmem_cache_alloc_node (net_hotdata.skb_small_head_cache, flags, node); -return kmalloc_node_track_caller (obj_size, flags, node); +return kmem_buckets_alloc_node_track_caller (skb_data_buckets, obj_size, + flags, node);Sashiko noted that some drivers may require
GFP_DMAbuckets, and the above may break them:
Oh, this is really awkward. Adding linux-mm and slab maintainers for input here.
Considering the current slab bucketing does not seem to duplicate DMA or CGROUP caches, could it make sense to duplicate those as well? Otherwise we could add a branch like:
if (gfp_flags & __GFP_DMA)
/* use the global dma kmalloc caches */
Quoted 14 lines
net/core/skbuff.c static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node, · line 642
Quoted 12 lines
net/core/skbuff.c +1
-1}/* @@ -634,7 +637,7 @@ static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node,* Try a regular allocation, when that fails and we're not entitled * to the reserves, fail. */ - obj =kmalloc_node_track_caller (obj_size, + obj =kmem_buckets_alloc_node_track_caller (skb_data_buckets, obj_size, flags | __GFP_NOMEMALLOC | __GFP_NOWARN, node);Minor nit: checkpatch laments WRT brackets alignment.
Will fix, thanks.
Signature · 1 line
Pedro
Quoted 22 lines · Pedro Falcato
Quoted 21 lines · Paolo Abeni
On 7/2/26 7:07 PM, Pedro Falcato wrote:> @@ -586,6 +586,8 @@ struct
sk_buff*napi_build_skb(void *data, unsigned intfrag_size)Quoted 9 lines
}
EXPORT_SYMBOL(napi_build_skb);net/core/skbuff.c +2
-0+static kmem_buckets *skb_data_buckets__ro_after_init ; +static void *kmalloc_pfmemalloc (size_t obj_size,gfp_t flags,int node) {if (!gfp_pfmemalloc_allowed (flags))net/core/skbuff.c static void *kmalloc_pfmemalloc(size_t obj_size, gfp_t flags, int node) · line 599
Quoted 6 lines
net/core/skbuff.c +2
-1@@ -593,7 +595,8 @@ static void *kmalloc_pfmemalloc(size_t obj_size, gfp_t flags, int node)if (!obj_size)return kmem_cache_alloc_node (net_hotdata.skb_small_head_cache, flags, node); -return kmalloc_node_track_caller (obj_size, flags, node); +return kmem_buckets_alloc_node_track_caller (skb_data_buckets, obj_size, + flags, node);Sashiko noted that some drivers may require
GFP_DMAbuckets, and the above may break them:
Quoted 6 lines
Oh, this is really awkward. Adding linux-mm and slab maintainers for input here.
Considering the current slab bucketing does not seem to duplicate DMA or CGROUP caches, could it make sense to duplicate those as well?
Could we specify what kmalloc types the user needs when creating kmem_buckets and duplicate caches for the requested kmalloc types only?
Quoted 4 lines
Otherwise we could add a branch like:
if (
gfp_flags&__GFP_DMA)/* use the global dma kmalloc caches */
Signature · 2 lines
Cheers, Harry / Hyeonggon
Quoted 27 lines · Harry Yoo
Quoted 24 lines · Pedro Falcato
Quoted 22 lines · Paolo Abeni
On 7/2/26 7:07 PM, Pedro Falcato wrote:> @@ -586,6 +586,8 @@ struct
sk_buff*napi_build_skb(void *data, unsigned intfrag_size)Quoted 9 lines
}
EXPORT_SYMBOL(napi_build_skb);net/core/skbuff.c +2
-0+static kmem_buckets *skb_data_buckets__ro_after_init ; +static void *kmalloc_pfmemalloc (size_t obj_size,gfp_t flags,int node) {if (!gfp_pfmemalloc_allowed (flags))net/core/skbuff.c static void *kmalloc_pfmemalloc(size_t obj_size, gfp_t flags, int node) · line 599
Quoted 6 lines
net/core/skbuff.c +2
-1@@ -593,7 +595,8 @@ static void *kmalloc_pfmemalloc(size_t obj_size, gfp_t flags, int node)if (!obj_size)return kmem_cache_alloc_node (net_hotdata.skb_small_head_cache, flags, node); -return kmalloc_node_track_caller (obj_size, flags, node); +return kmem_buckets_alloc_node_track_caller (skb_data_buckets, obj_size, + flags, node);Sashiko noted that some drivers may require
GFP_DMAbuckets, and the above may break them:https://sashiko.dev/#/patchset/20260702170728.168755-1-pfalcato%40suse.de
Quoted 7 lines
Quoted 4 lines
Oh, this is really awkward. Adding linux-mm and slab maintainers for input here.
Considering the current slab bucketing does not seem to duplicate DMA or CGROUP caches, could it make sense to duplicate those as well?
Could we specify what kmalloc types the user needs when creating
kmem_bucketsand duplicate caches for the requested kmalloc types only?
Perhaps. But do the users themselves know? alloc_skb() allows users to specify random __GFP flags. We're bound to see some random caller do alloc_skb(__GFP_ACCOUNT) ;)
In all honesty, I'm not quite sure what the best way forward here is. The most transparent way is to bucket those other kmalloc types as well, but that might very trivially result in a lot more caches (and possibly memory usage) for no great reason. So perhaps specifying caches might do.
Signature · 1 line
Pedro
Quoted 31 lines · Pedro Falcato
Quoted 30 lines · Harry Yoo
Quoted 28 lines · Pedro Falcato
Quoted 21 lines · Paolo Abeni
On 7/2/26 7:07 PM, Pedro Falcato wrote:> @@ -586,6 +586,8 @@ struct
sk_buff*napi_build_skb(void *data, unsigned intfrag_size)Quoted 9 lines
}
EXPORT_SYMBOL(napi_build_skb);net/core/skbuff.c +2
-0+static kmem_buckets *skb_data_buckets__ro_after_init ; +static void *kmalloc_pfmemalloc (size_t obj_size,gfp_t flags,int node) {if (!gfp_pfmemalloc_allowed (flags))net/core/skbuff.c static void *kmalloc_pfmemalloc(size_t obj_size, gfp_t flags, int node) · line 599
Quoted 6 lines
net/core/skbuff.c +2
-1@@ -593,7 +595,8 @@ static void *kmalloc_pfmemalloc(size_t obj_size, gfp_t flags, int node)if (!obj_size)return kmem_cache_alloc_node (net_hotdata.skb_small_head_cache, flags, node); -return kmalloc_node_track_caller (obj_size, flags, node); +return kmem_buckets_alloc_node_track_caller (skb_data_buckets, obj_size, + flags, node);Sashiko noted that some drivers may require
GFP_DMAbuckets, and the above may break them:Oh, this is really awkward. Adding linux-mm and slab maintainers for input here.
Considering the current slab bucketing does not seem to duplicate DMA or CGROUP caches, could it make sense to duplicate those as well?
Quoted 6 lines
Quoted 2 lines
Could we specify what kmalloc types the user needs when creating
kmem_bucketsand duplicate caches for the requested kmalloc types only?Perhaps. But do the users themselves know?
alloc_skb()allows users to specify random__GFPflags. We're bound to see some random caller doalloc_skb(__GFP_ACCOUNT);)
Other users don't expose the buckets to drivers, so I thought only alloc_skb() would create the buckets for each kmalloc type.
Quoted 4 lines
In all honesty, I'm not quite sure what the best way forward here is. The most transparent way is to bucket those other kmalloc types as well, but that might very trivially result in a lot more caches (and possibly memory usage) for no great reason. So perhaps specifying caches might do.
Another direction could be merging those buckets.
If we want to protect kmalloc objects from user-controllable allocations, can we create buckets for each kmalloc type during the boot process and let the kmem_buckets users share them?
That doesn't sound like creating too many kmalloc caches, while providing a decent separation. We already have two buckets users, one w/ SLAB_ACCOUNT and the other w/o SLAB_ACCOUNT.
If you really want each bucket to have a separate set of caches, you have to sacrifice some memory for security :)
Signature · 2 lines
Cheers, Harry / Hyeonggon
Quoted 36 lines · Pedro Falcato
Quoted 27 lines · Harry Yoo
Quoted 24 lines · Pedro Falcato
Quoted 22 lines · Paolo Abeni
On 7/2/26 7:07 PM, Pedro Falcato wrote:> @@ -586,6 +586,8 @@ struct
sk_buff*napi_build_skb(void *data, unsigned intfrag_size)Quoted 9 lines
}
EXPORT_SYMBOL(napi_build_skb);net/core/skbuff.c +2
-0+static kmem_buckets *skb_data_buckets__ro_after_init ; +static void *kmalloc_pfmemalloc (size_t obj_size,gfp_t flags,int node) {if (!gfp_pfmemalloc_allowed (flags))net/core/skbuff.c static void *kmalloc_pfmemalloc(size_t obj_size, gfp_t flags, int node) · line 599
Quoted 6 lines
net/core/skbuff.c +2
-1@@ -593,7 +595,8 @@ static void *kmalloc_pfmemalloc(size_t obj_size, gfp_t flags, int node)if (!obj_size)return kmem_cache_alloc_node (net_hotdata.skb_small_head_cache, flags, node); -return kmalloc_node_track_caller (obj_size, flags, node); +return kmem_buckets_alloc_node_track_caller (skb_data_buckets, obj_size, + flags, node);Sashiko noted that some drivers may require
GFP_DMAbuckets, and the above may break them:https://sashiko.dev/#/patchset/20260702170728.168755-1-pfalcato%40suse.de
Quoted 7 lines
Quoted 4 lines
Oh, this is really awkward. Adding linux-mm and slab maintainers for input here.
Considering the current slab bucketing does not seem to duplicate DMA or CGROUP caches, could it make sense to duplicate those as well?
Could we specify what kmalloc types the user needs when creating
kmem_bucketsand duplicate caches for the requested kmalloc types only?
Quoted 8 lines
Perhaps. But do the users themselves know?
alloc_skb()allows users to specify random__GFPflags. We're bound to see some random caller doalloc_skb(__GFP_ACCOUNT);)In all honesty, I'm not quite sure what the best way forward here is. The most transparent way is to bucket those other kmalloc types as well, but that might very trivially result in a lot more caches (and possibly memory usage) for no great reason. So perhaps specifying caches might do.
How about dropping non-standard flag alloc requests into the general kmalloc buckets? (I.e. have kmem_buckets_alloc redirect on flag mismatch?)
-Kees
Signature · 1 line
Kees Cook