From: Cai Xinchen <caixinchen1@huawei.com>
To: <linux-kernel@vger.kernel.org>, <selinux@vger.kernel.org>
Cc: <paul@paul-moore.com>, <stephen.smalley.work@gmail.com>,
<omosnace@redhat.com>, <ericsu@linux.microsoft.com>,
<caixinchen1@huawei.com>
Subject: [PATCH] SELinux: Add check for the user data passed to kcalloc in hashtab_init
Date: Tue, 6 May 2025 03:18:33 +0000 [thread overview]
Message-ID: <20250506031833.6107-1-caixinchen1@huawei.com> (raw)
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_noprof
hashtab_init
common_read
policydb_read
security_load_policy
sel_write_load
vfs_write
ksys_write
do_syscall_64
This warning can be reproduced by writing this content to
/sys/fs/selinux/policy
8cff7cf9 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.
Signed-off-by: Cai Xinchen <caixinchen1@huawei.com>
---
security/selinux/ss/hashtab.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/security/selinux/ss/hashtab.c b/security/selinux/ss/hashtab.c
index 383fd2d70878..18bcf3978c9e 100644
--- a/security/selinux/ss/hashtab.c
+++ b/security/selinux/ss/hashtab.c
@@ -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
next reply other threads:[~2025-05-06 3:30 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-06 3:18 Cai Xinchen [this message]
2025-05-06 8:14 ` [PATCH] SELinux: Add check for the user data passed to kcalloc in hashtab_init Christian Göttsche
2025-05-20 23:46 ` Paul Moore
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250506031833.6107-1-caixinchen1@huawei.com \
--to=caixinchen1@huawei.com \
--cc=ericsu@linux.microsoft.com \
--cc=linux-kernel@vger.kernel.org \
--cc=omosnace@redhat.com \
--cc=paul@paul-moore.com \
--cc=selinux@vger.kernel.org \
--cc=stephen.smalley.work@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox