From patchwork Thu Jun 15 16:42:53 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Salvatore Mesoraca X-Patchwork-Id: 9789345 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 8793760384 for ; Thu, 15 Jun 2017 16:45:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 795C027528 for ; Thu, 15 Jun 2017 16:45:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6E2B427F17; Thu, 15 Jun 2017 16:45:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E1B9F27528 for ; Thu, 15 Jun 2017 16:45:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752352AbdFOQpd (ORCPT ); Thu, 15 Jun 2017 12:45:33 -0400 Received: from mail-wr0-f193.google.com ([209.85.128.193]:34910 "EHLO mail-wr0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752542AbdFOQoj (ORCPT ); Thu, 15 Jun 2017 12:44:39 -0400 Received: by mail-wr0-f193.google.com with SMTP id z45so4286459wrb.2; Thu, 15 Jun 2017 09:44:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=8wngdlmIde2iGLd+VkvqdwbU2QDF6BoDd5kEduAIAvE=; b=WXqrJ91a11T8Bl3n+88bWwRnzmldI6KHqED2B0aqtUbAuSTaEsdiTHRzsdWhPII/R+ Vl+mNtwGDqEUfFp0OOxpOgOUe+BElFizY99bHY6rT3A6lfxDTsqohi1rI2cr89ytN4Yk 340xyWSpOEmYZraf/HJ14P6JmesWrVmsDtDqVFR4snVHpsz/JhorCilCMwZ4mGbcM4Qa F4jlDx3uOtM4rxWBGjYfb03OHtVPdQOna7k3EDZik3v76yE88XKLFCDz3boB7BNACqZ5 47azPs4S781gjmqC2XcvUvij5dGUl1homluGWPIourGHiWR/c9aXpxJp38Q1K7pJ+1sA lpkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=8wngdlmIde2iGLd+VkvqdwbU2QDF6BoDd5kEduAIAvE=; b=e+6vs4nvEd+mn4QIcLGNojzfchOO+CwoiZJNIvqKJjYECyLhu3v2E7WBMPrsWtDNF/ QFxudRv8pxQrMbYapb2PnGvkaLq8ZCHLvc/uJGQI6XMdqGncXrSwM0gWJ9/C4F2g4lFD 7uiK7n9FMV5tbD5BTCHkpdMulWkNZwZqSDSGmY7BTAsEFBVucRr8BMPL1uxFJZaJvWpb bAgGO1O2GpdPPnUcQCjU8wuiNWJHtWBtFf4hm1daYKnO5TWxUQnubcA+EnKC7DsdpeBQ SiTSJfrm/c/Y6H4xiHzJycJLIO3PAJeUPVe9JBa4wlG054ztLvH7rL0nQf4E/SskG5qb 6xaQ== X-Gm-Message-State: AKS2vOy+dydn+y9xRqOtuIV8g9dExpyomBfpI7gqAhWW5S7NNYsIKNtK 089qOhL/VNX001fw7v29UEon X-Received: by 10.223.147.98 with SMTP id 89mr4060728wro.103.1497545077949; Thu, 15 Jun 2017 09:44:37 -0700 (PDT) Received: from localhost ([37.176.236.94]) by smtp.gmail.com with ESMTPSA id k19sm615938wmg.9.2017.06.15.09.44.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 15 Jun 2017 09:44:37 -0700 (PDT) From: Salvatore Mesoraca To: linux-kernel@vger.kernel.org Cc: linux-security-module@vger.kernel.org, kernel-hardening@lists.openwall.com, Salvatore Mesoraca , Brad Spengler , PaX Team , Casey Schaufler , Kees Cook , James Morris , "Serge E. Hallyn" , linux-mm@kvack.org, x86@kernel.org, Jann Horn , Christoph Hellwig , Thomas Gleixner Subject: [RFC v2 6/9] Creation of "pagefault_handler_x86" LSM hook Date: Thu, 15 Jun 2017 18:42:53 +0200 Message-Id: <1497544976-7856-7-git-send-email-s.mesoraca16@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1497544976-7856-1-git-send-email-s.mesoraca16@gmail.com> References: <1497544976-7856-1-git-send-email-s.mesoraca16@gmail.com> Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Creation of a new hook to let LSM modules handle user-space pagefaults on x86. It can be used to avoid segfaulting the originating process. If it's the case it can modify process registers before returning. This is not a security feature by itself, it's a way to soften some unwanted side-effects of restrictive security features. In particular this is used by S.A.R.A. can be used to implement what PaX call "trampoline emulation" that, in practice, allow for some specific code sequences to be executed even if they are in non executable memory. This may look like a bad thing at first, but you have to consider that: - This allows for strict memory restrictions (e.g. W^X) to stay on even when they should be turned off. And, even if this emulation makes those features less effective, it's still better than having them turned off completely. - The only code sequences emulated are trampolines used to make function calls. In many cases, when you have the chance to make arbitrary memory writes, you can already manipulate the control flow of the program by overwriting function pointers or return values. So, in many cases, the "trampoline emulation" doesn't introduce new exploit vectors. - It's a feature that can be turned on only if needed, on a per executable file basis. Signed-off-by: Salvatore Mesoraca --- arch/x86/mm/fault.c | 6 ++++++ include/linux/lsm_hooks.h | 9 +++++++++ include/linux/security.h | 11 +++++++++++ security/security.c | 11 +++++++++++ 4 files changed, 37 insertions(+) diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index 8ad91a0..b75b81a 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -15,6 +15,7 @@ #include /* prefetchw */ #include /* exception_enter(), ... */ #include /* faulthandler_disabled() */ +#include /* security_pagefault_handler */ #include /* boot_cpu_has, ... */ #include /* dotraplinkage, ... */ @@ -1358,6 +1359,11 @@ static inline bool smap_violation(int error_code, struct pt_regs *regs) local_irq_enable(); } + if (unlikely(security_pagefault_handler_x86(regs, + error_code, + address))) + return; + perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); if (error_code & PF_WRITE) diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h index 33dab16..da487e5 100644 --- a/include/linux/lsm_hooks.h +++ b/include/linux/lsm_hooks.h @@ -488,6 +488,11 @@ * @vmflags contains requested the vmflags. * Return 0 if the operation is allowed to continue otherwise return * the appropriate error code. + * @pagefault_handler_x86: + * Handle pagefaults on x86. + * @regs contains process' registers. + * @error_code contains error code for the pagefault. + * @address contains the address that caused the pagefault. * @file_lock: * Check permission before performing file locking operations. * Note: this hook mediates both flock and fcntl style locks. @@ -1483,6 +1488,9 @@ int (*file_mprotect)(struct vm_area_struct *vma, unsigned long reqprot, unsigned long prot); int (*check_vmflags)(vm_flags_t vmflags); + int (*pagefault_handler_x86)(struct pt_regs *regs, + unsigned long error_code, + unsigned long address); int (*file_lock)(struct file *file, unsigned int cmd); int (*file_fcntl)(struct file *file, unsigned int cmd, unsigned long arg); @@ -1754,6 +1762,7 @@ struct security_hook_heads { struct list_head mmap_file; struct list_head file_mprotect; struct list_head check_vmflags; + struct list_head pagefault_handler_x86; struct list_head file_lock; struct list_head file_fcntl; struct list_head file_set_fowner; diff --git a/include/linux/security.h b/include/linux/security.h index 8701872..3b91999 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -301,6 +301,9 @@ int security_mmap_file(struct file *file, unsigned long prot, int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot, unsigned long prot); int security_check_vmflags(vm_flags_t vmflags); +int __maybe_unused security_pagefault_handler_x86(struct pt_regs *regs, + unsigned long error_code, + unsigned long address); int security_file_lock(struct file *file, unsigned int cmd); int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg); void security_file_set_fowner(struct file *file); @@ -829,6 +832,14 @@ static inline int security_check_vmflags(vm_flags_t vmflags) return 0; } +static inline int __maybe_unused security_pagefault_handler_x86( + struct pt_regs *regs, + unsigned long error_code, + unsigned long address) +{ + return 0; +} + static inline int security_file_lock(struct file *file, unsigned int cmd) { return 0; diff --git a/security/security.c b/security/security.c index 7e45846..f7df697 100644 --- a/security/security.c +++ b/security/security.c @@ -905,6 +905,17 @@ int security_check_vmflags(vm_flags_t vmflags) return call_int_hook(check_vmflags, 0, vmflags); } +int __maybe_unused security_pagefault_handler_x86(struct pt_regs *regs, + unsigned long error_code, + unsigned long address) +{ + return call_int_hook(pagefault_handler_x86, + 0, + regs, + error_code, + address); +} + int security_file_lock(struct file *file, unsigned int cmd) { return call_int_hook(file_lock, 0, file, cmd);