Skip to contentSkip to conversation

selftests/x86/lam: Zero out buffer for readlink()

PATCH Applied Binbin Wu 4 participants 4 messages Oct 16, 2023

Subsystems (2)
lore
Share & tools
lore

Binbin Wu

author
#0 lore raw

PATCH v2 1/1 selftests/x86/lam: Zero out buffer for readlink()

Zero out the buffer for readlink() since readlink() does not append a terminating null byte to the buffer. Also change the buffer length passed to readlink() to 'PATH_MAX - 1' to ensure the resulting string is always null terminated.

Fixes 833c12ce0f430 ("selftests/x86/lam: Add inherit test cases for linear-address masking")

---
v1->v2:
- Change the buffer length passed to readlink() to 'PATH_MAX - 1' to ensure the resulting string is always null terminated. [Kirill]

 tools/testing/selftests/x86/lam.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/x86/lam.c +3 -3
index eb0e46905bf9..8f9b06d9ce03 100644
@@ -573,7 +573,7 @@ int do_uring(unsigned long lam)
 	char path[PATH_MAX] = {0};
 
 	/* get current process path */
-	if (readlink("/proc/self/exe", path, PATH_MAX) <= 0)
+	if (readlink("/proc/self/exe", path, PATH_MAX - 1) <= 0)
 		return 1;
 
 	int file_fd = open(path, O_RDONLY);
@@ -680,14 +680,14 @@ static int handle_execve(struct testcases *test)
 		perror("Fork failed.");
 		ret = 1;
 	} else if (pid == 0) {
-		char path[PATH_MAX];
+		char path[PATH_MAX] = {0};
 
 		/* Set LAM mode in parent process */
 		if (set_lam(lam) != 0)
 			return 1;
 
 		/* Get current binary's path and the binary was run by execve */
-		if (readlink("/proc/self/exe", path, PATH_MAX) <= 0)
+		if (readlink("/proc/self/exe", path, PATH_MAX - 1) <= 0)
 			exit(-1);
 
 		/* run binary to get LAM mode and return to parent process */

base-commit: 58720809f52779dc0f08e53e54b014209d13eebb

Signature · 1 line
2.25.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 8 lines · Binbin Wu

Zero out the buffer for readlink() since readlink() does not append a terminating null byte to the buffer. Also change the buffer length passed to readlink() to 'PATH_MAX - 1' to ensure the resulting string is always null terminated.

Fixes 833c12ce0f430 ("selftests/x86/lam: Add inherit test cases for linear-address masking")
Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
maintainer
#2 lore raw
Quoted 6 lines · On 10/16/23 02:47, [email protected] wrote:
Quoted 5 lines · Binbin Wu

Zero out the buffer for readlink() since readlink() does not append a terminating null byte to the buffer. Also change the buffer length passed to readlink() to 'PATH_MAX - 1' to ensure the resulting string is always null terminated.

Quoted 6 lines
Quoted 3 lines
Fixes 833c12ce0f430 ("selftests/x86/lam: Add inherit test cases for linear-address masking")

Thank you. Applied to linux-kselftest next for Linux 6.7-rc1

thanks,
-- Shuah

merge bot Applied
#3 lore raw
In reply to Binbin Wu
The following commit has been merged into the x86/mm branch of tip:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     29060633411a02f6f2dd9d5245919385d69d81f0
Gitweb:        https://git.kernel.org/tip/29060633411a02f6f2dd9d5245919385d69d81f0
Author:        Binbin Wu <[email protected]>
AuthorDate:    Mon, 16 Oct 2023 14:24:46 +08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Mon, 16 Oct 2023 11:39:57 +02:00

selftests/x86/lam: Zero out buffer for readlink()

Zero out the buffer for readlink() since readlink() does not append a terminating null byte to the buffer. Also change the buffer length passed to readlink() to 'PATH_MAX - 1' to ensure the resulting string is always null terminated.

Fixes 833c12ce0f430 ("selftests/x86/lam: Add inherit test cases for linear-address masking")
---
 tools/testing/selftests/x86/lam.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/x86/lam.c +3 -3
index eb0e469..8f9b06d 100644
@@ -573,7 +573,7 @@ int do_uring(unsigned long lam)
 	char path[PATH_MAX] = {0};
 
 	/* get current process path */
-	if (readlink("/proc/self/exe", path, PATH_MAX) <= 0)
+	if (readlink("/proc/self/exe", path, PATH_MAX - 1) <= 0)
 		return 1;
 
 	int file_fd = open(path, O_RDONLY);
@@ -680,14 +680,14 @@ static int handle_execve(struct testcases *test)
 		perror("Fork failed.");
 		ret = 1;
 	} else if (pid == 0) {
-		char path[PATH_MAX];
+		char path[PATH_MAX] = {0};
 
 		/* Set LAM mode in parent process */
 		if (set_lam(lam) != 0)
 			return 1;
 
 		/* Get current binary's path and the binary was run by execve */
-		if (readlink("/proc/self/exe", path, PATH_MAX) <= 0)
+		if (readlink("/proc/self/exe", path, PATH_MAX - 1) <= 0)
 			exit(-1);
 
 		/* run binary to get LAM mode and return to parent process */