Skip to contentSkip to conversation

staging: gasket: sysfs: fix potential null dereference

PATCH Superseded Ivan Bornyakov 2 participants 3 messages Jul 31, 2018 – Aug 1, 2018 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: gasket: sysfs: fix potential null dereference

Add handling of possible allocation failure.

Reported by smatch:

drivers/staging/gasket/gasket_sysfs.c:105 put_mapping() error: potential null dereference 'files_to_remove'. (kcalloc returns null)

---
 drivers/staging/gasket/gasket_sysfs.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/staging/gasket/gasket_sysfs.c +6 -0
index ef4eca02afa6..682e5c21f203 100644
@@ -101,6 +101,12 @@ static void put_mapping(struct gasket_sysfs_mapping *mapping)
 		files_to_remove = kcalloc(num_files_to_remove,
 					  sizeof(*files_to_remove),
 					  GFP_KERNEL);
+		if (!files_to_remove) {
+			pr_err("%s: can't allocate memory\n", __func__);
+			mutex_unlock(&mapping->mutex);
+			return;
+		}
+
 		for (i = 0; i < num_files_to_remove; i++)
 			files_to_remove[i] = mapping->attributes[i].attr;
 
Signature · 1 line
2.16.4
maintainer
#1 lore raw
Quoted 16 lines · Ivan Bornyakov

Add handling of possible allocation failure.

Reported by smatch:

drivers/staging/gasket/gasket_sysfs.c:105 put_mapping() error: potential null dereference 'files_to_remove'. (kcalloc returns null)

---
 drivers/staging/gasket/gasket_sysfs.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/staging/gasket/gasket_sysfs.c +0 -0
index ef4eca02afa6..682e5c21f203 100644

drivers/staging/gasket/gasket_sysfs.c static void put_mapping(struct gasket_sysfs_mapping *mapping) · line 105

Quoted 5 lines
drivers/staging/gasket/gasket_sysfs.c +2 -0
@@ -101,6 +101,12 @@ static void put_mapping(struct gasket_sysfs_mapping *mapping)
 		files_to_remove = kcalloc(num_files_to_remove,
 					  sizeof(*files_to_remove),
 					  GFP_KERNEL);
+		if (!files_to_remove) {
+			pr_err("%s: can't allocate memory\n", __func__);

kcalloc failing will print something, no need to do it again here.

thanks,

greg k-h

#2 lore raw

PATCH v2 1/1 staging: gasket: sysfs: fix potential null dereference

Add handling of possible allocation failure.

Reported by smatch:

drivers/staging/gasket/gasket_sysfs.c:105 put_mapping() error: potential null dereference 'files_to_remove'. (kcalloc returns null)

---
 drivers/staging/gasket/gasket_sysfs.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/staging/gasket/gasket_sysfs.c +5 -0
index a4bfca43cd03..56d62aea5111 100644
@@ -101,6 +101,11 @@ static void put_mapping(struct gasket_sysfs_mapping *mapping)
 		files_to_remove = kcalloc(num_files_to_remove,
 					  sizeof(*files_to_remove),
 					  GFP_KERNEL);
+		if (!files_to_remove) {
+			mutex_unlock(&mapping->mutex);
+			return;
+		}
+
 		for (i = 0; i < num_files_to_remove; i++)
 			files_to_remove[i] = mapping->attributes[i].attr;
 
Signature · 1 line
2.16.4