* 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

@ -1,3 +1,22 @@
2012-02-10 Vladimir Serbinenko <phcoder@gmail.com>
* 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.
2012-02-10 Vladimir Serbinenko <phcoder@gmail.com>
* util/grub-setup.c (setup): Remove unused variable. Avoid shadowing.

View file

@ -596,7 +596,7 @@ compress_kernel_lzma (char *kernel_img, size_t kernel_size,
kernel_size,
&props, out_props, &out_props_size,
0, NULL, &g_Alloc, &g_Alloc) != SZ_OK)
grub_util_error (_("cannot compress the kernel image"));
grub_util_error ("%s", _("cannot compress the kernel image"));
}
#ifdef HAVE_LIBLZMA
@ -697,8 +697,8 @@ struct fixup_block_list
#undef MKIMAGE_ELF64
static void
generate_image (const char *dir, char *prefix, FILE *out, const char *outname,
char *mods[],
generate_image (const char *dir, const char *prefix,
FILE *out, const char *outname, char *mods[],
char *memdisk_path, char *config_path,
struct image_target_desc *image_target, int note,
grub_compression_t comp)
@ -735,7 +735,8 @@ generate_image (const char *dir, char *prefix, FILE *out, const char *outname,
if (memdisk_path)
{
memdisk_size = ALIGN_UP(grub_util_get_image_size (memdisk_path), 512);
grub_util_info ("the size of memory disk is 0x%x", memdisk_size);
grub_util_info ("the size of memory disk is 0x%llx",
(unsigned long long) memdisk_size);
total_module_size += memdisk_size + sizeof (struct grub_module_header);
}
@ -743,7 +744,8 @@ generate_image (const char *dir, char *prefix, FILE *out, const char *outname,
{
config_size_pure = grub_util_get_image_size (config_path) + 1;
config_size = ALIGN_ADDR (config_size_pure);
grub_util_info ("the size of config file is 0x%x", config_size);
grub_util_info ("the size of config file is 0x%llx",
(unsigned long long) config_size);
total_module_size += config_size + sizeof (struct grub_module_header);
}
@ -757,7 +759,8 @@ generate_image (const char *dir, char *prefix, FILE *out, const char *outname,
total_module_size += (ALIGN_ADDR (grub_util_get_image_size (p->name))
+ sizeof (struct grub_module_header));
grub_util_info ("the total module size is 0x%x", total_module_size);
grub_util_info ("the total module size is 0x%llx",
(unsigned long long) total_module_size);
if (image_target->voidp_sizeof == 4)
kernel_img = load_image32 (kernel_path, &exec_size, &kernel_size, &bss_size,
@ -874,12 +877,13 @@ generate_image (const char *dir, char *prefix, FILE *out, const char *outname,
offset += prefix_size;
}
grub_util_info ("kernel_img=%p, kernel_size=0x%x", kernel_img, kernel_size);
grub_util_info ("kernel_img=%p, kernel_size=0x%llx", kernel_img,
(unsigned long long) kernel_size);
compress_kernel (image_target, kernel_img, kernel_size + total_module_size,
&core_img, &core_size, comp);
free (kernel_img);
grub_util_info ("the core size is 0x%x", core_size);
grub_util_info ("the core size is 0x%llx", (unsigned long long) core_size);
if (!(image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS)
&& image_target->total_module_size != TARGET_NO_FIELD)
@ -915,7 +919,7 @@ generate_image (const char *dir, char *prefix, FILE *out, const char *outname,
if ((image_target->id == IMAGE_I386_PC
|| image_target->id == IMAGE_I386_PC_PXE)
&& decompress_size > GRUB_KERNEL_I386_PC_LINK_ADDR - 0x8200)
grub_util_error (_("Decompressor is too big"));
grub_util_error ("%s", _("Decompressor is too big"));
if (image_target->decompressor_compressed_size != TARGET_NO_FIELD)
*((grub_uint32_t *) (decompress_img
@ -1053,8 +1057,8 @@ generate_image (const char *dir, char *prefix, FILE *out, const char *outname,
image_target->section_align);
pe_img = xmalloc (reloc_addr + reloc_size);
memset (pe_img, 0, header_size);
memcpy (pe_img + header_size, core_img, core_size);
memcpy (pe_img + reloc_addr, rel_section, reloc_size);
memcpy ((char *) pe_img + header_size, core_img, core_size);
memcpy ((char *) pe_img + reloc_addr, rel_section, reloc_size);
header = pe_img;
/* The magic. */
@ -1265,7 +1269,7 @@ generate_image (const char *dir, char *prefix, FILE *out, const char *outname,
aout_head->a_text = grub_host_to_target32 (core_size);
aout_head->a_entry
= grub_host_to_target32 (GRUB_BOOT_SPARC64_IEEE1275_IMAGE_ADDRESS);
memcpy (aout_img + sizeof (*aout_head), core_img, core_size);
memcpy ((char *) aout_img + sizeof (*aout_head), core_img, core_size);
free (core_img);
core_img = aout_img;
@ -1284,7 +1288,7 @@ generate_image (const char *dir, char *prefix, FILE *out, const char *outname,
boot_path = grub_util_get_path (dir, "diskboot.img");
boot_size = grub_util_get_image_size (boot_path);
if (boot_size != GRUB_DISK_SECTOR_SIZE)
grub_util_error (_("diskboot.img is not one sector size"));
grub_util_error ("%s", _("diskboot.img is not one sector size"));
boot_img = grub_util_read_image (boot_path);
@ -1349,11 +1353,12 @@ generate_image (const char *dir, char *prefix, FILE *out, const char *outname,
GRUB_MD_SHA512->final (context);
if (grub_memcmp (GRUB_MD_SHA512->read (context), fwstart_good_hash,
GRUB_MD_SHA512->mdlen) != 0)
grub_util_warn (_("fwstart.img doesn't match the known good version. "
grub_util_warn ("%s",
_("fwstart.img doesn't match the known good version. "
"proceed at your own risk"));
if (core_size + boot_size > 512 * 1024)
grub_util_error (_("firmware image is too big"));
grub_util_error ("%s", _("firmware image is too big"));
rom_size = 512 * 1024;
rom_img = xmalloc (rom_size);
@ -1377,7 +1382,7 @@ generate_image (const char *dir, char *prefix, FILE *out, const char *outname,
size_t rom_size;
if (core_size > 512 * 1024)
grub_util_error (_("firmware image is too big"));
grub_util_error ("%s", _("firmware image is too big"));
rom_size = 512 * 1024;
rom_img = xmalloc (rom_size);
@ -1584,21 +1589,21 @@ generate_image (const char *dir, char *prefix, FILE *out, const char *outname,
if (note)
{
int note_size = sizeof (struct grub_ieee1275_note);
struct grub_ieee1275_note *note = (struct grub_ieee1275_note *)
struct grub_ieee1275_note *note_ptr = (struct grub_ieee1275_note *)
(elf_img + program_size + header_size);
grub_util_info ("adding CHRP NOTE segment");
note->header.namesz = grub_host_to_target32 (sizeof (GRUB_IEEE1275_NOTE_NAME));
note->header.descsz = grub_host_to_target32 (note_size);
note->header.type = grub_host_to_target32 (GRUB_IEEE1275_NOTE_TYPE);
strcpy (note->header.name, GRUB_IEEE1275_NOTE_NAME);
note->descriptor.real_mode = grub_host_to_target32 (0xffffffff);
note->descriptor.real_base = grub_host_to_target32 (0x00c00000);
note->descriptor.real_size = grub_host_to_target32 (0xffffffff);
note->descriptor.virt_base = grub_host_to_target32 (0xffffffff);
note->descriptor.virt_size = grub_host_to_target32 (0xffffffff);
note->descriptor.load_base = grub_host_to_target32 (0x00004000);
note_ptr->header.namesz = grub_host_to_target32 (sizeof (GRUB_IEEE1275_NOTE_NAME));
note_ptr->header.descsz = grub_host_to_target32 (note_size);
note_ptr->header.type = grub_host_to_target32 (GRUB_IEEE1275_NOTE_TYPE);
strcpy (note_ptr->header.name, GRUB_IEEE1275_NOTE_NAME);
note_ptr->descriptor.real_mode = grub_host_to_target32 (0xffffffff);
note_ptr->descriptor.real_base = grub_host_to_target32 (0x00c00000);
note_ptr->descriptor.real_size = grub_host_to_target32 (0xffffffff);
note_ptr->descriptor.virt_base = grub_host_to_target32 (0xffffffff);
note_ptr->descriptor.virt_size = grub_host_to_target32 (0xffffffff);
note_ptr->descriptor.load_base = grub_host_to_target32 (0x00004000);
phdr++;
phdr->p_type = grub_host_to_target32 (PT_NOTE);
@ -1707,8 +1712,6 @@ argp_parser (int key, char *arg, struct argp_state *state)
know is a pointer to our arguments structure. */
struct arguments *arguments = state->input;
char *p;
switch (key)
{
case 'o':
@ -1770,7 +1773,8 @@ argp_parser (int key, char *arg, struct argp_state *state)
#ifdef HAVE_LIBLZMA
arguments->comp = COMPRESSION_XZ;
#else
grub_util_error (_("grub-mkimage is compiled without XZ support"));
grub_util_error ("%s",
_("grub-mkimage is compiled without XZ support"));
#endif
}
else if (grub_strcmp (arg, "none") == 0)

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);