From patchwork Mon Jun 12 16:56:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Salvatore Mesoraca X-Patchwork-Id: 9782347 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 83DC260244 for ; Mon, 12 Jun 2017 17:00:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 730532832D for ; Mon, 12 Jun 2017 17:00:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 67585283F2; Mon, 12 Jun 2017 17:00:21 +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 D9160283C9 for ; Mon, 12 Jun 2017 17:00:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753735AbdFLQ6U (ORCPT ); Mon, 12 Jun 2017 12:58:20 -0400 Received: from mail-wr0-f193.google.com ([209.85.128.193]:35973 "EHLO mail-wr0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754231AbdFLQ6S (ORCPT ); Mon, 12 Jun 2017 12:58:18 -0400 Received: by mail-wr0-f193.google.com with SMTP id e23so23359086wre.3; Mon, 12 Jun 2017 09:58:17 -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=EXyegqNV+YdGaCuwaREB4JgVxfxr2txZwtHVp8f/my0=; b=QrTR2r13efQNx/Oi+rBnlnBQ0+nIkWuJAdhc1ID0EutP2bBoycP1Ri6jipPb9A26s6 ZNxgWKq0eM8OPz4v5sRySqIHdF0rSRomtqA4UE1a0e0rSxM2+R9S/EEl/FCkDwZrm5p6 YsWJk5KfrorasAoKtTkgVY6ouvy2nQ/cXm8W0btvD9K7Fa03qQRHUVq2i/ttMqnCL9mj m5gaOWzBwTX6TDTbaoltM2B0Df0lschlIl16RSdl+gvOQZ3dzptPx8OnWuQtIllgApmp kenaft04ZVLKy3r5l08FoRFodyH9E2qJolYxRBIea33yn9O7ixGiSEalj7G9bPC7boPm 87cA== 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=EXyegqNV+YdGaCuwaREB4JgVxfxr2txZwtHVp8f/my0=; b=OkLQCUZuZQnB1J6DvHfvBpk40B1XHUiGgDwJ5ukJr900m8fWIvEvhycxn2wdBj/KD7 wYZxxk40B77tEfFs6U4kJWgU9WICV7/pMpmKCO0J+vj8riiQSXs42fq4x7uxxDY3AHaQ Cvv/Y3ACtF5pymQQtX/4Fts416eujWtTFWM0FQ4ysvtvdudBYzQNp41wt5dpBrox2MZ1 3U26gULYsB/LGh85xBYtlz+GBJ92LOqMXoTmNGa2B4/kB5TCeSBsCOi+6d92z7hjYecO oUdXKppiS7V5TnwGXPj9R7UXCBomW2aMZIJp+k3Fb55zA29Bs418D1m3GuyfTdIWQkZ7 k+jw== X-Gm-Message-State: AODbwcBdiec1kpaq8yw/BbY1Mtncd+7yox+e1YijFodVaQGImqY+LkZu WHNqeQuSF4YMKebDbBaBp4kS X-Received: by 10.223.130.199 with SMTP id 65mr8606918wrc.150.1497286691756; Mon, 12 Jun 2017 09:58:11 -0700 (PDT) Received: from localhost ([109.112.0.253]) by smtp.gmail.com with ESMTPSA id n2sm20925973wrn.30.2017.06.12.09.58.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 12 Jun 2017 09:58:11 -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 Subject: [PATCH 05/11] Creation of "check_vmflags" LSM hook Date: Mon, 12 Jun 2017 18:56:54 +0200 Message-Id: <1497286620-15027-6-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 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 Cc: linux-mm@kvack.org --- include/linux/lsm_hooks.h | 6 ++++++ include/linux/security.h | 6 ++++++ mm/mmap.c | 9 +++++++++ security/security.c | 5 +++++ 4 files changed, 26 insertions(+) diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h index cc0937e..6934cc5 100644 --- a/include/linux/lsm_hooks.h +++ b/include/linux/lsm_hooks.h @@ -483,6 +483,10 @@ * @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. * @file_lock: * Check permission before performing file locking operations. * Note: this hook mediates both flock and fcntl style locks. @@ -1482,6 +1486,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); @@ -1753,6 +1758,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 19bc364..67e33b6 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -302,6 +302,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); @@ -830,6 +831,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..e19f04e 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1363,6 +1363,9 @@ 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; + if (security_check_vmflags(vm_flags)) + return -EPERM; + if (flags & MAP_LOCKED) if (!can_do_mlock()) return -EPERM; @@ -2833,6 +2836,9 @@ 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; + if (security_check_vmflags(flags)) + return -EPERM; + error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED); if (offset_in_page(error)) return error; @@ -3208,6 +3214,9 @@ static struct vm_area_struct *__install_special_mapping( int ret; struct vm_area_struct *vma; + if (security_check_vmflags(vm_flags)) + return ERR_PTR(-EPERM); + 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 e390f99..25d58f0 100644 --- a/security/security.c +++ b/security/security.c @@ -905,6 +905,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);