PackLinuxElf32::canPack must use explicit get_native32

This commit is contained in:
John Reiser
2007-11-13 11:11:31 -08:00
parent 887aa20116
commit 1a035e2866
+23 -20
View File
@@ -841,11 +841,12 @@ bool PackLinuxElf32::canPack()
return false; return false;
// additional requirements for linux/elf386 // additional requirements for linux/elf386
if (ehdr->e_ehsize != sizeof(*ehdr)) { if (get_native16(&ehdr->e_ehsize) != sizeof(*ehdr)) {
throwCantPack("invalid Ehdr e_ehsize; try '--force-execve'"); throwCantPack("invalid Ehdr e_ehsize; try '--force-execve'");
return false; return false;
} }
unsigned const e_phoff = ehdr->e_phoff; unsigned const e_shoff = get_native32(&ehdr->e_shoff);
unsigned const e_phoff = get_native32(&ehdr->e_phoff);
if (e_phoff != sizeof(*ehdr)) {// Phdrs not contiguous with Ehdr if (e_phoff != sizeof(*ehdr)) {// Phdrs not contiguous with Ehdr
throwCantPack("non-contiguous Ehdr/Phdr; try '--force-execve'"); throwCantPack("non-contiguous Ehdr/Phdr; try '--force-execve'");
return false; return false;
@@ -853,23 +854,24 @@ bool PackLinuxElf32::canPack()
unsigned osabi0 = buf[Elf32_Ehdr::EI_OSABI]; unsigned osabi0 = buf[Elf32_Ehdr::EI_OSABI];
// The first PT_LOAD32 must cover the beginning of the file (0==p_offset). // The first PT_LOAD32 must cover the beginning of the file (0==p_offset).
unsigned const e_phnum = ehdr->e_phnum; unsigned const e_phnum = get_native16(&ehdr->e_phnum);
Elf32_Phdr const *phdr = (Elf32_Phdr const *)(buf + e_phoff); Elf32_Phdr const *phdr = (Elf32_Phdr const *)(buf + e_phoff);
for (unsigned j=0; j < e_phnum; ++phdr, ++j) { for (unsigned j=0; j < e_phnum; ++phdr, ++j) {
if (j >= 14) if (j >= 14)
return false; return false;
if (1!=exetype && phdr->PT_LOAD32 == phdr->p_type) { unsigned const p_type = get_native32(&phdr->p_type);
if (phdr->p_offset != 0) { unsigned const p_offset = get_native32(&phdr->p_offset);
if (1!=exetype && phdr->PT_LOAD32 == p_type) {
if (p_offset != 0) {
throwCantPack("invalid Phdr p_offset; try '--force-execve'"); throwCantPack("invalid Phdr p_offset; try '--force-execve'");
return false; return false;
} }
exetype = 1; exetype = 1;
} }
if (Elf32_Ehdr::ELFOSABI_NONE==osabi0 // Still seems to be generic. if (Elf32_Ehdr::ELFOSABI_NONE==osabi0 // Still seems to be generic.
&& NULL!=osabi_note && phdr->PT_NOTE == phdr->p_type) { && NULL!=osabi_note && phdr->PT_NOTE == p_type) {
unsigned const offset = phdr->p_offset;
struct Elf32_Note note; memset(&note, 0, sizeof(note)); struct Elf32_Note note; memset(&note, 0, sizeof(note));
fi->seek(offset, SEEK_SET); fi->seek(p_offset, SEEK_SET);
fi->readx(&note, sizeof(note)); fi->readx(&note, sizeof(note));
fi->seek(0, SEEK_SET); fi->seek(0, SEEK_SET);
if (4==get_native32(&note.descsz) if (4==get_native32(&note.descsz)
@@ -903,26 +905,26 @@ bool PackLinuxElf32::canPack()
// Otherwise (no __libc_start_main as global undefined): skip it. // Otherwise (no __libc_start_main as global undefined): skip it.
// Also allow __uClibc_main and __uClibc_start_main . // Also allow __uClibc_main and __uClibc_start_main .
if (Elf32_Ehdr::ET_DYN==ehdr->e_type) { if (Elf32_Ehdr::ET_DYN==get_native16(&ehdr->e_type)) {
// The DT_STRTAB has no designated length. Read the whole file. // The DT_STRTAB has no designated length. Read the whole file.
file_image = new char[file_size]; file_image = new char[file_size];
fi->seek(0, SEEK_SET); fi->seek(0, SEEK_SET);
fi->readx(file_image, file_size); fi->readx(file_image, file_size);
ehdri= *ehdr; ehdri= *ehdr;
phdri= (Elf32_Phdr *)(ehdr->e_phoff + file_image); // do not free() !! phdri= (Elf32_Phdr *)(e_phoff + file_image); // do not free() !!
shdri= (Elf32_Shdr *)(ehdr->e_shoff + file_image); // do not free() !! shdri= (Elf32_Shdr *)(e_shoff + file_image); // do not free() !!
n_elf_shnum = ehdr->e_shnum; n_elf_shnum = get_native16(&ehdr->e_shnum);
shdri = (Elf32_Shdr const *)(ehdr->e_shoff + file_image); shdri = (Elf32_Shdr const *)(e_shoff + file_image);
//sec_strndx = &shdri[ehdr->e_shstrndx]; //sec_strndx = &shdri[ehdr->e_shstrndx];
//shstrtab = (char const *)(sec_strndx->sh_offset + file_image); //shstrtab = (char const *)(sec_strndx->sh_offset + file_image);
sec_dynsym = elf_find_section_type(Elf32_Shdr::SHT_DYNSYM); sec_dynsym = elf_find_section_type(Elf32_Shdr::SHT_DYNSYM);
sec_dynstr = sec_dynsym->sh_link + shdri; sec_dynstr = get_native32(&sec_dynsym->sh_link) + shdri;
int j= ehdr->e_phnum; int j= e_phnum;
phdr= phdri; phdr= phdri;
for (; --j>=0; ++phdr) if (Elf32_Phdr::PT_DYNAMIC==phdr->p_type) { for (; --j>=0; ++phdr) if (Elf32_Phdr::PT_DYNAMIC==get_native32(&phdr->p_type)) {
dynseg= (Elf32_Dyn const *)(phdr->p_offset + file_image); dynseg= (Elf32_Dyn const *)(get_native32(&phdr->p_offset) + file_image);
break; break;
} }
// elf_find_dynamic() returns 0 if 0==dynseg. // elf_find_dynamic() returns 0 if 0==dynseg.
@@ -945,11 +947,12 @@ bool PackLinuxElf32::canPack()
break; break;
} }
if (sec_dynsym) { if (sec_dynsym) {
Elf32_Sym const *symp = (Elf32_Sym const *)(sec_dynsym->sh_offset + file_image); Elf32_Sym const *symp = (Elf32_Sym const *)(get_native32(&sec_dynsym->sh_offset) + file_image);
Elf32_Sym const *const symlwa = (Elf32_Sym const *)( Elf32_Sym const *const symlwa = (Elf32_Sym const *)(
sec_dynsym->sh_size + sec_dynsym->sh_offset + file_image); get_native32(&sec_dynsym->sh_size) +
get_native32(&sec_dynsym->sh_offset) + file_image);
for (; symp < symlwa; ++symp) for (; symp < symlwa; ++symp)
if (0==strcmp(run_start[j], symp->st_name + dynstr)) { if (0==strcmp(run_start[j], get_native32(&symp->st_name) + dynstr)) {
goto found; goto found;
} }
} }