net: rds transform strncpy to strscpy
Subsystems (4)
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
@@ -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
@@ -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
Simon Horman
reviewerHi 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
Signed-off-by Baris Can Goral <[email protected]>--- net/rds/connection.c | 4 ++-- net/rds/stats.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)net/rds/connection.c +0
-0
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
-1cinfo->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@@ -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
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
@@ -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
PATCH v2 1/1 Replace strncpy with strscpy
Hi,
Any news about this Patch?
Best Regards,
Baris
Simon Horman
reviewerRe: [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
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
@@ -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
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_padis more appropriate because it also zero-fills any remaining space in the destination if the source is shorter than the provided buffer size.Signed-off-by goralbaris <[email protected]>
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@@ -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
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
@@ -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
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_padis more appropriate because it also zero-fills any remaining space in the destination if the source is shorter than the provided buffer size.Signed-off-by Baris Can Goral <[email protected]>--- net/rds/connection.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)net/rds/connection.c +2
-4@@ -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),
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
@@ -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
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_padis 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
Signed-off-by Baris Can Goral <[email protected]>--- net/rds/connection.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)net/rds/connection.c +2
-4@@ -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),