Skip to content

Commit 8664240

Browse files
mstarovodavem330
authored andcommitted
net: atlantic: add alignment checks in hw_atl2_utils_fw.c
This patch adds alignment checks in all the helper macros in hw_atl2_utils_fw.c These alignment checks are compile-time, so runtime is not affected. All these helper macros assume the length to be aligned (multiple of 4). If it's not aligned, then there might be issues, e.g. stack corruption. Signed-off-by: Mark Starovoytov <[email protected]> Signed-off-by: Igor Russkikh <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 6ec9922 commit 8664240

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

drivers/net/ethernet/aquantia/atlantic/hw_atl2/hw_atl2_utils_fw.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,29 @@
1616
#define AQ_A2_FW_READ_TRY_MAX 1000
1717

1818
#define hw_atl2_shared_buffer_write(HW, ITEM, VARIABLE) \
19+
{\
20+
BUILD_BUG_ON_MSG((offsetof(struct fw_interface_in, ITEM) % \
21+
sizeof(u32)) != 0,\
22+
"Unaligned write " # ITEM);\
23+
BUILD_BUG_ON_MSG((sizeof(VARIABLE) % sizeof(u32)) != 0,\
24+
"Unaligned write length " # ITEM);\
1925
hw_atl2_mif_shared_buf_write(HW,\
2026
(offsetof(struct fw_interface_in, ITEM) / sizeof(u32)),\
21-
(u32 *)&(VARIABLE), sizeof(VARIABLE) / sizeof(u32))
27+
(u32 *)&(VARIABLE), sizeof(VARIABLE) / sizeof(u32));\
28+
}
2229

2330
#define hw_atl2_shared_buffer_get(HW, ITEM, VARIABLE) \
31+
{\
32+
BUILD_BUG_ON_MSG((offsetof(struct fw_interface_in, ITEM) % \
33+
sizeof(u32)) != 0,\
34+
"Unaligned get " # ITEM);\
35+
BUILD_BUG_ON_MSG((sizeof(VARIABLE) % sizeof(u32)) != 0,\
36+
"Unaligned get length " # ITEM);\
2437
hw_atl2_mif_shared_buf_get(HW, \
2538
(offsetof(struct fw_interface_in, ITEM) / sizeof(u32)),\
2639
(u32 *)&(VARIABLE), \
27-
sizeof(VARIABLE) / sizeof(u32))
40+
sizeof(VARIABLE) / sizeof(u32));\
41+
}
2842

2943
/* This should never be used on non atomic fields,
3044
* treat any > u32 read as non atomic.
@@ -33,7 +47,9 @@
3347
{\
3448
BUILD_BUG_ON_MSG((offsetof(struct fw_interface_out, ITEM) % \
3549
sizeof(u32)) != 0,\
36-
"Non aligned read " # ITEM);\
50+
"Unaligned read " # ITEM);\
51+
BUILD_BUG_ON_MSG((sizeof(VARIABLE) % sizeof(u32)) != 0,\
52+
"Unaligned read length " # ITEM);\
3753
BUILD_BUG_ON_MSG(sizeof(VARIABLE) > sizeof(u32),\
3854
"Non atomic read " # ITEM);\
3955
hw_atl2_mif_shared_buf_read(HW, \
@@ -42,10 +58,18 @@
4258
}
4359

4460
#define hw_atl2_shared_buffer_read_safe(HW, ITEM, DATA) \
61+
({\
62+
BUILD_BUG_ON_MSG((offsetof(struct fw_interface_out, ITEM) % \
63+
sizeof(u32)) != 0,\
64+
"Unaligned read_safe " # ITEM);\
65+
BUILD_BUG_ON_MSG((sizeof(((struct fw_interface_out *)0)->ITEM) % \
66+
sizeof(u32)) != 0,\
67+
"Unaligned read_safe length " # ITEM);\
4568
hw_atl2_shared_buffer_read_block((HW), \
4669
(offsetof(struct fw_interface_out, ITEM) / sizeof(u32)),\
4770
sizeof(((struct fw_interface_out *)0)->ITEM) / sizeof(u32),\
48-
(DATA))
71+
(DATA));\
72+
})
4973

5074
static int hw_atl2_shared_buffer_read_block(struct aq_hw_s *self,
5175
u32 offset, u32 dwords, void *data)

0 commit comments

Comments
 (0)