File Index
Files and directories
Sign in
FILE

acl.c

File preview with syntax highlighting.

linux-6.19.14/fs/9p/acl.c C
  1// SPDX-License-Identifier: LGPL-2.1
  2/*
  3 * Copyright IBM Corporation, 2010
  4 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
  5 */
  6
  7#include <linux/module.h>
  8#include <linux/fs.h>
  9#include <linux/fs_struct.h>
 10#include <net/9p/9p.h>
 11#include <net/9p/client.h>
 12#include <linux/slab.h>
 13#include <linux/sched.h>
 14#include <linux/posix_acl_xattr.h>
 15#include "xattr.h"
 16#include "acl.h"
 17#include "v9fs.h"
 18#include "v9fs_vfs.h"
 19#include "fid.h"
 20
 21static struct posix_acl *v9fs_fid_get_acl(struct p9_fid *fid, const char *name)
 22{
 23	ssize_t size;
 24	void *value = NULL;
 25	struct posix_acl *acl = NULL;
 26
 27	size = v9fs_fid_xattr_get(fid, name, NULL, 0);
 28	if (size < 0)
 29		return ERR_PTR(size);
 30	if (size == 0)
 31		return ERR_PTR(-ENODATA);
 32
 33	value = kzalloc(size, GFP_NOFS);
 34	if (!value)
 35		return ERR_PTR(-ENOMEM);
 36
 37	size = v9fs_fid_xattr_get(fid, name, value, size);
 38	if (size < 0)
 39		acl = ERR_PTR(size);
 40	else if (size == 0)
 41		acl = ERR_PTR(-ENODATA);
 42	else
 43		acl = posix_acl_from_xattr(&init_user_ns, value, size);
 44	kfree(value);
 45	return acl;
 46}
 47
 48static struct posix_acl *v9fs_acl_get(struct dentry *dentry, const char *name)
 49{
 50	struct p9_fid *fid;
 51	struct posix_acl *acl = NULL;
 52
 53	fid = v9fs_fid_lookup(dentry);
 54	if (IS_ERR(fid))
 55		return ERR_CAST(fid);
 56
 57	acl = v9fs_fid_get_acl(fid, name);
 58	p9_fid_put(fid);
 59	return acl;
 60}
 61
 62static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, const char *name)
 63{
 64	int retval;
 65	struct posix_acl *acl = NULL;
 66
 67	acl = v9fs_fid_get_acl(fid, name);
 68	if (!IS_ERR(acl))
 69		return acl;
 70
 71	retval = PTR_ERR(acl);
 72	if (retval == -ENODATA || retval == -ENOSYS || retval == -EOPNOTSUPP)
 73		return NULL;
 74
 75	/* map everything else to -EIO */
 76	return ERR_PTR(-EIO);
 77}
 78
 79int v9fs_get_acl(struct inode *inode, struct p9_fid *fid)
 80{
 81	int retval = 0;
 82	struct posix_acl *pacl, *dacl;
 83	struct v9fs_session_info *v9ses;
 84
 85	v9ses = v9fs_inode2v9ses(inode);
 86	if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
 87			((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
 88		set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
 89		set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
 90		return 0;
 91	}
 92	/* get the default/access acl values and cache them */
 93	dacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_DEFAULT);
 94	pacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_ACCESS);
 95
 96	if (!IS_ERR(dacl) && !IS_ERR(pacl)) {
 97		set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
 98		set_cached_acl(inode, ACL_TYPE_ACCESS, pacl);
 99	} else
