Skip to contentSkip to conversation

net/rds: Replace deprecated strncpy() with strscpy_pad()

PATCH Applied Thorsten Blum 4 participants 7 messages Feb 19, 2025 – Feb 22, 2025

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

PATCH 1/1 net/rds: Replace deprecated strncpy() with strscpy_pad()

strncpy() is deprecated for NUL-terminated destination buffers. Use strscpy_pad() instead and remove the manual NUL-termination.

Compile-tested only.

---
 net/rds/stats.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
net/rds/stats.c +1 -2
index 9e87da43c004..cb2e3d2cdf73 100644
@@ -89,8 +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);
-		ctr.name[sizeof(ctr.name) - 1] = '\0';
+		strscpy_pad(ctr.name, names[i]);
 		ctr.value = values[i];
 
 		rds_info_copy(iter, &ctr, sizeof(ctr));
Signature · 1 line
2.48.1
#1 lore raw
Quoted 2 lines · Thorsten Blum

strncpy() is deprecated for NUL-terminated destination buffers. Use strscpy_pad() instead and remove the manual NUL-termination.

When doing these conversions, please describe two aspects of conversions:

- Why is it safe to be NUL terminated
- Why is it safe to be/not-be NUL-padded

In this case, the latter needs examination. Looking at how ctr is used, it is memcpy()ed later, which means this string MUST be NUL padded or it will leak stack memory contents.

So, please use strscpy_pad() here. :)

-Kees

Quoted 28 lines

Compile-tested only.

---
 net/rds/stats.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
net/rds/stats.c +1 -2
index 9e87da43c004..cb2e3d2cdf73 100644
@@ -89,8 +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);
-		ctr.name[sizeof(ctr.name) - 1] = '\0';
+		strscpy_pad(ctr.name, names[i]);
 		ctr.value = values[i];
 
 		rds_info_copy(iter, &ctr, sizeof(ctr));
Signature · 1 line
2.48.1
Signature · 1 line
Kees Cook
#2 lore raw
Quoted 7 lines · Kees Cook
Quoted 2 lines · Thorsten Blum

strncpy() is deprecated for NUL-terminated destination buffers. Use strscpy_pad() instead and remove the manual NUL-termination.

When doing these conversions, please describe two aspects of conversions:

Quoted 8 lines

- Why is it safe to be NUL terminated
- Why is it safe to be/not-be NUL-padded

In this case, the latter needs examination. Looking at how ctr is used, it is memcpy()ed later, which means this string MUST be NUL padded or it will leak stack memory contents.

So, please use strscpy_pad() here. :)

I am using strscpy_pad() here already because of the NUL-padding.

Did you just miss that?

Thanks,
Thorsten

Reviewed-by
#3 lore raw
Quoted 15 lines · Thorsten Blum
Quoted 14 lines · Kees Cook
Quoted 2 lines · Thorsten Blum

strncpy() is deprecated for NUL-terminated destination buffers. Use strscpy_pad() instead and remove the manual NUL-termination.

When doing these conversions, please describe two aspects of conversions:

- Why is it safe to be NUL terminated
- Why is it safe to be/not-be NUL-padded

In this case, the latter needs examination. Looking at how ctr is used, it is memcpy()ed later, which means this string MUST be NUL padded or it will leak stack memory contents.

Quoted 5 lines
Quoted 1 line

So, please use strscpy_pad() here. :)

I am using strscpy_pad() here already because of the NUL-padding.

Did you just miss that?

Well that's embarrassing. Yes, I must need stronger glasses. *sigh* Apologies for the noise!

Signature · 1 line
Kees Cook
Reviewed-by on v1 1/1
#4 lore raw
In reply to Thorsten Blum
Quoted 17 lines · Thorsten Blum

strncpy() is deprecated for NUL-terminated destination buffers. Use strscpy_pad() instead and remove the manual NUL-termination.

Compile-tested only.

---
 net/rds/stats.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
net/rds/stats.c +0 -0
index 9e87da43c004..cb2e3d2cdf73 100644

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

Quoted 8 lines
net/rds/stats.c +1 -2
@@ -89,8 +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);
-		ctr.name[sizeof(ctr.name) - 1] = '\0';
+		strscpy_pad(ctr.name, names[i]);
 		ctr.value = values[i];
 

Looks ok to me. Thanks Thorsten!

Quoted 1 line
net/rds/stats.c +0 -0
 		rds_info_copy(iter, &ctr, sizeof(ctr));
#5 lore raw
Quoted 5 lines · Allison Henderson
Quoted 4 lines · Thorsten Blum

strncpy() is deprecated for NUL-terminated destination buffers. Use strscpy_pad() instead and remove the manual NUL-termination.

Compile-tested only.

I went ahead and tested this for you, and it looks fine. So, you can go ahead and remove the "Compiled-tested only" and add:

Thank you!
Allison

Quoted 26 lines
Quoted 13 lines
---
 net/rds/stats.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
net/rds/stats.c +0 -0
index 9e87da43c004..cb2e3d2cdf73 100644

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

Quoted 8 lines
net/rds/stats.c +1 -2
@@ -89,8 +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);
-		ctr.name[sizeof(ctr.name) - 1] = '\0';
+		strscpy_pad(ctr.name, names[i]);
 		ctr.value = values[i];
 

Looks ok to me. Thanks Thorsten!

Quoted 1 line
net/rds/stats.c +0 -0
 		rds_info_copy(iter, &ctr, sizeof(ctr));
#6 lore raw
In reply to Thorsten Blum
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Wed, 19 Feb 2025 23:47:31 +0100 you wrote:

strncpy() is deprecated for NUL-terminated destination buffers. Use strscpy_pad() instead and remove the manual NUL-termination.

Compile-tested only.

[...]

Here is the summary with links:
- [net-next] net/rds: Replace deprecated strncpy() with strscpy_pad()

    https://git.kernel.org/netdev/net-next/c/c451715d78e3

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html