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 #include <linux/selinux.h>
37 #include <linux/xattr.h>
38 #include "llite_internal.h"
40 #ifndef XATTR_SELINUX_SUFFIX
41 # define XATTR_SELINUX_SUFFIX "selinux"
44 #ifndef XATTR_NAME_SELINUX
45 # define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX
49 * Check for LL_SBI_FILE_SECCTX before calling.
51 int ll_dentry_init_security(struct dentry *dentry, int mode, struct qstr *name,
52 const char **secctx_name, void **secctx,
55 #ifdef HAVE_SECURITY_DENTRY_INIT_SECURITY
58 /* security_dentry_init_security() is strange. Like
59 * security_inode_init_security() it may return a context (provided a
60 * Linux security module is enabled) but unlike
61 * security_inode_init_security() it does not return to us the name of
62 * the extended attribute to store the context under (for example
63 * "security.selinux"). So we only call it when we think we know what
64 * the name of the extended attribute will be. This is OK-ish since
65 * SELinux is the only module that implements
66 * security_dentry_init_security(). Note that the NFS client code just
67 * calls it and assumes that if anything is returned then it must come
70 if (!selinux_is_enabled())
73 rc = security_dentry_init_security(dentry, mode, name, secctx,
78 *secctx_name = XATTR_NAME_SELINUX;
79 #endif /* HAVE_SECURITY_DENTRY_INIT_SECURITY */
84 #ifdef HAVE_SECURITY_IINITSEC_CALLBACK
86 * A helper function for ll_security_inode_init_security()
87 * that takes care of setting xattrs
89 * Get security context of @inode from @xattr_array,
90 * and put it in 'security.xxx' xattr of dentry
94 * \retval -ENOMEM if no memory could be allocated for xattr name
95 * \retval < 0 failure to set xattr
98 ll_initxattrs(struct inode *inode, const struct xattr *xattr_array,
101 const struct xattr *xattr;
102 struct dentry *dentry = fs_info;
107 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
108 name_len = strlen(XATTR_SECURITY_PREFIX) + strlen(xattr->name)
110 OBD_ALLOC(full_name, name_len);
111 if (full_name == NULL)
113 strlcpy(full_name, XATTR_SECURITY_PREFIX, name_len);
114 strlcat(full_name, xattr->name, name_len);
116 err = ll_setxattr(dentry, full_name, xattr->value,
117 xattr->value_len, 0);
119 OBD_FREE(full_name, name_len);
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,
141 if (!selinux_is_enabled())
144 return ll_security_inode_init_security(inode, dir, NULL, NULL, 0,
145 &ll_initxattrs, dentry);
147 #else /* !HAVE_SECURITY_IINITSEC_CALLBACK */
149 * Initializes security context
151 * Get security context of @inode in @dir,
152 * and put it in 'security.xxx' xattr of @dentry.
154 * \retval 0 success, or SELinux is disabled
155 * \retval -ENOMEM if no memory could be allocated for xattr name
156 * \retval < 0 failure to get security context or set xattr
159 ll_inode_init_security(struct dentry *dentry, struct inode *inode,
163 size_t len, name_len;
165 char *name, *full_name;
167 if (!selinux_is_enabled())
170 err = ll_security_inode_init_security(inode, dir, &name, &value, &len,
173 if (err == -EOPNOTSUPP)
178 name_len = strlen(XATTR_SECURITY_PREFIX) + strlen(name) + 1;
179 OBD_ALLOC(full_name, name_len);
180 if (full_name == NULL)
181 GOTO(out_free, err = -ENOMEM);
182 strlcpy(full_name, XATTR_SECURITY_PREFIX, name_len);
183 strlcat(full_name, name, name_len);
185 err = ll_setxattr(dentry, full_name, value, len, 0);
186 OBD_FREE(full_name, name_len);
194 #endif /* HAVE_SECURITY_IINITSEC_CALLBACK */