From patchwork Mon Jun 12 16:56:57 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Salvatore Mesoraca X-Patchwork-Id: 9782339 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 2A19660244 for ; Mon, 12 Jun 2017 16:59:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 197E9284C7 for ; Mon, 12 Jun 2017 16:59:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0E0FB284DA; Mon, 12 Jun 2017 16:59:52 +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=unavailable 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 9BAFB284C7 for ; Mon, 12 Jun 2017 16:59:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754073AbdFLQ6f (ORCPT ); Mon, 12 Jun 2017 12:58:35 -0400 Received: from mail-wr0-f195.google.com ([209.85.128.195]:32983 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752018AbdFLQ6c (ORCPT ); Mon, 12 Jun 2017 12:58:32 -0400 Received: by mail-wr0-f195.google.com with SMTP id v104so23234275wrb.0; Mon, 12 Jun 2017 09:58:30 -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=ozaTRf8O0a2gZKmcjEiVXWaqvXZCFFu/Pfw+zPHhftI=; b=VEjXGxlfrP+O+3mdurR5jUnlEzqmPUSLZB3jOJDd28Geu/bCXLcy/lnbhDW5M9biHw xbKP3MD9kobRKneW/wDo8PhByXHFvO2LxP+gMhCxNkvMuGYNxRi/xUh9XFoY0ZxZgoRt 0B/JhNjGuOcGox5CQAKvcGhK0YVwIFSFX2an7moVPyjUb0MPayN2lbYtkvCBaGsBajY5 gAcoqAlOSz7O489PnfCf9G8nnAQEU2ztfzd2Lb8Y5iUE3udphVfe+gER5BKhH1C4A36z aXRJmz2eMsVEx40HGzfydnY61xwyIAYZgXc6Lghh82E33dALQm4R3t88HntyTf8l7R4I J7KA== 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=ozaTRf8O0a2gZKmcjEiVXWaqvXZCFFu/Pfw+zPHhftI=; b=L9CLD7mtoGzZ5BtZRuzUSUl9p3rqwdWwkcKU1sC9cO6qkNkIZYS5s4tg3B+TVsJMr8 Kq28KEYP7LV1IYZrHChNrhphL8XZb06sBM6jJoDvltCev7V6CcMieoVlMeh2n9bitlju SeJuAwj1uJCMuM/oOPcucIKPi1Jd4wg6MspYosZ7dijjMCaJ2uPHsC9pbs/al6PUVZEp fh4xOq7KGHbnHsMEUx4dK3iqSt7hvR6G8QeVyKV0UGsmRIdX4Auwbcy7Go8IOR/uwJqU V/BYojcR4qV9u3VfquX71EeMe08aMF6yUM9MWmVq74JbMPDEDsxu13fa9+POAilpi3hf 0nHg== X-Gm-Message-State: AODbwcCiRiMoxB/S2fHijB4xaMPEESMgIRsjzc9HqtFmcUgfQOIbP2aH VMVCLhR1G6jZAOJn05s4mSnr X-Received: by 10.223.163.27 with SMTP id c27mr8023393wrb.85.1497286704103; Mon, 12 Jun 2017 09:58:24 -0700 (PDT) Received: from localhost ([109.112.0.253]) by smtp.gmail.com with ESMTPSA id n2sm20925973wrn.30.2017.06.12.09.58.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 12 Jun 2017 09:58:23 -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" , x86@kernel.org, Ingo Molnar Subject: [PATCH 08/11] Creation of "pagefault_handler_x86" LSM hook Date: Mon, 12 Jun 2017 18:56:57 +0200 Message-Id: <1497286620-15027-9-git-send-email-s.mesoraca16@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1497286620-15027-1-git-send-email-s.mesoraca16@gmail.com> References: <1497286620-15027-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. Signed-off-by: Salvatore Mesoraca Cc: x86@kernel.org Cc: Ingo Molnar --- 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 6934cc5..a42c2f8 100644 --- a/include/linux/lsm_hooks.h +++ b/include/linux/lsm_hooks.h @@ -487,6 +487,11 @@ * Check if the requested @vmflags are allowed. * @vmflags contains requested the vmflags. * Return 0 if the operation is allowed to continue. + * @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. @@ -1487,6 +1492,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); @@ -1759,6 +1767,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 67e33b6..bc38c83 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -303,6 +303,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); @@ -836,6 +839,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 25d58f0..cf15686 100644 --- a/security/security.c +++ b/security/security.c @@ -910,6 +910,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);