Skip to contentSkip to conversation

lib/string_helpers: rework overflow-dependent code

PATCH Applied Justin Stitt 3 participants 10 messages Aug 8, 2024 – Aug 12, 2024

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

PATCH 1/1 lib/string_helpers: rework overflow-dependent code

When @size is 0, the desired behavior is to allow unlimited bytes to be parsed. Currently, this relies on some intentional arithmetic overflow where --size gives us SIZE_MAX when size is 0.

Explicitly spell out the desired behavior without relying on intentional overflow/underflow.

---
 lib/string_helpers.c | 3 +++
 1 file changed, 3 insertions(+)
lib/string_helpers.c +3 -0
index 69ba49b853c7..4f887aa62fa0 100644
@@ -321,6 +321,9 @@ int string_unescape(char *src, char *dst, size_t size, unsigned int flags)
 {
 	char *out = dst;
 
+	if (!size)
+		size = SIZE_MAX;
+
 	while (*src && --size) {
 		if (src[0] == '\\' && src[1] != '\0' && size > 1) {
 			src++;

---
base-commit: ee9a43b7cfe2d8a3520335fea7d8ce71b8cabd9d
change-id: 20240808-b4-string_helpers_caa133-53effde85a18

Best regards,
--
Justin Stitt <[email protected]>

#1 lore raw
Quoted 7 lines · Justin Stitt

When @size is 0, the desired behavior is to allow unlimited bytes to be parsed. Currently, this relies on some intentional arithmetic overflow where --size gives us SIZE_MAX when size is 0.

Explicitly spell out the desired behavior without relying on intentional overflow/underflow.

Hmm... but why? Overflow for the _unsigned_ types is okay. No?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#2 lore raw
Quoted 10 lines · Andy Shevchenko
Quoted 7 lines · Justin Stitt

When @size is 0, the desired behavior is to allow unlimited bytes to be parsed. Currently, this relies on some intentional arithmetic overflow where --size gives us SIZE_MAX when size is 0.

Explicitly spell out the desired behavior without relying on intentional overflow/underflow.

Hmm... but why? Overflow for the _unsigned_ types is okay. No?

Yes, it's well defined, but in trying to find a place to start making a meaningful impact on unexpected wrap-around, after discussions with Linus and Peter Zijlstra, we're going taking a stab at defining size_t as not expecting to wrap. Justin has been collecting false positive fixes while working on the compiler side of this, and I had asked him to send this one now since I think it additionally helps with readability.

Signature · 1 line
Kees Cook
#3 lore raw
Quoted 11 lines · Kees Cook
Quoted 9 lines · Andy Shevchenko
Quoted 7 lines · Justin Stitt

When @size is 0, the desired behavior is to allow unlimited bytes to be parsed. Currently, this relies on some intentional arithmetic overflow where --size gives us SIZE_MAX when size is 0.

Explicitly spell out the desired behavior without relying on intentional overflow/underflow.

Quoted 8 lines
Quoted 1 line

Hmm... but why? Overflow for the _unsigned_ types is okay. No?

Yes, it's well defined, but in trying to find a place to start making a meaningful impact on unexpected wrap-around, after discussions with Linus and Peter Zijlstra, we're going taking a stab at defining size_t as not expecting to wrap. Justin has been collecting false positive fixes while working on the compiler side of this, and I had asked him to send this one now since I think it additionally helps with readability.

Okay, but the patch has an off-by-one error (which has no impact on the behavior as it's close to unrealistic to have the SIZE_MAX array). I prefer that patch can be reconsidered to keep original behaviour, otherwise it might be not so clear why 0 is SIZE_MAX - 1 in _this_ case.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 21 lines · Andy Shevchenko
Quoted 11 lines · Kees Cook
Quoted 9 lines · Andy Shevchenko
Quoted 7 lines · Justin Stitt

When @size is 0, the desired behavior is to allow unlimited bytes to be parsed. Currently, this relies on some intentional arithmetic overflow where --size gives us SIZE_MAX when size is 0.

Explicitly spell out the desired behavior without relying on intentional overflow/underflow.

Quoted 8 lines
Quoted 1 line

Hmm... but why? Overflow for the _unsigned_ types is okay. No?

Yes, it's well defined, but in trying to find a place to start making a meaningful impact on unexpected wrap-around, after discussions with Linus and Peter Zijlstra, we're going taking a stab at defining size_t as not expecting to wrap. Justin has been collecting false positive fixes while working on the compiler side of this, and I had asked him to send this one now since I think it additionally helps with readability.

Quoted 5 lines

Okay, but the patch has an off-by-one error (which has no impact on the behavior as it's close to unrealistic to have the SIZE_MAX array). I prefer that patch can be reconsidered to keep original behaviour, otherwise it might be not so clear why 0 is SIZE_MAX - 1 in _this_ case.

Right, it is technically different but still functionally provides the "unlimited" behavior.

But, we could do this too:

lib/string_helpers.c +5 -3
index 69ba49b853c7..0f76b5288833 100644
@@ -320,11 +320,13 @@ static bool unescape_special(char **src, char **dst)
 int string_unescape(char *src, char *dst, size_t size, unsigned int flags)
 {
 	char *out = dst;
+	bool unlimited = !size;
 
-	while (*src && --size) {
-		if (src[0] == '\\' && src[1] != '\0' && size > 1) {
+	while (*src && (unlimited || --size)) {
+		if (src[0] == '\\' && src[1] != '\0' &&
+		    (unlimited || size > 1)) {
 			src++;
-			size--;
+			size -= !unlimited;
 
 			if (flags & UNESCAPE_SPACE &&
 					unescape_space(&src, &out))

Really, I am fine with either.

Thanks
Justin

#5 lore raw
Quoted 2 lines · Justin Stitt
Quoted 1 line · Andy Shevchenko

On Fri, Aug 9, 2024 at 2:11 AM Kees Cook <[email protected]> wrote:

...

Quoted 28 lines
Quoted 5 lines

Okay, but the patch has an off-by-one error (which has no impact on the behavior as it's close to unrealistic to have the SIZE_MAX array). I prefer that patch can be reconsidered to keep original behaviour, otherwise it might be not so clear why 0 is SIZE_MAX - 1 in _this_ case.

Right, it is technically different but still functionally provides the "unlimited" behavior.

But, we could do this too:

int string_unescape(char *src, char *dst, size_t size, unsigned int flags)

 {
        char *out = dst;
lib/string_helpers.c +5 -3
+       bool unlimited = !size;
 
-       while (*src && --size) {
-               if (src[0] == '\\' && src[1] != '\0' && size > 1) {
+       while (*src && (unlimited || --size)) {
+               if (src[0] == '\\' && src[1] != '\0' &&
+                   (unlimited || size > 1)) {
                        src++;
-                       size--;
+                       size -= !unlimited;
 
                        if (flags & UNESCAPE_SPACE &&
                                        unescape_space(&src, &out))
Quoted 1 line

Really, I am fine with either.

This one is worse, I think.
Let's take time and not hurry up and think more about better approaches.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Justin Stitt

On Fri, Aug 09, 2024 at 02:07:57PM GMT, Andy Shevchenko wrote:

...

Quoted 7 lines

lib/string_helpers.c static bool unescape_special(char **src, char **dst) · line 325

Quoted 4 lines

But, we could do this too:

lib/string_helpers.c +0 -2
-       while (*src && --size) {
-               if (src[0] == '\\' && src[1] != '\0' && size > 1) {

This one is worse, I think.
Let's take time and not hurry up and think more about better approaches.

Btw, have you played with the "do {} while (size);" approach?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw
Quoted 10 lines · Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Justin Stitt

On Fri, Aug 09, 2024 at 02:07:57PM GMT, Andy Shevchenko wrote:

...

Quoted 1 line
Quoted 1 line

But, we could do this too:

Quoted 7 lines
Quoted 5 lines

lib/string_helpers.c static bool unescape_special(char **src, char **dst) · line 325

Quoted 2 lines
lib/string_helpers.c +0 -2
-       while (*src && --size) {
-               if (src[0] == '\\' && src[1] != '\0' && size > 1) {

This one is worse, I think.
Let's take time and not hurry up and think more about better approaches.

Btw, have you played with the "do {} while (size);" approach?

Nope. Although, lots of ideas can work here. We can reorder the logic any which way, really.

I am happy to send a patch following any idea -- so long as the overflow-dependent code is gone :)

Quoted 4 lines

--
With Best Regards,
Andy Shevchenko

maintainer Applied
#8 lore raw
In reply to Justin Stitt
Quoted 8 lines · Justin Stitt

When @size is 0, the desired behavior is to allow unlimited bytes to be parsed. Currently, this relies on some intentional arithmetic overflow where --size gives us SIZE_MAX when size is 0.

Explicitly spell out the desired behavior without relying on intentional overflow/underflow.

[...]

Since we can't have allocations larger than INT_MAX, SIZE_MAX is sufficiently large for this use-case, and the "off by one" introduce here isn't meaningful. I prefer the improved readability and lack of wrapping pre-decrement.

Applied to for-next/hardening, thanks!

[1/1] lib/string_helpers: rework overflow-dependent code

      https://git.kernel.org/kees/c/5d6b91b74ccd

Take care,

Signature · 1 line
Kees Cook
#9 lore raw
Quoted 9 lines · Kees Cook
Quoted 7 lines · Justin Stitt

When @size is 0, the desired behavior is to allow unlimited bytes to be parsed. Currently, this relies on some intentional arithmetic overflow where --size gives us SIZE_MAX when size is 0.

Explicitly spell out the desired behavior without relying on intentional overflow/underflow.

Quoted 6 lines
Quoted 1 line

[...]

Since we can't have allocations larger than INT_MAX, SIZE_MAX is sufficiently large for this use-case, and the "off by one" introduce here isn't meaningful. I prefer the improved readability and lack of wrapping pre-decrement.

Great! Thanks.

Quoted 11 lines

Applied to for-next/hardening, thanks!

[1/1] lib/string_helpers: rework overflow-dependent code

      https://git.kernel.org/kees/c/5d6b91b74ccd

Take care,

--
Kees Cook