Skip to contentSkip to conversation

platform: uv: fix missing checks for kcalloc

PATCH Superseded Kangjie Lu 4 participants 5 messages Mar 24, 2019 – Apr 23, 2019

Subsystems (3)
lore
Share & tools
lore

Kangjie Lu

author
#0 lore raw

PATCH 1/1 platform: uv: fix missing checks for kcalloc

In case kcalloc fails, the patch return an error to avoid potential NULL pointer dereference.

---
 arch/x86/platform/uv/tlb_uv.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/platform/uv/tlb_uv.c +6 -0
index 2c53b0f19329..1ac777f14846 100644
@@ -2140,7 +2140,13 @@ static int __init init_per_cpu(int nuvhubs, int base_part_pnode)
 		timeout_us = calculate_destination_timeout();
 
 	uvhub_descs = kcalloc(nuvhubs, sizeof(struct uvhub_desc), GFP_KERNEL);
+	if (!uvhub_descs)
+		return 1;
 	uvhub_mask = kzalloc((nuvhubs+7)/8, GFP_KERNEL);
+	if (!uvhub_mask) {
+		kfree(uvhub_descs);
+		return 1;
+	}
 
 	if (get_cpu_topology(base_part_pnode, uvhub_descs, uvhub_mask))
 		goto fail;
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 13 lines · Kangjie Lu

In case kcalloc fails, the patch return an error to avoid potential NULL pointer dereference.

---
 arch/x86/platform/uv/tlb_uv.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/platform/uv/tlb_uv.c +0 -0
index 2c53b0f19329..1ac777f14846 100644

arch/x86/platform/uv/tlb_uv.c static int __init init_per_cpu(int nuvhubs, int base_part_pnode) · line 2144

Quoted 5 lines
arch/x86/platform/uv/tlb_uv.c +2 -0
@@ -2140,7 +2140,13 @@ static int __init init_per_cpu(int nuvhubs, int base_part_pnode)
 		timeout_us = calculate_destination_timeout();
 
 	uvhub_descs = kcalloc(nuvhubs, sizeof(struct uvhub_desc), GFP_KERNEL);
+	if (!uvhub_descs)
+		return 1;

Here...

arch/x86/platform/uv/tlb_uv.c static int __init init_per_cpu(int nuvhubs, int base_part_pnode) · line 2149

Quoted 5 lines
arch/x86/platform/uv/tlb_uv.c +4 -0
 	uvhub_mask = kzalloc((nuvhubs+7)/8, GFP_KERNEL);
+	if (!uvhub_mask) {
+		kfree(uvhub_descs);
+		return 1;
+	}

... and here you can simply do

		goto fail;

instead. After having initialized uvhub_descs and uvhub_mask to NULL;

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
author
#2 lore raw

PATCH v2 1/1 platform: uv: fix missing checks for kcalloc

In case kcalloc fails, the patch return an error to avoid potential NULL pointer dereference.

---
v2: reuse existing error path as suggested by
Borislav Petkov <[email protected]>

---
 arch/x86/platform/uv/tlb_uv.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/platform/uv/tlb_uv.c +5 -1