100		retval = -EIO;
101
102	if (!IS_ERR(dacl))
103		posix_acl_release(dacl);
104
105	if (!IS_ERR(pacl))
106		posix_acl_release(pacl);
107
108	return retval;
109}
110
111static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
112{
113	struct posix_acl *acl;
114	/*
115	 * 9p Always cache the acl value when
116	 * instantiating the inode (v9fs_inode_from_fid)
117	 */
118	acl = get_cached_acl(inode, type);
119	BUG_ON(is_uncached_acl(acl));
120	return acl;
121}
122
123struct posix_acl *v9fs_iop_get_inode_acl(struct inode *inode, int type, bool rcu)
124{
125	struct v9fs_session_info *v9ses;
126
127	if (rcu)
128		return ERR_PTR(-ECHILD);
129
130	v9ses = v9fs_inode2v9ses(inode);
131	if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
132			((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
133		/*
134		 * On access = client  and acl = on mode get the acl
135		 * values from the server
136		 */
137		return NULL;
138	}
139	return v9fs_get_cached_acl(inode, type);
140
141}
142
143struct posix_acl *v9fs_iop_get_acl(struct mnt_idmap *idmap,
144				   struct dentry *dentry, int type)
145{
146	struct v9fs_session_info *v9ses;
147
148	v9ses = v9fs_dentry2v9ses(dentry);
149	/* We allow set/get/list of acl when access=client is not specified. */
150	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
151		return v9fs_acl_get(dentry, posix_acl_xattr_name(type));
152	return v9fs_get_cached_acl(d_inode(dentry), type);
153}
154
155int v9fs_iop_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
156		     struct posix_acl *acl, int type)
157{
158	int retval;
159	size_t size = 0;
160	void *value = NULL;
161	const char *acl_name;
162	struct v9fs_session_info *v9ses;
163	struct inode *inode = d_inode(dentry);
164
165	if (acl) {
166		retval = posix_acl_valid(inode->i_sb->s_user_ns, acl);
167		if (retval)
168			goto err_out;
169
170		size = posix_acl_xattr_size(acl->a_count);
171
172		value = kzalloc(size, GFP_NOFS);
173		if (!value) {
174			retval = -ENOMEM;
175			goto err_out;
176		}
177
178		retval = posix_acl_to_xattr(&init_user_ns, acl, value, size);
179		if (retval < 0)
180			goto err_out;
181	}
182
183	/*
184	 * set the attribute on the remote. Without even looking at the
185	 * xattr value. We leave it to the server to validate
186	 */
187	acl_name = posix_acl_xattr_name(type);
188	v9ses = v9fs_dentry2v9ses(dentry);
189	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) {
190		retval = v9fs_xattr_set(dentry, acl_name, value, size, 0);
191		goto err_out;
192	}
193
194	if (S_ISLNK(inode->i_mode)) {
195		retval = -EOPNOTSUPP;
196		goto err_out;
197	}
198
199	if (!inode_owner_or_capable(&nop_mnt_idmap, inode)) {
200		retval = -EPERM;
201		goto err_out;
202	}
203
204	switch (type) {
205	case ACL_TYPE_ACCESS:
206		if (acl) {
207			struct iattr iattr = {};
208			struct posix_acl *acl_mode = acl;
209
210			retval = posix_acl_update_mode(&nop_mnt_idmap, inode,
211						       &iattr.ia_mode,
212						       &acl_mode);
213			if (retval)
214				goto err_out;
215			if (!acl_mode) {
216				/*
217				 * ACL can be represented by the mode bits.
218				 * So don't update ACL below.
219				 */
220				kfree(value);
221				value = NULL;
222				size = 0;
223			}
224			iattr.ia_valid = ATTR_MODE;
225			/*
226			 * FIXME should we update ctime ?
227			 * What is the following setxattr update the mode ?
228			 */
229			v9fs_vfs_setattr_dotl(&nop_mnt_idmap, dentry, &iattr);
230		}
231		break;
232	case ACL_TYPE_DEFAULT:
233		if (!S_ISDIR(inode->i_mode)) {
234			retval = acl ? -EINVAL : 0;
235			goto err_out;
236		}
237		break;
238	}
239
240	retval = v9fs_xattr_set(dentry, acl_name, value, size, 0);
241	if (!retval)
242		set_cached_acl(inode, type, acl);
243
244err_out:
245	kfree(value);
246	return retval;
247}
248
249static int v9fs_set_acl(struct p9_fid *fid, int type, struct posix_acl *acl)
250{
251	int retval;
252	char *name;
253	size_t size;
254	void *buffer;
255
256	if (!acl)
257		return 0;
258
259	/* Set a setxattr request to server */
260	size = posix_acl_xattr_size(acl->a_count);
261	buffer = kmalloc(size, GFP_KERNEL);
262	if (!buffer)
263		return -ENOMEM;
264	retval = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
265	if (retval < 0)
266		goto err_free_out;
267	switch (type) {
268	case ACL_TYPE_ACCESS:
269		name = XATTR_NAME_POSIX_ACL_ACCESS;
270		break;
271	case ACL_TYPE_DEFAULT:
272		name = XATTR_NAME_POSIX_ACL_DEFAULT;
273		break;
274	default:
275		BUG();
276	}
277	retval = v9fs_fid_xattr_set(fid, name, buffer, size, 0);
278err_free_out:
279	kfree(buffer);
280	return retval;
281}
282
283int v9fs_acl_chmod(struct inode *inode, struct p9_fid *fid)
284{
285	int retval = 0;
286	struct posix_acl *acl;
287
288	if (S_ISLNK(inode->i_mode))
289		return -EOPNOTSUPP;
290	acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
291	if (acl) {
292		retval = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
293		if (retval)
294			return retval;
295		set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
296		retval = v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl);
297		posix_acl_release(acl);
298	}
299	return retval;
300}
301
302int v9fs_set_create_acl(struct inode *inode, struct p9_fid *fid,
303			struct posix_acl *dacl, struct posix_acl *acl)
304{
305	set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
306	set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
307	v9fs_set_acl(fid, ACL_TYPE_DEFAULT, dacl);
308	v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl);
309	return 0;
310}
311
312void v9fs_put_acl(struct posix_acl *dacl,
313		  struct posix_acl *acl)
314{
315	posix_acl_release(dacl);
316	posix_acl_release(acl);
317}
318
319int v9fs_acl_mode(struct inode *dir, umode_t *modep,
320		  struct posix_acl **dpacl, struct posix_acl **pacl)
321{
322	int retval = 0;
323	umode_t mode = *modep;
324	struct posix_acl *acl = NULL;
325
326	if (!S_ISLNK(mode)) {
327		acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
328		if (IS_ERR(acl))
329			return PTR_ERR(acl);
330		if (!acl)
331			mode &= ~current_umask();
332	}
333	if (acl) {
334		if (S_ISDIR(mode))
335			*dpacl = posix_acl_dup(acl);
336		retval = __posix_acl_create(&acl, GFP_NOFS, &mode);
337		if (retval < 0)
338			return retval;
339		if (retval > 0)
340			*pacl = acl;
341		else
342			posix_acl_release(acl);
343	}
344	*modep  = mode;
345	return 0;
346}
~/ Read-only file preview.