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