Skip to contentSkip to conversation

parisc: use strscpy() to instead of strncpy()

PATCH In review [email protected] 3 participants 7 messages Dec 23, 2022 – Dec 28, 2022 PA-RISC architecture

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 parisc: use strscpy() to instead of strncpy()

From: Xu Panda <[email protected]>

The implementation of strscpy() is more robust and safer. That's now the recommended way to copy NUL-terminated strings.

---
 drivers/parisc/pdc_stable.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/parisc/pdc_stable.c +3 -6
index d6af5726ddf3..403bca0021c5 100644
@@ -274,8 +274,7 @@ pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t coun
 
 	/* We'll use a local copy of buf */
 	count = min_t(size_t, count, sizeof(in)-1);
-	strncpy(in, buf, count);
-	in[count] = '\0';
+	strscpy(in, buf, count + 1);
 	
 	/* Let's clean up the target. 0xff is a blank pattern */
 	memset(&hwpath, 0xff, sizeof(hwpath));
@@ -388,8 +387,7 @@ pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count
 
 	/* We'll use a local copy of buf */
 	count = min_t(size_t, count, sizeof(in)-1);
-	strncpy(in, buf, count);
-	in[count] = '\0';
+	strscpy(in, buf, count + 1);
 	
 	/* Let's clean up the target. 0 is a blank pattern */
 	memset(&layers, 0, sizeof(layers));
