From patchwork Tue Jun 18 23:05:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Casey Schaufler X-Patchwork-Id: 11003047 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7A55E1902 for ; Tue, 18 Jun 2019 23:07:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6CEC828B25 for ; Tue, 18 Jun 2019 23:07:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 617A228B2C; Tue, 18 Jun 2019 23:07:04 +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=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI 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 CEF9C28B26 for ; Tue, 18 Jun 2019 23:07:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730838AbfFRXHD (ORCPT ); Tue, 18 Jun 2019 19:07:03 -0400 Received: from sonic316-20.consmr.mail.bf2.yahoo.com ([74.6.130.194]:41235 "EHLO sonic316-20.consmr.mail.bf2.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730966AbfFRXHD (ORCPT ); Tue, 18 Jun 2019 19:07:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1560899221; bh=5+jmKlHvdVZ7YYwfP2i9ZbNBYe+Vg0tQ8ZkdMDEcwN4=; h=From:To:C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XtomKrxiPhUv52zhogwdArIiA36BE7i6onlE6mv65JjBRX.8 nfk72PZ6X58z7B2GNx61t81DmDw0GhTQwoZQEXje4RVlhpcPnlYOo.tv1Tc6QZRL7xYbZfSQA67C _o7oG2a9Q.J3VktQXxVRe35UIpN.Aif9u3cp7ReqLr5HI1WNNqXL.I70MEMqWBNefSQ8bQYGSiZc Onfu6Msnw2VVCM.4qvjl51Xee8XNiRQsZjt8NeSq9JkftbWUooKkFptHcLMRDc0XUWwSSrvaabk5 1IIMi_iqcqcxHVJJASi4yo0BCItXmhgOXZsu_Jt8VrJ1D6aiCaRTHj5QvFMBKehNiFeody4zYKlJ 7okjTQtJQKYqKBXulWWTOQpl30qCo6EjiU7rwiFIxjGQ9HSs0x5MeZNATXzh2D31H4qVF1c5w4E5 jcvqhNFk285wekJbB2zQMYspsoTId8xtBrNK_OLTjVY2VCVzewIW05_.9pZpWu.IDgA.n.vAzRNQ uoVIG5PP3rWnvccHDXc9XZQFKV_D4cwNSrqwVYiNQmj2oAcW7Z4iPWXS_OzibbGIANlafMKZWUEZ 7BvyT6PsFpgCZ1VP19Bdnl3UdvIdfvZp84eoE6EOn7aj0MgXcfsI- Received: from sonic.gate.mail.ne1.yahoo.com by sonic316.consmr.mail.bf2.yahoo.com with HTTP; Tue, 18 Jun 2019 23:07:01 +0000 Received: from c-73-223-4-185.hsd1.ca.comcast.net (EHLO localhost.net) ([73.223.4.185]) by smtp416.mail.bf1.yahoo.com (Oath Hermes SMTP Server) with ESMTPA ID 9bd52d88915a86244e569f3c22bd3931; Tue, 18 Jun 2019 23:06:59 +0000 (UTC) From: Casey Schaufler To: casey.schaufler@intel.com, jmorris@namei.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org Cc: casey@schaufler-ca.com, keescook@chromium.org, john.johansen@canonical.com, penguin-kernel@i-love.sakura.ne.jp, paul@paul-moore.com, sds@tycho.nsa.gov Subject: [PATCH v2 18/25] LSM: Use lsmcontext in security_dentry_init_security Date: Tue, 18 Jun 2019 16:05:44 -0700 Message-Id: <20190618230551.7475-19-casey@schaufler-ca.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190618230551.7475-1-casey@schaufler-ca.com> References: <20190618230551.7475-1-casey@schaufler-ca.com> MIME-Version: 1.0 Sender: selinux-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Chance the security_dentry_init_security() interface to fill an lsmcontext structure instead of a void * data area and a length. The lone caller of this interface is NFS4, which may make copies of the data using its own mechanisms. A rework of the nfs4 code to use the lsmcontext properly is a significant project, so the coward's way out is taken, and the lsmcontext data from security_dentry_init_security() is copied, then released directly. Signed-off-by: Casey Schaufler --- fs/nfs/nfs4proc.c | 26 ++++++++++++++++---------- include/linux/security.h | 7 +++---- security/security.c | 20 ++++++++++++++++---- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/fs/nfs/nfs4proc.c b/fs/nfs/nfs4proc.c index af1c0db29c39..952f805965bb 100644 --- a/fs/nfs/nfs4proc.c +++ b/fs/nfs/nfs4proc.c @@ -113,6 +113,7 @@ static inline struct nfs4_label * nfs4_label_init_security(struct inode *dir, struct dentry *dentry, struct iattr *sattr, struct nfs4_label *label) { + struct lsmcontext context; int err; if (label == NULL) @@ -122,21 +123,26 @@ nfs4_label_init_security(struct inode *dir, struct dentry *dentry, return NULL; err = security_dentry_init_security(dentry, sattr->ia_mode, - &dentry->d_name, (void **)&label->label, &label->len); - if (err == 0) - return label; + &dentry->d_name, &context); + + if (err) + return NULL; + + label->label = kmemdup(context.context, context.len, GFP_KERNEL); + if (label->label == NULL) + label = NULL; + else + label->len = context.len; + + security_release_secctx(&context); + + return label; - return NULL; } static inline void nfs4_label_release_security(struct nfs4_label *label) { - struct lsmcontext scaff; /* scaffolding */ - - if (label) { - lsmcontext_init(&scaff, label->label, label->len, 0); - security_release_secctx(&scaff); - } + kfree(label->label); } static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label) { diff --git a/include/linux/security.h b/include/linux/security.h index 1fd87e80656f..92c4960dd57f 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -346,8 +346,8 @@ int security_sb_clone_mnt_opts(const struct super_block *oldsb, int security_add_mnt_opt(const char *option, const char *val, int len, void **mnt_opts); int security_dentry_init_security(struct dentry *dentry, int mode, - const struct qstr *name, void **ctx, - u32 *ctxlen); + const struct qstr *name, + struct lsmcontext *ctx); int security_dentry_create_files_as(struct dentry *dentry, int mode, struct qstr *name, const struct cred *old, @@ -718,8 +718,7 @@ static inline void security_inode_free(struct inode *inode) static inline int security_dentry_init_security(struct dentry *dentry, int mode, const struct qstr *name, - void **ctx, - u32 *ctxlen) + struct lsmcontext *ctx) { return -EOPNOTSUPP; } diff --git a/security/security.c b/security/security.c index 2ea810fc4a45..23d8049ec0c1 100644 --- a/security/security.c +++ b/security/security.c @@ -453,6 +453,8 @@ void __init security_add_hooks(struct security_hook_list *hooks, int count, * secid in the lsmblob structure. */ if (hooks[i].head == &security_hook_heads.audit_rule_match || + hooks[i].head == + &security_hook_heads.dentry_init_security || hooks[i].head == &security_hook_heads.kernel_act_as || hooks[i].head == &security_hook_heads.socket_getpeersec_dgram || @@ -1030,11 +1032,21 @@ void security_inode_free(struct inode *inode) } int security_dentry_init_security(struct dentry *dentry, int mode, - const struct qstr *name, void **ctx, - u32 *ctxlen) + const struct qstr *name, + struct lsmcontext *cp) { - return call_int_hook(dentry_init_security, -EOPNOTSUPP, dentry, mode, - name, ctx, ctxlen); + int *display = current->security; + struct security_hook_list *hp; + + hlist_for_each_entry(hp, &security_hook_heads.dentry_init_security, + list) + if (*display == 0 || *display == hp->slot) { + cp->slot = hp->slot; + return hp->hook.dentry_init_security(dentry, mode, + name, (void **)&cp->context, &cp->len); + } + + return -EOPNOTSUPP; } EXPORT_SYMBOL(security_dentry_init_security);