Re: [RFC, PATCH 4/4] elf_core_dump(): Add extended numbering support

From: AmÃrico Wang
Date: Tue Dec 15 2009 - 03:27:15 EST


On Tue, Dec 15, 2009 at 10:41 AM, Daisuke HATAYAMA
<d.hatayama@xxxxxxxxxxxxxx> wrote:
> The current ELF dumper implementation can produce broken corefiles
> if program headers exceed 65535. This number is determined by the
> number of vmas which the process have. In particular, some extreme
> programs may use more than 65535 vmas. (If you google max_map_count,
> you can find some users facing this problem.) This kind of program
> never be able to generate correct coredumps.
>
> This patch implements ``extended numbering'' that uses sh_info
> field of the first section header instead of e_phnum field in order
> to represent upto 4294967295 vmas.
>
> This is supported by AMD64-ABI(http://www.x86-64.org/documentation.html)
> and Solaris(http://docs.sun.com/app/docs/doc/817-1984/). Of course,
> we are preparing patches for gdb and binutils.
>
> Signed-off-by: Daisuke HATAYAMA <d.hatayama@xxxxxxxxxxxxxx>


Some comments below.

> ---
> Âarch/ia64/kernel/elfcore.c | Â 16 ++++++++
> Âarch/um/sys-i386/elfcore.c | Â 18 +++++++++
> Âfs/binfmt_elf.c      Â|  88 +++++++++++++++++++++++++++++++++++++++-----
> Âinclude/linux/elf.h    Â|  26 ++++++++++++-
> Â4 files changed, 137 insertions(+), 11 deletions(-)
>
> diff --git a/arch/ia64/kernel/elfcore.c b/arch/ia64/kernel/elfcore.c
> index 9c0dd8b..a15d8d4 100644
> --- a/arch/ia64/kernel/elfcore.c
> +++ b/arch/ia64/kernel/elfcore.c
> @@ -73,3 +73,19 @@ int elf_core_write_extra_data(struct file *file, size_t *size,
> Â Â Â Â}
> Â Â Â Âreturn 1;
> Â}
> +
> +size_t elf_core_extra_data_size(void)
> +{
> + Â Â Â const struct elf_phdr *const gate_phdrs =
> + Â Â Â Â Â Â Â (const struct elf_phdr *) (GATE_ADDR + GATE_EHDR->e_phoff);
> + Â Â Â int i;
> + Â Â Â size_t size = 0;
> +
> + Â Â Â for (i = 0; i < GATE_EHDR->e_phnum; ++i) {
> + Â Â Â Â Â Â Â if (gate_phdrs[i].p_type == PT_LOAD) {
> + Â Â Â Â Â Â Â Â Â Â Â size += PAGE_ALIGN(gate_phdrs[i].p_memsz);
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> + Â Â Â return size;
> +}
> diff --git a/arch/um/sys-i386/elfcore.c b/arch/um/sys-i386/elfcore.c
> index 4e320f0..4e34e47 100644
> --- a/arch/um/sys-i386/elfcore.c
> +++ b/arch/um/sys-i386/elfcore.c
> @@ -76,3 +76,21 @@ int elf_core_write_extra_data(struct file *file, size_t *size,
> Â Â Â Â}
> Â Â Â Âreturn 1;
> Â}
> +
> +size_t elf_core_extra_data_size(void)
> +{
> + Â Â Â if ( vsyscall_ehdr ) {
> + Â Â Â Â Â Â Â const struct elfhdr *const ehdrp =
> + Â Â Â Â Â Â Â Â Â Â Â (struct elfhdr *)vsyscall_ehdr;
> + Â Â Â Â Â Â Â const struct elf_phdr *const phdrp =
> + Â Â Â Â Â Â Â Â Â Â Â (const struct elf_phdr *) (vsyscall_ehdr + ehdrp->e_phoff);
> + Â Â Â Â Â Â Â int i;
> +
> + Â Â Â Â Â Â Â for (i = 0; i < ehdrp->e_phnum; ++i) {
> + Â Â Â Â Â Â Â Â Â Â Â if (phdrp[i].p_type == PT_LOAD) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return (size_t) phdrp[i].p_filesz;
> + Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â }

Unnecessary braces.

> + Â Â Â }
> + Â Â Â return 0;
> +}
> diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
> index cded1ba..ad2ad5f 100644
> --- a/fs/binfmt_elf.c
> +++ b/fs/binfmt_elf.c
> @@ -1895,6 +1895,38 @@ static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
> Â Â Â Âreturn gate_vma;
> Â}
>
> +static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Âelf_addr_t e_shoff, int segs)
> +{
> + Â Â Â elf->e_shoff = e_shoff;
> + Â Â Â elf->e_shentsize = sizeof(*shdr4extnum);
> + Â Â Â elf->e_shnum = 1;
> + Â Â Â elf->e_shstrndx = SHN_UNDEF;
> +
> + Â Â Â shdr4extnum->sh_name = 0;
> + Â Â Â shdr4extnum->sh_addr = 0;
> + Â Â Â shdr4extnum->sh_offset = 0;
> + Â Â Â shdr4extnum->sh_type = SHT_NULL;
> + Â Â Â shdr4extnum->sh_flags = 0;
> + Â Â Â shdr4extnum->sh_size = elf->e_shnum;
> + Â Â Â shdr4extnum->sh_link = elf->e_shstrndx;
> + Â Â Â shdr4extnum->sh_info = segs;
> + Â Â Â shdr4extnum->sh_addralign = 0;
> + Â Â Â shdr4extnum->sh_entsize = 0;


Why not kzalloc() the struct and just fill the non-zero values?

<snip>

> @@ -2079,11 +2139,19 @@ static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un
> Â Â Â Âif (!elf_core_write_extra_data(file, &size, limit))
> Â Â Â Â Â Â Â Âgoto end_coredump;
>
> + Â Â Â if (e_phnum == PN_XNUM) {
> + Â Â Â Â Â Â Â size += sizeof(*shdr4extnum);
> + Â Â Â Â Â Â Â if (size > limit
> + Â Â Â Â Â Â Â Â Â || !dump_write(file, shdr4extnum, sizeof(*shdr4extnum)))
> + Â Â Â Â Â Â Â Â Â Â Â goto end_coredump;
> + Â Â Â }
> +
> Âend_coredump:

Even without the last 'goto', it will also come here finally.
So, that is unnecessary.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/