Skip to contentSkip to conversation

sfdisk: Avoid out of boundary read with readline

PATCH Applied Tobias Stoeckmann 2 participants 2 messages Feb 17, 2019 – Feb 18, 2019 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sfdisk: Avoid out of boundary read with readline

It is not guaranteed that the returned string of readline() actually contains as many bytes as buf can contain.

If bufsz is larger than the allocated memory by readline, an out of boundary read occurs and leads to undefined behaviour. Most likely that will be a crash.

This can be reproduced when readline-support is compiled in and when you directly enter "quit" and "n" (to not write changes back to disk) when sfdisk was called with any given device.

---
 disk-utils/sfdisk.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
disk-utils/sfdisk.c +3 -1
index 3911dda85..52ccc5251 100644
@@ -133,7 +133,9 @@ static int get_user_reply(const char *prompt, char *buf, size_t bufsz)
 		p = readline(prompt);
 		if (!p)
 			return 1;
-		memcpy(buf, p, bufsz);
+		strncpy(buf, p, bufsz);
+		if (bufsz != 0)
+			buf[bufsz - 1] = '\0';
 		free(p);
 	} else
 #endif
Signature · 1 line
2.20.1
maintainer Applied
#1 lore raw
Quoted 2 lines · Tobias Stoeckmann
 disk-utils/sfdisk.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Applied, thanks.

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com