4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdd/mdd_permission.c
34 * Lustre Metadata Server (mdd) routines
36 * Author: fangyong@clusterfs.com
37 * Author: lsy@clusterfs.com
40 #define DEBUG_SUBSYSTEM S_MDS
42 #include <obd_class.h>
43 #include <lustre_ver.h>
44 #include <lprocfs_status.h>
45 #include <lustre_mds.h>
46 #include <lustre_idmap.h>
47 #include "mdd_internal.h"
49 #ifdef CONFIG_FS_POSIX_ACL
52 * Hold write_lock for o.
54 int mdd_acl_chmod(const struct lu_env *env, struct mdd_object *o, __u32 mode,
55 struct thandle *handle)
58 posix_acl_xattr_header *head;
59 posix_acl_xattr_entry *entry;
65 lu_buf_check_and_alloc(&mdd_env_info(env)->mti_xattr_buf,
66 mdd_obj2mdd_dev(o)->mdd_dt_conf.ddp_max_ea_size);
67 buf = mdd_env_info(env)->mti_xattr_buf;
68 rc = mdo_xattr_get(env, o, &buf, XATTR_NAME_ACL_ACCESS);
69 if ((rc == -EOPNOTSUPP) || (rc == -ENODATA))
75 head = (posix_acl_xattr_header *)(buf.lb_buf);
76 entry = head->a_entries;
77 entry_count = (buf.lb_len - sizeof(head->a_version)) /
78 sizeof(posix_acl_xattr_entry);
82 rc = lustre_posix_acl_chmod_masq(entry, mode, entry_count);
86 rc = mdo_xattr_set(env, o, &buf, XATTR_NAME_ACL_ACCESS,
91 int mdd_acl_set(const struct lu_env *env, struct mdd_object *obj,
92 struct lu_attr *la, const struct lu_buf *buf, int fl)
94 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
95 struct thandle *handle;
96 posix_acl_xattr_header *head;
97 posix_acl_xattr_entry *entry;
99 bool not_equiv, mode_change;
104 head = (posix_acl_xattr_header *)(buf->lb_buf);
105 entry = head->a_entries;
106 entry_count = (buf->lb_len - sizeof(head->a_version)) /
107 sizeof(posix_acl_xattr_entry);
108 if (entry_count <= 0)
111 LASSERT(la->la_valid & LA_MODE);
113 rc = lustre_posix_acl_equiv_mode(entry, &mode, entry_count);
117 not_equiv = (rc > 0);
118 mode_change = (mode != la->la_mode);
120 handle = mdd_trans_create(env, mdd);
122 RETURN(PTR_ERR(handle));
124 /* rc tells whether ACL can be represented by i_mode only */
126 rc = mdo_declare_xattr_set(env, obj, buf,
127 XATTR_NAME_ACL_ACCESS, fl, handle);
129 rc = mdo_declare_xattr_del(env, obj, XATTR_NAME_ACL_ACCESS,
136 la->la_valid = LA_MODE;
137 rc = mdo_declare_attr_set(env, obj, la, handle);
140 rc = mdd_trans_start(env, mdd, handle);
144 mdd_write_lock(env, obj, MOR_TGT_CHILD);
145 /* whether ACL can be represented by i_mode only */
147 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_ACL_ACCESS, fl,
150 rc = mdo_xattr_del(env, obj, XATTR_NAME_ACL_ACCESS, handle);
155 rc = mdo_attr_set(env, obj, la, handle);
157 /* security-replated changes may require sync */
158 if (S_ISDIR(mdd_object_type(obj)))
159 handle->th_sync |= !!mdd->mdd_sync_permission;
161 mdd_write_unlock(env, obj);
163 rc = mdd_trans_stop(env, mdd, rc, handle);
169 * Fix mode and ACL according to the default ACL(buf)
170 * \retval = 0 ACL does not need to be reset.
171 * \retval = 1 ACL needs to be reset.
174 int __mdd_fix_mode_acl(const struct lu_env *env, struct lu_buf *buf,
177 posix_acl_xattr_header *head;
178 posix_acl_xattr_entry *entry;
184 head = (posix_acl_xattr_header *)(buf->lb_buf);
185 entry = head->a_entries;
186 entry_count = (buf->lb_len - sizeof(head->a_version)) /
187 sizeof(posix_acl_xattr_entry);
188 if (entry_count <= 0)
191 rc = lustre_posix_acl_create_masq(entry, mode, entry_count);
199 * Hold read_lock for obj.
201 static int mdd_check_acl(const struct lu_env *env, struct mdd_object *obj,
202 const struct lu_attr *la, int mask)
204 #ifdef CONFIG_FS_POSIX_ACL
205 struct lu_ucred *uc = lu_ucred_assert(env);
206 posix_acl_xattr_header *head;
207 posix_acl_xattr_entry *entry;
213 lu_buf_check_and_alloc(&mdd_env_info(env)->mti_xattr_buf,
214 mdd_obj2mdd_dev(obj)->mdd_dt_conf.ddp_max_ea_size);
215 buf = mdd_env_info(env)->mti_xattr_buf;
216 rc = mdo_xattr_get(env, obj, &buf, XATTR_NAME_ACL_ACCESS);
218 RETURN(rc ? : -EACCES);
221 head = (posix_acl_xattr_header *)(buf.lb_buf);
222 entry = head->a_entries;
223 entry_count = posix_acl_xattr_count(buf.lb_len);
225 /* Disregard empty ACLs and fall back to
226 * standard UNIX permissions. See LU-5434 */
227 if (entry_count <= 0)
230 rc = lustre_posix_acl_permission(uc, la, mask, entry, entry_count);
238 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
239 const struct lu_attr *la, int mask, int role)
241 struct lu_ucred *uc = lu_ucred(env);
249 /* These means unnecessary for permission check */
250 if ((uc == NULL) || (uc->uc_valid == UCRED_INIT))
253 /* Invalid user credit */
254 if (uc->uc_valid == UCRED_INVALID)
258 * Nobody gets write access to an immutable file.
260 if (mask & MAY_WRITE && la->la_flags & LUSTRE_IMMUTABLE_FL)
266 if (uc->uc_fsuid == la->la_uid) {
269 if (mode & S_IRWXG) {
271 mdd_read_lock(env, obj, role);
272 rc = mdd_check_acl(env, obj, la, mask);
274 mdd_read_unlock(env, obj);
276 goto check_capabilities;
277 else if ((rc != -EAGAIN) && (rc != -EOPNOTSUPP) &&
281 if (lustre_in_group_p(uc, la->la_gid))
285 if (((mode & mask & S_IRWXO) == mask))
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))
294 if ((mask == MAY_READ) ||
295 (S_ISDIR(la->la_mode) && !(mask & MAY_WRITE)))
296 if (md_capable(uc, CFS_CAP_DAC_READ_SEARCH))
299 CDEBUG(D_SEC, "permission denied, mode %x, fsuid %u, uid %u\n",
300 la->la_mode, uc->uc_fsuid, la->la_uid);
305 int mdd_permission(const struct lu_env *env,
306 struct md_object *pobj, struct md_object *cobj,
307 struct md_attr *ma, int mask)
309 struct mdd_object *mdd_pobj = NULL;
310 struct mdd_object *mdd_cobj;
311 struct lu_ucred *uc = NULL;
312 struct lu_attr *pattr = NULL;
313 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
319 mdd_pobj = md2mdd_obj(pobj);
320 pattr = MDD_ENV_VAR(env, pattr);
321 rc = mdd_la_get(env, mdd_pobj, pattr);
326 mdd_cobj = md2mdd_obj(cobj);
327 rc = mdd_la_get(env, mdd_cobj, cattr);
331 /* For cross_open case, the "mask" is open flags,
332 * so convert it to permission mask first.
333 * XXX: MDS_OPEN_CROSS must be NOT equal to permission mask MAY_*. */
334 if (unlikely(mask & MDS_OPEN_CROSS))
335 mask = accmode(env, cattr, mask & ~MDS_OPEN_CROSS);
337 rc = mdd_permission_internal_locked(env, mdd_cobj, cattr,
338 mask & ~MAY_RGETFACL,
341 if (unlikely(rc == 0 && (mask & MAY_RGETFACL))) {
343 uc = lu_ucred_assert(env);
345 if (cattr->la_uid != uc->uc_fsuid &&
346 !md_capable(uc, CFS_CAP_FOWNER))