net: zero the tail of ha->addr when allocating address list entries
Subsystems (3)
PATCH v3 1/1 net: zero the tail of ha->addr when allocating address list entries
__hw_addr_create() allocates entries with kmalloc() and initializes only addr_len bytes of ha->addr, but the dump paths emit each entry using the current dev->addr_len, so an entry created with a smaller addr_len is dumped with an uninitialized tail.
dev->addr_len can grow at runtime and nothing flushes the lists. While addr_len is 0 every addition collapses into one node, because __hw_addr_add_ex() compares zero bytes, so the test adds one zero-length multicast entry to each of 64 tun devices and switches them to ARPHRD_IP6GRE (addr_len 16). RTM_GETMULTICAST then returned 64 entries of 16 bytes, 37 with non-zero bytes and 21 starting with a kernel heap pointer (e.g. 0xffff8188_04857ee0). tun has to flush dev->uc and dev->mc in TUNSETLINK as well, which is a separate patch; qmi_wwan raw IP mode and the HDLC attach/detach paths change addr_len in the same way, so tun is not the only case.
Allocate with kzalloc() so the tail is zeroed at creation; the explicit ha->sync_cnt = 0 then becomes redundant.
Found by an automated static analysis scan (Tencent CodeBuddy Security) and triggered at runtime on v7.4-rc3 as an unprivileged user.
---
v2 -> v3:
- describe the test setup accurately: one entry on each of 64 tun devices, since all additions collapse into one node while addr_len is 0
- point Fixes: at cca8ea3b05c9, which made the leak reachable from tun
- link to v1 instead of the earlier review thread, and link to the companion tun patch
- note that other drivers change addr_len without flushing the lists
- state how the issue was found
net/core/dev_addr_lists.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/dev_addr_lists.c +1 -2
@@ -96,7 +96,7 @@ __hw_addr_create(const unsigned char *addr, int addr_len, alloc_size =sizeof (*ha);if (alloc_size <L1_CACHE_BYTES ) alloc_size =L1_CACHE_BYTES ; - ha =kmalloc (alloc_size,GFP_ATOMIC ); + ha =kzalloc (alloc_size,GFP_ATOMIC );if (!ha)return NULL ;memcpy (ha->addr, addr, addr_len);
@@ -104,7 +104,6 @@ __hw_addr_create(const unsigned char *addr, int addr_len, ha->refcount =1 ; ha->global_use = global; ha->synced = sync ?1 :0 ; - ha->sync_cnt =0 ;return ha; }
Signature · 1 line
2.43.7
Quoted 66 lines · Henry Martin
__hw_addr_create()allocates entries withkmalloc()and initializes onlyaddr_lenbytes ofha->addr, but the dump paths emit each entry using the currentdev->addr_len, so an entry created with a smalleraddr_lenis dumped with an uninitialized tail.
dev->addr_lencan grow at runtime and nothing flushes the lists. Whileaddr_lenis 0 every addition collapses into one node, because__hw_addr_add_ex()compares zero bytes, so the test adds one zero-length multicast entry to each of 64 tun devices and switches them toARPHRD_IP6GRE(addr_len16).RTM_GETMULTICASTthen returned 64 entries of 16 bytes, 37 with non-zero bytes and 21 starting with a kernel heap pointer (e.g. 0xffff8188_04857ee0). tun has to flushdev->ucanddev->mcin TUNSETLINK as well, which is a separate patch;qmi_wwanraw IP mode and the HDLC attach/detach paths changeaddr_lenin the same way, so tun is not the only case.Allocate with
kzalloc()so the tail is zeroed at creation; the explicitha->sync_cnt= 0 then becomes redundant.Found by an automated static analysis scan (Tencent CodeBuddy Security) and triggered at runtime on v7.4-rc3 as an unprivileged user.
Fixes cca8ea3b05c9 ("net: tun: set tun->dev->addr_len during TUNSETLINK processing")Reviewed-by Daniel Machon <[email protected]>Reviewed-by Nicolas Dichtel <[email protected]>Signed-off-by Henry Martin <[email protected]>---
v2 -> v3:
- describe the test setup accurately: one entry on each of 64 tun devices, since all additions collapse into one node whileaddr_lenis 0
- point Fixes: atcca8ea3b05c9, which made the leak reachable from tun
- link to v1 instead of the earlier review thread, and link to the companion tun patch
- note that other drivers changeaddr_lenwithout flushing the lists
- state how the issue was foundnet/core/dev_addr_lists.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/core/dev_addr_lists.c +1
-3@@ -96,7 +96,7 @@ __hw_addr_create(const unsigned char *addr, int addr_len, alloc_size =sizeof (*ha);if (alloc_size <L1_CACHE_BYTES ) alloc_size =L1_CACHE_BYTES ; - ha =kmalloc (alloc_size,GFP_ATOMIC ); + ha =kzalloc (alloc_size,GFP_ATOMIC );if (!ha)return NULL ;memcpy (ha->addr, addr, addr_len);@@ -104,7 +104,6 @@ __hw_addr_create(const unsigned char *addr, int addr_len, ha->refcount =1 ; ha->global_use = global; ha->synced = sync ?1 :0 ; - ha->sync_cnt =0 ;return ha; } --
Quoted 2 lines
2.43.7
Quoted 65 lines · Yuyang Huang
Quoted 63 lines · Henry Martin
__hw_addr_create()allocates entries withkmalloc()and initializes onlyaddr_lenbytes ofha->addr, but the dump paths emit each entry using the currentdev->addr_len, so an entry created with a smalleraddr_lenis dumped with an uninitialized tail.
dev->addr_lencan grow at runtime and nothing flushes the lists. Whileaddr_lenis 0 every addition collapses into one node, because__hw_addr_add_ex()compares zero bytes, so the test adds one zero-length multicast entry to each of 64 tun devices and switches them toARPHRD_IP6GRE(addr_len16).RTM_GETMULTICASTthen returned 64 entries of 16 bytes, 37 with non-zero bytes and 21 starting with a kernel heap pointer (e.g. 0xffff8188_04857ee0). tun has to flushdev->ucanddev->mcin TUNSETLINK as well, which is a separate patch;qmi_wwanraw IP mode and the HDLC attach/detach paths changeaddr_lenin the same way, so tun is not the only case.Allocate with
kzalloc()so the tail is zeroed at creation; the explicitha->sync_cnt= 0 then becomes redundant.Found by an automated static analysis scan (Tencent CodeBuddy Security) and triggered at runtime on v7.4-rc3 as an unprivileged user.
Fixes cca8ea3b05c9 ("net: tun: set tun->dev->addr_len during TUNSETLINK processing")Reviewed-by Daniel Machon <[email protected]>Reviewed-by Nicolas Dichtel <[email protected]>Signed-off-by Henry Martin <[email protected]>---
v2 -> v3:
- describe the test setup accurately: one entry on each of 64 tun devices, since all additions collapse into one node whileaddr_lenis 0
- point Fixes: atcca8ea3b05c9, which made the leak reachable from tun
- link to v1 instead of the earlier review thread, and link to the companion tun patch
- note that other drivers changeaddr_lenwithout flushing the lists
- state how the issue was foundnet/core/dev_addr_lists.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/core/dev_addr_lists.c +1
-2@@ -96,7 +96,7 @@ __hw_addr_create(const unsigned char *addr, int addr_len, alloc_size =sizeof (*ha);if (alloc_size <L1_CACHE_BYTES ) alloc_size =L1_CACHE_BYTES ; - ha =kmalloc (alloc_size,GFP_ATOMIC ); + ha =kzalloc (alloc_size,GFP_ATOMIC );if (!ha)return NULL ;memcpy (ha->addr, addr, addr_len);@@ -104,7 +104,6 @@ __hw_addr_create(const unsigned char *addr, int addr_len, ha->refcount =1 ; ha->global_use = global; ha->synced = sync ?1 :0 ; - ha->sync_cnt =0 ;
Quoted 7 lines
Quoted 5 lines
net/core/dev_addr_lists.c +0
-1return ha; } --2.43.7
Reviewed-by Yuyang Huang <[email protected]>