Whamcloud - gitweb
LU-14361 statahead: regularized fname statahead pattern
[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 #ifdef HAVE_SECURITY_DENTRY_INIT_SECURTY_WITH_CTX
51 #define HAVE_SECURITY_DENTRY_INIT_WITH_XATTR_NAME_ARG 1
52 #endif
53
54 /*
55  * Check for LL_SBI_FILE_SECCTX before calling.
56  */
57 int ll_dentry_init_security(struct dentry *dentry, int mode, struct qstr *name,
58                             const char **secctx_name, __u32 *secctx_name_size,
59                             void **secctx, __u32 *secctx_size, int *secctx_slot)
60 {
61         struct ll_sb_info *sbi = ll_s2sbi(dentry->d_sb);
62 #ifdef HAVE_SECURITY_DENTRY_INIT_WITH_XATTR_NAME_ARG
63         const char *secctx_name_lsm = NULL;
64 #endif
65 #ifdef HAVE_SECURITY_DENTRY_INIT_SECURTY_WITH_CTX
66         struct lsmcontext ctx = {};
67 #endif
68         int rc;
69
70         /*
71          * Before kernel 5.15-rc1-20-g15bf32398ad4,
72          * security_inode_init_security() does not return to us the name of the
73          * extended attribute to store the context under (for example
74          * "security.selinux"). So we only call it when we think we know what
75          * the name of the extended attribute will be. This is OK-ish since
76          * SELinux is the only module that implements
77          * security_dentry_init_security(). Note that the NFS client code just
78          * calls it and assumes that if anything is returned then it must come
79          * from SELinux.
80          */
81
82         *secctx_name_size = ll_secctx_name_get(sbi, secctx_name);
83         /* xattr name length == 0 means no LSM module manage file contexts */
84         if (*secctx_name_size == 0)
85                 return 0;
86
87         rc = security_dentry_init_security(dentry, mode, name,
88 #ifdef HAVE_SECURITY_DENTRY_INIT_WITH_XATTR_NAME_ARG
89                                            &secctx_name_lsm,
90 #endif
91 #ifdef HAVE_SECURITY_DENTRY_INIT_SECURTY_WITH_CTX
92                                            &ctx);
93 #else
94                                            secctx, secctx_size);
95 #endif
96         /* ignore error if the hook is not supported by the LSM module */
97         if (rc == -EOPNOTSUPP)
98                 return 0;
99         if (rc < 0)
100                 return rc;
101
102 #ifdef HAVE_SECURITY_DENTRY_INIT_SECURTY_WITH_CTX
103         *secctx = ctx.context;
104         *secctx_size = ctx.len;
105         *secctx_slot = ctx.slot;
106 #endif
107
108 #ifdef HAVE_SECURITY_DENTRY_INIT_WITH_XATTR_NAME_ARG
109         if (strncmp(*secctx_name, secctx_name_lsm, *secctx_name_size) != 0) {
110                 CERROR("%s: LSM secctx_name '%s' does not match the one stored by Lustre '%s'\n",
111                       sbi->ll_fsname, secctx_name_lsm, *secctx_name);
112                 return -EOPNOTSUPP;
113         }
114 #endif
115
116         return 0;
117 }
118
119 /**
120  * A helper function for security_inode_init_security()
121  * that takes care of setting xattrs
122  *
123  * Get security context of @inode from @xattr_array,
124  * and put it in 'security.xxx' xattr of dentry
125  * stored in @fs_info.
126  *
127  * \retval 0        success
128  * \retval -ENOMEM  if no memory could be allocated for xattr name
129  * \retval < 0      failure to set xattr
130  */
131 static int
132 ll_initxattrs(struct inode *inode, const struct xattr *xattr_array,
133               void *fs_info)
134 {
135         struct dentry *dentry = fs_info;
136         const struct xattr *xattr;
137         int err = 0;
138
139         for (xattr = xattr_array; xattr->name; xattr++) {
140                 char *full_name;
141
142                 full_name = kasprintf(GFP_KERNEL, "%s%s",
143                                       XATTR_SECURITY_PREFIX, xattr->name);
144                 if (!full_name) {
145                         err = -ENOMEM;
146                         break;
147                 }
148
149                 err = ll_vfs_setxattr(dentry, inode, full_name, xattr->value,
150                                       xattr->value_len, XATTR_CREATE);
151                 kfree(full_name);
152                 if (err < 0)
153                         break;
154         }
155         return err;
156 }
157
158 /**
159  * Initializes security context
160  *
161  * Get security context of @inode in @dir,
162  * and put it in 'security.xxx' xattr of @dentry.
163  *
164  * \retval 0        success, or SELinux is disabled
165  * \retval -ENOMEM  if no memory could be allocated for xattr name
166  * \retval < 0      failure to get security context or set xattr
167  */
168 int
169 ll_inode_init_security(struct dentry *dentry, struct inode *inode,
170                        struct inode *dir)
171 {
172         int rc;
173
174         if (!ll_security_xattr_wanted(dir))
175                 return 0;
176
177         rc = security_inode_init_security(inode, dir, NULL,
178                                           &ll_initxattrs, dentry);
179         if (rc == -EOPNOTSUPP)
180                 return 0;
181
182         return rc;
183 }
184
185 /**
186  * Notify security context to the security layer
187  *
188  * Notify security context @secctx of inode @inode to the security layer.
189  *
190  * \retval 0        success, or SELinux is disabled or not supported by the fs
191  * \retval < 0      failure to set the security context
192  */
193 int ll_inode_notifysecctx(struct inode *inode,
194                           void *secctx, __u32 secctxlen)
195 {
196         struct ll_sb_info *sbi = ll_i2sbi(inode);
197         int rc;
198
199         if (!test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags) ||
200             !ll_security_xattr_wanted(inode) ||
201             !secctx || !secctxlen)
202                 return 0;
203
204         /* no need to protect selinux_inode_setsecurity() by
205          * inode_lock. Taking it would lead to a client deadlock
206          * LU-13617
207          */
208         rc = security_inode_notifysecctx(inode, secctx, secctxlen);
209         if (rc)
210                 CWARN("%s: cannot set security context for "DFID": rc = %d\n",
211                       sbi->ll_fsname, PFID(ll_inode2fid(inode)), rc);
212
213         return rc;
214 }
215
216 /**
217  * Free the security context xattr name used by policy
218  */
219 void ll_secctx_name_free(struct ll_sb_info *sbi)
220 {
221         OBD_FREE(sbi->ll_secctx_name, sbi->ll_secctx_name_size + 1);
222         sbi->ll_secctx_name = NULL;
223         sbi->ll_secctx_name_size = 0;
224 }
225
226 /**
227  * Get security context xattr name used by policy and save it.
228  *
229  * \retval > 0      length of xattr name
230  * \retval == 0     no LSM module registered supporting security contexts
231  * \retval <= 0     failure to get xattr name or xattr is not supported
232  */
233 int ll_secctx_name_store(struct inode *in)
234 {
235         struct ll_sb_info *sbi = ll_i2sbi(in);
236         int rc = 0;
237
238         if (!ll_security_xattr_wanted(in))
239                 return 0;
240
241         /* get size of xattr name */
242         rc = security_inode_listsecurity(in, NULL, 0);
243         if (rc <= 0)
244                 return rc;
245
246         if (sbi->ll_secctx_name)
247                 ll_secctx_name_free(sbi);
248
249         OBD_ALLOC(sbi->ll_secctx_name, rc + 1);
250         if (!sbi->ll_secctx_name)
251                 return -ENOMEM;
252
253         /* save the xattr name */
254         sbi->ll_secctx_name_size = rc;
255         rc = security_inode_listsecurity(in, sbi->ll_secctx_name,
256                                          sbi->ll_secctx_name_size);
257         if (rc <= 0)
258                 goto err_free;
259
260         if (rc > sbi->ll_secctx_name_size) {
261                 rc = -ERANGE;
262                 goto err_free;
263         }
264
265         /* sanity check */
266         sbi->ll_secctx_name[rc] = '\0';
267         if (rc < sizeof(XATTR_SECURITY_PREFIX)) {
268                 rc = -EINVAL;
269                 goto err_free;
270         }
271         if (strncmp(sbi->ll_secctx_name, XATTR_SECURITY_PREFIX,
272                     sizeof(XATTR_SECURITY_PREFIX) - 1) != 0) {
273                 rc = -EOPNOTSUPP;
274                 goto err_free;
275         }
276
277         return rc;
278
279 err_free:
280         ll_secctx_name_free(sbi);
281         return rc;
282 }
283
284 /**
285  * Retrieved file security context xattr name stored.
286  *
287  * \retval      security context xattr name size stored.
288  * \retval 0    no xattr name stored.
289  */
290 __u32 ll_secctx_name_get(struct ll_sb_info *sbi, const char **secctx_name)
291 {
292         if (!sbi->ll_secctx_name || !sbi->ll_secctx_name_size)
293                 return 0;
294
295         *secctx_name = sbi->ll_secctx_name;
296
297         return sbi->ll_secctx_name_size;
298 }
299
300 /**
301  * Filter out xattr file security context if not managed by LSM
302  *
303  * This is done to improve performance for application that blindly try to get
304  * file context (like "ls -l" for security.linux).
305  * See LU-549 for more information.
306  *
307  * \retval 0            xattr not filtered
308  * \retval -EOPNOTSUPP  no enabled LSM security module supports the xattr
309  */
310 int ll_security_secctx_name_filter(struct ll_sb_info *sbi, int xattr_type,
311                                    const char *suffix)
312 {
313         const char *cached_suffix = NULL;
314
315         if (xattr_type != XATTR_SECURITY_T ||
316             !ll_xattr_suffix_is_seclabel(suffix))
317                 return 0;
318
319         /* is the xattr label used by lsm ? */
320         if (!ll_secctx_name_get(sbi, &cached_suffix))
321                 return -EOPNOTSUPP;
322
323         cached_suffix += sizeof(XATTR_SECURITY_PREFIX) - 1;
324         if (strcmp(suffix, cached_suffix) != 0)
325                 return -EOPNOTSUPP;
326
327         return 0;
328 }