Skip to contentSkip to conversation

iio: gyro: adis16136: use scnprintf instead of snprintf

PATCH In review Rohit Sarkar 4 participants 6 messages Mar 18, 2020 – Mar 23, 2020

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

PATCH 1/1 iio: gyro: adis16136: use scnprintf instead of snprintf

scnprintf returns the actual number of bytes written into the buffer as opposed to snprintf which returns the number of bytes that would have been written if the buffer was big enough. Using the output of snprintf may lead to difficult to detect bugs.

Thanks,
Rohit

---
 drivers/iio/gyro/adis16136.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/gyro/adis16136.c +1 -1
index a4c967a5fc5c..0a8bb02dc4b9 100644
@@ -96,7 +96,7 @@ static ssize_t adis16136_show_serial(struct file *file,
 	if (ret)
 		return ret;
 
-	len = snprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2,
+	len = scnprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2,
 		lot3, serial);
 
 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
Signature · 1 line
2.23.0.385.gbc12974a89
#1 lore raw
Quoted 4 lines · Rohit Sarkar

scnprintf returns the actual number of bytes written into the buffer as opposed to snprintf which returns the number of bytes that would have been written if the buffer was big enough. Using the output of snprintf may lead to difficult to detect bugs.

Nice. Have you investigate the code?

drivers/iio/gyro/adis16136.c static ssize_t adis16136_show_serial(struct file *file, · line 102

Quoted 9 lines
drivers/iio/gyro/adis16136.c +1 -1
@@ -96,7 +96,7 @@ static ssize_t adis16136_show_serial(struct file *file,
 	if (ret)
 		return ret;
 
-	len = snprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2,
+	len = scnprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2,
 		lot3, serial);
 
 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);

The buffer size is 20, the pattern size I count to 19. Do you think snprintf() can fail?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 12 lines · Andy Shevchenko
Quoted 4 lines · Rohit Sarkar

scnprintf returns the actual number of bytes written into the buffer as opposed to snprintf which returns the number of bytes that would have been written if the buffer was big enough. Using the output of snprintf may lead to difficult to detect bugs.

Nice. Have you investigate the code?

Quoted 4 lines
drivers/iio/gyro/adis16136.c +0 -0
@@ -96,7 +96,7 @@ static ssize_t adis16136_show_serial(struct file *file,
 	if (ret)
 		return ret;
 
Quoted 8 lines

drivers/iio/gyro/adis16136.c static ssize_t adis16136_show_serial(struct file *file, · line 102

Quoted 5 lines
drivers/iio/gyro/adis16136.c +1 -1
-	len = snprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2,
+	len = scnprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2,
 		lot3, serial);
 
 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);

The buffer size is 20, the pattern size I count to 19. Do you think snprintf() can fail?

That might be the case, but IMO using scnprintf can be considered as a best practice. There is no overhead with this change and further if the pattern is changed by someone in the future they might overlook the buffersize
Thanks,
Rohit

Quoted 5 lines
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 18 lines · Rohit Sarkar
Quoted 16 lines · Andy Shevchenko
Quoted 4 lines · Rohit Sarkar

scnprintf returns the actual number of bytes written into the buffer as opposed to snprintf which returns the number of bytes that would have been written if the buffer was big enough. Using the output of snprintf may lead to difficult to detect bugs.

Nice. Have you investigate the code?

Quoted 8 lines
drivers/iio/gyro/adis16136.c +1 -1
@@ -96,7 +96,7 @@ static ssize_t adis16136_show_serial(struct file *file,
    if (ret)
            return ret;
 
-   len = snprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2,
+   len = scnprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2,
            lot3, serial);
 
Quoted 8 lines
Quoted 4 lines

