Skip to contentSkip to conversation

Fix a couple of potential problem (static analysis)

PATCH Applied Yauheni Kaliuta 2 participants 8 messages Jun 8, 2021 – Jun 22, 2021

lore
Share & tools
lore
#0 lore raw

cover Fix a couple of potential problem (static analysis)

Yauheni Kaliuta (2):
libkmod-module: check "new_from_name" return value in get_builtin
libkmod-builtin: consider final NIL in name length check

 libkmod/libkmod-builtin.c | 2 +-
 libkmod/libkmod-module.c  | 5 ++++-
 2 files changed, 5 insertions(+), 2 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/2 libkmod-module: check "new_from_name" return value in get_builtin

kmod_module_new_from_name() may fail and return error value. It is handled properly across the code, but in this particular place the check is missing.

---
 libkmod/libkmod-module.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
libkmod/libkmod-module.c +4 -1
index b6320cc87e80..6e0ff1a99604 100644
@@ -2912,7 +2912,10 @@ int kmod_module_get_builtin(struct kmod_ctx *ctx, struct kmod_list **list)
 			goto fail;
 		}
 
-		kmod_module_new_from_name(ctx, modname, &mod);
+		err = kmod_module_new_from_name(ctx, modname, &mod);
+		if (err < 0)
+			goto fail;
+
 		kmod_module_set_builtin(mod, true);
 
 		*list = kmod_list_append(*list, mod);
Signature · 1 line
2.31.1
#2 lore raw

PATCH 2/2 libkmod-builtin: consider final NIL in name length check

There is potential buffer overrun in kmod_builtin_iter_get_modname() for the name of length PATH_MAX. The required buffer size is PATH_MAX, so modname[len] = '\0' with len == PATH_MAX will write right beyond the buffer.

Check the length against PATH_MAX - 1.

---
 libkmod/libkmod-builtin.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libkmod/libkmod-builtin.c +1 -1
index a002cb5ee2c6..3d4d77ab29b3 100644
@@ -246,7 +246,7 @@ bool kmod_builtin_iter_get_modname(struct kmod_builtin_iter *iter,
 
 	len = dot - line;
 
-	if (len >= PATH_MAX) {
+	if (len >= PATH_MAX - 1) {
 		sv_errno = ENAMETOOLONG;
 		goto fail;
 	}
Signature · 1 line
2.31.1
#3 lore raw

Re: [PATCH 2/2] libkmod-builtin: consider final NIL in name length check

Quoted 4 lines · Yauheni Kaliuta

There is potential buffer overrun in kmod_builtin_iter_get_modname() for the name of length PATH_MAX. The required buffer size is PATH_MAX, so modname[len] = '\0' with len == PATH_MAX will write right beyond the buffer.

this doesn't look correct. "with len == PATH_MAX" we will actually return an error.

What indeed is happening is truncation: since we are not reserving 1 char for NUL termination, we will truncate the name. If we update the commit message to state the right reasoning, then we can land this patch.

I don't see any buffer overflow here, but I may be missing something.

thanks
LUcas De Marchi

Quoted 24 lines

Check the length against PATH_MAX - 1.

---
libkmod/libkmod-builtin.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
libkmod/libkmod-builtin.c +1 -1
index a002cb5ee2c6..3d4d77ab29b3 100644
@@ -246,7 +246,7 @@ bool kmod_builtin_iter_get_modname(struct kmod_builtin_iter *iter,
 
 	len = dot - line;
 
-	if (len >= PATH_MAX) {
+	if (len >= PATH_MAX - 1) {
 		sv_errno = ENAMETOOLONG;
 		goto fail;
 	}
Signature · 1 line
2.31.1
#4 lore raw

Re: [PATCH 2/2] libkmod-builtin: consider final NIL in name length check

Quoted 6 lines · Lucas De Marchi
Quoted 4 lines · Yauheni Kaliuta

There is potential buffer overrun in kmod_builtin_iter_get_modname() for the name of length PATH_MAX. The required buffer size is PATH_MAX, so modname[len] = '\0' with len == PATH_MAX will write right beyond the buffer.

Quoted 8 lines

this doesn't look correct. "with len == PATH_MAX" we will actually return an error.

What indeed is happening is truncation: since we are not reserving 1 char for NUL termination, we will truncate the name. If we update the commit message to state the right reasoning, then we can land this patch.

I don't see any buffer overflow here, but I may be missing something.

another thing... what is your git-sendemail setup? This is putting patch 2 as a reply to patch 1 and that breaks b4. See:
https://lore.kernel.org/linux-modules/[email protected]/T/#u

Lucas De Marchi

Quoted 28 lines

thanks
LUcas De Marchi

Quoted 24 lines

Check the length against PATH_MAX - 1.

---
libkmod/libkmod-builtin.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
libkmod/libkmod-builtin.c +1 -1
index a002cb5ee2c6..3d4d77ab29b3 100644
@@ -246,7 +246,7 @@ bool kmod_builtin_iter_get_modname(struct kmod_builtin_iter *iter,
 
 	len = dot - line;
 
-	if (len >= PATH_MAX) {
+	if (len >= PATH_MAX - 1) {
 		sv_errno = ENAMETOOLONG;
 		goto fail;
 	}
Signature · 1 line
2.31.1
#5 lore raw

Re: [PATCH 2/2] libkmod-builtin: consider final NIL in name length check

Quoted 15 lines · Lucas De Marchi
Quoted 13 lines · Lucas De Marchi
Quoted 4 lines · Yauheni Kaliuta

There is potential buffer overrun in kmod_builtin_iter_get_modname() for the name of length PATH_MAX. The required buffer size is PATH_MAX, so modname[len] = '\0' with len == PATH_MAX will write right beyond the buffer.

this doesn't look correct. "with len == PATH_MAX" we will actually return an error.

What indeed is happening is truncation: since we are not reserving 1 char for NUL termination, we will truncate the name. If we update the commit message to state the right reasoning, then we can land this patch.

Quoted 5 lines
Quoted 1 line

I don't see any buffer overflow here, but I may be missing something.

another thing... what is your git-sendemail setup? This is putting patch 2 as a reply to patch 1 and that breaks b4. See:
https://lore.kernel.org/linux-modules/[email protected]/T/#u

Thanks, yes. I have thread = true, but send it after the cover letter with

git send-email --to [email protected] --cc [email protected] [email protected] 0001-libkmod-module-check-new_from_name-return-value-in-g.patch 0002-libkmod-builtin-consider-final-NIL-in-name-length-ch.patch

So, the right way is to send all together, or disable 'thread'.

Quoted 32 lines

Lucas De Marchi

Quoted 28 lines

thanks
LUcas De Marchi

Quoted 24 lines

Check the length against PATH_MAX - 1.

---
libkmod/libkmod-builtin.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
libkmod/libkmod-builtin.c +1 -2
index a002cb5ee2c6..3d4d77ab29b3 100644
@@ -246,7 +246,7 @@ bool kmod_builtin_iter_get_modname(struct kmod_builtin_iter *iter,
 
     len = dot - line;
 
-     if (len >= PATH_MAX) {
+     if (len >= PATH_MAX - 1) {
             sv_errno = ENAMETOOLONG;
             goto fail;
     }
--

2.31.1

Signature · 1 line
WBR, Yauheni
#6 lore raw

Re: [PATCH 2/2] libkmod-builtin: consider final NIL in name length check

In reply to Lucas De Marchi

Hi, Lucas!

My filters missed your replies, sorry :(

Quoted 7 lines · Lucas De Marchi
Quoted 4 lines · Yauheni Kaliuta

There is potential buffer overrun in kmod_builtin_iter_get_modname() for the name of length PATH_MAX. The required buffer size is PATH_MAX, so modname[len] = '\0' with len == PATH_MAX will write right beyond the buffer.

Quoted 8 lines

this doesn't look correct. "with len == PATH_MAX" we will actually return an error.

What indeed is happening is truncation: since we are not reserving 1 char for NUL termination, we will truncate the name. If we update the commit message to state the right reasoning, then we can land this patch.

I don't see any buffer overflow here, but I may be missing something.

Yes, you are right. I see the actual overflow is fixed by 1cab02ecf6ee ("libkmod: fix an overflow with wrong modules.builtin.modinfo")

I should have checked some reports more carefully. Please, ignore the patch.

Quoted 19 lines

thanks
LUcas De Marchi

Quoted 16 lines

Check the length against PATH_MAX - 1.

---
libkmod/libkmod-builtin.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
libkmod/libkmod-builtin.c +0 -0
index a002cb5ee2c6..3d4d77ab29b3 100644
@@ -246,7 +246,7 @@ bool kmod_builtin_iter_get_modname(struct kmod_builtin_iter *iter,
 
      len = dot - line;
 
Quoted 9 lines
Quoted 8 lines
libkmod/libkmod-builtin.c +1 -2
-      if (len >= PATH_MAX) {
+      if (len >= PATH_MAX - 1) {
              sv_errno = ENAMETOOLONG;
              goto fail;
      }
--

2.31.1

--
WBR, Yauheni

#7 lore raw

Re: [PATCH 1/2] libkmod-module: check "new_from_name" return value in get_builtin

In reply to Yauheni Kaliuta
Quoted 5 lines · Yauheni Kaliuta

kmod_module_new_from_name() may fail and return error value. It is handled properly across the code, but in this particular place the check is missing.

applied, thanks

Lucas De Marchi

Quoted 23 lines

---
libkmod/libkmod-module.c | 5 ++++-

1 file changed, 4 insertions(+), 1 deletion(-)
libkmod/libkmod-module.c +4 -1
index b6320cc87e80..6e0ff1a99604 100644
@@ -2912,7 +2912,10 @@ int kmod_module_get_builtin(struct kmod_ctx *ctx, struct kmod_list **list)
 			goto fail;
 		}
 
-		kmod_module_new_from_name(ctx, modname, &mod);
+		err = kmod_module_new_from_name(ctx, modname, &mod);
+		if (err < 0)
+			goto fail;
+
 		kmod_module_set_builtin(mod, true);
 
 		*list = kmod_list_append(*list, mod);
Signature · 1 line
2.31.1