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=-4.0 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 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 2A1E9C433DF for ; Fri, 31 Jul 2020 18:11:38 +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 F1F752177B for ; Fri, 31 Jul 2020 18:11:37 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="S/DD016Q" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F1F752177B Authentication-Results: mail.kernel.org; dmarc=none (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=PMpzot8/nI4mpJZj0lVf3tywdLbkzosa3WZyaTr16wg=; b=S/DD016QYvk+u3kklwIm/JLFZ /lhjNAReja8AfyFff61eGlTSrGTxBjng1UZa8xP/LMgiT2YxGnZJBAB/+xNyxKee9uZPaCBSE6rZm YHZ+IZ8H2t66awGTX/rulEEWeAHVfTjitqaumIUDlDheJg893z9qnEcfHfEDdQ6/T8HHULETPtMCa RQnDGlLyS6ivS9UiQMvAQ80KbHcpoJIysC7GAmSqMelEW6MSnwEVgm8E76j73cCEcJqBa8pnaF+IQ hig6pBGKaJrNxTk4rPFfQhoWmXFkMEESudI2GVFsuRxOXCj36u+j8eH+klPtizpHW7riARCDIGBYQ Z02ubahKQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1k1ZTq-00022a-TV; Fri, 31 Jul 2020 18:10:06 +0000 Received: from foss.arm.com ([217.140.110.172]) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1k1ZTn-00021l-Pb for linux-arm-kernel@lists.infradead.org; Fri, 31 Jul 2020 18:10:04 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id BC48631B; Fri, 31 Jul 2020 11:10:00 -0700 (PDT) Received: from C02TD0UTHF1T.local (unknown [10.57.4.61]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id BD9B03F71F; Fri, 31 Jul 2020 11:09:58 -0700 (PDT) Date: Fri, 31 Jul 2020 19:09:55 +0100 From: Mark Rutland To: madvenka@linux.microsoft.com Subject: Re: [PATCH v1 0/4] [RFC] Implement Trampoline File Descriptor Message-ID: <20200731180955.GC67415@C02TD0UTHF1T.local> References: <20200728131050.24443-1-madvenka@linux.microsoft.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20200728131050.24443-1-madvenka@linux.microsoft.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200731_141003_934598_1F8BBA2B X-CRM114-Status: GOOD ( 42.74 ) 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: kernel-hardening@lists.openwall.com, linux-api@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org, oleg@redhat.com, linux-security-module@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-integrity@vger.kernel.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 Hi, On Tue, Jul 28, 2020 at 08:10:46AM -0500, madvenka@linux.microsoft.com wrote: > From: "Madhavan T. Venkataraman" > Trampoline code is placed either in a data page or in a stack page. In > order to execute a trampoline, the page it resides in needs to be mapped > with execute permissions. Writable pages with execute permissions provide > an attack surface for hackers. Attackers can use this to inject malicious > code, modify existing code or do other harm. For the purpose of below, IIUC this assumes the adversary has an arbitrary write. > To mitigate this, LSMs such as SELinux may not allow pages to have both > write and execute permissions. This prevents trampolines from executing > and blocks applications that use trampolines. To allow genuine applications > to run, exceptions have to be made for them (by setting execmem, etc). > In this case, the attack surface is just the pages of such applications. > > An application that is not allowed to have writable executable pages > may try to load trampoline code into a file and map the file with execute > permissions. In this case, the attack surface is just the buffer that > contains trampoline code. However, a successful exploit may provide the > hacker with means to load his own code in a file, map it and execute it. It's not clear to me what power the adversary is assumed to have here, and consequently it's not clear to me how the proposal mitigates this. For example, if the attack can control the arguments to syscalls, and has an arbitrary write as above, what prevents them from creating a trampfd of their own? [...] > GCC has traditionally used trampolines for implementing nested > functions. The trampoline is placed on the user stack. So, the stack > needs to be executable. IIUC generally nested functions are avoided these days, specifically to prevent the creation of gadgets on the stack. So I don't think those are relevant as a cased to care about. Applications using them should move to not using them, and would be more secure generally for doing so. [...] > Trampoline File Descriptor (trampfd) > -------------------------- > > I am proposing a kernel API using anonymous file descriptors that > can be used to create and execute trampolines with the help of the > kernel. In this solution also, the kernel does the work of the trampoline. What's the rationale for the kernel emulating the trampoline here? In ther case of EMUTRAMP this was necessary to work with existing application binaries and kernel ABIs which placed instructions onto the stack, and the stack needed to remain RW for other reasons. That restriction doesn't apply here. Assuming trampfd creation is somehow authenticated, the code could be placed in a r-x page (which the kernel could refuse to add write permission), in order to prevent modification. If that's sufficient, it's not much of a leap to allow userspace to generate the code. > The kernel creates the trampoline mapping without any permissions. When > the trampoline is executed by user code, a page fault happens and the > kernel gets control. The kernel recognizes that this is a trampoline > invocation. It sets up the user registers based on the specified > register context, and/or pushes values on the user stack based on the > specified stack context, and sets the user PC to the requested target > PC. When the kernel returns, execution continues at the target PC. > So, the kernel does the work of the trampoline on behalf of the > application. > > In this case, the attack surface is the context buffer. A hacker may > attack an application with a vulnerability and may be able to modify the > context buffer. So, when the register or stack context is set for > a trampoline, the values may have been tampered with. From an attack > surface perspective, this is similar to Trampoline Emulation. But > with trampfd, user code can retrieve a trampoline's context from the > kernel and add defensive checks to see if the context has been > tampered with. Can you elaborate on this: what sort of checks would be applied, and how? Why is this not possible in a r-x user page? [...] > - trampfd provides a basic framework. In the future, new trampoline types > can be implemented, new contexts can be defined, and additional rules > can be implemented for security purposes. >From a kernel developer perspective, this reads as "this ABI will become more complex", which I think is worrisome. I'm also worried that this is liable to have nasty interaction with HW CFI mechanisms (e.g. PAC+BTI on arm64) either now or in future, and that we bake incompatibility into ABI. > - For instance, trampfd defines an "Allowed PCs" context in this initial > work. As an example, libffi can create a read-only array of all ABI > handlers for an architecture at build time. This array can be used to > set the list of allowed PCs for a trampoline. This will mean that a hacker > cannot hack the PC part of the register context and make it point to > arbitrary locations. I'm not exactly sure what's meant here. Do you mean that this prevents userspace from branching into the middle of a trampoline, or that the trampfd code prevents where the trampoline itself can branch to? Both x86 and arm64 have upcoming HW CFI (CET and BTI) to deal with the former, and I believe the latter can also be implemented in userspace with defensive checks in the trampolines, provided that they are protected read-only. > - An SELinux setting called "exectramp" can be implemented along the > lines of "execmem", "execstack" and "execheap" to selectively allow the > use of trampolines on a per application basis. > > - User code can add defensive checks in the code before invoking a > trampoline to make sure that a hacker has not modified the context data. > It can do this by getting the trampoline context from the kernel and > double checking it. As above, without examples it's not clear to me what sort of chacks are possible nor where they wouild need to be made. So it's difficult to see whether that's actually possible or subject to TOCTTOU races and similar. > - In the future, if the kernel can be enhanced to use a safe code > generation component, that code can be placed in the trampoline mapping > pages. Then, the trampoline invocation does not have to incur a trip > into the kernel. > > - Also, if the kernel can be enhanced to use a safe code generation > component, other forms of dynamic code such as JIT code can be > addressed by the trampfd framework. I don't see why it's necessary for the kernel to generate code at all. If the trampfd creation requests can be trusted, what prevents trusting a sealed set of instructions generated in userspace? > - Trampolines can be shared across processes which can give rise to > interesting uses in the future. This sounds like the use-case of a sealed memfd. Is a sealed executable memfd not sufficient? Thanks, Mark. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel