Skip to content

Commit 7565293

Browse files
committed
multiboot.c: include bound checking in mb2_find_tag_by_type
note that multiboot header is already authenticated so it's trusted.
1 parent 731dd3a commit 7565293

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

src/multiboot.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,15 @@ static uint8_t* mb2_align_address_up(uint8_t *addr, int align)
102102
return (uint8_t*)((v + mask) & ~(mask));
103103
}
104104

105-
static uint8_t *mb2_find_tag_by_type(uint8_t *tags, uint32_t type)
105+
static uint8_t *mb2_find_tag_by_type(uint8_t *tags, uint32_t tags_len,
106+
uint32_t type)
106107
{
108+
uint8_t *end = tags + tags_len;
107109
struct mb2_tag* tag = (struct mb2_tag*)tags;
108110

109-
while (tag->type != 0) {
111+
while ((uint8_t*)tag + sizeof(*tag) <= end && tag->type != 0) {
112+
if (tag->size < sizeof(*tag))
113+
return NULL;
110114
if (tag->type == type)
111115
return (uint8_t*)tag;
112116
tag = (struct mb2_tag*)mb2_align_address_up((uint8_t*)tag + tag->size,
@@ -247,6 +251,7 @@ int mb2_build_boot_info_header(uint8_t *mb2_boot_info,
247251
(struct mb2_boot_info_header *)mb2_boot_info;
248252
struct mb2_tag_info_req *info_req_tag;
249253
int requested_tags, i, r;
254+
uint32_t header_length;
250255
uint8_t *idx;
251256

252257
if (max_size < sizeof(*hdr)) {
@@ -256,8 +261,14 @@ int mb2_build_boot_info_header(uint8_t *mb2_boot_info,
256261
max_size -= sizeof(*hdr);
257262
idx = (uint8_t*)hdr + sizeof(*hdr);
258263
hdr->reserved = 0;
264+
header_length = ((struct mb2_header *)mb2_header)->header_length;
265+
if (header_length < sizeof(struct mb2_header))
266+
return -1;
259267
info_req_tag =
260-
(struct mb2_tag_info_req *)mb2_find_tag_by_type(mb2_header + sizeof(struct mb2_header), MB2_TAG_TYPE_INFO_REQ);
268+
(struct mb2_tag_info_req *)mb2_find_tag_by_type(
269+
mb2_header + sizeof(struct mb2_header),
270+
header_length - sizeof(struct mb2_header),
271+
MB2_TAG_TYPE_INFO_REQ);
261272
if (info_req_tag == NULL)
262273
return -1;
263274
requested_tags = (info_req_tag->size - sizeof(struct mb2_tag_info_req)) / sizeof(uint32_t);

0 commit comments

Comments
 (0)