From unknown Fri Jun 20 07:22:35 2025 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Mailer: MIME-tools 5.509 (Entity 5.509) Content-Type: text/plain; charset=utf-8 From: bug#74669 <74669@debbugs.gnu.org> To: bug#74669 <74669@debbugs.gnu.org> Subject: Status: [PATCH] gnu: elfutils: Fix cross-compilation. Reply-To: bug#74669 <74669@debbugs.gnu.org> Date: Fri, 20 Jun 2025 14:22:35 +0000 retitle 74669 [PATCH] gnu: elfutils: Fix cross-compilation. reassign 74669 guix-patches submitter 74669 Zheng Junjie severity 74669 normal tag 74669 patch thanks From debbugs-submit-bounces@debbugs.gnu.org Tue Dec 03 07:19:23 2024 Received: (at submit) by debbugs.gnu.org; 3 Dec 2024 12:19:23 +0000 Received: from localhost ([127.0.0.1]:58516 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tIRsB-0002xz-09 for submit@debbugs.gnu.org; Tue, 03 Dec 2024 07:19:23 -0500 Received: from lists.gnu.org ([209.51.188.17]:36048) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tIRs6-0002xo-R2 for submit@debbugs.gnu.org; Tue, 03 Dec 2024 07:19:21 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tIRs6-0002hG-K3 for guix-patches@gnu.org; Tue, 03 Dec 2024 07:19:18 -0500 Received: from smtp81.cstnet.cn ([159.226.251.81] helo=cstnet.cn) by eggs.gnu.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1tIRs2-0006n2-4A for guix-patches@gnu.org; Tue, 03 Dec 2024 07:19:18 -0500 Received: from m.tailaa68d.ts.net (unknown [107.174.64.25]) by APP-03 (Coremail) with SMTP id rQCowAAXuO0t905newhyBw--.18010S2; Tue, 03 Dec 2024 20:18:58 +0800 (CST) From: Zheng Junjie To: guix-patches@gnu.org Subject: [PATCH] gnu: elfutils: Fix cross-compilation. Date: Tue, 3 Dec 2024 20:18:51 +0800 Message-ID: <814be590b438138df7b35507a0fd266b319da388.1733228331.git.zhengjunjie@iscas.ac.cn> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CM-TRANSID: rQCowAAXuO0t905newhyBw--.18010S2 X-Coremail-Antispam: 1UD129KBjvJXoW3XFyUJryxCF4DCr4rtF4Utwb_yoW3Cw15pa yayFs5JayrJrn7Zrs3JF10kw1rZ397XFy5Z34fG3W0vF4Ygwn5GrWrtryYkFW7Crs7C343 Zws5tFykXas8A3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUy2b7Iv0xC_Kw4lb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Jr0_Gr1l84ACjcxK6I8E87Iv67AKxVWxJr0_GcWl84ACjcxK6I 8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI 64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8Jw Am72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64vIr41l4I8I 3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxV WUGVWUWwC2zVAF1VAY17CE14v26r1j6r15MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAF wI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE42xK8VAvwI8IcI k0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r1j 6r4UYxBIdaVFxhVjvjDU0xZFpf9x07bOoGdUUUUU= X-Originating-IP: [107.174.64.25] X-CM-SenderInfo: x2kh0wxmxqyx3h6l2u1dvotugofq/ Received-SPF: pass client-ip=159.226.251.81; envelope-from=zhengjunjie@iscas.ac.cn; helo=cstnet.cn X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.4 (-) X-Debbugs-Envelope-To: submit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -2.4 (--) * gnu/packages/elf.scm (elfutils): Fix cross-compilation [arguments]: When cross-compilation, apply patch. [native-inputs]: When cross-compilation, Add patch. * gnu/packages/patches/elfutils-libdwfl-Rewrite-reading-of-ar_size-in-elf_begin_rand.patch: new file. * gnu/local.mk (dist_patch_DATA): Register it. Change-Id: Ia1ab9c09124a140608bda34bf62f0ae80eb44f9b --- gnu/local.mk | 1 + gnu/packages/elf.scm | 21 +++- ...reading-of-ar_size-in-elf_begin_rand.patch | 116 ++++++++++++++++++ 3 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 gnu/packages/patches/elfutils-libdwfl-Rewrite-reading-of-ar_size-in-elf_begin_rand.patch diff --git a/gnu/local.mk b/gnu/local.mk index a7bd32453b0..eee768615ea 100644 --- a/gnu/local.mk +++ b/gnu/local.mk @@ -1177,6 +1177,7 @@ dist_patch_DATA = \ %D%/packages/patches/eigen-fix-strict-aliasing-bug.patch \ %D%/packages/patches/einstein-build.patch \ %D%/packages/patches/elfutils-tests-ptrace.patch \ + %D%/packages/patches/elfutils-libdwfl-Rewrite-reading-of-ar_size-in-elf_begin_rand.patch \ %D%/packages/patches/elixir-path-length.patch \ %D%/packages/patches/elm-ghc9.2.patch \ %D%/packages/patches/elm-offline-package-registry.patch \ diff --git a/gnu/packages/elf.scm b/gnu/packages/elf.scm index 81a753a4835..4fae3929ba2 100644 --- a/gnu/packages/elf.scm +++ b/gnu/packages/elf.scm @@ -11,6 +11,7 @@ ;;; Copyright © 2021 Leo Le Bouter ;;; Copyright © 2021 Maxime Devos ;;; Copyright © 2023, 2024 Janneke Nieuwenhuizen +;;; Copyright © 2024 Zheng Junjie <873216071@qq.com> ;;; ;;; This file is part of GNU Guix. ;;; @@ -140,9 +141,25 @@ (define-public elfutils "tests/run-varlocs.sh") (("^#!.*" all) (string-append all "exit 77;\n")))))) - '())))) + '()) + ,@(if + (%current-target-system) + '((add-after 'unpack 'patch + (lambda* (#:key native-inputs #:allow-other-keys) + (invoke "patch" "-Np1" "-i" + (assoc-ref native-inputs + "elfutils-libdwfl-Rewrite-reading-of-ar\ +_size-in-elf_begin_rand.patch"))))) + '())))) - (native-inputs (list m4)) + (native-inputs + (if + (%current-target-system) + `(("m4" ,m4) + ("elfutils-libdwfl-Rewrite-reading-of-ar_size-in-elf_begin_rand.patch" + ,(search-patch + "elfutils-libdwfl-Rewrite-reading-of-ar_size-in-elf_begin_rand.patch"))) + (list m4))) (inputs (list xz zlib)) (home-page "https://sourceware.org/elfutils/") (synopsis "Collection of utilities and libraries to handle ELF files and diff --git a/gnu/packages/patches/elfutils-libdwfl-Rewrite-reading-of-ar_size-in-elf_begin_rand.patch b/gnu/packages/patches/elfutils-libdwfl-Rewrite-reading-of-ar_size-in-elf_begin_rand.patch new file mode 100644 index 00000000000..e9d51359c4b --- /dev/null +++ b/gnu/packages/patches/elfutils-libdwfl-Rewrite-reading-of-ar_size-in-elf_begin_rand.patch @@ -0,0 +1,116 @@ +from https://sourceware.org/cgit/elfutils/patch/?id=0873ae782d14e672e8344775e76b7fca0a8b41bf + +Adjust the changelog so it can be applied on elfutils 0.187. + +From 0873ae782d14e672e8344775e76b7fca0a8b41bf Mon Sep 17 00:00:00 2001 +From: Mark Wielaard +Date: Thu, 28 Jul 2022 15:31:12 +0200 +Subject: libdwfl: Rewrite reading of ar_size in elf_begin_rand +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +With GCC 12.1.1, glibc 2.35, -fsanitize=undefined and +-D_FORTIFY_SOURCE=3 we get the following error message: + +In file included from /usr/include/ar.h:22, + from ../libelf/libelfP.h:33, + from core-file.c:31: +In function ‘pread’, + inlined from ‘pread_retry’ at ../lib/system.h:188:21, + inlined from ‘elf_begin_rand’ at core-file.c:86:16, + inlined from ‘core_file_read_eagerly’ at core-file.c:205:15: +/usr/include/bits/unistd.h:74:10: error: ‘__pread_alias’ writing 58 or more bytes into a region of size 10 overflows the destination [-Werror=stringop-overflow=] + 74 | return __glibc_fortify (pread, __nbytes, sizeof (char), + | ^~~~~~~~~~~~~~~ +/usr/include/ar.h: In function ‘core_file_read_eagerly’: +/usr/include/ar.h:41:10: note: destination object ‘ar_size’ of size 10 + 41 | char ar_size[10]; /* File size, in ASCII decimal. */ + | ^~~~~~~ +/usr/include/bits/unistd.h:50:16: note: in a call to function ‘__pread_alias’ declared with attribute ‘access (write_only, 2, 3)’ + 50 | extern ssize_t __REDIRECT (__pread_alias, + | ^~~~~~~~~~ +cc1: all warnings being treated as errors + +The warning disappears when dropping either -fsanitize=undefined +or when using -D_FORTIFY_SOURCE=2. It looks like a false positive. +But I haven't figured out how/why it happens. + +The code is a little tricky to proof correct though. The ar_size +field is a not-zero terminated string ASCII decimal, right-padded +with spaces. Which is then converted with strtoll. Relying on the +fact that the struct ar_hdr is zero initialized, so there will be +a zero byte after the ar_size field. + +Rewrite the code to just use a zero byte terminated char array. +Which is much easier to reason about. As a bonus the error disappears. + +Signed-off-by: Mark Wielaard +--- + libdwfl/ChangeLog | 5 +++++ + libdwfl/core-file.c | 26 ++++++++++++++++---------- + 2 files changed, 21 insertions(+), 10 deletions(-) + +diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog +index 75c53948d..acdaa0138 100644 +--- a/libdwfl/ChangeLog ++++ b/libdwfl/ChangeLog +@@ -1,0 +1,5 @@ ++2022-07-28 Mark Wielaard ++ ++ * core-file.c (elf_begin_rand): Replace struct ar_hdr h with ++ a char ar_size[AR_SIZE_CHARS + 1] array to read size. ++ +2022-04-22 Mark Wielaard + + * debuginfod-client.c (init_control): New static pthread_once_t. +diff --git a/libdwfl/core-file.c b/libdwfl/core-file.c +index cefc3db0f..4418ef338 100644 +--- a/libdwfl/core-file.c ++++ b/libdwfl/core-file.c +@@ -75,26 +75,32 @@ elf_begin_rand (Elf *parent, off_t offset, off_t size, off_t *next) + from the archive header to override SIZE. */ + if (parent->kind == ELF_K_AR) + { +- struct ar_hdr h = { .ar_size = "" }; +- +- if (unlikely (parent->maximum_size - offset < sizeof h)) ++ /* File size, in ASCII decimal, right-padded with ASCII spaces. ++ Max 10 characters. Not zero terminated. So make this ar_size ++ array one larger and explicitly zero terminate it. As needed ++ for strtoll. */ ++ #define AR_SIZE_CHARS 10 ++ char ar_size[AR_SIZE_CHARS + 1]; ++ ar_size[AR_SIZE_CHARS] = '\0'; ++ ++ if (unlikely (parent->maximum_size - offset < sizeof (struct ar_hdr))) + return fail (ELF_E_RANGE); + + if (parent->map_address != NULL) +- memcpy (h.ar_size, parent->map_address + parent->start_offset + offset, +- sizeof h.ar_size); ++ memcpy (ar_size, parent->map_address + parent->start_offset + offset, ++ AR_SIZE_CHARS); + else if (unlikely (pread_retry (parent->fildes, +- h.ar_size, sizeof (h.ar_size), ++ ar_size, AR_SIZE_CHARS, + parent->start_offset + offset + + offsetof (struct ar_hdr, ar_size)) +- != sizeof (h.ar_size))) ++ != AR_SIZE_CHARS)) + return fail (ELF_E_READ_ERROR); + +- offset += sizeof h; ++ offset += sizeof (struct ar_hdr); + + char *endp; +- size = strtoll (h.ar_size, &endp, 10); +- if (unlikely (endp == h.ar_size) ++ size = strtoll (ar_size, &endp, 10); ++ if (unlikely (endp == ar_size) + || unlikely ((off_t) parent->maximum_size - offset < size)) + return fail (ELF_E_INVALID_ARCHIVE); + } +-- +cgit + base-commit: 58a134224e327cfc0af105314598d080dfc0392d -- 2.46.0 From debbugs-submit-bounces@debbugs.gnu.org Wed Dec 04 12:04:48 2024 Received: (at 74669) by debbugs.gnu.org; 4 Dec 2024 17:04:48 +0000 Received: from localhost ([127.0.0.1]:36577 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tIsnt-0003um-0m for submit@debbugs.gnu.org; Wed, 04 Dec 2024 12:04:48 -0500 Received: from smtp81.cstnet.cn ([159.226.251.81]:38224 helo=cstnet.cn) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tIsnp-0003uT-7L for 74669@debbugs.gnu.org; Wed, 04 Dec 2024 12:04:43 -0500 Received: from m.tailaa68d.ts.net (unknown [107.174.64.25]) by APP-03 (Coremail) with SMTP id rQCowACXduyYi1Bndk+9Bw--.31525S2; Thu, 05 Dec 2024 01:04:27 +0800 (CST) From: Zheng Junjie To: 74669@debbugs.gnu.org Subject: [PATCH v2] gnu: elfutils: Fix cross-compilation. Date: Thu, 5 Dec 2024 01:04:21 +0800 Message-ID: X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CM-TRANSID: rQCowACXduyYi1Bndk+9Bw--.31525S2 X-Coremail-Antispam: 1UD129KBjvJXoW3XFyUJryxCF4DCr4rtF4Utwb_yoW3uFyDpF WayFs5JayrJrs7Zrs3JF18Kw1rZws7JFy5X34fG3W0vF4Ygwn5GrWrtryYkFW7Crs7G343 Xrs8tFykuas8AaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUyIb7Iv0xC_Kw4lb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Jr0_Gr1l84ACjcxK6I8E87Iv67AKxVW8JVWxJwA2z4x0Y4vEx4 A2jsIEc7CjxVAFwI0_Gr0_Gr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI 64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1Y6r17McIj6I8E87Iv67AKxVWUJVW8Jw Am72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64vIr41l4I8I 3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxV WUGVWUWwC2zVAF1VAY17CE14v26r1j6r15MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAF wI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE42xK8VAvwI8IcI k0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j 6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUgVHqDUUUU X-Originating-IP: [107.174.64.25] X-CM-SenderInfo: x2kh0wxmxqyx3h6l2u1dvotugofq/ X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 74669 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) * gnu/packages/elf.scm (elfutils): Fix cross-compilation [arguments]: When cross-compilation, apply patch. [native-inputs]: When cross-compilation, add patch. * gnu/packages/patches/elfutils-libdwfl-Rewrite-reading-of-ar_size-in-elf_begin_rand.patch: New file. * gnu/local.mk (dist_patch_DATA): Register it. Change-Id: Ia1ab9c09124a140608bda34bf62f0ae80eb44f9b --- gnu/local.mk | 1 + gnu/packages/elf.scm | 21 +++- ...reading-of-ar_size-in-elf_begin_rand.patch | 116 ++++++++++++++++++ 3 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 gnu/packages/patches/elfutils-libdwfl-Rewrite-reading-of-ar_size-in-elf_begin_rand.patch diff --git a/gnu/local.mk b/gnu/local.mk index a7bd32453b0..eee768615ea 100644 --- a/gnu/local.mk +++ b/gnu/local.mk @@ -1177,6 +1177,7 @@ dist_patch_DATA = \ %D%/packages/patches/eigen-fix-strict-aliasing-bug.patch \ %D%/packages/patches/einstein-build.patch \ %D%/packages/patches/elfutils-tests-ptrace.patch \ + %D%/packages/patches/elfutils-libdwfl-Rewrite-reading-of-ar_size-in-elf_begin_rand.patch \ %D%/packages/patches/elixir-path-length.patch \ %D%/packages/patches/elm-ghc9.2.patch \ %D%/packages/patches/elm-offline-package-registry.patch \ diff --git a/gnu/packages/elf.scm b/gnu/packages/elf.scm index 81a753a4835..4fae3929ba2 100644 --- a/gnu/packages/elf.scm +++ b/gnu/packages/elf.scm @@ -11,6 +11,7 @@ ;;; Copyright © 2021 Leo Le Bouter ;;; Copyright © 2021 Maxime Devos ;;; Copyright © 2023, 2024 Janneke Nieuwenhuizen +;;; Copyright © 2024 Zheng Junjie <873216071@qq.com> ;;; ;;; This file is part of GNU Guix. ;;; @@ -140,9 +141,25 @@ (define-public elfutils "tests/run-varlocs.sh") (("^#!.*" all) (string-append all "exit 77;\n")))))) - '())))) + '()) + ,@(if + (%current-target-system) + '((add-after 'unpack 'patch + (lambda* (#:key native-inputs #:allow-other-keys) + (invoke "patch" "-Np1" "-i" + (assoc-ref native-inputs + "elfutils-libdwfl-Rewrite-reading-of-ar\ +_size-in-elf_begin_rand.patch"))))) + '())))) - (native-inputs (list m4)) + (native-inputs + (if + (%current-target-system) + `(("m4" ,m4) + ("elfutils-libdwfl-Rewrite-reading-of-ar_size-in-elf_begin_rand.patch" + ,(search-patch + "elfutils-libdwfl-Rewrite-reading-of-ar_size-in-elf_begin_rand.patch"))) + (list m4))) (inputs (list xz zlib)) (home-page "https://sourceware.org/elfutils/") (synopsis "Collection of utilities and libraries to handle ELF files and diff --git a/gnu/packages/patches/elfutils-libdwfl-Rewrite-reading-of-ar_size-in-elf_begin_rand.patch b/gnu/packages/patches/elfutils-libdwfl-Rewrite-reading-of-ar_size-in-elf_begin_rand.patch new file mode 100644 index 00000000000..e9d51359c4b --- /dev/null +++ b/gnu/packages/patches/elfutils-libdwfl-Rewrite-reading-of-ar_size-in-elf_begin_rand.patch @@ -0,0 +1,116 @@ +from https://sourceware.org/cgit/elfutils/patch/?id=0873ae782d14e672e8344775e76b7fca0a8b41bf + +Adjust the changelog so it can be applied on elfutils 0.187. + +From 0873ae782d14e672e8344775e76b7fca0a8b41bf Mon Sep 17 00:00:00 2001 +From: Mark Wielaard +Date: Thu, 28 Jul 2022 15:31:12 +0200 +Subject: libdwfl: Rewrite reading of ar_size in elf_begin_rand +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +With GCC 12.1.1, glibc 2.35, -fsanitize=undefined and +-D_FORTIFY_SOURCE=3 we get the following error message: + +In file included from /usr/include/ar.h:22, + from ../libelf/libelfP.h:33, + from core-file.c:31: +In function ‘pread’, + inlined from ‘pread_retry’ at ../lib/system.h:188:21, + inlined from ‘elf_begin_rand’ at core-file.c:86:16, + inlined from ‘core_file_read_eagerly’ at core-file.c:205:15: +/usr/include/bits/unistd.h:74:10: error: ‘__pread_alias’ writing 58 or more bytes into a region of size 10 overflows the destination [-Werror=stringop-overflow=] + 74 | return __glibc_fortify (pread, __nbytes, sizeof (char), + | ^~~~~~~~~~~~~~~ +/usr/include/ar.h: In function ‘core_file_read_eagerly’: +/usr/include/ar.h:41:10: note: destination object ‘ar_size’ of size 10 + 41 | char ar_size[10]; /* File size, in ASCII decimal. */ + | ^~~~~~~ +/usr/include/bits/unistd.h:50:16: note: in a call to function ‘__pread_alias’ declared with attribute ‘access (write_only, 2, 3)’ + 50 | extern ssize_t __REDIRECT (__pread_alias, + | ^~~~~~~~~~ +cc1: all warnings being treated as errors + +The warning disappears when dropping either -fsanitize=undefined +or when using -D_FORTIFY_SOURCE=2. It looks like a false positive. +But I haven't figured out how/why it happens. + +The code is a little tricky to proof correct though. The ar_size +field is a not-zero terminated string ASCII decimal, right-padded +with spaces. Which is then converted with strtoll. Relying on the +fact that the struct ar_hdr is zero initialized, so there will be +a zero byte after the ar_size field. + +Rewrite the code to just use a zero byte terminated char array. +Which is much easier to reason about. As a bonus the error disappears. + +Signed-off-by: Mark Wielaard +--- + libdwfl/ChangeLog | 5 +++++ + libdwfl/core-file.c | 26 ++++++++++++++++---------- + 2 files changed, 21 insertions(+), 10 deletions(-) + +diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog +index 75c53948d..acdaa0138 100644 +--- a/libdwfl/ChangeLog ++++ b/libdwfl/ChangeLog +@@ -1,0 +1,5 @@ ++2022-07-28 Mark Wielaard ++ ++ * core-file.c (elf_begin_rand): Replace struct ar_hdr h with ++ a char ar_size[AR_SIZE_CHARS + 1] array to read size. ++ +2022-04-22 Mark Wielaard + + * debuginfod-client.c (init_control): New static pthread_once_t. +diff --git a/libdwfl/core-file.c b/libdwfl/core-file.c +index cefc3db0f..4418ef338 100644 +--- a/libdwfl/core-file.c ++++ b/libdwfl/core-file.c +@@ -75,26 +75,32 @@ elf_begin_rand (Elf *parent, off_t offset, off_t size, off_t *next) + from the archive header to override SIZE. */ + if (parent->kind == ELF_K_AR) + { +- struct ar_hdr h = { .ar_size = "" }; +- +- if (unlikely (parent->maximum_size - offset < sizeof h)) ++ /* File size, in ASCII decimal, right-padded with ASCII spaces. ++ Max 10 characters. Not zero terminated. So make this ar_size ++ array one larger and explicitly zero terminate it. As needed ++ for strtoll. */ ++ #define AR_SIZE_CHARS 10 ++ char ar_size[AR_SIZE_CHARS + 1]; ++ ar_size[AR_SIZE_CHARS] = '\0'; ++ ++ if (unlikely (parent->maximum_size - offset < sizeof (struct ar_hdr))) + return fail (ELF_E_RANGE); + + if (parent->map_address != NULL) +- memcpy (h.ar_size, parent->map_address + parent->start_offset + offset, +- sizeof h.ar_size); ++ memcpy (ar_size, parent->map_address + parent->start_offset + offset, ++ AR_SIZE_CHARS); + else if (unlikely (pread_retry (parent->fildes, +- h.ar_size, sizeof (h.ar_size), ++ ar_size, AR_SIZE_CHARS, + parent->start_offset + offset + + offsetof (struct ar_hdr, ar_size)) +- != sizeof (h.ar_size))) ++ != AR_SIZE_CHARS)) + return fail (ELF_E_READ_ERROR); + +- offset += sizeof h; ++ offset += sizeof (struct ar_hdr); + + char *endp; +- size = strtoll (h.ar_size, &endp, 10); +- if (unlikely (endp == h.ar_size) ++ size = strtoll (ar_size, &endp, 10); ++ if (unlikely (endp == ar_size) + || unlikely ((off_t) parent->maximum_size - offset < size)) + return fail (ELF_E_INVALID_ARCHIVE); + } +-- +cgit + base-commit: e00ca95e08bc1cc2cb39f3178485ef16defce0be prerequisite-patch-id: a1adabf857d746a919597ce5a4f50a391115748f prerequisite-patch-id: bec1beee57f8cddaf583960ffb67c9badd172de7 prerequisite-patch-id: dcf9a53f458adf55547ef075c83a881c238d476e -- 2.46.0 From debbugs-submit-bounces@debbugs.gnu.org Thu Dec 12 17:03:29 2024 Received: (at 74669) by debbugs.gnu.org; 12 Dec 2024 22:03:29 +0000 Received: from localhost ([127.0.0.1]:40713 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tLrHM-0003pE-Rc for submit@debbugs.gnu.org; Thu, 12 Dec 2024 17:03:29 -0500 Received: from eggs.gnu.org ([209.51.188.92]:54404) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tLrHH-0003oj-G9 for 74669@debbugs.gnu.org; Thu, 12 Dec 2024 17:03:27 -0500 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tLrH9-0004BI-Tu; Thu, 12 Dec 2024 17:03:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:Date:References:In-Reply-To:Subject:To: From; bh=1UWTnsE5sdhqTymS5LRz0Rzc39oxeK9CzgeQxJSNp5s=; b=e7Q3u0VsySUhLmRSPFH+ yHjZg/gzh+o5+oK6vMRwpIFGEbjqwFf0nuUkiOGUdWU5fkVpzPdB/ceG0mIh85p9nq17j8BJF7Ag9 /VgDsHqDHK8ojeUmJWWfVqE7CqalPa0UmHrVNojNNZhIQsdYTp2r0an09hJ/arezs4mpg4jJov03+ cy1aVRIgyVCxlwIb5k5C1qMVmJjSsyxi4SmHoUxU/ZgYT3IWjdXFmmhElv6P9Rov39Y8UikBKGwIk 3pnjXjeX8nlQg7QmEcdyVLfumf85UyCEjp24kewqWj5ejuWebKPZlhDhjcDxfiqX1q1IR2BJ4tmFQ trbl/aPB44yVgw==; From: =?utf-8?Q?Ludovic_Court=C3=A8s?= To: Zheng Junjie Subject: Re: [bug#74669] [PATCH v2] gnu: elfutils: Fix cross-compilation. In-Reply-To: (Zheng Junjie's message of "Thu, 5 Dec 2024 01:04:21 +0800") References: <814be590b438138df7b35507a0fd266b319da388.1733228331.git.zhengjunjie@iscas.ac.cn> Date: Thu, 12 Dec 2024 23:03:13 +0100 Message-ID: <87ikro5zku.fsf@gnu.org> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 74669 Cc: 74669@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) Hi, Zheng Junjie skribis: > * gnu/packages/elf.scm (elfutils): Fix cross-compilation > [arguments]: When cross-compilation, apply patch. > [native-inputs]: When cross-compilation, add patch. > * gnu/packages/patches/elfutils-libdwfl-Rewrite-reading-of-ar_size-in-elf= _begin_rand.patch: > New file. > * gnu/local.mk (dist_patch_DATA): Register it. > > Change-Id: Ia1ab9c09124a140608bda34bf62f0ae80eb44f9b [...] > + %D%/packages/patches/elfutils-libdwfl-Rewrite-reading-of-ar_size-in-el= f_begin_rand.patch \ Please pick a shorter file name, as noted by =E2=80=98guix lint=E2=80=99. (Like =E2=80=98elfutils-libdwfl-string-overflow.patch=E2=80=99.) > + '((add-after 'unpack 'patch > + (lambda* (#:key native-inputs #:allow-other-keys) > + (invoke "patch" "-Np1" "-i" > + (assoc-ref native-inputs > + "elfutils-libdwfl-Rewrite-reading-o= f-ar\ > +_size-in-elf_begin_rand.patch"))))) Could you use a gexp instead? And =E2=80=98--force=E2=80=99 is better suit= ed than =E2=80=98-N=E2=80=99 I think. #~(add-after =E2=80=A6 =E2=80=A6 (invoke "patch" "-p1" "--force" "-i" #$(local-file (search-patch =E2= =80=A6)))) Thanks, Ludo=E2=80=99. From debbugs-submit-bounces@debbugs.gnu.org Fri Dec 13 08:06:28 2024 Received: (at 74669) by debbugs.gnu.org; 13 Dec 2024 13:06:28 +0000 Received: from localhost ([127.0.0.1]:42056 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tM5NC-0005Vn-KW for submit@debbugs.gnu.org; Fri, 13 Dec 2024 08:06:28 -0500 Received: from smtp21.cstnet.cn ([159.226.251.21]:56624 helo=cstnet.cn) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tM5N5-0005VM-TF for 74669@debbugs.gnu.org; Fri, 13 Dec 2024 08:06:24 -0500 Received: from m.tailaa68d.ts.net (unknown [107.174.64.25]) by APP-01 (Coremail) with SMTP id qwCowAAXHNA2MVxnfSeTAQ--.8797S2; Fri, 13 Dec 2024 21:06:02 +0800 (CST) From: Zheng Junjie To: 74669@debbugs.gnu.org Subject: [PATCH v3] gnu: elfutils: Fix cross-compilation. Date: Fri, 13 Dec 2024 21:05:53 +0800 Message-ID: <1f088473ff01e785e8a8b3414f831dc6b1fe6841.1734095153.git.zhengjunjie@iscas.ac.cn> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CM-TRANSID: qwCowAAXHNA2MVxnfSeTAQ--.8797S2 X-Coremail-Antispam: 1UD129KBjvJXoW3uFyUWF18GFyrtr15Wr18AFb_yoWDAr4rpa y3Ar1Yq3yfJFs3Jan3ZF1Ikw1rG3s7AFWY9ry3G3W0yF4YqF13JrWftryrCayUWryku3yr uwsYqa4fuF95WaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUyIb7Iv0xC_Kw4lb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVWxJr0_GcWl84ACjcxK6I 8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI 64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1Y6r17McIj6I8E87Iv67AKxVWUJVW8Jw Am72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64vIr41l4I8I 3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxV WUGVWUWwC2zVAF1VAY17CE14v26r1j6r15MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAF wI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE42xK8VAvwI8IcI k0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j 6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUgVHqDUUUU X-Originating-IP: [107.174.64.25] X-CM-SenderInfo: x2kh0wxmxqyx3h6l2u1dvotugofq/ X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 74669 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) * gnu/packages/elf.scm (elfutils): Fix cross-compilation [arguments]: When cross-compilation, apply patch. <#:phases>: Use G-expressions. * gnu/packages/patches/elfutils-libdwfl-string-overflow.patch: New file. * gnu/local.mk (dist_patch_DATA): Register it. Change-Id: Ia1ab9c09124a140608bda34bf62f0ae80eb44f9b --- gnu/local.mk | 1 + gnu/packages/elf.scm | 94 +++++++------- .../elfutils-libdwfl-string-overflow.patch | 116 ++++++++++++++++++ 3 files changed, 169 insertions(+), 42 deletions(-) create mode 100644 gnu/packages/patches/elfutils-libdwfl-string-overflow.patch diff --git a/gnu/local.mk b/gnu/local.mk index 39b142af5a..67d0085b71 100644 --- a/gnu/local.mk +++ b/gnu/local.mk @@ -1177,6 +1177,7 @@ dist_patch_DATA = \ %D%/packages/patches/eigen-fix-strict-aliasing-bug.patch \ %D%/packages/patches/einstein-build.patch \ %D%/packages/patches/elfutils-tests-ptrace.patch \ + %D%/packages/patches/elfutils-libdwfl-string-overflow.patch \ %D%/packages/patches/elixir-path-length.patch \ %D%/packages/patches/elm-ghc9.2.patch \ %D%/packages/patches/elm-offline-package-registry.patch \ diff --git a/gnu/packages/elf.scm b/gnu/packages/elf.scm index 81a753a483..8f036ab221 100644 --- a/gnu/packages/elf.scm +++ b/gnu/packages/elf.scm @@ -11,6 +11,7 @@ ;;; Copyright © 2021 Leo Le Bouter ;;; Copyright © 2021 Maxime Devos ;;; Copyright © 2023, 2024 Janneke Nieuwenhuizen +;;; Copyright © 2024 Zheng Junjie <873216071@qq.com> ;;; ;;; This file is part of GNU Guix. ;;; @@ -99,48 +100,57 @@ (define-public elfutils '()) #:phases - (modify-phases %standard-phases - ;; No reason has been found for this test to reliably fail on aarch64-linux. - (add-after 'unpack 'disable-failing-aarch64-tests - (lambda _ - (substitute* "tests/Makefile.in" - (("run-backtrace-native.sh") "")) - #t)) - ,@(if (target-riscv64?) - `((add-after 'unpack 'disable-failing-riscv64-test - (lambda _ - (substitute* "tests/Makefile.in" - ;; dwfl_thread_getframes: No DWARF information found - (("run-backtrace-dwarf.sh") "") - ;; These tests have several errors: - ;; unknown program header entry type 0x70000003 - ;; '.riscv.attributes' has unsupported type 1879048195 - (("run-reverse-sections-self.sh") "") - (("run-strip-strmerge.sh") "") - (("run-elflint-self.sh") ""))))) - '()) - ,@(if (system-hurd?) - `((add-after 'unpack 'skip-tests - (lambda _ - (substitute* '("tests/elfstrtab.c" - "tests/emptyfile.c") - (("elf_version \\(EV_CURRENT\\);" all) - "exit (77);")) - (substitute* '("tests/run-all-dwarf-ranges.sh" - "tests/run-allfcts-multi.sh" - "tests/run-attr-integrate-skel.sh" - "tests/run-bug1-test.sh" - "tests/run-copyadd-sections.sh" - "tests/run-deleted.sh" - "tests/run-get-units-split.sh" - "tests/run-native-test.sh" - "tests/run-readelf-loc.sh" - "tests/run-readelf-ranges.sh" - "tests/run-unit-info.sh" - "tests/run-varlocs.sh") - (("^#!.*" all) - (string-append all "exit 77;\n")))))) - '())))) + ,#~(modify-phases %standard-phases + ;; No reason has been found for this test to reliably fail on aarch64-linux. + (add-after 'unpack 'disable-failing-aarch64-tests + (lambda _ + (substitute* "tests/Makefile.in" + (("run-backtrace-native.sh") "")) + #t)) + #$@(if (target-riscv64?) + #~((add-after 'unpack 'disable-failing-riscv64-test + (lambda _ + (substitute* "tests/Makefile.in" + ;; dwfl_thread_getframes: No DWARF information found + (("run-backtrace-dwarf.sh") "") + ;; These tests have several errors: + ;; unknown program header entry type 0x70000003 + ;; '.riscv.attributes' has unsupported type 1879048195 + (("run-reverse-sections-self.sh") "") + (("run-strip-strmerge.sh") "") + (("run-elflint-self.sh") ""))))) + #~()) + #$@(if (system-hurd?) + #~((add-after 'unpack 'skip-tests + (lambda _ + (substitute* '("tests/elfstrtab.c" + "tests/emptyfile.c") + (("elf_version \\(EV_CURRENT\\);" all) + "exit (77);")) + (substitute* '("tests/run-all-dwarf-ranges.sh" + "tests/run-allfcts-multi.sh" + "tests/run-attr-integrate-skel.sh" + "tests/run-bug1-test.sh" + "tests/run-copyadd-sections.sh" + "tests/run-deleted.sh" + "tests/run-get-units-split.sh" + "tests/run-native-test.sh" + "tests/run-readelf-loc.sh" + "tests/run-readelf-ranges.sh" + "tests/run-unit-info.sh" + "tests/run-varlocs.sh") + (("^#!.*" all) + (string-append all "exit 77;\n")))))) + #~()) + #$@(if (%current-target-system) + #~((add-after 'unpack 'patch + (lambda* (#:key native-inputs #:allow-other-keys) + (invoke + "patch" "-p1" "--force" "-i" + #$(local-file + (search-patch + "elfutils-libdwfl-string-overflow.patch")))))) + #~())))) (native-inputs (list m4)) (inputs (list xz zlib)) diff --git a/gnu/packages/patches/elfutils-libdwfl-string-overflow.patch b/gnu/packages/patches/elfutils-libdwfl-string-overflow.patch new file mode 100644 index 0000000000..e9d51359c4 --- /dev/null +++ b/gnu/packages/patches/elfutils-libdwfl-string-overflow.patch @@ -0,0 +1,116 @@ +from https://sourceware.org/cgit/elfutils/patch/?id=0873ae782d14e672e8344775e76b7fca0a8b41bf + +Adjust the changelog so it can be applied on elfutils 0.187. + +From 0873ae782d14e672e8344775e76b7fca0a8b41bf Mon Sep 17 00:00:00 2001 +From: Mark Wielaard +Date: Thu, 28 Jul 2022 15:31:12 +0200 +Subject: libdwfl: Rewrite reading of ar_size in elf_begin_rand +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +With GCC 12.1.1, glibc 2.35, -fsanitize=undefined and +-D_FORTIFY_SOURCE=3 we get the following error message: + +In file included from /usr/include/ar.h:22, + from ../libelf/libelfP.h:33, + from core-file.c:31: +In function ‘pread’, + inlined from ‘pread_retry’ at ../lib/system.h:188:21, + inlined from ‘elf_begin_rand’ at core-file.c:86:16, + inlined from ‘core_file_read_eagerly’ at core-file.c:205:15: +/usr/include/bits/unistd.h:74:10: error: ‘__pread_alias’ writing 58 or more bytes into a region of size 10 overflows the destination [-Werror=stringop-overflow=] + 74 | return __glibc_fortify (pread, __nbytes, sizeof (char), + | ^~~~~~~~~~~~~~~ +/usr/include/ar.h: In function ‘core_file_read_eagerly’: +/usr/include/ar.h:41:10: note: destination object ‘ar_size’ of size 10 + 41 | char ar_size[10]; /* File size, in ASCII decimal. */ + | ^~~~~~~ +/usr/include/bits/unistd.h:50:16: note: in a call to function ‘__pread_alias’ declared with attribute ‘access (write_only, 2, 3)’ + 50 | extern ssize_t __REDIRECT (__pread_alias, + | ^~~~~~~~~~ +cc1: all warnings being treated as errors + +The warning disappears when dropping either -fsanitize=undefined +or when using -D_FORTIFY_SOURCE=2. It looks like a false positive. +But I haven't figured out how/why it happens. + +The code is a little tricky to proof correct though. The ar_size +field is a not-zero terminated string ASCII decimal, right-padded +with spaces. Which is then converted with strtoll. Relying on the +fact that the struct ar_hdr is zero initialized, so there will be +a zero byte after the ar_size field. + +Rewrite the code to just use a zero byte terminated char array. +Which is much easier to reason about. As a bonus the error disappears. + +Signed-off-by: Mark Wielaard +--- + libdwfl/ChangeLog | 5 +++++ + libdwfl/core-file.c | 26 ++++++++++++++++---------- + 2 files changed, 21 insertions(+), 10 deletions(-) + +diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog +index 75c53948d..acdaa0138 100644 +--- a/libdwfl/ChangeLog ++++ b/libdwfl/ChangeLog +@@ -1,0 +1,5 @@ ++2022-07-28 Mark Wielaard ++ ++ * core-file.c (elf_begin_rand): Replace struct ar_hdr h with ++ a char ar_size[AR_SIZE_CHARS + 1] array to read size. ++ +2022-04-22 Mark Wielaard + + * debuginfod-client.c (init_control): New static pthread_once_t. +diff --git a/libdwfl/core-file.c b/libdwfl/core-file.c +index cefc3db0f..4418ef338 100644 +--- a/libdwfl/core-file.c ++++ b/libdwfl/core-file.c +@@ -75,26 +75,32 @@ elf_begin_rand (Elf *parent, off_t offset, off_t size, off_t *next) + from the archive header to override SIZE. */ + if (parent->kind == ELF_K_AR) + { +- struct ar_hdr h = { .ar_size = "" }; +- +- if (unlikely (parent->maximum_size - offset < sizeof h)) ++ /* File size, in ASCII decimal, right-padded with ASCII spaces. ++ Max 10 characters. Not zero terminated. So make this ar_size ++ array one larger and explicitly zero terminate it. As needed ++ for strtoll. */ ++ #define AR_SIZE_CHARS 10 ++ char ar_size[AR_SIZE_CHARS + 1]; ++ ar_size[AR_SIZE_CHARS] = '\0'; ++ ++ if (unlikely (parent->maximum_size - offset < sizeof (struct ar_hdr))) + return fail (ELF_E_RANGE); + + if (parent->map_address != NULL) +- memcpy (h.ar_size, parent->map_address + parent->start_offset + offset, +- sizeof h.ar_size); ++ memcpy (ar_size, parent->map_address + parent->start_offset + offset, ++ AR_SIZE_CHARS); + else if (unlikely (pread_retry (parent->fildes, +- h.ar_size, sizeof (h.ar_size), ++ ar_size, AR_SIZE_CHARS, + parent->start_offset + offset + + offsetof (struct ar_hdr, ar_size)) +- != sizeof (h.ar_size))) ++ != AR_SIZE_CHARS)) + return fail (ELF_E_READ_ERROR); + +- offset += sizeof h; ++ offset += sizeof (struct ar_hdr); + + char *endp; +- size = strtoll (h.ar_size, &endp, 10); +- if (unlikely (endp == h.ar_size) ++ size = strtoll (ar_size, &endp, 10); ++ if (unlikely (endp == ar_size) + || unlikely ((off_t) parent->maximum_size - offset < size)) + return fail (ELF_E_INVALID_ARCHIVE); + } +-- +cgit + base-commit: 0787a180b3ca64d3958438920ca1b58a2a27fba4 -- 2.46.0 From debbugs-submit-bounces@debbugs.gnu.org Mon Dec 23 11:48:06 2024 Received: (at 74669-done) by debbugs.gnu.org; 23 Dec 2024 16:48:06 +0000 Received: from localhost ([127.0.0.1]:55798 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tPlbB-0006mH-5u for submit@debbugs.gnu.org; Mon, 23 Dec 2024 11:48:06 -0500 Received: from smtp81.cstnet.cn ([159.226.251.81]:57494 helo=cstnet.cn) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tPlb5-0006lb-Am for 74669-done@debbugs.gnu.org; Mon, 23 Dec 2024 11:48:03 -0500 Received: from m (unknown [107.174.64.25]) by APP-03 (Coremail) with SMTP id rQCowADX3g4olGln5N+YAw--.10735S2; Tue, 24 Dec 2024 00:47:40 +0800 (CST) From: Zheng Junjie To: 74669-done@debbugs.gnu.org Subject: Re: [bug#74669] [PATCH v3] gnu: elfutils: Fix cross-compilation. In-Reply-To: <1f088473ff01e785e8a8b3414f831dc6b1fe6841.1734095153.git.zhengjunjie@iscas.ac.cn> (Zheng Junjie's message of "Fri, 13 Dec 2024 21:05:53 +0800") References: <814be590b438138df7b35507a0fd266b319da388.1733228331.git.zhengjunjie@iscas.ac.cn> <1f088473ff01e785e8a8b3414f831dc6b1fe6841.1734095153.git.zhengjunjie@iscas.ac.cn> User-Agent: mu4e 1.12.7; emacs 30.0.92 Date: Tue, 24 Dec 2024 00:47:34 +0800 Message-ID: <87cyhipcrd.fsf@iscas.ac.cn> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" X-CM-TRANSID: rQCowADX3g4olGln5N+YAw--.10735S2 X-Coremail-Antispam: 1UD129KBjvJXoWfGw1xWFy7Zw4fGrW3tr43ZFb_yoWDuryxp3 y3ArnYv3yxJrnxJanxXFnFkwn5G3s7CFWY9r9xGw1vyF45XF1xXrWxKryrCayUuryku3ya 9wsYqayfAF98WFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvlb7Iv0xC_Kw4lb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I 8E87Iv6xkF7I0E14v26r4j6r4UJwAa7VASzI0EjI02j7AqF2xKxVCjxxvEa2IrM2AIxVAI cxkEcVAq07x20xvEncxIr21le4C267I2x7xF54xIwI1l5I8CrVACY4xI64kE6c02F40Ex7 xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Y z7v_Jr0_Gr1lF7xvr2IY64vIr41l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr 0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY 17CE14v26r1j6r15MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcV C0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY 6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r1j6r4UYxBIdaVFxhVjvj DU0xZFpf9x07jEOJOUUUUU= X-Originating-IP: [107.174.64.25] X-CM-SenderInfo: x2kh0wxmxqyx3h6l2u1dvotugofq/ X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 74669-done X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Zheng Junjie writes: > * gnu/packages/elf.scm (elfutils): Fix cross-compilation > [arguments]: When cross-compilation, apply patch. > <#:phases>: Use G-expressions. > * gnu/packages/patches/elfutils-libdwfl-string-overflow.patch: New file. > * gnu/local.mk (dist_patch_DATA): Register it. > > Change-Id: Ia1ab9c09124a140608bda34bf62f0ae80eb44f9b > --- > gnu/local.mk | 1 + > gnu/packages/elf.scm | 94 +++++++------- > .../elfutils-libdwfl-string-overflow.patch | 116 ++++++++++++++++++ > 3 files changed, 169 insertions(+), 42 deletions(-) > create mode 100644 gnu/packages/patches/elfutils-libdwfl-string-overflow= .patch > > diff --git a/gnu/local.mk b/gnu/local.mk > index 39b142af5a..67d0085b71 100644 > --- a/gnu/local.mk > +++ b/gnu/local.mk > @@ -1177,6 +1177,7 @@ dist_patch_DATA =3D \ > %D%/packages/patches/eigen-fix-strict-aliasing-bug.patch \ > %D%/packages/patches/einstein-build.patch \ > %D%/packages/patches/elfutils-tests-ptrace.patch \ > + %D%/packages/patches/elfutils-libdwfl-string-overflow.patch \ > %D%/packages/patches/elixir-path-length.patch \ > %D%/packages/patches/elm-ghc9.2.patch \ > %D%/packages/patches/elm-offline-package-registry.patch \ > diff --git a/gnu/packages/elf.scm b/gnu/packages/elf.scm > index 81a753a483..8f036ab221 100644 > --- a/gnu/packages/elf.scm > +++ b/gnu/packages/elf.scm > @@ -11,6 +11,7 @@ > ;;; Copyright =C2=A9 2021 Leo Le Bouter > ;;; Copyright =C2=A9 2021 Maxime Devos > ;;; Copyright =C2=A9 2023, 2024 Janneke Nieuwenhuizen > +;;; Copyright =C2=A9 2024 Zheng Junjie <873216071@qq.com> > ;;; > ;;; This file is part of GNU Guix. > ;;; > @@ -99,48 +100,57 @@ (define-public elfutils > '()) >=20=20 > #:phases > - (modify-phases %standard-phases > - ;; No reason has been found for this test to reliably fail on a= arch64-linux. > - (add-after 'unpack 'disable-failing-aarch64-tests > - (lambda _ > - (substitute* "tests/Makefile.in" > - (("run-backtrace-native.sh") "")) > - #t)) > - ,@(if (target-riscv64?) > - `((add-after 'unpack 'disable-failing-riscv64-test > - (lambda _ > - (substitute* "tests/Makefile.in" > - ;; dwfl_thread_getframes: No DWARF information found > - (("run-backtrace-dwarf.sh") "") > - ;; These tests have several errors: > - ;; unknown program header entry type 0x70000003 > - ;; '.riscv.attributes' has unsupported type 1879048= 195 > - (("run-reverse-sections-self.sh") "") > - (("run-strip-strmerge.sh") "") > - (("run-elflint-self.sh") ""))))) > - '()) > - ,@(if (system-hurd?) > - `((add-after 'unpack 'skip-tests > - (lambda _ > - (substitute* '("tests/elfstrtab.c" > - "tests/emptyfile.c") > - (("elf_version \\(EV_CURRENT\\);" all) > - "exit (77);")) > - (substitute* '("tests/run-all-dwarf-ranges.sh" > - "tests/run-allfcts-multi.sh" > - "tests/run-attr-integrate-skel.sh" > - "tests/run-bug1-test.sh" > - "tests/run-copyadd-sections.sh" > - "tests/run-deleted.sh" > - "tests/run-get-units-split.sh" > - "tests/run-native-test.sh" > - "tests/run-readelf-loc.sh" > - "tests/run-readelf-ranges.sh" > - "tests/run-unit-info.sh" > - "tests/run-varlocs.sh") > - (("^#!.*" all) > - (string-append all "exit 77;\n")))))) > - '())))) > + ,#~(modify-phases %standard-phases > + ;; No reason has been found for this test to reliably fail o= n aarch64-linux. > + (add-after 'unpack 'disable-failing-aarch64-tests > + (lambda _ > + (substitute* "tests/Makefile.in" > + (("run-backtrace-native.sh") "")) > + #t)) > + #$@(if (target-riscv64?) > + #~((add-after 'unpack 'disable-failing-riscv64-test > + (lambda _ > + (substitute* "tests/Makefile.in" > + ;; dwfl_thread_getframes: No DWARF informati= on found > + (("run-backtrace-dwarf.sh") "") > + ;; These tests have several errors: > + ;; unknown program header entry type 0x70000= 003 > + ;; '.riscv.attributes' has unsupported type = 1879048195 > + (("run-reverse-sections-self.sh") "") > + (("run-strip-strmerge.sh") "") > + (("run-elflint-self.sh") ""))))) > + #~()) > + #$@(if (system-hurd?) > + #~((add-after 'unpack 'skip-tests > + (lambda _ > + (substitute* '("tests/elfstrtab.c" > + "tests/emptyfile.c") > + (("elf_version \\(EV_CURRENT\\);" all) > + "exit (77);")) > + (substitute* '("tests/run-all-dwarf-ranges.sh" > + "tests/run-allfcts-multi.sh" > + "tests/run-attr-integrate-skel.= sh" > + "tests/run-bug1-test.sh" > + "tests/run-copyadd-sections.sh" > + "tests/run-deleted.sh" > + "tests/run-get-units-split.sh" > + "tests/run-native-test.sh" > + "tests/run-readelf-loc.sh" > + "tests/run-readelf-ranges.sh" > + "tests/run-unit-info.sh" > + "tests/run-varlocs.sh") > + (("^#!.*" all) > + (string-append all "exit 77;\n")))))) > + #~()) > + #$@(if (%current-target-system) > + #~((add-after 'unpack 'patch > + (lambda* (#:key native-inputs #:allow-other-keys) > + (invoke > + "patch" "-p1" "--force" "-i" > + #$(local-file > + (search-patch > + "elfutils-libdwfl-string-overflow.patch")= ))))) > + #~())))) >=20=20 > (native-inputs (list m4)) > (inputs (list xz zlib)) > diff --git a/gnu/packages/patches/elfutils-libdwfl-string-overflow.patch = b/gnu/packages/patches/elfutils-libdwfl-string-overflow.patch > new file mode 100644 > index 0000000000..e9d51359c4 > --- /dev/null > +++ b/gnu/packages/patches/elfutils-libdwfl-string-overflow.patch > @@ -0,0 +1,116 @@ > +from https://sourceware.org/cgit/elfutils/patch/?id=3D0873ae782d14e672e8= 344775e76b7fca0a8b41bf > + > +Adjust the changelog so it can be applied on elfutils 0.187. > + > +From 0873ae782d14e672e8344775e76b7fca0a8b41bf Mon Sep 17 00:00:00 2001 > +From: Mark Wielaard > +Date: Thu, 28 Jul 2022 15:31:12 +0200 > +Subject: libdwfl: Rewrite reading of ar_size in elf_begin_rand > +MIME-Version: 1.0 > +Content-Type: text/plain; charset=3DUTF-8 > +Content-Transfer-Encoding: 8bit > + > +With GCC 12.1.1, glibc 2.35, -fsanitize=3Dundefined and > +-D_FORTIFY_SOURCE=3D3 we get the following error message: > + > +In file included from /usr/include/ar.h:22, > + from ../libelf/libelfP.h:33, > + from core-file.c:31: > +In function =E2=80=98pread=E2=80=99, > + inlined from =E2=80=98pread_retry=E2=80=99 at ../lib/system.h:188:21, > + inlined from =E2=80=98elf_begin_rand=E2=80=99 at core-file.c:86:16, > + inlined from =E2=80=98core_file_read_eagerly=E2=80=99 at core-file.c= :205:15: > +/usr/include/bits/unistd.h:74:10: error: =E2=80=98__pread_alias=E2=80=99= writing 58 or more bytes into a region of size 10 overflows the destinatio= n [-Werror=3Dstringop-overflow=3D] > + 74 | return __glibc_fortify (pread, __nbytes, sizeof (char), > + | ^~~~~~~~~~~~~~~ > +/usr/include/ar.h: In function =E2=80=98core_file_read_eagerly=E2=80=99: > +/usr/include/ar.h:41:10: note: destination object =E2=80=98ar_size=E2=80= =99 of size 10 > + 41 | char ar_size[10]; /* File size, in ASCII decimal. = */ > + | ^~~~~~~ > +/usr/include/bits/unistd.h:50:16: note: in a call to function =E2=80=98_= _pread_alias=E2=80=99 declared with attribute =E2=80=98access (write_only, = 2, 3)=E2=80=99 > + 50 | extern ssize_t __REDIRECT (__pread_alias, > + | ^~~~~~~~~~ > +cc1: all warnings being treated as errors > + > +The warning disappears when dropping either -fsanitize=3Dundefined > +or when using -D_FORTIFY_SOURCE=3D2. It looks like a false positive. > +But I haven't figured out how/why it happens. > + > +The code is a little tricky to proof correct though. The ar_size > +field is a not-zero terminated string ASCII decimal, right-padded > +with spaces. Which is then converted with strtoll. Relying on the > +fact that the struct ar_hdr is zero initialized, so there will be > +a zero byte after the ar_size field. > + > +Rewrite the code to just use a zero byte terminated char array. > +Which is much easier to reason about. As a bonus the error disappears. > + > +Signed-off-by: Mark Wielaard > +--- > + libdwfl/ChangeLog | 5 +++++ > + libdwfl/core-file.c | 26 ++++++++++++++++---------- > + 2 files changed, 21 insertions(+), 10 deletions(-) > + > +diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog > +index 75c53948d..acdaa0138 100644 > +--- a/libdwfl/ChangeLog > ++++ b/libdwfl/ChangeLog > +@@ -1,0 +1,5 @@ > ++2022-07-28 Mark Wielaard > ++ > ++ * core-file.c (elf_begin_rand): Replace struct ar_hdr h with > ++ a char ar_size[AR_SIZE_CHARS + 1] array to read size. > ++ > +2022-04-22 Mark Wielaard > + > + * debuginfod-client.c (init_control): New static pthread_once_t. > +diff --git a/libdwfl/core-file.c b/libdwfl/core-file.c > +index cefc3db0f..4418ef338 100644 > +--- a/libdwfl/core-file.c > ++++ b/libdwfl/core-file.c > +@@ -75,26 +75,32 @@ elf_begin_rand (Elf *parent, off_t offset, off_t siz= e, off_t *next) > + from the archive header to override SIZE. */ > + if (parent->kind =3D=3D ELF_K_AR) > + { > +- struct ar_hdr h =3D { .ar_size =3D "" }; > +- > +- if (unlikely (parent->maximum_size - offset < sizeof h)) > ++ /* File size, in ASCII decimal, right-padded with ASCII spaces. > ++ Max 10 characters. Not zero terminated. So make this ar_size > ++ array one larger and explicitly zero terminate it. As needed > ++ for strtoll. */ > ++ #define AR_SIZE_CHARS 10 > ++ char ar_size[AR_SIZE_CHARS + 1]; > ++ ar_size[AR_SIZE_CHARS] =3D '\0'; > ++ > ++ if (unlikely (parent->maximum_size - offset < sizeof (struct ar_h= dr))) > + return fail (ELF_E_RANGE); > +=20 > + if (parent->map_address !=3D NULL) > +- memcpy (h.ar_size, parent->map_address + parent->start_offset + offset, > +- sizeof h.ar_size); > ++ memcpy (ar_size, parent->map_address + parent->start_offset + offset, > ++ AR_SIZE_CHARS); > + else if (unlikely (pread_retry (parent->fildes, > +- h.ar_size, sizeof (h.ar_size), > ++ ar_size, AR_SIZE_CHARS, > + parent->start_offset + offset > + + offsetof (struct ar_hdr, ar_size)) > +- !=3D sizeof (h.ar_size))) > ++ !=3D AR_SIZE_CHARS)) > + return fail (ELF_E_READ_ERROR); > +=20 > +- offset +=3D sizeof h; > ++ offset +=3D sizeof (struct ar_hdr); > +=20 > + char *endp; > +- size =3D strtoll (h.ar_size, &endp, 10); > +- if (unlikely (endp =3D=3D h.ar_size) > ++ size =3D strtoll (ar_size, &endp, 10); > ++ if (unlikely (endp =3D=3D ar_size) > + || unlikely ((off_t) parent->maximum_size - offset < size)) > + return fail (ELF_E_INVALID_ARCHIVE); > + } > +--=20 > +cgit=20 > + > > base-commit: 0787a180b3ca64d3958438920ca1b58a2a27fba4 pushed, closing. --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEfr6klGDOXiwIdX/bO1qpk+Gi3/AFAmdplCYACgkQO1qpk+Gi 3/CptxAAn60SY4Xg5G48coOZVxawnxO6EpaEt/1HpGKzXtcsBVfZ3T7JloM/qxcw rRvO9vwEFbrXEu5SvAXdEr55vDsfPiWW4wIRNhDrD9N4XWAUbFoYyN8EwtXfQM7x J0Y8cGiwW4HDQ5gZbyRTn0AZ1WHkbXwF+mj+k1KuEvAV/BplQVOOcjAmLVRsCnsI RCFboKLDvjlzRYO30WHRIZBjUTmASkyj4d35U5AU54z9E+GNo82pnIPIdj5s/Q3v lwWJIO+nYgxG6VxakItS5lWma2bcTAe5vl2gXaf8wqJv9cFGSIF0biJxhO4HdMUm 5Z4l+aAvptB7jPHv4ctaEhGtkYhUKR/Xqt0RrwUI9jxu+XEFf/m1ra6TF5s/eEfz BJs4WyHabauriVhMJqMiLZaq3OmAKpjUP04tG4VwtbHwZBHfvrtXXVfYL6A8EK1s hZXRah8fYyUS+hMCB0CDSYdrQbbSNVRApSSV3UTwq1aeh4ZD2V6M/gOlaer8OvYQ GN935uC+ydLJ0yY1l9ahjEE13tfybym2PCN6O4mooB2pA5RoSGGNvIYNb0wWq6Je uLMZT0ClJ4C8d1ZiNTsTdQUWVjNW4HvSY9P9BleaQrBTLpiIcSVTLA1Bjc7lPIvS Ys0VtG2TOX9aooQPRghtq1opD8NxleoVtQnKKBvuTE3mA3bz1zA= =FOxi -----END PGP SIGNATURE----- --=-=-=-- From unknown Fri Jun 20 07:22:35 2025 Received: (at fakecontrol) by fakecontrolmessage; To: internal_control@debbugs.gnu.org From: Debbugs Internal Request Subject: Internal Control Message-Id: bug archived. Date: Tue, 21 Jan 2025 12:24:20 +0000 User-Agent: Fakemail v42.6.9 # This is a fake control message. # # The action: # bug archived. thanks # This fakemail brought to you by your local debbugs # administrator