All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 1/2] vsprintf: Fix potential unaligned access
@ 2022-01-27 18:12 Andy Shevchenko
  2022-01-27 18:12 ` [PATCH v4 2/2] vsprintf: Move space out of string literals in fourcc_string() Andy Shevchenko
  2022-02-10 13:39 ` [PATCH v4 1/2] vsprintf: Fix potential unaligned access Petr Mladek
  0 siblings, 2 replies; 4+ messages in thread
From: Andy Shevchenko @ 2022-01-27 18:12 UTC (permalink / raw)
  To: Petr Mladek, linux-kernel
  Cc: Steven Rostedt, Sergey Senozhatsky, Andy Shevchenko,
	Rasmus Villemoes, Sakari Ailus

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.

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>
Reviewed-by: Petr Mladek <pmladek@suse.com>
---
v4: no changes
 lib/vsprintf.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 61528094ec87..4e8f3e9acb99 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"
@@ -1762,7 +1763,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);
@@ -1770,21 +1771,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';
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v4 2/2] vsprintf: Move space out of string literals in fourcc_string()
  2022-01-27 18:12 [PATCH v4 1/2] vsprintf: Fix potential unaligned access Andy Shevchenko
@ 2022-01-27 18:12 ` Andy Shevchenko
  2022-01-28  8:31   ` Petr Mladek
  2022-02-10 13:39 ` [PATCH v4 1/2] vsprintf: Fix potential unaligned access Petr Mladek
  1 sibling, 1 reply; 4+ messages in thread
From: Andy Shevchenko @ 2022-01-27 18:12 UTC (permalink / raw)
  To: Petr Mladek, linux-kernel
  Cc: Steven Rostedt, Sergey Senozhatsky, Andy Shevchenko,
	Rasmus Villemoes, Sakari Ailus

The literals "big-endian" and "little-endian" may be potentially
occurred in other places. Dropping space allows linker to
merge them by using only a single copy.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Acked-by: Sakari Ailus <sakari.ailus@linux.intel.com>
---
v4: dropped stpcpy() use (Kees), replaced "compress" with merge (Nick)
 lib/vsprintf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 4e8f3e9acb99..a1babe5e07d1 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1781,7 +1781,8 @@ char *fourcc_string(char *buf, char *end, const u32 *fourcc,
 		*p++ = isascii(c) && isprint(c) ? c : '.';
 	}
 
-	strcpy(p, orig & BIT(31) ? " big-endian" : " little-endian");
+	*p++ = ' ';
+	strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian");
 	p += strlen(p);
 
 	*p++ = ' ';
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v4 2/2] vsprintf: Move space out of string literals in fourcc_string()
  2022-01-27 18:12 ` [PATCH v4 2/2] vsprintf: Move space out of string literals in fourcc_string() Andy Shevchenko
@ 2022-01-28  8:31   ` Petr Mladek
  0 siblings, 0 replies; 4+ messages in thread
From: Petr Mladek @ 2022-01-28  8:31 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: linux-kernel, Steven Rostedt, Sergey Senozhatsky,
	Rasmus Villemoes, Sakari Ailus

On Thu 2022-01-27 20:12:33, Andy Shevchenko wrote:
> The literals "big-endian" and "little-endian" may be potentially
> occurred in other places. Dropping space allows linker to
> merge them by using only a single copy.
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Acked-by: Sakari Ailus <sakari.ailus@linux.intel.com>

Reviewed-by: Petr Mladek <pmladek@suse.com>

Best Regards,
Petr

PS: I have vacation the following week and will have limited internet
    access. I am going to push this patchset after I am back
    unless there are some complains.
    

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4 1/2] vsprintf: Fix potential unaligned access
  2022-01-27 18:12 [PATCH v4 1/2] vsprintf: Fix potential unaligned access Andy Shevchenko
  2022-01-27 18:12 ` [PATCH v4 2/2] vsprintf: Move space out of string literals in fourcc_string() Andy Shevchenko
@ 2022-02-10 13:39 ` Petr Mladek
  1 sibling, 0 replies; 4+ messages in thread
From: Petr Mladek @ 2022-02-10 13:39 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: linux-kernel, Steven Rostedt, Sergey Senozhatsky,
	Rasmus Villemoes, Sakari Ailus

On Thu 2022-01-27 20:12:32, 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.
> 
> 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>
> Reviewed-by: Petr Mladek <pmladek@suse.com>

Both patches have been committed into printk/linux.git,
branch for-5.18-vsprintf-fourcc-fixup .

Best Regards,
Petr

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-02-10 13:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-27 18:12 [PATCH v4 1/2] vsprintf: Fix potential unaligned access Andy Shevchenko
2022-01-27 18:12 ` [PATCH v4 2/2] vsprintf: Move space out of string literals in fourcc_string() Andy Shevchenko
2022-01-28  8:31   ` Petr Mladek
2022-02-10 13:39 ` [PATCH v4 1/2] vsprintf: Fix potential unaligned access Petr Mladek

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.