Whamcloud - gitweb
LU-1187 tests: Add DNE test cases in sanity.
[fs/lustre-release.git] / lustre / mdd / mdd_permission.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 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/mdd/mdd_permission.c
35  *
36  * Lustre Metadata Server (mdd) routines
37  *
38  * Author: fangyong@clusterfs.com
39  * Author: lsy@clusterfs.com
40  */
41
42 #define DEBUG_SUBSYSTEM S_MDS
43
44 #include <obd_class.h>
45 #include <lustre_ver.h>
46 #include <lprocfs_status.h>
47 #include <lustre_mds.h>
48
49 #include "mdd_internal.h"
50
51 #ifdef CONFIG_FS_POSIX_ACL
52
53 /*
54  * Hold write_lock for o.
55  */
56 int mdd_acl_chmod(const struct lu_env *env, struct mdd_object *o, __u32 mode,
57                   struct thandle *handle)
58 {
59         struct lu_buf           *buf;
60         posix_acl_xattr_header  *head;
61         posix_acl_xattr_entry   *entry;
62         int                      entry_count;
63         int                      rc;
64
65         ENTRY;
66
67         buf = mdd_buf_get(env, mdd_env_info(env)->mti_xattr_buf,
68                           sizeof(mdd_env_info(env)->mti_xattr_buf));
69
70         rc = mdo_xattr_get(env, o, buf, XATTR_NAME_ACL_ACCESS, BYPASS_CAPA);
71         if ((rc == -EOPNOTSUPP) || (rc == -ENODATA))
72                 RETURN(0);
73         else if (rc <= 0)
74                 RETURN(rc);
75
76         buf->lb_len = rc;
77         head = (posix_acl_xattr_header *)(buf->lb_buf);
78         entry = head->a_entries;
79         entry_count = (buf->lb_len - sizeof(head->a_version)) /
80                 sizeof(posix_acl_xattr_entry);
81         if (entry_count <= 0)
82                 RETURN(0);
83
84         rc = lustre_posix_acl_chmod_masq(entry, mode, entry_count);
85         if (rc)
86                 RETURN(rc);
87
88         rc = mdo_xattr_set(env, o, buf, XATTR_NAME_ACL_ACCESS,
89                            0, handle, BYPASS_CAPA);
90         RETURN(rc);
91 }
92
93 int mdd_acl_set(const struct lu_env *env, struct mdd_object *obj,
94                 const struct lu_buf *buf, int fl)
95 {
96         struct mdd_device       *mdd = mdd_obj2mdd_dev(obj);
97         struct lu_attr          *la = &mdd_env_info(env)->mti_la;
98         struct thandle          *handle;
99         posix_acl_xattr_header  *head;
100         posix_acl_xattr_entry   *entry;
101         int                      rc, entry_count;
102         bool                     not_equiv, mode_change;
103         mode_t                   mode;
104         ENTRY;
105
106         rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
107         if (rc)
108                 RETURN(rc);
109
110         head = (posix_acl_xattr_header *)(buf->lb_buf);
111         entry = head->a_entries;
112         entry_count = (buf->lb_len - sizeof(head->a_version)) /
113                 sizeof(posix_acl_xattr_entry);
114         if (entry_count <= 0)
115                 RETURN(0);
116
117         LASSERT(la->la_valid & LA_MODE);
118         mode = la->la_mode;
119         rc = lustre_posix_acl_equiv_mode(entry, &mode, entry_count);
120         if (rc < 0)
121                 RETURN(rc);
122
123         not_equiv = (rc > 0);
124         mode_change = (mode != la->la_mode);
125
126         handle = mdd_trans_create(env, mdd);
127         if (IS_ERR(handle))
128                 RETURN(PTR_ERR(handle));
129
130         /* rc tells whether ACL can be represented by i_mode only */
131         if (not_equiv)
132                 rc = mdo_declare_xattr_set(env, obj, buf,
133                                 XATTR_NAME_ACL_ACCESS, fl, handle);
134         else
135                 rc = mdo_declare_xattr_del(env, obj, XATTR_NAME_ACL_ACCESS,
136                                 handle);
137         if (rc)
138                 GOTO(stop, rc);
139
140         if (mode_change) {
141                 la->la_mode = mode;
142                 rc = mdo_declare_attr_set(env, obj, la, handle);
143         }
144
145         rc = mdd_trans_start(env, mdd, handle);
146         if (rc)
147                 GOTO(stop, rc);
148
149         mdd_write_lock(env, obj, MOR_TGT_CHILD);
150         /* whether ACL can be represented by i_mode only */
151         if (not_equiv)
152                 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_ACL_ACCESS, fl,
153                                 handle, mdd_object_capa(env, obj));
154         else
155                 rc = mdo_xattr_del(env, obj, XATTR_NAME_ACL_ACCESS, handle,
156                                 mdd_object_capa(env, obj));
157         if (rc)
158                 GOTO(unlock, rc);
159
160         if (mode_change)
161                 rc = mdo_attr_set(env, obj, la, handle,
162                                 mdd_object_capa(env, obj));
163
164         /* security-replated changes may require sync */
165         handle->th_sync |= !!mdd->mdd_sync_permission;
166 unlock:
167         mdd_write_unlock(env, obj);
168 stop:
169         mdd_trans_stop(env, mdd, rc, handle);
170
171         RETURN(rc);
172 }
173
174 /*
175  * Hold write_lock for obj.
176  */
177 int __mdd_acl_init(const struct lu_env *env, struct mdd_object *obj,
178                    struct lu_buf *buf, __u32 *mode, struct thandle *handle)
179 {
180         posix_acl_xattr_header  *head;
181         posix_acl_xattr_entry   *entry;
182         int                      entry_count;
183         __u32                    old = *mode;
184         int                      rc, rc2;
185
186         ENTRY;
187
188         head = (posix_acl_xattr_header *)(buf->lb_buf);
189         entry = head->a_entries;
190         entry_count = (buf->lb_len - sizeof(head->a_version)) /
191                       sizeof(posix_acl_xattr_entry);
192         if (entry_count <= 0)
193                 RETURN(0);
194
195         if (S_ISDIR(*mode)) {
196                 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_ACL_DEFAULT, 0,
197                                    handle, BYPASS_CAPA);
198                 if (rc)
199                         RETURN(rc);
200         }
201
202         rc = lustre_posix_acl_create_masq(entry, mode, entry_count);
203         if (rc < 0)
204                 RETURN(rc);
205
206         /* part of ACL went into i_mode */
207         if (*mode != old) {
208                 struct mdd_thread_info  *info = mdd_env_info(env);
209                 struct lu_attr          *pattr = &info->mti_pattr;
210
211                 /* mode was initialized within object creation,
212                  * so we need explict ->attr_set() to update it */
213                 pattr->la_valid = LA_MODE;
214                 pattr->la_mode = *mode;
215                 rc2 = mdo_attr_set(env, obj, pattr, handle, BYPASS_CAPA);
216                 if (rc2 < 0)
217                         rc = rc2;
218         }
219
220         if (rc > 0)
221                 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_ACL_ACCESS, 0,
222                                    handle, BYPASS_CAPA);
223         RETURN(rc);
224 }
225 #endif
226
227 /*
228  * Hold read_lock for obj.
229  */
230 static int mdd_check_acl(const struct lu_env *env, struct mdd_object *obj,
231                          struct lu_attr *la, int mask)
232 {
233 #ifdef CONFIG_FS_POSIX_ACL
234         struct lu_ucred  *uc  = lu_ucred_assert(env);
235         posix_acl_xattr_header *head;
236         posix_acl_xattr_entry *entry;
237         struct lu_buf   *buf;
238         int entry_count;
239         int rc;
240         ENTRY;
241
242         buf = mdd_buf_get(env, mdd_env_info(env)->mti_xattr_buf,
243                           sizeof(mdd_env_info(env)->mti_xattr_buf));
244         rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_ACL_ACCESS,
245                            mdd_object_capa(env, obj));
246         if (rc <= 0)
247                 RETURN(rc ? : -EACCES);
248
249         buf->lb_len = rc;
250         head = (posix_acl_xattr_header *)(buf->lb_buf);
251         entry = head->a_entries;
252         entry_count = (buf->lb_len - sizeof(head->a_version)) /
253                       sizeof(posix_acl_xattr_entry);
254
255         rc = lustre_posix_acl_permission(uc, la, mask, entry, entry_count);
256         RETURN(rc);
257 #else
258         ENTRY;
259         RETURN(-EAGAIN);
260 #endif
261 }
262
263 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
264                               struct lu_attr *la, int mask, int role)
265 {
266         struct lu_ucred *uc = lu_ucred(env);
267         __u32 mode;
268         int rc;
269         ENTRY;
270
271         if (mask == 0)
272                 RETURN(0);
273
274         /* These means unnecessary for permission check */
275         if ((uc == NULL) || (uc->uc_valid == UCRED_INIT))
276                 RETURN(0);
277
278         /* Invalid user credit */
279         if (uc->uc_valid == UCRED_INVALID)
280                 RETURN(-EACCES);
281
282         /*
283          * Nobody gets write access to an immutable file.
284          */
285         if ((mask & MAY_WRITE) && mdd_is_immutable(obj))
286                 RETURN(-EACCES);
287
288         if (la == NULL) {
289                 la = &mdd_env_info(env)->mti_la;
290                 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
291                 if (rc)
292                         RETURN(rc);
293         }
294
295         mode = la->la_mode;
296         if (uc->uc_fsuid == la->la_uid) {
297                 mode >>= 6;
298         } else {
299                 if (mode & S_IRWXG) {
300                         if (role != -1)
301                                 mdd_read_lock(env, obj, role);
302                         rc = mdd_check_acl(env, obj, la, mask);
303                         if (role != -1)
304                                 mdd_read_unlock(env, obj);
305                         if (rc == -EACCES)
306                                 goto check_capabilities;
307                         else if ((rc != -EAGAIN) && (rc != -EOPNOTSUPP) &&
308                                  (rc != -ENODATA))
309                                 RETURN(rc);
310                 }
311                 if (lustre_in_group_p(uc, la->la_gid))
312                         mode >>= 3;
313         }
314
315         if (((mode & mask & S_IRWXO) == mask))
316                 RETURN(0);
317
318 check_capabilities:
319         if (!(mask & MAY_EXEC) ||
320             (la->la_mode & S_IXUGO) || S_ISDIR(la->la_mode))
321                 if (mdd_capable(uc, CFS_CAP_DAC_OVERRIDE))
322                         RETURN(0);
323
324         if ((mask == MAY_READ) ||
325             (S_ISDIR(la->la_mode) && !(mask & MAY_WRITE)))
326                 if (mdd_capable(uc, CFS_CAP_DAC_READ_SEARCH))
327                         RETURN(0);
328
329         RETURN(-EACCES);
330 }
331
332 int mdd_permission(const struct lu_env *env,
333                    struct md_object *pobj, struct md_object *cobj,
334                    struct md_attr *ma, int mask)
335 {
336         struct mdd_object *mdd_pobj, *mdd_cobj;
337         struct lu_ucred *uc = NULL;
338         struct lu_attr *la = &mdd_env_info(env)->mti_cattr;
339         int check_create, check_link;
340         int check_unlink;
341         int check_rename_src, check_rename_tar;
342         int check_vtx_part, check_vtx_full;
343         int check_rgetfacl;
344         int rc = 0;
345         ENTRY;
346
347         LASSERT(cobj);
348         mdd_cobj = md2mdd_obj(cobj);
349
350         rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
351         if (rc)
352                 RETURN(rc);
353
354         /* For cross_open case, the "mask" is open flags,
355          * so convert it to permission mask first.
356          * XXX: MDS_OPEN_CROSS must be NOT equal to permission mask MAY_*. */
357         if (unlikely(mask & MDS_OPEN_CROSS))
358                 mask = accmode(env, la, mask & ~MDS_OPEN_CROSS);
359
360         check_create = mask & MAY_CREATE;
361         check_link = mask & MAY_LINK;
362         check_unlink = mask & MAY_UNLINK;
363         check_rename_src = mask & MAY_RENAME_SRC;
364         check_rename_tar = mask & MAY_RENAME_TAR;
365         check_vtx_part = mask & MAY_VTX_PART;
366         check_vtx_full = mask & MAY_VTX_FULL;
367         check_rgetfacl = mask & MAY_RGETFACL;
368
369         mask &= ~(MAY_CREATE | MAY_LINK |
370                 MAY_UNLINK |
371                 MAY_RENAME_SRC | MAY_RENAME_TAR |
372                 MAY_VTX_PART | MAY_VTX_FULL |
373                 MAY_RGETFACL);
374
375         rc = mdd_permission_internal_locked(env, mdd_cobj, NULL, mask,
376                                             MOR_TGT_CHILD);
377
378         if (!rc && (check_create || check_link))
379                 rc = mdd_may_create(env, mdd_cobj, NULL, 1, check_link);
380
381         if (!rc && check_unlink)
382                 rc = mdd_may_unlink(env, mdd_cobj, la);
383
384         if (!rc && (check_rename_src || check_rename_tar)) {
385                 LASSERT(pobj);
386                 mdd_pobj = md2mdd_obj(pobj);
387                 rc = mdd_may_delete(env, mdd_pobj, mdd_cobj, la, NULL, 1,
388                                     check_rename_tar);
389         }
390
391         if (!rc && (check_vtx_part || check_vtx_full)) {
392                 uc = lu_ucred_assert(env);
393                 if (likely(!la)) {
394                         la = &mdd_env_info(env)->mti_la;
395                         rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
396                         if (rc)
397                                 RETURN(rc);
398                 }
399
400                 if (!(la->la_mode & S_ISVTX) || (la->la_uid == uc->uc_fsuid) ||
401                     (check_vtx_full && (ma->ma_attr.la_valid & LA_UID) &&
402                      (ma->ma_attr.la_uid == uc->uc_fsuid))) {
403                         ma->ma_attr_flags |= MDS_VTX_BYPASS;
404                 } else {
405                         ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
406                         if (check_vtx_full)
407                                 rc = -EPERM;
408                 }
409         }
410
411         if (unlikely(!rc && check_rgetfacl)) {
412                 if (likely(!uc))
413                         uc = lu_ucred_assert(env);
414
415                 if (la->la_uid != uc->uc_fsuid &&
416                     !mdd_capable(uc, CFS_CAP_FOWNER))
417                         rc = -EPERM;
418         }
419
420         RETURN(rc);
421 }
422
423 int mdd_capa_get(const struct lu_env *env, struct md_object *obj,
424                  struct lustre_capa *capa, int renewal)
425 {
426         struct mdd_object *mdd_obj = md2mdd_obj(obj);
427         struct obd_capa *oc;
428         int rc = 0;
429         ENTRY;
430
431         oc = mdo_capa_get(env, mdd_obj, renewal ? capa : NULL,
432                           capa->lc_opc);
433         if (IS_ERR(oc)) {
434                 rc = PTR_ERR(oc);
435         } else if (likely(oc != NULL)) {
436                 capa_cpy(capa, oc);
437                 capa_put(oc);
438         }
439
440         RETURN(rc);
441 }