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