@@ -756,8 +754,7 @@ static ssize_t pdcs_auto_write(struct kobject *kobj,
 
 	/* We'll use a local copy of buf */
 	count = min_t(size_t, count, sizeof(in)-1);
-	strncpy(in, buf, count);
-	in[count] = '\0';
+	strscpy(in, buf, count + 1);
 
 	/* Current flags are stored in primary boot path entry */
 	pathentry = &pdcspath_entry_primary;
Signature · 1 line
2.15.2
maintainer
#1 lore raw
Quoted 4 lines · On 12/23/22 03:40, [email protected] wrote:

From: Xu Panda <[email protected]>

The implementation of strscpy() is more robust and safer. That's now the recommended way to copy NUL-terminated strings.

Thanks for your patch, but....

Quoted 11 lines
---
  drivers/parisc/pdc_stable.c | 9 +++------
  1 file changed, 3 insertions(+), 6 deletions(-)
drivers/parisc/pdc_stable.c +0 -0
index d6af5726ddf3..403bca0021c5 100644

drivers/parisc/pdc_stable.c pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t coun · line 277

Quoted 6 lines
drivers/parisc/pdc_stable.c +1 -2
@@ -274,8 +274,7 @@ pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t coun
 
  	/* We'll use a local copy of buf */
  	count = min_t(size_t, count, sizeof(in)-1);
-	strncpy(in, buf, count);
-	in[count] = '\0';
+	strscpy(in, buf, count + 1);

could you resend it somewhat simplified, e.g.
strscpy(in, buf, sizeof(in));

drivers/parisc/pdc_stable.c pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count · line 390

Quoted 10 lines
drivers/parisc/pdc_stable.c +1 -2
  	/* Let's clean up the target. 0xff is a blank pattern */
  	memset(&hwpath, 0xff, sizeof(hwpath));
@@ -388,8 +387,7 @@ pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count
 
  	/* We'll use a local copy of buf */
  	count = min_t(size_t, count, sizeof(in)-1);
-	strncpy(in, buf, count);
-	in[count] = '\0';
+	strscpy(in, buf, count + 1);

same here

drivers/parisc/pdc_stable.c static ssize_t pdcs_auto_write(struct kobject *kobj, · line 757

Quoted 10 lines
drivers/parisc/pdc_stable.c +1 -2
  	/* Let's clean up the target. 0 is a blank pattern */
  	memset(&layers, 0, sizeof(layers));
@@ -756,8 +754,7 @@ static ssize_t pdcs_auto_write(struct kobject *kobj,
 
  	/* We'll use a local copy of buf */
  	count = min_t(size_t, count, sizeof(in)-1);
-	strncpy(in, buf, count);
-	in[count] = '\0';
+	strscpy(in, buf, count + 1);

and here

Quoted 3 lines
drivers/parisc/pdc_stable.c +0 -0
  	/* Current flags are stored in primary boot path entry */
  	pathentry = &pdcspath_entry_primary;
maintainer
#2 lore raw
Quoted 22 lines · Helge Deller
Quoted 4 lines · On 12/23/22 03:40, [email protected] wrote:

From: Xu Panda <[email protected]>

The implementation of strscpy() is more robust and safer. That's now the recommended way to copy NUL-terminated strings.

Thanks for your patch, but....

Quoted 14 lines

---
  drivers/parisc/pdc_stable.c | 9 +++------

  1 file changed, 3 insertions(+), 6 deletions(-)
drivers/parisc/pdc_stable.c +0 -0
index d6af5726ddf3..403bca0021c5 100644
@@ -274,8 +274,7 @@ pdcspath_hwpath_write(struct pdcspath_entry
 *entry, const char *buf, size_t coun
 
Quoted 8 lines

drivers/parisc/pdc_stable.c pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t coun · line 277

Quoted 5 lines
drivers/parisc/pdc_stable.c +1 -2
         /* We'll use a local copy of buf */
         count = min_t(size_t, count, sizeof(in)-1);
-       strncpy(in, buf, count);
-       in[count] = '\0';
+       strscpy(in, buf, count + 1);

could you resend it somewhat simplified, e.g.
strscpy(in, buf, sizeof(in));

I don't think you can: count is the size of buf, if that's < sizeof(in) you've introduced a write beyond end of buffer. In fact sysfs tends to pass pages as buffers, so there's no actual problem, but if that ever changed ...

James

maintainer
#3 lore raw

Hi James,

Quoted 32 lines · James Bottomley
Quoted 22 lines · Helge Deller
Quoted 4 lines · On 12/23/22 03:40, [email protected] wrote:

From: Xu Panda <[email protected]>

The implementation of strscpy() is more robust and safer. That's now the recommended way to copy NUL-terminated strings.

Thanks for your patch, but....

Quoted 14 lines
---
   drivers/parisc/pdc_stable.c | 9 +++------
   1 file changed, 3 insertions(+), 6 deletions(-)
drivers/parisc/pdc_stable.c +0 -0
index d6af5726ddf3..403bca0021c5 100644
@@ -274,8 +274,7 @@ pdcspath_hwpath_write(struct pdcspath_entry
 *entry, const char *buf, size_t coun
 
Quoted 8 lines

drivers/parisc/pdc_stable.c pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t coun · line 277

Quoted 5 lines
drivers/parisc/pdc_stable.c +1 -2
         /* We'll use a local copy of buf */
         count = min_t(size_t, count, sizeof(in)-1);
-       strncpy(in, buf, count);
-       in[count] = '\0';
+       strscpy(in, buf, count + 1);

could you resend it somewhat simplified, e.g.
strscpy(in, buf, sizeof(in));

Quoted 4 lines

I don't think you can: count is the size of buf, if that's < sizeof(in) you've introduced a write beyond end of buffer. In fact sysfs tends to pass pages as buffers, so there's no actual problem, but if that ever changed ...

Huh?... he doesn't change "count", so what's wrong with the latest patch?

Helge

maintainer
#4 lore raw
Quoted 35 lines · Helge Deller

Hi James,

Quoted 32 lines · James Bottomley
Quoted 30 lines · Helge Deller
Quoted 4 lines · On 12/23/22 03:40, [email protected] wrote:

From: Xu Panda <[email protected]>

The implementation of strscpy() is more robust and safer. That's now the recommended way to copy NUL-terminated strings.

Thanks for your patch, but....

Quoted 12 lines

---
   drivers/parisc/pdc_stable.c | 9 +++------

   1 file changed, 3 insertions(+), 6 deletions(-)
drivers/parisc/pdc_stable.c +0 -0
index d6af5726ddf3..403bca0021c5 100644

drivers/parisc/pdc_stable.c pdcspath_hwpath_write(struct pdcspath_entry · line 278

Quoted 7 lines
drivers/parisc/pdc_stable.c +1 -2
@@ -274,8 +274,7 @@ pdcspath_hwpath_write(struct pdcspath_entry
 *entry, const char *buf, size_t coun
 
          /* We'll use a local copy of buf */
          count = min_t(size_t, count, sizeof(in)-1);
-       strncpy(in, buf, count);
-       in[count] = '\0';
+       strscpy(in, buf, count + 1);

could you resend it somewhat simplified, e.g.
strscpy(in, buf, sizeof(in));

Quoted 7 lines
Quoted 4 lines

I don't think you can: count is the size of buf, if that's < sizeof(in) you've introduced a write beyond end of buffer.  In fact sysfs tends to pass pages as buffers, so there's no actual problem, but if that ever changed ...

Huh?... he doesn't change "count", so what's wrong with the latest patch?

the array buf[] is actually buf[count], so if count < 64 then sizeof(buf) < sizeof(in) and you're copying whatever is after buf on the stack or wherever it comes from. The amount you copy into in[] truly has to be the smaller of count and sizeof(in). These are file operations, so you shouldn't rely on buf[] being null terminated (kernfs ensures it is, but it's a dangerous thing to rely on in the face of someone trying to exploit a stack smashing attack).

James

#5 lore raw
Quoted 7 lines

the array buf[] is actually buf[count], so if count < 64 then sizeof(buf) < sizeof(in) and you're copying whatever is after buf on the stack or wherever it comes from. The amount you copy into in[] truly has to be the smaller of count and sizeof(in). These are file operations, so you shouldn't rely on buf[] being null terminated (kernfs ensures it is, but it's a dangerous thing to rely on in the face of someone trying to exploit a stack smashing attack).

Should we send patchv3 which is back to v1, or we directly use patchv1 to continue the reviewing?

Thanks!

maintainer
#6 lore raw
In reply to James Bottomley
Quoted 41 lines · James Bottomley
Quoted 40 lines · Helge Deller

Hi James,

Quoted 36 lines · James Bottomley
Quoted 22 lines · Helge Deller
Quoted 4 lines · On 12/23/22 03:40, [email protected] wrote:

From: Xu Panda <[email protected]>

The implementation of strscpy() is more robust and safer. That's now the recommended way to copy NUL-terminated strings.

Thanks for your patch, but....

Quoted 14 lines
---
    drivers/parisc/pdc_stable.c | 9 +++------
    1 file changed, 3 insertions(+), 6 deletions(-)
drivers/parisc/pdc_stable.c +0 -0
index d6af5726ddf3..403bca0021c5 100644
@@ -274,8 +274,7 @@ pdcspath_hwpath_write(struct pdcspath_entry
 *entry, const char *buf, size_t coun
 
Quoted 8 lines

drivers/parisc/pdc_stable.c pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t coun · line 277

Quoted 5 lines
drivers/parisc/pdc_stable.c +1 -2
          /* We'll use a local copy of buf */
          count = min_t(size_t, count, sizeof(in)-1);
-       strncpy(in, buf, count);
-       in[count] = '\0';
+       strscpy(in, buf, count + 1);

could you resend it somewhat simplified, e.g.
strscpy(in, buf, sizeof(in));

I don't think you can: count is the size of buf, if that's < sizeof(in) you've introduced a write beyond end of buffer.  In fact sysfs tends to pass pages as buffers, so there's no actual problem, but if that ever changed ...

Quoted 8 lines
Quoted 2 lines

Huh?... he doesn't change "count", so what's wrong with the latest patch?

the array buf[] is actually buf[count], so if count < 64 then sizeof(buf) < sizeof(in) and you're copying whatever is after buf on the stack or wherever it comes from. The amount you copy into in[] truly has to be the smaller of count and sizeof(in). These are file operations, so you shouldn't rely on buf[] being null terminated

Ok, the main point I missed was that buf[] might not be null terminated. Thanks for the explanation.

Yang & Xu, no need to resend the patch. I'll take your v1 version.

Thanks!
Helge

Quoted 5 lines

(kernfs ensures it is, but it's a dangerous thing to rely on in the face of someone trying to exploit a stack smashing attack).

James