Blame kexec-tools-2.0.23-05-util_lib_elf_info_harden_parsing_of_printk_buffer.patch

Philipp Rudo 574a20
 commit f4c59879b830c7d574a953e6ce970ddaf20910d7
Philipp Rudo 574a20
 Author: Philipp Rudo <prudo@redhat.com>
Philipp Rudo 574a20
 Date:   Wed Mar 23 16:35:36 2022 +0100
Philipp Rudo 574a20
 
Philipp Rudo 574a20
     util_lib/elf_info: harden parsing of printk buffer
Philipp Rudo 574a20
     
Philipp Rudo 574a20
     The old printk mechanism (> v3.5.0 and < v5.10.0) had a fixed size
Philipp Rudo 574a20
     buffer (log_buf) that contains all messages. The location for the next
Philipp Rudo 574a20
     message is stored in log_next_idx. In case the log_buf runs full
Philipp Rudo 574a20
     log_next_idx wraps around and starts overwriting old messages at the
Philipp Rudo 574a20
     beginning of the buffer. The wraparound is denoted by a message with
Philipp Rudo 574a20
     msg->len == 0.
Philipp Rudo 574a20
     
Philipp Rudo 574a20
     Following the behavior described above blindly is dangerous as e.g. a
Philipp Rudo 574a20
     memory corruption could overwrite (parts of) the log_buf. If the
Philipp Rudo 574a20
     corruption adds a message with msg->len == 0 this leads to an endless
Philipp Rudo 574a20
     loop when dumping the dmesg. Fix this by verifying that not wrapped
Philipp Rudo 574a20
     around before when it encounters a message with msg->len == 0.
Philipp Rudo 574a20
     
Philipp Rudo 574a20
     While at it also verify that the index is within the log_buf and thus
Philipp Rudo 574a20
     guard against corruptions with msg->len != 0.
Philipp Rudo 574a20
     
Philipp Rudo 574a20
     The same bug has been reported and fixed in makedumpfile [1].
Philipp Rudo 574a20
     
Philipp Rudo 574a20
     [1] http://lists.infradead.org/pipermail/kexec/2022-March/024272.html
Philipp Rudo 574a20
     
Philipp Rudo 574a20
     Signed-off-by: Philipp Rudo <prudo@redhat.com>
Philipp Rudo 574a20
     Signed-off-by: Simon Horman <horms@verge.net.au>
Philipp Rudo 574a20
 
Philipp Rudo 574a20
 diff --git a/util_lib/elf_info.c b/util_lib/elf_info.c
Philipp Rudo 574a20
 index d252eff5bd582837595a22aa387f53675c402121..ce71c6055c3a6ce8698d35960a8448be1dc8adc1 100644
Philipp Rudo 574a20
 --- a/util_lib/elf_info.c
Philipp Rudo 574a20
 +++ b/util_lib/elf_info.c
Philipp Rudo 574a20
 @@ -763,8 +763,9 @@ static void dump_dmesg_structured(int fd, void (*handler)(char*, unsigned int))
Philipp Rudo 574a20
  {
Philipp Rudo 574a20
  #define OUT_BUF_SIZE	4096
Philipp Rudo 574a20
  	uint64_t log_buf, log_buf_offset, ts_nsec;
Philipp Rudo 574a20
 -	uint32_t log_first_idx, log_next_idx, current_idx, len = 0, i;
Philipp Rudo 574a20
 +	uint32_t log_buf_len, log_first_idx, log_next_idx, current_idx, len = 0, i;
Philipp Rudo 574a20
  	char *buf, out_buf[OUT_BUF_SIZE];
Philipp Rudo 574a20
 +	bool has_wrapped_around = false;
Philipp Rudo 574a20
  	ssize_t ret;
Philipp Rudo 574a20
  	char *msg;
Philipp Rudo 574a20
  	uint16_t text_len;
Philipp Rudo 574a20
 @@ -811,6 +812,7 @@ static void dump_dmesg_structured(int fd, void (*handler)(char*, unsigned int))
Philipp Rudo 574a20
  	}
Philipp Rudo 574a20
  
Philipp Rudo 574a20
  	log_buf = read_file_pointer(fd, vaddr_to_offset(log_buf_vaddr));
Philipp Rudo 574a20
 +	log_buf_len = read_file_s32(fd, vaddr_to_offset(log_buf_len_vaddr));
Philipp Rudo 574a20
  
Philipp Rudo 574a20
  	log_first_idx = read_file_u32(fd, vaddr_to_offset(log_first_idx_vaddr));
Philipp Rudo 574a20
  	log_next_idx = read_file_u32(fd, vaddr_to_offset(log_next_idx_vaddr));
Philipp Rudo 574a20
 @@ -882,11 +884,31 @@ static void dump_dmesg_structured(int fd, void (*handler)(char*, unsigned int))
Philipp Rudo 574a20
  		 * and read the message at the start of the buffer.
Philipp Rudo 574a20
  		 */
Philipp Rudo 574a20
  		loglen = struct_val_u16(buf, log_offset_len);
Philipp Rudo 574a20
 -		if (!loglen)
Philipp Rudo 574a20
 +		if (!loglen) {
Philipp Rudo 574a20
 +			if (has_wrapped_around) {
Philipp Rudo 574a20
 +				if (len && handler)
Philipp Rudo 574a20
 +					handler(out_buf, len);
Philipp Rudo 574a20
 +				fprintf(stderr, "Cycle when parsing dmesg detected.\n");
Philipp Rudo 574a20
 +				fprintf(stderr, "The prink log_buf is most likely corrupted.\n");
Philipp Rudo 574a20
 +				fprintf(stderr, "log_buf = 0x%lx, idx = 0x%x\n",
Philipp Rudo 574a20
 +					log_buf, current_idx);
Philipp Rudo 574a20
 +				exit(68);
Philipp Rudo 574a20
 +			}
Philipp Rudo 574a20
  			current_idx = 0;
Philipp Rudo 574a20
 -		else
Philipp Rudo 574a20
 +			has_wrapped_around = true;
Philipp Rudo 574a20
 +		} else {
Philipp Rudo 574a20
  			/* Move to next record */
Philipp Rudo 574a20
  			current_idx += loglen;
Philipp Rudo 574a20
 +			if(current_idx > log_buf_len - log_sz) {
Philipp Rudo 574a20
 +				if (len && handler)
Philipp Rudo 574a20
 +					handler(out_buf, len);
Philipp Rudo 574a20
 +				fprintf(stderr, "Index outside log_buf detected.\n");
Philipp Rudo 574a20
 +				fprintf(stderr, "The prink log_buf is most likely corrupted.\n");
Philipp Rudo 574a20
 +				fprintf(stderr, "log_buf = 0x%lx, idx = 0x%x\n",
Philipp Rudo 574a20
 +					log_buf, current_idx);
Philipp Rudo 574a20
 +				exit(69);
Philipp Rudo 574a20
 +			}
Philipp Rudo 574a20
 +		}
Philipp Rudo 574a20
  	}
Philipp Rudo 574a20
  	free(buf);
Philipp Rudo 574a20
  	if (len && handler)