Whamcloud - gitweb
21e165910fee1a933b8864ed7a03ac2510eda892
[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  * Get default acl EA only.
55  * Hold read_lock for mdd_obj.
56  */
57 int mdd_def_acl_get(const struct lu_env *env, struct mdd_object *mdd_obj,
58                     struct md_attr *ma)
59 {
60         struct lu_buf *buf;
61         int rc;
62         ENTRY;
63
64         if (ma->ma_valid & MA_ACL_DEF)
65                 RETURN(0);
66
67         buf = mdd_buf_get(env, ma->ma_acl, ma->ma_acl_size);
68         rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_ACL_DEFAULT,
69                            BYPASS_CAPA);
70         if (rc > 0) {
71                 ma->ma_acl_size = rc;
72                 ma->ma_valid |= MA_ACL_DEF;
73                 rc = 0;
74         } else if ((rc == -EOPNOTSUPP) || (rc == -ENODATA)) {
75                 rc = 0;
76         }
77         RETURN(rc);
78 }
79
80 /*
81  * Hold write_lock for o.
82  */
83 int mdd_acl_chmod(const struct lu_env *env, struct mdd_object *o, __u32 mode,
84                   struct thandle *handle)
85 {
86         struct lu_buf           *buf;
87         posix_acl_xattr_header  *head;
88         posix_acl_xattr_entry   *entry;
89         int                      entry_count;
90         int                      rc;
91
92         ENTRY;
93
94         buf = mdd_buf_get(env, mdd_env_info(env)->mti_xattr_buf,
95                           sizeof(mdd_env_info(env)->mti_xattr_buf));
96
97         rc = mdo_xattr_get(env, o, buf, XATTR_NAME_ACL_ACCESS, BYPASS_CAPA);
98         if ((rc == -EOPNOTSUPP) || (rc == -ENODATA))
99                 RETURN(0);
100         else if (rc <= 0)
101                 RETURN(rc);
102
103         buf->lb_len = rc;
104         head = (posix_acl_xattr_header *)(buf->lb_buf);
105         entry = head->a_entries;
106         entry_count = (buf->lb_len - sizeof(head->a_version)) /
107                       sizeof(posix_acl_xattr_entry);
108         if (entry_count <= 0)
109                 RETURN(0);
110
111         rc = lustre_posix_acl_chmod_masq(entry, mode, entry_count);
112         if (rc)
113                 RETURN(rc);
114
115         rc = mdo_xattr_set(env, o, buf, XATTR_NAME_ACL_ACCESS,
116                            0, handle, BYPASS_CAPA);
117         RETURN(rc);
118 }
119
120 /*
121  * Hold write_lock for obj.
122  */
123 int __mdd_acl_init(const struct lu_env *env, struct mdd_object *obj,
124                    struct lu_buf *buf, __u32 *mode, struct thandle *handle)
125 {
126         posix_acl_xattr_header  *head;
127         posix_acl_xattr_entry   *entry;
128         int                      entry_count;
129         int                      rc;
130
131         ENTRY;
132
133         head = (posix_acl_xattr_header *)(buf->lb_buf);
134         entry = head->a_entries;
135         entry_count = (buf->lb_len - sizeof(head->a_version)) /
136                       sizeof(posix_acl_xattr_entry);
137         if (entry_count <= 0)
138                 RETURN(0);
139
140         if (S_ISDIR(*mode)) {
141                 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_ACL_DEFAULT, 0,
142                                    handle, BYPASS_CAPA);
143                 if (rc)
144                         RETURN(rc);
145         }
146
147         rc = lustre_posix_acl_create_masq(entry, mode, entry_count);
148         if (rc <= 0)
149                 RETURN(rc);
150
151         rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_ACL_ACCESS, 0, handle,
152                            BYPASS_CAPA);
153         RETURN(rc);
154 }
155 #endif
156
157 /*
158  * Hold read_lock for obj.
159  */
160 static int mdd_check_acl(const struct lu_env *env, struct mdd_object *obj,
161                          struct lu_attr *la, int mask)
162 {
163 #ifdef CONFIG_FS_POSIX_ACL
164         struct md_ucred  *uc  = md_ucred(env);
165         posix_acl_xattr_header *head;
166         posix_acl_xattr_entry *entry;
167         struct lu_buf   *buf;
168         int entry_count;
169         int rc;
170         ENTRY;
171
172         buf = mdd_buf_get(env, mdd_env_info(env)->mti_xattr_buf,
173                           sizeof(mdd_env_info(env)->mti_xattr_buf));
174         rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_ACL_ACCESS,
175                            mdd_object_capa(env, obj));
176         if (rc <= 0)
177                 RETURN(rc ? : -EACCES);
178
179         buf->lb_len = rc;
180         head = (posix_acl_xattr_header *)(buf->lb_buf);
181         entry = head->a_entries;
182         entry_count = (buf->lb_len - sizeof(head->a_version)) /
183                       sizeof(posix_acl_xattr_entry);
184
185         rc = lustre_posix_acl_permission(uc, la, mask, entry, entry_count);
186         RETURN(rc);
187 #else
188         ENTRY;
189         RETURN(-EAGAIN);
190 #endif
191 }
192
193 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
194                               struct lu_attr *la, int mask, int role)
195 {
196         struct md_ucred *uc = md_ucred(env);
197         __u32 mode;
198         int rc;
199         ENTRY;
200
201         if (mask == 0)
202                 RETURN(0);
203
204         /* These means unnecessary for permission check */
205         if ((uc == NULL) || (uc->mu_valid == UCRED_INIT))
206                 RETURN(0);
207
208         /* Invalid user credit */
209         if (uc->mu_valid == UCRED_INVALID)
210                 RETURN(-EACCES);
211
212         /*
213          * Nobody gets write access to an immutable file.
214          */
215         if ((mask & MAY_WRITE) && mdd_is_immutable(obj))
216                 RETURN(-EACCES);
217
218         if (la == NULL) {
219                 la = &mdd_env_info(env)->mti_la;
220                 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
221                 if (rc)
222                         RETURN(rc);
223         }
224
225         mode = la->la_mode;
226         if (uc->mu_fsuid == la->la_uid) {
227                 mode >>= 6;
228         } else {
229                 if (mode & S_IRWXG) {
230                         if (role != -1)
231                                 mdd_read_lock(env, obj, role);
232                         rc = mdd_check_acl(env, obj, la, mask);
233                         if (role != -1)
234                                 mdd_read_unlock(env, obj);
235                         if (rc == -EACCES)
236                                 goto check_capabilities;
237                         else if ((rc != -EAGAIN) && (rc != -EOPNOTSUPP) &&
238                                  (rc != -ENODATA))
239                                 RETURN(rc);
240                 }
241                 if (lustre_in_group_p(uc, la->la_gid))
242                         mode >>= 3;
243         }
244
245         if (((mode & mask & S_IRWXO) == mask))
246                 RETURN(0);
247
248 check_capabilities:
249         if (!(mask & MAY_EXEC) ||
250             (la->la_mode & S_IXUGO) || S_ISDIR(la->la_mode))
251                 if (mdd_capable(uc, CFS_CAP_DAC_OVERRIDE))
252                         RETURN(0);
253
254         if ((mask == MAY_READ) ||
255             (S_ISDIR(la->la_mode) && !(mask & MAY_WRITE)))
256                 if (mdd_capable(uc, CFS_CAP_DAC_READ_SEARCH))
257                         RETURN(0);
258
259         RETURN(-EACCES);
260 }
261
262 int mdd_permission(const struct lu_env *env,
263                    struct md_object *pobj, struct md_object *cobj,
264                    struct md_attr *ma, int mask)
265 {
266         struct mdd_object *mdd_pobj, *mdd_cobj;
267         struct md_ucred *uc = NULL;
268         struct lu_attr *la = NULL;
269         int check_create, check_link;
270         int check_unlink;
271         int check_rename_src, check_rename_tar;
272         int check_vtx_part, check_vtx_full;
273         int check_rgetfacl;
274         int rc = 0;
275         ENTRY;
276
277         LASSERT(cobj);
278         mdd_cobj = md2mdd_obj(cobj);
279
280         /* For cross_open case, the "mask" is open flags,
281          * so convert it to permission mask first.
282          * XXX: MDS_OPEN_CROSS must be NOT equal to permission mask MAY_*. */
283         if (unlikely(mask & MDS_OPEN_CROSS)) {
284                 la = &mdd_env_info(env)->mti_la;
285                 rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
286                 if (rc)
287                         RETURN(rc);
288
289                 mask = accmode(env, la, mask & ~MDS_OPEN_CROSS);
290         }
291
292         check_create = mask & MAY_CREATE;
293         check_link = mask & MAY_LINK;
294         check_unlink = mask & MAY_UNLINK;
295         check_rename_src = mask & MAY_RENAME_SRC;
296         check_rename_tar = mask & MAY_RENAME_TAR;
297         check_vtx_part = mask & MAY_VTX_PART;
298         check_vtx_full = mask & MAY_VTX_FULL;
299         check_rgetfacl = mask & MAY_RGETFACL;
300
301         mask &= ~(MAY_CREATE | MAY_LINK |
302                 MAY_UNLINK |
303                 MAY_RENAME_SRC | MAY_RENAME_TAR |
304                 MAY_VTX_PART | MAY_VTX_FULL |
305                 MAY_RGETFACL);
306
307         rc = mdd_permission_internal_locked(env, mdd_cobj, NULL, mask,
308                                             MOR_TGT_CHILD);
309
310         if (!rc && (check_create || check_link))
311                 rc = mdd_may_create(env, mdd_cobj, NULL, 1, check_link);
312
313         if (!rc && check_unlink) {
314                 LASSERT(ma);
315                 rc = mdd_may_unlink(env, mdd_cobj, ma);
316         }
317
318         if (!rc && (check_rename_src || check_rename_tar)) {
319                 LASSERT(pobj);
320                 LASSERT(ma);
321                 mdd_pobj = md2mdd_obj(pobj);
322                 rc = mdd_may_delete(env, mdd_pobj, mdd_cobj, ma, 1,
323                                     check_rename_tar);
324         }
325
326         if (!rc && (check_vtx_part || check_vtx_full)) {
327                 uc = md_ucred(env);
328                 LASSERT(ma);
329                 if (likely(!la)) {
330                         la = &mdd_env_info(env)->mti_la;
331                         rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
332                         if (rc)
333                                 RETURN(rc);
334                 }
335
336                 if (!(la->la_mode & S_ISVTX) || (la->la_uid == uc->mu_fsuid) ||
337                     (check_vtx_full && (ma->ma_attr.la_valid & LA_UID) &&
338                     (ma->ma_attr.la_uid == uc->mu_fsuid))) {
339                         ma->ma_attr_flags |= MDS_VTX_BYPASS;
340                 } else {
341                         ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
342                         if (check_vtx_full)
343                                 rc = -EPERM;
344                 }
345         }
346
347         if (unlikely(!rc && check_rgetfacl)) {
348                 if (likely(!uc))
349                         uc = md_ucred(env);
350
351                 if (likely(!la)) {
352                         la = &mdd_env_info(env)->mti_la;
353                         rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
354                         if (rc)
355                                 RETURN(rc);
356                 }
357
358                 if (la->la_uid != uc->mu_fsuid &&
359                     !mdd_capable(uc, CFS_CAP_FOWNER))
360                         rc = -EPERM;
361         }
362
363         RETURN(rc);
364 }
365
366 int mdd_capa_get(const struct lu_env *env, struct md_object *obj,
367                  struct lustre_capa *capa, int renewal)
368 {
369         struct mdd_object *mdd_obj = md2mdd_obj(obj);
370         struct obd_capa *oc;
371         int rc = 0;
372         ENTRY;
373
374         oc = mdo_capa_get(env, mdd_obj, renewal ? capa : NULL,
375                           capa->lc_opc);
376         if (IS_ERR(oc)) {
377                 rc = PTR_ERR(oc);
378         } else if (likely(oc != NULL)) {
379                 capa_cpy(capa, oc);
380                 capa_put(oc);
381         }
382
383         RETURN(rc);
384 }