Skip to content

Commit 5ba1071

Browse files
nums11suryasaimadhu
authored andcommitted
x86/insn, tools/x86: Fix undefined behavior due to potential unaligned accesses
Don't perform unaligned loads in __get_next() and __peek_nbyte_next() as these are forms of undefined behavior: "A pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type. If the resulting pointer is not correctly aligned for the pointed-to type, the behavior is undefined." (from http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf) These problems were identified using the undefined behavior sanitizer (ubsan) with the tools version of the code and perf test. [ bp: Massage commit message. ] Signed-off-by: Numfor Mbiziwo-Tiapo <[email protected]> Signed-off-by: Ian Rogers <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Acked-by: Masami Hiramatsu <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent d81ff5f commit 5ba1071

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

arch/x86/lib/insn.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
((insn)->next_byte + sizeof(t) + n <= (insn)->end_kaddr)
3838

3939
#define __get_next(t, insn) \
40-
({ t r = *(t*)insn->next_byte; insn->next_byte += sizeof(t); leXX_to_cpu(t, r); })
40+
({ t r; memcpy(&r, insn->next_byte, sizeof(t)); insn->next_byte += sizeof(t); leXX_to_cpu(t, r); })
4141

4242
#define __peek_nbyte_next(t, insn, n) \
43-
({ t r = *(t*)((insn)->next_byte + n); leXX_to_cpu(t, r); })
43+
({ t r; memcpy(&r, (insn)->next_byte + n, sizeof(t)); leXX_to_cpu(t, r); })
4444

4545
#define get_next(t, insn) \
4646
({ if (unlikely(!validate_next(t, insn, 0))) goto err_out; __get_next(t, insn); })

tools/arch/x86/lib/insn.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
((insn)->next_byte + sizeof(t) + n <= (insn)->end_kaddr)
3838

3939
#define __get_next(t, insn) \
40-
({ t r = *(t*)insn->next_byte; insn->next_byte += sizeof(t); leXX_to_cpu(t, r); })
40+
({ t r; memcpy(&r, insn->next_byte, sizeof(t)); insn->next_byte += sizeof(t); leXX_to_cpu(t, r); })
4141

4242
#define __peek_nbyte_next(t, insn, n) \
43-
({ t r = *(t*)((insn)->next_byte + n); leXX_to_cpu(t, r); })
43+
({ t r; memcpy(&r, (insn)->next_byte + n, sizeof(t)); leXX_to_cpu(t, r); })
4444

4545
#define get_next(t, insn) \
4646
({ if (unlikely(!validate_next(t, insn, 0))) goto err_out; __get_next(t, insn); })

0 commit comments

Comments
 (0)