Skip to content
/ linux Public

Commit 47e351d

Browse files
hdellerSasha Levin
authored andcommitted
apparmor: Fix & Optimize table creation from possibly unaligned memory
[ Upstream commit 6fc367b ] 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: e6e8bf4 ("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> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent ec737e7 commit 47e351d

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

security/apparmor/include/match.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,18 @@ struct aa_dfa {
102102
struct table_header *tables[YYTD_ID_TSIZE];
103103
};
104104

105-
#define byte_to_byte(X) (X)
106-
107105
#define UNPACK_ARRAY(TABLE, BLOB, LEN, TTYPE, BTYPE, NTOHX) \
108106
do { \
109107
typeof(LEN) __i; \
110108
TTYPE *__t = (TTYPE *) TABLE; \
111109
BTYPE *__b = (BTYPE *) BLOB; \
112-
for (__i = 0; __i < LEN; __i++) { \
113-
__t[__i] = NTOHX(__b[__i]); \
114-
} \
110+
BUILD_BUG_ON(sizeof(TTYPE) != sizeof(BTYPE)); \
111+
if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) \
112+
memcpy(__t, __b, (LEN) * sizeof(BTYPE)); \
113+
else /* copy & convert from big-endian */ \
114+
for (__i = 0; __i < LEN; __i++) { \
115+
__t[__i] = NTOHX(&__b[__i]); \
116+
} \
115117
} while (0)
116118

117119
static inline size_t table_size(size_t len, size_t el_size)

security/apparmor/match.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,13 @@ static struct table_header *unpack_table(char *blob, size_t bsize)
6767
table->td_flags = th.td_flags;
6868
table->td_lolen = th.td_lolen;
6969
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);
7271
else if (th.td_flags == YYTD_DATA16)
7372
UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
74-
u16, __be16, be16_to_cpu);
73+
u16, __be16, get_unaligned_be16);
7574
else if (th.td_flags == YYTD_DATA32)
7675
UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
77-
u32, __be32, be32_to_cpu);
76+
u32, __be32, get_unaligned_be32);
7877
else
7978
goto fail;
8079
/* if table was vmalloced make sure the page tables are synced

0 commit comments

Comments
 (0)