public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Kees Cook <keescook@chromium.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Al Viro <viro@zeniv.linux.org.uk>,
	Muchun Song <muchun.song@linux.dev>,
	linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH] seq_file: seq_show_option_n() is used for precise sizes
Date: Thu, 27 Jul 2023 16:02:42 +0300	[thread overview]
Message-ID: <ZMJq8nzPevdSon94@smile.fi.intel.com> (raw)
In-Reply-To: <20230726215957.never.619-kees@kernel.org>

On Wed, Jul 26, 2023 at 02:59:57PM -0700, Kees Cook wrote:
> When seq_show_option_n() is used, it is for non-string memory that
> happens to be printable bytes. As such, we must use memcpy() to copy the
> bytes and then explicitly NUL-terminate the result.

FWIW,
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

> Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Al Viro <viro@zeniv.linux.org.uk>
> Cc: Muchun Song <muchun.song@linux.dev>
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  include/linux/seq_file.h | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h
> index bd023dd38ae6..386ab580b839 100644
> --- a/include/linux/seq_file.h
> +++ b/include/linux/seq_file.h
> @@ -249,18 +249,19 @@ static inline void seq_show_option(struct seq_file *m, const char *name,
>  
>  /**
>   * seq_show_option_n - display mount options with appropriate escapes
> - *		       where @value must be a specific length.
> + *		       where @value must be a specific length (i.e.
> + *		       not NUL-terminated).
>   * @m: the seq_file handle
>   * @name: the mount option name
>   * @value: the mount option name's value, cannot be NULL
> - * @length: the length of @value to display
> + * @length: the exact length of @value to display, must be constant expression
>   *
>   * This is a macro since this uses "length" to define the size of the
>   * stack buffer.
>   */
>  #define seq_show_option_n(m, name, value, length) {	\
>  	char val_buf[length + 1];			\
> -	strncpy(val_buf, value, length);		\
> +	memcpy(val_buf, value, length);			\
>  	val_buf[length] = '\0';				\
>  	seq_show_option(m, name, val_buf);		\
>  }
> -- 
> 2.34.1
> 

-- 
With Best Regards,
Andy Shevchenko



  reply	other threads:[~2023-07-27 13:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-26 21:59 [PATCH] seq_file: seq_show_option_n() is used for precise sizes Kees Cook
2023-07-27 13:02 ` Andy Shevchenko [this message]
2023-07-27 15:49 ` Kees Cook

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZMJq8nzPevdSon94@smile.fi.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=keescook@chromium.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=muchun.song@linux.dev \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox