From patchwork Thu Jun 15 16:42:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Salvatore Mesoraca X-Patchwork-Id: 9789321 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 ADF1060384 for ; Thu, 15 Jun 2017 16:44:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9FF3427F7F for ; Thu, 15 Jun 2017 16:44:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 94D2F2852E; Thu, 15 Jun 2017 16:44:55 +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 7FA8B27F7F for ; Thu, 15 Jun 2017 16:44:54 +0000 (UTC) Received: (qmail 25899 invoked by uid 550); 15 Jun 2017 16:44:49 -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 24396 invoked from network); 15 Jun 2017 16:44:44 -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=+Tg1CEkD7pcmfNq7b+BgqJU2zWhP7GH3o4KtS9kcKqY=; b=NUtEwQ4Hc2Jpp9ibh1EA/6WHquSVihM0FAtisoT0a3heYaqjPIajUnLisgzg2R4S1N 60X1SsFEhFQ0+ZWPbrsFVEbM6tFLdrA8usFdYWwADQoqWgsXVv+a1gDK8SOMoMXDDiCq dN10XC1QalZxDEJd7C7dZMm2jcs5jotydrjkibtyCr455Mr5mFSecJfxvryrm5Dw+HaZ U4ik7g28Fdqgo41pseeESgE6p2YUr2VgsA6dQ9Z4SLuqdFuEpWDTZMWkquuBmF3mFUBZ VN8Sy6CZIeCtWW3kpiShtDgsBZM579a6Sw1L6atnz+MfOphuLgmbl8IXlcYN+frK+ker /s2Q== 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=+Tg1CEkD7pcmfNq7b+BgqJU2zWhP7GH3o4KtS9kcKqY=; b=KdFdU/0LX+h2/41ECyRpBWmbyiX5PpNKbpAHpfJjqfTqMKqn2VlbAtTp6uJWeQp+ab hGixZl3/ifqQEdT2Jc1YpN3TBSBxDgtkgpMGQAwHLD9Xw6rlZ46RntnuJ7jxLCDgtzZQ m7IQ1n+vhzMyk3zIbVMenE7BzgFiudct9dOUMJXS2nbt7bs2+zg3IEn4CVuXTEHNq6LS ciIHr2qFxZ9S+zX3ir970xP41rwKsOLWScJ20CkWEc7+3dJWI2ipZxVJ1h6fSS5Qdsfe Ef1jspDepmn9wFwQG+F5L35+ibswWVv8aXtAhSU5kkUV/DtqR7ydWJTdD8N3HB0m9Mkv 3GsQ== X-Gm-Message-State: AKS2vOw/i9Br6u5KTnvb4/6JNTkMsdCkPhbldqg672esGvZJrbMpaSUe L1UxOZVlMvoClg== X-Received: by 10.223.148.195 with SMTP id 61mr4586989wrr.28.1497545072987; Thu, 15 Jun 2017 09:44:32 -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 Date: Thu, 15 Jun 2017 18:42:50 +0200 Message-Id: <1497544976-7856-4-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> Subject: [kernel-hardening] [RFC v2 3/9] Creation of "check_vmflags" LSM hook 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 bd274db..33dab16 100644 --- a/include/linux/lsm_hooks.h +++ b/include/linux/lsm_hooks.h @@ -483,6 +483,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 requested the 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. @@ -1477,6 +1482,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); @@ -1747,6 +1753,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 af675b5..8701872 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -300,6 +300,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); @@ -823,6 +824,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 f82741e..132061b 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1311,6 +1311,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr, { struct mm_struct *mm = current->mm; int pkey = 0; + int error; *populate = 0; @@ -1363,6 +1364,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; @@ -2833,6 +2838,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; @@ -3208,6 +3217,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 42c8028..7e45846 100644 --- a/security/security.c +++ b/security/security.c @@ -900,6 +900,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);