Skip to contentSkip to conversation

initramfs: Fix a missing-check bug in init/initramfs.c

PATCH Superseded Gen Zhang 1 participant 4 messages May 22, 2019 2 messages not in the archive VFS

lore
Share & tools
lore

Gen Zhang

author
#0 lore raw

PATCH 1/1 initramfs: Fix a missing-check bug in init/initramfs.c

In dir_add(), de and de->name are allocated by kmalloc() and kstrdup(). And de->name is dereferenced in the following codes. However, memory allocation functions such as kmalloc() and kstrdup() may fail. Dereferencing this de->name null pointer may cause the kernel go wrong. Thus we should check this allocation.
Further, if kstrdup() returns NULL, we should free de and panic().

---

init/initramfs.c +4 -0
index 178130f..dc8063f 100644
@@ -125,6 +125,10 @@ static void __init dir_add(const char *name, time64_t mtime)
 		panic("can't allocate dir_entry buffer");
 	INIT_LIST_HEAD(&de->list);
 	de->name = kstrdup(name, GFP_KERNEL);
+	if (!de->name) {
+		kfree(de);
+		panic("can't allocate dir_entry name buffer");
+	}
 	de->mtime = mtime;
 	list_add(&de->list, &dir_list);
 }

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#2 lore raw
Quoted 27 lines · Li Zhijian
Quoted 15 lines · Gen Zhang

In dir_add(), de and de->name are allocated by kmalloc() and kstrdup(). And de->name is dereferenced in the following codes. However, memory allocation functions such as kmalloc() and kstrdup() may fail. Dereferencing this de->name null pointer may cause the kernel go wrong. Thus we should check this allocation.
Further, if kstrdup() returns NULL, we should free de and panic().

---

init/initramfs.c +0 -0
index 178130f..dc8063f 100644

init/initramfs.c static void __init dir_add(const char *name, time64_t mtime) · line 131

Quoted 7 lines
init/initramfs.c +4 -0
@@ -125,6 +125,10 @@ static void __init dir_add(const char *name, time64_t mtime)
 		panic("can't allocate dir_entry buffer");
 	INIT_LIST_HEAD(&de->list);
 	de->name = kstrdup(name, GFP_KERNEL);
+	if (!de->name) {
+		kfree(de);
+		panic("can't allocate dir_entry name buffer");
+	}

Looks good

Quoted 7 lines

but the following place should be considered as well i think
342                                 vcollected = kstrdup(collected, GFP_KERNEL); 343                                 state = CopyFile;

Thanks
Zhijian

Thanks for your comments, Zhijian!
I thinks you are correct that vcollected should also be checked. I will work on this patch and resubmit it.
Thank
Gen

author
#3 lore raw
In reply to a message not in the archive
Quoted 9 lines · Li Zhijian

Looks good

but the following place should be considered as well i think
342                                 vcollected = kstrdup(collected, GFP_KERNEL); 343                                 state = CopyFile;

Thanks
Zhijian

In dir_add() and do_name(), de->name and vcollected are allocated by kstrdup(). And de->name and vcollected are dereferenced in the following codes. However, memory allocation functions such as kstrdup() may fail. Dereferencing this null pointer may cause the kernel go wrong. Thus we should check these two kstrdup() operations.
Further, if kstrdup() returns NULL, we should free de in dir_add().

---

init/initramfs.c +8 -0
index 178130f..1421488 100644
@@ -125,6 +125,10 @@ static void __init dir_add(const char *name, time64_t mtime)
 		panic("can't allocate dir_entry buffer");
 	INIT_LIST_HEAD(&de->list);
 	de->name = kstrdup(name, GFP_KERNEL);
+	if (!de->name) {
+		kfree(de);
+		panic("can't allocate dir_entry name buffer");
+	}
 	de->mtime = mtime;
 	list_add(&de->list, &dir_list);
 }
@@ -340,6 +344,10 @@ static int __init do_name(void)
 				if (body_len)
 					ksys_ftruncate(wfd, body_len);
 				vcollected = kstrdup(collected, GFP_KERNEL);
+				if (!vcollected) {
+					panic("can't allocate vcollected buffer");
+					return 0;
+				}
 				state = CopyFile;
 			}
 		}

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#5 lore raw

PATCH v2 1/1 initramfs: Fix a missing-check bug in init/initramfs.c

In dir_add() and do_name(), de->name and vcollected are allocated by kstrdup(). And de->name and vcollected are dereferenced in the following codes. However, memory allocation functions such as kstrdup() may fail. Dereferencing this null pointer may cause the kernel go wrong. Thus we should check these two kstrdup() operations.
Further, if kstrdup() returns NULL, we should free de in dir_add().

---

init/initramfs.c +8 -0
index 178130f..1421488 100644
@@ -125,6 +125,10 @@ static void __init dir_add(const char *name, time64_t mtime)
 		panic("can't allocate dir_entry buffer");
 	INIT_LIST_HEAD(&de->list);
 	de->name = kstrdup(name, GFP_KERNEL);
+	if (!de->name) {
+		kfree(de);
+		panic("can't allocate dir_entry name buffer");
+	}
 	de->mtime = mtime;
 	list_add(&de->list, &dir_list);
 }
@@ -340,6 +344,10 @@ static int __init do_name(void)
 				if (body_len)
 					ksys_ftruncate(wfd, body_len);
 				vcollected = kstrdup(collected, GFP_KERNEL);
+				if (!vcollected) {
+					panic("can't allocate vcollected buffer");
+					return 0;
+				}
 				state = CopyFile;
 			}
 		}