Skip to content

Commit 0f70fe6

Browse files
hmijailtorvalds
authored andcommitted
hexdump: fix for non-aligned buffers
A hexdump with a buf not aligned to the groupsize causes non-naturally-aligned memory accesses. This was causing a kernel panic on the processor BlackFin BF527, when such an unaligned buffer was fed by the function ubifs_scanned_corruption in fs/ubifs/scan.c . To fix this, change accesses to the contents of the buffer so they go through get_unaligned(). This change should be harmless to unaligned- access-capable architectures, and any performance hit should be anyway dwarfed by the snprintf() processing time. Signed-off-by: Horacio Mijail Antón Quiles <[email protected]> Cc: Andy Shevchenko <[email protected]> Cc: David Howells <[email protected]> Cc: Vivek Goyal <[email protected]> Cc: Joe Perches <[email protected]> Acked-by: Geert Uytterhoeven <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent b4749e9 commit 0f70fe6

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

lib/hexdump.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/ctype.h>
1212
#include <linux/kernel.h>
1313
#include <linux/export.h>
14+
#include <asm/unaligned.h>
1415

1516
const char hex_asc[] = "0123456789abcdef";
1617
EXPORT_SYMBOL(hex_asc);
@@ -139,7 +140,7 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
139140
for (j = 0; j < ngroups; j++) {
140141
ret = snprintf(linebuf + lx, linebuflen - lx,
141142
"%s%16.16llx", j ? " " : "",
142-
(unsigned long long)*(ptr8 + j));
143+
get_unaligned(ptr8 + j));
143144
if (ret >= linebuflen - lx)
144145
goto overflow1;
145146
lx += ret;
@@ -150,7 +151,7 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
150151
for (j = 0; j < ngroups; j++) {
151152
ret = snprintf(linebuf + lx, linebuflen - lx,
152153
"%s%8.8x", j ? " " : "",
153-
*(ptr4 + j));
154+
get_unaligned(ptr4 + j));
154155
if (ret >= linebuflen - lx)
155156
goto overflow1;
156157
lx += ret;
@@ -161,7 +162,7 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
161162
for (j = 0; j < ngroups; j++) {
162163
ret = snprintf(linebuf + lx, linebuflen - lx,
163164
"%s%4.4x", j ? " " : "",
164-
*(ptr2 + j));
165+
get_unaligned(ptr2 + j));
165166
if (ret >= linebuflen - lx)
166167
goto overflow1;
167168
lx += ret;

0 commit comments

Comments
 (0)