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 DD789C3DA4B for ; Mon, 8 Jul 2024 17:35:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 591DD6B0085; Mon, 8 Jul 2024 13:35:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 540476B0098; Mon, 8 Jul 2024 13:35:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3E1366B0099; Mon, 8 Jul 2024 13:35:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 208BD6B0085 for ; Mon, 8 Jul 2024 13:35:43 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id A1111C1499 for ; Mon, 8 Jul 2024 17:35:42 +0000 (UTC) X-FDA: 82317287724.27.7E11D96 Received: from out03.mta.xmission.com (out03.mta.xmission.com [166.70.13.233]) by imf24.hostedemail.com (Postfix) with ESMTP id B7F5C18001A for ; Mon, 8 Jul 2024 17:35:39 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=xmission.com; spf=pass (imf24.hostedemail.com: domain of ebiederm@xmission.com designates 166.70.13.233 as permitted sender) smtp.mailfrom=ebiederm@xmission.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720460116; a=rsa-sha256; cv=none; b=7HMapncOVsK/2b2Mjqxtp7xQyDXSrYX8cTzHsvZ1Zng97W7VblzMwFnJ1RA2Ps4q61SOf8 MDI4LmwAFcy0lUIbTvAf2zhCOKWmlpritJcsTkJUe38FL1QMovF/ghmvAfrQqR1dSNSsA1 Xkx0tQFpsRtrHV7GwpFeRHZskMa2qAY= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=xmission.com; spf=pass (imf24.hostedemail.com: domain of ebiederm@xmission.com designates 166.70.13.233 as permitted sender) smtp.mailfrom=ebiederm@xmission.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720460115; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xtgcXZAAOAsxt8XOvnrdfHpj8EbUUyZl4Fs/cr4ue54=; b=OnePmJxbj6WdobvQutN34LY0Zal5pkiRpmrwb9VJaD2hyWhusMKuSl32GK5DesVKYWksk+ w+ExuCpVjpFQNlyDbKtJ8rE7QitCkRt1kR4eq4jDVKsBLqzxNfbOT+471P8eHLafZvmWQX jR1V2iI9sjHbH6gSSeBwNHtQtGsRVW4= Received: from in02.mta.xmission.com ([166.70.13.52]:54410) by out03.mta.xmission.com with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1sQsGh-006EIl-Hs; Mon, 08 Jul 2024 11:35:15 -0600 Received: from ip68-227-165-127.om.om.cox.net ([68.227.165.127]:41240 helo=email.froward.int.ebiederm.org.xmission.com) by in02.mta.xmission.com with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1sQsGg-009Wzz-4Z; Mon, 08 Jul 2024 11:35:15 -0600 From: "Eric W. Biederman" To: Florian Weimer Cc: =?utf-8?Q?Micka=C3=ABl_Sala=C3=BCn?= , Al Viro , Christian Brauner , Kees Cook , Linus Torvalds , Paul Moore , Theodore Ts'o , Alejandro Colomar , Aleksa Sarai , Andrew Morton , Andy Lutomirski , Arnd Bergmann , Casey Schaufler , Christian Heimes , Dmitry Vyukov , Eric Biggers , Eric Chiang , Fan Wu , Geert Uytterhoeven , James Morris , Jan Kara , Jann Horn , Jeff Xu , Jonathan Corbet , Jordan R Abrahams , Lakshmi Ramasubramanian , Luca Boccassi , Luis Chamberlain , "Madhavan T . Venkataraman" , Matt Bobrowski , Matthew Garrett , Matthew Wilcox , Miklos Szeredi , Mimi Zohar , Nicolas Bouchinet , Scott Shell , Shuah Khan , Stephen Rothwell , Steve Dower , Steve Grubb , Thibaut Sautereau , Vincent Strubel , Xiaoming Ni , Yin Fengwei , kernel-hardening@lists.openwall.com, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, linux-mm@kvack.org References: <20240704190137.696169-1-mic@digikod.net> <20240704190137.696169-2-mic@digikod.net> <87bk3bvhr1.fsf@oldenburg.str.redhat.com> <20240706.poo9ahd3La9b@digikod.net> <871q46bkoz.fsf@oldenburg.str.redhat.com> <20240708.zooj9Miaties@digikod.net> <878qybet6t.fsf_-_@oldenburg.str.redhat.com> Date: Mon, 08 Jul 2024 12:34:28 -0500 In-Reply-To: <878qybet6t.fsf_-_@oldenburg.str.redhat.com> (Florian Weimer's message of "Mon, 08 Jul 2024 18:37:14 +0200") Message-ID: <87cynn3hzv.fsf@email.froward.int.ebiederm.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-XM-SPF: eid=1sQsGg-009Wzz-4Z;;;mid=<87cynn3hzv.fsf@email.froward.int.ebiederm.org>;;;hst=in02.mta.xmission.com;;;ip=68.227.165.127;;;frm=ebiederm@xmission.com;;;spf=pass X-XM-AID: U2FsdGVkX19HTToYa5/poe3TSuCfV/i9WSH+lfvgNyM= X-SA-Exim-Connect-IP: 68.227.165.127 X-SA-Exim-Mail-From: ebiederm@xmission.com Subject: Re: [PATCH] binfmt_elf: Fail execution of shared objects with ELIBEXEC X-SA-Exim-Version: 4.2.1 (built Sat, 08 Feb 2020 21:53:50 +0000) X-SA-Exim-Scanned: Yes (on in02.mta.xmission.com) X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: B7F5C18001A X-Stat-Signature: iqkojn356zrcqo6n85jijybyn73qtxxb X-Rspam-User: X-HE-Tag: 1720460139-506627 X-HE-Meta: U2FsdGVkX1/pOnxg6Q7ACfpCekWagidIsV0Yv29HkG9UBM39pXwKuRJz7G4/RCTqaWKNLnfCsN1n/ZYqwmYbFPGKzjDSET8qx3zfHQJeVQ5/kPUcwbEEA3QT9dpyEtbVKnR3dZnmkK0KhP+vgb/rylUqJPfoie7p/nrsKXKJv/nqHQ/fYY2Ag/hjfYAb+SED+bDwqegelhEWE9FW2R58bSbYs0bFYeAebgKkiGd/GRQcqJKSRk+iO0tTRzOVPPYemo86MaYWoJlInk4n6sZtobt9Aw53TS135RTb7fnCFjcs7FMn8N6UzbiRAgagAVLrjSHmCMMpQae6SfuLdi8dY+01STMk9+klHOMb2yY/aY7KwX3iTIiXkXsmnfUdJEn4Sn8WTwJOzGk4+qtbSoTpvY6ODQR+YwbtkdJOT87ZHtJP4VKMms6T3tSg3qbEejRPwWz6q4PhLr39qNjqHkFXgxUfvVvzmOAXIyC9gMnrRbqyWQyxVeXQFA22XjVRILnAzZn57RsEfIWMCAp3LB52QdsplSt2D1n+hRRuwAyxsCxz64EL7NKkpAhgM0wpIM5qxJyQdJ++8CQ/vm/D1/fv9mKJYLobxCU4Q+BfsmchrUWeadrV7XSZkBLWYu1Sl69Vg1wf6kNYP5Ma/7a+qLl6yjh5OA9wXAnlwN+S5Rw3gkilI3Yi2lmJ2gX9hQWbLJE6a92e6iat0iMPzwecrpwt1j6hLvE7wI8XsCTQ6+JP3hFna6RxF/x8W6i1p2EJmilnh7Pkqa3+RqcMjWxv3xhZ0FsoqjoCoMEekU61w/nsBaGaT3SN+radfzsF2vO0w//wDIOz4KI5jKkuAjtFbXr4XFX6LA93ce5PjJY/eHUMBFa6Vb0RnPjxR2dWxNjwcomJYMlbWAf6xPYXJppwSMUTBOWH6q6YI99pN5yAf/Cd/bcSO/167kwQ5w50/8q3COS5V3IlQJfbyO2sfAc/Uav NYGPopIW dvId171glm3bX9aMqluRIQCJQMcq5T2Gr6pHu9CikotaIXkyOsw0SaMD8cKxWVJwh43oRiITu5g58vXX0KYmYSNAQoWOrDXerrINBsC3A/l4rHF00D3wOnqzqsRAe2gCX258LJ0XEBQSoeNTpaAfCSiKoZczrgXdtDN/uSbPEOU2JF4c= 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: List-Subscribe: List-Unsubscribe: Florian Weimer writes: > * Micka=C3=ABl Sala=C3=BCn: > >> On Sat, Jul 06, 2024 at 05:32:12PM +0200, Florian Weimer wrote: >>> * Micka=C3=ABl Sala=C3=BCn: >>>=20 >>> > On Fri, Jul 05, 2024 at 08:03:14PM +0200, Florian Weimer wrote: >>> >> * Micka=C3=ABl Sala=C3=BCn: >>> >>=20 >>> >> > Add a new AT_CHECK flag to execveat(2) to check if a file would be >>> >> > allowed for execution. The main use case is for script interprete= rs and >>> >> > dynamic linkers to check execution permission according to the ker= nel's >>> >> > security policy. Another use case is to add context to access logs= e.g., >>> >> > which script (instead of interpreter) accessed a file. As any >>> >> > executable code, scripts could also use this check [1]. >>> >>=20 >>> >> Some distributions no longer set executable bits on most shared obje= cts, >>> >> which I assume would interfere with AT_CHECK probing for shared obje= cts. >>> > >>> > A file without the execute permission is not considered as executable= by >>> > the kernel. The AT_CHECK flag doesn't change this semantic. Please >>> > note that this is just a check, not a restriction. See the next patch >>> > for the optional policy enforcement. >>> > >>> > Anyway, we need to define the policy, and for Linux this is done with >>> > the file permission bits. So for systems willing to have a consistent >>> > execution policy, we need to rely on the same bits. >>>=20 >>> Yes, that makes complete sense. I just wanted to point out the odd >>> interaction with the old binutils bug and the (sadly still current) >>> kernel bug. >>>=20 >>> >> Removing the executable bit is attractive because of a combination of >>> >> two bugs: a binutils wart which until recently always set the entry >>> >> point address in the ELF header to zero, and the kernel not checking= for >>> >> a zero entry point (maybe in combination with an absent program >>> >> interpreter) and failing the execve with ELIBEXEC, instead of doing = the >>> >> execve and then faulting at virtual address zero. Removing the >>> >> executable bit is currently the only way to avoid these confusing >>> >> crashes, so I understand the temptation. >>> > >>> > Interesting. Can you please point to the bug report and the fix? I >>> > don't see any ELIBEXEC in the kernel. >>>=20 >>> The kernel hasn't been fixed yet. I do think this should be fixed, so >>> that distributions can bring back the executable bit. >> >> Can you please point to the mailing list discussion or the bug report? > > I'm not sure if this was ever reported upstream as an RFE to fail with > ELIBEXEC. We have downstream bug report: > > Prevent executed .so files with e_entry =3D=3D 0 from attempting to bec= ome > a process. > > > I've put together a patch which seems to work, see below. > > I don't think there's any impact on AT_CHECK with execveat because that > mode will never get to this point. > > Thanks, > Florian > > ---8<----------------------------------------------------------------- > Subject: binfmt_elf: Fail execution of shared objects with ELIBEXEC >=20=20=20=20=20 > Historically, binutils has used the start of the text segment as the > entry point if _start was not defined. Executing such files results > in crashes with random effects, depending on what code resides there. > However, starting with binutils 2.38, BFD ld uses a zero entry point, > due to commit 5226a6a892f922ea672e5775c61776830aaf27b7 ("Change the > linker's heuristic for computing the entry point for binaries so that > shared libraries default to an entry point of 0."). This means > that shared objects with zero entry points are becoming more common, > and it makes sense for the kernel to recognize them and refuse > to execute them. > > For backwards compatibility, if a load segment does not map the ELF > header at file offset zero, the kernel still proceeds as before, in > case the file is very non-standard and can actually start executing > at virtual offset zero. As written I find the logic of the patch confusing, and slightly wrong. The program header value e_entry is a virtual address, possibly adjusted by load_bias. Which makes testing it against the file offset of a PT_LOAD segment wrong. It needs to test against elf_ppnt->p_vaddr. I think performing an early sanity check to avoid very confusing crashes seems sensible (as long as it is inexpensive). This appears inexpensive enough that we don't care. This code is also before begin_new_exec so it is early enough to be meaningful. I think the check should simply test if e_entry is mapped. So a range check please to see if e_entry falls in a PT_LOAD segment. Having code start at virtual address 0 is a perfectly fine semantically and might happen in embedded scenarios. The program header is not required to be mapped or be first, (AKA p_offset and p_vaddr can have a somewhat arbitrary relationship) so any mention of the program header in your logic seems confusing to me. I think your basic structure will work. Just the first check needs to check if e_entry is lands inside the virtual address of a PT_LOAD segment. The second check should just be checking a variable to see if e_entry was inside any PT_LOAD segment, and there is no interpreter. Does that make sense? Eric > > Signed-off-by: Florian Weimer > > diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c > index a43897b03ce9..ebd7052eb616 100644 > --- a/fs/binfmt_elf.c > +++ b/fs/binfmt_elf.c > @@ -830,6 +830,7 @@ static int load_elf_binary(struct linux_binprm *bprm) > unsigned long e_entry; > unsigned long interp_load_addr =3D 0; > unsigned long start_code, end_code, start_data, end_data; > + bool elf_header_mapped =3D false; > unsigned long reloc_func_desc __maybe_unused =3D 0; > int executable_stack =3D EXSTACK_DEFAULT; > struct elfhdr *elf_ex =3D (struct elfhdr *)bprm->buf; > @@ -865,6 +866,9 @@ static int load_elf_binary(struct linux_binprm *bprm) > continue; > } >=20=20 > + if (elf_ppnt->p_type =3D=3D PT_LOAD && !elf_ppnt->p_offset) > + elf_header_mapped =3D true; > + > if (elf_ppnt->p_type !=3D PT_INTERP) > continue; >=20=20 > @@ -921,6 +925,20 @@ static int load_elf_binary(struct linux_binprm *bprm) > goto out_free_ph; > } >=20=20 > + /* > + * A zero value for e_entry means that the ELF file has no > + * entry point. If the ELF header is mapped, this is > + * guaranteed to crash (often even on the first instruction), > + * so fail the execve system call instead. (This is most > + * likely to happen for a shared object.) If the object has a > + * program interpreter, dealing with the situation is its > + * responsibility. > + */ > + if (elf_header_mapped && !elf_ex->e_entry && !interpreter) { > + retval =3D -ELIBEXEC; > + goto out_free_dentry; > + } > + > elf_ppnt =3D elf_phdata; > for (i =3D 0; i < elf_ex->e_phnum; i++, elf_ppnt++) > switch (elf_ppnt->p_type) {