Skip to contentSkip to conversation

alsactl: sprintf to snprintf prevent buffer overflow

PATCH Applied Doug Goldstein 2 participants 2 messages May 1, 2013 – May 6, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 alsactl: sprintf to snprintf prevent buffer overflow

sprintf() is a bit dangerous unless you explicitly know your type size and want to keep it in sync always. Its safer to just use snprintf() and ensure your string doesn't overflow and is NULL terminated.

---
This was written to fix the buffer overflow crash in alsactl with the 1.0.27 release, which is documented in Gentoo bug #468160 [1]. The issue is actually already fixed upstream by 95788fea25c1a59985828d4b91af0772d077600b however the fix just changes the size and continues to use sprintf() which IMHO is still dangerous. The attached patch will fix the issue in a safer manner for good.

[1] https://bugs.gentoo.org/show_bug.cgi?id=468160

---
 alsactl/lock.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
alsactl/lock.c +2 -2
index fce208b..587a109 100644
@@ -53,9 +53,9 @@ static int state_lock_(const char *file, int lock, int timeout)
 	lck.l_len = 11;
 	lck.l_pid = 0;
 	if (lock) {
-		sprintf(lcktxt, "%10li\n", (long)getpid());
+		snprintf(lcktxt, sizeof(lcktxt), "%10li\n", (long)getpid());
 	} else {
-		sprintf(lcktxt, "%10s\n", "");
+		snprintf(lcktxt, sizeof(lcktxt), "%10s\n", "");
 	}
 	while (fd < 0 && timeout-- > 0) {
 		fd = open(nfile, O_RDWR);
Signature · 1 line
1.8.2.1
Applied
#1 lore raw

At Wed, 1 May 2013 11:30:26 -0500,

Quoted 14 lines · Doug Goldstein

sprintf() is a bit dangerous unless you explicitly know your type size and want to keep it in sync always. Its safer to just use snprintf() and ensure your string doesn't overflow and is NULL terminated.

---
This was written to fix the buffer overflow crash in alsactl with the 1.0.27 release, which is documented in Gentoo bug #468160 [1]. The issue is actually already fixed upstream by 95788fea25c1a59985828d4b91af0772d077600b however the fix just changes the size and continues to use sprintf() which IMHO is still dangerous. The attached patch will fix the issue in a safer manner for good.

Quoted 1 line

[1] https://bugs.gentoo.org/show_bug.cgi?id=468160

Thanks, applied it now.

Though, the change in the second part is obviously superfluous (i.e. not "optimal"). But stupidity isn't bad so that one can be even more stupid sometimes :)

Takashi

Quoted 28 lines
---
 alsactl/lock.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
alsactl/lock.c +2 -2
index fce208b..587a109 100644
@@ -53,9 +53,9 @@ static int state_lock_(const char *file, int lock, int timeout)
 	lck.l_len = 11;
 	lck.l_pid = 0;
 	if (lock) {
-		sprintf(lcktxt, "%10li\n", (long)getpid());
+		snprintf(lcktxt, sizeof(lcktxt), "%10li\n", (long)getpid());
 	} else {
-		sprintf(lcktxt, "%10s\n", "");
+		snprintf(lcktxt, sizeof(lcktxt), "%10s\n", "");
 	}
 	while (fd < 0 && timeout-- > 0) {
 		fd = open(nfile, O_RDWR);
Signature · 6 lines
1.8.2.1

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel