Whamcloud - gitweb
LU-4973 mdd: only check links limit for non-directories
[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                 struct lu_attr *la, const struct lu_buf *buf, int fl)
97 {
98         struct mdd_device       *mdd = mdd_obj2mdd_dev(obj);
99         struct thandle          *handle;
100         posix_acl_xattr_header  *head;
101         posix_acl_xattr_entry   *entry;
102         int                      rc, entry_count;
103         bool                     not_equiv, mode_change;
104         mode_t                   mode;
105         ENTRY;
106
107         head = (posix_acl_xattr_header *)(buf->lb_buf);
108         entry = head->a_entries;
109         entry_count = (buf->lb_len - sizeof(head->a_version)) /
110                 sizeof(posix_acl_xattr_entry);
111         if (entry_count <= 0)
112                 RETURN(0);
113
114         LASSERT(la->la_valid & LA_MODE);
115         mode = la->la_mode;
116         rc = lustre_posix_acl_equiv_mode(entry, &mode, entry_count);
117         if (rc < 0)
118                 RETURN(rc);
119
120         not_equiv = (rc > 0);
121         mode_change = (mode != la->la_mode);
122
123         handle = mdd_trans_create(env, mdd);
124         if (IS_ERR(handle))
125                 RETURN(PTR_ERR(handle));
126
127         /* rc tells whether ACL can be represented by i_mode only */
128         if (not_equiv)
129                 rc = mdo_declare_xattr_set(env, obj, buf,
130                                 XATTR_NAME_ACL_ACCESS, fl, handle);
131         else
132                 rc = mdo_declare_xattr_del(env, obj, XATTR_NAME_ACL_ACCESS,
133                                 handle);
134         if (rc)
135                 GOTO(stop, rc);
136
137         if (mode_change) {
138                 la->la_mode = mode;
139                 rc = mdo_declare_attr_set(env, obj, la, handle);
140         }
141
142         rc = mdd_trans_start(env, mdd, handle);
143         if (rc)
144                 GOTO(stop, rc);
145
146         mdd_write_lock(env, obj, MOR_TGT_CHILD);
147         /* whether ACL can be represented by i_mode only */
148         if (not_equiv)
149                 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_ACL_ACCESS, fl,
150                                 handle, mdd_object_capa(env, obj));
151         else
152                 rc = mdo_xattr_del(env, obj, XATTR_NAME_ACL_ACCESS, handle,
153                                 mdd_object_capa(env, obj));
154         if (rc)
155                 GOTO(unlock, rc);
156
157         if (mode_change)
158                 rc = mdo_attr_set(env, obj, la, handle,
159                                 mdd_object_capa(env, obj));
160
161         /* security-replated changes may require sync */
162         if (S_ISDIR(mdd_object_type(obj)))
163                 handle->th_sync |= !!mdd->mdd_sync_permission;
164 unlock:
165         mdd_write_unlock(env, obj);
166 stop:
167         mdd_trans_stop(env, mdd, rc, handle);
168
169         RETURN(rc);
170 }
171
172 /**
173  * Fix mode and ACL according to the default ACL(buf)
174  * \retval = 0 ACL does not need to be reset.
175  * \retval = 1 ACL needs to be reset.
176  * \retval < 0 error.
177  **/
178 int __mdd_fix_mode_acl(const struct lu_env *env, struct lu_buf *buf,
179                        __u32 *mode)
180 {
181         posix_acl_xattr_header  *head;
182         posix_acl_xattr_entry   *entry;
183         int                      entry_count;
184         int                      rc;
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         rc = lustre_posix_acl_create_masq(entry, mode, entry_count);
196
197         RETURN(rc);
198 }
199
200 #endif
201
202 /*
203  * Hold read_lock for obj.
204  */
205 static int mdd_check_acl(const struct lu_env *env, struct mdd_object *obj,
206                         const struct lu_attr *la, int mask)
207 {
208 #ifdef CONFIG_FS_POSIX_ACL
209         struct lu_ucred  *uc  = lu_ucred_assert(env);
210         posix_acl_xattr_header *head;
211         posix_acl_xattr_entry *entry;
212         struct lu_buf   *buf;
213         int entry_count;
214         int rc;
215         ENTRY;
216
217         buf = mdd_buf_get(env, mdd_env_info(env)->mti_xattr_buf,
218                           sizeof(mdd_env_info(env)->mti_xattr_buf));
219         rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_ACL_ACCESS,
220                            mdd_object_capa(env, obj));
221         if (rc <= 0)
222                 RETURN(rc ? : -EACCES);
223
224         buf->lb_len = rc;
225         head = (posix_acl_xattr_header *)(buf->lb_buf);
226         entry = head->a_entries;
227         entry_count = (buf->lb_len - sizeof(head->a_version)) /
228                       sizeof(posix_acl_xattr_entry);
229
230         rc = lustre_posix_acl_permission(uc, la, mask, entry, entry_count);
231         RETURN(rc);
232 #else
233         ENTRY;
234         RETURN(-EAGAIN);
235 #endif
236 }
237
238 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
239                                 const struct lu_attr *la, int mask, int role)
240 {
241         struct lu_ucred *uc = lu_ucred(env);
242         __u32 mode;
243         int rc;
244         ENTRY;
245
246         if (mask == 0)
247                 RETURN(0);
248
249         /* These means unnecessary for permission check */
250         if ((uc == NULL) || (uc->uc_valid == UCRED_INIT))
251                 RETURN(0);
252
253         /* Invalid user credit */
254         if (uc->uc_valid == UCRED_INVALID)
255                 RETURN(-EACCES);
256
257         /*
258          * Nobody gets write access to an immutable file.
259          */
260         if ((mask & MAY_WRITE) && mdd_is_immutable(obj))
261                 RETURN(-EACCES);
262
263         LASSERT(la != NULL);
264
265         mode = la->la_mode;
266         if (uc->uc_fsuid == la->la_uid) {
267                 mode >>= 6;
268         } else {
269                 if (mode & S_IRWXG) {
270                         if (role != -1)
271                                 mdd_read_lock(env, obj, role);
272                         rc = mdd_check_acl(env, obj, la, mask);
273                         if (role != -1)
274                                 mdd_read_unlock(env, obj);
275                         if (rc == -EACCES)
276                                 goto check_capabilities;
277                         else if ((rc != -EAGAIN) && (rc != -EOPNOTSUPP) &&
278                                  (rc != -ENODATA))
279                                 RETURN(rc);
280                 }
281                 if (lustre_in_group_p(uc, la->la_gid))
282                         mode >>= 3;
283         }
284
285         if (((mode & mask & S_IRWXO) == mask))
286                 RETURN(0);
287
288 check_capabilities:
289         if (!(mask & MAY_EXEC) ||
290             (la->la_mode & S_IXUGO) || S_ISDIR(la->la_mode))
291                 if (md_capable(uc, CFS_CAP_DAC_OVERRIDE))
292                         RETURN(0);
293
294         if ((mask == MAY_READ) ||
295             (S_ISDIR(la->la_mode) && !(mask & MAY_WRITE)))
296                 if (md_capable(uc, CFS_CAP_DAC_READ_SEARCH))
297                         RETURN(0);
298
299         RETURN(-EACCES);
300 }
301
302 int mdd_permission(const struct lu_env *env,
303                    struct md_object *pobj, struct md_object *cobj,
304                    struct md_attr *ma, int mask)
305 {
306         struct mdd_object *mdd_pobj = NULL;
307         struct mdd_object *mdd_cobj;
308         struct lu_ucred *uc = NULL;
309         struct lu_attr *pattr = NULL;
310         struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
311         bool check_create;
312         bool check_link;
313         int check_unlink;
314         int check_rename_src, check_rename_tar;
315         int check_vtx_part, check_vtx_full;
316         int check_rgetfacl;
317         int rc = 0;
318         ENTRY;
319
320         LASSERT(cobj);
321         if (pobj != NULL) {
322                 mdd_pobj = md2mdd_obj(pobj);
323                 pattr = MDD_ENV_VAR(env, pattr);
324                 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
325                 if (rc)
326                         RETURN(rc);
327         }
328
329         mdd_cobj = md2mdd_obj(cobj);
330         rc = mdd_la_get(env, mdd_cobj, cattr, 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, cattr, 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, cattr, mask,
356                                         MOR_TGT_CHILD);
357
358         if (!rc && check_create)
359                 rc = mdd_may_create(env, mdd_pobj, pattr, mdd_cobj, true);
360
361         if (!rc && check_unlink)
362                 rc = mdd_may_unlink(env, mdd_pobj, pattr, cattr);
363
364         if (!rc && (check_rename_src || check_rename_tar))
365                 rc = mdd_may_delete(env, mdd_pobj, pattr, mdd_cobj, cattr, NULL,
366                                 1, check_rename_tar);
367
368         if (!rc && (check_vtx_part || check_vtx_full)) {
369                 uc = lu_ucred_assert(env);
370
371                 if (!(cattr->la_mode & S_ISVTX) ||
372                     (cattr->la_uid == uc->uc_fsuid) ||
373                     (check_vtx_full && (ma->ma_attr.la_valid & LA_UID) &&
374                      (ma->ma_attr.la_uid == uc->uc_fsuid))) {
375                         ma->ma_attr_flags |= MDS_VTX_BYPASS;
376                 } else {
377                         ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
378                         if (check_vtx_full)
379                                 rc = -EPERM;
380                 }
381         }
382
383         if (unlikely(!rc && check_rgetfacl)) {
384                 if (likely(!uc))
385                         uc = lu_ucred_assert(env);
386
387                 if (cattr->la_uid != uc->uc_fsuid &&
388                     !md_capable(uc, CFS_CAP_FOWNER))
389                         rc = -EPERM;
390         }
391
392         RETURN(rc);
393 }
394
395 int mdd_capa_get(const struct lu_env *env, struct md_object *obj,
396                  struct lustre_capa *capa, int renewal)
397 {
398         struct mdd_object *mdd_obj = md2mdd_obj(obj);
399         struct obd_capa *oc;
400         int rc = 0;
401         ENTRY;
402
403         oc = mdo_capa_get(env, mdd_obj, renewal ? capa : NULL,
404                           capa->lc_opc);
405         if (IS_ERR(oc)) {
406                 rc = PTR_ERR(oc);
407         } else if (likely(oc != NULL)) {
408                 capa_cpy(capa, oc);
409                 capa_put(oc);
410         }
411
412         RETURN(rc);
413 }