Skip to content

Commit e2bca48

Browse files
q2venPaolo Abeni
authored andcommitted
af_packet: Fix fortified memcpy() without flex array.
Sergei Trofimovich reported a regression [0] caused by commit a0ade84 ("af_packet: Fix warning of fortified memcpy() in packet_getname()."). It introduced a flex array sll_addr_flex in struct sockaddr_ll as a union-ed member with sll_addr to work around the fortified memcpy() check. However, a userspace program uses a struct that has struct sockaddr_ll in the middle, where a flex array is illegal to exist. include/linux/if_packet.h:24:17: error: flexible array member 'sockaddr_ll::<unnamed union>::<unnamed struct>::sll_addr_flex' not at end of 'struct packet_info_t' 24 | __DECLARE_FLEX_ARRAY(unsigned char, sll_addr_flex); | ^~~~~~~~~~~~~~~~~~~~ To fix the regression, let's go back to the first attempt [1] telling memcpy() the actual size of the array. Reported-by: Sergei Trofimovich <slyich@gmail.com> Closes: NixOS/nixpkgs#252587 (comment) [0] Link: https://lore.kernel.org/netdev/20230720004410.87588-3-kuniyu@amazon.com/ [1] Fixes: a0ade84 ("af_packet: Fix warning of fortified memcpy() in packet_getname().") Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com> Link: https://lore.kernel.org/r/20231009153151.75688-1-kuniyu@amazon.com Signed-off-by: Paolo Abeni <pabeni@redhat.com>
1 parent 71c299c commit e2bca48

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

include/uapi/linux/if_packet.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ struct sockaddr_ll {
1818
unsigned short sll_hatype;
1919
unsigned char sll_pkttype;
2020
unsigned char sll_halen;
21-
union {
22-
unsigned char sll_addr[8];
23-
/* Actual length is in sll_halen. */
24-
__DECLARE_FLEX_ARRAY(unsigned char, sll_addr_flex);
25-
};
21+
unsigned char sll_addr[8];
2622
};
2723

2824
/* Packet types */

net/packet/af_packet.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3607,7 +3607,12 @@ static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
36073607
if (dev) {
36083608
sll->sll_hatype = dev->type;
36093609
sll->sll_halen = dev->addr_len;
3610-
memcpy(sll->sll_addr_flex, dev->dev_addr, dev->addr_len);
3610+
3611+
/* Let __fortify_memcpy_chk() know the actual buffer size. */
3612+
memcpy(((struct sockaddr_storage *)sll)->__data +
3613+
offsetof(struct sockaddr_ll, sll_addr) -
3614+
offsetofend(struct sockaddr_ll, sll_family),
3615+
dev->dev_addr, dev->addr_len);
36113616
} else {
36123617
sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
36133618
sll->sll_halen = 0;

0 commit comments

Comments
 (0)