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: 9782319 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 A00E460244 for ; Mon, 12 Jun 2017 16:59:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 90B63284C7 for ; Mon, 12 Jun 2017 16:59:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 85458284DB; Mon, 12 Jun 2017 16:59:01 +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=-4.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 95FFF284C7 for ; Mon, 12 Jun 2017 16:58:59 +0000 (UTC) Received: (qmail 8068 invoked by uid 550); 12 Jun 2017 16:58:37 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 7948 invoked from network); 12 Jun 2017 16:58:35 -0000 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=akFR2dH59YDNYpLeY6Xwx/jON/qG9sK0aI+pn4cYcjFQqVaTZAV0Rfi5pTNi34txk/ 4XcwqIGDhEnuYmg95bjqyxielp9JEY3PelUjoP5sdZSJtxGwdtu8wpJhOsEmODn/aLZV CqS/e3XyzpiyuI0wG6FG0L07wS3OlqrhEPPeX6s/xXGdmb7ao1EcabINYUMDlKG1M3qJ mw4A6T0Y5Vbewd3kIjGjofiKdhQf+TH1mUm98yzGfwEc22/HfxizsptBr49uLlQF+9sB yPUPTdEUVdqgqZlHGjpHCiu2MNljr5QbbNqr1DrySGJZvmIKwwgb+7vlxUUNFW1cgnbV CJhQ== X-Gm-Message-State: AODbwcAlAujq9kmFDRRt7+BgnbfQkdEm3deasOaUvsxEjyr9Eu0Ldo5O xO4SFaBlVOeufg== X-Received: by 10.223.163.27 with SMTP id c27mr8023393wrb.85.1497286704103; Mon, 12 Jun 2017 09:58:24 -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 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> Subject: [kernel-hardening] [PATCH 08/11] Creation of "pagefault_handler_x86" LSM hook 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);