Skip to contentSkip to conversation

um: fix len of file in create_pid_file

PATCH In review Wen Yang 2 participants 2 messages Feb 19, 2020 – Mar 29, 2020 User-mode Linux (UML)

lore
Share & tools
lore

Wen Yang

author
#0 lore raw

PATCH 1/1 um: fix len of file in create_pid_file

sizeof gives us the size of the pointer variable, not of the area it points to. So the number of bytes copied by umid_file_name() is 8.
We should pass in the correct length of the file buffer.

Cc Jeff Dike <[email protected]> (maintainer:USER-MODE LINUX (UML))
Cc Richard Weinberger <[email protected]> (maintainer:USER-MODE LINUX (UML),commit_signer:2/2=100%)
Cc Anton Ivanov <[email protected]> (maintainer:USER-MODE LINUX (UML))
Cc Bartosz Golaszewski <[email protected]> (commit_signer:1/2=50%,authored:1/2=50%,added_lines:27/28=96%,removed_lines:9/10=90%)
Cc Alex Dewar <[email protected]> (commit_signer:1/2=50%,authored:1/2=50%,removed_lines:1/10=10%)
Cc [email protected] (open list:USER-MODE LINUX (UML))
---
 arch/um/os-Linux/umid.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/um/os-Linux/umid.c +3 -2
index 44def53a11cd..9e16078a4bf8 100644
@@ -220,11 +220,12 @@ static void __init create_pid_file(void)
 	char pid[sizeof("nnnnn\0")], *file;
 	int fd, n;
 
-	file = malloc(strlen(uml_dir) + UMID_LEN + sizeof("/pid\0"));
+	n = strlen(uml_dir) + UMID_LEN + sizeof("/pid\0");
+	file = malloc(n);
 	if (!file)
 		return;
 
-	if (umid_file_name("pid", file, sizeof(file)))
+	if (umid_file_name("pid", file, n))
 		goto out;
 
 	fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644);
Signature · 7 lines
2.23.0


_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
#1 lore raw
Quoted 25 lines · Wen Yang

sizeof gives us the size of the pointer variable, not of the area it points to. So the number of bytes copied by umid_file_name() is 8.
We should pass in the correct length of the file buffer.

Cc Jeff Dike <[email protected]> (maintainer:USER-MODE LINUX (UML))
Cc Richard Weinberger <[email protected]> (maintainer:USER-MODE LINUX (UML),commit_signer:2/2=100%)
Cc Anton Ivanov <[email protected]> (maintainer:USER-MODE LINUX (UML))
Cc Bartosz Golaszewski <[email protected]> (commit_signer:1/2=50%,authored:1/2=50%,added_lines:27/28=96%,removed_lines:9/10=90%)
Cc Alex Dewar <[email protected]> (commit_signer:1/2=50%,authored:1/2=50%,removed_lines:1/10=10%)
Cc [email protected] (open list:USER-MODE LINUX (UML))
---
 arch/um/os-Linux/umid.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/um/os-Linux/umid.c +0 -0
index 44def53a11cd..9e16078a4bf8 100644
@@ -220,11 +220,12 @@ static void __init create_pid_file(void)
        char pid[sizeof("nnnnn\0")], *file;
        int fd, n;
 

arch/um/os-Linux/umid.c static void __init create_pid_file(void) · line 230

Quoted 10 lines
arch/um/os-Linux/umid.c +3 -2
-       file = malloc(strlen(uml_dir) + UMID_LEN + sizeof("/pid\0"));
+       n = strlen(uml_dir) + UMID_LEN + sizeof("/pid\0");
+       file = malloc(n);
        if (!file)
                return;
 
-       if (umid_file_name("pid", file, sizeof(file)))
+       if (umid_file_name("pid", file, n))
                goto out;
 

Good catch, applied.

Signature · 7 lines
Thanks,
//richard

_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um