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