Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp3804030rdb; Thu, 14 Sep 2023 03:14:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEcg1jiUYjaxa9OhmtksB/lo+yeYCjCV68L1isuTT6ardb4xdOiLFtCSS4yxjMRsChBqe9h X-Received: by 2002:a81:5c45:0:b0:59b:4f5e:12d8 with SMTP id q66-20020a815c45000000b0059b4f5e12d8mr5331268ywb.47.1694686458896; Thu, 14 Sep 2023 03:14:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694686458; cv=none; d=google.com; s=arc-20160816; b=dH++WJq0GbpGuS52e6g/gCsA80leH+VdFzTNfx8u6smehG0nmmhkkw2DySPUqADnzl y+SotldokcVUKdAg38cRgWf738YfdrwuK8X36m34tLKRsI0+B/JjsrrbpcVEyo+4BUeT +Xmv2ZShpbQXfiTfuhD+Ums6AYtthbytzEelm/mD9krY79guGc1RtY6zxyGPTuaebFdP l/wJcLA97KbbbXfZN+/LKSnVX/Fxdq0nuztG8YTir7Q9RMqmCR8+xKLBxKa3Zm1GmhbQ oS2l0uWCYDv+TXBUljDjQHL+C4aXuZHqO6zkDz57gX2VPtpsfP1W623trXGO1Q6neGKK oHSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=fdvL/ezhSBd5/U+goujrTh2bmrKZeoU+G2r72C7ioLc=; fh=kkErRrUiwv/2mCQIf8VZbJU4agBSpowlFWdTkqfmH+k=; b=ukhU9UMY5yprTdxfZQbPA/VetJ2+K4NtOGIQqpvVgraM/GE6n+DJ//VylL8Ci0yLNo fdGmPj1EVe0vs7WOAcdNvS1cBT+P8kBJieQyJk5Jl3vT9GkbgOIJV7WDJn23g7xuJjC0 7zFUZXq+Rszv9kUZ84YnUgMlM6PEdea3Q1L5faL/ZjOD0zpcWXEVp7U7wB5xUo4eRZZt whJy+gktvvMJG+IbbdZB0yV5DS2+bk9pQcNo9qFpDj0DM6N3KoG/cEKCvjVuXsPdbDif P84vvTPjG/MCfP0g1ucLPAQtjcHhlYq9CdO1lWfUnnXM++uPFpagMm8l8w36XektEVGj a+Xg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=ei0JJks1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id w8-20020a63f508000000b00565eb0b4f33si1274934pgh.224.2023.09.14.03.14.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 03:14:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=ei0JJks1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 4082781D4750; Wed, 13 Sep 2023 16:02:35 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233103AbjIMXCi (ORCPT + 99 others); Wed, 13 Sep 2023 19:02:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233007AbjIMXCh (ORCPT ); Wed, 13 Sep 2023 19:02:37 -0400 Received: from todd.t-8ch.de (todd.t-8ch.de [IPv6:2a01:4f8:c010:41de::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 963DE1BCB for ; Wed, 13 Sep 2023 16:02:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1694646151; bh=9nwKgi53Nw3aMPL4gvYJ3xe4olH9cOVWHjkvB/aIJa8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ei0JJks17AdpLhLQiE88kxIh56GBLFTWb4JsFbgm6YICyQPFfLT7TD4a4897X+CZz CSOsImUJVhSjrQC5qdb+S9g/a4YrNE0U7tybLo+TigOT4msWLVrK5I93DXDBKlkTpg 7Q98fH5wLEgXLsjZqzzQ4zdM1e175kcUvrvLqrHU= Date: Thu, 14 Sep 2023 01:02:30 +0200 From: Thomas =?utf-8?Q?Wei=C3=9Fschuh?= To: Sebastian Ott Cc: Mark Brown , Willy Tarreau , linux-kernel@vger.kernel.org Subject: Re: aarch64 binaries using nolibc segfault before reaching the entry point Message-ID: <1d0342f3-0474-482b-b6db-81ca7820a462@t-8ch.de> References: <5d49767a-fbdc-fbe7-5fb2-d99ece3168cb@redhat.com> <2da5ce29-e0de-4715-aa77-453ff3cc48aa@t-8ch.de> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <2da5ce29-e0de-4715-aa77-453ff3cc48aa@t-8ch.de> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 13 Sep 2023 16:02:35 -0700 (PDT) On 2023-09-13 22:19:00+0200, Thomas Weißschuh wrote: > On 2023-09-13 20:44:59+0200, Sebastian Ott wrote: > > the tpidr2 selftest on an arm box segfaults before reaching the entry point. > > I have no clue what is to blame for this or how to debug it but for a > > statically linked binary there shouldn't be much stuff going on besides the > > elf loader? > [..] > > I reduced it to the following reproducer: > > $ cat test.c > int foo; /* It works when deleting this variable */ > > void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) _start(void) > { > __asm__ volatile ( > "mov x8, 93\n" /* NR_exit == 93 */ > "svc #0\n" > ); > __builtin_unreachable(); > } > > $ aarch64-linux-gnu-gcc -Os -static -fno-stack-protector -Wall -nostdlib test.c > $ ./a.out > Segmentation fault > > Also when running under gdb the error message is: > > During startup program terminated with signal SIGSEGV, Segmentation fault. > > So it seems the error already happens during loading. > > Could be a compiler or kernel bug? Callchain for the failure: load_elf_binary() -> if (likely(elf_bss != elf_brk) && unlikely(padzero(elf_bess))) -> padzero() -> clear_user() -> __arch_clear_user() -> failure in arch/arm64/lib/clear_user.S Resulting in a EFAULT which gets translated to SIGSEGV somewhere. The following patch, which seems sensible to me, fixes it for me. But as this is really old, heavily used code I'm a bit hesitant. diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index 7b3d2d491407..13f71733ba63 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -112,7 +112,7 @@ static struct linux_binfmt elf_format = { static int set_brk(unsigned long start, unsigned long end, int prot) { - start = ELF_PAGEALIGN(start); + start = ELF_PAGESTART(start); end = ELF_PAGEALIGN(end); if (end > start) { /*