History

af_packet: Fix fortified memcpy() without flex array.

Sergei Trofimovich reported a regression [0] caused by commit a0ade8404c3b ("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.

Kuniyuki Iwashima committed 3 years ago

Commit e2bca481 parent 71c299c2 files · +7 −6Browse files

Unified · Split

2 files
  1. include/uapi/linux/if_packet.h +1 −5
  2. net/packet/af_packet.c +6 −1
include/uapi/linux/if_packet.h+1−5
@@ -18,11 +18,7 @@ struct sockaddr_ll { · 17 unmodified lines
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+6−1
@@ -3607,7 +3607,12 @@ static int packet_getname(struct socket *sock, struct sockaddr *uaddr, · 3606 unmodified lines
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;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t