Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752840AbdFLVbx (ORCPT ); Mon, 12 Jun 2017 17:31:53 -0400 Received: from nm13-vm5.bullet.mail.ne1.yahoo.com ([98.138.91.235]:45190 "EHLO nm13-vm5.bullet.mail.ne1.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752756AbdFLVbv (ORCPT ); Mon, 12 Jun 2017 17:31:51 -0400 X-Yahoo-Newman-Id: 648682.20082.bm@smtp217.mail.ne1.yahoo.com X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: qKmkX3IVM1m.dm.N8mkNo65soY3M_9fR6OQRevWb24DfiRH 5WLr6Tf4BMoiojqqlCE.cmN28ZEu6xgxhzf1o_wi06cUx6NiQBQx4lS2dS6c _fUK4RarXGRYwQJa7c7x5dtyZnrfIzCqDzjx_AKO9jqzqofRAWZkZnujBCRT Alhg7O3V8guJ5tMZd911jCM4z_MCEP6mop4GBXGRVKDxAeiq74kefTBu0B5l bJpb4GVwco5zcLkczqrTH4kzRj7qFd7jboCR.E3ZJLfRx9QeYwq53WyLWSZJ Qbck0NU7CTaMucN1SJaxUYMfSW0itfEzanSiJKSa6RJxUfdkKOXwPqa7SPxh FhiLsOnIvf_GfQuTVXaoqe3.PRd9w5spH_z2mrmiX9ShElg_5mt0Ca8NFnc6 EUfy2VDm0CNcuCfMREE48NSKCj.E9cqQPl1jrdPsjiSbWPAwBQViYiXjSWqd it3ZqTfsaPqwxTMKvwZRsndxi1bF__.KX3yapWxDTIQRyyClh9bd5GAcSeMf vnDAAWHXc033ixAkIPMlPaBptKPV63eyjhUU2dkn3nhYXQKDV8hVkCkMhbQR Y4sSpPT8KI1cKRBUUl_5glw-- X-Yahoo-SMTP: OIJXglSswBDfgLtXluJ6wiAYv6_cnw-- Subject: Re: [PATCH 05/11] Creation of "check_vmflags" LSM hook To: Salvatore Mesoraca , linux-kernel@vger.kernel.org Cc: linux-security-module@vger.kernel.org, kernel-hardening@lists.openwall.com, Brad Spengler , PaX Team , Kees Cook , James Morris , "Serge E. Hallyn" , linux-mm@kvack.org References: <1497286620-15027-1-git-send-email-s.mesoraca16@gmail.com> <1497286620-15027-6-git-send-email-s.mesoraca16@gmail.com> From: Casey Schaufler Message-ID: <1dccd8da-c96f-3947-d90f-a3f3d4f389fd@schaufler-ca.com> Date: Mon, 12 Jun 2017 14:31:48 -0700 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1 MIME-Version: 1.0 In-Reply-To: <1497286620-15027-6-git-send-email-s.mesoraca16@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Content-Language: en-US Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4875 Lines: 132 On 6/12/2017 9:56 AM, Salvatore Mesoraca wrote: > 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; > + Have the hook return a value and return that rather than -EPERM. That way a security module can choose an error that it determines is appropriate. It is possible that a module might want to deny the access for a reason other than lack of privilege. > 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; > + Same here > 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); > + And here. > 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);