From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by sourceware.org (Postfix) with ESMTPS id 92DD43847825 for ; Tue, 29 Jun 2021 01:26:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 92DD43847825 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=immunant.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=immunant.com Received: by mail-pj1-x1033.google.com with SMTP id l11so11369660pji.5 for ; Mon, 28 Jun 2021 18:26:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=immunant-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Yz2YJvRPb+YZ5hWeR0YVPbGYS/heb03hq1blvZj/174=; b=yVCqDcBY3D5CZRNZINkUt66KMGIZhlyRTSFnBW7jakANQ/xvR9R94cTdneYy6nt+U4 wyImrKiQMaRhyqgntOaq4S9TQGhNWSPwNxcPt4CF9C0FM9whZgiWxmM8oGijVvLiauLN YXHnf1Nj52vSKeUBA2Bs6vED1O4Hm/CydRdoCPT1uzi3wU5XYSPHI+0mvqoLsGJaYAhZ FoUlMAHEtP2QniE+fXXQvaoZf6I2VosWZ3es9V+j8lhWWJXk5CNga8JaQAY2OkPd9tV5 1lHe8h8OqzDxZDl+BPtm+vOmayKiCBY/3OZR/YdhMnIqzHC39rCRcvJgcvS0S/N3uSvv 8hbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Yz2YJvRPb+YZ5hWeR0YVPbGYS/heb03hq1blvZj/174=; b=IxigU3CAw6w4HmhcrJrKNAJAKwPiB1Ha9KPlufX780mJhH4ATfAhNAvld82nPgau9X xbehm5VuE9+qhb6oylUnYcPQOgEp0cydu6Czd6daZc/BQtmZrLXsrQ5WD9t5gvFhcGzU 6A56mPkqNK0jcFr+s0ZHdhNClF7PEO/388cRJw9uijyDZQ/k9T51CpkSNpjNblYcSwaw MoGeZ3BXOdQ7sbo7xerp0Mo6sxd8VNS0NIDh4AZ32hYblU67brCPbiYXAV/md/aMX0om l1WjUjPOLeErZaXkqu3sJsW0ncS8HJ4IMWQcThckNoj0Rchd2rl0xJIdr1a3tbsorveS 0C4Q== X-Gm-Message-State: AOAM531GrQcDmr/nV1wG06CvdEQA560ahBikTJhglEz1UJ8qaGO9mjFo ip8XLHbyiTYXmBVUVgzP/ilYexZipt5O2w== X-Google-Smtp-Source: ABdhPJzs4CQcCCVM+mO05ZTA4aL9JbBvblZfmdqD4LOOFnCeXP6aZ/8Bv1NNrHYYDPCv6zHnYe1kRA== X-Received: by 2002:a17:90b:a0c:: with SMTP id gg12mr30146314pjb.138.1624930018631; Mon, 28 Jun 2021 18:26:58 -0700 (PDT) Received: from soufflegirl.immunant.com ([205.209.28.226]) by smtp.gmail.com with ESMTPSA id m10sm4192105pfa.42.2021.06.28.18.26.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Jun 2021 18:26:58 -0700 (PDT) From: Andrei Homescu To: elfutils-devel@sourceware.org Cc: Andrei Homescu Subject: [PATCH] libelf: Fix unaligned d_off offsets for input sections with large alignments Date: Mon, 28 Jun 2021 18:26:53 -0700 Message-Id: <20210629012653.2375942-1-ah@immunant.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, URIBL_SBL, URIBL_SBL_A autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 29 Jun 2021 01:27:01 -0000 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 --- libelf/ChangeLog | 5 +++++ libelf/elf_getdata.c | 13 +++++++++++- tests/ChangeLog | 7 +++++++ tests/Makefile.am | 5 +++-- tests/run-strip-largealign.sh | 34 ++++++++++++++++++++++++++++++++ tests/testfile-largealign.o.bz2 | Bin 0 -> 183 bytes 6 files changed, 61 insertions(+), 3 deletions(-) create mode 100755 tests/run-strip-largealign.sh create mode 100644 tests/testfile-largealign.o.bz2 diff --git a/libelf/ChangeLog b/libelf/ChangeLog index a1fd414c..6144ee78 100644 --- a/libelf/ChangeLog +++ b/libelf/ChangeLog @@ -1,3 +1,8 @@ +2021-05-27 Andrei Homescu + + * elf_getdata.c: Fix d_align for sections where alignment is larger + than offset. + 2020-12-12 Mark Wielaard * 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 38e92659..12a6dc7f 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,3 +1,10 @@ +2021-06-09 Andrei Homescu + + * 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-05-14 Frank Ch. Eigler PR27859 diff --git a/tests/Makefile.am b/tests/Makefile.am index dc7517d9..e93f0c2c 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -188,7 +188,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 @@ -509,7 +509,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..897ca5ba --- /dev/null +++ b/tests/run-strip-largealign.sh @@ -0,0 +1,34 @@ +#! /bin/sh +# Copyright (C) 2011 TODO. +# 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 . +# + +. $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 diff --git a/tests/testfile-largealign.o.bz2 b/tests/testfile-largealign.o.bz2 new file mode 100644 index 0000000000000000000000000000000000000000..324c1eae376dab373d4860188d1c29d577b64abc GIT binary patch literal 183 zcmV;o07(BrT4*^jL0KkKS$lTN?*IUO|K$Gs@n9qY1_MJx2>>=i9ALlzKmb4hAON5M zfHcu5lL%>%qalz6gBn8_42FY2qd*z}8W~!#9HOGvK}7V;m`k9kmnQ7Xs*(clq9*|2 zA}D|*im9-mNr*r@N){aWV)s@c9Zj;%)REkP6oAtzQc;Q^velES!XQ@Wspq}yD~@;F lCD&sZA1p^UFHqN<4rm_o_X>?mK--t`cO+AV2?w`qzVN>OO*jAm literal 0 HcmV?d00001 -- 2.31.1