From patchwork Wed Jan 9 16:28:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ondrej Mosnacek X-Patchwork-Id: 10754463 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 A9AF114DE for ; Wed, 9 Jan 2019 16:29:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 98499292F3 for ; Wed, 9 Jan 2019 16:29:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8CF6529304; Wed, 9 Jan 2019 16:29:00 +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,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 0DDB2292D8 for ; Wed, 9 Jan 2019 16:29:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725535AbfAIQ2y (ORCPT ); Wed, 9 Jan 2019 11:28:54 -0500 Received: from mail-wr1-f68.google.com ([209.85.221.68]:35704 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726087AbfAIQ2x (ORCPT ); Wed, 9 Jan 2019 11:28:53 -0500 Received: by mail-wr1-f68.google.com with SMTP id 96so8320992wrb.2 for ; Wed, 09 Jan 2019 08:28:51 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gSGo71MvUVspkie9GwhrspwHi6UUZoiqR33nTq1pDDE=; b=nG1YnU6XX633DpQqj8NIjf1VIZGCyny54F4Iltg3XAuy/bzOPEs/4vJtosX8LOLNae usMhYUC+OF7KKVHCkSEqCkJUQP7LQ8vFvgYt8hBtlquAs1igzEXUCBgN27cu+NtiiRKq B0lVyDkBC4M11TgszXifAkcFF33Nt/IE9iv9eMPFfZ49bifkhY2ZsE0uGovtU4YMD1uu 1YtexLbOt6Ik1vXfJqF8v+OICuDtoI9uAMGjxtRyfOv1nFCu9CMjcwfksAbkEdFJosQv EOwROhYOfHH1TV9YTXu0esyYKOsRdSpULTWoFCOtCq6JMEnlWNkAIEbJXhmDgCdp57Mi ruDg== X-Gm-Message-State: AJcUukf+aFX+5v9d0ZxaM8hVWk556krN6D2EGbfdAG/0zUSqCvQk8DwD 5UjNGKNHd281D6f8r2fOudvdBzshKZM= X-Google-Smtp-Source: ALg8bN5z+Kc3uvEW27PAqliKYup8u1mgW4Sjhrer+xcoqph97R9SKccYCJH5gQ3HhVRTwpPoULgpgA== X-Received: by 2002:adf:e488:: with SMTP id i8mr5908936wrm.10.1547051330463; Wed, 09 Jan 2019 08:28:50 -0800 (PST) Received: from localhost.localdomain.com (nat-pool-brq-t.redhat.com. [213.175.37.10]) by smtp.gmail.com with ESMTPSA id a17sm13661438wma.15.2019.01.09.08.28.48 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 09 Jan 2019 08:28:49 -0800 (PST) From: Ondrej Mosnacek To: selinux@vger.kernel.org, Paul Moore Cc: Stephen Smalley , linux-security-module@vger.kernel.org, Greg Kroah-Hartman , Tejun Heo , linux-fsdevel@vger.kernel.org, cgroups@vger.kernel.org, Ondrej Mosnacek Subject: [PATCH v2 1/3] LSM: Add new hook for generic node initialization Date: Wed, 9 Jan 2019 17:28:28 +0100 Message-Id: <20190109162830.8309-2-omosnace@redhat.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190109162830.8309-1-omosnace@redhat.com> References: <20190109162830.8309-1-omosnace@redhat.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 This patch introduces a new security hook that is intended for initializing the security data for newly created pseudo filesystem objects (such as kernfs nodes) that provide a way of storing a non-default security context, but need to operate independently from mounts. The main motivation is to allow kernfs nodes to inherit the context of the parent under SELinux, similar to the behavior of security_inode_init_security(). Other LSMs may implement their own logic for handling the creation of new nodes. Signed-off-by: Ondrej Mosnacek --- include/linux/lsm_hooks.h | 30 ++++++++++++++++++++++++++++++ include/linux/security.h | 14 ++++++++++++++ security/security.c | 10 ++++++++++ 3 files changed, 54 insertions(+) diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h index aaeb7fa24dc4..3a2399d7721f 100644 --- a/include/linux/lsm_hooks.h +++ b/include/linux/lsm_hooks.h @@ -429,6 +429,31 @@ * to abort the copy up. Note that the caller is responsible for reading * and writing the xattrs as this hook is merely a filter. * + * Security hooks for special file-like objects + * + * @object_init_security: + * Obtain the security context for a newly created filesystem object + * based on the security context of the parent node. The purpose is + * similar to @inode_init_security, but this hook is intended for + * non-inode objects that need to behave like a directory tree (e.g. + * kernfs nodes). In this case it is assumed that the LSM assigns some + * default context to the node by default and the object internally stores + * a copy of the security context if (and only if) it has been set to a + * non-default value explicitly (e.g. via *setxattr(2)). + * + * @parent_ctx contains the security context of the parent directory + * (must not be NULL -- if the parent has no explicit context set, + * the child should also keep the default context and the hook should + * not be called). + * @parent_ctxlen contains the length of @parent_ctx data. + * @qstr contains the last path component of the new object. + * @mode contanis the file mode of the object. + * @ctx is a pointer in which to place the allocated security context. + * @ctxlen points to the place to put the length of @ctx. + * + * Returns 0 if @ctx and @ctxlen have been successfully set or + * -ENOMEM on memory allocation failure. + * * Security hooks for file operations * * @file_permission: @@ -1556,6 +1581,10 @@ union security_list_options { int (*inode_copy_up)(struct dentry *src, struct cred **new); int (*inode_copy_up_xattr)(const char *name); + int (*object_init_security)(void *parent_ctx, u32 parent_ctxlen, + const struct qstr *qstr, u16 mode, + void **ctx, u32 *ctxlen); + int (*file_permission)(struct file *file, int mask); int (*file_alloc_security)(struct file *file); void (*file_free_security)(struct file *file); @@ -1855,6 +1884,7 @@ struct security_hook_heads { struct hlist_head inode_getsecid; struct hlist_head inode_copy_up; struct hlist_head inode_copy_up_xattr; + struct hlist_head object_init_security; struct hlist_head file_permission; struct hlist_head file_alloc_security; struct hlist_head file_free_security; diff --git a/include/linux/security.h b/include/linux/security.h index d170a5b031f3..1e7971d10fe6 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -315,6 +315,9 @@ int security_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer void security_inode_getsecid(struct inode *inode, u32 *secid); int security_inode_copy_up(struct dentry *src, struct cred **new); int security_inode_copy_up_xattr(const char *name); +int security_object_init_security(void *parent_ctx, u32 parent_ctxlen, + const struct qstr *qstr, u16 mode, + void **ctx, u32 *ctxlen); int security_file_permission(struct file *file, int mask); int security_file_alloc(struct file *file); void security_file_free(struct file *file); @@ -815,6 +818,17 @@ static inline int security_inode_copy_up_xattr(const char *name) return -EOPNOTSUPP; } +static inline int security_object_init_security(void *parent_ctx, + u32 parent_ctxlen, + const struct qstr *qstr, + u16 mode, void **ctx, + u32 *ctxlen) +{ + *ctx = NULL; + *ctxlen = 0; + return 0; +} + static inline int security_file_permission(struct file *file, int mask) { return 0; diff --git a/security/security.c b/security/security.c index 04d173eb93f6..a010bfbe3fc6 100644 --- a/security/security.c +++ b/security/security.c @@ -879,6 +879,16 @@ int security_inode_copy_up_xattr(const char *name) } EXPORT_SYMBOL(security_inode_copy_up_xattr); +int security_object_init_security(void *parent_ctx, u32 parent_ctxlen, + const struct qstr *qstr, u16 mode, + void **ctx, u32 *ctxlen) +{ + *ctx = NULL; + *ctxlen = 0; + return call_int_hook(object_init_security, 0, parent_ctx, parent_ctxlen, + qstr, mode, ctx, ctxlen); +} + int security_file_permission(struct file *file, int mask) { int ret; From patchwork Wed Jan 9 16:28:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ondrej Mosnacek X-Patchwork-Id: 10754473 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 594E118E8 for ; Wed, 9 Jan 2019 16:29:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 46496292C6 for ; Wed, 9 Jan 2019 16:29:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3A7AD292D9; Wed, 9 Jan 2019 16:29:01 +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,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 4F56E292C6 for ; Wed, 9 Jan 2019 16:29:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726198AbfAIQ2z (ORCPT ); Wed, 9 Jan 2019 11:28:55 -0500 Received: from mail-wr1-f67.google.com ([209.85.221.67]:34614 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726206AbfAIQ2y (ORCPT ); Wed, 9 Jan 2019 11:28:54 -0500 Received: by mail-wr1-f67.google.com with SMTP id j2so8343541wrw.1 for ; Wed, 09 Jan 2019 08:28:53 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gUKYH05t5p4O+cmOA3k/UE0l2qyG664+3B+v8a8N7S0=; b=i4xfVJzuGhBN7vn2DIAqDINd78HRP3wTd9n1NkGYCD3Nm+EiNrrYz1lejHnuGJy+PO osajIBy3jeiQxpfs9r2i/abpoU0Ir26hiUftQXEJUf3VJaj62B3AoUlgUH9ooTU8hBCo XTsznnmkYojVnY37QVxAgqTwoolj7/Vlek0wDp6CXI7IkH2apbPDmZHUHvrKhbIib9k+ 1iuPJ6ge2VlT4a3vmrsIioEihVmUmAd7MLt9mzcrL102LN9jqFjn1e+Jz48ywVpCwiwM IQb5naCGj0mw/d525630vdsGVSnuFXEvmtjJ8xSE/Xhk0GiWsDZ4g0FRYaHoLKadADpb OAIA== X-Gm-Message-State: AJcUukdGiDkFwZnMaYvT6oSIu86unol+kbIVqp64IQcmP/6DIv2b7qmn wEXR9yD4oBb8rH8akuExImH2W/YZMHM= X-Google-Smtp-Source: ALg8bN6J5pHuVP32MSuo+49cH2swPLCPzGxXZqEs0yOj1SdXDlNSXWh12ZsH7rgztwgnOMf9RzRIhg== X-Received: by 2002:a5d:6244:: with SMTP id m4mr5546430wrv.314.1547051332035; Wed, 09 Jan 2019 08:28:52 -0800 (PST) Received: from localhost.localdomain.com (nat-pool-brq-t.redhat.com. [213.175.37.10]) by smtp.gmail.com with ESMTPSA id a17sm13661438wma.15.2019.01.09.08.28.50 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 09 Jan 2019 08:28:50 -0800 (PST) From: Ondrej Mosnacek To: selinux@vger.kernel.org, Paul Moore Cc: Stephen Smalley , linux-security-module@vger.kernel.org, Greg Kroah-Hartman , Tejun Heo , linux-fsdevel@vger.kernel.org, cgroups@vger.kernel.org, Ondrej Mosnacek Subject: [PATCH v2 2/3] selinux: Implement the object_init_security hook Date: Wed, 9 Jan 2019 17:28:29 +0100 Message-Id: <20190109162830.8309-3-omosnace@redhat.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190109162830.8309-1-omosnace@redhat.com> References: <20190109162830.8309-1-omosnace@redhat.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 The hook applies the same logic as selinux_determine_inode_label(), with the exception of the super_block handling, which will be enforced on the actual inodes by other hooks. Signed-off-by: Ondrej Mosnacek Reviewed-by: Stephen Smalley --- security/selinux/hooks.c | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 7ce012d9ec51..29c038513504 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -3526,6 +3526,45 @@ static int selinux_inode_copy_up_xattr(const char *name) return -EOPNOTSUPP; } +/* file-like object operations */ + +/* Used e.g. for kernfs_node for newly created nodes */ +static int selinux_object_init_security(void *parent_ctx, u32 parent_ctxlen, + const struct qstr *qstr, u16 mode, + void **ctx, u32 *ctxlen) +{ + const struct task_security_struct *tsec = current_security(); + u32 parent_sid, newsid, clen; + int rc; + char *context; + + rc = security_context_to_sid(&selinux_state, parent_ctx, parent_ctxlen, + &parent_sid, GFP_KERNEL); + if (rc) + return rc; + + if (tsec->create_sid) { + newsid = tsec->create_sid; + } else { + u16 secclass = inode_mode_to_security_class(mode); + + rc = security_transition_sid(&selinux_state, tsec->sid, + parent_sid, secclass, qstr, + &newsid); + if (rc) + return rc; + } + + rc = security_sid_to_context_force(&selinux_state, newsid, + &context, &clen); + if (rc) + return rc; + + *ctx = context; + *ctxlen = clen; + return 0; +} + /* file security operations */ static int selinux_revalidate_file_permission(struct file *file, int mask) @@ -6965,6 +7004,8 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up), LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr), + LSM_HOOK_INIT(object_init_security, selinux_object_init_security), + 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), From patchwork Wed Jan 9 16:28:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ondrej Mosnacek X-Patchwork-Id: 10754477 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 7765613BF for ; Wed, 9 Jan 2019 16:29:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 687CC292CD for ; Wed, 9 Jan 2019 16:29:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5D01D292C6; Wed, 9 Jan 2019 16:29:08 +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,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 D20C6292CF for ; Wed, 9 Jan 2019 16:29:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726255AbfAIQ27 (ORCPT ); Wed, 9 Jan 2019 11:28:59 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:46564 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726239AbfAIQ2z (ORCPT ); Wed, 9 Jan 2019 11:28:55 -0500 Received: by mail-wr1-f65.google.com with SMTP id l9so8262755wrt.13 for ; Wed, 09 Jan 2019 08:28:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=fRDDqmr3Vwt5yZVlqcMbm6I6d7BK2hVZRky0qELkXvU=; b=Y63zFbQr9S6KotSVGbNY5pkXa7hsnvWh39gFgjm3xxElxDu+s1F53IjoI5TzSrH0sy FnBy9WN31YkZUPSDdEpYafzfTxLs8ugJnMT/Hd/GasS722V98gdY+AKs7HqaBiX/IzIR TPfu5GxHcOfdi8SjGHhXotruHE2G2ANafXfvVlPwkr6tDdRzVLTCqMSqglb0/14AP9rI w6gGNxnsN5PQ14NFYcFPpFYYBVLna5yLJy3cTgeSAKpbQsDhYLQvfvgLc8iYoFXZ1XSG h9PVOg88Fi6XBGLO2hvQ+M7BYOIxlGSB2IxjEIZ1Jl3qO1tX0BW4R+HXgygreOJuMRt7 Un4A== X-Gm-Message-State: AJcUukfgrQaohu8BAsf61aj5TMhfUSP4PzsIHGJSwso3oNqg+GInr/eE WFy93GIwQ7J3QTQhC4o3Rz3CQs2Zdfk= X-Google-Smtp-Source: ALg8bN5socWIWRP3eTHOqxhvYBqZihCXAong9TjQcm79TqyuN8r/u/HPX4Pe0aCXwlxJxBQTR1tqhw== X-Received: by 2002:adf:f009:: with SMTP id j9mr5565083wro.170.1547051333255; Wed, 09 Jan 2019 08:28:53 -0800 (PST) Received: from localhost.localdomain.com (nat-pool-brq-t.redhat.com. [213.175.37.10]) by smtp.gmail.com with ESMTPSA id a17sm13661438wma.15.2019.01.09.08.28.52 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 09 Jan 2019 08:28:52 -0800 (PST) From: Ondrej Mosnacek To: selinux@vger.kernel.org, Paul Moore Cc: Stephen Smalley , linux-security-module@vger.kernel.org, Greg Kroah-Hartman , Tejun Heo , linux-fsdevel@vger.kernel.org, cgroups@vger.kernel.org, Ondrej Mosnacek Subject: [PATCH v2 3/3] kernfs: Initialize security of newly created nodes Date: Wed, 9 Jan 2019 17:28:30 +0100 Message-Id: <20190109162830.8309-4-omosnace@redhat.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190109162830.8309-1-omosnace@redhat.com> References: <20190109162830.8309-1-omosnace@redhat.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 Use the new security_object_init_security() hook to allow LSMs to possibly assign a non-default security context to newly created nodes based on the context of their parent node. This fixes an issue with cgroupfs under SELinux, where newly created cgroup subdirectories would not inherit its parent's context if it had been set explicitly to a non-default value (other than the genfs context specified by the policy). This can be reproduced as follows: # mkdir /sys/fs/cgroup/unified/test # chcon -R system_u:object_r:cgroup_t:s0:c123 /sys/fs/cgroup/unified/test # ls -lZ /sys/fs/cgroup/unified total 0 -r--r--r--. 1 root root system_u:object_r:cgroup_t:s0 0 Jan 8 05:00 cgroup.controllers -rw-r--r--. 1 root root system_u:object_r:cgroup_t:s0 0 Jan 8 05:00 cgroup.max.depth -rw-r--r--. 1 root root system_u:object_r:cgroup_t:s0 0 Jan 8 05:00 cgroup.max.descendants -rw-r--r--. 1 root root system_u:object_r:cgroup_t:s0 0 Jan 8 05:00 cgroup.procs -r--r--r--. 1 root root system_u:object_r:cgroup_t:s0 0 Jan 8 05:00 cgroup.stat -rw-r--r--. 1 root root system_u:object_r:cgroup_t:s0 0 Jan 8 05:00 cgroup.subtree_control -rw-r--r--. 1 root root system_u:object_r:cgroup_t:s0 0 Jan 8 05:00 cgroup.threads drwxr-xr-x. 2 root root system_u:object_r:cgroup_t:s0 0 Jan 8 04:54 init.scope drwxr-xr-x. 25 root root system_u:object_r:cgroup_t:s0 0 Jan 8 04:54 system.slice drwxr-xr-x. 2 root root system_u:object_r:cgroup_t:s0:c123 0 Jan 8 04:59 test drwxr-xr-x. 3 root root system_u:object_r:cgroup_t:s0 0 Jan 8 04:55 user.slice # mkdir /sys/fs/cgroup/unified/test/subdir Actual result: # ls -ldZ /sys/fs/cgroup/unified/test/subdir drwxr-xr-x. 2 root root system_u:object_r:cgroup_t:s0 0 Jan 8 05:10 /sys/fs/cgroup/unified/test/subdir Expected result: # ls -ldZ /sys/fs/cgroup/unified/test/subdir drwxr-xr-x. 2 root root unconfined_u:object_r:cgroup_t:s0:c123 0 Jan 8 05:10 /sys/fs/cgroup/unified/test/subdir Link: https://github.com/SELinuxProject/selinux-kernel/issues/39 Signed-off-by: Ondrej Mosnacek Reviewed-by: Stephen Smalley --- fs/kernfs/dir.c | 49 ++++++++++++++++++++++++++++++++++--- fs/kernfs/inode.c | 9 +++---- fs/kernfs/kernfs-internal.h | 4 +++ 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 4ca0b5c18192..8a678a934f65 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "kernfs-internal.h" @@ -617,7 +618,43 @@ struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry) return NULL; } -static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root, +static int kernfs_node_init_security(struct kernfs_node *parent, + struct kernfs_node *kn, umode_t mode) +{ + struct kernfs_iattrs *attrs; + struct qstr q; + void *ctx; + u32 ctxlen; + int ret; + + /* If parent has no explicit context set, leave child unset as well */ + if (!parent->iattr) + return 0; + if (!parent->iattr->ia_secdata || !parent->iattr->ia_secdata_len) + return 0; + + q.name = kn->name; + q.hash_len = hashlen_string(parent, kn->name); + + ret = security_object_init_security(parent->iattr->ia_secdata, + parent->iattr->ia_secdata_len, + &q, (u16)mode, &ctx, &ctxlen); + if (ret) + return ret; + + attrs = kernfs_iattrs(kn); + if (!attrs) { + security_release_secctx(ctx, ctxlen); + return -ENOMEM; + } + + kernfs_node_setsecdata(attrs, &ctx, &ctxlen); + /* The inode is fresh, so the returned ctx is always NULL. */ + return 0; +} + +static struct kernfs_node *__kernfs_new_node(struct kernfs_node *parent, + struct kernfs_root *root, const char *name, umode_t mode, kuid_t uid, kgid_t gid, unsigned flags) @@ -674,6 +711,12 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root, goto err_out3; } + if (parent) { + ret = kernfs_node_init_security(parent, kn, mode); + if (ret) + goto err_out3; + } + return kn; err_out3: @@ -692,7 +735,7 @@ struct kernfs_node *kernfs_new_node(struct kernfs_node *parent, { struct kernfs_node *kn; - kn = __kernfs_new_node(kernfs_root(parent), + kn = __kernfs_new_node(parent, kernfs_root(parent), name, mode, uid, gid, flags); if (kn) { kernfs_get(parent); @@ -962,7 +1005,7 @@ struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops, INIT_LIST_HEAD(&root->supers); root->next_generation = 1; - kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO, + kn = __kernfs_new_node(NULL, root, "", S_IFDIR | S_IRUGO | S_IXUGO, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, KERNFS_DIR); if (!kn) { diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c index 80cebcd94c90..e6db8d23437b 100644 --- a/fs/kernfs/inode.c +++ b/fs/kernfs/inode.c @@ -31,7 +31,7 @@ static const struct inode_operations kernfs_iops = { .listxattr = kernfs_iop_listxattr, }; -static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn) +struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn) { static DEFINE_MUTEX(iattr_mutex); struct kernfs_iattrs *ret; @@ -135,8 +135,8 @@ out: return error; } -static int kernfs_node_setsecdata(struct kernfs_iattrs *attrs, void **secdata, - u32 *secdata_len) +void kernfs_node_setsecdata(struct kernfs_iattrs *attrs, void **secdata, + u32 *secdata_len) { void *old_secdata; size_t old_secdata_len; @@ -149,7 +149,6 @@ static int kernfs_node_setsecdata(struct kernfs_iattrs *attrs, void **secdata, *secdata = old_secdata; *secdata_len = old_secdata_len; - return 0; } ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size) @@ -365,7 +364,7 @@ static int kernfs_security_xattr_set(const struct xattr_handler *handler, return error; mutex_lock(&kernfs_mutex); - error = kernfs_node_setsecdata(attrs, &secdata, &secdata_len); + kernfs_node_setsecdata(attrs, &secdata, &secdata_len); mutex_unlock(&kernfs_mutex); if (secdata) diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h index 3d83b114bb08..f6fb2df24c30 100644 --- a/fs/kernfs/kernfs-internal.h +++ b/fs/kernfs/kernfs-internal.h @@ -92,6 +92,10 @@ int kernfs_iop_getattr(const struct path *path, struct kstat *stat, ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size); int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr); +struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn); +void kernfs_node_setsecdata(struct kernfs_iattrs *attrs, void **secdata, + u32 *secdata_len); + /* * dir.c */