From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by sourceware.org (Postfix) with ESMTP id D7732389803B for ; Tue, 24 Nov 2020 19:32:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D7732389803B Received: from microsoft-linux.home (unknown [47.187.219.45]) by linux.microsoft.com (Postfix) with ESMTPSA id 1089C20B717A; Tue, 24 Nov 2020 11:32:14 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 1089C20B717A From: madvenka@linux.microsoft.com To: libffi-discuss@sourceware.org Cc: fw@deneb.enyo.de, dj@redhat.com, madvenka@linux.microsoft.com Subject: [RFC PATCH v1 0/4] Libffi Static Trampolines Date: Tue, 24 Nov 2020 13:32:02 -0600 Message-Id: <20201124193206.10289-1-madvenka@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <9bd94fd78a3c8f638b8a0d2269258da99d58e70f> References: <9bd94fd78a3c8f638b8a0d2269258da99d58e70f> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-19.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, ENV_AND_HDR_SPF_MATCH, KAM_ASCII_DIVIDERS, SPF_HELO_PASS, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libffi-discuss@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libffi-discuss mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 24 Nov 2020 19:32:20 -0000 From: "Madhavan T. Venkataraman" Closures ======== Libffi supports a feature called "Closures". Closures enable a program to call a function whose arguments, argument types and return value are known only at runtime. Also, the calling conventions of the called function can be different from native calling conventions. Closures support a variety of architectures and Application Binary Interfaces or ABIs. The calling conventions are defined in the ABI. Closure Trampoline ================== When a program invokes a closure, a libffi supplied trampoline is executed. The trampoline loads the closure pointer in a designated register or on the stack (depending on the ABI of the target function) and jumps to an ABI handler. The ABI handler extracts arguments from the closure, calls the target function and returns the function's return value in the native ABI. To the program, it appears as if the target function was called natively. Security Issue ============== Currently, the trampoline code used in libffi is not statically defined in a source file (except for MACH). The trampoline is either pre-defined machine code in a data buffer. Or, it is generated at runtime. In order to execute a trampoline, it needs to be placed in a page with executable permissions. Executable data pages are attack surfaces for attackers who may try to inject their own code into the page and contrive to have it executed. The security settings in a system may prevent various tricks used in user land to write code into a page and to have it executed somehow. On such systems, libffi trampolines would not be able to run. NOTE: The ABI handlers are all statically defined. Static Trampoline ================= To solve this problem, the trampoline code needs to be defined statically in a source file, compiled and placed in the text segment so it can be mapped and executed naturally without any tricks. However, the trampoline needs to be able to access the closure pointer at runtime. PC-relative data referencing ============================ The solution implemented in this patch set uses PC-relative data references. The trampoline is mapped in a code page. Adjacent to the code page, a data page is mapped that contains the parameters of the trampoline: - the closure pointer - pointer to the ABI handler to jump to The trampoline code uses an offset relative to its current PC to access its data. Some architectures support PC-relative data references in the ISA itself. E.g., X64 supports RIP-relative references. For others, the PC has to somehow be loaded into a general purpose register to do PC-relative data referencing. To do this, we need to define a get_pc() kind of function and call it to load the PC in a desired register. There are two cases: 1. The call instruction pushes the return address on the stack. In this case, get_pc() will extract the return address from the stack and load it in the desired register and return. 2. The call instruction stores the return address in a designated register. In this case, get_pc() will copy the return address to the desired register and return. Either way, the PC next to the call instruction is obtained. Scratch register ================ In order to do its job, the trampoline code would be required to use a scratch register. Depending on the ABI, there may not be a register available for scratch. This problem needs to be solved so that all ABIs will work. The trampoline will save two values on the stack: - the closure pointer - the original value of the scratch register This is what the stack will look like: sp before trampoline ------> -------------------- | closure pointer | -------------------- | scratch register | sp after trampoline -------> -------------------- The ABI handler can do the following as needed by the ABI: - the closure pointer can be loaded in a desired register - the scratch register can be restored to its original value - the stack pointer can be restored to its original value (when the trampoline was invoked) Thus the ABI handlers will have a couple of lines of code at the very beginning to do this so that all ABIs will work. NOTE: The documentation for this feature will contain information on: - the name of the scratch register for each architecture - the stack offsets at which the closure and the scratch register will be copied Trampoline Table ================ In order to reduce the trampoline memory footprint, the trampoline code would be defined as a code array in the text segment. This array would be mapped into the address space of the caller. The mapping would, therefore, contain a trampoline table. Adjacent to the trampoline table, there will be a data mapping that contains a parameter table, one parameter block for each trampoline. The parameter table will contain: - a pointer to the closure - a pointer to the ABI handler The trampoline code would finally look like this: - Make space on the stack for the closure and the scratch register by moving the stack pointer down - Store the original value of the scratch register on the stack - Using PC-relative reference, get the closure pointer - Store the closure pointer on the stack - Using PC-relative reference, get the ABI handler pointer - Jump to the ABI handler Trampoline API ============== There is a lot of dynamic code out there. They all have the same security issue. Dynamic code can be re-written into static code provided the data required by the static code can be passed to it just like we pass the closure pointer to an ABI handler. So, the same trampoline functions used by libffi internally need to be made available to the rest of the world in the form of an API. The following API has been defined in this solution: int ffi_tramp_is_supported(void); To support static trampolines, code needs to be added to each architecture. Also, the feature itself can be enabled via a configuration option. So, this function tells us if the feature is supported and enabled in the current libffi or not. void *ffi_tramp_alloc (int flags); Allocate a trampoline. Currently, flags are zero. An opaque trampoline structure pointer is returned. Internally, libffi manages trampoline tables and individual trampolines in each table. int ffi_tramp_set_parms (void *tramp, void *target, void *data); Initialize the parameters of a trampoline. That is, the target code that the trampoline should jump to and the data that needs to be passed to the target code. void *ffi_tramp_get_addr (void *tramp); Return the address of the trampoline to invoke the trampoline with. The trampoline can be invoked in one of two ways: - Simply branch to the trampoline address - Treat the trampoline address as a function pointer and call it. Which method is used depends on the target code. void ffi_tramp_free (void *tramp); Free a trampoline. Testing ======= The libffi selftests have been run successfully on X86 and ARM, 32-bit and 64-bit. I also have my own API test that does stress testing of the API. TBD === I need to study how to include my trampoline API test in the libffi selftests. Signed-off-by: Madhavan T. Venkataraman Madhavan T. Venkataraman (4): Libffi Static Trampolines x86: Support for Static Trampolines aarch64: Support for Static Trampolines arm: Support for Static Trampolines Makefile.am | 3 +- configure.ac | 7 + include/ffi.h.in | 13 +- include/ffi_common.h | 4 + libffi.map.in | 11 + src/aarch64/ffi.c | 16 + src/aarch64/internal.h | 10 + src/aarch64/sysv.S | 46 ++ src/arm/ffi.c | 16 + src/arm/internal.h | 10 + src/arm/sysv.S | 37 ++ src/closures.c | 54 +- src/tramp.c | 563 ++++++++++++++++++++ src/x86/ffi.c | 17 + src/x86/ffi64.c | 19 +- src/x86/ffiw64.c | 8 +- src/x86/internal.h | 10 + src/x86/internal64.h | 10 + src/x86/sysv.S | 52 ++ src/x86/unix64.S | 48 ++ src/x86/win64.S | 4 + testsuite/libffi.closures/closure_loc_fn0.c | 3 + 22 files changed, 952 insertions(+), 9 deletions(-) create mode 100644 src/tramp.c -- 2.25.1