Skip to contentSkip to conversation

printk: drop redundant devkmsg_log_str memsets

PATCH In review Sergey Senozhatsky 3 participants 5 messages Jan 19, 2018 – Jan 22, 2018 Printk

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 printk: drop redundant devkmsg_log_str memsets

We copy in null terminated strings "on" and "off", no need to zero out devkmsg_log_str in control_devkmsg().

---
 kernel/printk/printk.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
kernel/printk/printk.c +4 -7
index d70927c384f3..9faddcfd3994 100644
@@ -136,13 +136,10 @@ static int __init control_devkmsg(char *str)
 	/*
 	 * Set sysctl string accordingly:
 	 */
-	if (devkmsg_log == DEVKMSG_LOG_MASK_ON) {
-		memset(devkmsg_log_str, 0, DEVKMSG_STR_MAX_SIZE);
-		strncpy(devkmsg_log_str, "on", 2);
-	} else if (devkmsg_log == DEVKMSG_LOG_MASK_OFF) {
-		memset(devkmsg_log_str, 0, DEVKMSG_STR_MAX_SIZE);
-		strncpy(devkmsg_log_str, "off", 3);
-	}
+	if (devkmsg_log == DEVKMSG_LOG_MASK_ON)
+		strcpy(devkmsg_log_str, "on");
+	else if (devkmsg_log == DEVKMSG_LOG_MASK_OFF)
+		strcpy(devkmsg_log_str, "off");
 	/* else "ratelimit" which is set by default. */
 
 	/*
Signature · 1 line
2.16.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 20 lines · On Fri, 19 Jan 2018 13:39:01 +0900 Sergey Senozhatsky…

We copy in null terminated strings "on" and "off", no need to zero out devkmsg_log_str in control_devkmsg().

---
 kernel/printk/printk.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
kernel/printk/printk.c +0 -4
index d70927c384f3..9faddcfd3994 100644
@@ -136,13 +136,10 @@ static int __init control_devkmsg(char *str)
 	/*
 	 * Set sysctl string accordingly:
 	 */
-	if (devkmsg_log == DEVKMSG_LOG_MASK_ON) {
-		memset(devkmsg_log_str, 0, DEVKMSG_STR_MAX_SIZE);
-		strncpy(devkmsg_log_str, "on", 2);
-	} else if (devkmsg_log == DEVKMSG_LOG_MASK_OFF) {

kernel/printk/printk.c static int __init control_devkmsg(char *str) · line 145

Quoted 10 lines
kernel/printk/printk.c +4 -3
-		memset(devkmsg_log_str, 0, DEVKMSG_STR_MAX_SIZE);
-		strncpy(devkmsg_log_str, "off", 3);
-	}
+	if (devkmsg_log == DEVKMSG_LOG_MASK_ON)
+		strcpy(devkmsg_log_str, "on");
+	else if (devkmsg_log == DEVKMSG_LOG_MASK_OFF)
+		strcpy(devkmsg_log_str, "off");
 	/* else "ratelimit" which is set by default. */
 
 	/*

Yep, and even the sysctl checks for the terminating nul string, so there's no leaks of any kind.

Tested by adding: printk.devkmsg=on and running:

 $ hexdump -C /proc/sys/kernel/printk_devkmsg
00000000  6f 6e 0a                                          |on.|
00000003

-- Steve

#2 lore raw
Quoted 11 lines · Steven Rostedt

Yep, and even the sysctl checks for the terminating nul string, so there's no leaks of any kind.

Tested by adding: printk.devkmsg=on and running:

 $ hexdump -C /proc/sys/kernel/printk_devkmsg
00000000  6f 6e 0a                                          |on.|
00000003

thanks.

	-ss
maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Sergey Senozhatsky
Quoted 4 lines · Sergey Senozhatsky

We copy in null terminated strings "on" and "off", no need to zero out devkmsg_log_str in control_devkmsg().

Great catch!

I have pushed this into for-4.16 branch in printk.git, see
https://git.kernel.org/pub/scm/linux/kernel/git/pmladek/printk.git/commit/?h=for-4.16&id=6fd78a1a99c9580da49ee8f951fdce9846256375

Best Regards,
Petr

#4 lore raw
Quoted 4 lines · Petr Mladek
Quoted 3 lines · Sergey Senozhatsky

We copy in null terminated strings "on" and "off", no need to zero out devkmsg_log_str in control_devkmsg().

Quoted 8 lines
Quoted 1 line

Great catch!

I have pushed this into for-4.16 branch in printk.git, see
https://git.kernel.org/pub/scm/linux/kernel/git/pmladek/printk.git/commit/?h=for-4.16&id=6fd78a1a99c9580da49ee8f951fdce9846256375

Thanks.

	-ss