4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see http://www.gnu.org/licenses
23 * Copyright (c) 2014 Bull SAS
25 * Copyright (c) 2015, 2016, Intel Corporation.
26 * Author: Sebastien Buisson sebastien.buisson@bull.net
30 * lustre/llite/xattr_security.c
31 * Handler for storing security labels as extended attributes.
34 #include <linux/types.h>
35 #include <linux/security.h>
36 #ifdef HAVE_LINUX_SELINUX_IS_ENABLED
37 #include <linux/selinux.h>
39 #include <linux/xattr.h>
40 #include "llite_internal.h"
42 #ifndef XATTR_SELINUX_SUFFIX
43 # define XATTR_SELINUX_SUFFIX "selinux"
46 #ifndef XATTR_NAME_SELINUX
47 # define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX
51 * Check for LL_SBI_FILE_SECCTX before calling.
53 int ll_dentry_init_security(struct dentry *dentry, int mode, struct qstr *name,
54 const char **secctx_name, void **secctx,
60 * security_dentry_init_security() is strange. Like
61 * security_inode_init_security() it may return a context (provided a
62 * Linux security module is enabled) but unlike
63 * security_inode_init_security() it does not return to us the name of
64 * the extended attribute to store the context under (for example
65 * "security.selinux"). So we only call it when we think we know what
66 * the name of the extended attribute will be. This is OK-ish since
67 * SELinux is the only module that implements
68 * security_dentry_init_security(). Note that the NFS client code just
69 * calls it and assumes that if anything is returned then it must come
73 if (!selinux_is_enabled())
76 rc = security_dentry_init_security(dentry, mode, name, secctx,
78 if (rc == -EOPNOTSUPP)
83 *secctx_name = XATTR_NAME_SELINUX;
89 * A helper function for security_inode_init_security()
90 * that takes care of setting xattrs
92 * Get security context of @inode from @xattr_array,
93 * and put it in 'security.xxx' xattr of dentry
97 * \retval -ENOMEM if no memory could be allocated for xattr name
98 * \retval < 0 failure to set xattr
101 ll_initxattrs(struct inode *inode, const struct xattr *xattr_array,
104 struct dentry *dentry = fs_info;
105 const struct xattr *xattr;
108 for (xattr = xattr_array; xattr->name; xattr++) {
111 full_name = kasprintf(GFP_KERNEL, "%s%s",
112 XATTR_SECURITY_PREFIX, xattr->name);
118 err = ll_vfs_setxattr(dentry, inode, full_name, xattr->value,
119 xattr->value_len, XATTR_CREATE);
128 * Initializes security context
130 * Get security context of @inode in @dir,
131 * and put it in 'security.xxx' xattr of @dentry.
133 * \retval 0 success, or SELinux is disabled
134 * \retval -ENOMEM if no memory could be allocated for xattr name
135 * \retval < 0 failure to get security context or set xattr
138 ll_inode_init_security(struct dentry *dentry, struct inode *inode,
143 if (!selinux_is_enabled())
146 rc = security_inode_init_security(inode, dir, NULL,
147 &ll_initxattrs, dentry);
148 if (rc == -EOPNOTSUPP)
155 * Get security context xattr name used by policy.
157 * \retval >= 0 length of xattr name
158 * \retval < 0 failure to get security context xattr name
161 ll_listsecurity(struct inode *inode, char *secctx_name, size_t secctx_name_size)
165 if (!selinux_is_enabled())
168 rc = security_inode_listsecurity(inode, secctx_name, secctx_name_size);
169 if (rc >= secctx_name_size)
172 secctx_name[rc] = '\0';