Skip to contentSkip to conversation

makedumpfile: Fix string append in dump_log_entry()

PATCH In review Petr Tesarik 2 participants 4 messages Feb 26, 2014 – Mar 7, 2014

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

PATCH 1/1 makedumpfile: Fix string append in dump_log_entry()

To quote the sprintf(3) man page:

Some programs imprudently rely on code such as the following

sprintf(buf, "%s some further text", buf);

to append text to buf. However, the standards explicitly note that the results are undefined if source and destination buffers overlap when calling sprintf(), snprintf(), vsprintf(), and vsnprintf(). Depending on the version of gcc(1) used, and the compiler options employed, calls such as the above will not produce the expected results.

The original code is actually miscompiled on openSUSE 13.1.

It's also overkill to call sprintf() for something that can be done with a simple assignment.

---
 makedumpfile.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
makedumpfile.c +7 -6
index 579be61..013fce7 100644
@@ -3866,7 +3866,7 @@ reset_bitmap_of_free_pages(unsigned long node_zones, struct cycle *cycle)
 static int
 dump_log_entry(char *logptr, int fp)
 {
-	char *msg, *p;
+	char *msg, *p, *bufp;
 	unsigned int i, text_len;
 	unsigned long long ts_nsec;
 	char buf[BUFSIZE];
@@ -3881,18 +3881,19 @@ dump_log_entry(char *logptr, int fp)
 
 	msg = logptr + SIZE(printk_log);
 
-	sprintf(buf, "[%5lld.%06ld] ", nanos, rem/1000);
+	bufp = buf;
+	bufp += sprintf(buf, "[%5lld.%06ld] ", nanos, rem/1000);
 
 	for (i = 0, p = msg; i < text_len; i++, p++) {
 		if (isprint(*p) || isspace(*p))
-			sprintf(buf, "%s%c", buf, *p);
+			*bufp++ = *p;
 		else
-			sprintf(buf, "%s\\x%02x", buf, *p);
+			bufp += sprintf(bufp, "\\x%02x", *p);
 	}
 
-	sprintf(buf, "%s\n", buf);
+	*bufp++ = '\n';
 
-	if (write(info->fd_dumpfile, buf, strlen(buf)) < 0)
+	if (write(info->fd_dumpfile, buf, bufp - buf) < 0)
 		return FALSE;
 	else
 		return TRUE;
Signature · 6 lines
1.8.4.5

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#1 lore raw

Hello Petr,

Quoted 12 lines

To quote the sprintf(3) man page:

Some programs imprudently rely on code such as the following

       sprintf(buf, "%s some further text", buf);

to append text to buf. However, the standards explicitly note that the results are undefined if source and destination buffers overlap when calling sprintf(), snprintf(), vsprintf(), and vsnprintf(). Depending on the version of gcc(1) used, and the compiler options employed, calls such as the above will not produce the expected results.

Quoted 6 lines

The original code is actually miscompiled on openSUSE 13.1.

It's also overkill to call sprintf() for something that can be done with a simple assignment.

Thanks, it seems good to me.

Actually, Nick sent the same patch in last July and we tried to take care of buffer overflow at the same time as below:

http://lists.infradead.org/pipermail/kexec/2013-August/009430.html

However, this thread has been left open, so I was wondering if you could take over this work. Of course you can decline this, then I'll do it later as another patch.

Thanks
Atsushi Kumagai

Quoted 43 lines

---
makedumpfile.c | 13 +++++++------

1 file changed, 7 insertions(+), 6 deletions(-)
makedumpfile.c +0 -0
index 579be61..013fce7 100644
@@ -3866,7 +3866,7 @@ reset_bitmap_of_free_pages(unsigned long node_zones, struct cycle *cycle)

static int
dump_log_entry(char *logptr, int fp)
{

makedumpfile.c +1 -1
-	char *msg, *p;
+	char *msg, *p, *bufp;
	unsigned int i, text_len;
	unsigned long long ts_nsec;
	char buf[BUFSIZE];
makedumpfile.c +6 -6
@@ -3881,18 +3881,19 @@ dump_log_entry(char *logptr, int fp)
 
 	msg = logptr + SIZE(printk_log);
 
-	sprintf(buf, "[%5lld.%06ld] ", nanos, rem/1000);
+	bufp = buf;
+	bufp += sprintf(buf, "[%5lld.%06ld] ", nanos, rem/1000);
 
 	for (i = 0, p = msg; i < text_len; i++, p++) {
 		if (isprint(*p) || isspace(*p))
-			sprintf(buf, "%s%c", buf, *p);
+			*bufp++ = *p;
 		else
-			sprintf(buf, "%s\\x%02x", buf, *p);
+			bufp += sprintf(bufp, "\\x%02x", *p);
 	}
 
-	sprintf(buf, "%s\n", buf);
+	*bufp++ = '\n';
 
-	if (write(info->fd_dumpfile, buf, strlen(buf)) < 0)
+	if (write(info->fd_dumpfile, buf, bufp - buf) < 0)
 		return FALSE;
 	else
 		return TRUE;
--
Quoted 1 line

1.8.4.5

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#2 lore raw
Quoted 1 line · On Fri, 28 Feb 2014 01:01:33 +0000 Atsushi Kumagai…

Hello Petr,

Hello Kumagai-san,

Quoted 21 lines
Quoted 12 lines

To quote the sprintf(3) man page:

Some programs imprudently rely on code such as the following

       sprintf(buf, "%s some further text", buf);

to append text to buf. However, the standards explicitly note that the results are undefined if source and destination buffers overlap when calling sprintf(), snprintf(), vsprintf(), and vsnprintf(). Depending on the version of gcc(1) used, and the compiler options employed, calls such as the above will not produce the expected results.

Quoted 6 lines

The original code is actually miscompiled on openSUSE 13.1.

It's also overkill to call sprintf() for something that can be done with a simple assignment.

Thanks, it seems good to me.

Quoted 8 lines

Actually, Nick sent the same patch in last July and we tried to take care of buffer overflow at the same time as below:

http://lists.infradead.org/pipermail/kexec/2013-August/009430.html

However, this thread has been left open, so I was wondering if you could take over this work. Of course you can decline this, then I'll do it later as another patch.

I don't mind taking over this work, but I don't think it's a good thing to combine the buffer overflow fix with the sprintf buffer overlap fix.

What is the expected plan? Are you waiting for me to send a two-patch series now?

Petr Tesarik

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#3 lore raw
Quoted 34 lines
Quoted 1 line

Hello Petr,

Hello Kumagai-san,

Quoted 21 lines
Quoted 12 lines

To quote the sprintf(3) man page:

Some programs imprudently rely on code such as the following

       sprintf(buf, "%s some further text", buf);

to append text to buf. However, the standards explicitly note that the results are undefined if source and destination buffers overlap when calling sprintf(), snprintf(), vsprintf(), and vsnprintf(). Depending on the version of gcc(1) used, and the compiler options employed, calls such as the above will not produce the expected results.

Quoted 6 lines

The original code is actually miscompiled on openSUSE 13.1.

It's also overkill to call sprintf() for something that can be done with a simple assignment.

Thanks, it seems good to me.

Quoted 8 lines

Actually, Nick sent the same patch in last July and we tried to take care of buffer overflow at the same time as below:

http://lists.infradead.org/pipermail/kexec/2013-August/009430.html

However, this thread has been left open, so I was wondering if you could take over this work. Of course you can decline this, then I'll do it later as another patch.

Quoted 6 lines

I don't mind taking over this work, but I don't think it's a good thing to combine the buffer overflow fix with the sprintf buffer overlap fix.

What is the expected plan? Are you waiting for me to send a two-patch series now?

Yes, I agree with separating the two fixes.
Thanks for your help !

Atsushi Kumagai

Quoted 6 lines

Petr Tesarik

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec