Skip to contentSkip to conversation

net/rds: using strlcpy instead of strncpy

PATCH In review Chen Gang 4 participants 15 messages Feb 28, 2013 – Mar 8, 2013

Subsystems (3)
lore
Share & tools
lore

Chen Gang

author
#0 lore raw

PATCH 1/1 net/rds: using strlcpy instead of strncpy

when src strlen is sizeof(ctr.name) - 1, the dest str is not '\0' end. better use strlcpy instead.

---
 net/rds/stats.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
net/rds/stats.c +1 -1
index 7be790d..b9ac1df 100644
@@ -86,7 +86,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);
+		strlcpy(ctr.name, names[i], sizeof(ctr.name));
 		ctr.value = values[i];
 
 		rds_info_copy(iter, &ctr, sizeof(ctr));
Signature · 1 line
1.7.7.6
#1 lore raw
Quoted 13 lines

when src strlen is sizeof(ctr.name) - 1, the dest str is not '\0' end. better use strlcpy instead.

---
 net/rds/stats.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
net/rds/stats.c +0 -0
index 7be790d..b9ac1df 100644

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

Quoted 7 lines
net/rds/stats.c +1 -1
@@ -86,7 +86,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);
+		strlcpy(ctr.name, names[i], sizeof(ctr.name));
 		ctr.value = values[i];
 

If the target buffer ends up being copied to userspace that might lead to random kernel memory being leaked.

David

author
#2 lore raw
Quoted 6 lines · David Laight
Quoted 6 lines
Quoted 6 lines
net/rds/stats.c +0 -0
index 7be790d..b9ac1df 100644
@@ -86,7 +86,7 @@ void rds_stats_info_copy(struct rds_info_iterator *iter,
 
Quoted 8 lines
Quoted 6 lines

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

Quoted 6 lines
net/rds/stats.c +1 -1
 	for (i = 0; i < nr; i++) {
 		BUG_ON(strlen(names[i]) >= sizeof(ctr.name));
-		strncpy(ctr.name, names[i], sizeof(ctr.name) - 1);
+		strlcpy(ctr.name, names[i], sizeof(ctr.name));
 		ctr.value = values[i];
 

If the target buffer ends up being copied to userspace that might lead to random kernel memory being leaked.

excuse me, I do not quite understand what your meaning.

I guess what you said is:
if crt.name is not ended by '\0', it will cause issue. the issue is:
"If the target buffer ends up being copied to userspace that
might lead to random kernel memory being leaked."

is it correct ?
if it is correct, need I add them as patch comments, too ? else (incorrect), please give more descriptions, thanks.

:-)

Signature · 3 lines
Chen Gang

Asianux Corporation
#3 lore raw
In reply to David Laight
Quoted 14 lines · David Laight
Quoted 14 lines

when src strlen is sizeof(ctr.name) - 1, the dest str is not '\0' end. better use strlcpy instead.

---
 net/rds/stats.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
net/rds/stats.c +0 -0
index 7be790d..b9ac1df 100644
@@ -86,7 +86,7 @@ void rds_stats_info_copy(struct rds_info_iterator *iter,
 
Quoted 9 lines

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

Quoted 6 lines
net/rds/stats.c +1 -1
 	for (i = 0; i < nr; i++) {
 		BUG_ON(strlen(names[i]) >= sizeof(ctr.name));
-		strncpy(ctr.name, names[i], sizeof(ctr.name) - 1);
+		strlcpy(ctr.name, names[i], sizeof(ctr.name));
 		ctr.value = values[i];
 

If the target buffer ends up being copied to userspace that might lead to random kernel memory being leaked.

Seems it is. The last byte of 'name' is not currently initialised and therefore is already leaked to userland.

But it's OK because rds_info_copy() uses memcpy() not __copy_to_user(), so SMAP will block this leak. :-)

Ben.

Signature · 3 lines
Ben Hutchings, Staff Engineer, Solarflare
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.
#4 lore raw
Quoted 25 lines · Ben Hutchings
Quoted 14 lines · David Laight
Quoted 14 lines

when src strlen is sizeof(ctr.name) - 1, the dest str is not '\0' end. better use strlcpy instead.

---
 net/rds/stats.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
net/rds/stats.c +0 -0
index 7be790d..b9ac1df 100644
@@ -86,7 +86,7 @@ void rds_stats_info_copy(struct rds_info_iterator *iter,
 
Quoted 9 lines

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

Quoted 6 lines
net/rds/stats.c +1 -1
 	for (i = 0; i < nr; i++) {
 		BUG_ON(strlen(names[i]) >= sizeof(ctr.name));
-		strncpy(ctr.name, names[i], sizeof(ctr.name) - 1);
+		strlcpy(ctr.name, names[i], sizeof(ctr.name));
 		ctr.value = values[i];
 

If the target buffer ends up being copied to userspace that might lead to random kernel memory being leaked.

Quoted 5 lines

Seems it is. The last byte of 'name' is not currently initialised and therefore is already leaked to userland.

But it's OK because rds_info_copy() uses memcpy() not __copy_to_user(), so SMAP will block this leak. :-)

Or not, as kmap() presumably evades that.

Ben.

Signature · 3 lines
Ben Hutchings, Staff Engineer, Solarflare
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.
author
#5 lore raw
Quoted 6 lines · Ben Hutchings
Quoted 5 lines · Ben Hutchings
Quoted 5 lines · David Laight
Quoted 3 lines
Quoted 3 lines

If the target buffer ends up being copied to userspace that might lead to random kernel memory being leaked.

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

Seems it is. The last byte of 'name' is not currently initialised and therefore is already leaked to userland.

But it's OK because rds_info_copy() uses memcpy() not __copy_to_user(), so SMAP will block this leak. :-)

Or not, as kmap() presumably evades that.

is this patch ok, or need improving ?

BTW:
excuse me, maybe my reply will be late during this week. the reason:
my father had a serious heart disease, and is in hospital. during these days, most of my time has to be in hospital.
(God Bless, and thank Jesus Christ, my father is safe, now).
within my company (Asianux), I also have something to do.

:-)

Signature · 3 lines
Chen Gang

Asianux Corporation
#6 lore raw
Quoted 7 lines · Chen Gang
Quoted 6 lines · Ben Hutchings
Quoted 5 lines · Ben Hutchings
Quoted 5 lines · David Laight
Quoted 3 lines
Quoted 3 lines

If the target buffer ends up being copied to userspace that might lead to random kernel memory being leaked.

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

Seems it is. The last byte of 'name' is not currently initialised and therefore is already leaked to userland.

But it's OK because rds_info_copy() uses memcpy() not __copy_to_user(), so SMAP will block this leak. :-)

Or not, as kmap() presumably evades that.

is this patch ok, or need improving ?

I think it is wrong, and the code should be changed to do either:

1. Zero-initialise the whole of the name, then use strlcpy().
2. Keep using strncpy(), and also set the last byte of name to 0.

Quoted 7 lines

BTW:
excuse me, maybe my reply will be late during this week. the reason:
my father had a serious heart disease, and is in hospital. during these days, most of my time has to be in hospital.
(God Bless, and thank Jesus Christ, my father is safe, now).
within my company (Asianux), I also have something to do.

Best wishes to you both.

Ben.

Signature · 3 lines
Ben Hutchings, Staff Engineer, Solarflare
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.
author
#7 lore raw
Quoted 5 lines · Ben Hutchings

I think it is wrong, and the code should be changed to do either:

1. Zero-initialise the whole of the name, then use strlcpy().
2. Keep using strncpy(), and also set the last byte of name to 0.

I think what I have done is just like your choice "2." for me, I think they are equal:

- strncpy(ctr.name, names[i], sizeof(ctr.name) - 1);
+ strlcpy(ctr.name, names[i], sizeof(ctr.name));

		strncpy(ctr.name, names[i], sizeof(ctr.name) - 1);

+ ctr.name[sizeof(ctr.name) - 1] = '\0';

Quoted 8 lines
Quoted 7 lines
Quoted 7 lines

BTW:
excuse me, maybe my reply will be late during this week. the reason:
my father had a serious heart disease, and is in hospital. during these days, most of my time has to be in hospital.
(God Bless, and thank Jesus Christ, my father is safe, now).
within my company (Asianux), I also have something to do.

Best wishes to you both.

thank you.

:-)

Signature · 3 lines
Chen Gang

Asianux Corporation
#8 lore raw
Quoted 15 lines · Chen Gang
Quoted 5 lines · Ben Hutchings

I think it is wrong, and the code should be changed to do either:

1. Zero-initialise the whole of the name, then use strlcpy().
2. Keep using strncpy(), and also set the last byte of name to 0.

I think what I have done is just like your choice "2." for me, I think they are equal:

(quoted diff) +1 -1
-		strncpy(ctr.name, names[i], sizeof(ctr.name) - 1);
+		strlcpy(ctr.name, names[i], sizeof(ctr.name));

		strncpy(ctr.name, names[i], sizeof(ctr.name) - 1);

Review

Quoted 1 line
(quoted diff) +1 -0
+ 		ctr.name[sizeof(ctr.name) - 1] = '\0';

They are not. strncpy() pads with zeroes to the end of the given buffer whereas strlcpy() adds only a single zero byte (and truncates if necessary to fit the zero byte).

Ben.

Signature · 3 lines
Ben Hutchings, Staff Engineer, Solarflare
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.
author
#9 lore raw
Quoted 7 lines · Ben Hutchings
Quoted 7 lines

I think what I have done is just like your choice "2."

Quoted 6 lines

for me, I think they are equal:

(quoted diff) +1 -1
-		strncpy(ctr.name, names[i], sizeof(ctr.name) - 1);
+		strlcpy(ctr.name, names[i], sizeof(ctr.name));
Quoted 5 lines
Quoted 2 lines

Review

Quoted 2 lines
		strncpy(ctr.name, names[i], sizeof(ctr.name) - 1);
(quoted diff) +1 -0
+ 		ctr.name[sizeof(ctr.name) - 1] = '\0';

They are not. strncpy() pads with zeroes to the end of the given buffer whereas strlcpy() adds only a single zero byte (and truncates if necessary to fit the zero byte).

ok, thank you. they are really not the same (originally, I did not
notice it)

could you supply the reason:
why need we zero all of ctr.name ?
(for me, I think, keeping ctr.name just a zero-based string is ok)

thanks.

:-)

Signature · 3 lines
Chen Gang

Asianux Corporation
#10 lore raw
Quoted 14 lines · Chen Gang
Quoted 7 lines · Ben Hutchings
Quoted 7 lines

I think what I have done is just like your choice "2."

Quoted 6 lines

for me, I think they are equal:

(quoted diff) +1 -1
-		strncpy(ctr.name, names[i], sizeof(ctr.name) - 1);
+		strlcpy(ctr.name, names[i], sizeof(ctr.name));
Quoted 5 lines
Quoted 2 lines

Review

Quoted 2 lines
		strncpy(ctr.name, names[i], sizeof(ctr.name) - 1);
(quoted diff) +1 -0
+ 		ctr.name[sizeof(ctr.name) - 1] = '\0';

They are not. strncpy() pads with zeroes to the end of the given buffer whereas strlcpy() adds only a single zero byte (and truncates if necessary to fit the zero byte).

Quoted 6 lines

ok, thank you. they are really not the same (originally, I did not
notice it)

could you supply the reason:
why need we zero all of ctr.name ?
(for me, I think, keeping ctr.name just a zero-based string is ok)

This function calls rds_copy_info() to copy the whole of ctr into userland.

If ctr is not completely initialised, then the values of the uninitialised bytes are left over from the local variables of an earlier system call. If an attacker knows enough about the stack layout (easy if this is a distribution kernel), they can make a series of system calls that leak information about heap-allocated objects. That can help them to exploit other kernel bugs for privilege escalation. So we should initialise every bit of memory that is going to be copied to userland.

(In fact, in general it's not even enough to initialise all fields of the structure, because there may be padding bytes between them. In this case we know there isn't, because it's declared as packed.)

Ben.

Signature · 3 lines
Ben Hutchings, Staff Engineer, Solarflare
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.
author
#11 lore raw
Quoted 12 lines · Ben Hutchings

This function calls rds_copy_info() to copy the whole of ctr into userland.

If ctr is not completely initialised, then the values of the uninitialised bytes are left over from the local variables of an earlier system call. If an attacker knows enough about the stack layout (easy if this is a distribution kernel), they can make a series of system calls that leak information about heap-allocated objects. That can help them to exploit other kernel bugs for privilege escalation. So we should initialise every bit of memory that is going to be copied to userland.

Quoted 5 lines

(In fact, in general it's not even enough to initialise all fields of the structure, because there may be padding bytes between them. In this case we know there isn't, because it's declared as packed.)

Ben.

thank you for your information.

I should send patch v2 for it.

:-)

Signature · 3 lines
Chen Gang

Asianux Corporation
author
#12 lore raw

PATCH 1/1 net/rds: zero last byte for strncpy

In reply to Ben Hutchings

for NUL terminated string, need be always sure '\0' in the end.

additional info:
strncpy will pads with zeroes to the end of the given buffer. should initialise every bit of memory that is going to be copied to userland

---
 net/rds/stats.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
net/rds/stats.c +1 -0
index 7be790d..73be187 100644
@@ -87,6 +87,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';
 		ctr.value = values[i];
 
 		rds_info_copy(iter, &ctr, sizeof(ctr));
Signature · 1 line
1.7.7.6
maintainer Applied
#13 lore raw

Re: [PATCH] net/rds: zero last byte for strncpy

From: Chen Gang <[email protected]>
Date: Fri, 08 Mar 2013 11:19:32 +0800

Quoted 10 lines

for NUL terminated string, need be always sure '\0' in the end.

additional info:
strncpy will pads with zeroes to the end of the given buffer. should initialise every bit of memory that is going to be copied to userland

Applied.

author
#14 lore raw

Re: [PATCH] net/rds: zero last byte for strncpy

Quoted 11 lines · David Miller

From: Chen Gang <[email protected]>
Date: Fri, 08 Mar 2013 11:19:32 +0800

Quoted 8 lines

for NUL terminated string, need be always sure '\0' in the end.

additional info:
strncpy will pads with zeroes to the end of the given buffer. should initialise every bit of memory that is going to be copied to userland

Quoted 6 lines
Quoted 2 lines

Applied.

thanks.

Signature · 3 lines
Chen Gang

Asianux Corporation