Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

apparmor: Fix & Optimize table creation from possibly unaligned memory

Source blob may come from userspace and might be unaligned.
Try to optize the copying process by avoiding unaligned memory accesses.

- Added Fixes tag
- Added "Fix &" to description as this doesn't just optimize but fixes
a potential unaligned memory access
Fixes: e6e8bf418850d ("apparmor: fix restricted endian type warnings for dfa unpack")
Signed-off-by: Helge Deller <deller@gmx.de>
[jj: remove duplicate word "convert" in comment trigger checkpatch warning]
Signed-off-by: John Johansen <john.johansen@canonical.com>

authored by

Helge Deller and committed by
John Johansen
6fc367bf 64802f73

+10 -9
+7 -5
security/apparmor/include/match.h
··· 104 104 struct table_header *tables[YYTD_ID_TSIZE]; 105 105 }; 106 106 107 - #define byte_to_byte(X) (X) 108 - 109 107 #define UNPACK_ARRAY(TABLE, BLOB, LEN, TTYPE, BTYPE, NTOHX) \ 110 108 do { \ 111 109 typeof(LEN) __i; \ 112 110 TTYPE *__t = (TTYPE *) TABLE; \ 113 111 BTYPE *__b = (BTYPE *) BLOB; \ 114 - for (__i = 0; __i < LEN; __i++) { \ 115 - __t[__i] = NTOHX(__b[__i]); \ 116 - } \ 112 + BUILD_BUG_ON(sizeof(TTYPE) != sizeof(BTYPE)); \ 113 + if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) \ 114 + memcpy(__t, __b, (LEN) * sizeof(BTYPE)); \ 115 + else /* copy & convert from big-endian */ \ 116 + for (__i = 0; __i < LEN; __i++) { \ 117 + __t[__i] = NTOHX(&__b[__i]); \ 118 + } \ 117 119 } while (0) 118 120 119 121 static inline size_t table_size(size_t len, size_t el_size)
+3 -4
security/apparmor/match.c
··· 67 67 table->td_flags = th.td_flags; 68 68 table->td_lolen = th.td_lolen; 69 69 if (th.td_flags == YYTD_DATA8) 70 - UNPACK_ARRAY(table->td_data, blob, th.td_lolen, 71 - u8, u8, byte_to_byte); 70 + memcpy(table->td_data, blob, th.td_lolen); 72 71 else if (th.td_flags == YYTD_DATA16) 73 72 UNPACK_ARRAY(table->td_data, blob, th.td_lolen, 74 - u16, __be16, be16_to_cpu); 73 + u16, __be16, get_unaligned_be16); 75 74 else if (th.td_flags == YYTD_DATA32) 76 75 UNPACK_ARRAY(table->td_data, blob, th.td_lolen, 77 - u32, __be32, be32_to_cpu); 76 + u32, __be32, get_unaligned_be32); 78 77 else 79 78 goto fail; 80 79 /* if table was vmalloced make sure the page tables are synced