Skip to contentSkip to conversation

net: zero the tail of ha->addr when allocating address list entries

PATCH In review Henry Martin 3 participants 3 messages Nov 23

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

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
index d827cc21867b..9613c8a9ed7f 100644
@@ -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
Reviewed-by on v3 1/1
#1 lore raw
Quoted 66 lines · Henry Martin

__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 -3
index d827cc21867b..9613c8a9ed7f 100644
@@ -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

maintainer Reviewed-by
#2 lore raw
Quoted 65 lines · Yuyang Huang
Quoted 63 lines · Henry Martin

__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
index d827cc21867b..9613c8a9ed7f 100644
@@ -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 -1
        return ha;
 }
--

2.43.7