Whamcloud - gitweb
Revert "LU-1346 libcfs: replace CFS_CAP_XXX with kernel definition"
[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, 2013, 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 #include <lustre_idmap.h>
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         if (S_ISDIR(mdd_object_type(obj)))
168                 handle->th_sync |= !!mdd->mdd_sync_permission;
169 unlock:
170         mdd_write_unlock(env, obj);
171 stop:
172         mdd_trans_stop(env, mdd, rc, handle);
173
174         RETURN(rc);
175 }
176
177 /**
178  * Fix mode and ACL according to the default ACL(buf)
179  * \retval = 0 ACL does not need to be reset.
180  * \retval = 1 ACL needs to be reset.
181  * \retval < 0 error.
182  **/
183 int __mdd_fix_mode_acl(const struct lu_env *env, struct lu_buf *buf,
184                        __u32 *mode)
185 {
186         posix_acl_xattr_header  *head;
187         posix_acl_xattr_entry   *entry;
188         int                      entry_count;
189         int                      rc;
190
191         ENTRY;
192
193         head = (posix_acl_xattr_header *)(buf->lb_buf);
194         entry = head->a_entries;
195         entry_count = (buf->lb_len - sizeof(head->a_version)) /
196                       sizeof(posix_acl_xattr_entry);
197         if (entry_count <= 0)
198                 RETURN(0);
199
200         rc = lustre_posix_acl_create_masq(entry, mode, entry_count);
201
202         RETURN(rc);
203 }
204
205 #endif
206
207 /*
208  * Hold read_lock for obj.
209  */
210 static int mdd_check_acl(const struct lu_env *env, struct mdd_object *obj,
211                          struct lu_attr *la, int mask)
212 {
213 #ifdef CONFIG_FS_POSIX_ACL
214         struct lu_ucred  *uc  = lu_ucred_assert(env);
215         posix_acl_xattr_header *head;
216         posix_acl_xattr_entry *entry;
217         struct lu_buf   *buf;
218         int entry_count;
219         int rc;
220         ENTRY;
221
222         buf = mdd_buf_get(env, mdd_env_info(env)->mti_xattr_buf,
223                           sizeof(mdd_env_info(env)->mti_xattr_buf));
224         rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_ACL_ACCESS,
225                            mdd_object_capa(env, obj));
226         if (rc <= 0)
227                 RETURN(rc ? : -EACCES);
228
229         buf->lb_len = rc;
230         head = (posix_acl_xattr_header *)(buf->lb_buf);
231         entry = head->a_entries;
232         entry_count = (buf->lb_len - sizeof(head->a_version)) /
233                       sizeof(posix_acl_xattr_entry);
234
235         rc = lustre_posix_acl_permission(uc, la, mask, entry, entry_count);
236         RETURN(rc);
237 #else
238         ENTRY;
239         RETURN(-EAGAIN);
240 #endif
241 }
242
243 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
244                               struct lu_attr *la, int mask, int role)
245 {
246         struct lu_ucred *uc = lu_ucred(env);
247         __u32 mode;
248         int rc;
249         ENTRY;
250
251         if (mask == 0)
252                 RETURN(0);
253
254         /* These means unnecessary for permission check */
255         if ((uc == NULL) || (uc->uc_valid == UCRED_INIT))
256                 RETURN(0);
257
258         /* Invalid user credit */
259         if (uc->uc_valid == UCRED_INVALID)
260                 RETURN(-EACCES);
261
262         /*
263          * Nobody gets write access to an immutable file.
264          */
265         if ((mask & MAY_WRITE) && mdd_is_immutable(obj))
266                 RETURN(-EACCES);
267
268         if (la == NULL) {
269                 la = &mdd_env_info(env)->mti_la;
270                 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
271                 if (rc)
272                         RETURN(rc);
273         }
274
275         mode = la->la_mode;
276         if (uc->uc_fsuid == la->la_uid) {
277                 mode >>= 6;
278         } else {
279                 if (mode & S_IRWXG) {
280                         if (role != -1)
281                                 mdd_read_lock(env, obj, role);
282                         rc = mdd_check_acl(env, obj, la, mask);
283                         if (role != -1)
284                                 mdd_read_unlock(env, obj);
285                         if (rc == -EACCES)
286                                 goto check_capabilities;
287                         else if ((rc != -EAGAIN) && (rc != -EOPNOTSUPP) &&
288                                  (rc != -ENODATA))
289                                 RETURN(rc);
290                 }
291                 if (lustre_in_group_p(uc, la->la_gid))
292                         mode >>= 3;
293         }
294
295         if (((mode & mask & S_IRWXO) == mask))
296                 RETURN(0);
297
298 check_capabilities:
299         if (!(mask & MAY_EXEC) ||
300             (la->la_mode & S_IXUGO) || S_ISDIR(la->la_mode))
301                 if (md_capable(uc, CFS_CAP_DAC_OVERRIDE))
302                         RETURN(0);
303
304         if ((mask == MAY_READ) ||
305             (S_ISDIR(la->la_mode) && !(mask & MAY_WRITE)))
306                 if (md_capable(uc, CFS_CAP_DAC_READ_SEARCH))
307                         RETURN(0);
308
309         RETURN(-EACCES);
310 }
311
312 int mdd_permission(const struct lu_env *env,
313                    struct md_object *pobj, struct md_object *cobj,
314                    struct md_attr *ma, int mask)
315 {
316         struct mdd_object *mdd_pobj, *mdd_cobj;
317         struct lu_ucred *uc = NULL;
318         struct lu_attr *la = &mdd_env_info(env)->mti_cattr;
319         int check_create, check_link;
320         int check_unlink;
321         int check_rename_src, check_rename_tar;
322         int check_vtx_part, check_vtx_full;
323         int check_rgetfacl;
324         int rc = 0;
325         ENTRY;
326
327         LASSERT(cobj);
328         mdd_cobj = md2mdd_obj(cobj);
329
330         rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
331         if (rc)
332                 RETURN(rc);
333
334         /* For cross_open case, the "mask" is open flags,
335          * so convert it to permission mask first.
336          * XXX: MDS_OPEN_CROSS must be NOT equal to permission mask MAY_*. */
337         if (unlikely(mask & MDS_OPEN_CROSS))
338                 mask = accmode(env, la, mask & ~MDS_OPEN_CROSS);
339
340         check_create = mask & MAY_CREATE;
341         check_link = mask & MAY_LINK;
342         check_unlink = mask & MAY_UNLINK;
343         check_rename_src = mask & MAY_RENAME_SRC;
344         check_rename_tar = mask & MAY_RENAME_TAR;
345         check_vtx_part = mask & MAY_VTX_PART;
346         check_vtx_full = mask & MAY_VTX_FULL;
347         check_rgetfacl = mask & MAY_RGETFACL;
348
349         mask &= ~(MAY_CREATE | MAY_LINK |
350                 MAY_UNLINK |
351                 MAY_RENAME_SRC | MAY_RENAME_TAR |
352                 MAY_VTX_PART | MAY_VTX_FULL |
353                 MAY_RGETFACL);
354
355         rc = mdd_permission_internal_locked(env, mdd_cobj, NULL, mask,
356                                             MOR_TGT_CHILD);
357
358         if (!rc && (check_create || check_link))
359                 rc = mdd_may_create(env, mdd_cobj, NULL, 1, check_link);
360
361         if (!rc && check_unlink)
362                 rc = mdd_may_unlink(env, mdd_cobj, la);
363
364         if (!rc && (check_rename_src || check_rename_tar)) {
365                 LASSERT(pobj);
366                 mdd_pobj = md2mdd_obj(pobj);
367                 rc = mdd_may_delete(env, mdd_pobj, mdd_cobj, la, NULL, 1,
368                                     check_rename_tar);
369         }
370
371         if (!rc && (check_vtx_part || check_vtx_full)) {
372                 uc = lu_ucred_assert(env);
373                 if (likely(!la)) {
374                         la = &mdd_env_info(env)->mti_la;
375                         rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
376                         if (rc)
377                                 RETURN(rc);
378                 }
379
380                 if (!(la->la_mode & S_ISVTX) || (la->la_uid == uc->uc_fsuid) ||
381                     (check_vtx_full && (ma->ma_attr.la_valid & LA_UID) &&
382                      (ma->ma_attr.la_uid == uc->uc_fsuid))) {
383                         ma->ma_attr_flags |= MDS_VTX_BYPASS;
384                 } else {
385                         ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
386                         if (check_vtx_full)
387                                 rc = -EPERM;
388                 }
389         }
390
391         if (unlikely(!rc && check_rgetfacl)) {
392                 if (likely(!uc))
393                         uc = lu_ucred_assert(env);
394
395                 if (la->la_uid != uc->uc_fsuid &&
396                     !md_capable(uc, CFS_CAP_FOWNER))
397                         rc = -EPERM;
398         }
399
400         RETURN(rc);
401 }
402
403 int mdd_capa_get(const struct lu_env *env, struct md_object *obj,
404                  struct lustre_capa *capa, int renewal)
405 {
406         struct mdd_object *mdd_obj = md2mdd_obj(obj);
407         struct obd_capa *oc;
408         int rc = 0;
409         ENTRY;
410
411         oc = mdo_capa_get(env, mdd_obj, renewal ? capa : NULL,
412                           capa->lc_opc);
413         if (IS_ERR(oc)) {
414                 rc = PTR_ERR(oc);
415         } else if (likely(oc != NULL)) {
416                 capa_cpy(capa, oc);
417                 capa_put(oc);
418         }
419
420         RETURN(rc);
421 }