Whamcloud - gitweb
LU-12624 lod: alloc dir stripes by QoS
[fs/lustre-release.git] / lustre / llite / xattr_security.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
18  *
19  * GPL HEADER END
20  */
21
22 /*
23  * Copyright (c) 2014 Bull SAS
24  *
25  * Copyright (c) 2015, 2016, Intel Corporation.
26  * Author: Sebastien Buisson sebastien.buisson@bull.net
27  */
28
29 /*
30  * lustre/llite/xattr_security.c
31  * Handler for storing security labels as extended attributes.
32  */
33
34 #include <linux/types.h>
35 #include <linux/security.h>
36 #ifdef HAVE_LINUX_SELINUX_IS_ENABLED
37 #include <linux/selinux.h>
38 #endif
39 #include <linux/xattr.h>
40 #include "llite_internal.h"
41
42 #ifndef XATTR_SELINUX_SUFFIX
43 # define XATTR_SELINUX_SUFFIX "selinux"
44 #endif
45
46 #ifndef XATTR_NAME_SELINUX
47 # define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX
48 #endif
49
50 /*
51  * Check for LL_SBI_FILE_SECCTX before calling.
52  */
53 int ll_dentry_init_security(struct dentry *dentry, int mode, struct qstr *name,
54                             const char **secctx_name, void **secctx,
55                             __u32 *secctx_size)
56 {
57 #ifdef HAVE_SECURITY_DENTRY_INIT_SECURITY
58         int rc;
59
60         /*
61          * security_dentry_init_security() is strange. Like
62          * security_inode_init_security() it may return a context (provided a
63          * Linux security module is enabled) but unlike
64          * security_inode_init_security() it does not return to us the name of
65          * the extended attribute to store the context under (for example
66          * "security.selinux"). So we only call it when we think we know what
67          * the name of the extended attribute will be. This is OK-ish since
68          * SELinux is the only module that implements
69          * security_dentry_init_security(). Note that the NFS client code just
70          * calls it and assumes that if anything is returned then it must come
71          * from SELinux.
72          */
73
74         if (!selinux_is_enabled())
75                 return 0;
76
77         rc = security_dentry_init_security(dentry, mode, name, secctx,
78                                            secctx_size);
79         if (rc == -EOPNOTSUPP)
80                 return 0;
81         if (rc < 0)
82                 return rc;
83
84         *secctx_name = XATTR_NAME_SELINUX;
85 #endif /* HAVE_SECURITY_DENTRY_INIT_SECURITY */
86
87         return 0;
88 }
89
90 #ifdef HAVE_SECURITY_IINITSEC_CALLBACK
91 /**
92  * A helper function for ll_security_inode_init_security()
93  * that takes care of setting xattrs
94  *
95  * Get security context of @inode from @xattr_array,
96  * and put it in 'security.xxx' xattr of dentry
97  * stored in @fs_info.
98  *
99  * \retval 0        success
100  * \retval -ENOMEM  if no memory could be allocated for xattr name
101  * \retval < 0      failure to set xattr
102  */
103 static int
104 ll_initxattrs(struct inode *inode, const struct xattr *xattr_array,
105               void *fs_info)
106 {
107         struct dentry *dentry = fs_info;
108         const struct xattr *xattr;
109         int err = 0;
110
111         for (xattr = xattr_array; xattr->name; xattr++) {
112                 char *full_name;
113
114                 full_name = kasprintf(GFP_KERNEL, "%s%s",
115                                       XATTR_SECURITY_PREFIX, xattr->name);
116                 if (!full_name) {
117                         err = -ENOMEM;
118                         break;
119                 }
120
121                 err = __vfs_setxattr(dentry, inode, full_name, xattr->value,
122                                      xattr->value_len, XATTR_CREATE);
123                 kfree(full_name);
124                 if (err < 0)
125                         break;
126         }
127         return err;
128 }
129
130 /**
131  * Initializes security context
132  *
133  * Get security context of @inode in @dir,
134  * and put it in 'security.xxx' xattr of @dentry.
135  *
136  * \retval 0        success, or SELinux is disabled
137  * \retval -ENOMEM  if no memory could be allocated for xattr name
138  * \retval < 0      failure to get security context or set xattr
139  */
140 int
141 ll_inode_init_security(struct dentry *dentry, struct inode *inode,
142                        struct inode *dir)
143 {
144         int rc;
145
146         if (!selinux_is_enabled())
147                 return 0;
148
149         rc = ll_security_inode_init_security(inode, dir, NULL, NULL, 0,
150                                               &ll_initxattrs, dentry);
151         if (rc == -EOPNOTSUPP)
152                 return 0;
153
154         return rc;
155 }
156 #else /* !HAVE_SECURITY_IINITSEC_CALLBACK */
157 /**
158  * Initializes security context
159  *
160  * Get security context of @inode in @dir,
161  * and put it in 'security.xxx' xattr of @dentry.
162  *
163  * \retval 0        success, or SELinux is disabled
164  * \retval -ENOMEM  if no memory could be allocated for xattr name
165  * \retval < 0      failure to get security context or set xattr
166  */
167 int
168 ll_inode_init_security(struct dentry *dentry, struct inode *inode,
169                        struct inode *dir)
170 {
171         char *full_name;
172         void *value;
173         char *name;
174         size_t len;
175         int err;
176
177         if (!selinux_is_enabled())
178                 return 0;
179
180         err = ll_security_inode_init_security(inode, dir, &name, &value, &len,
181                                               NULL, dentry);
182         if (err != 0) {
183                 if (err == -EOPNOTSUPP)
184                         return 0;
185                 return err;
186         }
187
188         full_name = kasprintf(GFP_KERNEL, "%s%s", XATTR_SECURITY_PREFIX, name);
189         if (!full_name)
190                 GOTO(out_free, err = -ENOMEM);
191
192         err = __vfs_setxattr(dentry, inode, full_name, value, len,
193                              XATTR_CREATE);
194         kfree(full_name);
195 out_free:
196         kfree(name);
197         kfree(value);
198
199         return err;
200 }
201 #endif /* HAVE_SECURITY_IINITSEC_CALLBACK */
202
203 /**
204  * Get security context xattr name used by policy.
205  *
206  * \retval >= 0     length of xattr name
207  * \retval < 0      failure to get security context xattr name
208  */
209 int
210 ll_listsecurity(struct inode *inode, char *secctx_name, size_t secctx_name_size)
211 {
212         int rc;
213
214         if (!selinux_is_enabled())
215                 return 0;
216
217 #ifdef HAVE_SECURITY_INODE_LISTSECURITY
218         rc = security_inode_listsecurity(inode, secctx_name, secctx_name_size);
219         if (rc >= secctx_name_size)
220                 rc = -ERANGE;
221         else if (rc >= 0)
222                 secctx_name[rc] = '\0';
223         return rc;
224 #else /* !HAVE_SECURITY_INODE_LISTSECURITY */
225         rc = sizeof(XATTR_NAME_SELINUX);
226         if (secctx_name && rc < secctx_name_size) {
227                 memcpy(secctx_name, XATTR_NAME_SELINUX, rc);
228                 secctx_name[rc] = '\0';
229         } else {
230                 rc = -ERANGE;
231         }
232         return rc;
233 #endif /* HAVE_SECURITY_INODE_LISTSECURITY */
234 }