* util/grub-mkimage.c (compress_kernel_lzma): Respect format security.

(generate_image): Make prefix a const char *.
	Fix format specifications. Respect format security.
	Avoid void * arithmetics.
	Avoid shadowing.
	(argp_parser): Remove unused variable. Respect format security.
	* util/grub-mkimagexx.c (relocate_symbols): Avoid shadowing.
	(count_funcs) [!MKIMAGE_ELF64]: #if-out.
	(count_funcs): Remove unused variable.
	(relocate_addresses): Fix format specification.
	Disable x86-64 with elf32. Remove unused variables.
	(add_fixup_entry): Avoid shadowing.
	(make_reloc_section): Fix format specification.
	Use assert.
	(locate_sections): Fix format specifications.
	(load_image): Avoid shadowing.
This commit is contained in:
Vladimir 'phcoder' Serbinenko 2012-02-10 13:56:18 +01:00
parent 043c2ea33b
commit 495fc8c181
3 changed files with 106 additions and 75 deletions

View file

@ -83,28 +83,28 @@ SUFFIX (relocate_symbols) (Elf_Ehdr *e, Elf_Shdr *sections,
i < num_syms;
i++, sym = (Elf_Sym *) ((char *) sym + sym_size))
{
Elf_Section index;
Elf_Section cur_index;
const char *name;
name = strtab + grub_target_to_host32 (sym->st_name);
index = grub_target_to_host16 (sym->st_shndx);
if (index == STN_ABS)
cur_index = grub_target_to_host16 (sym->st_shndx);
if (cur_index == STN_ABS)
{
continue;
}
else if ((index == STN_UNDEF))
else if ((cur_index == STN_UNDEF))
{
if (sym->st_name)
grub_util_error ("undefined symbol %s", name);
else
continue;
}
else if (index >= num_sections)
grub_util_error ("section %d does not exist", index);
else if (cur_index >= num_sections)
grub_util_error ("section %d does not exist", cur_index);
sym->st_value = (grub_target_to_host (sym->st_value)
+ section_addresses[index]);
+ section_addresses[cur_index]);
if (image_target->elf_target == EM_IA_64 && ELF_ST_TYPE (sym->st_info)
== STT_FUNC)
@ -115,7 +115,9 @@ SUFFIX (relocate_symbols) (Elf_Ehdr *e, Elf_Shdr *sections,
*jptr = 0;
jptr++;
}
grub_util_info ("locating %s at 0x%x", name, sym->st_value, section_addresses[index]);
grub_util_info ("locating %s at 0x%llx (0x%llx)", name,
(unsigned long long) sym->st_value,
(unsigned long long) section_addresses[cur_index]);
if (! start_address)
if (strcmp (name, "_start") == 0 || strcmp (name, "start") == 0)
@ -146,13 +148,13 @@ SUFFIX (get_target_address) (Elf_Ehdr *e, Elf_Shdr *s, Elf_Addr offset,
return (Elf_Addr *) ((char *) e + grub_target_to_host32 (s->sh_offset) + offset);
}
#ifdef MKIMAGE_ELF64
static Elf_Addr
SUFFIX (count_funcs) (Elf_Ehdr *e, Elf_Shdr *symtab_section,
struct image_target_desc *image_target)
{
Elf_Word symtab_size, sym_size, num_syms;
Elf_Off symtab_offset;
Elf_Addr start_address = 0;
Elf_Sym *sym;
Elf_Word i;
int ret = 0;
@ -170,6 +172,7 @@ SUFFIX (count_funcs) (Elf_Ehdr *e, Elf_Shdr *symtab_section,
return ret;
}
#endif
#ifdef MKIMAGE_ELF64
struct unaligned_uint32
@ -307,8 +310,10 @@ SUFFIX (relocate_addresses) (Elf_Ehdr *e, Elf_Shdr *sections,
{
Elf_Half i;
Elf_Shdr *s;
#ifdef MKIMAGE_ELF64
struct ia64_kernel_trampoline *tr = (void *) (pe_target + tramp_off);
grub_uint64_t *gpptr = (void *) (pe_target + got_off);
#endif
for (i = 0, s = sections;
i < num_sections;
@ -375,8 +380,9 @@ SUFFIX (relocate_addresses) (Elf_Ehdr *e, Elf_Shdr *sections,
/* This is absolute. */
*target = grub_host_to_target32 (grub_target_to_host32 (*target)
+ addend + sym_addr);
grub_util_info ("relocating an R_386_32 entry to 0x%x at the offset 0x%x",
*target, offset);
grub_util_info ("relocating an R_386_32 entry to 0x%llx at the offset 0x%llx",
(unsigned long long) *target,
(unsigned long long) offset);
break;
case R_386_PC32:
@ -385,8 +391,9 @@ SUFFIX (relocate_addresses) (Elf_Ehdr *e, Elf_Shdr *sections,
+ addend + sym_addr
- target_section_addr - offset
- image_target->vaddr_offset);
grub_util_info ("relocating an R_386_PC32 entry to 0x%x at the offset 0x%x",
*target, offset);
grub_util_info ("relocating an R_386_PC32 entry to 0x%llx at the offset 0x%llx",
(unsigned long long) *target,
(unsigned long long) offset);
break;
default:
grub_util_error (_("relocation 0x%x is not implemented yet"),
@ -394,6 +401,7 @@ SUFFIX (relocate_addresses) (Elf_Ehdr *e, Elf_Shdr *sections,
break;
}
break;
#ifdef MKIMAGE_ELF64
case EM_X86_64:
switch (ELF_R_TYPE (info))
{
@ -405,7 +413,8 @@ SUFFIX (relocate_addresses) (Elf_Ehdr *e, Elf_Shdr *sections,
*target = grub_host_to_target64 (grub_target_to_host64 (*target)
+ addend + sym_addr);
grub_util_info ("relocating an R_X86_64_64 entry to 0x%llx at the offset 0x%llx",
*target, offset);
(unsigned long long) *target,
(unsigned long long) offset);
break;
case R_X86_64_PC32:
@ -416,7 +425,7 @@ SUFFIX (relocate_addresses) (Elf_Ehdr *e, Elf_Shdr *sections,
- target_section_addr - offset
- image_target->vaddr_offset);
grub_util_info ("relocating an R_X86_64_PC32 entry to 0x%x at the offset 0x%llx",
*t32, offset);
*t32, (unsigned long long) offset);
break;
}
@ -427,7 +436,7 @@ SUFFIX (relocate_addresses) (Elf_Ehdr *e, Elf_Shdr *sections,
*t32 = grub_host_to_target64 (grub_target_to_host32 (*t32)
+ addend + sym_addr);
grub_util_info ("relocating an R_X86_64_32(S) entry to 0x%x at the offset 0x%llx",
*t32, offset);
*t32, (unsigned long long) offset);
break;
}
@ -437,7 +446,6 @@ SUFFIX (relocate_addresses) (Elf_Ehdr *e, Elf_Shdr *sections,
break;
}
break;
#ifdef MKIMAGE_ELF64
case EM_IA_64:
switch (ELF_R_TYPE (info))
{
@ -496,8 +504,8 @@ SUFFIX (relocate_addresses) (Elf_Ehdr *e, Elf_Shdr *sections,
*target = grub_host_to_target64 (grub_target_to_host64 (*target)
+ addend + sym_addr);
grub_util_info ("relocating a direct entry to 0x%"
PRIxGRUB_UINT64_T " at the offset 0x%x",
*target, offset);
PRIxGRUB_UINT64_T " at the offset 0x%llx",
*target, (unsigned long long) offset);
break;
/* We treat LTOFF22X as LTOFF22, so we can ignore LDXMOV. */
@ -543,17 +551,17 @@ SUFFIX (add_fixup_entry) (struct fixup_block_list **cblock, grub_uint16_t type,
with a section boundary. */
Elf_Addr next_address;
unsigned padding_size;
size_t index;
size_t cur_index;
next_address = current_address + b->block_size;
padding_size = ((ALIGN_UP (next_address, image_target->section_align)
- next_address)
>> 1);
index = ((b->block_size - sizeof (*b)) >> 1);
cur_index = ((b->block_size - sizeof (*b)) >> 1);
grub_util_info ("adding %d padding fixup entries", padding_size);
while (padding_size--)
{
b->entries[index++] = 0;
b->entries[cur_index++] = 0;
b->block_size += 2;
}
}
@ -561,11 +569,11 @@ SUFFIX (add_fixup_entry) (struct fixup_block_list **cblock, grub_uint16_t type,
{
/* If not aligned with a 32-bit boundary, add
a padding entry. */
size_t index;
size_t cur_index;
grub_util_info ("adding a padding fixup entry");
index = ((b->block_size - sizeof (*b)) >> 1);
b->entries[index] = 0;
cur_index = ((b->block_size - sizeof (*b)) >> 1);
b->entries[cur_index] = 0;
b->block_size += 2;
}
@ -587,7 +595,7 @@ SUFFIX (add_fixup_entry) (struct fixup_block_list **cblock, grub_uint16_t type,
if (! flush)
{
grub_uint16_t entry;
size_t index;
size_t cur_index;
/* If not allocated yet, allocate a block with enough entries. */
if (! (*cblock)->state)
@ -605,9 +613,9 @@ SUFFIX (add_fixup_entry) (struct fixup_block_list **cblock, grub_uint16_t type,
grub_util_error ("too many fixup entries");
/* Add a new entry. */
index = ((b->block_size - sizeof (*b)) >> 1);
cur_index = ((b->block_size - sizeof (*b)) >> 1);
entry = GRUB_PE32_FIXUP_ENTRY (type, addr - b->page_rva);
b->entries[index] = grub_host_to_target16 (entry);
b->entries[cur_index] = grub_host_to_target16 (entry);
b->block_size += 2;
}
@ -671,7 +679,8 @@ SUFFIX (make_reloc_section) (Elf_Ehdr *e, void **out,
Elf_Addr addr;
addr = section_address + offset;
grub_util_info ("adding a relocation entry for 0x%x", addr);
grub_util_info ("adding a relocation entry for 0x%llx",
(unsigned long long) addr);
current_address
= SUFFIX (add_fixup_entry) (&lst,
GRUB_PE32_REL_BASED_HIGHLOW,
@ -690,7 +699,8 @@ SUFFIX (make_reloc_section) (Elf_Ehdr *e, void **out,
Elf_Addr addr;
addr = section_address + offset;
grub_util_info ("adding a relocation entry for 0x%llx", addr);
grub_util_info ("adding a relocation entry for 0x%llx",
(unsigned long long) addr);
current_address
= SUFFIX (add_fixup_entry) (&lst,
GRUB_PE32_REL_BASED_DIR64,
@ -719,7 +729,8 @@ SUFFIX (make_reloc_section) (Elf_Ehdr *e, void **out,
Elf_Addr addr;
addr = section_address + offset;
grub_util_info ("adding a relocation entry for 0x%llx", addr);
grub_util_info ("adding a relocation entry for 0x%llx",
(unsigned long long) addr);
current_address
= SUFFIX (add_fixup_entry) (&lst,
GRUB_PE32_REL_BASED_DIR64,
@ -760,11 +771,7 @@ SUFFIX (make_reloc_section) (Elf_Ehdr *e, void **out,
memcpy (ptr, &lst->b, grub_target_to_host32 (lst->b.block_size));
ptr += grub_target_to_host32 (lst->b.block_size);
}
if (current_address + *out != ptr)
{
grub_util_error ("Bug detected %d != %d\n", ptr - (grub_uint8_t *) *out,
current_address);
}
assert ((current_address + (grub_uint8_t *) *out) == ptr);
}
return current_address;
@ -853,8 +860,8 @@ SUFFIX (locate_sections) (Elf_Shdr *sections, Elf_Half section_entsize,
if (align)
current_address = ALIGN_UP (current_address + image_target->vaddr_offset,
align) - image_target->vaddr_offset;
grub_util_info ("locating the section %s at 0x%x",
name, current_address);
grub_util_info ("locating the section %s at 0x%llx",
name, (unsigned long long) current_address);
section_addresses[i] = current_address;
current_address += grub_host_to_target_addr (s->sh_size);
}
@ -878,8 +885,8 @@ SUFFIX (locate_sections) (Elf_Shdr *sections, Elf_Half section_entsize,
align)
- image_target->vaddr_offset;
grub_util_info ("locating the section %s at 0x%x",
name, current_address);
grub_util_info ("locating the section %s at 0x%llx",
name, (unsigned long long) current_address);
section_addresses[i] = current_address;
current_address += grub_host_to_target_addr (s->sh_size);
}
@ -958,16 +965,17 @@ SUFFIX (load_image) (const char *kernel_path, grub_size_t *exec_size,
i++, s = (Elf_Shdr *) ((char *) s + section_entsize))
if (grub_target_to_host32 (s->sh_type) == SHT_NOBITS)
{
Elf_Word align = grub_host_to_target_addr (s->sh_addralign);
Elf_Word sec_align = grub_host_to_target_addr (s->sh_addralign);
const char *name = strtab + grub_host_to_target32 (s->sh_name);
if (align)
if (sec_align)
current_address = ALIGN_UP (current_address
+ image_target->vaddr_offset, align)
+ image_target->vaddr_offset,
sec_align)
- image_target->vaddr_offset;
grub_util_info ("locating the section %s at 0x%x",
name, current_address);
grub_util_info ("locating the section %s at 0x%llx",
name, (unsigned long long) current_address);
section_vaddresses[i] = current_address
+ image_target->vaddr_offset;
current_address += grub_host_to_target_addr (s->sh_size);