cmd: pstore: fix out-of-bounds accesses in display and save
cover cmd: pstore: fix out-of-bounds accesses in display and save
pstore display and pstore save read the records that the Linux ramoops driver left in RAM. This series fixes three ways in which they access memory past a record or a buffer. Each fix is followed by a test in test_pstore.py that fails without it.
Patch 1 checks the record header. pstore_get_buffer() copies the bytes from offset start to the end of the record, but only rejected a record whose size is 0 or larger than its zone, so a start beyond the size made the length wrap around and crashed U-Boot. The ftrace records are read without checking their signature, so leftover data in RAM is enough. The record is now rejected, as Linux does in persistent_ram_post_init(), when its size does not fit in the zone after the 12-byte header or its start is beyond its size. Patch 2 writes such headers and checks that the records are skipped.
Patch 3 fixes a heap overflow. Each record is copied into a buffer sized for the largest record on the first pstore display or save, and pstore set changed the record sizes without sizing it again. Patch 4 makes the console record larger after a first display and saves a full one.
Patch 5 looks for the newline that ends the header line of a dump record within the record. strchr() went past the end of a record without one, the header length got larger than the record, and pstore save tried to write almost 4 GiB. Patch 6 cuts the header line of a dump record.
Without patch 3, the test in patch 4 trips an assertion in the sandbox malloc checks, which stops U-Boot with "### ERROR ### Please RESET the board ###". test.py treats that as fatal and stops the whole run instead of failing one test.
Each commit builds for sandbox, and cmd/pstore.c builds without warnings with W=1 for pinephone (arm64) and r8a78000_ironhide_cm33 (32-bit). All pstore tests pass on sandbox.
Aristo Chen (6):
cmd: pstore: check the record header before using it
test: pstore: check that records with an invalid header are skipped
cmd: pstore: size the buffer again after pstore set
test: pstore: check pstore save after a record is made larger
cmd: pstore: look for the end of the dump header within the record
test: pstore: check a dump record whose header line is cut
cmd/pstore.c | 20 +++++--- test/py/tests/test_pstore.py | 88 ++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 7 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/6 cmd: pstore: check the record header before using it
pstore_get_buffer() only rejects a record whose size is 0 or larger than its zone, then copies size - start bytes from offset start. A start beyond the size makes that length wrap around, and pstore display or pstore save crash. The ftrace records are read without checking their signature, so leftover data in RAM is enough to get there. Also, as the zone holds the 12-byte header too, a record can be read up to 12 bytes past the end of its zone.
Reject the record, as Linux does in persistent_ram_post_init(), when its size does not fit in the zone after the header or its start is beyond its size. Skip a zone that is not larger than the header, so that this bound cannot wrap around either.
--- cmd/pstore.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
cmd/pstore.c +4 -1
@@ -92,13 +92,16 @@ static u32 pstore_get_buffer(u32 sig, phys_addr_t buffer, u32 size, char *dest) (struct persistent_ram_buffer *)map_sysmem (buffer, size);u32 dest_size; +if (size <=sizeof (*prb)) +return 0 ; +if (sig ==0 || prb->sig == sig) {if (prb->size ==0 ) {log_debug ("found existing empty buffer\n" );return 0 ; } -if (prb->size > size) { +if (prb->size > size -sizeof (*prb) || prb->start > prb->size) {log_debug ("found existing invalid buffer, size %u, start %u\n" , prb->size, prb->start);return 0 ;
Signature · 1 line
2.43.0
PATCH 2/6 test: pstore: check that records with an invalid header are skipped
In reply to Aristo ChenWrite a console record whose start is beyond its size, the same in the ftrace zone without a signature, and a user record larger than its zone minus the header. Check that pstore display shows none of them and that pstore save writes no file. Without the record header checks, the first two crash U-Boot and the last one is shown.
--- test/py/tests/test_pstore.py | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)
test/py/tests/test_pstore.py +37 -0
@@ -10,6 +10,7 @@ import shutil PSTORE_ADDR=0x3000000 PSTORE_LENGTH=0x100000 +PSTORE_SIG=0x43474244 PSTORE_PANIC1='test/py/tests/test_pstore_data_panic1.hex' PSTORE_PANIC2='test/py/tests/test_pstore_data_panic2.hex' PSTORE_CONSOLE='test/py/tests/test_pstore_data_console.hex'
@@ -40,6 +41,14 @@ def checkfile(ubman, path, filesize, checksum): 'setenv filesize']) assert(checksum in ''.join(output)) +def write_header(ubman, addr, sig, start, size): + """Write a record header at addr""" + + ubman.run_command_list([ + 'mw.l 0x%x 0x%x' % (addr, sig), + 'mw.l 0x%x 0x%x' % (addr + 4, start), + 'mw.l 0x%x 0x%x' % (addr + 8, size)]) + @pytest.mark.buildconfigspec('cmd_pstore') def test_pstore_display_all_records(ubman): """Test that pstore displays all records."""
@@ -75,3 +84,31 @@ def test_pstore_save_records(ubman): checkfile(ubman, '%s/console-ramoops-0' % (outdir), 4084, 'bb44de4a9b8ebd9b17ae98003287325b') shutil.rmtree(outdir) + [email protected]('cmd_pstore') +def test_pstore_invalid_records(ubman): + """Test that pstore skips records with an invalid header.""" + + console = PSTORE_ADDR + PSTORE_LENGTH - 3 * 4096 + ftrace = console + 4096 + pmsg = ftrace + 4096 + outdir = tempfile.mkdtemp() + + ubman.run_command('') + ubman.run_command_list([ + 'mw.b 0x%x 0 0x%x' % (PSTORE_ADDR, PSTORE_LENGTH), + 'pstore set 0x%x 0x%x' % (PSTORE_ADDR, PSTORE_LENGTH)]) + # Start beyond the size + write_header(ubman, console, PSTORE_SIG, 0x10, 0x8) + # The same in the ftrace zone, which is read without checking the signature + write_header(ubman, ftrace, 0, 0x10, 0x8) + # Size larger than the zone minus the header + write_header(ubman, pmsg, PSTORE_SIG, 0, 4096) + + response = ubman.run_command('pstore display') + assert('****' not in response) + + ubman.run_command('pstore save hostfs - %s' % (outdir)) + assert(not os.listdir(outdir)) + + shutil.rmtree(outdir)
Signature · 1 line
2.43.0
PATCH 3/6 cmd: pstore: size the buffer again after pstore set
In reply to Aristo Chenpstore display and pstore save copy each record into a buffer of buffer_size bytes, the largest record size, but compute buffer_size only while it is 0, so only on their first run. pstore set changes the record sizes without resetting it. After a first display or save, a pstore set with a larger record, console, ftrace or pmsg size makes the next display or save copy a record past the end of the buffer, which corrupts the heap.
Reset buffer_size in pstore set, so that it is computed again for the new sizes.
--- cmd/pstore.c | 3 +++ 1 file changed, 3 insertions(+)
cmd/pstore.c +3 -0
@@ -197,6 +197,9 @@ static int pstore_set(struct cmd_tbl *cmdtp, int flag, int argc,if (argc >7 ) pstore_ecc_size =hextoul (argv[7 ],NULL ); +/* Size the buffer again for the new record sizes on next use */ + buffer_size =0 ; +if (pstore_length < (pstore_record_size + pstore_console_size + pstore_ftrace_size + pstore_pmsg_size)) {printf ("pstore <len> should be larger than the sum of all records sizes\n" );
Signature · 1 line
2.43.0
PATCH 4/6 test: pstore: check pstore save after a record is made larger
In reply to Aristo ChenRun pstore display with the default record sizes, then make the console record twice as large with pstore set and save a full console record. Check that the whole record is saved. Without sizing the buffer again, the copy overflows it and the sandbox malloc checks stop U-Boot.
--- test/py/tests/test_pstore.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
test/py/tests/test_pstore.py +31 -0
@@ -112,3 +112,34 @@ def test_pstore_invalid_records(ubman): assert(not os.listdir(outdir)) shutil.rmtree(outdir) + [email protected]('cmd_pstore') +def test_pstore_set_larger_record(ubman): + """Test that pstore save works after pstore set makes a record larger.""" + + size = 0x2000 + console = PSTORE_ADDR + PSTORE_LENGTH - 2 * 4096 - size + outdir = tempfile.mkdtemp() + + ubman.run_command('') + try: + # Size the buffer for the default record sizes + ubman.run_command_list([ + 'mw.b 0x%x 0 0x%x' % (PSTORE_ADDR, PSTORE_LENGTH), + 'pstore set 0x%x 0x%x' % (PSTORE_ADDR, PSTORE_LENGTH), + 'pstore display']) + # Make the console record larger and save a full one. With 0xaa + # data, an overflow of the buffer fails the sandbox malloc checks. + ubman.run_command_list([ + 'pstore set 0x%x 0x%x 0x1000 0x%x' % (PSTORE_ADDR, PSTORE_LENGTH, + size), + 'mw.b 0x%x 0xaa 0x%x' % (console + 12, size - 12)]) + write_header(ubman, console, PSTORE_SIG, size - 12, size - 12) + ubman.run_command('pstore save hostfs - %s' % (outdir)) + with open('%s/console-ramoops-0' % (outdir), 'rb') as f: + assert(f.read() == b'\xaa' * (size - 12)) + finally: + # Restore the default record sizes + ubman.restart_uboot() + + shutil.rmtree(outdir)
Signature · 1 line
2.43.0
PATCH 5/6 cmd: pstore: look for the end of the dump header within the record
In reply to Aristo Chenpstore_read_kmsg_hdr() finds the newline that ends the "====<secs>.<nsecs>-<C|D>" line of a dump record with strchr(), which does not stop at the end of the record: the buffer is not NUL-terminated after it and still holds earlier records. When the record itself has no newline, strchr() finds one further on and the header length becomes larger than the record, so size - header_len wraps around. pstore display then prints stale data, and pstore save asks to write almost 4 GiB, which on sandbox writes the rest of the RAM to the file.
Pass the record size to pstore_read_kmsg_hdr() and look for the newline with memchr() within the record.
--- cmd/pstore.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
cmd/pstore.c +6 -6
@@ -42,6 +42,7 @@ static unsigned int buffer_size;* pstore_read_kmsg_hdr() - Check kernel header and get compression flag if * available. * @buffer: Kernel messages buffer. +* @size: Length of the kernel messages in @buffer. * @compressed: Returns TRUE if kernel buffer is compressed, else FALSE. * * Check if buffer starts with a kernel header of the form:
@@ -51,7 +52,7 @@ static unsigned int buffer_size;* * Return: Length of kernel header. */ -static int pstore_read_kmsg_hdr (char *buffer,bool *compressed) +static int pstore_read_kmsg_hdr (char *buffer,u32 size,bool *compressed) {char *ptr = buffer; *compressed = false;
@@ -59,9 +60,7 @@ static int pstore_read_kmsg_hdr(char *buffer, bool *compressed)if (strncmp (RAMOOPS_KERNMSG_HDR , ptr,strlen (RAMOOPS_KERNMSG_HDR )) !=0 )return 0 ; - ptr +=strlen (RAMOOPS_KERNMSG_HDR ); - - ptr =strchr (ptr,'\n' ); + ptr =memchr (buffer,'\n' , size);if (!ptr)return 0 ;
@@ -299,7 +298,8 @@ static int pstore_display(struct cmd_tbl *cmdtp, int flag, int argc,if (size ==0 )continue ; - header_len =pstore_read_kmsg_hdr (buffer, &compressed); + header_len =pstore_read_kmsg_hdr (buffer, size, + &compressed);if (header_len ==0 ) {log_debug ("no valid kernel header\n" );continue ;
@@ -411,7 +411,7 @@ static int pstore_save(struct cmd_tbl *cmdtp, int flag, int argc,if (size ==0 )continue ; - header_len =pstore_read_kmsg_hdr (buffer, &compressed); + header_len =pstore_read_kmsg_hdr (buffer, size, &compressed);if (header_len ==0 ) {log_debug ("no valid kernel header\n" );continue ;
Signature · 1 line
2.43.0
PATCH 6/6 test: pstore: check a dump record whose header line is cut
In reply to Aristo ChenLoad the sample records and cut the second dump record inside its header line. Check that pstore display shows only the first dump record and that pstore save does not save the second one. Without looking for the end of the header within the record, pstore display shows the second record with stale data of the first one, and pstore save writes far more than the record to dmesg-ramoops-1.
--- test/py/tests/test_pstore.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
test/py/tests/test_pstore.py +20 -0
@@ -143,3 +143,23 @@ def test_pstore_set_larger_record(ubman): ubman.restart_uboot() shutil.rmtree(outdir) + [email protected]('cmd_pstore') +def test_pstore_cut_dump_header(ubman): + """Test that pstore skips a dump record whose header line is cut.""" + + outdir = tempfile.mkdtemp() + + ubman.run_command('') + ubman.run_command('mw.b 0x%x 0 0x%x' % (PSTORE_ADDR, PSTORE_LENGTH)) + load_pstore(ubman) + # Cut the second dump record inside its header line + write_header(ubman, PSTORE_ADDR + 4096, PSTORE_SIG, 8, 8) + + response = ubman.run_command('pstore display dump') + assert(response.count('**** Dump') == 1) + + ubman.run_command('pstore save hostfs - %s' % (outdir)) + assert(not os.path.exists('%s/dmesg-ramoops-1' % (outdir))) + + shutil.rmtree(outdir)
Signature · 1 line
2.43.0
Quoted 15 lines · Aristo Chen
pstore display and pstore save read the records that the Linux ramoops driver left in RAM. This series fixes three ways in which they access memory past a record or a buffer. Each fix is followed by a test in
test_pstore.pythat fails without it.Patch 1 checks the record header.
pstore_get_buffer()copies the bytes from offset start to the end of the record, but only rejected a record whose size is 0 or larger than its zone, so a start beyond the size made the length wrap around and crashed U-Boot. The ftrace records are read without checking their signature, so leftover data in RAM is enough. The record is now rejected, as Linux does inpersistent_ram_post_init(), when its size does not fit in the zone after the 12-byte header or its start is beyond its size. Patch 2 writes such headers and checks that the records are skipped.
Quoted 1 line
[...]
Applied to u-boot/main, thanks!
[1/6] cmd: pstore: check the record header before using it
commit: ca0b83af174cb1d9fa9d227aeb535acbc5c4a493
[2/6] test: pstore: check that records with an invalid header are skipped
commit: 748d8fba4dd3e3b0adabc8228dd6d7bf14e2495d
[3/6] cmd: pstore: size the buffer again after pstore set
commit: 2b03bc4d7dfa2ecd0ed8d623ee0bdd94b0fca605
[4/6] test: pstore: check pstore save after a record is made larger
commit: c24eb8c92cf88f8a1975430a64fac6641db8d724
[5/6] cmd: pstore: look for the end of the dump header within the record
commit: f5794cf41b8bbfd6af847b11a2de86ba22216867
[6/6] test: pstore: check a dump record whose header line is cut
commit: 885d0fa66485d8a046ac56473bb3b3ee9985fe17Signature · 1 line
Tom