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