From: Remington Brasga <rbrasga@uci.edu>
To: Christian Heusel <christian@heusel.eu>,
Shuah Khan <skhan@linuxfoundation.org>,
Thomas Gleixner <tglx@linutronix.de>,
"Ahmed S . Darwish" <darwi@linutronix.de>
Cc: linux-kernel@vger.kernel.org,
linux-kernel-mentees@lists.linuxfoundation.org,
Remington Brasga <rbrasga@uci.edu>
Subject: [PATCH] kcpuid: Fix potential dereferencing of null pointers
Date: Thu, 26 Sep 2024 22:35:57 +0000 [thread overview]
Message-ID: <20240926223557.2048-1-rbrasga@uci.edu> (raw)
Clang reported "clang-analyzer-core.NullDereference" on the `leaf` and `range`
variables in kcpuid.c, which makes sense if malloc/realloc fail.
These changes will ensure that the variables are not dereferenced while null.
Signed-off-by: Remington Brasga <rbrasga@uci.edu>
---
tools/arch/x86/kcpuid/kcpuid.c | 23 +++++++++++++++++++----
1 file changed, 19 insertions(+), 4 deletions(-)
diff --git a/tools/arch/x86/kcpuid/kcpuid.c b/tools/arch/x86/kcpuid/kcpuid.c
index 1b25c0a95d3f..c05226d105b6 100644
--- a/tools/arch/x86/kcpuid/kcpuid.c
+++ b/tools/arch/x86/kcpuid/kcpuid.c
@@ -144,19 +144,29 @@ static bool cpuid_store(struct cpuid_range *range, u32 f, int subleaf,
if (!func->leafs) {
func->leafs = malloc(sizeof(struct subleaf));
- if (!func->leafs)
+ if (!func->leafs) {
perror("malloc func leaf");
+ return false; // On malloc failure
+ }
func->nr = 1;
} else {
s = func->nr;
func->leafs = realloc(func->leafs, (s + 1) * sizeof(*leaf));
- if (!func->leafs)
+ if (!func->leafs) {
perror("realloc f->leafs");
+ return false; // On realloc failure
+ }
func->nr++;
}
+ // Check for valid index
+ if (s >= func->nr) {
+ fprintf(stderr, "Error: Invalid index for leaf\n");
+ return false;
+ }
+
leaf = &func->leafs[s];
leaf->index = f;
@@ -210,8 +220,10 @@ struct cpuid_range *setup_cpuid_range(u32 input_eax)
idx_func = (max_func & 0xffff) + 1;
range = malloc(sizeof(struct cpuid_range));
- if (!range)
+ if (!range) {
perror("malloc range");
+ return NULL; // On malloc failure
+ }
if (input_eax & 0x80000000)
range->is_ext = true;
@@ -219,8 +231,11 @@ struct cpuid_range *setup_cpuid_range(u32 input_eax)
range->is_ext = false;
range->funcs = malloc(sizeof(struct cpuid_func) * idx_func);
- if (!range->funcs)
+ if (!range->funcs) {
perror("malloc range->funcs");
+ free(range);
+ return NULL; // On malloc failure
+ }
range->nr = idx_func;
memset(range->funcs, 0, sizeof(struct cpuid_func) * idx_func);
--
2.34.1
next reply other threads:[~2024-09-26 22:36 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-26 22:35 Remington Brasga [this message]
2024-09-29 10:52 ` [PATCH] kcpuid: Fix potential dereferencing of null pointers Thomas Gleixner
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=20240926223557.2048-1-rbrasga@uci.edu \
--to=rbrasga@uci.edu \
--cc=christian@heusel.eu \
--cc=darwi@linutronix.de \
--cc=linux-kernel-mentees@lists.linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=skhan@linuxfoundation.org \
--cc=tglx@linutronix.de \
/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