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 X-Spam-Level: X-Spam-Status: No, score=-5.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0FFDC55178 for ; Thu, 29 Oct 2020 11:02:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4663020825 for ; Thu, 29 Oct 2020 11:02:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727075AbgJ2LCc (ORCPT ); Thu, 29 Oct 2020 07:02:32 -0400 Received: from mail.kernel.org ([198.145.29.99]:39330 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726385AbgJ2LCc (ORCPT ); Thu, 29 Oct 2020 07:02:32 -0400 Received: from gaia (unknown [95.145.162.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A0AB520735; Thu, 29 Oct 2020 11:02:24 +0000 (UTC) Date: Thu, 29 Oct 2020 11:02:22 +0000 From: Catalin Marinas To: Dave Martin Cc: Jeremy Linton , Szabolcs Nagy , Mark Rutland , systemd-devel@lists.freedesktop.org, Kees Cook , Will Deacon , "linux-kernel@vger.kernel.org" , Mark Brown , toiwoton@gmail.com, libc-alpha@sourceware.org, "linux-arm-kernel@lists.infradead.org" Subject: Re: BTI interaction between seccomp filters in systemd and glibc mprotect calls, causing service failures Message-ID: <20201029110220.GC10776@gaia> References: <8584c14f-5c28-9d70-c054-7c78127d84ea@arm.com> <20201026162410.GB27285@arm.com> <20201026165755.GV3819@arm.com> <20201026175230.GC27285@arm.com> <45c64b49-a38b-4b0c-d9cf-6c586dacbcc9@arm.com> <20201027141522.GD27285@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201027141522.GD27285@arm.com> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 27, 2020 at 02:15:22PM +0000, Dave P Martin wrote: > I also wonder whether we actually care whether the pages are marked > executable or not here; probably the flags can just be independent. This > rather depends on whether the how the architecture treats the BTI (a.k.a > GP) pagetable bit for non-executable pages. I have a feeling we already > allow PROT_BTI && !PROT_EXEC through anyway. > > > What about a generic-ish set/clear interface that still works by just > adding a couple of PROT_ flags: > > switch (flags & (PROT_SET | PROT_CLEAR)) { > case PROT_SET: prot |= flags; break; > case PROT_CLEAR: prot &= ~flags; break; > case 0: prot = flags; break; > > default: > return -EINVAL; > } > > This can't atomically set some flags while clearing some others, but for > simple stuff it seems sufficient and shouldn't be too invasive on the > kernel side. > > We will still have to take the mm lock when doing a SET or CLEAR, but > not for the non-set/clear case. > > > Anyway, libc could now do: > > mprotect(addr, len, PROT_SET | PROT_BTI); > > with much the same effect as your PROT_BTI_IF_X. > > > JITting or breakpoint setting code that wants to change the permissions > temporarily, without needing to know whether PROT_BTI is set, say: > > mprotect(addr, len, PROT_SET | PROT_WRITE); > *addr = BKPT_INSN; > mprotect(addr, len, PROT_CLEAR | PROT_WRITE); The problem with this approach is that you can't catch PROT_EXEC|PROT_WRITE mappings via seccomp. So you'd have to limit it to some harmless PROT_ flags only. I don't like this limitation, nor the PROT_BTI_IF_X approach. The only generic solutions I see are to either use a stateful filter in systemd or pass the old state to the kernel in a cmpxchg style so that seccomp can check it (I think you suggest this at some point). The latter requires a new syscall which is not something we can address as a quick, back-portable fix here. If systemd cannot be changed to use a stateful filter for w^x detection, my suggestion is to go for the kernel setting PROT_BTI on the main executable with glibc changed to tolerate EPERM on mprotect(). I don't mind adding an AT_FLAGS bit if needed but I don't think it buys us much. Once the current problem is fixed, we can look at a better solution longer term as a new syscall. -- Catalin 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 X-Spam-Level: X-Spam-Status: No, score=-5.3 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DF59BC2D0A3 for ; Thu, 29 Oct 2020 11:03:53 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 4A38720759 for ; Thu, 29 Oct 2020 11:03:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="qk5rUk8D" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4A38720759 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=BMm0akGtFokf3coRh3zxqkZxR1CpTAg96tcKaaxFugo=; b=qk5rUk8DjQUXz8og5uLH+C/Fz CN6VUpedi9ORYqu2xCXrQm9NJnqNtbbl6PAEsfZDiXyZJKRkMKoMv7lwEpi7YcjN74ArWP7ADNw51 OX0s+0HLwyczOHS5Te6khgHrlHLpEsNv1yrQ3X9CyxzLYNxIzSGv9KzsRrNN/RyuPSg/Uc1c1pj8+ 2aeV8ttD3Kizz7t4qXmV+UlJypSEvZmnpXX25TVwgGh9B6M6wz3ZA314enVYdnqoWg0k+DyoJy9ZY Jc8MX1JUJA+Q4E/wt4O4CyROZ9i72xIYLWPwm+JgJimwT1mA6ZKJTFlRgOrXHRtR0W13wBF7Pz60H PD9S3GM2w==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kY5hP-0006z5-Cw; Thu, 29 Oct 2020 11:02:31 +0000 Received: from mail.kernel.org ([198.145.29.99]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kY5hL-0006yW-V3 for linux-arm-kernel@lists.infradead.org; Thu, 29 Oct 2020 11:02:28 +0000 Received: from gaia (unknown [95.145.162.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A0AB520735; Thu, 29 Oct 2020 11:02:24 +0000 (UTC) Date: Thu, 29 Oct 2020 11:02:22 +0000 From: Catalin Marinas To: Dave Martin Subject: Re: BTI interaction between seccomp filters in systemd and glibc mprotect calls, causing service failures Message-ID: <20201029110220.GC10776@gaia> References: <8584c14f-5c28-9d70-c054-7c78127d84ea@arm.com> <20201026162410.GB27285@arm.com> <20201026165755.GV3819@arm.com> <20201026175230.GC27285@arm.com> <45c64b49-a38b-4b0c-d9cf-6c586dacbcc9@arm.com> <20201027141522.GD27285@arm.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20201027141522.GD27285@arm.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201029_070228_102952_97307208 X-CRM114-Status: GOOD ( 22.85 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , systemd-devel@lists.freedesktop.org, Kees Cook , Szabolcs Nagy , Will Deacon , "linux-kernel@vger.kernel.org" , Jeremy Linton , Mark Brown , toiwoton@gmail.com, libc-alpha@sourceware.org, "linux-arm-kernel@lists.infradead.org" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Tue, Oct 27, 2020 at 02:15:22PM +0000, Dave P Martin wrote: > I also wonder whether we actually care whether the pages are marked > executable or not here; probably the flags can just be independent. This > rather depends on whether the how the architecture treats the BTI (a.k.a > GP) pagetable bit for non-executable pages. I have a feeling we already > allow PROT_BTI && !PROT_EXEC through anyway. > > > What about a generic-ish set/clear interface that still works by just > adding a couple of PROT_ flags: > > switch (flags & (PROT_SET | PROT_CLEAR)) { > case PROT_SET: prot |= flags; break; > case PROT_CLEAR: prot &= ~flags; break; > case 0: prot = flags; break; > > default: > return -EINVAL; > } > > This can't atomically set some flags while clearing some others, but for > simple stuff it seems sufficient and shouldn't be too invasive on the > kernel side. > > We will still have to take the mm lock when doing a SET or CLEAR, but > not for the non-set/clear case. > > > Anyway, libc could now do: > > mprotect(addr, len, PROT_SET | PROT_BTI); > > with much the same effect as your PROT_BTI_IF_X. > > > JITting or breakpoint setting code that wants to change the permissions > temporarily, without needing to know whether PROT_BTI is set, say: > > mprotect(addr, len, PROT_SET | PROT_WRITE); > *addr = BKPT_INSN; > mprotect(addr, len, PROT_CLEAR | PROT_WRITE); The problem with this approach is that you can't catch PROT_EXEC|PROT_WRITE mappings via seccomp. So you'd have to limit it to some harmless PROT_ flags only. I don't like this limitation, nor the PROT_BTI_IF_X approach. The only generic solutions I see are to either use a stateful filter in systemd or pass the old state to the kernel in a cmpxchg style so that seccomp can check it (I think you suggest this at some point). The latter requires a new syscall which is not something we can address as a quick, back-portable fix here. If systemd cannot be changed to use a stateful filter for w^x detection, my suggestion is to go for the kernel setting PROT_BTI on the main executable with glibc changed to tolerate EPERM on mprotect(). I don't mind adding an AT_FLAGS bit if needed but I don't think it buys us much. Once the current problem is fixed, we can look at a better solution longer term as a new syscall. -- Catalin _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel