Skip to contentSkip to conversation

rds: tcp: fix uninit-value in __inet_bind

PATCH Superseded Tabrez Ahmed 3 participants 3 messages Feb 15 – Feb 16

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rds: tcp: fix uninit-value in __inet_bind

KMSAN reported an uninit-value access in __inet_bind() when binding an RDS TCP socket.
The uninitialized memory originates from rds_tcp_conn_alloc(), which uses kmem_cache_alloc() to allocate the rds_tcp_connection structure.

The structure is not zero-initialized, leaving random data in its fields.
When the networking stack later tries to bind the socket using these dirty values, KMSAN flags the uninitialized access.

Fix this by using kmem_cache_zalloc() instead of kmem_cache_alloc() to ensure the structure is zeroed out upon allocation.

Reported-by [email protected]
Fixes 70041088e3b9 ("RDS: Add TCP transport to RDS")

---
This is my first patch. Any feedback is appreciated!

 net/rds/tcp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/rds/tcp.c +1 -1
index 45484a93d75f..04f310255692 100644
@@ -373,7 +373,7 @@ static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp)
 	int ret = 0;
 
 	for (i = 0; i < RDS_MPATH_WORKERS; i++) {
-		tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp);
+		tc = kmem_cache_zalloc(rds_tcp_conn_slab, gfp);
 		if (!tc) {
 			ret = -ENOMEM;
 			goto fail;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 5 lines · Tabrez Ahmed

KMSAN reported an uninit-value access in __inet_bind() when binding an RDS TCP socket.
The uninitialized memory originates from rds_tcp_conn_alloc(), which uses kmem_cache_alloc() to allocate the rds_tcp_connection structure.

The structure is not zero-initialized, leaving random data in its fields.
When the networking stack later tries to bind the socket using these dirty values, KMSAN flags the uninitialized access.

Most fields in rds_tcp_connection are explicitly initialized after allocation right? The only field actually read before being written is t_client_port_group. Could the description be more specific about which field is problematic?

Quoted 7 lines

Fix this by using kmem_cache_zalloc() instead of kmem_cache_alloc() to ensure the structure is zeroed out upon allocation.

Reported-by [email protected]
Fixes 70041088e3b9 ("RDS: Add TCP transport to RDS")

Not sure about this, the field trigger this bug, t_client_port_group, did not exist in 70041088e3b9. It was introduced in

a20a6992558f ("net/rds: Encode cp_index in TCP source port")

which added both the field and the code in rds_tcp_conn_path_connect() that reads it uninitialized:

     if (++tc->t_client_port_group >= port_groups)

Should the Fixes that reference that instead? If I'm correct ofc.

Also, nit, commit message body should not exceed 75 characters.

Quoted 4 lines

---
This is my first patch. Any feedback is appreciated!

Best of luck.

Quoted 17 lines
 net/rds/tcp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/rds/tcp.c +1 -1
index 45484a93d75f..04f310255692 100644
@@ -373,7 +373,7 @@ static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp)
 	int ret = 0;
 
 	for (i = 0; i < RDS_MPATH_WORKERS; i++) {
-		tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp);
+		tc = kmem_cache_zalloc(rds_tcp_conn_slab, gfp);
 		if (!tc) {
 			ret = -ENOMEM;
 			goto fail;
#2 lore raw
Quoted 26 lines · Charalampos Mitrodimas
Quoted 5 lines · Tabrez Ahmed

KMSAN reported an uninit-value access in __inet_bind() when binding an RDS TCP socket.
The uninitialized memory originates from rds_tcp_conn_alloc(), which uses kmem_cache_alloc() to allocate the rds_tcp_connection structure.

The structure is not zero-initialized, leaving random data in its fields.
When the networking stack later tries to bind the socket using these dirty values, KMSAN flags the uninitialized access.

Most fields in rds_tcp_connection are explicitly initialized after allocation right? The only field actually read before being written is t_client_port_group. Could the description be more specific about which field is problematic?

Quoted 7 lines

Fix this by using kmem_cache_zalloc() instead of kmem_cache_alloc() to ensure the structure is zeroed out upon allocation.

Not sure about this, the field trigger this bug, t_client_port_group, did not exist in 70041088e3b9. It was introduced in

a20a6992558f ("net/rds: Encode cp_index in TCP source port")

Quoted 8 lines

which added both the field and the code in rds_tcp_conn_path_connect() that reads it uninitialized:

     if (++tc->t_client_port_group >= port_groups)

Should the Fixes that reference that instead? If I'm correct ofc.

Also, nit, commit message body should not exceed 75 characters.

Hi Tabrez,

Thanks for catching this. Charalampos is correct in that t_client_port_group member was recently added in Commit a20a6992558f ("net/rds: Encode cp_index in TCP source port"). So that would be the appropriate tag. The uninitialized reference in rds_tcp_conn_path_connect is consistent with the syzkaller backtrace. So a quick mention of it in the commit message would be appropriate. Thank you!

Allison

Quoted 25 lines
Quoted 5 lines

---
This is my first patch. Any feedback is appreciated!

Best of luck.

Quoted 17 lines
 net/rds/tcp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/rds/tcp.c +1 -1
index 45484a93d75f..04f310255692 100644
@@ -373,7 +373,7 @@ static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp)
 	int ret = 0;
 
 	for (i = 0; i < RDS_MPATH_WORKERS; i++) {
-		tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp);
+		tc = kmem_cache_zalloc(rds_tcp_conn_slab, gfp);
 		if (!tc) {
 			ret = -ENOMEM;
 			goto fail;