Re: [PATCH] printk: drop redundant devkmsg_log_str memsets

From: Steven Rostedt
Date: Fri Jan 19 2018 - 09:17:22 EST


On Fri, 19 Jan 2018 13:39:01 +0900
Sergey Senozhatsky <sergey.senozhatsky.work@xxxxxxxxx> wrote:

> We copy in null terminated strings "on" and "off", no
> need to zero out devkmsg_log_str in control_devkmsg().
>
> Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@xxxxxxxxx>
> ---
> kernel/printk/printk.c | 11 ++++-------
> 1 file changed, 4 insertions(+), 7 deletions(-)
>
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> index d70927c384f3..9faddcfd3994 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -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. */
>
> /*

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


Reviewed-by: Steven Rostedt (VMware) <rostedt@xxxxxxxxxxx>

-- Steve