From patchwork Mon Jul 11 19:31:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Casey Schaufler X-Patchwork-Id: 9224017 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 179AB60572 for ; Mon, 11 Jul 2016 19:31:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 054C627DCE for ; Mon, 11 Jul 2016 19:31:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EDD3B27E5A; Mon, 11 Jul 2016 19:31:32 +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_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham 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 8B19427E22 for ; Mon, 11 Jul 2016 19:31:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751886AbcGKTb1 (ORCPT ); Mon, 11 Jul 2016 15:31:27 -0400 Received: from nm30-vm0.bullet.mail.bf1.yahoo.com ([98.139.213.126]:54635 "EHLO nm30-vm0.bullet.mail.bf1.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751117AbcGKTbK (ORCPT ); Mon, 11 Jul 2016 15:31:10 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1468265469; bh=KFkwQWbXlptPZkzrmyKIhK/uhzz2A/CBNjidrOoXyxE=; h=Subject:To:References:Cc:From:Date:In-Reply-To:From:Subject; b=fG6IDWCPbhqGlVQfjtJblqAy+H8WZhQ5QhtJ/Ztg4Fw84F4LOqRklQiOfugbt1KAxRGNLouTk9Q5vb9svcRfGXXe2JBZjSY48Tffuq3wXVW+V837XyQJ/WquuVGYyl70SbN7P4RysxIpTxstEkgA31Acu6HbymqdZGlagJYP1eyXhylXSPwhEDQ8otUMRBhxpZzIiHNODa1v7cq4uAdwNf0SjJACnVxhkfDBjuhjOI4vSOmoiWk0D3utyERw0i6kQGAzvM8MwTUVQlNqntbyeBsnuNv4imwSUHmecXjN6QA4KipMc5BQxB9+GvPVi/PMo1hKuzNO2KsQpUGQGa/jHg== Received: from [98.139.170.178] by nm30.bullet.mail.bf1.yahoo.com with NNFMP; 11 Jul 2016 19:31:09 -0000 Received: from [68.142.230.69] by tm21.bullet.mail.bf1.yahoo.com with NNFMP; 11 Jul 2016 19:31:09 -0000 Received: from [127.0.0.1] by smtp226.mail.bf1.yahoo.com with NNFMP; 11 Jul 2016 19:31:09 -0000 X-Yahoo-Newman-Id: 28889.29233.bm@smtp226.mail.bf1.yahoo.com X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: 5uG3AxgVM1k9wShHTOtF8yKG1brNsVjr0_TFODWTZvw37Pr UVDWiZUac_y.i5estXVW9MNJjMBuso0zH2tEhfUgcZ.dCgl9CL2lpNXwKSkW _DKXT5i6KQ8IorSZ1VwoCQSyl8uja_vWIiHZkSnJ1El0TheRVxy35XUjfqfn WxiWtrbMk9pojZWg.eGbmi6WjdzBT9QpuXgo6QIwkNUs0_4kIzRm3uzihQ89 hl4mg43WGfQDNyhAiTt42BRYPf6EuiXNVaR87cnhg4RhF1m14EWTB1pE9XpR Q39x1vCu03mEC75OT7NfpHoPV_mzt0FUie.s3BAlXHvRe6Z7xGUSUr95H9i7 m0OXoeTdM9jOTJ48zCnFdxpJqNO.CiVrWilCxMnWDQR46cooi56x3tQokokv .npucCUkoWs2_IekRzUiShYag.g3uNWPJ03ibubC8y7aVNXDIrt7B2FP.DBk 2ny2zDJ65VG9iRx8vd58NTKYQ5LjTpgiO3edlfC2dP112cOgWoKmx79GFPKK eAZX9UcLcWW6YXVJv1pdBaa.GXFQQTc8mkkhEgaVCJRqnPj8DmNJom4cP_1o 1CX4WHTVGtwXZ X-Yahoo-SMTP: OIJXglSswBDfgLtXluJ6wiAYv6_cnw-- Subject: [PATCH RFC 09/10] LSM: Infrastructure managed file security blob To: LSM , James Morris References: Cc: John Johansen , Tetsuo Handa , Paul Moore , Stephen Smalley From: Casey Schaufler Message-ID: <2fe37520-47ef-eb1c-4e7e-75771c98a802@schaufler-ca.com> Date: Mon, 11 Jul 2016 12:31:03 -0700 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 MIME-Version: 1.0 In-Reply-To: Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Subject: [PATCH RFC 09/10] LSM: Infrastructure managed file security blob Move management of the file security blob from the security modules to the LSM infrastructure. Module hooks that are no longer required are removed. Smack was stuffing a pointer that it manages into file->f_security. Now a blob big enough for the pointer is allocated by the infrastruture and the pointer is saved there. It might make sense to revise the way this module manages its file data. Signed-off-by: Casey Schaufler --- include/linux/lsm_hooks.h | 1 + security/apparmor/lsm.c | 20 +------------------- security/security.c | 34 ++++++++++++++++++++++++++++++++++ security/selinux/hooks.c | 25 ++----------------------- security/smack/smack.h | 2 +- security/smack/smack_lsm.c | 26 +++++++++----------------- 6 files changed, 48 insertions(+), 60 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h index 6086045..51f40b3 100644 --- a/include/linux/lsm_hooks.h +++ b/include/linux/lsm_hooks.h @@ -1849,6 +1849,7 @@ struct security_hook_list { */ struct lsm_blob_sizes { int lbs_cred; + int lbs_file; }; /* diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c index 1bd99fa..6c21b08 100644 --- a/security/apparmor/lsm.c +++ b/security/apparmor/lsm.c @@ -352,23 +352,6 @@ static int apparmor_file_open(struct file *file, const struct cred *cred) return error; } -static int apparmor_file_alloc_security(struct file *file) -{ - /* freed by apparmor_file_free_security */ - file->f_security = aa_alloc_file_context(GFP_KERNEL); - if (!file->f_security) - return -ENOMEM; - return 0; - -} - -static void apparmor_file_free_security(struct file *file) -{ - struct aa_file_cxt *cxt = file_cxt(file); - - aa_free_file_context(cxt); -} - static int common_file_perm(int op, struct file *file, u32 mask) { struct aa_file_cxt *fcxt = file_cxt(file); @@ -590,6 +573,7 @@ static int apparmor_task_setrlimit(struct task_struct *task, struct lsm_blob_sizes apparmor_blob_sizes = { .lbs_cred = sizeof(struct aa_task_cxt), + .lbs_file = sizeof(struct aa_file_cxt), }; static struct security_hook_list apparmor_hooks[] = { @@ -612,8 +596,6 @@ static struct security_hook_list apparmor_hooks[] = { LSM_HOOK_INIT(file_open, apparmor_file_open), LSM_HOOK_INIT(file_permission, apparmor_file_permission), - LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security), - LSM_HOOK_INIT(file_free_security, apparmor_file_free_security), LSM_HOOK_INIT(mmap_file, apparmor_mmap_file), LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect), LSM_HOOK_INIT(file_lock, apparmor_file_lock), diff --git a/security/security.c b/security/security.c index 69200ab..88a9b48 100644 --- a/security/security.c +++ b/security/security.c @@ -80,6 +80,7 @@ int __init security_init(void) #ifdef CONFIG_SECURITY_STACKING_DEBUG pr_info("LSM: cred blob size = %d\n", blob_sizes.lbs_cred); + pr_info("LSM: file blob size = %d\n", blob_sizes.lbs_file); #endif return 0; @@ -199,6 +200,32 @@ static void __init lsm_set_size(int *need, int *lbs) void __init security_add_blobs(struct lsm_blob_sizes *needed) { lsm_set_size(&needed->lbs_cred, &blob_sizes.lbs_cred); + lsm_set_size(&needed->lbs_file, &blob_sizes.lbs_file); +} + +/** + * lsm_file_alloc - allocate a composite file blob + * @file: the file that needs a blob + * + * Allocate the file blob for all the modules + * + * Returns 0, or -ENOMEM if memory can't be allocated. + */ +int lsm_file_alloc(struct file *file) +{ +#ifdef CONFIG_SECURITY_STACKING_DEBUG + if (file->f_security) { + pr_info("%s: Inbound file blob is not NULL.\n", __func__); + return 0; + } +#endif + if (blob_sizes.lbs_file == 0) + return 0; + + file->f_security = kzalloc(blob_sizes.lbs_file, GFP_KERNEL); + if (file->f_security == NULL) + return -ENOMEM; + return 0; } /* @@ -865,12 +892,19 @@ int security_file_permission(struct file *file, int mask) int security_file_alloc(struct file *file) { + int rc = lsm_file_alloc(file); + + if (rc) + return rc; return call_int_hook(file_alloc_security, 0, file); } void security_file_free(struct file *file) { call_void_hook(file_free_security, file); + + kfree(file->f_security); + file->f_security = NULL; } int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg) diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 95a2de9..9d769f3 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -126,7 +126,6 @@ int selinux_enabled = 1; #endif static struct kmem_cache *sel_inode_cache; -static struct kmem_cache *file_security_cache; /** * selinux_secmark_enabled - Check to see if SECMARK is currently enabled @@ -355,27 +354,15 @@ static void inode_free_security(struct inode *inode) static int file_alloc_security(struct file *file) { - struct file_security_struct *fsec; + struct file_security_struct *fsec = selinux_file(file); u32 sid = current_sid(); - fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL); - if (!fsec) - return -ENOMEM; - fsec->sid = sid; fsec->fown_sid = sid; - file->f_security = fsec; return 0; } -static void file_free_security(struct file *file) -{ - struct file_security_struct *fsec = selinux_file(file); - file->f_security = NULL; - kmem_cache_free(file_security_cache, fsec); -} - static int superblock_alloc_security(struct super_block *sb) { struct superblock_security_struct *sbsec; @@ -3307,11 +3294,6 @@ static int selinux_file_alloc_security(struct file *file) return file_alloc_security(file); } -static void selinux_file_free_security(struct file *file) -{ - file_free_security(file); -} - /* * Check whether a task has the ioctl permission and cmd * operation to an inode. @@ -5972,6 +5954,7 @@ static int selinux_key_getsecurity(struct key *key, char **_buffer) struct lsm_blob_sizes selinux_blob_sizes = { .lbs_cred = sizeof(struct task_security_struct), + .lbs_file = sizeof(struct file_security_struct), }; static struct security_hook_list selinux_hooks[] = { @@ -6040,7 +6023,6 @@ static struct security_hook_list selinux_hooks[] = { LSM_HOOK_INIT(file_permission, selinux_file_permission), LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security), - LSM_HOOK_INIT(file_free_security, selinux_file_free_security), LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl), LSM_HOOK_INIT(mmap_file, selinux_mmap_file), LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr), @@ -6215,9 +6197,6 @@ static __init int selinux_init(void) sel_inode_cache = kmem_cache_create("selinux_inode_security", sizeof(struct inode_security_struct), 0, SLAB_PANIC, NULL); - file_security_cache = kmem_cache_create("selinux_file_security", - sizeof(struct file_security_struct), - 0, SLAB_PANIC, NULL); avc_init(); security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux"); diff --git a/security/smack/smack.h b/security/smack/smack.h index d929f3d..0027ccd 100644 --- a/security/smack/smack.h +++ b/security/smack/smack.h @@ -343,7 +343,7 @@ static inline struct task_smack *smack_cred(const struct cred *cred) return cred->security; } -static inline struct smack_known *smack_file(const struct file *file) +static inline struct smack_known **smack_file(const struct file *file) { return file->f_security; } diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c index bce7c44..94aff94 100644 --- a/security/smack/smack_lsm.c +++ b/security/smack/smack_lsm.c @@ -1563,25 +1563,13 @@ static void smack_inode_getsecid(struct inode *inode, u32 *secid) */ static int smack_file_alloc_security(struct file *file) { - struct smack_known *skp = smk_of_current(); + struct smack_known **blob = smack_file(file); - file->f_security = skp; + *blob = smk_of_current(); return 0; } /** - * smack_file_free_security - clear a file security blob - * @file: the object - * - * The security blob for a file is a pointer to the master - * label list, so no memory is freed. - */ -static void smack_file_free_security(struct file *file) -{ - file->f_security = NULL; -} - -/** * smack_file_ioctl - Smack check on ioctls * @file: the object * @cmd: what to do @@ -1788,7 +1776,9 @@ static int smack_mmap_file(struct file *file, */ static void smack_file_set_fowner(struct file *file) { - file->f_security = smk_of_current(); + struct smack_known **blob = smack_file(file); + + *blob = smk_of_current(); } /** @@ -1805,6 +1795,7 @@ static void smack_file_set_fowner(struct file *file) static int smack_file_send_sigiotask(struct task_struct *tsk, struct fown_struct *fown, int signum) { + struct smack_known **blob; struct smack_known *skp; struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred)); struct file *file; @@ -1817,7 +1808,8 @@ static int smack_file_send_sigiotask(struct task_struct *tsk, file = container_of(fown, struct file, f_owner); /* we don't log here as rc can be overriden */ - skp = smack_file(file); + blob = smack_file(file); + skp = *blob; rc = smk_access(skp, tkp, MAY_WRITE, NULL); rc = smk_bu_note("sigiotask", skp, tkp, MAY_WRITE, rc); if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE)) @@ -4554,6 +4546,7 @@ static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) struct lsm_blob_sizes smack_blob_sizes = { .lbs_cred = sizeof(struct task_smack), + .lbs_file = sizeof(struct smack_known *), }; static struct security_hook_list smack_hooks[] = { @@ -4593,7 +4586,6 @@ static struct security_hook_list smack_hooks[] = { LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid), LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security), - LSM_HOOK_INIT(file_free_security, smack_file_free_security), LSM_HOOK_INIT(file_ioctl, smack_file_ioctl), LSM_HOOK_INIT(file_lock, smack_file_lock), LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),