Skip to contentSkip to conversation

meson-mx-socinfo: Fix possible null-pointer dereference issues in meson_mx_socinfo_init

PATCH In review Kunwu Chan 2 participants 2 messages Jan 19, 2024

Subsystems (2)
lore
Share & tools
lore

Kunwu Chan

author
#0 lore raw

PATCH 1/1 meson-mx-socinfo: Fix possible null-pointer dereference issues in meson_mx_socinfo_init

In meson_mx_socinfo_revision, kasprintf() returns a pointer to dynamically allocated memory which can be NULL upon failure.

Similarly, the kstrdup_const in the meson_mx_socinfo_soc_id returns a null pointer when it fails. Ensure the allocation was successful by checking the pointer validity. Avoid null pointer dereference issues.

---
 drivers/soc/amlogic/meson-mx-socinfo.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/soc/amlogic/meson-mx-socinfo.c +6 -0
index 92125dd65f33..9c0296599561 100644
@@ -160,6 +160,12 @@ static int __init meson_mx_socinfo_init(void)
 							   metal_rev);
 	soc_dev_attr->soc_id = meson_mx_socinfo_soc_id(major_ver, metal_rev);
 
+	if (!soc_dev_attr->revision || !soc_dev_attr->soc_id) {
+		kfree_const(soc_dev_attr->revision);
+		kfree_const(soc_dev_attr->soc_id);
+		kfree(soc_dev_attr);
+		return -ENOMEM;
+	}
 	soc_dev = soc_device_register(soc_dev_attr);
 	if (IS_ERR(soc_dev)) {
 		kfree_const(soc_dev_attr->revision);
Signature · 1 line
2.39.2
#1 lore raw
Quoted 6 lines

In meson_mx_socinfo_revision, kasprintf() returns a pointer to dynamically allocated memory which can be NULL upon failure.

Similarly, the kstrdup_const in the meson_mx_socinfo_soc_id returns a null pointer when it fails. Ensure the allocation was successful by checking the pointer validity. Avoid null pointer dereference issues.

Would you like to add the tag “Fixes” for the completion of the error handling in this function implementation?

…

drivers/soc/amlogic/meson-mx-socinfo.c static int __init meson_mx_socinfo_init(void) · line 163

Quoted 6 lines

+++ b/drivers/soc/amlogic/meson-mx-socinfo.c

drivers/soc/amlogic/meson-mx-socinfo.c +1 -0
@@ -160,6 +160,12 @@ static int __init meson_mx_socinfo_init(void)
 							   metal_rev);
 	soc_dev_attr->soc_id = meson_mx_socinfo_soc_id(major_ver, metal_rev);
 
+	if (!soc_dev_attr->revision || !soc_dev_attr->soc_id) {

I suggest to split such a check for null pointers.

drivers/soc/amlogic/meson-mx-socinfo.c static int __init meson_mx_socinfo_init(void) · line 171

Quoted 8 lines
drivers/soc/amlogic/meson-mx-socinfo.c +5 -0
+		kfree_const(soc_dev_attr->revision);
+		kfree_const(soc_dev_attr->soc_id);
+		kfree(soc_dev_attr);
+		return -ENOMEM;
+	}
 	soc_dev = soc_device_register(soc_dev_attr);
 	if (IS_ERR(soc_dev)) {
 		kfree_const(soc_dev_attr->revision);

Please use a goto chain for better exception handling.
https://wiki.sei.cmu.edu/confluence/display/c/MEM12-C.+Consider+using+a+goto+chain+when+leaving+a+function+on+error+when+using+and+releasing+resources

Regards,
Markus

_______________________________________________
linux-amlogic mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-amlogic