SELinux: Add check for the user data passed to kcalloc in hashtab_init
PATCH 1/1 SELinux: Add check for the user data passed to kcalloc in hashtab_init
When the user writes some data to the file /sys/fs/selinux/policy, there is no check for the user buf passed to kcalloc. Syzkaller shows
this warning:
WARNING: CPU: 1 PID: 6642 at mm/page_alloc.c
__alloc_pages_noprof___kmalloc_large_node__kmalloc_large_node_noprof__kmalloc_noprofhashtab_initcommon_readpolicydb_readsecurity_load_policysel_write_loadvfs_writeksys_writedo_syscall_64
This warning can be reproduced by writing this content to /sys/fs/selinux/policy8cff7cf9 08000000 5345204c 696e7578 15000000 e0ff962a 08000000 07000000 4cf523cd 7eec2688 6d70a6b7 c78b496f 1a0a192c ea34ff41 70581a74 3ff0cfb9 7ea0f0d1 70d1fe14 41c2f7c8 ea1c78dd 17a19249 35210081 a83c30ec 4171450b fc1de12c fe1ff342 a887
Add check to prevent the size passed to kcalloc larger than MAX_PAGE_ORDER after get_order.
--- security/selinux/ss/hashtab.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
security/selinux/ss/hashtab.c +18 -0
@@ -30,6 +30,21 @@ static u32 hashtab_compute_size(u32 nel)return nel ==0 ?0 :roundup_pow_of_two (nel); } +static bool is_order_out_of_range (u32 size,struct hashtab *h) +{ +size_t bytes; +u32 order; + +if (unlikely (check_mul_overflow (size,sizeof (*h->htable), &bytes))) +return true; + + order =get_order (bytes); +if (order >MAX_PAGE_ORDER ) +return true; + +return false; +} +int hashtab_init (struct hashtab *h,u32 nel_hint) {u32 size =hashtab_compute_size (nel_hint);
@@ -40,6 +55,9 @@ int hashtab_init(struct hashtab *h, u32 nel_hint) h->htable =NULL ;if (size) { +if (is_order_out_of_range (size, h)) +return -ENOMEM ; + h->htable =kcalloc (size,sizeof (*h->htable),GFP_KERNEL );if (!h->htable)return -ENOMEM ;
Signature · 1 line
2.34.1
May 6, 2025 09:20:51 Cai Xinchen <[email protected]>:
Quoted 25 lines
When the user writes some data to the file
/sys/fs/selinux/policy, there is no check for the user buf passed to kcalloc. Syzkaller shows this warning:WARNING: CPU: 1 PID: 6642 at mm/page_alloc.c
__alloc_pages_noprof___kmalloc_large_node__kmalloc_large_node_noprof__kmalloc_noprofhashtab_initcommon_readpolicydb_readsecurity_load_policysel_write_loadvfs_writeksys_writedo_syscall_64This warning can be reproduced by writing this content to
/sys/fs/selinux/policy8cff7cf9080000005345204c696e757815000000e0ff962a08000000 070000004cf523cd7eec26886d70a6b7c78b496f1a0a192cea34ff4170581a743ff0cfb97ea0f0d170d1fe1441c2f7c8ea1c78dd17a1924935210081a83c30ec4171450bfc1de12cfe1ff342a887
Quoted 2 lines
Add check to prevent the size passed to kcalloc larger than
MAX_PAGE_ORDERafterget_order.
This might be similar to https://lore.kernel.org/selinux/[email protected]/
I'll try to respin that patch set in time.
Quoted 43 lines
Signed-off-by Cai Xinchen <[email protected]>---
security/selinux/ss/hashtab.c| 18 ++++++++++++++++++1 file changed, 18 insertions(+)security/selinux/ss/hashtab.c +18
-1@@ -30,6 +30,21 @@ static u32 hashtab_compute_size(u32 nel) return nel == 0 ? 0 : roundup_pow_of_two(nel); } +static bool is_order_out_of_range (u32 size,struct hashtab *h) +{ + size_t bytes; + u32 order; + + if (unlikely(check_mul_overflow(size, sizeof(*h->htable), &bytes))) + return true; + + order = get_order(bytes); + if (order > MAX_PAGE_ORDER) + return true; + + return false; +} +int hashtab_init (struct hashtab *h,u32 nel_hint) { u32 size = hashtab_compute_size(nel_hint);@@ -40,6 +55,9 @@ int hashtab_init(struct hashtab *h, u32 nel_hint) h->htable = NULL; if (size) { + if (is_order_out_of_range(size, h)) + return -ENOMEM; + h->htable = kcalloc(size, sizeof(*h->htable), GFP_KERNEL); if (!h->htable) return -ENOMEM; --2.34.1
Paul Moore
maintainerQuoted 27 lines · Christian Göttsche
May 6, 2025 09:20:51 Cai Xinchen <[email protected]>:
Quoted 25 lines
When the user writes some data to the file
/sys/fs/selinux/policy, there is no check for the user buf passed to kcalloc. Syzkaller shows this warning:WARNING: CPU: 1 PID: 6642 at mm/page_alloc.c
__alloc_pages_noprof___kmalloc_large_node__kmalloc_large_node_noprof__kmalloc_noprofhashtab_initcommon_readpolicydb_readsecurity_load_policysel_write_loadvfs_writeksys_writedo_syscall_64This warning can be reproduced by writing this content to
/sys/fs/selinux/policy8cff7cf9080000005345204c696e757815000000e0ff962a08000000 070000004cf523cd7eec26886d70a6b7c78b496f1a0a192cea34ff4170581a743ff0cfb97ea0f0d170d1fe1441c2f7c8ea1c78dd17a1924935210081a83c30ec4171450bfc1de12cfe1ff342a887
Quoted 6 lines
Quoted 2 lines
Add check to prevent the size passed to kcalloc larger than
MAX_PAGE_ORDERafterget_order.This might be similar to https://lore.kernel.org/selinux/[email protected]/
I'll try to respin that patch set in time.
Or we just use _GFP_NOWARN which I believe should quiet the warning without having to do a lot of extra work.
If someone wanted to track down all of the different allocations that can be triggered from a policy load and mark them all as NOWARN, I think that would be time well spent.
Signature · 1 line
paul-moore.com