net: Fix error/warning by -fstrict-flex-arrays=3
Subsystems (5)
cover net: Fix error/warning by -fstrict-flex-arrays=3
df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3") started applying strict rules for standard string functions (strlen(), memcpy(), etc.) if CONFIG_FORTIFY_SOURCE=y.
This series fixes two false positives caught by syzkaller.
Kuniyuki Iwashima (2):
af_unix: Fix fortify_panic() in unix_bind_bsd().
af_packet: Fix warning of fortified memcpy() in packet_getname().
net/packet/af_packet.c | 5 ++++- net/unix/af_unix.c | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-)
Signature · 1 line
2.30.2
PATCH 1/2 af_unix: Fix fortify_panic() in unix_bind_bsd().
syzkaller found a bug in unix_bind_bsd() [0]. We can reproduce it by bind()ing a socket on a path with length 108.
108 is the size of sun_addr of struct sockaddr_un and is the maximum valid length for the pathname socket. When calling bind(), we use struct sockaddr_storage as the actual buffer size, so terminating sun_addr[108] with null is legitimate.
However, strlen(sunaddr) for such a case causes fortify_panic() if CONFIG_FORTIFY_SOURCE=y. __fortify_strlen() has no idea about the actual buffer size and takes 108 as overflow, although 108 still fits in struct sockaddr_storage.
Let's make __fortify_strlen() recognise the actual buffer size.
[0]:
detected buffer overflow in __fortify_strlen
kernel BUG at lib/string_helpers.c:1031!
Internal error: Oops - BUG: 00000000f2000800 [#1] PREEMPT SMP Modules linked in: CPU: 0 PID: 255 Comm: syz-executor296 Not tainted 6.5.0-rc1-00330-g60cc1f7d0605 #4 Hardware name: linux,dummy-virt (DT) pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--) pc : fortify_panic+0x1c/0x20 lib/string_helpers.c:1030 lr : fortify_panic+0x1c/0x20 lib/string_helpers.c:1030
sp : ffff800089817af0
x29: ffff800089817af0 x28: ffff800089817b40 x27: 1ffff00011302f68
x26: 000000000000006e x25: 0000000000000012 x24: ffff800087e60140
x23: dfff800000000000 x22: ffff800089817c20 x21: ffff800089817c8e
x20: 000000000000006c x19: ffff00000c323900 x18: ffff800086ab1630
x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000001
x14: 1ffff00011302eb8 x13: 0000000000000000 x12: 0000000000000000
x11: 0000000000000000 x10: 0000000000000000 x9 : 64a26b65474d2a00 x8 : 64a26b65474d2a00 x7 : 0000000000000001 x6 : 0000000000000001 x5 : ffff800089817438 x4 : ffff800086ac99e0 x3 : ffff800080f19e8c x2 : 0000000000000001 x1 : 0000000100000000 x0 : 000000000000002c
Call trace: fortify_panic+0x1c/0x20 lib/string_helpers.c:1030 _Z16__fortify_strlenPKcU25pass_dynamic_object_size1 include/linux/fortify-string.h:217 [inline] unix_bind_bsd net/unix/af_unix.c:1212 [inline] unix_bind+0xba8/0xc58 net/unix/af_unix.c:1326 __sys_bind+0x1ac/0x248 net/socket.c:1792 __do_sys_bind net/socket.c:1803 [inline] __se_sys_bind net/socket.c:1801 [inline] __arm64_sys_bind+0x7c/0x94 net/socket.c:1801 __invoke_syscall arch/arm64/kernel/syscall.c:38 [inline] invoke_syscall+0x98/0x2c0 arch/arm64/kernel/syscall.c:52 el0_svc_common+0x134/0x240 arch/arm64/kernel/syscall.c:139 do_el0_svc+0x64/0x198 arch/arm64/kernel/syscall.c:188 el0_svc+0x2c/0x7c arch/arm64/kernel/entry-common.c:647 el0t_64_sync_handler+0x84/0xfc arch/arm64/kernel/entry-common.c:665 el0t_64_sync+0x190/0x194 arch/arm64/kernel/entry.S:591 Code: aa0003e1 d0000e80 91030000 97ffc91a (d4210000)
--- net/unix/af_unix.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
net/unix/af_unix.c +13 -3
@@ -302,6 +302,18 @@ static void unix_mkname_bsd(struct sockaddr_un *sunaddr, int addr_len) ((char *)sunaddr)[addr_len] =0 ; } +static int unix_strlen_bsd (struct sockaddr_un *sunaddr) +{ +/* Don't pass sunaddr->sun_path to strlen(). Otherwise, the +* max valid length UNIX_PATH_MAX (108) will cause panic if +* CONFIG_FORTIFY_SOURCE=y. Let __fortify_strlen() know that +* the actual buffer is struct sockaddr_storage and that 108 +* is within __data[]. See also: unix_mkname_bsd(). +*/ +return strlen (((struct sockaddr_storage *)sunaddr)->__data) + + offsetof(struct sockaddr_un , sun_path) +1 ; +} +static void __unix_remove_socket (struct sock *sk) {sk_del_node_init (sk);
@@ -1209,9 +1221,7 @@ static int unix_bind_bsd(struct sock *sk, struct sockaddr_un *sunaddr,int err;unix_mkname_bsd (sunaddr, addr_len); - addr_len =strlen (sunaddr->sun_path) + - offsetof(struct sockaddr_un , sun_path) +1 ; - + addr_len =unix_strlen_bsd (sunaddr); addr =unix_create_addr (sunaddr, addr_len);if (!addr)return -ENOMEM ;
Signature · 1 line
2.30.2
RE: [PATCH v1 net 1/2] af_unix: Fix fortify_panic() in unix_bind_bsd().
Quoted 54 lines · Kuniyuki Iwashima
syzkaller found a bug in
unix_bind_bsd()[0]. We can reproduce it by bind()ing a socket on a path with length 108.108 is the size of
sun_addrof structsockaddr_unand is the maximum valid length for the pathname socket. When callingbind(), we use structsockaddr_storageas the actual buffer size, so terminating sun_addr[108] with null is legitimate.However, strlen(sunaddr) for such a case causes
fortify_panic()ifCONFIG_FORTIFY_SOURCE=y.__fortify_strlen()has no idea about the actual buffer size and takes 108 as overflow, although 108 still fits in structsockaddr_storage.Let's make
__fortify_strlen()recognise the actual buffer size.[0]:
detected buffer overflow in__fortify_strlen
kernel BUG atlib/string_helpers.c:1031!Internal error: Oops - BUG: 00000000f2000800 [#1] PREEMPT SMP Modules linked in: CPU: 0 PID: 255 Comm: syz-executor296 Not tainted 6.5.0-rc1-00330-g60cc1f7d0605 #4 Hardware name: linux,dummy-virt (DT) pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--) pc : fortify_panic+0x1c/0x20 lib/string_helpers.c:1030 lr : fortify_panic+0x1c/0x20 lib/string_helpers.c:1030sp :
ffff800089817af0
x29:ffff800089817af0x28:ffff800089817b40x27:1ffff00011302f68
x26:000000000000006ex25: 0000000000000012 x24:ffff800087e60140
x23:dfff800000000000x22:ffff800089817c20x21:ffff800089817c8e
x20:000000000000006cx19:ffff00000c323900x18:ffff800086ab1630
x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000001
x14:1ffff00011302eb8x13: 0000000000000000 x12: 0000000000000000
x11: 0000000000000000 x10: 0000000000000000 x9 :64a26b65474d2a00x8 :64a26b65474d2a00x7 : 0000000000000001 x6 : 0000000000000001 x5 :ffff800089817438x4 :ffff800086ac99e0x3 :ffff800080f19e8cx2 : 0000000000000001 x1 : 0000000100000000 x0 :000000000000002cCall trace: fortify_panic+0x1c/0x20 lib/string_helpers.c:1030 _Z16__fortify_strlenPKcU25pass_dynamic_object_size1 include/linux/fortify-string.h:217 [inline] unix_bind_bsd net/unix/af_unix.c:1212 [inline] unix_bind+0xba8/0xc58 net/unix/af_unix.c:1326 __sys_bind+0x1ac/0x248 net/socket.c:1792 __do_sys_bind net/socket.c:1803 [inline] __se_sys_bind net/socket.c:1801 [inline] __arm64_sys_bind+0x7c/0x94 net/socket.c:1801 __invoke_syscall arch/arm64/kernel/syscall.c:38 [inline] invoke_syscall+0x98/0x2c0 arch/arm64/kernel/syscall.c:52 el0_svc_common+0x134/0x240 arch/arm64/kernel/syscall.c:139 do_el0_svc+0x64/0x198 arch/arm64/kernel/syscall.c:188 el0_svc+0x2c/0x7c arch/arm64/kernel/entry-common.c:647 el0t_64_sync_handler+0x84/0xfc arch/arm64/kernel/entry-common.c:665 el0t_64_sync+0x190/0x194 arch/arm64/kernel/entry.S:591 Code: aa0003e1 d0000e80 91030000 97ffc91a (d4210000)
Quoted 3 lines
Fixes df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")Reported-by syzkaller <[email protected]>Signed-off-by Kuniyuki Iwashima <[email protected]>
The extensive comments are really helpful to understand what's going on.
An alternative would be to just cast sunaddr to a struct sockaddr_storage *ss and use that both here and in unix_mkname_bsd? It's not immediately trivial that the caller has always actually allocated one of those. But the rest becomes self documenting.
Quoted 41 lines
--- net/unix/af_unix.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)net/unix/af_unix.c +13
-3@@ -302,6 +302,18 @@ static void unix_mkname_bsd(struct sockaddr_un *sunaddr, int addr_len) ((char *)sunaddr)[addr_len] =0 ; } +static int unix_strlen_bsd (struct sockaddr_un *sunaddr) +{ +/* Don't pass sunaddr->sun_path to strlen(). Otherwise, the +* max valid length UNIX_PATH_MAX (108) will cause panic if +* CONFIG_FORTIFY_SOURCE=y. Let __fortify_strlen() know that +* the actual buffer is struct sockaddr_storage and that 108 +* is within __data[]. See also: unix_mkname_bsd(). +*/ +return strlen (((struct sockaddr_storage *)sunaddr)->__data) + + offsetof(struct sockaddr_un , sun_path) +1 ; +} +static void __unix_remove_socket (struct sock *sk) {sk_del_node_init (sk);@@ -1209,9 +1221,7 @@ static int unix_bind_bsd(struct sock *sk, struct sockaddr_un *sunaddr,int err;unix_mkname_bsd (sunaddr, addr_len); - addr_len =strlen (sunaddr->sun_path) + - offsetof(struct sockaddr_un , sun_path) +1 ; - + addr_len =unix_strlen_bsd (sunaddr); addr =unix_create_addr (sunaddr, addr_len);if (!addr)return -ENOMEM ;Signature · 1 line
2.30.2
RE: [PATCH v1 net 1/2] af_unix: Fix fortify_panic() in unix_bind_bsd().
Quoted 8 lines · Willem de Bruijn
Kuniyuki Iwashima wrote:
The extensive comments are really helpful to understand what's going on.An alternative would be to just cast sunaddr to a struct
sockaddr_storage*ss and use that both here and inunix_mkname_bsd? It's not immediately trivial that the caller has always actually allocated one of those. But the rest becomes self documenting.
I would much prefer the internal APIs actually passed around the true sockaddr_storage pointer. This is what I did recently for NFS, for example:
https://git.kernel.org/linus/cf0d7e7f4520
-Kees
Signature · 1 line
Kees Cook
Re: [PATCH v1 net 1/2] af_unix: Fix fortify_panic() in unix_bind_bsd().
From: Kees Cook <[email protected]>
Date: Wed, 19 Jul 2023 15:34:33 -0700
Quoted 9 lines · Willem de Bruijn
Quoted 8 lines
Kuniyuki Iwashima wrote:
The extensive comments are really helpful to understand what's going on.An alternative would be to just cast sunaddr to a struct
sockaddr_storage*ss and use that both here and inunix_mkname_bsd? It's not immediately trivial that the caller has always actually allocated one of those. But the rest becomes self documenting.
Yeah, this is also my initial attempt, and I separted it because unix_find_bsd() need not to call it and I tried to separate unnecessary calls in this series (compilers might drop the unused strlen() though).
https://lore.kernel.org/netdev/[email protected]/
Quoted 3 lines
I would much prefer the internal APIs actually passed around the true
sockaddr_storagepointer. This is what I did recently for NFS, for example:
https://git.kernel.org/linus/cf0d7e7f4520
We can convert struct proto_ops and struct proto later as such if needed, but I think it's too invasive as a fix.
PATCH 2/2 af_packet: Fix warning of fortified memcpy() in packet_getname().
In reply to Kuniyuki Iwashimasyzkaller found a warning in packet_getname() [0], where we try to copy 16 bytes to sockaddr_ll.sll_addr[8].
Some devices (ip6gre, vti6, ip6tnl) have 16 bytes address expressed by struct in6_addr.
The write seems to overflow, but actually not since we use struct sockaddr_storage defined in __sys_getsockname().
To avoid the warning, we need to let __fortify_memcpy_chk() know the actual buffer size.
Another option would be to use strncpy() and limit the copied length to sizeof(sll_addr), but it will return the partial address and might break an application that passes sockaddr_storage to getsockname().
[0]:
memcpy: detected field-spanning write (size 16) of single field "sll->sll_addr" at net/packet/af_packet.c:3604 (size 8)
WARNING: CPU: 0 PID: 255 at net/packet/af_packet.c:3604 packet_getname+0x25c/0x3a0 net/packet/af_packet.c:3604 Modules linked in: CPU: 0 PID: 255 Comm: syz-executor750 Not tainted 6.5.0-rc1-00330-g60cc1f7d0605 #4 Hardware name: linux,dummy-virt (DT) pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--) pc : packet_getname+0x25c/0x3a0 net/packet/af_packet.c:3604 lr : packet_getname+0x25c/0x3a0 net/packet/af_packet.c:3604
sp : ffff800089887bc0
x29: ffff800089887bc0 x28: ffff000010f80f80 x27: 0000000000000003
x26: dfff800000000000 x25: ffff700011310f80 x24: ffff800087d55000
x23: dfff800000000000 x22: ffff800089887c2c x21: 0000000000000010
x20: ffff00000de08310 x19: ffff800089887c20 x18: ffff800086ab1630
x17: 20646c6569662065 x16: 6c676e697320666f x15: 0000000000000001
x14: 1fffe0000d56d7ca x13: 0000000000000000 x12: 0000000000000000
x11: 0000000000000000 x10: 0000000000000000 x9 : 3e60944c3da92b00 x8 : 3e60944c3da92b00 x7 : 0000000000000001 x6 : 0000000000000001 x5 : ffff8000898874f8 x4 : ffff800086ac99e0 x3 : ffff8000803f8808 x2 : 0000000000000001 x1 : 0000000100000000 x0 : 0000000000000000
Call trace: packet_getname+0x25c/0x3a0 net/packet/af_packet.c:3604 __sys_getsockname+0x168/0x24c net/socket.c:2042 __do_sys_getsockname net/socket.c:2057 [inline] __se_sys_getsockname net/socket.c:2054 [inline] __arm64_sys_getsockname+0x7c/0x94 net/socket.c:2054 __invoke_syscall arch/arm64/kernel/syscall.c:38 [inline] invoke_syscall+0x98/0x2c0 arch/arm64/kernel/syscall.c:52 el0_svc_common+0x134/0x240 arch/arm64/kernel/syscall.c:139 do_el0_svc+0x64/0x198 arch/arm64/kernel/syscall.c:188 el0_svc+0x2c/0x7c arch/arm64/kernel/entry-common.c:647 el0t_64_sync_handler+0x84/0xfc arch/arm64/kernel/entry-common.c:665 el0t_64_sync+0x190/0x194 arch/arm64/kernel/entry.S:591
--- net/packet/af_packet.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/packet/af_packet.c +4 -1
@@ -3601,7 +3601,10 @@ static int packet_getname(struct socket *sock, struct sockaddr *uaddr,if (dev) { sll->sll_hatype = dev->type; sll->sll_halen = dev->addr_len; -memcpy (sll->sll_addr, dev->dev_addr, dev->addr_len); + +/* Let __fortify_memcpy_chk() know the actual buffer size. */ +memcpy (((struct sockaddr_storage *)sll)->__data + + offsetof(struct sockaddr_ll , sll_addr), dev->dev_addr, dev->addr_len); }else { sll->sll_hatype =0 ;/* Bad: we have no ARPHRD_UNSPEC */ sll->sll_halen =0 ;
Signature · 1 line
2.30.2
Re: [PATCH v1 net 2/2] af_packet: Fix warning of fortified memcpy() in packet_getname().
From: Kuniyuki Iwashima <[email protected]>
Date: Wed, 19 Jul 2023 11:53:22 -0700
Quoted 62 lines
syzkaller found a warning in
packet_getname()[0], where we try to copy 16 bytes to sockaddr_ll.sll_addr[8].Some devices (ip6gre, vti6, ip6tnl) have 16 bytes address expressed by struct
in6_addr.The write seems to overflow, but actually not since we use struct
sockaddr_storagedefined in__sys_getsockname().To avoid the warning, we need to let
__fortify_memcpy_chk()know the actual buffer size.Another option would be to use
strncpy()and limit the copied length tosizeof(sll_addr), but it will return the partial address and might break an application that passessockaddr_storagetogetsockname().[0]:
memcpy: detected field-spanning write (size 16) of single field "sll->sll_addr" atnet/packet/af_packet.c:3604(size 8)WARNING: CPU: 0 PID: 255 at net/packet/af_packet.c:3604 packet_getname+0x25c/0x3a0 net/packet/af_packet.c:3604 Modules linked in: CPU: 0 PID: 255 Comm: syz-executor750 Not tainted 6.5.0-rc1-00330-g60cc1f7d0605 #4 Hardware name: linux,dummy-virt (DT) pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--) pc : packet_getname+0x25c/0x3a0 net/packet/af_packet.c:3604 lr : packet_getname+0x25c/0x3a0 net/packet/af_packet.c:3604sp :
ffff800089887bc0
x29:ffff800089887bc0x28:ffff000010f80f80x27: 0000000000000003
x26:dfff800000000000x25:ffff700011310f80x24:ffff800087d55000
x23:dfff800000000000x22:ffff800089887c2cx21: 0000000000000010
x20:ffff00000de08310x19:ffff800089887c20x18:ffff800086ab1630
x17:20646c6569662065x16:6c676e697320666fx15: 0000000000000001
x14:1fffe0000d56d7cax13: 0000000000000000 x12: 0000000000000000
x11: 0000000000000000 x10: 0000000000000000 x9 :3e60944c3da92b00x8 :3e60944c3da92b00x7 : 0000000000000001 x6 : 0000000000000001 x5 :ffff8000898874f8x4 :ffff800086ac99e0x3 :ffff8000803f8808x2 : 0000000000000001 x1 : 0000000100000000 x0 : 0000000000000000Call trace: packet_getname+0x25c/0x3a0 net/packet/af_packet.c:3604 __sys_getsockname+0x168/0x24c net/socket.c:2042 __do_sys_getsockname net/socket.c:2057 [inline] __se_sys_getsockname net/socket.c:2054 [inline] __arm64_sys_getsockname+0x7c/0x94 net/socket.c:2054 __invoke_syscall arch/arm64/kernel/syscall.c:38 [inline] invoke_syscall+0x98/0x2c0 arch/arm64/kernel/syscall.c:52 el0_svc_common+0x134/0x240 arch/arm64/kernel/syscall.c:139 do_el0_svc+0x64/0x198 arch/arm64/kernel/syscall.c:188 el0_svc+0x2c/0x7c arch/arm64/kernel/entry-common.c:647 el0t_64_sync_handler+0x84/0xfc arch/arm64/kernel/entry-common.c:665 el0t_64_sync+0x190/0x194 arch/arm64/kernel/entry.S:591Fixes df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")Reported-by syzkaller <[email protected]>Signed-off-by Kuniyuki Iwashima <[email protected]>--- net/packet/af_packet.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)net/packet/af_packet.c +0
-0
net/packet/af_packet.c static int packet_getname(struct socket *sock, struct sockaddr *uaddr, · line 3607
Quoted 8 lines
net/packet/af_packet.c +4
-1@@ -3601,7 +3601,10 @@ static int packet_getname(struct socket *sock, struct sockaddr *uaddr,if (dev) { sll->sll_hatype = dev->type; sll->sll_halen = dev->addr_len; -memcpy (sll->sll_addr, dev->dev_addr, dev->addr_len); + +/* Let __fortify_memcpy_chk() know the actual buffer size. */ +memcpy (((struct sockaddr_storage *)sll)->__data + + offsetof(struct sockaddr_ll , sll_addr), dev->dev_addr, dev->addr_len);
Sorry, this offset was wrong and needs minusoffsetof(struct sockaddr_ll, sll_family).
Will fix in v2.
pw-bot: cr
Quoted 5 lines
net/packet/af_packet.c +0
-0}else { sll->sll_hatype =0 ;/* Bad: we have no ARPHRD_UNSPEC */ sll->sll_halen =0 ;Signature · 1 line
2.30.2
Willem de Bruijn
maintainerRE: [PATCH v1 net 2/2] af_packet: Fix warning of fortified memcpy() in packet_getname().
In reply to Kuniyuki IwashimaQuoted 5 lines · Kuniyuki Iwashima
syzkaller found a warning in
packet_getname()[0], where we try to copy 16 bytes to sockaddr_ll.sll_addr[8].Some devices (ip6gre, vti6, ip6tnl) have 16 bytes address expressed by struct
in6_addr.
Some are even larger. MAX_ADDR_LEN == 32. I think Infiniband may use that?
Quoted 2 lines
The write seems to overflow, but actually not since we use struct
sockaddr_storagedefined in__sys_getsockname().
Which gives _K_SS_MAXSIZE == 128, minus offsetof(struct sockaddr_ll, sll_addr).
For fun, there is another caller. getsockopt SO_PEERNAME also calls sock->ops->getname, with a buffer hardcoded to 128. Should probably use sizeof(sockaddr_storage) for documentation, at least.
.. and I just noticed that that was attempted, but not completed
https://lore.kernel.org/lkml/[email protected]/
Quoted 7 lines
To avoid the warning, we need to let
__fortify_memcpy_chk()know the actual buffer size.Another option would be to use
strncpy()and limit the copied length tosizeof(sll_addr), but it will return the partial address and might break an application that passessockaddr_storagetogetsockname().
Yeah, that would break stuff.
Quoted 60 lines
[0]:
memcpy: detected field-spanning write (size 16) of single field "sll->sll_addr" atnet/packet/af_packet.c:3604(size 8)WARNING: CPU: 0 PID: 255 at net/packet/af_packet.c:3604 packet_getname+0x25c/0x3a0 net/packet/af_packet.c:3604 Modules linked in: CPU: 0 PID: 255 Comm: syz-executor750 Not tainted 6.5.0-rc1-00330-g60cc1f7d0605 #4 Hardware name: linux,dummy-virt (DT) pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--) pc : packet_getname+0x25c/0x3a0 net/packet/af_packet.c:3604 lr : packet_getname+0x25c/0x3a0 net/packet/af_packet.c:3604sp :
ffff800089887bc0
x29:ffff800089887bc0x28:ffff000010f80f80x27: 0000000000000003
x26:dfff800000000000x25:ffff700011310f80x24:ffff800087d55000
x23:dfff800000000000x22:ffff800089887c2cx21: 0000000000000010
x20:ffff00000de08310x19:ffff800089887c20x18:ffff800086ab1630
x17:20646c6569662065x16:6c676e697320666fx15: 0000000000000001
x14:1fffe0000d56d7cax13: 0000000000000000 x12: 0000000000000000
x11: 0000000000000000 x10: 0000000000000000 x9 :3e60944c3da92b00x8 :3e60944c3da92b00x7 : 0000000000000001 x6 : 0000000000000001 x5 :ffff8000898874f8x4 :ffff800086ac99e0x3 :ffff8000803f8808x2 : 0000000000000001 x1 : 0000000100000000 x0 : 0000000000000000Call trace: packet_getname+0x25c/0x3a0 net/packet/af_packet.c:3604 __sys_getsockname+0x168/0x24c net/socket.c:2042 __do_sys_getsockname net/socket.c:2057 [inline] __se_sys_getsockname net/socket.c:2054 [inline] __arm64_sys_getsockname+0x7c/0x94 net/socket.c:2054 __invoke_syscall arch/arm64/kernel/syscall.c:38 [inline] invoke_syscall+0x98/0x2c0 arch/arm64/kernel/syscall.c:52 el0_svc_common+0x134/0x240 arch/arm64/kernel/syscall.c:139 do_el0_svc+0x64/0x198 arch/arm64/kernel/syscall.c:188 el0_svc+0x2c/0x7c arch/arm64/kernel/entry-common.c:647 el0t_64_sync_handler+0x84/0xfc arch/arm64/kernel/entry-common.c:665 el0t_64_sync+0x190/0x194 arch/arm64/kernel/entry.S:591Fixes df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")Reported-by syzkaller <[email protected]>Signed-off-by Kuniyuki Iwashima <[email protected]>--- net/packet/af_packet.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)net/packet/af_packet.c +4
-1@@ -3601,7 +3601,10 @@ static int packet_getname(struct socket *sock, struct sockaddr *uaddr,if (dev) { sll->sll_hatype = dev->type; sll->sll_halen = dev->addr_len; -memcpy (sll->sll_addr, dev->dev_addr, dev->addr_len); + +/* Let __fortify_memcpy_chk() know the actual buffer size. */ +memcpy (((struct sockaddr_storage *)sll)->__data + + offsetof(struct sockaddr_ll , sll_addr), dev->dev_addr, dev->addr_len); }else { sll->sll_hatype =0 ;/* Bad: we have no ARPHRD_UNSPEC */ sll->sll_halen =0 ;Signature · 1 line
2.30.2
RE: [PATCH v1 net 2/2] af_packet: Fix warning of fortified memcpy() in packet_getname().
From: Willem de Bruijn <[email protected]>
Date: Wed, 19 Jul 2023 17:15:09 -0400
Quoted 9 lines
Quoted 5 lines · Kuniyuki Iwashima
syzkaller found a warning in
packet_getname()[0], where we try to copy 16 bytes to sockaddr_ll.sll_addr[8].Some devices (ip6gre, vti6, ip6tnl) have 16 bytes address expressed by struct
in6_addr.Some are even larger.
MAX_ADDR_LEN== 32. I think Infiniband may use that?
Exactly, I didn't know that.
Quoted 4 lines
Quoted 3 lines
The write seems to overflow, but actually not since we use struct
sockaddr_storagedefined in__sys_getsockname().
Quoted 8 lines
Which gives
_K_SS_MAXSIZE== 128, minusoffsetof(struct sockaddr_ll, sll_addr).For fun, there is another caller. getsockopt
SO_PEERNAMEalso callssock->ops->getname, with a buffer hardcoded to 128. Should probably usesizeof(sockaddr_storage)for documentation, at least... and I just noticed that that was attempted, but not completed
https://lore.kernel.org/lkml/[email protected]/
Yes, acutally my first draft had the diff below, but I dropped it because packet_getname() does not call memcpy() for SO_PEERNAME at least, and same for getpeername().
And interestingly there was a revival thread.
https://lore.kernel.org/netdev/[email protected]/
I can include this in v2 if needed.
What do you think ?
---8<---
net/core/sock.c +3 -3
@@ -1815,14 +1815,14 @@ int sk_getsockopt(struct sock *sk, int level, int optname,case SO_PEERNAME : { -char address[128 ]; +struct sockaddr_storage address; - lv = sock->ops->getname (sock, (struct sockaddr *)address,2 ); + lv = sock->ops->getname (sock, (struct sockaddr *)&address,2 );if (lv <0 )return -ENOTCONN ;if (lv < len)return -EINVAL ; -if (copy_to_sockptr (optval, address, len)) +if (copy_to_sockptr (optval, &address, len))return -EFAULT ; goto lenout; }
---8<---
Thanks!
Quoted 70 lines
Quoted 8 lines
To avoid the warning, we need to let
__fortify_memcpy_chk()know the actual buffer size.Another option would be to use
strncpy()and limit the copied length tosizeof(sll_addr), but it will return the partial address and might break an application that passessockaddr_storagetogetsockname().Yeah, that would break stuff.
Quoted 59 lines
[0]:
memcpy: detected field-spanning write (size 16) of single field "sll->sll_addr" atnet/packet/af_packet.c:3604(size 8)WARNING: CPU: 0 PID: 255 at net/packet/af_packet.c:3604 packet_getname+0x25c/0x3a0 net/packet/af_packet.c:3604 Modules linked in: CPU: 0 PID: 255 Comm: syz-executor750 Not tainted 6.5.0-rc1-00330-g60cc1f7d0605 #4 Hardware name: linux,dummy-virt (DT) pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--) pc : packet_getname+0x25c/0x3a0 net/packet/af_packet.c:3604 lr : packet_getname+0x25c/0x3a0 net/packet/af_packet.c:3604sp :
ffff800089887bc0
x29:ffff800089887bc0x28:ffff000010f80f80x27: 0000000000000003
x26:dfff800000000000x25:ffff700011310f80x24:ffff800087d55000
x23:dfff800000000000x22:ffff800089887c2cx21: 0000000000000010
x20:ffff00000de08310x19:ffff800089887c20x18:ffff800086ab1630
x17:20646c6569662065x16:6c676e697320666fx15: 0000000000000001
x14:1fffe0000d56d7cax13: 0000000000000000 x12: 0000000000000000
x11: 0000000000000000 x10: 0000000000000000 x9 :3e60944c3da92b00x8 :3e60944c3da92b00x7 : 0000000000000001 x6 : 0000000000000001 x5 :ffff8000898874f8x4 :ffff800086ac99e0x3 :ffff8000803f8808x2 : 0000000000000001 x1 : 0000000100000000 x0 : 0000000000000000Call trace: packet_getname+0x25c/0x3a0 net/packet/af_packet.c:3604 __sys_getsockname+0x168/0x24c net/socket.c:2042 __do_sys_getsockname net/socket.c:2057 [inline] __se_sys_getsockname net/socket.c:2054 [inline] __arm64_sys_getsockname+0x7c/0x94 net/socket.c:2054 __invoke_syscall arch/arm64/kernel/syscall.c:38 [inline] invoke_syscall+0x98/0x2c0 arch/arm64/kernel/syscall.c:52 el0_svc_common+0x134/0x240 arch/arm64/kernel/syscall.c:139 do_el0_svc+0x64/0x198 arch/arm64/kernel/syscall.c:188 el0_svc+0x2c/0x7c arch/arm64/kernel/entry-common.c:647 el0t_64_sync_handler+0x84/0xfc arch/arm64/kernel/entry-common.c:665 el0t_64_sync+0x190/0x194 arch/arm64/kernel/entry.S:591Fixes df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")Reported-by syzkaller <[email protected]>Signed-off-by Kuniyuki Iwashima <[email protected]>--- net/packet/af_packet.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)net/packet/af_packet.c +4
-1@@ -3601,7 +3601,10 @@ static int packet_getname(struct socket *sock, struct sockaddr *uaddr,if (dev) { sll->sll_hatype = dev->type; sll->sll_halen = dev->addr_len; -memcpy (sll->sll_addr, dev->dev_addr, dev->addr_len); + +/* Let __fortify_memcpy_chk() know the actual buffer size. */ +memcpy (((struct sockaddr_storage *)sll)->__data + + offsetof(struct sockaddr_ll , sll_addr), dev->dev_addr, dev->addr_len); }else { sll->sll_hatype =0 ;/* Bad: we have no ARPHRD_UNSPEC */ sll->sll_halen =0 ;Signature · 1 line
2.30.2
Willem de Bruijn
maintainerRE: [PATCH v1 net 2/2] af_packet: Fix warning of fortified memcpy() in packet_getname().
Quoted 13 lines
Quoted 4 lines
Quoted 3 lines
The write seems to overflow, but actually not since we use struct
sockaddr_storagedefined in__sys_getsockname().Quoted 8 lines
Which gives
_K_SS_MAXSIZE== 128, minusoffsetof(struct sockaddr_ll, sll_addr).For fun, there is another caller. getsockopt
SO_PEERNAMEalso callssock->ops->getname, with a buffer hardcoded to 128. Should probably usesizeof(sockaddr_storage)for documentation, at least... and I just noticed that that was attempted, but not completed
https://lore.kernel.org/lkml/[email protected]/
Quoted 6 lines
Yes, acutally my first draft had the diff below, but I dropped it because
packet_getname()does not callmemcpy()forSO_PEERNAMEat least, and same forgetpeername().And interestingly there was a revival thread.
https://lore.kernel.org/netdev/[email protected]/
Ah interesting :) Topical.
Quoted 17 lines
I can include this in v2 if needed.
What do you think ?---8<---
net/core/sock.c +2
-2@@ -1815,14 +1815,14 @@ int sk_getsockopt(struct sock *sk, int level, int optname,case SO_PEERNAME : { -char address[128 ]; +struct sockaddr_storage address; - lv = sock->ops->getname (sock, (struct sockaddr *)address,2 ); + lv = sock->ops->getname (sock, (struct sockaddr *)&address,2 );
net/core/sock.c int sk_getsockopt(struct sock *sk, int level, int optname,
Quoted 10 lines
net/core/sock.c +1
-2if (lv <0 )return -ENOTCONN ;if (lv < len)return -EINVAL ; -if (copy_to_sockptr (optval, address, len)) +if (copy_to_sockptr (optval, &address, len))return -EFAULT ; goto lenout; } ---8<---
I agree that it's a worthwhile change. I think it should be an independent commit. And since it does not fix a bug, target net-next.
RE: [PATCH v1 net 2/2] af_packet: Fix warning of fortified memcpy() in packet_getname().
From: Willem de Bruijn
Quoted 39 lines
Sent: 19 July 2023 22:34
Quoted 13 lines
Quoted 4 lines
Quoted 3 lines
The write seems to overflow, but actually not since we use struct
sockaddr_storagedefined in__sys_getsockname().Quoted 8 lines
Which gives
_K_SS_MAXSIZE== 128, minusoffsetof(struct sockaddr_ll, sll_addr).For fun, there is another caller. getsockopt
SO_PEERNAMEalso callssock->ops->getname, with a buffer hardcoded to 128. Should probably usesizeof(sockaddr_storage)for documentation, at least... and I just noticed that that was attempted, but not completed
https://lore.kernel.org/lkml/[email protected]/Quoted 6 lines
Yes, acutally my first draft had the diff below, but I dropped it because
packet_getname()does not callmemcpy()forSO_PEERNAMEat least, and same forgetpeername().And interestingly there was a revival thread.
https://lore.kernel.org/netdev/[email protected]/Ah interesting :) Topical.
Quoted 15 lines
I can include this in v2 if needed.
What do you think ?---8<---
net/core/sock.c +1
-1@@ -1815,14 +1815,14 @@ int sk_getsockopt(struct sock *sk, int level, int optname,case SO_PEERNAME : { -char address[128 ]; +struct sockaddr_storage address;
Quoted 15 lines
net/core/sock.c int sk_getsockopt(struct sock *sk, int level, int optname,
Quoted 12 lines
net/core/sock.c +2
-3- lv = sock->ops->getname (sock, (struct sockaddr *)address,2 ); + lv = sock->ops->getname (sock, (struct sockaddr *)&address,2 );if (lv <0 )return -ENOTCONN ;if (lv < len)return -EINVAL ; -if (copy_to_sockptr (optval, address, len)) +if (copy_to_sockptr (optval, &address, len))return -EFAULT ; goto lenout; } ---8<---I agree that it's a worthwhile change. I think it should be an independent commit. And since it does not fix a bug, target net-next.
It is potentially a bug.
There is no requirement that the compiler align 'char address[128]'. So the accesses could fault later on.
In practise it will be aligned - unless the compiler is being perverse.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
RE: [PATCH v1 net 2/2] af_packet: Fix warning of fortified memcpy() in packet_getname().
In reply to Willem de BruijnFrom: Willem de Bruijn <[email protected]>
Date: Wed, 19 Jul 2023 17:34:13 -0400
Quoted 36 lines
Quoted 12 lines
Quoted 11 lines
Quoted 2 lines
The write seems to overflow, but actually not since we use struct
sockaddr_storagedefined in__sys_getsockname().Which gives
_K_SS_MAXSIZE== 128, minusoffsetof(struct sockaddr_ll, sll_addr).For fun, there is another caller. getsockopt
SO_PEERNAMEalso callssock->ops->getname, with a buffer hardcoded to 128. Should probably usesizeof(sockaddr_storage)for documentation, at least... and I just noticed that that was attempted, but not completed
https://lore.kernel.org/lkml/[email protected]/Quoted 6 lines
Yes, acutally my first draft had the diff below, but I dropped it because
packet_getname()does not callmemcpy()forSO_PEERNAMEat least, and same forgetpeername().And interestingly there was a revival thread.
https://lore.kernel.org/netdev/[email protected]/Ah interesting :) Topical.
Quoted 15 lines
I can include this in v2 if needed.
What do you think ?---8<---
net/core/sock.c +1
-1@@ -1815,14 +1815,14 @@ int sk_getsockopt(struct sock *sk, int level, int optname,case SO_PEERNAME : { -char address[128 ]; +struct sockaddr_storage address;
Quoted 15 lines
net/core/sock.c int sk_getsockopt(struct sock *sk, int level, int optname,
Quoted 12 lines
net/core/sock.c +2
-3- lv = sock->ops->getname (sock, (struct sockaddr *)address,2 ); + lv = sock->ops->getname (sock, (struct sockaddr *)&address,2 );if (lv <0 )return -ENOTCONN ;if (lv < len)return -EINVAL ; -if (copy_to_sockptr (optval, address, len)) +if (copy_to_sockptr (optval, &address, len))return -EFAULT ; goto lenout; } ---8<---I agree that it's a worthwhile change. I think it should be an independent commit. And since it does not fix a bug, target net-next.
Sure, will post a patch to net-next later.