Whamcloud - gitweb
505fb315193addb5f1cf9ef35507614b4776fcf7
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/mdd/mdd_permission.c
33  *
34  * Lustre Metadata Server (mdd) routines
35  *
36  * Author: fangyong@clusterfs.com
37  * Author: lsy@clusterfs.com
38  */
39
40 #define DEBUG_SUBSYSTEM S_MDS
41
42 #include <obd_class.h>
43 #include <lprocfs_status.h>
44 #include <lustre_mds.h>
45 #include <lustre_idmap.h>
46 #include "mdd_internal.h"
47
48 #ifdef CONFIG_FS_POSIX_ACL
49
50 /*
51  * Hold write_lock for o.
52  */
53 int mdd_acl_chmod(const struct lu_env *env, struct mdd_object *o, __u32 mode,
54                   struct thandle *handle)
55 {
56         struct lu_buf            buf;
57         posix_acl_xattr_header  *head;
58         posix_acl_xattr_entry   *entry;
59         int                      entry_count;
60         int                      rc;
61
62         ENTRY;
63
64         lu_buf_check_and_alloc(&mdd_env_info(env)->mti_xattr_buf,
65                         mdd_obj2mdd_dev(o)->mdd_dt_conf.ddp_max_ea_size);
66         buf = mdd_env_info(env)->mti_xattr_buf;
67         rc = mdo_xattr_get(env, o, &buf, XATTR_NAME_ACL_ACCESS);
68         if ((rc == -EOPNOTSUPP) || (rc == -ENODATA))
69                 RETURN(0);
70         else if (rc <= 0)
71                 RETURN(rc);
72
73         buf.lb_len = rc;
74         head = (posix_acl_xattr_header *)(buf.lb_buf);
75         entry = GET_POSIX_ACL_XATTR_ENTRY(head);
76         entry_count = (buf.lb_len - sizeof(head->a_version)) /
77                 sizeof(posix_acl_xattr_entry);
78         if (entry_count <= 0)
79                 RETURN(0);
80
81         rc = lustre_posix_acl_chmod_masq(entry, mode, entry_count);
82         if (rc)
83                 RETURN(rc);
84
85         rc = mdo_xattr_set(env, o, &buf, XATTR_NAME_ACL_ACCESS,
86                            0, handle);
87         RETURN(rc);
88 }
89
90 int mdd_acl_set(const struct lu_env *env, struct mdd_object *obj,
91                 struct lu_attr *la, const struct lu_buf *buf, int fl)
92 {
93         struct mdd_device       *mdd = mdd_obj2mdd_dev(obj);
94         struct thandle          *handle;
95         posix_acl_xattr_header  *head;
96         posix_acl_xattr_entry   *entry;
97         int                      entry_count;
98         bool                     not_equiv, mode_change;
99         mode_t                   mode;
100         int                      rc;
101         ENTRY;
102
103         head = (posix_acl_xattr_header *)(buf->lb_buf);
104         entry = GET_POSIX_ACL_XATTR_ENTRY(head);
105         entry_count = (buf->lb_len - sizeof(head->a_version)) /
106                 sizeof(posix_acl_xattr_entry);
107         if (entry_count <= 0)
108                 RETURN(0);
109
110         LASSERT(la->la_valid & LA_MODE);
111         mode = la->la_mode;
112         rc = lustre_posix_acl_equiv_mode(entry, &mode, entry_count);
113         if (rc < 0)
114                 RETURN(rc);
115
116         not_equiv = (rc > 0);
117         mode_change = (mode != la->la_mode);
118
119         handle = mdd_trans_create(env, mdd);
120         if (IS_ERR(handle))
121                 RETURN(PTR_ERR(handle));
122
123         /* rc tells whether ACL can be represented by i_mode only */
124         if (not_equiv)
125                 rc = mdo_declare_xattr_set(env, obj, buf,
126                                 XATTR_NAME_ACL_ACCESS, fl, handle);
127         else
128                 rc = mdo_declare_xattr_del(env, obj, XATTR_NAME_ACL_ACCESS,
129                                 handle);
130         if (rc)
131                 GOTO(stop, rc);
132
133         if (mode_change) {
134                 la->la_mode = mode;
135                 la->la_valid = LA_MODE;
136                 rc = mdo_declare_attr_set(env, obj, la, handle);
137         }
138
139         rc = mdd_trans_start(env, mdd, handle);
140         if (rc)
141                 GOTO(stop, rc);
142
143         mdd_write_lock(env, obj, MOR_TGT_CHILD);
144         /* whether ACL can be represented by i_mode only */
145         if (not_equiv)
146                 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_ACL_ACCESS, fl,
147                                    handle);
148         else
149                 rc = mdo_xattr_del(env, obj, XATTR_NAME_ACL_ACCESS, handle);
150         if (rc)
151                 GOTO(unlock, rc);
152
153         if (mode_change)
154                 rc = mdo_attr_set(env, obj, la, handle);
155
156         /* security-replated changes may require sync */
157         if (S_ISDIR(mdd_object_type(obj)))
158                 handle->th_sync |= !!mdd->mdd_sync_permission;
159 unlock:
160         mdd_write_unlock(env, obj);
161 stop:
162         rc = mdd_trans_stop(env, mdd, rc, handle);
163
164         RETURN(rc);
165 }
166
167 /**
168  * Fix mode and ACL according to the default ACL(buf)
169  * \retval = 0 ACL does not need to be reset.
170  * \retval = 1 ACL needs to be reset.
171  * \retval < 0 error.
172  **/
173 int __mdd_fix_mode_acl(const struct lu_env *env, struct lu_buf *buf,
174                        __u32 *mode)
175 {
176         posix_acl_xattr_header  *head;
177         posix_acl_xattr_entry   *entry;
178         int                      entry_count;
179         int                      rc;
180
181         ENTRY;
182
183         head = (posix_acl_xattr_header *)(buf->lb_buf);
184         entry = GET_POSIX_ACL_XATTR_ENTRY(head);
185         entry_count = (buf->lb_len - sizeof(head->a_version)) /
186                       sizeof(posix_acl_xattr_entry);
187         if (entry_count <= 0)
188                 RETURN(0);
189
190         rc = lustre_posix_acl_create_masq(entry, mode, entry_count);
191
192         RETURN(rc);
193 }
194
195 #endif
196
197 /*
198  * Hold read_lock for obj.
199  */
200 static int mdd_check_acl(const struct lu_env *env, struct mdd_object *obj,
201                         const struct lu_attr *la, int mask)
202 {
203 #ifdef CONFIG_FS_POSIX_ACL
204         struct lu_ucred  *uc  = lu_ucred_assert(env);
205         posix_acl_xattr_header *head;
206         posix_acl_xattr_entry *entry;
207         struct lu_buf buf;
208         int entry_count;
209         int rc;
210         ENTRY;
211
212         lu_buf_check_and_alloc(&mdd_env_info(env)->mti_xattr_buf,
213                         mdd_obj2mdd_dev(obj)->mdd_dt_conf.ddp_max_ea_size);
214         buf = mdd_env_info(env)->mti_xattr_buf;
215         rc = mdo_xattr_get(env, obj, &buf, XATTR_NAME_ACL_ACCESS);
216         if (rc <= 0)
217                 RETURN(rc ? : -EACCES);
218
219         buf.lb_len = rc;
220         head = (posix_acl_xattr_header *)(buf.lb_buf);
221         entry = GET_POSIX_ACL_XATTR_ENTRY(head);
222         entry_count = posix_acl_xattr_count(buf.lb_len);
223
224         /* Disregard empty ACLs and fall back to
225          * standard UNIX permissions. See LU-5434 */
226         if (entry_count <= 0)
227                 RETURN(-EAGAIN);
228
229         rc = lustre_posix_acl_permission(uc, la, mask, entry, entry_count);
230         RETURN(rc);
231 #else
232         ENTRY;
233         RETURN(-EAGAIN);
234 #endif
235 }
236
237 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
238                               const struct lu_attr *la, int mask, int role)
239 {
240         struct lu_ucred *uc = lu_ucred(env);
241         __u32 mode;
242         int rc;
243         ENTRY;
244
245         if (mask == 0)
246                 RETURN(0);
247
248         /* These means unnecessary for permission check */
249         if ((uc == NULL) || (uc->uc_valid == UCRED_INIT))
250                 RETURN(0);
251
252         /* Invalid user credit */
253         if (uc->uc_valid == UCRED_INVALID)
254                 RETURN(-EACCES);
255
256         /*
257          * Nobody gets write access to an immutable file.
258          */
259         if (mask & MAY_WRITE && la->la_flags & LUSTRE_IMMUTABLE_FL)
260                 RETURN(-EACCES);
261
262         LASSERT(la != NULL);
263
264         mode = la->la_mode;
265         if (uc->uc_fsuid == la->la_uid) {
266                 mode >>= 6;
267         } else {
268                 if (mode & S_IRWXG) {
269                         if (role != -1)
270                                 mdd_read_lock(env, obj, role);
271                         rc = mdd_check_acl(env, obj, la, mask);
272                         if (role != -1)
273                                 mdd_read_unlock(env, obj);
274                         if (rc == -EACCES)
275                                 goto check_capabilities;
276                         else if ((rc != -EAGAIN) && (rc != -EOPNOTSUPP) &&
277                                  (rc != -ENODATA))
278                                 RETURN(rc);
279                 }
280                 if (lustre_in_group_p(uc, la->la_gid))
281                         mode >>= 3;
282         }
283
284         if (((mode & mask & S_IRWXO) == mask))
285                 RETURN(0);
286
287 check_capabilities:
288         if (!(mask & MAY_EXEC) ||
289             (la->la_mode & S_IXUGO) || S_ISDIR(la->la_mode))
290                 if (md_capable(uc, CFS_CAP_DAC_OVERRIDE))
291                         RETURN(0);
292
293         if ((mask == MAY_READ) ||
294             (S_ISDIR(la->la_mode) && !(mask & MAY_WRITE)))
295                 if (md_capable(uc, CFS_CAP_DAC_READ_SEARCH))
296                         RETURN(0);
297
298         CDEBUG(D_SEC, "permission denied, mode %x, fsuid %u, uid %u\n",
299                la->la_mode, uc->uc_fsuid, la->la_uid);
300
301         RETURN(-EACCES);
302 }
303
304 int mdd_permission(const struct lu_env *env,
305                    struct md_object *pobj, struct md_object *cobj,
306                    struct md_attr *ma, int mask)
307 {
308         struct mdd_object *mdd_pobj = NULL;
309         struct mdd_object *mdd_cobj;
310         struct lu_ucred *uc = NULL;
311         struct lu_attr *pattr = NULL;
312         struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
313         int rc = 0;
314         ENTRY;
315
316         LASSERT(cobj);
317         if (pobj != NULL) {
318                 mdd_pobj = md2mdd_obj(pobj);
319                 pattr = MDD_ENV_VAR(env, pattr);
320                 rc = mdd_la_get(env, mdd_pobj, pattr);
321                 if (rc)
322                         RETURN(rc);
323         }
324
325         mdd_cobj = md2mdd_obj(cobj);
326         rc = mdd_la_get(env, mdd_cobj, cattr);
327         if (rc)
328                 RETURN(rc);
329
330         /* For cross_open case, the "mask" is open flags,
331          * so convert it to permission mask first.
332          * XXX: MDS_OPEN_CROSS must be NOT equal to permission mask MAY_*. */
333         if (unlikely(mask & MDS_OPEN_CROSS))
334                 mask = accmode(env, cattr, mask & ~MDS_OPEN_CROSS);
335
336         rc = mdd_permission_internal_locked(env, mdd_cobj, cattr,
337                                             mask & ~MAY_RGETFACL,
338                                             MOR_TGT_CHILD);
339
340         if (unlikely(rc == 0 && (mask & MAY_RGETFACL))) {
341                 if (likely(!uc))
342                         uc = lu_ucred_assert(env);
343
344                 if (cattr->la_uid != uc->uc_fsuid &&
345                     !md_capable(uc, CFS_CAP_FOWNER))
346                         rc = -EPERM;
347         }
348
349         RETURN(rc);
350 }