Whamcloud - gitweb
LU-1482 mdd: Setting xattr are properly checked with and without ACLs
[fs/lustre-release.git] / lustre / llite / xattr.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
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <linux/fs.h>
38 #include <linux/sched.h>
39 #include <linux/mm.h>
40 #include <linux/xattr.h>
41 #include <linux/selinux.h>
42
43 #define DEBUG_SUBSYSTEM S_LLITE
44
45 #include <obd_support.h>
46 #include <lustre_dlm.h>
47 #include <lustre_ver.h>
48 #include <lustre_eacl.h>
49
50 #include "llite_internal.h"
51
52 /* xattr related to IMA(Integrity Measurement Architecture) */
53 #ifndef XATTR_NAME_IMA
54 #define XATTR_NAME_IMA          "security.ima"
55 #endif
56 #ifndef XATTR_NAME_EVM
57 #define XATTR_NAME_EVM          "security.evm"
58 #endif
59
60 #define XATTR_USER_T            (1)
61 #define XATTR_TRUSTED_T         (2)
62 #define XATTR_SECURITY_T        (3)
63 #define XATTR_ACL_ACCESS_T      (4)
64 #define XATTR_ACL_DEFAULT_T     (5)
65 #define XATTR_LUSTRE_T          (6)
66 #define XATTR_OTHER_T           (7)
67
68 static
69 int get_xattr_type(const char *name)
70 {
71         if (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS))
72                 return XATTR_ACL_ACCESS_T;
73
74         if (!strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT))
75                 return XATTR_ACL_DEFAULT_T;
76
77         if (!strncmp(name, XATTR_USER_PREFIX,
78                      sizeof(XATTR_USER_PREFIX) - 1))
79                 return XATTR_USER_T;
80
81         if (!strncmp(name, XATTR_TRUSTED_PREFIX,
82                      sizeof(XATTR_TRUSTED_PREFIX) - 1))
83                 return XATTR_TRUSTED_T;
84
85         if (!strncmp(name, XATTR_SECURITY_PREFIX,
86                      sizeof(XATTR_SECURITY_PREFIX) - 1))
87                 return XATTR_SECURITY_T;
88
89         if (!strncmp(name, XATTR_LUSTRE_PREFIX,
90                      sizeof(XATTR_LUSTRE_PREFIX) - 1))
91                 return XATTR_LUSTRE_T;
92
93         return XATTR_OTHER_T;
94 }
95
96 static
97 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
98 {
99         if ((xattr_type == XATTR_ACL_ACCESS_T ||
100              xattr_type == XATTR_ACL_DEFAULT_T) &&
101            !(sbi->ll_flags & LL_SBI_ACL))
102                 return -EOPNOTSUPP;
103
104         if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
105                 return -EOPNOTSUPP;
106         if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
107                 return -EPERM;
108         if (xattr_type == XATTR_OTHER_T)
109                 return -EOPNOTSUPP;
110
111         return 0;
112 }
113
114 static
115 int ll_setxattr_common(struct inode *inode, const char *name,
116                        const void *value, size_t size,
117                        int flags, __u64 valid)
118 {
119         struct ll_sb_info *sbi = ll_i2sbi(inode);
120         struct ptlrpc_request *req = NULL;
121         int xattr_type, rc;
122         const char *pv = value;
123         ENTRY;
124
125         /*FIXME: enable IMA when the conditions are ready */
126         if (strncmp(name, XATTR_NAME_IMA,
127                     sizeof(XATTR_NAME_IMA)) == 0 ||
128             strncmp(name, XATTR_NAME_EVM,
129                     sizeof(XATTR_NAME_EVM)) == 0)
130                 return -EOPNOTSUPP;
131
132         xattr_type = get_xattr_type(name);
133         rc = xattr_type_filter(sbi, xattr_type);
134         if (rc)
135                 RETURN(rc);
136
137         if ((xattr_type == XATTR_ACL_ACCESS_T ||
138              xattr_type == XATTR_ACL_DEFAULT_T) &&
139 #ifdef HAVE_INODE_OWNER_OR_CAPABLE
140             !inode_owner_or_capable(inode))
141 #else
142             !is_owner_or_cap(inode))
143 #endif
144                 return -EPERM;
145
146         /* b10667: ignore lustre special xattr for now */
147         if (strcmp(name, XATTR_NAME_HSM) == 0 ||
148                 (xattr_type == XATTR_TRUSTED_T &&
149                 strcmp(name, XATTR_NAME_LOV) == 0) ||
150                 (xattr_type == XATTR_LUSTRE_T &&
151                  strcmp(name, "lustre.lov") == 0))
152                 RETURN(0);
153
154         /* b15587: ignore security.capability xattr for now */
155         if ((xattr_type == XATTR_SECURITY_T &&
156             strcmp(name, "security.capability") == 0))
157                 RETURN(0);
158
159         /* LU-549:  Disable security.selinux when selinux is disabled */
160         if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
161             strcmp(name, "security.selinux") == 0)
162                 RETURN(-EOPNOTSUPP);
163
164         /* In user.* namespace, only regular files and directories can have
165          * extended attributes. */
166         if (xattr_type == XATTR_USER_T) {
167                 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
168                         RETURN(-EPERM);
169         }
170
171         rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid, name, pv,
172                          size, 0, flags, ll_i2suppgid(inode), &req);
173         if (rc) {
174                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
175                         LCONSOLE_INFO("Disabling user_xattr feature because "
176                                       "it is not supported on the server\n");
177                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
178                 }
179                 RETURN(rc);
180         }
181
182         ptlrpc_req_finished(req);
183         RETURN(0);
184 }
185
186 static int get_hsm_state(struct inode *inode, __u32 *hus_states)
187 {
188         struct md_op_data *op_data;
189         struct hsm_user_state *hus;
190         int rc;
191
192         OBD_ALLOC_PTR(hus);
193         if (hus == NULL)
194                 return -ENOMEM;
195
196         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
197                                      LUSTRE_OPC_ANY, hus);
198         if (!IS_ERR(op_data)) {
199                 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
200                                    sizeof(*op_data), op_data, NULL);
201                 if (rc == 0)
202                         *hus_states = hus->hus_states;
203                 else
204                         CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
205                                rc);
206
207                 ll_finish_md_op_data(op_data);
208         } else {
209                 rc = PTR_ERR(op_data);
210                 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
211                        rc);
212         }
213         OBD_FREE_PTR(hus);
214         return rc;
215 }
216
217 int ll_setxattr(struct dentry *dentry, const char *name,
218                 const void *value, size_t size, int flags)
219 {
220         struct inode *inode = dentry->d_inode;
221
222         LASSERT(inode);
223         LASSERT(name);
224
225         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
226                PFID(ll_inode2fid(inode)), inode, name);
227
228         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
229
230         if ((strncmp(name, XATTR_TRUSTED_PREFIX,
231                      sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
232              strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
233             (strncmp(name, XATTR_LUSTRE_PREFIX,
234                      sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
235              strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
236                 struct lov_user_md *lump = (struct lov_user_md *)value;
237                 int rc = 0;
238
239                 /* Attributes that are saved via getxattr will always have
240                  * the stripe_offset as 0.  Instead, the MDS should be
241                  * allowed to pick the starting OST index.   b=17846 */
242                 if (lump != NULL && lump->lmm_stripe_offset == 0)
243                         lump->lmm_stripe_offset = -1;
244                 /* Avoid anyone directly setting the RELEASED flag. */
245                 if (lump != NULL &&
246                         (lump->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
247                         /* Only if we have a released flag check if the file
248                         * was indeed archived. */
249                         __u32 state = HS_NONE;
250                         rc = get_hsm_state(inode, &state);
251                         if (rc != 0)
252                                 RETURN(rc);
253                         if (!(state & HS_ARCHIVED)) {
254                                 CDEBUG(D_VFSTRACE,
255                                         "hus_states state = %x, pattern = %x\n",
256                                         state, lump->lmm_pattern);
257                                 /* Here the state is: real file is not
258                                  * archived but user is requesting to set
259                                  * the RELEASED flag so we mask off the
260                                  * released flag from the request */
261                                 lump->lmm_pattern ^= LOV_PATTERN_F_RELEASED;
262                         }
263                 }
264
265                 if (lump != NULL && S_ISREG(inode->i_mode)) {
266                         struct file     f;
267                         __u64           it_flags = FMODE_WRITE;
268                         int             lum_size;
269
270                         lum_size = ll_lov_user_md_size(lump);
271                         if (lum_size < 0 || size < lum_size)
272                                 return 0; /* b=10667: ignore error */
273
274                         memset(&f, 0, sizeof(f)); /* f.f_flags is used below */
275                         f.f_path.dentry = dentry;
276                         rc = ll_lov_setstripe_ea_info(inode, &f, it_flags, lump,
277                                                       lum_size);
278                         /* b=10667: rc always be 0 here for now */
279                         rc = 0;
280                 } else if (S_ISDIR(inode->i_mode)) {
281                         rc = ll_dir_setstripe(inode, lump, 0);
282                 }
283
284                 return rc;
285
286         } else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
287                    strcmp(name, XATTR_NAME_LINK) == 0)
288                 return 0;
289
290         return ll_setxattr_common(inode, name, value, size, flags,
291                                   OBD_MD_FLXATTR);
292 }
293
294 int ll_removexattr(struct dentry *dentry, const char *name)
295 {
296         struct inode *inode = dentry->d_inode;
297
298         LASSERT(inode);
299         LASSERT(name);
300
301         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
302                PFID(ll_inode2fid(inode)), inode, name);
303
304         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
305         return ll_setxattr_common(inode, name, NULL, 0, 0,
306                                   OBD_MD_FLXATTRRM);
307 }
308
309 int ll_getxattr_common(struct inode *inode, const char *name,
310                        void *buffer, size_t size, __u64 valid)
311 {
312         struct ll_sb_info *sbi = ll_i2sbi(inode);
313         struct ptlrpc_request *req = NULL;
314         struct mdt_body *body;
315         int xattr_type, rc;
316         void *xdata;
317         struct ll_inode_info *lli = ll_i2info(inode);
318         ENTRY;
319
320         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
321                PFID(ll_inode2fid(inode)), inode);
322
323         /* listxattr have slightly different behavior from of ext3:
324          * without 'user_xattr' ext3 will list all xattr names but
325          * filtered out "^user..*"; we list them all for simplicity.
326          */
327         if (!name) {
328                 xattr_type = XATTR_OTHER_T;
329                 goto do_getxattr;
330         }
331
332         xattr_type = get_xattr_type(name);
333         rc = xattr_type_filter(sbi, xattr_type);
334         if (rc)
335                 RETURN(rc);
336
337         /* b15587: ignore security.capability xattr for now */
338         if ((xattr_type == XATTR_SECURITY_T &&
339             strcmp(name, "security.capability") == 0))
340                 RETURN(-ENODATA);
341
342         /* LU-549:  Disable security.selinux when selinux is disabled */
343         if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
344             strcmp(name, "security.selinux") == 0)
345                 RETURN(-EOPNOTSUPP);
346
347 #ifdef CONFIG_FS_POSIX_ACL
348         /* posix acl is under protection of LOOKUP lock. when calling to this,
349          * we just have path resolution to the target inode, so we have great
350          * chance that cached ACL is uptodate.
351          */
352         if (xattr_type == XATTR_ACL_ACCESS_T) {
353                 struct posix_acl *acl;
354
355                 spin_lock(&lli->lli_lock);
356                 acl = posix_acl_dup(lli->lli_posix_acl);
357                 spin_unlock(&lli->lli_lock);
358
359                 if (!acl)
360                         RETURN(-ENODATA);
361
362                 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
363                 posix_acl_release(acl);
364                 RETURN(rc);
365         }
366         if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
367                 RETURN(-ENODATA);
368 #endif
369
370 do_getxattr:
371         if (sbi->ll_xattr_cache_enabled &&
372             xattr_type != XATTR_ACL_ACCESS_T &&
373             (xattr_type != XATTR_SECURITY_T ||
374                 strcmp(name, "security.selinux") != 0)) {
375                 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
376                 if (rc == -EAGAIN)
377                         goto getxattr_nocache;
378                 if (rc < 0)
379                         GOTO(out_xattr, rc);
380
381                 /* Add "system.posix_acl_access" to the list */
382                 if (lli->lli_posix_acl != NULL && valid & OBD_MD_FLXATTRLS) {
383                         if (size == 0) {
384                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
385                         } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
386                                 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
387                                        sizeof(XATTR_NAME_ACL_ACCESS));
388                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
389                         } else {
390                                 GOTO(out_xattr, rc = -ERANGE);
391                         }
392                 }
393         } else {
394 getxattr_nocache:
395                 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
396                                 valid, name, NULL, 0, size, 0, &req);
397
398                 if (rc < 0)
399                         GOTO(out_xattr, rc);
400
401                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
402                 LASSERT(body);
403
404                 /* only detect the xattr size */
405                 if (size == 0)
406                         GOTO(out, rc = body->mbo_eadatasize);
407
408                 if (size < body->mbo_eadatasize) {
409                         CERROR("server bug: replied size %u > %u\n",
410                                 body->mbo_eadatasize, (int)size);
411                         GOTO(out, rc = -ERANGE);
412                 }
413
414                 if (body->mbo_eadatasize == 0)
415                         GOTO(out, rc = -ENODATA);
416
417                 /* do not need swab xattr data */
418                 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
419                                                         body->mbo_eadatasize);
420                 if (!xdata)
421                         GOTO(out, rc = -EFAULT);
422
423                 memcpy(buffer, xdata, body->mbo_eadatasize);
424                 rc = body->mbo_eadatasize;
425         }
426
427         EXIT;
428
429 out_xattr:
430         if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
431                 LCONSOLE_INFO("%s: disabling user_xattr feature because "
432                                 "it is not supported on the server: rc = %d\n",
433                                 ll_get_fsname(inode->i_sb, NULL, 0), rc);
434                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
435         }
436 out:
437         ptlrpc_req_finished(req);
438         return rc;
439 }
440
441 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
442 {
443         ssize_t rc;
444
445         if (S_ISREG(inode->i_mode)) {
446                 struct cl_object *obj = ll_i2info(inode)->lli_clob;
447                 struct lu_env *env;
448                 struct cl_layout cl = {
449                         .cl_buf.lb_buf = buf,
450                         .cl_buf.lb_len = buf_size,
451                 };
452                 __u16 refcheck;
453
454                 if (obj == NULL)
455                         RETURN(-ENODATA);
456
457                 env = cl_env_get(&refcheck);
458                 if (IS_ERR(env))
459                         RETURN(PTR_ERR(env));
460
461                 rc = cl_object_layout_get(env, obj, &cl);
462                 if (rc < 0)
463                         GOTO(out_env, rc);
464
465                 if (cl.cl_size == 0)
466                         GOTO(out_env, rc = -ENODATA);
467
468                 rc = cl.cl_size;
469
470                 if (buf_size == 0)
471                         GOTO(out_env, rc);
472
473                 LASSERT(buf != NULL && rc <= buf_size);
474
475                 /* Do not return layout gen for getxattr() since
476                  * otherwise it would confuse tar --xattr by
477                  * recognizing layout gen as stripe offset when the
478                  * file is restored. See LU-2809. */
479                 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
480 out_env:
481                 cl_env_put(env, &refcheck);
482
483                 RETURN(rc);
484         } else if (S_ISDIR(inode->i_mode)) {
485                 struct lov_mds_md *lmm = NULL;
486                 int lmm_size = 0;
487                 struct ptlrpc_request *req = NULL;
488
489                 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
490                                       &req, 0);
491                 if (rc < 0)
492                         GOTO(out_req, rc);
493
494                 if (buf_size == 0)
495                         GOTO(out_req, rc = lmm_size);
496
497                 if (buf_size < lmm_size)
498                         GOTO(out_req, rc = -ERANGE);
499
500                 memcpy(buf, lmm, lmm_size);
501                 GOTO(out_req, rc = lmm_size);
502 out_req:
503                 if (req != NULL)
504                         ptlrpc_req_finished(req);
505
506                 return rc;
507         } else {
508                 RETURN(-ENODATA);
509         }
510 }
511
512 ssize_t ll_getxattr(struct dentry *dentry, const char *name, void *buf,
513                     size_t buf_size)
514 {
515         struct inode *inode = dentry->d_inode;
516
517         LASSERT(inode);
518         LASSERT(name);
519
520         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
521                PFID(ll_inode2fid(inode)), inode, name);
522
523         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
524
525         if (strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
526             strcmp(name, XATTR_NAME_LOV) == 0)
527                 return ll_getxattr_lov(inode, buf, buf_size);
528         else
529                 return ll_getxattr_common(inode, name, buf, buf_size,
530                                           OBD_MD_FLXATTR);
531 }
532
533 ssize_t ll_listxattr(struct dentry *dentry, char *buf, size_t buf_size)
534 {
535         struct inode *inode = dentry->d_inode;
536         struct ll_sb_info *sbi = ll_i2sbi(inode);
537         char *xattr_name;
538         ssize_t rc, rc2;
539         size_t len, rem;
540
541         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
542                PFID(ll_inode2fid(inode)), inode);
543
544         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
545
546         rc = ll_getxattr_common(inode, NULL, buf, buf_size, OBD_MD_FLXATTRLS);
547         if (rc < 0)
548                 RETURN(rc);
549
550         /* If we're being called to get the size of the xattr list
551          * (buf_size == 0) then just assume that a lustre.lov xattr
552          * exists. */
553         if (buf_size == 0)
554                 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
555
556         xattr_name = buf;
557         rem = rc;
558
559         while (rem > 0) {
560                 len = strnlen(xattr_name, rem - 1) + 1;
561                 rem -= len;
562                 if (xattr_type_filter(sbi, get_xattr_type(xattr_name)) == 0) {
563                         /* Skip OK xattr type, leave it in buffer. */
564                         xattr_name += len;
565                         continue;
566                 }
567
568                 /* Move up remaining xattrs in buffer removing the
569                  * xattr that is not OK. */
570                 memmove(xattr_name, xattr_name + len, rem);
571                 rc -= len;
572         }
573
574         rc2 = ll_getxattr_lov(inode, NULL, 0);
575         if (rc2 == -ENODATA)
576                 RETURN(rc);
577
578         if (rc2 < 0)
579                 RETURN(rc2);
580
581         if (buf_size < rc + sizeof(XATTR_LUSTRE_LOV))
582                 RETURN(-ERANGE);
583
584         memcpy(buf + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
585
586         RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
587 }