https://sourceware.org/bugzilla/show_bug.cgi?id=35073

            Bug ID: 35073
           Summary: elf_clone() of an archive with ELF_C_EMPTY makes
                    elf_end() free a pointer inside the clone
           Product: elfutils
           Version: unspecified
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libelf
          Assignee: unassigned at sourceware dot org
          Reporter: dl at ieee dot org
                CC: elfutils-devel at sourceware dot org
  Target Milestone: ---

Checked out git master 11d2e85059645688ca2942e2943dab3a02fce1df on 2026-11-18.

elf_end() on a clone of a normal .a frees an address that was not malloc'd.

    Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
    Elf *clone = elf_clone (elf, ELF_C_EMPTY);
    elf_end (clone);

On this run elf_kind() of the opened archive printed 1. elf_clone() passes that
kind into allocate_elf(), and the clone is what elf_end() then frees.

Any cmd other than ELF_C_EMPTY hits this and returns NULL:

    if (cmd != ELF_C_EMPTY)
        // XXX TODO handle ELF_C_READ/WRITE etc
        goto out;

The ELF_C_EMPTY path stores the address of the clone's own section list:

    retval->state.elf.scns_last = &retval->state.elf32.scns;

This is elf_clone.c:69. The pointer points into the clone.

In libelfP.h, state.elf.scns_last and state.ar.long_names are the same offset
in the state union. scns_last follows three pointers (ehdr, shdr, phdr).
long_names follows two pointers and a size_t (children, ar_sym, ar_sym_num). On
this 64-bit run those are the same number of bytes, so the store above is the
value elf_end() reads as the long-name table. For ELF_K_AR it does free
(elf->state.ar.long_names) at elf_end.c:130.

ASan: free of an address which was not malloc'd, 256 bytes inside the 312-byte
region. Allocated by allocate_elf() at common.h:71, called from elf_clone.c:55.

elf_clone() should fill in state.ar for an archive, or reject ELF_C_EMPTY for
one.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

Reply via email to