Whamcloud - gitweb
LU-6450 mdd: Remove unused MAY_ constants
[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, 2014, 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);
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);
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                 la->la_valid = LA_MODE;
140                 rc = mdo_declare_attr_set(env, obj, la, handle);
141         }
142
143         rc = mdd_trans_start(env, mdd, handle);
144         if (rc)
145                 GOTO(stop, rc);
146
147         mdd_write_lock(env, obj, MOR_TGT_CHILD);
148         /* whether ACL can be represented by i_mode only */
149         if (not_equiv)
150                 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_ACL_ACCESS, fl,
151                                    handle);
152         else
153                 rc = mdo_xattr_del(env, obj, XATTR_NAME_ACL_ACCESS, handle);
154         if (rc)
155                 GOTO(unlock, rc);
156
157         if (mode_change)
158                 rc = mdo_attr_set(env, obj, la, handle);
159
160         /* security-replated changes may require sync */
161         if (S_ISDIR(mdd_object_type(obj)))
162                 handle->th_sync |= !!mdd->mdd_sync_permission;
163 unlock:
164         mdd_write_unlock(env, obj);
165 stop:
166         mdd_trans_stop(env, mdd, rc, handle);
167
168         RETURN(rc);
169 }
170
171 /**
172  * Fix mode and ACL according to the default ACL(buf)
173  * \retval = 0 ACL does not need to be reset.
174  * \retval = 1 ACL needs to be reset.
175  * \retval < 0 error.
176  **/
177 int __mdd_fix_mode_acl(const struct lu_env *env, struct lu_buf *buf,
178                        __u32 *mode)
179 {
180         posix_acl_xattr_header  *head;
181         posix_acl_xattr_entry   *entry;
182         int                      entry_count;
183         int                      rc;
184
185         ENTRY;
186
187         head = (posix_acl_xattr_header *)(buf->lb_buf);
188         entry = head->a_entries;
189         entry_count = (buf->lb_len - sizeof(head->a_version)) /
190                       sizeof(posix_acl_xattr_entry);
191         if (entry_count <= 0)
192                 RETURN(0);
193
194         rc = lustre_posix_acl_create_masq(entry, mode, entry_count);
195
196         RETURN(rc);
197 }
198
199 #endif
200
201 /*
202  * Hold read_lock for obj.
203  */
204 static int mdd_check_acl(const struct lu_env *env, struct mdd_object *obj,
205                         const struct lu_attr *la, int mask)
206 {
207 #ifdef CONFIG_FS_POSIX_ACL
208         struct lu_ucred  *uc  = lu_ucred_assert(env);
209         posix_acl_xattr_header *head;
210         posix_acl_xattr_entry *entry;
211         struct lu_buf   *buf;
212         int entry_count;
213         int rc;
214         ENTRY;
215
216         buf = mdd_buf_get(env, mdd_env_info(env)->mti_xattr_buf,
217                           sizeof(mdd_env_info(env)->mti_xattr_buf));
218         rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_ACL_ACCESS);
219         if (rc <= 0)
220                 RETURN(rc ? : -EACCES);
221
222         buf->lb_len = rc;
223         head = (posix_acl_xattr_header *)(buf->lb_buf);
224         entry = head->a_entries;
225         entry_count = posix_acl_xattr_count(buf->lb_len);
226
227         /* Disregard empty ACLs and fall back to
228          * standard UNIX permissions. See LU-5434 */
229         if (entry_count <= 0)
230                 RETURN(-EAGAIN);
231
232         rc = lustre_posix_acl_permission(uc, la, mask, entry, entry_count);
233         RETURN(rc);
234 #else
235         ENTRY;
236         RETURN(-EAGAIN);
237 #endif
238 }
239
240 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
241                               const struct lu_attr *la, int mask, int role)
242 {
243         struct lu_ucred *uc = lu_ucred(env);
244         __u32 mode;
245         int rc;
246         ENTRY;
247
248         if (mask == 0)
249                 RETURN(0);
250
251         /* These means unnecessary for permission check */
252         if ((uc == NULL) || (uc->uc_valid == UCRED_INIT))
253                 RETURN(0);
254
255         /* Invalid user credit */
256         if (uc->uc_valid == UCRED_INVALID)
257                 RETURN(-EACCES);
258
259         /*
260          * Nobody gets write access to an immutable file.
261          */
262         if (mask & MAY_WRITE && la->la_flags & LUSTRE_IMMUTABLE_FL)
263                 RETURN(-EACCES);
264
265         LASSERT(la != NULL);
266
267         mode = la->la_mode;
268         if (uc->uc_fsuid == la->la_uid) {
269                 mode >>= 6;
270         } else {
271                 if (mode & S_IRWXG) {
272                         if (role != -1)
273                                 mdd_read_lock(env, obj, role);
274                         rc = mdd_check_acl(env, obj, la, mask);
275                         if (role != -1)
276                                 mdd_read_unlock(env, obj);
277                         if (rc == -EACCES)
278                                 goto check_capabilities;
279                         else if ((rc != -EAGAIN) && (rc != -EOPNOTSUPP) &&
280                                  (rc != -ENODATA))
281                                 RETURN(rc);
282                 }
283                 if (lustre_in_group_p(uc, la->la_gid))
284                         mode >>= 3;
285         }
286
287         if (((mode & mask & S_IRWXO) == mask))
288                 RETURN(0);
289
290 check_capabilities:
291         if (!(mask & MAY_EXEC) ||
292             (la->la_mode & S_IXUGO) || S_ISDIR(la->la_mode))
293                 if (md_capable(uc, CFS_CAP_DAC_OVERRIDE))
294                         RETURN(0);
295
296         if ((mask == MAY_READ) ||
297             (S_ISDIR(la->la_mode) && !(mask & MAY_WRITE)))
298                 if (md_capable(uc, CFS_CAP_DAC_READ_SEARCH))
299                         RETURN(0);
300
301         CDEBUG(D_SEC, "permission denied, mode %x, fsuid %u, uid %u\n",
302                la->la_mode, uc->uc_fsuid, la->la_uid);
303
304         RETURN(-EACCES);
305 }
306
307 int mdd_permission(const struct lu_env *env,
308                    struct md_object *pobj, struct md_object *cobj,
309                    struct md_attr *ma, int mask)
310 {
311         struct mdd_object *mdd_pobj = NULL;
312         struct mdd_object *mdd_cobj;
313         struct lu_ucred *uc = NULL;
314         struct lu_attr *pattr = NULL;
315         struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
316         int rc = 0;
317         ENTRY;
318
319         LASSERT(cobj);
320         if (pobj != NULL) {
321                 mdd_pobj = md2mdd_obj(pobj);
322                 pattr = MDD_ENV_VAR(env, pattr);
323                 rc = mdd_la_get(env, mdd_pobj, pattr);
324                 if (rc)
325                         RETURN(rc);
326         }
327
328         mdd_cobj = md2mdd_obj(cobj);
329         rc = mdd_la_get(env, mdd_cobj, cattr);
330         if (rc)
331                 RETURN(rc);
332
333         /* For cross_open case, the "mask" is open flags,
334          * so convert it to permission mask first.
335          * XXX: MDS_OPEN_CROSS must be NOT equal to permission mask MAY_*. */
336         if (unlikely(mask & MDS_OPEN_CROSS))
337                 mask = accmode(env, cattr, mask & ~MDS_OPEN_CROSS);
338
339         rc = mdd_permission_internal_locked(env, mdd_cobj, cattr,
340                                             mask & ~MAY_RGETFACL,
341                                             MOR_TGT_CHILD);
342
343         if (unlikely(rc == 0 && (mask & MAY_RGETFACL))) {
344                 if (likely(!uc))
345                         uc = lu_ucred_assert(env);
346
347                 if (cattr->la_uid != uc->uc_fsuid &&
348                     !md_capable(uc, CFS_CAP_FOWNER))
349                         rc = -EPERM;
350         }
351
352         RETURN(rc);
353 }