From mboxrd@z Thu Jan 1 00:00:00 1970 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 60024C433F5 for ; Thu, 14 Apr 2022 23:51:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B3B876B0073; Thu, 14 Apr 2022 19:51:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AEA8E6B0074; Thu, 14 Apr 2022 19:51:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9652F6B0075; Thu, 14 Apr 2022 19:51:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.28]) by kanga.kvack.org (Postfix) with ESMTP id 89C036B0073 for ; Thu, 14 Apr 2022 19:51:35 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 4FC47609EA for ; Thu, 14 Apr 2022 23:51:35 +0000 (UTC) X-FDA: 79357134150.19.AF05FAD Received: from esa4.hgst.iphmx.com (esa4.hgst.iphmx.com [216.71.154.42]) by imf07.hostedemail.com (Postfix) with ESMTP id 6DEE740006 for ; Thu, 14 Apr 2022 23:51:34 +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=1649980294; x=1681516294; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=WbVM9cfScKKYc/EursZygzoF86dsBLIRg6xwYxy7W0E=; b=nElI6yjehrkMun5MRHQrBcTYMnW6a84NcBwQSDaINF19q7bYptNi05Iv /c1pE491DTnodlapgHWw/VffQk1E+n0M9L/IoLZYKejYojFlwq6v4lvOV 4ZyTxzAlqpsZMr5hhcH4jSvYXSeAIcGDp9iRhjJhO5IkUhfV2O0BoxMWh zJwX9ftx6zmrexZlgm6yDduGtz0UKYqccazpnc2jtyVDmavKwqmvHADtQ PWysJ5Y929fClRD4FjtwdIJlSiOQN4Thss9DsDJ8amSwD7ZmWyC9Yz7/1 46/YHkZ4K+UkVD6yH89kcEyEdiO0eFQTOJq3qH8J3dZN3Xpjh36ZdWSAs g==; X-IronPort-AV: E=Sophos;i="5.90,261,1643644800"; d="scan'208";a="196816234" Received: from h199-255-45-14.hgst.com (HELO uls-op-cesaep01.wdc.com) ([199.255.45.14]) by ob1.hgst.iphmx.com with ESMTP; 15 Apr 2022 07:51:33 +0800 IronPort-SDR: 3lojhWzBwcVxmGE9iupWh6LGqvSB7hk/1sVOXbkhjWWSRDK/Dcf7n8TFHoF10MAxEG899/UiNx /9xFNx3OOA05l08B3Pu7dQvqyw7SAdbHlhtcHpN0QZFCuEfUtf2gCU6hncOyYA3HLLDOE0EOXz lSWFi2JF3fg1lz4c26nJQvRs3YWtIlKHj6/+M59bFLOE3hF5dn3t4bu5NUHBU0ZnjycjvgvYfP hzUTMMrN0OPzDAyj95x/mPAVC9yUKIgeGbwacMswKEcVXTuHi9Ajv+VVIDYpIC0Bvq45esjAG6 mCsXdGKMLZdTIPWsAaogJJOa Received: from uls-op-cesaip02.wdc.com ([10.248.3.37]) by uls-op-cesaep01.wdc.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 14 Apr 2022 16:22:45 -0700 IronPort-SDR: /WclrTrsbaZ5neuSqT+ucq7IOUatk9Qxkh1FIQAo+IK3AhA4DRHHkOpUHpOVg3FrEOVhJicnxH f7+CuYNlpUHZ39nYxNjCJ478UvYaxkML6cxCnXUfDmzP95E/CZwTpzg4BiAlZ4vOJlGx/BHYrI pPsXrrH/734RUoDD8Vw6kwVOAQv0PwlQZThqFV/F7EesNy2NHrBm7VOeKZgaDBvsrNhP+qpH6X ac9GyE4URFttVk9f0d+G95ANv0OTTBqJ6GGlEnCobjmOsTzUEnfZpRSE+0aWosJ78DpzDb0VFn dQo= WDCIronportException: Internal Received: from usg-ed-osssrv.wdc.com ([10.3.10.180]) by uls-op-cesaip02.wdc.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 14 Apr 2022 16:51:34 -0700 Received: from usg-ed-osssrv.wdc.com (usg-ed-osssrv.wdc.com [127.0.0.1]) by usg-ed-osssrv.wdc.com (Postfix) with ESMTP id 4KfbpD67Wfz1SVp2 for ; Thu, 14 Apr 2022 16:51:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d= opensource.wdc.com; h=content-transfer-encoding:content-type :in-reply-to:organization:from:references:to:content-language :subject:user-agent:mime-version:date:message-id; s=dkim; t= 1649980291; x=1652572292; bh=WbVM9cfScKKYc/EursZygzoF86dsBLIRg6x wYxy7W0E=; b=cKEhB42giErCT2IlFVEQa2gh6lHnR8sHxTTUPuQWALBSpaAGvGT mGL4PdrltRTNgGcZVQhmNWj+iOhhaj7raUOIsMozPAD3jvXBE4Y7rhE6kYmMW+GU SqbqYrYbDcdJwswoJ2O7p/OylgnhOC4xbRfuY15N5vs8YrmxwMdkVq4zffFXHgY0 I/FAgjKzXKzRjN6jSnwKZA0MrTdAW2KKLPtvrD9a0EBVi3Vt/yPxqUCF4cBm7Xaa UPjYAq3mJb+Z7jFuHA0DJkxAcDC3tz3bWT8y1s99lGuVm4JPrILm0NvTCS6AUgRb 9frurXIYEBUxcziyVQUXnkBzincyCOiaBLQ== X-Virus-Scanned: amavisd-new at usg-ed-osssrv.wdc.com Received: from usg-ed-osssrv.wdc.com ([127.0.0.1]) by usg-ed-osssrv.wdc.com (usg-ed-osssrv.wdc.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id j9Pn_4L58vJr for ; Thu, 14 Apr 2022 16:51:31 -0700 (PDT) Received: from [10.225.163.9] (unknown [10.225.163.9]) by usg-ed-osssrv.wdc.com (Postfix) with ESMTPSA id 4Kfbp91f4qz1Rvlx; Thu, 14 Apr 2022 16:51:29 -0700 (PDT) Message-ID: Date: Fri, 15 Apr 2022 08:51:27 +0900 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: [PATCH v2] binfmt_flat: do not stop relocating GOT entries prematurely on riscv Content-Language: en-US To: Niklas Cassel , Alexander Viro , Eric Biederman , Kees Cook , Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Greg Ungerer , Mike Frysinger , stable@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org References: <20220414091018.896737-1-niklas.cassel@wdc.com> From: Damien Le Moal Organization: Western Digital Research In-Reply-To: <20220414091018.896737-1-niklas.cassel@wdc.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Stat-Signature: utnaknqf6h377f865xbubfmjqs3fmmka X-Rspam-User: Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=wdc.com header.s=dkim.wdc.com header.b=nElI6yje; dkim=pass header.d=opensource.wdc.com header.s=dkim header.b=cKEhB42g; dmarc=pass (policy=quarantine) header.from=opensource.wdc.com; spf=pass (imf07.hostedemail.com: domain of "prvs=0964214f5=damien.lemoal@opensource.wdc.com" designates 216.71.154.42 as permitted sender) smtp.mailfrom="prvs=0964214f5=damien.lemoal@opensource.wdc.com" X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 6DEE740006 X-HE-Tag: 1649980294-599586 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: On 4/14/22 18:10, Niklas Cassel wrote: > 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 > --- > 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; This looks good to me. But thinking more about it, do we really need to check what the content of the header is ? Why not simply replace this entire hunk with: return rp + sizeof(unsigned long) * 2; to ignore the 16B (or 8B for 32-bits arch) header regardless of what the header word values are ? Are there any case where the header is *not* present ? > + } > + 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; Regardless of the above nit, feel free to add: Reviewed-by: Damien Le Moal -- Damien Le Moal Western Digital Research