Skip to contentSkip to conversation

fs: ext4: Fix dereferencing the null pointer 'ptr'

PATCH In review Mikhail Ilin 2 participants 2 messages Nov 23, 2022 ext4 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: ext4: Fix dereferencing the null pointer 'ptr'

If memory allocation fails on line 780, then 'fail' will be jumped to and 'ptr' will be null, causing it to be dereferenced it on line
855. Thus, before using 'ptr[i]' one must make sure that the 'ptr' pointer is not NULL.

Fixes 934b14f2bb30 ("ext4: free allocations by parse_path()")
---
 fs/ext4/ext4_common.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
fs/ext4/ext4_common.c +6 -4
index 1185cb2c04..3cdd1a04a9 100644
@@ -851,10 +851,12 @@ end:
 fail:
 	free(depth_dirname);
 	free(parse_dirname);
-	for (i = 0; i < depth; i++) {
-		if (!ptr[i])
-			break;
-		free(ptr[i]);
+	if (ptr) {
+		for (i = 0; i < depth; i++) {
+			if (!ptr[i])
+				break;
+			free(ptr[i]);
+		}
 	}
 	free(ptr);
 	free(parent_inode);
Signature · 1 line
2.17.1
#1 lore raw
Quoted 4 lines · Mikhail Ilin

If memory allocation fails on line 780, then 'fail' will be jumped to and 'ptr' will be null, causing it to be dereferenced it on line
855. Thus, before using 'ptr[i]' one must make sure that the 'ptr' pointer is not NULL.

Nitpicking. You seem to have a leading space in this comment block. Please remove next time.

Quoted 17 lines
Fixes 934b14f2bb30 ("ext4: free allocations by parse_path()")
---
  fs/ext4/ext4_common.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
fs/ext4/ext4_common.c +0 -2
index 1185cb2c04..3cdd1a04a9 100644
@@ -851,10 +851,12 @@ end:
  fail:
  	free(depth_dirname);
  	free(parse_dirname);
-	for (i = 0; i < depth; i++) {
-		if (!ptr[i])

fs/ext4/ext4_common.c end: · line 861

Quoted 10 lines
fs/ext4/ext4_common.c +6 -2
-			break;
-		free(ptr[i]);
+	if (ptr) {
+		for (i = 0; i < depth; i++) {
+			if (!ptr[i])
+				break;
+			free(ptr[i]);
+		}
  	}
  	free(ptr);

Won't this fail with ptr == NULL? Please also include the free(ptr) into the if (ptr) { } part.

Thanks,
Stefan