diff options
author | Andrei Homescu <ah@immunant.com> | 2021-06-28 18:26:53 -0700 |
---|---|---|
committer | Mark Wielaard <mark@klomp.org> | 2021-07-08 11:31:22 +0200 |
commit | b3601167d7a4c9f34eb65c3892c9ef25e3c1c66f (patch) | |
tree | 4d5651a699e00edfca95b5e6c9666071500f056b | |
parent | 6648d378bd94f50b455a8550db5f43ef3690b451 (diff) | |
download | elfutils-b3601167d7a4c9f34eb65c3892c9ef25e3c1c66f.tar.gz |
libelf: Fix unaligned d_off offsets for input sections with large alignments
The mkl_memory_patched.o object inside the libmkl_core.a library from
the Intel Math Kernel Library version 2018.2.199 has this section
with an alignment of 4096 and offset of 0xb68:
[ 2] .data PROGBITS 0000000000000000 000b68 011000 00 WA 0 0 4096
Reading this file with libelf and trying to write it back to disk triggers
the following sequence of events:
1) code in elf_getdata.c clamps d_align for this section's data buffer
to the section's offset
2) code in elf32_updatenull.c checks if the alignment is a power of two
and incorrectly returns an error
This commit fixes this corner case by increasing the alignment to the
next power of two after the clamping, so the check passes.
A test that reproduces this bug using strip is also included.
Signed-off-by: Andrei Homescu <ah@immunant.com>
-rw-r--r-- | libelf/ChangeLog | 5 | ||||
-rw-r--r-- | libelf/elf_getdata.c | 13 | ||||
-rw-r--r-- | tests/ChangeLog | 12 | ||||
-rw-r--r-- | tests/Makefile.am | 5 | ||||
-rwxr-xr-x | tests/run-strip-largealign.sh | 35 | ||||
-rw-r--r-- | tests/testfile-largealign.o.bz2 | bin | 0 -> 183 bytes |
6 files changed, 65 insertions, 5 deletions
diff --git a/libelf/ChangeLog b/libelf/ChangeLog index a1fd414c..62437c55 100644 --- a/libelf/ChangeLog +++ b/libelf/ChangeLog @@ -1,3 +1,8 @@ +2021-06-09 Andrei Homescu <ah@immunant.com> + + * elf_getdata.c: Fix d_align for sections where alignment is larger + than offset. + 2020-12-12 Mark Wielaard <mark@klomp.org> * elf.h: Update from glibc. diff --git a/libelf/elf_getdata.c b/libelf/elf_getdata.c index 6ed44504..3cad29de 100644 --- a/libelf/elf_getdata.c +++ b/libelf/elf_getdata.c @@ -384,7 +384,18 @@ __libelf_set_rawdata_wrlock (Elf_Scn *scn) which should be uncommon. */ align = align ?: 1; if (type != SHT_NOBITS && align > offset) - align = offset; + { + /* Align the offset to the next power of two. Uses algorithm from + https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 */ + align = offset - 1; + align |= align >> 1; + align |= align >> 2; + align |= align >> 4; + align |= align >> 8; + align |= align >> 16; + align |= align >> 32; + align++; + } scn->rawdata.d.d_align = align; if (elf->class == ELFCLASS32 || (offsetof (struct Elf, state.elf32.ehdr) diff --git a/tests/ChangeLog b/tests/ChangeLog index b65cbeb7..28aaf85e 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,7 +1,15 @@ +2021-06-09 Andrei Homescu <ah@immunant.com> + + * testfile-largealign.o.bz2: New test file. + * run-strip-largealign.sh: New test. + * Makefile.am (TESTS): Add run-strip-largealign.sh. + (EXTRA_DIST): Add run-strip-largealign.sh and + testfile-largealign.o.bz2 + 2021-07-02 Mark Wielaard <mark@klomp.org> - * run-debuginfo-find.sh: unset VALGRIND_CMD before testing debuginfod - client cache. + * run-debuginfo-find.sh: unset VALGRIND_CMD before testing debuginfod + client cache. 2021-06-16 Frank Ch. Eigler <fche@redhat.com> diff --git a/tests/Makefile.am b/tests/Makefile.am index 76204277..8ac0d2e6 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -189,7 +189,7 @@ TESTS = run-arextract.sh run-arsymtest.sh run-ar.sh newfile test-nlist \ leb128 read_unaligned \ msg_tst system-elf-libelf-test \ $(asm_TESTS) run-disasm-bpf.sh run-low_high_pc-dw-form-indirect.sh \ - run-readelf-dw-form-indirect.sh + run-readelf-dw-form-indirect.sh run-strip-largealign.sh if !BIARCH export ELFUTILS_DISABLE_BIARCH = 1 @@ -511,7 +511,8 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \ testfile_pt_gnu_prop.bz2 testfile_pt_gnu_prop32.bz2 \ run-getphdrnum.sh testfile-phdrs.elf.bz2 \ run-test-includes.sh run-low_high_pc-dw-form-indirect.sh \ - run-readelf-dw-form-indirect.sh testfile-dw-form-indirect.bz2 + run-readelf-dw-form-indirect.sh testfile-dw-form-indirect.bz2 \ + testfile-largealign.bz2 run-strip-largealign.sh if USE_VALGRIND diff --git a/tests/run-strip-largealign.sh b/tests/run-strip-largealign.sh new file mode 100755 index 00000000..4f81d3c1 --- /dev/null +++ b/tests/run-strip-largealign.sh @@ -0,0 +1,35 @@ +#! /bin/sh +# Copyright (C) 2021 Runsafe Security, Inc. +# This file is part of elfutils. +# +# This file is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# elfutils is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +. $srcdir/test-subr.sh + +# = testfile-largealign.S = +# section .data +# align 4096 +# dd 0x12345678 +# +# nasm -f elf64 -o testfile-largealign.o testfile-largealign.S + +infile=testfile-largealign.o +outfile=$infile.stripped + +testfiles $infile +tempfiles $outfile + +testrun ${abs_top_builddir}/src/strip -o $outfile $infile +testrun ${abs_top_builddir}/src/elflint --gnu $outfile diff --git a/tests/testfile-largealign.o.bz2 b/tests/testfile-largealign.o.bz2 Binary files differnew file mode 100644 index 00000000..324c1eae --- /dev/null +++ b/tests/testfile-largealign.o.bz2 |