From patchwork Mon Jul 11 19:30:14 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Casey Schaufler X-Patchwork-Id: 9224005 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 7308560572 for ; Mon, 11 Jul 2016 19:30:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5F25E27DCE for ; Mon, 11 Jul 2016 19:30:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 534B727E5A; Mon, 11 Jul 2016 19:30:27 +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 5013227DCE for ; Mon, 11 Jul 2016 19:30:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751214AbcGKTaW (ORCPT ); Mon, 11 Jul 2016 15:30:22 -0400 Received: from nm10-vm0.bullet.mail.bf1.yahoo.com ([98.139.213.147]:35981 "EHLO nm10-vm0.bullet.mail.bf1.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750941AbcGKTaV (ORCPT ); Mon, 11 Jul 2016 15:30:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1468265420; bh=YgyrtOWNptX/z0SOh3mNL0c6RE6yZvt7dRdrRJSsT+s=; h=Subject:To:References:Cc:From:Date:In-Reply-To:From:Subject; b=ejbrNhftZoZxWau+q3DTenCZT3fg1kho5R83rlb4A2n482QfCZAAbaJIzQt3W8+KvOGt0iceeTXOuadlKf92yzgv8FkY85/9bqY0r7Quau0Kw71vRFKSPolm+Ou7WJXJ2V4UmltkS2bHE8YL8Cl8i+M9NcaRZlwMEPGfikuHADuVelLXoKxnXkNah9qdLBtDvGsG85pJOpQGN1zMOdXYbl2WTmXfsIoKL1mnhDNfNrh7rMK/p80grvqjw8lXPhCfhYe6Xi2UM1G/Cr+5ydnkz3of8/tbx5k5GMXOKRFD3anyQTYIli/x8MSxY8NKHgR8/Bbhla6bylaCOEHuFYdLAg== Received: from [66.196.81.173] by nm10.bullet.mail.bf1.yahoo.com with NNFMP; 11 Jul 2016 19:30:20 -0000 Received: from [68.142.230.69] by tm19.bullet.mail.bf1.yahoo.com with NNFMP; 11 Jul 2016 19:30:20 -0000 Received: from [127.0.0.1] by smtp226.mail.bf1.yahoo.com with NNFMP; 11 Jul 2016 19:30:20 -0000 X-Yahoo-Newman-Id: 175385.29242.bm@smtp226.mail.bf1.yahoo.com X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: FgI9PjEVM1ljcNzBh_R9Ox7Rmf02JO0zjVpcnXTpwm5RzNu CIfEz8fB4xSqBTmXAwSYli3ewjYODGqRvPD5PSADsn0l5iw6Iyu1SOipRfCA 5yHQeT5IeF43YZV9Hoc.sAbba1Ee1aWW5qSv.e8KIHBs_qOjJPizVzW5yi90 XahjWeMLQoW_vE_cAdPv51YsZzWiOUzvgaEWHJxP2FwdpRLvU.QD9BEadbyQ zcimYHamV0Wsv0p5Zi6lI_CNE5LEpnK8EKTcc1ndjUeAmvd2MhuXVNWSm5Vt uZJVwAx0AF.j2D04wmzfKbzVFwcPjxTVeynyB6D049PsB5ATUtTuAdBz.lK9 6SBYmO7J0yS2HnTAgEsjuDXx9A52lhiem7twLgo9YWgv7cVUIoXkYi9_qsdY ox7dvt1y5cxKNsBmtx.pf61MXODMbAlt3eDMA1QLTtimaLRgReGU5M_yhL6v CXKdoLWPmoySu7Z6by22kLGAl.k7NwDUq1fMdCniOI1O0XlR5m.6ZBe9arl3 oq08EiWakE2csMmXloxmA9yogj0_uCGDf8bJxQbj4VWi7kEbUpetxJBLu0mb icGSRugRggXSw X-Yahoo-SMTP: OIJXglSswBDfgLtXluJ6wiAYv6_cnw-- Subject: [PATCH RFC 03/10] Smack: Abstract the cred security blob To: LSM , James Morris References: Cc: John Johansen , Tetsuo Handa , Paul Moore , Stephen Smalley From: Casey Schaufler Message-ID: <3987f51c-3777-f015-9272-320ff00fadef@schaufler-ca.com> Date: Mon, 11 Jul 2016 12:30:14 -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 03/10] Smack: Abstract the cred security blob Abstract reading the credential security blob. Remove abstraction when writing the credential security blob. There is no change in the behavior of the code. Signed-off-by: Casey Schaufler --- security/smack/smack.h | 15 +++++-- security/smack/smack_access.c | 2 +- security/smack/smack_lsm.c | 98 ++++++++++++++++++++++++------------------- security/smack/smackfs.c | 18 ++++---- 4 files changed, 77 insertions(+), 56 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/security/smack/smack.h b/security/smack/smack.h index 6c91156..a2a98c0 100644 --- a/security/smack/smack.h +++ b/security/smack/smack.h @@ -24,6 +24,7 @@ #include #include #include +#include /* * Use IPv6 port labeling if IPv6 is enabled and secmarks @@ -337,6 +338,11 @@ extern struct list_head smack_onlycap_list; #define SMACK_HASH_SLOTS 16 extern struct hlist_head smack_known_hash[SMACK_HASH_SLOTS]; +static inline struct task_smack *smack_cred(const struct cred *cred) +{ + return cred->security; +} + /* * Is the directory transmuting? */ @@ -363,13 +369,16 @@ static inline struct smack_known *smk_of_task(const struct task_smack *tsp) return tsp->smk_task; } -static inline struct smack_known *smk_of_task_struct(const struct task_struct *t) +static inline struct smack_known *smk_of_task_struct( + const struct task_struct *t) { struct smack_known *skp; + const struct cred *cred; rcu_read_lock(); - skp = smk_of_task(__task_cred(t)->security); + cred = __task_cred(t); rcu_read_unlock(); + skp = smk_of_task(smack_cred(cred)); return skp; } @@ -386,7 +395,7 @@ static inline struct smack_known *smk_of_forked(const struct task_smack *tsp) */ static inline struct smack_known *smk_of_current(void) { - return smk_of_task(current_security()); + return smk_of_task(smack_cred(current_cred())); } /* diff --git a/security/smack/smack_access.c b/security/smack/smack_access.c index a283f9e..c0ca510 100644 --- a/security/smack/smack_access.c +++ b/security/smack/smack_access.c @@ -280,7 +280,7 @@ out_audit: int smk_curacc(struct smack_known *obj_known, u32 mode, struct smk_audit_info *a) { - struct task_smack *tsp = current_security(); + struct task_smack *tsp = smack_cred(current_cred()); return smk_tskacc(tsp, obj_known, mode, a); } diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c index 92e66f8..d7aaef0 100644 --- a/security/smack/smack_lsm.c +++ b/security/smack/smack_lsm.c @@ -120,7 +120,7 @@ static int smk_bu_note(char *note, struct smack_known *sskp, static int smk_bu_current(char *note, struct smack_known *oskp, int mode, int rc) { - struct task_smack *tsp = current_security(); + struct task_smack *tsp = smack_cred(current_cred()); char acc[SMK_NUM_ACCESS_TYPE + 1]; if (rc <= 0) @@ -141,7 +141,7 @@ static int smk_bu_current(char *note, struct smack_known *oskp, #ifdef CONFIG_SECURITY_SMACK_BRINGUP static int smk_bu_task(struct task_struct *otp, int mode, int rc) { - struct task_smack *tsp = current_security(); + struct task_smack *tsp = smack_cred(current_cred()); struct smack_known *smk_task = smk_of_task_struct(otp); char acc[SMK_NUM_ACCESS_TYPE + 1]; @@ -163,7 +163,7 @@ static int smk_bu_task(struct task_struct *otp, int mode, int rc) #ifdef CONFIG_SECURITY_SMACK_BRINGUP static int smk_bu_inode(struct inode *inode, int mode, int rc) { - struct task_smack *tsp = current_security(); + struct task_smack *tsp = smack_cred(current_cred()); struct inode_smack *isp = inode->i_security; char acc[SMK_NUM_ACCESS_TYPE + 1]; @@ -193,7 +193,7 @@ static int smk_bu_inode(struct inode *inode, int mode, int rc) #ifdef CONFIG_SECURITY_SMACK_BRINGUP static int smk_bu_file(struct file *file, int mode, int rc) { - struct task_smack *tsp = current_security(); + struct task_smack *tsp = smack_cred(current_cred()); struct smack_known *sskp = tsp->smk_task; struct inode *inode = file_inode(file); struct inode_smack *isp = inode->i_security; @@ -223,7 +223,7 @@ static int smk_bu_file(struct file *file, int mode, int rc) static int smk_bu_credfile(const struct cred *cred, struct file *file, int mode, int rc) { - struct task_smack *tsp = cred->security; + struct task_smack *tsp = smack_cred(cred); struct smack_known *sskp = tsp->smk_task; struct inode *inode = file->f_inode; struct inode_smack *isp = inode->i_security; @@ -299,14 +299,29 @@ static struct inode_smack *new_inode_smack(struct smack_known *skp) if (isp == NULL) return NULL; - isp->smk_inode = skp; - isp->smk_flags = 0; - mutex_init(&isp->smk_lock); +/*FIX*/isp->smk_inode = skp; isp->smk_flags = 0; mutex_init(&isp->smk_lock); return isp; } /** + * init_task_smack - initialize a task security blob + * @tsp: blob to initialize + * @task: a pointer to the Smack label for the running task + * @forked: a pointer to the Smack label for the forked task + * + */ +static void init_task_smack(struct task_smack *tsp, struct smack_known *task, + struct smack_known *forked) +{ + tsp->smk_task = task; + tsp->smk_forked = forked; + INIT_LIST_HEAD(&tsp->smk_rules); + INIT_LIST_HEAD(&tsp->smk_relabel); + mutex_init(&tsp->smk_rules_lock); +} + +/** * new_task_smack - allocate a task security blob * @task: a pointer to the Smack label for the running task * @forked: a pointer to the Smack label for the forked task @@ -323,12 +338,7 @@ static struct task_smack *new_task_smack(struct smack_known *task, if (tsp == NULL) return NULL; - tsp->smk_task = task; - tsp->smk_forked = forked; - INIT_LIST_HEAD(&tsp->smk_rules); - INIT_LIST_HEAD(&tsp->smk_relabel); - mutex_init(&tsp->smk_rules_lock); - + init_task_smack(tsp, task, forked); return tsp; } @@ -431,7 +441,7 @@ static int smk_ptrace_rule_check(struct task_struct *tracer, } rcu_read_lock(); - tsp = __task_cred(tracer)->security; + tsp = smack_cred(__task_cred(tracer)); tracer_known = smk_of_task(tsp); if ((mode & PTRACE_MODE_ATTACH) && @@ -498,7 +508,7 @@ static int smack_ptrace_traceme(struct task_struct *ptp) int rc; struct smack_known *skp; - skp = smk_of_task(current_security()); + skp = smk_of_task(smack_cred(current_cred())); rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__); return rc; @@ -906,7 +916,7 @@ static int smack_sb_statfs(struct dentry *dentry) static int smack_bprm_set_creds(struct linux_binprm *bprm) { struct inode *inode = file_inode(bprm->file); - struct task_smack *bsp = bprm->cred->security; + struct task_smack *bsp = smack_cred(bprm->cred); struct inode_smack *isp; int rc; @@ -949,7 +959,7 @@ static int smack_bprm_set_creds(struct linux_binprm *bprm) */ static void smack_bprm_committing_creds(struct linux_binprm *bprm) { - struct task_smack *bsp = bprm->cred->security; + struct task_smack *bsp = smack_cred(bprm->cred); if (bsp->smk_task != bsp->smk_forked) current->pdeath_signal = 0; @@ -963,7 +973,7 @@ static void smack_bprm_committing_creds(struct linux_binprm *bprm) */ static int smack_bprm_secureexec(struct linux_binprm *bprm) { - struct task_smack *tsp = current_security(); + struct task_smack *tsp = smack_cred(current_cred()); if (tsp->smk_task != tsp->smk_forked) return 1; @@ -1721,7 +1731,7 @@ static int smack_mmap_file(struct file *file, return 0; mkp = isp->smk_mmap; - tsp = current_security(); + tsp = smack_cred(current_cred()); skp = smk_of_current(); rc = 0; @@ -1817,7 +1827,7 @@ static int smack_file_send_sigiotask(struct task_struct *tsk, struct fown_struct *fown, int signum) { struct smack_known *skp; - struct smack_known *tkp = smk_of_task(tsk->cred->security); + struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred)); struct file *file; int rc; struct smk_audit_info ad; @@ -1865,7 +1875,7 @@ static int smack_file_receive(struct file *file) if (S_ISSOCK(inode->i_mode)) { sock = SOCKET_I(inode); ssp = sock->sk->sk_security; - tsp = current_security(); + tsp = smack_cred(current_cred()); /* * If the receiving process can't write to the * passed socket or if the passed socket can't @@ -1907,7 +1917,7 @@ static int smack_file_receive(struct file *file) */ static int smack_file_open(struct file *file, const struct cred *cred) { - struct task_smack *tsp = cred->security; + struct task_smack *tsp = smack_cred(cred); struct inode *inode = file_inode(file); struct smk_audit_info ad; int rc; @@ -1938,9 +1948,8 @@ static int smack_file_open(struct file *file, const struct cred *cred) */ static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp) { - struct task_smack *tsp; + struct task_smack *tsp = new_task_smack(NULL, NULL, gfp); - tsp = new_task_smack(NULL, NULL, gfp); if (tsp == NULL) return -ENOMEM; @@ -1957,7 +1966,7 @@ static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp) */ static void smack_cred_free(struct cred *cred) { - struct task_smack *tsp = cred->security; + struct task_smack *tsp = smack_cred(cred); struct smack_rule *rp; struct list_head *l; struct list_head *n; @@ -1987,7 +1996,7 @@ static void smack_cred_free(struct cred *cred) static int smack_cred_prepare(struct cred *new, const struct cred *old, gfp_t gfp) { - struct task_smack *old_tsp = old->security; + struct task_smack *old_tsp = smack_cred(old); struct task_smack *new_tsp; int rc; @@ -2017,8 +2026,8 @@ static int smack_cred_prepare(struct cred *new, const struct cred *old, */ static void smack_cred_transfer(struct cred *new, const struct cred *old) { - struct task_smack *old_tsp = old->security; - struct task_smack *new_tsp = new->security; + struct task_smack *old_tsp = smack_cred(old); + struct task_smack *new_tsp = smack_cred(new); new_tsp->smk_task = old_tsp->smk_task; new_tsp->smk_forked = old_tsp->smk_task; @@ -2038,7 +2047,7 @@ static void smack_cred_transfer(struct cred *new, const struct cred *old) */ static int smack_kernel_act_as(struct cred *new, u32 secid) { - struct task_smack *new_tsp = new->security; + struct task_smack *new_tsp = smack_cred(new); struct smack_known *skp = smack_from_secid(secid); if (skp == NULL) @@ -2060,7 +2069,7 @@ static int smack_kernel_create_files_as(struct cred *new, struct inode *inode) { struct inode_smack *isp = inode->i_security; - struct task_smack *tsp = new->security; + struct task_smack *tsp = smack_cred(new); tsp->smk_forked = isp->smk_inode; tsp->smk_task = tsp->smk_forked; @@ -3602,10 +3611,10 @@ static int smack_getprocattr(struct task_struct *p, char *name, char **value) * * Returns the length of the smack label or an error code */ -static int smack_setprocattr(struct task_struct *p, char *name, - void *value, size_t size) +static int smack_setprocattr(struct task_struct *p, char *name, void *value, + size_t size) { - struct task_smack *tsp = current_security(); + struct task_smack *tsp = smack_cred(current_cred()); struct cred *new; struct smack_known *skp; struct smack_known_list_elem *sklep; @@ -3652,7 +3661,7 @@ static int smack_setprocattr(struct task_struct *p, char *name, if (new == NULL) return -ENOMEM; - tsp = new->security; + tsp = smack_cred(new); tsp->smk_task = skp; /* * process can change its label only once @@ -4298,7 +4307,7 @@ static void smack_inet_csk_clone(struct sock *sk, static int smack_key_alloc(struct key *key, const struct cred *cred, unsigned long flags) { - struct smack_known *skp = smk_of_task(cred->security); + struct smack_known *skp = smk_of_task(smack_cred(cred)); key->security = skp; return 0; @@ -4329,7 +4338,7 @@ static int smack_key_permission(key_ref_t key_ref, { struct key *keyp; struct smk_audit_info ad; - struct smack_known *tkp = smk_of_task(cred->security); + struct smack_known *tkp = smk_of_task(smack_cred(cred)); int request = 0; int rc; @@ -4761,7 +4770,7 @@ static __init void init_smack_known_list(void) */ static __init int smack_init(void) { - struct cred *cred; + struct cred *cred = (struct cred *) current->cred; struct task_smack *tsp; if (!security_module_enable("smack")) @@ -4777,7 +4786,15 @@ static __init int smack_init(void) kmem_cache_destroy(smack_inode_cache); return -ENOMEM; } + /* + * Set the security state for the initial task. + */ + cred->security = tsp; + /* + * Register with LSM + */ + security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack"); smack_enabled = 1; pr_info("Smack: Initializing.\n"); @@ -4800,11 +4817,6 @@ static __init int smack_init(void) /* initialize the smack_known_list */ init_smack_known_list(); - /* - * Register with LSM - */ - security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack"); - return 0; } diff --git a/security/smack/smackfs.c b/security/smack/smackfs.c index e249a66..6218bf7 100644 --- a/security/smack/smackfs.c +++ b/security/smack/smackfs.c @@ -2203,14 +2203,14 @@ static const struct file_operations smk_logging_ops = { static void *load_self_seq_start(struct seq_file *s, loff_t *pos) { - struct task_smack *tsp = current_security(); + struct task_smack *tsp = smack_cred(current_cred()); return smk_seq_start(s, pos, &tsp->smk_rules); } static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos) { - struct task_smack *tsp = current_security(); + struct task_smack *tsp = smack_cred(current_cred()); return smk_seq_next(s, v, pos, &tsp->smk_rules); } @@ -2257,7 +2257,7 @@ static int smk_open_load_self(struct inode *inode, struct file *file) static ssize_t smk_write_load_self(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { - struct task_smack *tsp = current_security(); + struct task_smack *tsp = smack_cred(current_cred()); return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules, &tsp->smk_rules_lock, SMK_FIXED24_FMT); @@ -2409,14 +2409,14 @@ static const struct file_operations smk_load2_ops = { static void *load_self2_seq_start(struct seq_file *s, loff_t *pos) { - struct task_smack *tsp = current_security(); + struct task_smack *tsp = smack_cred(current_cred()); return smk_seq_start(s, pos, &tsp->smk_rules); } static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos) { - struct task_smack *tsp = current_security(); + struct task_smack *tsp = smack_cred(current_cred()); return smk_seq_next(s, v, pos, &tsp->smk_rules); } @@ -2462,7 +2462,7 @@ static int smk_open_load_self2(struct inode *inode, struct file *file) static ssize_t smk_write_load_self2(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { - struct task_smack *tsp = current_security(); + struct task_smack *tsp = smack_cred(current_cred()); return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules, &tsp->smk_rules_lock, SMK_LONG_FMT); @@ -2681,14 +2681,14 @@ static const struct file_operations smk_syslog_ops = { static void *relabel_self_seq_start(struct seq_file *s, loff_t *pos) { - struct task_smack *tsp = current_security(); + struct task_smack *tsp = smack_cred(current_cred()); return smk_seq_start(s, pos, &tsp->smk_relabel); } static void *relabel_self_seq_next(struct seq_file *s, void *v, loff_t *pos) { - struct task_smack *tsp = current_security(); + struct task_smack *tsp = smack_cred(current_cred()); return smk_seq_next(s, v, pos, &tsp->smk_relabel); } @@ -2736,7 +2736,7 @@ static int smk_open_relabel_self(struct inode *inode, struct file *file) static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { - struct task_smack *tsp = current_security(); + struct task_smack *tsp = smack_cred(current_cred()); char *data; int rc; LIST_HEAD(list_tmp);