From patchwork Wed Apr 5 21:52:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Casey Schaufler X-Patchwork-Id: 9665791 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 7CACE602B5 for ; Wed, 5 Apr 2017 21:52:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6D89028173 for ; Wed, 5 Apr 2017 21:52:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 61A702856D; Wed, 5 Apr 2017 21:52:57 +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_SIGNED, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, 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 2F04928573 for ; Wed, 5 Apr 2017 21:52:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934156AbdDEVwy (ORCPT ); Wed, 5 Apr 2017 17:52:54 -0400 Received: from nm24-vm4.bullet.mail.gq1.yahoo.com ([98.136.217.99]:33653 "EHLO nm24-vm4.bullet.mail.gq1.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934094AbdDEVwy (ORCPT ); Wed, 5 Apr 2017 17:52:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1491429173; bh=5uoO92MMbMyPWL5FBXqn76Tk3C+xCoErgFNQXvXz91Y=; h=Subject:To:References:Cc:From:Date:In-Reply-To:From:Subject; b=FOjoBLl41QKppLhjvwdov999m1XbGdUmwlgwpPEsXWX8O4yi2d+bEFRtC+TTOW7D0oecmQJeBI2pIjJ5WI6nfie3snhVKaOmVZ+O/Ft/7UUu5DYZTa/JXUdqFInLLrD+wpTbJHi23+setCgXFiuB3bv5ZZaEHd+FsWU9LJpvtJk5V5HrtXb3hzSCFIOj/93DFZXqxenJmQF8AoYQC/mZIuS6IgeuYLbMvUSx+YOR62k+ECGLENuzAnCYTk+y7uiiQaxb6OfrTNzwZ7LSPEkDzKF4SypHOdGimG1lEUJIuve/kvGu8Aatav3bGewJtX+H8XxfJmJmt+MMUMbkfWUeqQ== Received: from [98.137.12.174] by nm24.bullet.mail.gq1.yahoo.com with NNFMP; 05 Apr 2017 21:52:53 -0000 Received: from [98.138.226.130] by tm13.bullet.mail.gq1.yahoo.com with NNFMP; 05 Apr 2017 21:52:53 -0000 Received: from [127.0.0.1] by smtp217.mail.ne1.yahoo.com with NNFMP; 05 Apr 2017 21:52:53 -0000 X-Yahoo-Newman-Id: 16150.52194.bm@smtp217.mail.ne1.yahoo.com X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: PTmOOL4VM1mdEIfXf9wRwOxbb_HIP1qmZ4JRyYrfcWIHOiF 75urAWU0eXJkSR0RRthjBg4NkWkedsNUgKpvSHrA0.Ng7jzp5z11xqbmE7PY 80BvqO6z6ctyQAgzYysMYXxjma2i1BAC_bWP9lWi.u.gWemHQEeK0dBmkuFV 3lnCOEpSa4l4JH1IFLMJwcF9T3rF0LMeC_FMFuAUHN5wCp2_M._BMWkBbkjm sGJ6Z2yzlyO3INYBoH5sO3MRp8PpBomk.zOsBM7H3w_mS51BZ_UcBZHXsMnM 37.lTdys6tley.E3engKICx0.ioSdK4waxDp0J8rhXmx0BTkENaVulc6Uf6N 8S7sfVKRR1KyyrcoBddlj7e5rPI53n.ZsfLeeHk9UTsIWWxKjJIk7ofQaKkX Mwxl8fQUV9ZTphY6TW37xMjXq837jHMnlCULufZBxErwWohsDpZUOpcLI_K. 7uep0xzr8kpoi5.ynVDKMS7_VMjfX2TC.3uCmPIXtQyry3wg_131K.gHJgyr JwR4iKu1Td6ixw.tW5uBw1Rnpj05SdTGqBU00nvBSdnGqh88b2mZn5E_qGFS w6FValVWRzA-- X-Yahoo-SMTP: OIJXglSswBDfgLtXluJ6wiAYv6_cnw-- Subject: [PATCH RFC 03/11] LSM: Manage file security blobs To: LSM , James Morris References: <509e0281-9f8a-83c2-f9d6-5532903cda46@schaufler-ca.com> Cc: John Johansen , Tetsuo Handa , Paul Moore , Stephen Smalley , Kees Cook , Casey Schaufler From: Casey Schaufler Message-ID: <443c4fb4-be4c-60c9-1867-18487570fc86@schaufler-ca.com> Date: Wed, 5 Apr 2017 14:52:47 -0700 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: <509e0281-9f8a-83c2-f9d6-5532903cda46@schaufler-ca.com> Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Subject: [PATCH RFC 03/11] LSM: Manage file security blobs Move the management of file security blobs from the individual security modules to the security infrastructure. The security modules using file blobs have been updated accordingly. Modules are required to identify the space they need at module initialization. In some cases a module no longer needs to supply a blob management hook, in which case the hook has been removed. Signed-off-by: Casey Schaufler --- include/linux/lsm_hooks.h | 1 + security/apparmor/include/context.h | 6 ++++++ security/apparmor/lsm.c | 26 ++++------------------- security/security.c | 32 +++++++++++++++++++++++++++++ security/selinux/hooks.c | 41 +++++++++---------------------------- security/selinux/include/objsec.h | 5 +++++ security/smack/smack.h | 5 +++++ security/smack/smack_lsm.c | 26 ++++++++--------------- 8 files changed, 72 insertions(+), 70 deletions(-) diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h index 41f01e2..dc012eb 100644 --- a/include/linux/lsm_hooks.h +++ b/include/linux/lsm_hooks.h @@ -1894,6 +1894,7 @@ struct security_hook_list { */ struct lsm_blob_sizes { int lbs_cred; + int lbs_file; }; /* diff --git a/security/apparmor/include/context.h b/security/apparmor/include/context.h index f35ad39..3ff3879 100644 --- a/security/apparmor/include/context.h +++ b/security/apparmor/include/context.h @@ -25,6 +25,7 @@ #define cred_ctx(X) apparmor_cred(X) #define current_ctx() cred_ctx(current_cred()) +#define file_ctx(X) apparmor_file(X) /* struct aa_file_ctx - the AppArmor context the file was opened in * @perms: the permission the file was opened with @@ -92,6 +93,11 @@ static inline struct aa_task_ctx *apparmor_cred(const struct cred *cred) return cred->security; } +static inline struct aa_file_ctx *apparmor_file(const struct file *file) +{ + return file->f_security; +} + /** * aa_cred_profile - obtain cred's profiles * @cred: cred to obtain profiles from (NOT NULL) diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c index 56a8fd9..ff0010f 100644 --- a/security/apparmor/lsm.c +++ b/security/apparmor/lsm.c @@ -329,7 +329,7 @@ static int apparmor_inode_getattr(const struct path *path) static int apparmor_file_open(struct file *file, const struct cred *cred) { - struct aa_file_ctx *fctx = file->f_security; + struct aa_file_ctx *fctx = file_ctx(file); struct aa_profile *profile; int error = 0; @@ -360,26 +360,9 @@ 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_ctx *ctx = file->f_security; - - aa_free_file_context(ctx); -} - static int common_file_perm(const char *op, struct file *file, u32 mask) { - struct aa_file_ctx *fctx = file->f_security; + struct aa_file_ctx *fctx = file_ctx(file); struct aa_profile *profile, *fprofile = aa_cred_profile(file->f_cred); int error = 0; @@ -425,7 +408,7 @@ static int common_mmap(const char *op, struct file *file, unsigned long prot, { int mask = 0; - if (!file || !file->f_security) + if (!file || file_ctx(file) == NULL) return 0; if (prot & PROT_READ) @@ -566,6 +549,7 @@ static int apparmor_task_setrlimit(struct task_struct *task, struct lsm_blob_sizes apparmor_blob_sizes = { .lbs_cred = sizeof(struct aa_task_ctx), + .lbs_file = sizeof(struct aa_file_ctx), }; static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { @@ -588,8 +572,6 @@ static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { 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 5160a52..86c6796 100644 --- a/security/security.c +++ b/security/security.c @@ -87,6 +87,7 @@ int __init security_init(void) #ifdef CONFIG_SECURITY_LSM_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; @@ -223,6 +224,30 @@ 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_LSM_DEBUG + if (file->f_security) + pr_info("%s: Inbound file blob is not NULL.\n", __func__); +#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; } /* @@ -910,12 +935,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 dee7ea7..e1e0f4e 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -127,7 +127,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 @@ -347,27 +346,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 = file->f_security; - 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; @@ -1793,7 +1780,7 @@ static int file_has_perm(const struct cred *cred, struct file *file, u32 av) { - struct file_security_struct *fsec = file->f_security; + struct file_security_struct *fsec = selinux_file(file); struct inode *inode = file_inode(file); struct common_audit_data ad; u32 sid = cred_sid(cred); @@ -2112,7 +2099,7 @@ static int selinux_binder_transfer_file(struct task_struct *from, struct file *file) { u32 sid = task_sid(to); - struct file_security_struct *fsec = file->f_security; + struct file_security_struct *fsec = selinux_file(file); struct dentry *dentry = file->f_path.dentry; struct inode_security_struct *isec; struct common_audit_data ad; @@ -3376,7 +3363,7 @@ static int selinux_revalidate_file_permission(struct file *file, int mask) static int selinux_file_permission(struct file *file, int mask) { struct inode *inode = file_inode(file); - struct file_security_struct *fsec = file->f_security; + struct file_security_struct *fsec = selinux_file(file); struct inode_security_struct *isec; u32 sid = current_sid(); @@ -3398,11 +3385,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. @@ -3411,7 +3393,7 @@ static int ioctl_has_perm(const struct cred *cred, struct file *file, u32 requested, u16 cmd) { struct common_audit_data ad; - struct file_security_struct *fsec = file->f_security; + struct file_security_struct *fsec = selinux_file(file); struct inode *inode = file_inode(file); struct inode_security_struct *isec; struct lsm_ioctlop_audit ioctl; @@ -3645,7 +3627,7 @@ static void selinux_file_set_fowner(struct file *file) { struct file_security_struct *fsec; - fsec = file->f_security; + fsec = selinux_file(file); fsec->fown_sid = current_sid(); } @@ -3660,7 +3642,7 @@ static int selinux_file_send_sigiotask(struct task_struct *tsk, /* struct fown_struct is never outside the context of a struct file */ file = container_of(fown, struct file, f_owner); - fsec = file->f_security; + fsec = selinux_file(file); if (!signum) perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ @@ -3683,7 +3665,7 @@ static int selinux_file_open(struct file *file, const struct cred *cred) struct file_security_struct *fsec; struct inode_security_struct *isec; - fsec = file->f_security; + fsec = selinux_file(file); isec = inode_security(file_inode(file)); /* * Save inode label and policy sequence number @@ -3812,7 +3794,7 @@ static int selinux_kernel_module_from_file(struct file *file) ad.type = LSM_AUDIT_DATA_FILE; ad.u.file = file; - fsec = file->f_security; + fsec = selinux_file(file); if (sid != fsec->sid) { rc = avc_has_perm(sid, fsec->sid, SECCLASS_FD, FD__USE, &ad); if (rc) @@ -6086,6 +6068,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[] __lsm_ro_after_init = { @@ -6157,7 +6140,6 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { 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), @@ -6332,9 +6314,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/selinux/include/objsec.h b/security/selinux/include/objsec.h index 48132cc..6a638da 100644 --- a/security/selinux/include/objsec.h +++ b/security/selinux/include/objsec.h @@ -150,4 +150,9 @@ static inline struct task_security_struct *selinux_cred(const struct cred *cred) return cred->security; } +static inline struct file_security_struct *selinux_file(const struct file *file) +{ + return file->f_security; +} + #endif /* _SELINUX_OBJSEC_H_ */ diff --git a/security/smack/smack.h b/security/smack/smack.h index 17949ec..9e704a6 100644 --- a/security/smack/smack.h +++ b/security/smack/smack.h @@ -361,6 +361,11 @@ 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) +{ + return file->f_security; +} + /* * Is the directory transmuting? */ diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c index 6ce81ac..74957d7 100644 --- a/security/smack/smack_lsm.c +++ b/security/smack/smack_lsm.c @@ -1603,25 +1603,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 @@ -1845,7 +1833,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(); } /** @@ -1862,6 +1852,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; @@ -1874,7 +1865,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 = file->f_security; + blob = smack_file(file); + skp = *blob; rc = smk_access(skp, tkp, MAY_DELIVER, NULL); rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc); if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE)) @@ -4606,6 +4598,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[] __lsm_ro_after_init = { @@ -4645,7 +4638,6 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = { 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),