lib/vsprintf: Implement spprintf() to catch truncated strings
Subsystems (2)
PATCH v3 1/1 lib/vsprintf: Implement spprintf() to catch truncated strings
There is an ongoing effort to replace the use of {v}snprintf() variants with safer alternatives - for a more in depth view, see Jon's write-up on LWN [0] and/or Alex's on the Kernel Self Protection Project [1].
Whist executing the task, it quickly became apparent that the initial thought of simply s/snprintf/scnprintf/ wasn't going to be adequate for a number of cases. Specifically ones where the caller needs to know whether the given string ends up being truncated. This is where spprintf() comes in, since it takes the best parts of both of the aforementioned variants. It has the testability of truncation of snprintf() and returns the number of Bytes *actually* written, similar to scnprintf(), making it a very programmer friendly alternative.
Here's some examples to show the differences:
Success: No truncation - all 9 Bytes successfully written to the buffer
ret = snprintf (buf, 10, "%s", "123456789"); // ret = 9
ret = scnprintf(buf, 10, "%s", "123456789"); // ret = 9
ret = spprintf (buf, 10, "%s", "123456789"); // ret = 9Failure: Truncation - only 9 of 10 Bytes written; '-' is truncated
ret = snprintf (buf, 10, "%s", "123456789---"); // ret = 12
Reports: "12 Bytes would have been written if buf was large enough"
Issue: Too easy for programmers to assume ret is Bytes written
ret = scnprintf(buf, 10, "%s", "123456789---"); // ret = 9
Reports: "9 Bytes actually written"
Issue: Not testable - returns 9 on success AND failure (see above)
ret = spprintf (buf, 10, "%s", "123456789---"); // ret = 10
Reports: "Data provided is too large to fit in the buffer"
Issue: No tangible impact: No way to tell how much data was lost
Since spprintf() only reports the total size of the buffer, it's easy to test if they buffer overflowed since if we include the compulsory '\0', only 9 Bytes additional Bytes can fit, so the return of 10 informs the caller of an overflow. Also, if the return data is plugged straight into an additional call to spprintf() after the occurrence of an overflow, no out-of-bounds will occur:
int size = 10;
char buf[size];
char *b = buf;
ret = spprintf(b, size, "1234");
size -= ret;
b += ret;
// ret = 4 size = 6 buf = "1234\0"
ret = spprintf(b, size, "5678");
size -= ret;
b += ret;
// ret = 4 size = 2 buf = "12345678\0"
ret = spprintf(b, size, "9***");
size -= ret;
b += ret;
// ret = 2 size = 0 buf = "123456789\0"Since size is now 0, further calls result in no changes of state.
ret = spprintf(b, size, "----");
size -= ret;
b += ret;
// ret = 0 size = 0 buf = "123456789\0"[0] https://lwn.net/Articles/69419/
[1] https://github.com/KSPP/linux/issues/105
---
Changelog:
v1 => v2:
- Address Rasmus Villemoes's review comments:
- Remove explicit check for zero sized buffer (-E2BIG is appropriate)
- Remove unreachable branch in vssprintf()
v2 => v3:
- Address session from David Laight
- Return 'size' instead of '-E2BIG'
include/linux/sprintf.h | 2 ++ lib/vsprintf.c | 51 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+)
include/linux/sprintf.h +2 -0
@@ -13,6 +13,8 @@ __printf(3, 4) int snprintf(char *buf, size_t size, const char *fmt, ...); __printf(3, 0) int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); __printf(3, 4) int scnprintf(char *buf, size_t size, const char *fmt, ...); __printf(3, 0) int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); +__printf(3, 4) int spprintf(char *buf, size_t size, const char *fmt, ...); +__printf(3, 0) int vspprintf(char *buf, size_t size, const char *fmt, va_list args); __printf(2, 3) __malloc char *kasprintf(gfp_t gfp, const char *fmt, ...); __printf(2, 0) __malloc char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); __printf(2, 0) const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
lib/vsprintf.c +51 -0
63 lines not on this page · raw message
Signature · 1 line
2.43.0.429.g432eaa2c6b-goog
Andy Shevchenko
reviewerQuoted 66 lines · Lee Jones
There is an ongoing effort to replace the use of {v}snprintf() variants with safer alternatives - for a more in depth view, see Jon's write-up on LWN [0] and/or Alex's on the Kernel Self Protection Project [1].
Whist executing the task, it quickly became apparent that the initial thought of simply s/snprintf/scnprintf/ wasn't going to be adequate for a number of cases. Specifically ones where the caller needs to know whether the given string ends up being truncated. This is where
spprintf()comes in, since it takes the best parts of both of the aforementioned variants. It has the testability of truncation ofsnprintf()and returns the number of Bytes *actually* written, similar toscnprintf(), making it a very programmer friendly alternative.Here's some examples to show the differences:
Success: No truncation - all 9 Bytes successfully written to the buffer
ret = snprintf (buf, 10, "%s", "123456789"); // ret = 9 ret = scnprintf(buf, 10, "%s", "123456789"); // ret = 9 ret = spprintf (buf, 10, "%s", "123456789"); // ret = 9Failure: Truncation - only 9 of 10 Bytes written; '-' is truncated
ret = snprintf (buf, 10, "%s", "123456789---"); // ret = 12
Reports: "12 Bytes would have been written if buf was large enough"
Issue: Too easy for programmers to assume ret is Bytes writtenret =
scnprintf(buf, 10, "%s", "123456789---"); // ret = 9Reports: "9 Bytes actually written"
Issue: Not testable - returns 9 on success AND failure (see above)ret = spprintf (buf, 10, "%s", "123456789---"); // ret = 10
Reports: "Data provided is too large to fit in the buffer"
Issue: No tangible impact: No way to tell how much data was lostSince
spprintf()only reports the total size of the buffer, it's easy to test if they buffer overflowed since if we include the compulsory '\0', only 9 Bytes additional Bytes can fit, so the return of 10 informs the caller of an overflow. Also, if the return data is plugged straight into an additional call tospprintf()after the occurrence of an overflow, no out-of-bounds will occur:int size = 10; char buf[size]; char *b = buf; ret = spprintf(b, size, "1234"); size -= ret; b += ret; // ret = 4 size = 6 buf = "1234\0" ret = spprintf(b, size, "5678"); size -= ret; b += ret; // ret = 4 size = 2 buf = "12345678\0" ret = spprintf(b, size, "9***"); size -= ret; b += ret; // ret = 2 size = 0 buf = "123456789\0"Since size is now 0, further calls result in no changes of state.
Quoted 7 lines
ret = spprintf(b, size, "----"); size -= ret; b += ret; // ret = 0 size = 0 buf = "123456789\0"[0] https://lwn.net/Articles/69419/
[1] https://github.com/KSPP/linux/issues/105
Quoted 1 line
Signed-off-by Lee Jones <[email protected]>
...
I'm a bit late in this discussion, but the commit message doesn't spit a single word on why seq_buf() approach can't be used in those cases?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 72 lines · Andy Shevchenko
Quoted 70 lines · Lee Jones
There is an ongoing effort to replace the use of {v}snprintf() variants with safer alternatives - for a more in depth view, see Jon's write-up on LWN [0] and/or Alex's on the Kernel Self Protection Project [1].
Whist executing the task, it quickly became apparent that the initial thought of simply s/snprintf/scnprintf/ wasn't going to be adequate for a number of cases. Specifically ones where the caller needs to know whether the given string ends up being truncated. This is where
spprintf()comes in, since it takes the best parts of both of the aforementioned variants. It has the testability of truncation ofsnprintf()and returns the number of Bytes *actually* written, similar toscnprintf(), making it a very programmer friendly alternative.Here's some examples to show the differences:
Success: No truncation - all 9 Bytes successfully written to the buffer
ret = snprintf (buf, 10, "%s", "123456789"); // ret = 9 ret = scnprintf(buf, 10, "%s", "123456789"); // ret = 9 ret = spprintf (buf, 10, "%s", "123456789"); // ret = 9Failure: Truncation - only 9 of 10 Bytes written; '-' is truncated
ret = snprintf (buf, 10, "%s", "123456789---"); // ret = 12
Reports: "12 Bytes would have been written if buf was large enough"
Issue: Too easy for programmers to assume ret is Bytes writtenret =
scnprintf(buf, 10, "%s", "123456789---"); // ret = 9Reports: "9 Bytes actually written"
Issue: Not testable - returns 9 on success AND failure (see above)ret = spprintf (buf, 10, "%s", "123456789---"); // ret = 10
Reports: "Data provided is too large to fit in the buffer"
Issue: No tangible impact: No way to tell how much data was lostSince
spprintf()only reports the total size of the buffer, it's easy to test if they buffer overflowed since if we include the compulsory '\0', only 9 Bytes additional Bytes can fit, so the return of 10 informs the caller of an overflow. Also, if the return data is plugged straight into an additional call tospprintf()after the occurrence of an overflow, no out-of-bounds will occur:int size = 10; char buf[size]; char *b = buf; ret = spprintf(b, size, "1234"); size -= ret; b += ret; // ret = 4 size = 6 buf = "1234\0" ret = spprintf(b, size, "5678"); size -= ret; b += ret; // ret = 4 size = 2 buf = "12345678\0" ret = spprintf(b, size, "9***"); size -= ret; b += ret; // ret = 2 size = 0 buf = "123456789\0"Since size is now 0, further calls result in no changes of state.
ret = spprintf(b, size, "----"); size -= ret; b += ret; // ret = 0 size = 0 buf = "123456789\0"
Quoted 5 lines
Quoted 2 lines
[0] https://lwn.net/Articles/69419/
[1] https://github.com/KSPP/linux/issues/105Link ... [0]Link ... [1]
OOI, what does that do?
Does tooling pick-up on them?
These links are for humans.
Is there documentation I can go look at?
Quoted 6 lines
Quoted 1 line
Signed-off-by Lee Jones <[email protected]>...
I'm a bit late in this discussion, but the commit message doesn't spit a single word on why
seq_buf()approach can't be used in those cases?
When I can carve out a little more free time, investigating seq_buf() is the next step.
Signature · 1 line
Lee Jones [李琼斯]
Andy Shevchenko
reviewerQuoted 2 lines · Lee Jones
Quoted 1 line · Andy Shevchenko
On Tue, Jan 30, 2024 at 04:09:53PM +0000, Lee Jones wrote:
...
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
[0] https://lwn.net/Articles/69419/
[1] https://github.com/KSPP/linux/issues/105Link ... [0]Link ... [1]OOI, what does that do?
Does tooling pick-up on them?
Yes, b4 shows them when you do b4 am and then you can click from the terminal.
Quoted 1 line
These links are for humans.
No objections on this.
Quoted 1 line
Is there documentation I can go look at?
https://docs.kernel.org/process/submitting-patches.html
"If related discussions or any other background information behind the change can be found on the web, add 'Link:' tags pointing to it."
Signature · 2 lines
With Best Regards, Andy Shevchenko