Whamcloud - gitweb
LU-17662 osd-zfs: Support for ZFS 2.2.3
[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 #ifdef HAVE_LSMCONTEXT_HAS_ID
106         *secctx_slot = ctx.id;
107 #else
108         *secctx_slot = ctx.slot;
109 #endif /* HAVE_LSMCONTEXT_HAS_ID */
110 #endif /* HAVE_SECURITY_DENTRY_INIT_SECURTY_WITH_CTX */
111
112 #ifdef HAVE_SECURITY_DENTRY_INIT_WITH_XATTR_NAME_ARG
113         if (strncmp(*secctx_name, secctx_name_lsm, *secctx_name_size) != 0) {
114                 CERROR("%s: LSM secctx_name '%s' does not match the one stored by Lustre '%s'\n",
115                       sbi->ll_fsname, secctx_name_lsm, *secctx_name);
116                 return -EOPNOTSUPP;
117         }
118 #endif
119
120         return 0;
121 }
122
123 /**
124  * A helper function for security_inode_init_security()
125  * that takes care of setting xattrs
126  *
127  * Get security context of @inode from @xattr_array,
128  * and put it in 'security.xxx' xattr of dentry
129  * stored in @fs_info.
130  *
131  * \retval 0        success
132  * \retval -ENOMEM  if no memory could be allocated for xattr name
133  * \retval < 0      failure to set xattr
134  */
135 static int
136 ll_initxattrs(struct inode *inode, const struct xattr *xattr_array,
137               void *fs_info)
138 {
139         struct dentry *dentry = fs_info;
140         const struct xattr *xattr;
141         int err = 0;
142
143         for (xattr = xattr_array; xattr->name; xattr++) {
144                 char *full_name;
145
146                 full_name = kasprintf(GFP_KERNEL, "%s%s",
147                                       XATTR_SECURITY_PREFIX, xattr->name);
148                 if (!full_name) {
149                         err = -ENOMEM;
150                         break;
151                 }
152
153                 err = ll_vfs_setxattr(dentry, inode, full_name, xattr->value,
154                                       xattr->value_len, XATTR_CREATE);
155                 kfree(full_name);
156                 if (err < 0)
157                         break;
158         }
159         return err;
160 }
161
162 /**
163  * Initializes security context
164  *
165  * Get security context of @inode in @dir,
166  * and put it in 'security.xxx' xattr of @dentry.
167  *
168  * \retval 0        success, or SELinux is disabled
169  * \retval -ENOMEM  if no memory could be allocated for xattr name
170  * \retval < 0      failure to get security context or set xattr
171  */
172 int
173 ll_inode_init_security(struct dentry *dentry, struct inode *inode,
174                        struct inode *dir)
175 {
176         int rc;
177
178         if (!ll_security_xattr_wanted(dir))
179                 return 0;
180
181         rc = security_inode_init_security(inode, dir, NULL,
182                                           &ll_initxattrs, dentry);
183         if (rc == -EOPNOTSUPP)
184                 return 0;
185
186         return rc;
187 }
188
189 /**
190  * Notify security context to the security layer
191  *
192  * Notify security context @secctx of inode @inode to the security layer.
193  *
194  * \retval 0        success, or SELinux is disabled or not supported by the fs
195  * \retval < 0      failure to set the security context
196  */
197 int ll_inode_notifysecctx(struct inode *inode,
198                           void *secctx, __u32 secctxlen)
199 {
200         struct ll_sb_info *sbi = ll_i2sbi(inode);
201         int rc;
202
203         if (!test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags) ||
204             !ll_security_xattr_wanted(inode) ||
205             !secctx || !secctxlen)
206                 return 0;
207
208         /* no need to protect selinux_inode_setsecurity() by
209          * inode_lock. Taking it would lead to a client deadlock
210          * LU-13617
211          */
212         rc = security_inode_notifysecctx(inode, secctx, secctxlen);
213         if (rc)
214                 CWARN("%s: cannot set security context for "DFID": rc = %d\n",
215                       sbi->ll_fsname, PFID(ll_inode2fid(inode)), rc);
216
217         return rc;
218 }
219
220 /**
221  * Free the security context xattr name used by policy
222  */
223 void ll_secctx_name_free(struct ll_sb_info *sbi)
224 {
225         OBD_FREE(sbi->ll_secctx_name, sbi->ll_secctx_name_size + 1);
226         sbi->ll_secctx_name = NULL;
227         sbi->ll_secctx_name_size = 0;
228 }
229
230 /**
231  * Get security context xattr name used by policy and save it.
232  *
233  * \retval > 0      length of xattr name
234  * \retval == 0     no LSM module registered supporting security contexts
235  * \retval <= 0     failure to get xattr name or xattr is not supported
236  */
237 int ll_secctx_name_store(struct inode *in)
238 {
239         struct ll_sb_info *sbi = ll_i2sbi(in);
240         int rc = 0;
241
242         if (!ll_security_xattr_wanted(in))
243                 return 0;
244
245         /* get size of xattr name */
246         rc = security_inode_listsecurity(in, NULL, 0);
247         if (rc <= 0)
248                 return rc;
249
250         if (sbi->ll_secctx_name)
251                 ll_secctx_name_free(sbi);
252
253         OBD_ALLOC(sbi->ll_secctx_name, rc + 1);
254         if (!sbi->ll_secctx_name)
255                 return -ENOMEM;
256
257         /* save the xattr name */
258         sbi->ll_secctx_name_size = rc;
259         rc = security_inode_listsecurity(in, sbi->ll_secctx_name,
260                                          sbi->ll_secctx_name_size);
261         if (rc <= 0)
262                 goto err_free;
263
264         if (rc > sbi->ll_secctx_name_size) {
265                 rc = -ERANGE;
266                 goto err_free;
267         }
268
269         /* sanity check */
270         sbi->ll_secctx_name[rc] = '\0';
271         if (rc < sizeof(XATTR_SECURITY_PREFIX)) {
272                 rc = -EINVAL;
273                 goto err_free;
274         }
275         if (strncmp(sbi->ll_secctx_name, XATTR_SECURITY_PREFIX,
276                     sizeof(XATTR_SECURITY_PREFIX) - 1) != 0) {
277                 rc = -EOPNOTSUPP;
278                 goto err_free;
279         }
280
281         return rc;
282
283 err_free:
284         ll_secctx_name_free(sbi);
285         return rc;
286 }
287
288 /**
289  * Retrieved file security context xattr name stored.
290  *
291  * \retval      security context xattr name size stored.
292  * \retval 0    no xattr name stored.
293  */
294 __u32 ll_secctx_name_get(struct ll_sb_info *sbi, const char **secctx_name)
295 {
296         if (!sbi->ll_secctx_name || !sbi->ll_secctx_name_size)
297                 return 0;
298
299         *secctx_name = sbi->ll_secctx_name;
300
301         return sbi->ll_secctx_name_size;
302 }
303
304 /**
305  * Filter out xattr file security context if not managed by LSM
306  *
307  * This is done to improve performance for application that blindly try to get
308  * file context (like "ls -l" for security.linux).
309  * See LU-549 for more information.
310  *
311  * \retval 0            xattr not filtered
312  * \retval -EOPNOTSUPP  no enabled LSM security module supports the xattr
313  */
314 int ll_security_secctx_name_filter(struct ll_sb_info *sbi, int xattr_type,
315                                    const char *suffix)
316 {
317         const char *cached_suffix = NULL;
318
319         if (xattr_type != XATTR_SECURITY_T ||
320             !ll_xattr_suffix_is_seclabel(suffix))
321                 return 0;
322
323         /* is the xattr label used by lsm ? */
324         if (!ll_secctx_name_get(sbi, &cached_suffix))
325                 return -EOPNOTSUPP;
326
327         cached_suffix += sizeof(XATTR_SECURITY_PREFIX) - 1;
328         if (strcmp(suffix, cached_suffix) != 0)
329                 return -EOPNOTSUPP;
330
331         return 0;
332 }