Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp86291pxk; Tue, 22 Sep 2020 19:45:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwjp6+cu+GuhsnhU+uDMRdv6lzbVW6E7zAE5w0ZwLkz1ak2+GeaQpcNo8/3xMvekl08Hb3M X-Received: by 2002:a50:ab59:: with SMTP id t25mr7028593edc.364.1600829121899; Tue, 22 Sep 2020 19:45:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600829121; cv=none; d=google.com; s=arc-20160816; b=tqfA5E43RuE90NiNPotodym+4WPVXeFHEWf3pXvtaB/82lzbNdrmj4bYisltyKqIEa Lg36n00Nfu7dBBeIcVO3Vxwg/F32HAVUC4Y/3j3oFOCXRnUUQreMBlyOsJ/Gr3PGcH18 JAX1EnTf8pG2cQmJL7Bmnhr7daCcp7dJOd5WLLo1S61uErubQAWkgndcHyqXDwdVNEKG uvdC3kIRvRurL8Lu8BID3XtZOeg7UHpHOOj5eQxxdqzvnPVaw5LQaFSIF8iCA3JDlb8m 6UQAqI9YbSfprmmZFuqV1/4ihBhCNJ2L+z67EAvkx4vMQqxKsq9jnSu/C1jru0yqEOcb 1ZRg== 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-disposition:mime-version :references:message-id:subject:cc:to:date:from:sender:dkim-signature; bh=ssYhrlHCw7Ub9xPyJrNfDQfuZKnG7yNmanf9qk5D/30=; b=pfNrlSm1poRW8+NaUG4v8P+g0h3Bcejn28BS+CWBiPTIYxgtGPf19pWTM3vAjO/N0h ueuzd1Ql7LxcbRKPHLuoUufD1tbK33Ks50wVMFCdJicW61wY87n7NQV7HZncpEbgBhr2 ESSRlcvtoN91//dMHV+Gaw2ZJatTXuiFjPgSKseCOYPXTwJIBVLi4ZSAAp1MgBm6lrpC 4C5lzFyd9mw0UYusE61AvppiXq2WzqiIZhxDihRC4GADBJ+7PP+ysAlj23T6EY1WWwIe zhfPkxoM6BFl/kkQTHsghG08KZ1aUpz/8fFsFZ72LDumJFOfK9WEn9DjmgWSYsWuMb8H ZQqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=uhilotA2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a9si12110715edr.259.2020.09.22.19.44.58; Tue, 22 Sep 2020 19:45:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=uhilotA2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726998AbgIWBqU (ORCPT + 99 others); Tue, 22 Sep 2020 21:46:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726716AbgIWBqU (ORCPT ); Tue, 22 Sep 2020 21:46:20 -0400 Received: from mail-qv1-xf41.google.com (mail-qv1-xf41.google.com [IPv6:2607:f8b0:4864:20::f41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D437C061755; Tue, 22 Sep 2020 18:46:20 -0700 (PDT) Received: by mail-qv1-xf41.google.com with SMTP id z18so10608530qvp.6; Tue, 22 Sep 2020 18:46:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:date:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=ssYhrlHCw7Ub9xPyJrNfDQfuZKnG7yNmanf9qk5D/30=; b=uhilotA2RIu/i3TSt6riD62hHewNX0HCYmMAm6Yecx7s6wLqAfVdwPC94TV27TBtq4 +0q0YajK8JyGTHcprchBDFv2ayV59vk0oXGyJlBiFXIqPW4j7qGv0v4IRsopqpngRSr1 Sy4HfLiaqVt6AkHeytLRBjGlCNtInHzH2aFM4TGr3Pn3QrgB/tRSBLBkE+o0ihGxfUiX dhbC8CXns3F1amdB7aenJ/Cxc4yWDSvifq4ZHXkuT03S+tFjT5yxE/eAMet3CHs3A7Hk OXdUPZQvllLZfjnidcNs42c0LsQ0538v8PzsgpNJMClW1XGO0YSC62irz5h/fviXUgUI YbEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:date:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to; bh=ssYhrlHCw7Ub9xPyJrNfDQfuZKnG7yNmanf9qk5D/30=; b=N3H6UYz/F43n73Oq3O6ceVIQmEvcsb4CmiFo8SgdK18Dd+qlW/5d46j6nUf38Ld3ef 8kAmw1PTEsB2Xpc6jrLTrkrvDHgtbC1O+bAjpPspQo+z9IIfS8kqcmcDO4xc23QKT4/n WCkpTZne7dBxteu9ByhBJbeifBXJEtCeMtAe98Zw0W5Tr/n8DykBZu6GXLFCdk6nsXo4 q4nquK6gJ0wayAJFmFlFtWx6fqooOBiWpupTxK/Fj2kTdHH3QmklhNoVkR8h/rpbGvMJ 4XMslm1g7dEbD4ZGEsAGCE11AtWJeIo2PfssdELOpd4Z1Cu5LGtG3tWYXBBWNKnA9Afl Yq5w== X-Gm-Message-State: AOAM533WRUwqBoOaNYmGnSDwKK7yFFE2Q21an85szWr1zPTeSaCTdyS0 66xRKz+45aUeHEPY/SZ18y3CQJthSgg= X-Received: by 2002:a0c:cdc4:: with SMTP id a4mr9110819qvn.31.1600825579406; Tue, 22 Sep 2020 18:46:19 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id g4sm13248370qth.30.2020.09.22.18.46.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Sep 2020 18:46:18 -0700 (PDT) Sender: Arvind Sankar From: Arvind Sankar X-Google-Original-From: Arvind Sankar Date: Tue, 22 Sep 2020 21:46:16 -0400 To: "Madhavan T. Venkataraman" Cc: Florian Weimer , kernel-hardening@lists.openwall.com, linux-api@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, oleg@redhat.com, x86@kernel.org, libffi-discuss@sourceware.org, luto@kernel.org, David.Laight@ACULAB.COM, mark.rutland@arm.com, mic@digikod.net, pavel@ucw.cz Subject: Re: [PATCH v2 0/4] [RFC] Implement Trampoline File Descriptor Message-ID: <20200923014616.GA1216401@rani.riverdale.lan> References: <20200916150826.5990-1-madvenka@linux.microsoft.com> <87v9gdz01h.fsf@mid.deneb.enyo.de> <96ea02df-4154-5888-1669-f3beeed60b33@linux.microsoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <96ea02df-4154-5888-1669-f3beeed60b33@linux.microsoft.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Sep 17, 2020 at 10:36:02AM -0500, Madhavan T. Venkataraman wrote: > > > On 9/16/20 8:04 PM, Florian Weimer wrote: > > * madvenka: > > > >> Examples of trampolines > >> ======================= > >> > >> libffi (A Portable Foreign Function Interface Library): > >> > >> libffi allows a user to define functions with an arbitrary list of > >> arguments and return value through a feature called "Closures". > >> Closures use trampolines to jump to ABI handlers that handle calling > >> conventions and call a target function. libffi is used by a lot > >> of different applications. To name a few: > >> > >> - Python > >> - Java > >> - Javascript > >> - Ruby FFI > >> - Lisp > >> - Objective C > > > > libffi does not actually need this. It currently collocates > > trampolines and the data they need on the same page, but that's > > actually unecessary. It's possible to avoid doing this just by > > changing libffi, without any kernel changes. > > > > I think this has already been done for the iOS port. > > > > The trampoline table that has been implemented for the iOS port (MACH) > is based on PC-relative data referencing. That is, the code and data > are placed in adjacent pages so that the code can access the data using > an address relative to the current PC. > > This is an ISA feature that is not supported on all architectures. > > Now, if it is a performance feature, we can include some architectures > and exclude others. But this is a security feature. IMO, we cannot > exclude any architecture even if it is a legacy one as long as Linux > is running on the architecture. So, we need a solution that does > not assume any specific ISA feature. Which ISA does not support PIC objects? You mentioned i386 below, but i386 does support them, it just needs to copy the PC into a GPR first (see below). > > >> The code for trampoline X in the trampoline table is: > >> > >> load &code_table[X], code_reg > >> load (code_reg), code_reg > >> load &data_table[X], data_reg > >> load (data_reg), data_reg > >> jump code_reg > >> > >> The addresses &code_table[X] and &data_table[X] are baked into the > >> trampoline code. So, PC-relative data references are not needed. The user > >> can modify code_table[X] and data_table[X] dynamically. > > > > You can put this code into the libffi shared object and map it from > > there, just like the rest of the libffi code. To get more > > trampolines, you can map the page containing the trampolines multiple > > times, each instance preceded by a separate data page with the control > > information. > > > > If you put the code in the libffi shared object, how do you pass data to > the code at runtime? If the code we are talking about is a function, then > there is an ABI defined way to pass data to the function. But if the > code we are talking about is some arbitrary code such as a trampoline, > there is no ABI defined way to pass data to it except in a couple of > platforms such as HP PA-RISC that have support for function descriptors > in the ABI itself. > > As mentioned before, if the ISA supports PC-relative data references > (e.g., X86 64-bit platforms support RIP-relative data references) > then we can pass data to that code by placing the code and data in > adjacent pages. So, you can implement the trampoline table for X64. > i386 does not support it. > i386 just needs a tiny bit of code to copy the PC into a GPR first, i.e. the trampoline would be: call 1f 1: pop %data_reg movl (code_table + X - 1b)(%data_reg), %code_reg movl (data_table + X - 1b)(%data_reg), %data_reg jmp *(%code_reg) I do not understand the point about passing data at runtime. This trampoline is to achieve exactly that, no? Thanks.