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