Skip to contentSkip to conversation

makedumpfile: Bunch of fixes for --dump-dmesg

PATCH In review Petr Tesarik 2 participants 5 messages Mar 14, 2014 – Mar 17, 2014

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

cover makedumpfile: Bunch of fixes for --dump-dmesg

This patch series addresses several issues with the dump_log_entry() function.

Petr Tesarik (3):
Fix string append in dump_log_entry()
Prevent buffer overflow in dump_log_entry
Optimize one-character output in dump_log_entry

 makedumpfile.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
Signature · 7 lines
1.8.4.5


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

PATCH 1/3 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.

---
 makedumpfile.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
makedumpfile.c +7 -6
index 579be61..fc4ce19 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 =+ sprintf(bufp, "%c", *p);
 		else
-			sprintf(buf, "%s\\x%02x", buf, *p);
+			bufp += sprintf(bufp, "\\x%02x", *p);
 	}
 
-	sprintf(buf, "%s\n", buf);
+	bufp += sprintf(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 · 7 lines
1.8.4.5


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

PATCH 2/3 Prevent buffer overflow in dump_log_entry

In reply to Petr Tesarik

One message may be longer than BUFSIZE. In that case, the output buffer overflows, causing stack corruption. Solve it by flushing the buffer when it gets full.

---
 makedumpfile.c | 6 ++++++
 1 file changed, 6 insertions(+)
makedumpfile.c +6 -0
index fc4ce19..261e1ad 100644
@@ -3885,6 +3885,12 @@ dump_log_entry(char *logptr, int fp)
 	bufp += sprintf(buf, "[%5lld.%06ld] ", nanos, rem/1000);
 
 	for (i = 0, p = msg; i < text_len; i++, p++) {
+		if (bufp - buf >= sizeof(buf) - 6) {
+			if (write(info->fd_dumpfile, buf, bufp - buf) < 0)
+				return FALSE;
+			bufp = buf;
+		}
+
 		if (isprint(*p) || isspace(*p))
 			bufp =+ sprintf(bufp, "%c", *p);
 		else
Signature · 7 lines
1.8.4.5


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

PATCH 3/3 Optimize one-character output in dump_log_entry

In reply to Petr Tesarik

Using sprintf to copy one character to the output buffer is overkill.

---
 makedumpfile.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
makedumpfile.c +2 -2
index 261e1ad..a98258c 100644
@@ -3892,12 +3892,12 @@ dump_log_entry(char *logptr, int fp)
 		}
 
 		if (isprint(*p) || isspace(*p))
-			bufp =+ sprintf(bufp, "%c", *p);
+			*bufp++ = *p;
 		else
 			bufp += sprintf(bufp, "\\x%02x", *p);
 	}
 
-	bufp += sprintf(bufp, "\n");
+	*bufp++ = '\n';
 
 	if (write(info->fd_dumpfile, buf, bufp - buf) < 0)
 		return FALSE;
Signature · 7 lines
1.8.4.5


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Acked-by on the v1 series
#4 lore raw
In reply to Petr Tesarik

Hello Petr,

Quoted 10 lines

This patch series addresses several issues with the dump_log_entry() function.

Petr Tesarik (3):
Fix string append in dump_log_entry()
Prevent buffer overflow in dump_log_entry
Optimize one-character output in dump_log_entry

makedumpfile.c | 19 +++++++++++++------

1 file changed, 13 insertions(+), 6 deletions(-)

Looks good to me, I really appreciate your help !

Thanks
Atsushi Kumagai

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