Skip to contentSkip to conversation

selftests/memfd/memfd_test: Fix possible NULL pointer dereference

PATCH Applied liuye 3 participants 3 messages Jan 14, 2025 Kselftest

lore
Share & tools
lore

liuye

author
#0 lore raw

PATCH 1/1 selftests/memfd/memfd_test: Fix possible NULL pointer dereference

If name is NULL, a NULL pointer may be accessed in printf.

---
 tools/testing/selftests/memfd/memfd_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/memfd/memfd_test.c +1 -1
index c0c53451a16d..5b993924cc3f 100644
@@ -171,7 +171,7 @@ static void mfd_fail_new(const char *name, unsigned int flags)
 	r = sys_memfd_create(name, flags);
 	if (r >= 0) {
 		printf("memfd_create(\"%s\", %u) succeeded, but failure expected\n",
-		       name, flags);
+		       name ? name : "NULL", flags);
 		close(r);
 		abort();
 	}
Signature · 1 line
2.25.1
#1 lore raw
Quoted 7 lines · On Tue, 14 Jan 2025 11:21:15 +0800 liuye <[email protected]> wrote:

If name is NULL, a NULL pointer may be accessed in printf.

...

tools/testing/selftests/memfd/memfd_test.c static void mfd_fail_new(const char *name, unsigned int flags) · line 176

Quoted 7 lines
tools/testing/selftests/memfd/memfd_test.c +1 -1
@@ -171,7 +171,7 @@ static void mfd_fail_new(const char *name, unsigned int flags)
 	r = sys_memfd_create(name, flags);
 	if (r >= 0) {
 		printf("memfd_create(\"%s\", %u) succeeded, but failure expected\n",
-		       name, flags);
+		       name ? name : "NULL", flags);
 		close(r);
 		abort();

Well huh. I though printf() would emit "(null)" in this situation, but my super-sophisticated test case says "core dumped".

#include <stdio.h>
#include <stdlib.h>

int main()

{
	printf("%s\n", (char *)0);
	exit(0);
}
Reviewed-by on v1 1/1
#2 lore raw
In reply to liuye
Quoted 3 lines · liuye

If name is NULL, a NULL pointer may be accessed in printf.

Quoted 9 lines
---
 tools/testing/selftests/memfd/memfd_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/memfd/memfd_test.c +0 -0
index c0c53451a16d..5b993924cc3f 100644

tools/testing/selftests/memfd/memfd_test.c static void mfd_fail_new(const char *name, unsigned int flags) · line 174

Quoted 5 lines
tools/testing/selftests/memfd/memfd_test.c +1 -1
@@ -171,7 +171,7 @@ static void mfd_fail_new(const char *name, unsigned int flags)
 	r = sys_memfd_create(name, flags);
 	if (r >= 0) {
 		printf("memfd_create(\"%s\", %u) succeeded, but failure expected\n",
-		       name, flags);
+		       name ? name : "NULL", flags);

Damn, I was going to say 'oh hey we never pass NULL' but in test_create() we explicitly... do. Good spot!

Quoted 6 lines
tools/testing/selftests/memfd/memfd_test.c +0 -1
 		close(r);
 		abort();
 	}
--

2.25.1