From patchwork Thu Apr 14 09:10:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Niklas Cassel X-Patchwork-Id: 12813191 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6569BC433F5 for ; Thu, 14 Apr 2022 09:10:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EF39D6B0071; Thu, 14 Apr 2022 05:10:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E7C396B0073; Thu, 14 Apr 2022 05:10:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF5E16B0074; Thu, 14 Apr 2022 05:10:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.a.hostedemail.com [64.99.140.24]) by kanga.kvack.org (Postfix) with ESMTP id BD7426B0071 for ; Thu, 14 Apr 2022 05:10:29 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7F78625A9A for ; Thu, 14 Apr 2022 09:10:29 +0000 (UTC) X-FDA: 79354913778.20.D405947 Received: from esa2.hgst.iphmx.com (esa2.hgst.iphmx.com [68.232.143.124]) by imf13.hostedemail.com (Postfix) with ESMTP id 5F19120009 for ; Thu, 14 Apr 2022 09:10:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1649927428; x=1681463428; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=n/RjFhCoHmyvQPqkferP8/GTynI8Zg21tVioUs2fo6s=; b=ksRGpONNjEPg+No0mnIqP8toewEuRUJyC4zJBHdMWl80aCD+yD5VWfaE v0YCDvFj1HXAmB0KvDxcplU/cJCk1eGLhfoCEikOY+nj118bpQwXYnOIz IHNuakzdQhJVRkzNskW6udCZrdsFe/eiBHSbXFjEKTZ7S35Un5/u/xdG8 PfnbsrPzHxeFFwODjJV/4rzADB39ckjm+kHN5OKqrElIIARAcX9gKRfii sABG3Wk4/HrxD1sU0is7A78rGbjFnGCpTW/memZHqnes+BRuC2tWvPb0K MwobaWl4R/63RErifN1ARdbzozY6Er/lSYQmVli550lFRCJKaUGhmf0Mu A==; X-IronPort-AV: E=Sophos;i="5.90,259,1643644800"; d="scan'208";a="302082172" Received: from uls-op-cesaip02.wdc.com (HELO uls-op-cesaep02.wdc.com) ([199.255.45.15]) by ob1.hgst.iphmx.com with ESMTP; 14 Apr 2022 17:10:27 +0800 IronPort-SDR: 40mEmTXlwNAPG4NkazTN8jkrdnXxPUNq2jh6N0wRPnOd0eeImLbOYpRCPa4kEOk72a0XbjSlNM ZZ5oFGuujirM7SQxmmierz9Z1SybAOJeLICK5E2T+pBUS6yeDQAMcJP/32iSR9GXunntYwbhGp cizU7qqcCuI2pgoUE7XLF3wJgladdlUy5MhA6hO5ohz9OLkxqg7kPK85wXC0OQV6WIbkKF+QF1 CT2c+5PFVqQqhM7MuHi3wjbA6vvRXOLJrGnRdFpw0I8hR+Wo/rl+jUrOKIh72UmcpGnV995X1e V7Ggzb20+QMMI0KXdkrS1Uiu Received: from uls-op-cesaip01.wdc.com ([10.248.3.36]) by uls-op-cesaep02.wdc.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 14 Apr 2022 01:40:52 -0700 IronPort-SDR: eECsn3y6XfvMEdB/CiQdQvSLz7JwPK0CMSCKeVLXCutpO/SyK1L6/AerYOS6fivGy+KWqq95WU KZbFYZgCW5kS7TYj5djeu2HI6ksPym8IbS8jLa9NQgjmPv9e38kpV1p2hXGKfrhpYHwUnZUmOb zRkNgzVAZDugj7lxxDR5RYZ3rJiuiBHVwnn0aS23M5CZbrJUeexv3kOpuX8Ucy+4FH3oLGZ0Ow Ctrb/JSBPZKJQNaQvbmnWzhGa2K0Zbfwm+K3S1Az/YVRNg3tciL2jDOMDG9mwyZrA+1qRhhUTO 5/E= WDCIronportException: Internal Received: from unknown (HELO x1-carbon.lan) ([10.225.164.18]) by uls-op-cesaip01.wdc.com with ESMTP; 14 Apr 2022 02:10:23 -0700 From: Niklas Cassel To: Alexander Viro , Eric Biederman , Kees Cook , Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Greg Ungerer , Mike Frysinger , Damien Le Moal , Niklas Cassel , stable@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org Subject: [PATCH v2] binfmt_flat: do not stop relocating GOT entries prematurely on riscv Date: Thu, 14 Apr 2022 11:10:18 +0200 Message-Id: <20220414091018.896737-1-niklas.cassel@wdc.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=wdc.com header.s=dkim.wdc.com header.b=ksRGpONN; spf=none (imf13.hostedemail.com: domain of "prvs=09613d920=niklas.cassel@wdc.com" has no SPF policy when checking 68.232.143.124) smtp.mailfrom="prvs=09613d920=niklas.cassel@wdc.com"; dmarc=pass (policy=quarantine) header.from=wdc.com X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 5F19120009 X-Stat-Signature: urtcp6rfdw8fbag3sqopfoa9jab9og1s X-HE-Tag: 1649927428-603779 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: bFLT binaries are usually created using elf2flt. The linker script used by elf2flt has defined the .data section like the following for the last 19 years: .data : { _sdata = . ; __data_start = . ; data_start = . ; *(.got.plt) *(.got) FILL(0) ; . = ALIGN(0x20) ; LONG(-1) . = ALIGN(0x20) ; ... } It places the .got.plt input section before the .got input section. The same is true for the default linker script (ld --verbose) on most architectures except x86/x86-64. The binfmt_flat loader should relocate all GOT entries until it encounters a -1 (the LONG(-1) in the linker script). The problem is that the .got.plt input section starts with a GOTPLT header (which has size 16 bytes on elf64-riscv and 8 bytes on elf32-riscv), where the first word is set to -1. See the binutils implementation for riscv [1]. This causes the binfmt_flat loader to stop relocating GOT entries prematurely and thus causes the application to crash when running. Fix this by skipping the whole GOTPLT header, since the whole GOTPLT header is reserved for the dynamic linker. The GOTPLT header will only be skipped for bFLT binaries with flag FLAT_FLAG_GOTPIC set. This flag is unconditionally set by elf2flt if the supplied ELF binary has the symbol _GLOBAL_OFFSET_TABLE_ defined. ELF binaries without a .got input section should thus remain unaffected. Tested on RISC-V Canaan Kendryte K210 and RISC-V QEMU nommu_virt_defconfig. [1] https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=bfd/elfnn-riscv.c;hb=binutils-2_38#l3275 Cc: Signed-off-by: Niklas Cassel Reviewed-by: Damien Le Moal --- Changes since v1: -Incorporated review comments from Eric Biederman. RISC-V elf2flt patches are still not merged, they can be found here: https://github.com/floatious/elf2flt/tree/riscv buildroot branch for k210 nommu (including this patch and elf2flt patches): https://github.com/floatious/buildroot/tree/k210-v14 fs/binfmt_flat.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c index 626898150011..e5e2a03b39c1 100644 --- a/fs/binfmt_flat.c +++ b/fs/binfmt_flat.c @@ -440,6 +440,30 @@ static void old_reloc(unsigned long rl) /****************************************************************************/ +static inline u32 __user *skip_got_header(u32 __user *rp) +{ + if (IS_ENABLED(CONFIG_RISCV)) { + /* + * RISC-V has a 16 byte GOT PLT header for elf64-riscv + * and 8 byte GOT PLT header for elf32-riscv. + * Skip the whole GOT PLT header, since it is reserved + * for the dynamic linker (ld.so). + */ + u32 rp_val0, rp_val1; + + if (get_user(rp_val0, rp)) + return rp; + if (get_user(rp_val1, rp + 1)) + return rp; + + if (rp_val0 == 0xffffffff && rp_val1 == 0xffffffff) + rp += 4; + else if (rp_val0 == 0xffffffff) + rp += 2; + } + return rp; +} + static int load_flat_file(struct linux_binprm *bprm, struct lib_info *libinfo, int id, unsigned long *extra_stack) { @@ -789,7 +813,8 @@ static int load_flat_file(struct linux_binprm *bprm, * image. */ if (flags & FLAT_FLAG_GOTPIC) { - for (rp = (u32 __user *)datapos; ; rp++) { + rp = skip_got_header((u32 * __user) datapos); + for (; ; rp++) { u32 addr, rp_val; if (get_user(rp_val, rp)) return -EFAULT;