drivers/iio/gyro/adis16136.c static ssize_t adis16136_show_serial(struct file *file, · line 102

Quoted 1 line
drivers/iio/gyro/adis16136.c +0 -0
    return simple_read_from_buffer(userbuf, count, ppos, buf, len);

The buffer size is 20, the pattern size I count to 19. Do you think snprintf() can fail?

That might be the case, but IMO using scnprintf can be considered as a best practice. There is no overhead with this change and further if the pattern is changed by someone in the future they might overlook the buffersize

If we cut the string above we will give wrong information to the user space. I think scnprintf() change is a noise and does not improve the situation anyhow.

So, when anybody modifying such code the test should be performed.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

From: Andy Shevchenko

Quoted 24 lines

Sent: 22 March 2020 10:27

Quoted 22 lines · Rohit Sarkar
Quoted 20 lines · Andy Shevchenko
Quoted 4 lines · Rohit Sarkar

scnprintf returns the actual number of bytes written into the buffer as opposed to snprintf which returns the number of bytes that would have been written if the buffer was big enough. Using the output of snprintf may lead to difficult to detect bugs.

Nice. Have you investigate the code?

drivers/iio/gyro/adis16136.c static ssize_t adis16136_show_serial(struct file *file, · line 102

Quoted 9 lines
drivers/iio/gyro/adis16136.c +1 -1
@@ -96,7 +96,7 @@ static ssize_t adis16136_show_serial(struct file *file,
    if (ret)
            return ret;
 
-   len = snprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2,
+   len = scnprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2,
            lot3, serial);
 
    return simple_read_from_buffer(userbuf, count, ppos, buf, len);

The buffer size is 20, the pattern size I count to 19. Do you think snprintf() can fail?

Quoted 7 lines
Quoted 4 lines

That might be the case, but IMO using scnprintf can be considered as a best practice. There is no overhead with this change and further if the pattern is changed by someone in the future they might overlook the buffersize

If we cut the string above we will give wrong information to the user space. I think scnprintf() change is a noise and does not improve the situation anyhow.

If, for any reason, any of the values are large the user will get corrupt data.
But you don't want to leak random kernel memory to the user.

So while you may be able to prove that this particular snprintf() can't overflow, in general checking it requires knowledge of the code.

With scnprintf() you know nothing odd will happen.

FWIW I suspect the 'standard' return value from snprintf() comes from the return value of the original user-space implementations which faked-up a FILE structure on stack and just silently discarded the output bytes that wouldn't fit in the buffer (they'd usually by flushed to a real file).
The original sprintf() just specified a very big length so the flush would never be requested.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#5 lore raw
Quoted 30 lines · David Laight

From: Andy Shevchenko

Quoted 29 lines

Sent: 22 March 2020 10:27

Quoted 26 lines · Rohit Sarkar
Quoted 12 lines · Andy Shevchenko
Quoted 4 lines · Rohit Sarkar

scnprintf returns the actual number of bytes written into the buffer as opposed to snprintf which returns the number of bytes that would have been written if the buffer was big enough. Using the output of snprintf may lead to difficult to detect bugs.

Nice. Have you investigate the code?

Quoted 4 lines
drivers/iio/gyro/adis16136.c +0 -0
@@ -96,7 +96,7 @@ static ssize_t adis16136_show_serial(struct file *file,
    if (ret)
            return ret;
 
Quoted 8 lines

drivers/iio/gyro/adis16136.c static ssize_t adis16136_show_serial(struct file *file, · line 102

Quoted 5 lines
drivers/iio/gyro/adis16136.c +1 -1
-   len = snprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2,
+   len = scnprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2,
            lot3, serial);
 
    return simple_read_from_buffer(userbuf, count, ppos, buf, len);

The buffer size is 20, the pattern size I count to 19. Do you think snprintf() can fail?

That might be the case, but IMO using scnprintf can be considered as a best practice. There is no overhead with this change and further if the pattern is changed by someone in the future they might overlook the buffersize

Quoted 7 lines
Quoted 2 lines

If we cut the string above we will give wrong information to the user space. I think scnprintf() change is a noise and does not improve the situation anyhow.

If, for any reason, any of the values are large the user will get corrupt data.

But you don't want to leak random kernel memory to the user.

How? Kernel already got crashed at this point.

Quoted 3 lines

So while you may be able to prove that this particular snprintf() can't overflow, in general checking it requires knowledge of the code.

Here it's still a noise.

Quoted 1 line

With scnprintf() you know nothing odd will happen.

...and quite likely hide a lot of issues.

Really any "micro" / "small" correction / optimization to be very carefully thought through.

Quoted 7 lines

FWIW I suspect the 'standard' return value from snprintf() comes from the return value of the original user-space implementations which faked-up a FILE structure on stack and just silently discarded the output bytes that wouldn't fit in the buffer (they'd usually by flushed to a real file).
The original sprintf() just specified a very big length so the flush would never be requested.

Signature · 2 lines
With Best Regards,
Andy Shevchenko