Skip to contentSkip to conversation

udev_rules_apply_format(): don't overflow the buffer when

PATCH In review Sergey Vlasov 2 participants 4 messages Feb 1, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 2/3 udev_rules_apply_format(): don't overflow the buffer when

When truncating the substitution string to the length specified in the format string, head[len] = '\0' could write outside the buffer if that length was too large.

---
 udev_rules.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
udev_rules.c +1 -1
index 44b41e9..90a83e5 100644
@@ -558,7 +558,7 @@ found:
 			break;
 		}
 		/* possibly truncate to format-char specified length */
-		if (len != -1) {
+		if (len >= 0 && (size_t)len < maxsize - (head-string)) {
 			head[len] = '\0';
 			dbg("truncate to %i chars, subtitution string becomes '%s'", len, head);
 		}
Signature · 12 lines
1.5.0.rc2.gd894fb


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid0709&bid&3057&dat1642
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
#1 lore raw

Re: [PATCH 2/3] udev_rules_apply_format(): don't overflow the buffer

Quoted 14 lines · Sergey Vlasov

When truncating the substitution string to the length specified in the format string, head[len] = '\0' could write outside the buffer if that length was too large.

---
 udev_rules.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
udev_rules.c +0 -0
index 44b41e9..90a83e5 100644

udev_rules.c found: · line 561

Quoted 5 lines
udev_rules.c +1 -1
@@ -558,7 +558,7 @@ found:
                        break;
                }
                /* possibly truncate to format-char specified length */
-               if (len != -1) {
+               if (len >= 0 && (size_t)len < maxsize - (head-string)) {

Yeah, that should be fixed. Wouldn't:
if (len >= 0 && len <= strlen(head))
also catch strings which fit in the buffer, but are shorter than the specified limit?

Thanks,
Kay

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier. Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid0709&bid&3057&dat1642
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#2 lore raw

Re: [PATCH 2/3] udev_rules_apply_format(): don't overflow the buffer

In reply to Sergey Vlasov
Quoted 10 lines · Kay Sievers
Quoted 9 lines · Sergey Vlasov

When truncating the substitution string to the length specified in the format string, head[len] = '\0' could write outside the buffer if that length was too large.

---
udev_rules.c | 2 +-

1 files changed, 1 insertions(+), 1 deletions(-)
Quoted 15 lines

udev_rules.c found: · line 561

Quoted 10 lines
udev_rules.c +1 -1
index 44b41e9..90a83e5 100644
@@ -558,7 +558,7 @@ found:
                       break;
               }
               /* possibly truncate to format-char specified length */
-               if (len != -1) {
+               if (len >= 0 && (size_t)len < maxsize - (head-string)) {

Yeah, that should be fixed. Wouldn't:
if (len >= 0 && len <= strlen(head))
also catch strings which fit in the buffer, but are shorter than the specified limit?

Yes, this should work properly too (or even "&& len < strlen(head)" to skip writing '\0' over itself). Working in constant time is probably not an issue here, given the number of strlcat() calls around.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier. Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#3 lore raw

Re: [PATCH 2/3] udev_rules_apply_format(): don't overflow the

In reply to Sergey Vlasov
Quoted 27 lines · Sergey Vlasov
Quoted 10 lines · Kay Sievers
Quoted 9 lines · Sergey Vlasov

When truncating the substitution string to the length specified in the format string, head[len] = '\0' could write outside the buffer if that length was too large.

---
udev_rules.c | 2 +-

1 files changed, 1 insertions(+), 1 deletions(-)
Quoted 15 lines

udev_rules.c found: · line 561

Quoted 10 lines
udev_rules.c +1 -1
index 44b41e9..90a83e5 100644
@@ -558,7 +558,7 @@ found:
                       break;
               }
               /* possibly truncate to format-char specified length */
-               if (len != -1) {
+               if (len >= 0 && (size_t)len < maxsize - (head-string)) {

Yeah, that should be fixed. Wouldn't:
if (len >= 0 && len <= strlen(head))
also catch strings which fit in the buffer, but are shorter than the specified limit?

Quoted 3 lines

Yes, this should work properly too (or even "&& len < strlen(head)" to skip writing '\0' over itself). Working in constant time is probably not an issue here, given the number of strlcat() calls around.

I've applied this together with the other patch, which logs an error for unknown substitutions.

Thanks a lot,
Kay

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier. Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid0709&bid&3057&dat1642
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel