Skip to contentSkip to conversation

multipathd: check for command overflow

PATCH In review Benjamin Marzinski 2 participants 2 messages Aug 29, 2022 – Aug 30, 2022 Device mapper

lore
Share & tools
lore

Benjamin Marzinski

author maintainer
#0 lore raw

PATCH 1/1 multipathd: check for command overflow

The code to build the command string calls snprintf() in a loop, adding the return value to the start pointer, c. Since snprintf() can return more characters than it actually printed if it runs out of space, c can end up pointing past the end of the command string buffer on sebsequent loops. Since the size argument to snprintf() is unsigned, after an overflow it will be a huge number, instead of a negative one, meaning that it will continue printing past the end of the buffer. Check for overflow after each snprintf() to avoid this.

---
 multipathd/main.c | 4 ++++
 1 file changed, 4 insertions(+)
multipathd/main.c +4 -0
index 5a408945..1032fb2a 100644
@@ -3707,6 +3707,10 @@ main (int argc, char *argv[])
 					c += snprintf(c, s + CMDSIZE - c,
 						      "%s ", argv[optind]);
 				optind++;
+				if (c >= s + CMDSIZE) {
+					fprintf(stderr, "multipathd command too large\n");
+					exit(1);
+				}
 			}
 			c += snprintf(c, s + CMDSIZE - c, "\n");
 		}
Signature · 6 lines
2.17.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Benjamin Marzinski

The code to build the command string calls snprintf() in a loop, adding
the return value to the start pointer, c. Since snprintf() can return more characters than it actually printed if it runs out of space, c can
end up pointing past the end of the command string buffer on sebsequent
loops.  Since the size argument to snprintf() is unsigned, after an overflow it will be a huge number, instead of a negative one, meaning that it will continue printing past the end of the buffer. Check for overflow after each snprintf() to avoid this.

Quoted 1 line

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel