From d680b7ffa3bca46c5a5fb4638365180c40f36be8 Mon Sep 17 00:00:00 2001 From: Julien Masson Date: Thu, 5 Oct 2023 17:32:16 +0200 Subject: [PATCH 1/2] kbootd: fix overflow when reading gpt header When we read GPT header on LBA 1 we must use data allocated with a size of LBA_SIZE. Otherwise we may have an overflow. Signed-off-by: Julien Masson --- kbootd/src/part.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/kbootd/src/part.c b/kbootd/src/part.c index 66da6bf..b4ae8da 100644 --- a/kbootd/src/part.c +++ b/kbootd/src/part.c @@ -327,7 +327,7 @@ static void gpt_convert_efi_name_to_char(char *s, void *es, int n) static int find_gpt_entry(int fd, const char *name, struct gpt_entry *gpt_e, off_t *offset) { - struct gpt_header gpt_hdr; + struct gpt_header *gpt_hdr; char part[PARTNAME_SZ]; char data[LBA_SIZE]; int ret; @@ -339,13 +339,16 @@ static int find_gpt_entry(int fd, const char *name, struct gpt_entry *gpt_e, return ret; } - ret = kread(fd, (char *)&gpt_hdr, LBA_SIZE); + memset(data, '\0', LBA_SIZE); + ret = kread(fd, data, LBA_SIZE); if (ret == -1) { log("read GPT header failed\n"); return -1; } + gpt_hdr = (struct gpt_header *)data; - for (int i = 0; i < gpt_hdr.n_parts; i++) { + for (int i = 0; i < gpt_hdr->n_parts; i++) { + memset(data, '\0', LBA_SIZE); ret = kread(fd, data, LBA_SIZE); if (ret == -1) { log("read GPT entry failed\n"); From 7d184f8943edfa77a6d2f5e70a213a37d70dfe66 Mon Sep 17 00:00:00 2001 From: Julien Masson Date: Thu, 5 Oct 2023 17:54:23 +0200 Subject: [PATCH 2/2] kbootd: run clang-format on main.c Signed-off-by: Julien Masson --- kbootd/src/main.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/kbootd/src/main.c b/kbootd/src/main.c index 66ab11a..796f271 100644 --- a/kbootd/src/main.c +++ b/kbootd/src/main.c @@ -68,17 +68,17 @@ static bool prompt_stop_boot(void) static void set_terminal_single_char_read(void) { - struct termios local_term_attributes; + struct termios local_term_attributes; - if (!isatty(STDIN_FILENO)) - return; - if (tcgetattr(STDIN_FILENO, &local_term_attributes)) - return; + if (!isatty(STDIN_FILENO)) + return; + if (tcgetattr(STDIN_FILENO, &local_term_attributes)) + return; - local_term_attributes.c_lflag &= ~(ICANON | ECHO); - local_term_attributes.c_cc[VMIN] = 1; - local_term_attributes.c_cc[VTIME] = 0; - tcsetattr(STDIN_FILENO, TCSANOW, &local_term_attributes); + local_term_attributes.c_lflag &= ~(ICANON | ECHO); + local_term_attributes.c_cc[VMIN] = 1; + local_term_attributes.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSANOW, &local_term_attributes); } static bool stop_boot(void) @@ -91,7 +91,7 @@ static bool stop_boot(void) return stop; } - set_terminal_single_char_read(); + set_terminal_single_char_read(); stop = prompt_stop_boot(); if (stop)