The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Sakari Ailus <sakari.ailus@linux.intel.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Petr Mladek <pmladek@suse.com>,
	linux-kernel@vger.kernel.org,
	Steven Rostedt <rostedt@goodmis.org>,
	Sergey Senozhatsky <senozhatsky@chromium.org>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Nick Desaulniers <ndesaulniers@google.com>
Subject: Re: [PATCH v1 1/1] vsprintf: Fix potential unaligned access
Date: Tue, 11 Jan 2022 00:12:46 +0200	[thread overview]
Message-ID: <YdyvXq8D2jsiM47E@paasikivi.fi.intel.com> (raw)
In-Reply-To: <20220110205049.11696-1-andriy.shevchenko@linux.intel.com>

Hi Andy,

On Mon, Jan 10, 2022 at 10:50:49PM +0200, Andy Shevchenko wrote:
> The %p4cc specifier in some cases might get an unaligned pointer.
> Due to this we need to make copy to local variable once to avoid
> potential crashes on some architectures due to improper access.

I guess this problem exists virtually everywhere where pointers are being
handled: the pointer could be unaligned. Does this even address the false
positive compiler warning?

> 
> Fixes: af612e43de6d ("lib/vsprintf: Add support for printing V4L2 and DRM fourccs")
> Cc: Sakari Ailus <sakari.ailus@linux.intel.com>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>  lib/vsprintf.c | 12 +++++++-----
>  1 file changed, 7 insertions(+), 5 deletions(-)
> 
> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> index c130dcaca5e2..b02f01366acb 100644
> --- a/lib/vsprintf.c
> +++ b/lib/vsprintf.c
> @@ -49,6 +49,7 @@
>  
>  #include <asm/page.h>		/* for PAGE_SIZE */
>  #include <asm/byteorder.h>	/* cpu_to_le16 */
> +#include <asm/unaligned.h>
>  
>  #include <linux/string_helpers.h>
>  #include "kstrtox.h"
> @@ -1761,7 +1762,7 @@ char *fourcc_string(char *buf, char *end, const u32 *fourcc,
>  	char output[sizeof("0123 little-endian (0x01234567)")];
>  	char *p = output;
>  	unsigned int i;
> -	u32 val;
> +	u32 orig, val;
>  
>  	if (fmt[1] != 'c' || fmt[2] != 'c')
>  		return error_string(buf, end, "(%p4?)", spec);
> @@ -1769,21 +1770,22 @@ char *fourcc_string(char *buf, char *end, const u32 *fourcc,
>  	if (check_pointer(&buf, end, fourcc, spec))
>  		return buf;
>  
> -	val = *fourcc & ~BIT(31);
> +	orig = get_unaligned(fourcc);
> +	val = orig & ~BIT(31);
>  
> -	for (i = 0; i < sizeof(*fourcc); i++) {
> +	for (i = 0; i < sizeof(u32); i++) {
>  		unsigned char c = val >> (i * 8);
>  
>  		/* Print non-control ASCII characters as-is, dot otherwise */
>  		*p++ = isascii(c) && isprint(c) ? c : '.';
>  	}
>  
> -	strcpy(p, *fourcc & BIT(31) ? " big-endian" : " little-endian");
> +	strcpy(p, orig & BIT(31) ? " big-endian" : " little-endian");
>  	p += strlen(p);
>  
>  	*p++ = ' ';
>  	*p++ = '(';
> -	p = special_hex_number(p, output + sizeof(output) - 2, *fourcc, sizeof(u32));
> +	p = special_hex_number(p, output + sizeof(output) - 2, orig, sizeof(u32));
>  	*p++ = ')';
>  	*p = '\0';
>  

-- 
Kind regards,

Sakari Ailus

  reply	other threads:[~2022-01-10 22:12 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-10 20:50 [PATCH v1 1/1] vsprintf: Fix potential unaligned access Andy Shevchenko
2022-01-10 22:12 ` Sakari Ailus [this message]
2022-01-11 10:10   ` Andy Shevchenko
2022-01-11 10:54     ` Sakari Ailus
2022-01-11 11:22       ` Andy Shevchenko
2022-01-11 14:13         ` Sakari Ailus
2022-01-11 14:18           ` Andy Shevchenko
2022-01-11 14:57 ` Petr Mladek
2022-01-11 15:09   ` Andy Shevchenko

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=YdyvXq8D2jsiM47E@paasikivi.fi.intel.com \
    --to=sakari.ailus@linux.intel.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=ndesaulniers@google.com \
    --cc=pmladek@suse.com \
    --cc=rostedt@goodmis.org \
    --cc=senozhatsky@chromium.org \
    /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