Skip to contentSkip to conversation

net: rds transform strncpy to strscpy

PATCH In review Baris Can Goral 5 participants 11 messages Apr 7, 2025 – May 20, 2025

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

PATCH 1/1 net: rds transform strncpy to strscpy

Hi,
The strncpy() function is actively dangerous to use since it may not NULL-terminate the destination string,resulting in potential memory content exposures, unbounded reads, or crashes.

---
 net/rds/connection.c | 4 ++--
 net/rds/stats.c      | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
net/rds/connection.c +2 -2
index c749c5525b40..fb2f14a1279a 100644
@@ -749,7 +749,7 @@ static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
 	cinfo->laddr = conn->c_laddr.s6_addr32[3];
 	cinfo->faddr = conn->c_faddr.s6_addr32[3];
 	cinfo->tos = conn->c_tos;
-	strncpy(cinfo->transport, conn->c_trans->t_name,
+	strscpy(cinfo->transport, conn->c_trans->t_name,
 		sizeof(cinfo->transport));
 	cinfo->flags = 0;
 
@@ -775,7 +775,7 @@ static int rds6_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
 	cinfo6->next_rx_seq = cp->cp_next_rx_seq;
 	cinfo6->laddr = conn->c_laddr;
 	cinfo6->faddr = conn->c_faddr;
-	strncpy(cinfo6->transport, conn->c_trans->t_name,
+	strscpy(cinfo6->transport, conn->c_trans->t_name,
 		sizeof(cinfo6->transport));
 	cinfo6->flags = 0;
 
net/rds/stats.c +1 -1
index 9e87da43c004..63c34dbdf97f 100644
@@ -89,7 +89,7 @@ void rds_stats_info_copy(struct rds_info_iterator *iter,
 
 	for (i = 0; i < nr; i++) {
 		BUG_ON(strlen(names[i]) >= sizeof(ctr.name));
-		strncpy(ctr.name, names[i], sizeof(ctr.name) - 1);
+		strscpy(ctr.name, names[i], sizeof(ctr.name) - 1);
 		ctr.name[sizeof(ctr.name) - 1] = '\0';
 		ctr.value = values[i];
 
Signature · 1 line
2.34.1
reviewer
#1 lore raw

+ [email protected]

Hi Baris,

Quoted 1 line · Baris Can Goral

Hi,

It's nice to be friendly, but I don't think a salutation belongs in a commit message. (Please remove the line above.)

Quoted 2 lines

The strncpy() function is actively dangerous to use since it may not NULL-terminate the destination string,resulting in potential memory

Space after the comma (,) please.

Quoted 1 line

content exposures, unbounded reads, or crashes.

I think there should be a blank like before the Link tag.

Quoted 1 line

But not between it and other tags.

Also, there should be a space after "Link:"

Quoted 11 lines
---
 net/rds/connection.c | 4 ++--
 net/rds/stats.c      | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
net/rds/connection.c +0 -0
index c749c5525b40..fb2f14a1279a 100644

net/rds/connection.c static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer) · line 753

Quoted 6 lines
net/rds/connection.c +1 -1
@@ -749,7 +749,7 @@ static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
 	cinfo->laddr = conn->c_laddr.s6_addr32[3];
 	cinfo->faddr = conn->c_faddr.s6_addr32[3];
 	cinfo->tos = conn->c_tos;
-	strncpy(cinfo->transport, conn->c_trans->t_name,
+	strscpy(cinfo->transport, conn->c_trans->t_name,
 		sizeof(cinfo->transport));

I agree that strscpy() is appropriate as we want null termination but not padding.

Because the destination, cinfo->transport, is an array I believe we can omit passing the size argument to strscpy, like this:

	strscpy(cinfo->transport, conn->c_trans->t_name);

net/rds/connection.c static int rds6_conn_info_visitor(struct rds_conn_path *cp, void *buffer) · line 780

Quoted 10 lines
net/rds/connection.c +1 -1
 	cinfo->flags = 0;
 
@@ -775,7 +775,7 @@ static int rds6_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
 	cinfo6->next_rx_seq = cp->cp_next_rx_seq;
 	cinfo6->laddr = conn->c_laddr;
 	cinfo6->faddr = conn->c_faddr;
-	strncpy(cinfo6->transport, conn->c_trans->t_name,
+	strscpy(cinfo6->transport, conn->c_trans->t_name,
 		sizeof(cinfo6->transport));
 	cinfo6->flags = 0;

Ditto.

net/rds/stats.c void rds_stats_info_copy(struct rds_info_iterator *iter, · line 94

Quoted 13 lines
net/rds/stats.c +1 -1
index 9e87da43c004..63c34dbdf97f 100644
@@ -89,7 +89,7 @@ void rds_stats_info_copy(struct rds_info_iterator *iter,
 
 	for (i = 0; i < nr; i++) {
 		BUG_ON(strlen(names[i]) >= sizeof(ctr.name));
-		strncpy(ctr.name, names[i], sizeof(ctr.name) - 1);
+		strscpy(ctr.name, names[i], sizeof(ctr.name) - 1);
 		ctr.name[sizeof(ctr.name) - 1] = '\0';
 		ctr.value = values[i];

This issue appears to have been addressed by
commit c451715d78e3 ("net/rds: Replace deprecated strncpy() with strscpy_pad()")

As a Networking patch please make sure it is based on the net-next tree and targeted at that tree like this:

Subject: [PATCH net-next v2] ...

Or the net tree if it is a big fix, which this patch isn't.

Please consider posting a v2 patch to address the above. And please consider CCing [email protected] on v2.

Signature · 1 line
pw-bot: changes-requested
#2 lore raw

PATCH v2 1/1 Replace strncpy with strscpy

The strncpy() function is actively dangerous to use since it may not NULL-terminate the destination string, resulting in potential memory.

---
 net/rds/connection.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
net/rds/connection.c +2 -3
index c749c5525b40..4bb727dd13b0 100644
@@ -749,8 +749,7 @@ static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
 	cinfo->laddr = conn->c_laddr.s6_addr32[3];
 	cinfo->faddr = conn->c_faddr.s6_addr32[3];
 	cinfo->tos = conn->c_tos;
-	strncpy(cinfo->transport, conn->c_trans->t_name,
-		sizeof(cinfo->transport));
+	strscpy(cinfo->transport, conn->c_trans->t_name);
 	cinfo->flags = 0;
 
 	rds_conn_info_set(cinfo->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
@@ -775,7 +774,7 @@ static int rds6_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
 	cinfo6->next_rx_seq = cp->cp_next_rx_seq;
 	cinfo6->laddr = conn->c_laddr;
 	cinfo6->faddr = conn->c_faddr;
-	strncpy(cinfo6->transport, conn->c_trans->t_name,
+	strscpy(cinfo6->transport, conn->c_trans->t_name,
 		sizeof(cinfo6->transport));
 	cinfo6->flags = 0;
 
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v2 1/1 Replace strncpy with strscpy

Hi,
Any news about this Patch?

Best Regards,
Baris

reviewer
#4 lore raw

Re: [PATCH net-next v2] Replace strncpy with strscpy

Quoted 2 lines · goralbaris

Hi,
Any news about this Patch?

Hi Baris,

I expect this patch floundered because it was not CCed to netdev.

In any case, there was a separate effort to address this problem.

- [PATCH v2] net: rds: Replace strncpy with strscpy in connection setup by Shankari Anand
https://lore.kernel.org/netdev/[email protected]/

I would suggest that a successful patch should:

* Use the two-argument variant of strscpy_pad (see thread above)
* Include commentary in the commit message regarding
- Why strctpy* is preferred over strncpy (you already did that :).
- Why strscpy_pad is appropriate instead of strscpy
* CC all relevant parties, including netdev, the Netdev maintainers, Shankari Anand and Allison Henderson

author
#5 lore raw

PATCH v3 1/1 replace strncpy with strscpy_pad

The strncpy() function is actively dangerous to use since it may not NULL-terminate the destination string, resulting in potential memory.

In addition, strscpy_pad is more appropriate because it also zero-fills any remaining space in the destination if the source is shorter than the provided buffer size.

---
 net/rds/connection.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
net/rds/connection.c +2 -4
index c749c5525b40..d62f486ab29f 100644
@@ -749,8 +749,7 @@ static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
 	cinfo->laddr = conn->c_laddr.s6_addr32[3];
 	cinfo->faddr = conn->c_faddr.s6_addr32[3];
 	cinfo->tos = conn->c_tos;
-	strncpy(cinfo->transport, conn->c_trans->t_name,
-		sizeof(cinfo->transport));
+	strscpy_pad(cinfo->transport, conn->c_trans->t_name);
 	cinfo->flags = 0;
 
 	rds_conn_info_set(cinfo->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
@@ -775,8 +774,7 @@ static int rds6_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
 	cinfo6->next_rx_seq = cp->cp_next_rx_seq;
 	cinfo6->laddr = conn->c_laddr;
 	cinfo6->faddr = conn->c_faddr;
-	strncpy(cinfo6->transport, conn->c_trans->t_name,
-		sizeof(cinfo6->transport));
+	strscpy_pad(cinfo6->transport, conn->c_trans->t_name);
 	cinfo6->flags = 0;
 
 	rds_conn_info_set(cinfo6->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
Signature · 1 line
2.34.1
Reviewed-by on v3 1/1
#6 lore raw

Re: [PATCH v3 net-next: rds] replace strncpy with strscpy_pad

Quoted 9 lines · goralbaris

The strncpy() function is actively dangerous to use since it may not NULL-terminate the destination string, resulting in potential memory.

In addition, strscpy_pad is more appropriate because it also zero-fills any remaining space in the destination if the source is shorter than the provided buffer size.

There should be your full name, not nick.

Feel free to add my RB tag

Quoted 30 lines
---
 net/rds/connection.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
net/rds/connection.c +2 -4
index c749c5525b40..d62f486ab29f 100644
@@ -749,8 +749,7 @@ static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
 	cinfo->laddr = conn->c_laddr.s6_addr32[3];
 	cinfo->faddr = conn->c_faddr.s6_addr32[3];
 	cinfo->tos = conn->c_tos;
-	strncpy(cinfo->transport, conn->c_trans->t_name,
-		sizeof(cinfo->transport));
+	strscpy_pad(cinfo->transport, conn->c_trans->t_name);
 	cinfo->flags = 0;
 
 	rds_conn_info_set(cinfo->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
@@ -775,8 +774,7 @@ static int rds6_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
 	cinfo6->next_rx_seq = cp->cp_next_rx_seq;
 	cinfo6->laddr = conn->c_laddr;
 	cinfo6->faddr = conn->c_faddr;
-	strncpy(cinfo6->transport, conn->c_trans->t_name,
-		sizeof(cinfo6->transport));
+	strscpy_pad(cinfo6->transport, conn->c_trans->t_name);
 	cinfo6->flags = 0;
 
 	rds_conn_info_set(cinfo6->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
Signature · 1 line
2.34.1
#7 lore raw

PATCH v4 1/1 replace strncpy with strscpy_pad

The strncpy() function is actively dangerous to use since it may not NULL-terminate the destination string, resulting in potential memory.

In addition, strscpy_pad is more appropriate because it also zero-fills any remaining space in the destination if the source is shorter than the provided buffer size.

---
 net/rds/connection.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
net/rds/connection.c +2 -4
index c749c5525b40..d62f486ab29f 100644
@@ -749,8 +749,7 @@ static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
 	cinfo->laddr = conn->c_laddr.s6_addr32[3];
 	cinfo->faddr = conn->c_faddr.s6_addr32[3];
 	cinfo->tos = conn->c_tos;
-	strncpy(cinfo->transport, conn->c_trans->t_name,
-		sizeof(cinfo->transport));
+	strscpy_pad(cinfo->transport, conn->c_trans->t_name);
 	cinfo->flags = 0;
 
 	rds_conn_info_set(cinfo->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
@@ -775,8 +774,7 @@ static int rds6_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
 	cinfo6->next_rx_seq = cp->cp_next_rx_seq;
 	cinfo6->laddr = conn->c_laddr;
 	cinfo6->faddr = conn->c_faddr;
-	strncpy(cinfo6->transport, conn->c_trans->t_name,
-		sizeof(cinfo6->transport));
+	strscpy_pad(cinfo6->transport, conn->c_trans->t_name);
 	cinfo6->flags = 0;
 
 	rds_conn_info_set(cinfo6->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
Signature · 1 line
2.34.1
#8 lore raw

Re: [PATCH v4 net-next: rds] replace strncpy with strscpy_pad

Quoted 2 lines · Baris Can Goral

The strncpy() function is actively dangerous to use since it may not NULL-terminate the destination string, resulting in potential memory.

It looks like we lost the last part of this phrase? I think you meant to quote the link: "...potential memory content exposures, unbounded reads, or crashes."

Other than that I think it looks ok. Thanks!
Allison

Quoted 35 lines

In addition, strscpy_pad is more appropriate because it also zero-fills any remaining space in the destination if the source is shorter than the provided buffer size.

---
 net/rds/connection.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
net/rds/connection.c +2 -4
index c749c5525b40..d62f486ab29f 100644
@@ -749,8 +749,7 @@ static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
 	cinfo->laddr = conn->c_laddr.s6_addr32[3];
 	cinfo->faddr = conn->c_faddr.s6_addr32[3];
 	cinfo->tos = conn->c_tos;
-	strncpy(cinfo->transport, conn->c_trans->t_name,
-		sizeof(cinfo->transport));
+	strscpy_pad(cinfo->transport, conn->c_trans->t_name);
 	cinfo->flags = 0;
 
 	rds_conn_info_set(cinfo->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
@@ -775,8 +774,7 @@ static int rds6_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
 	cinfo6->next_rx_seq = cp->cp_next_rx_seq;
 	cinfo6->laddr = conn->c_laddr;
 	cinfo6->faddr = conn->c_faddr;
-	strncpy(cinfo6->transport, conn->c_trans->t_name,
-		sizeof(cinfo6->transport));
+	strscpy_pad(cinfo6->transport, conn->c_trans->t_name);
 	cinfo6->flags = 0;
 
 	rds_conn_info_set(cinfo6->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
#9 lore raw

PATCH v5 1/1 replace strncpy with strscpy_pad

The strncpy() function is actively dangerous to use since it may not NULL-terminate the destination string, resulting in potential memory content exposures, unbounded reads, or crashes.

In addition, strscpy_pad is more appropriate because it also zero-fills any remaining space in the destination if the source is shorter than the provided buffer size.

---
 net/rds/connection.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
net/rds/connection.c +2 -4
index c749c5525b40..d62f486ab29f 100644
@@ -749,8 +749,7 @@ static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
 	cinfo->laddr = conn->c_laddr.s6_addr32[3];
 	cinfo->faddr = conn->c_faddr.s6_addr32[3];
 	cinfo->tos = conn->c_tos;
-	strncpy(cinfo->transport, conn->c_trans->t_name,
-		sizeof(cinfo->transport));
+	strscpy_pad(cinfo->transport, conn->c_trans->t_name);
 	cinfo->flags = 0;
 
 	rds_conn_info_set(cinfo->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
@@ -775,8 +774,7 @@ static int rds6_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
 	cinfo6->next_rx_seq = cp->cp_next_rx_seq;
 	cinfo6->laddr = conn->c_laddr;
 	cinfo6->faddr = conn->c_faddr;
-	strncpy(cinfo6->transport, conn->c_trans->t_name,
-		sizeof(cinfo6->transport));
+	strscpy_pad(cinfo6->transport, conn->c_trans->t_name);
 	cinfo6->flags = 0;
 
 	rds_conn_info_set(cinfo6->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
Signature · 1 line
2.34.1
#10 lore raw

Re: [PATCH v5 net-next: rds] replace strncpy with strscpy_pad

Quoted 8 lines · Baris Can Goral

The strncpy() function is actively dangerous to use since it may not NULL-terminate the destination string, resulting in potential memory content exposures, unbounded reads, or crashes.

In addition, strscpy_pad is more appropriate because it also zero-fills any remaining space in the destination if the source is shorter than the provided buffer size.

Please don't reply to an old thread when starting a new version.

It is better to start a new thread with the new version.

Zhu Yanjun

Quoted 30 lines
---
  net/rds/connection.c | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
net/rds/connection.c +2 -4
index c749c5525b40..d62f486ab29f 100644
@@ -749,8 +749,7 @@ static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
  	cinfo->laddr = conn->c_laddr.s6_addr32[3];
  	cinfo->faddr = conn->c_faddr.s6_addr32[3];
  	cinfo->tos = conn->c_tos;
-	strncpy(cinfo->transport, conn->c_trans->t_name,
-		sizeof(cinfo->transport));
+	strscpy_pad(cinfo->transport, conn->c_trans->t_name);
  	cinfo->flags = 0;
 
  	rds_conn_info_set(cinfo->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
@@ -775,8 +774,7 @@ static int rds6_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
  	cinfo6->next_rx_seq = cp->cp_next_rx_seq;
  	cinfo6->laddr = conn->c_laddr;
  	cinfo6->faddr = conn->c_faddr;
-	strncpy(cinfo6->transport, conn->c_trans->t_name,
-		sizeof(cinfo6->transport));
+	strscpy_pad(cinfo6->transport, conn->c_trans->t_name);
  	cinfo6->flags = 0;
 
  	rds_conn_info_set(cinfo6->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),