From patchwork Mon Sep 11 19:50:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Salvatore Mesoraca X-Patchwork-Id: 9948025 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 05302602C9 for ; Mon, 11 Sep 2017 19:53:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F193A28D34 for ; Mon, 11 Sep 2017 19:53:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E66AB28D38; Mon, 11 Sep 2017 19:53:39 +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.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, 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 80F9D28D34 for ; Mon, 11 Sep 2017 19:53:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751424AbdIKTvo (ORCPT ); Mon, 11 Sep 2017 15:51:44 -0400 Received: from mail-wm0-f67.google.com ([74.125.82.67]:33486 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751022AbdIKTvm (ORCPT ); Mon, 11 Sep 2017 15:51:42 -0400 Received: by mail-wm0-f67.google.com with SMTP id 187so7728084wmn.0; Mon, 11 Sep 2017 12:51:41 -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=ZyAP5S0tsli4ecbyanLWGjsrumWLQadgheG1O4wkNs4=; b=QPYQ/qEHXlWAm+8DPeVT5omqpEoEYrF3E01fMnA3qUe0rSFedyKZ5V9RUqmQeSfjgF 88XCDGAwFzJNRFQjIwsFhXWbEodjOKzgMJNPovo6FG6JVTSpCYtyZmopMbrs24JwAaz3 XriNfvTQCfH7C2KWJu/t1gF/VVpc62b928eOk24odTu/LYNbILDOo1kVKwGn9HNxGEER ww89dVDX+4tsulSkzD45dA+zV15AHfXRDHr1jURSuMvYCGaAi7X6B2NmgAk3Bt6NfWJ0 A4GpuO1eUp5M8uu3qpscJcO0CXHwQEOIbdW+0mRCrwWttcXx6CwlfcOygBGzjXT5cx1l nSkA== 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=ZyAP5S0tsli4ecbyanLWGjsrumWLQadgheG1O4wkNs4=; b=EKMPWoEClkNFVZs4Uu0ChQ21yVk7morCrV4pu7ZhVe30kp4mrI9Ki5GI8O9rJY8ETW o6289r6fY1nFhqi22qzHaE0Fk6qMJm9dLQS7HpjHrT9Elw6GuZcqNb7Uh1gmU34Wzy3M Czpn126Wof3tZoosdgeuo9PVPgJymkd60FVmS9Zu0yrJLd4QasKfVIfY1WVupHNVYPol q0U429aOLh/U7JFCPdhZNHXbi43cVpquF98K8w7lTMqvENWCpK25YXFYrHoexY3La/rV jbZOMyT3aFThc8wdtEJrayeqUg3s7TLXchz8lYJMWhWMUXfK4InhckYXGItoJWXnh4gq 05yg== X-Gm-Message-State: AHPjjUhx4FOTnGQmO1QHdGYAyZ6i1Yxzv8T9GDoISMS3NFy1Oa/S3wJL dc5xpKXGbaVKUAc+sTVIkc3vEq3WlAg= X-Google-Smtp-Source: AOwi7QAi0Moq0XLYDDLs9MN1tlmMWoKPRKor6ru4+xRjoSqADvbgl3mHctbNQoRzi1Sr5GJRdunsIQ== X-Received: by 10.28.10.132 with SMTP id 126mr3789037wmk.111.1505159500947; Mon, 11 Sep 2017 12:51:40 -0700 (PDT) Received: from localhost ([93.66.104.212]) by smtp.gmail.com with ESMTPSA id a39sm14663448wrc.48.2017.09.11.12.51.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 11 Sep 2017 12:51:40 -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" Subject: [RFC v3 3/9] Creation of "check_vmflags" LSM hook Date: Mon, 11 Sep 2017 21:50:21 +0200 Message-Id: <1505159427-11747-4-git-send-email-s.mesoraca16@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1505159427-11747-1-git-send-email-s.mesoraca16@gmail.com> References: <1505159427-11747-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 LSM hook to check if a given configuration of vmflags, for a new memory allocation request, should be allowed or not. It's placed in "do_mmap", "do_brk_flags" and "__install_special_mapping". Signed-off-by: Salvatore Mesoraca --- include/linux/lsm_hooks.h | 7 +++++++ include/linux/security.h | 6 ++++++ mm/mmap.c | 13 +++++++++++++ security/security.c | 5 +++++ 4 files changed, 31 insertions(+) diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h index c5a0322..2fb5c51f 100644 --- a/include/linux/lsm_hooks.h +++ b/include/linux/lsm_hooks.h @@ -484,6 +484,11 @@ * @reqprot contains the protection requested by the application. * @prot contains the protection that will be applied by the kernel. * Return 0 if permission is granted. + * @check_vmflags: + * Check if the requested @vmflags are allowed. + * @vmflags contains the requested vmflags. + * Return 0 if the operation is allowed to continue otherwise return + * the appropriate error code. * @file_lock: * Check permission before performing file locking operations. * Note: this hook mediates both flock and fcntl style locks. @@ -1496,6 +1501,7 @@ unsigned long prot, unsigned long flags); int (*file_mprotect)(struct vm_area_struct *vma, unsigned long reqprot, unsigned long prot); + int (*check_vmflags)(vm_flags_t vmflags); int (*file_lock)(struct file *file, unsigned int cmd); int (*file_fcntl)(struct file *file, unsigned int cmd, unsigned long arg); @@ -1773,6 +1779,7 @@ struct security_hook_heads { struct list_head mmap_addr; struct list_head mmap_file; struct list_head file_mprotect; + struct list_head check_vmflags; 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 974bb9b..59840a5 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -309,6 +309,7 @@ int security_mmap_file(struct file *file, unsigned long prot, int security_mmap_addr(unsigned long addr); int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot, unsigned long prot); +int security_check_vmflags(vm_flags_t vmflags); 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); @@ -844,6 +845,11 @@ static inline int security_file_mprotect(struct vm_area_struct *vma, return 0; } +static inline int security_check_vmflags(vm_flags_t vmflags) +{ + return 0; +} + static inline int security_file_lock(struct file *file, unsigned int cmd) { return 0; diff --git a/mm/mmap.c b/mm/mmap.c index 680506f..44bb1a3 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1326,6 +1326,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr, { struct mm_struct *mm = current->mm; int pkey = 0; + int error; *populate = 0; @@ -1378,6 +1379,10 @@ unsigned long do_mmap(struct file *file, unsigned long addr, vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; + error = security_check_vmflags(vm_flags); + if (error) + return error; + if (flags & MAP_LOCKED) if (!can_do_mlock()) return -EPERM; @@ -2873,6 +2878,10 @@ static int do_brk_flags(unsigned long addr, unsigned long request, unsigned long return -EINVAL; flags |= VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags; + error = security_check_vmflags(flags); + if (error) + return error; + error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED); if (offset_in_page(error)) return error; @@ -3269,6 +3278,10 @@ static struct vm_area_struct *__install_special_mapping( int ret; struct vm_area_struct *vma; + ret = security_check_vmflags(vm_flags); + if (ret) + return ERR_PTR(ret); + vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL); if (unlikely(vma == NULL)) return ERR_PTR(-ENOMEM); diff --git a/security/security.c b/security/security.c index d482e3d..484143f 100644 --- a/security/security.c +++ b/security/security.c @@ -938,6 +938,11 @@ int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot, return call_int_hook(file_mprotect, 0, vma, reqprot, prot); } +int security_check_vmflags(vm_flags_t vmflags) +{ + return call_int_hook(check_vmflags, 0, vmflags); +} + int security_file_lock(struct file *file, unsigned int cmd) { return call_int_hook(file_lock, 0, file, cmd);