index 2c53b0f19329..7b41b51b9a86 100644
@@ -2133,14 +2133,18 @@ static int __init summarize_uvhub_sockets(int nuvhubs,
  */
 static int __init init_per_cpu(int nuvhubs, int base_part_pnode)
 {
-	unsigned char *uvhub_mask;
 	struct uvhub_desc *uvhub_descs;
+	unsigned char *uvhub_mask = NULL;
 
 	if (is_uv3_hub() || is_uv2_hub() || is_uv1_hub())
 		timeout_us = calculate_destination_timeout();
 
 	uvhub_descs = kcalloc(nuvhubs, sizeof(struct uvhub_desc), GFP_KERNEL);
+	if (!uvhub_descs)
+		goto fail;
 	uvhub_mask = kzalloc((nuvhubs+7)/8, GFP_KERNEL);
+	if (!uvhub_mask)
+		goto fail;
 
 	if (get_cpu_topology(base_part_pnode, uvhub_descs, uvhub_mask))
 		goto fail;
Signature · 1 line
2.17.1
#3 lore raw

[tip:x86/cleanups] x86/platform/uv: Fix missing checks of kcalloc() return values

x86/platform/uv: Fix missing checks of kcalloc() return values
Commit-ID:  766460852cfaeca4042e5f3aeb9616b3689147bc
Gitweb:     https://git.kernel.org/tip/766460852cfaeca4042e5f3aeb9616b3689147bc
Author:     Kangjie Lu <[email protected]>
AuthorDate: Mon, 25 Mar 2019 15:29:22 -0500
Committer:  Borislav Petkov <[email protected]>
CommitDate: Tue, 26 Mar 2019 17:01:30 +0100

x86/platform/uv: Fix missing checks of kcalloc() return values

Handle potential errors returned from kcalloc().

[ bp: rewrite commit message. ]

Cc Andrew Banman <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Colin Ian King <[email protected]>
Cc Darren Hart <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Kees Cook <[email protected]>
Cc Mike Travis <[email protected]>
Cc Nicolai Stange <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Varsha Rao <[email protected]>
Cc x86-ml <[email protected]>
---
 arch/x86/platform/uv/tlb_uv.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/platform/uv/tlb_uv.c +6 -1
index 2c53b0f19329..1297e185b8c8 100644
@@ -2133,14 +2133,19 @@ static int __init summarize_uvhub_sockets(int nuvhubs,
  */
 static int __init init_per_cpu(int nuvhubs, int base_part_pnode)
 {
-	unsigned char *uvhub_mask;
 	struct uvhub_desc *uvhub_descs;
+	unsigned char *uvhub_mask = NULL;
 
 	if (is_uv3_hub() || is_uv2_hub() || is_uv1_hub())
 		timeout_us = calculate_destination_timeout();
 
 	uvhub_descs = kcalloc(nuvhubs, sizeof(struct uvhub_desc), GFP_KERNEL);
+	if (!uvhub_descs)
+		goto fail;
+
 	uvhub_mask = kzalloc((nuvhubs+7)/8, GFP_KERNEL);
+	if (!uvhub_mask)
+		goto fail;
 
 	if (get_cpu_topology(base_part_pnode, uvhub_descs, uvhub_mask))
 		goto fail;
Reviewed-by on v2 1/1
#4 lore raw
In reply to Kangjie Lu
Quoted 5 lines · Kangjie Lu

In case kcalloc fails, the patch return an error to avoid potential NULL pointer dereference.

-Kees

Quoted 35 lines

---
v2: reuse existing error path as suggested by
Borislav Petkov <[email protected]>

---
 arch/x86/platform/uv/tlb_uv.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/platform/uv/tlb_uv.c +5 -2
index 2c53b0f19329..7b41b51b9a86 100644
@@ -2133,14 +2133,18 @@ static int __init summarize_uvhub_sockets(int nuvhubs,
  */
 static int __init init_per_cpu(int nuvhubs, int base_part_pnode)
 {
-       unsigned char *uvhub_mask;
        struct uvhub_desc *uvhub_descs;
+       unsigned char *uvhub_mask = NULL;
 
        if (is_uv3_hub() || is_uv2_hub() || is_uv1_hub())
                timeout_us = calculate_destination_timeout();
 
        uvhub_descs = kcalloc(nuvhubs, sizeof(struct uvhub_desc), GFP_KERNEL);
+       if (!uvhub_descs)
+               goto fail;
        uvhub_mask = kzalloc((nuvhubs+7)/8, GFP_KERNEL);
+       if (!uvhub_mask)
+               goto fail;
 
        if (get_cpu_topology(base_part_pnode, uvhub_descs, uvhub_mask))
                goto fail;
--

2.17.1

Signature · 1 line
Kees Cook