xref: /openbmc/linux/fs/fuse/xattr.c (revision dd7b836d)
1 /*
2  * FUSE: Filesystem in Userspace
3  * Copyright (C) 2001-2016  Miklos Szeredi <miklos@szeredi.hu>
4  *
5  * This program can be distributed under the terms of the GNU GPL.
6  * See the file COPYING.
7  */
8 
9 #include "fuse_i.h"
10 
11 #include <linux/xattr.h>
12 #include <linux/posix_acl_xattr.h>
13 
14 int fuse_setxattr(struct inode *inode, const char *name, const void *value,
15 		  size_t size, int flags)
16 {
17 	struct fuse_mount *fm = get_fuse_mount(inode);
18 	FUSE_ARGS(args);
19 	struct fuse_setxattr_in inarg;
20 	int err;
21 
22 	if (fm->fc->no_setxattr)
23 		return -EOPNOTSUPP;
24 
25 	memset(&inarg, 0, sizeof(inarg));
26 	inarg.size = size;
27 	inarg.flags = flags;
28 	args.opcode = FUSE_SETXATTR;
29 	args.nodeid = get_node_id(inode);
30 	args.in_numargs = 3;
31 	args.in_args[0].size = sizeof(inarg);
32 	args.in_args[0].value = &inarg;
33 	args.in_args[1].size = strlen(name) + 1;
34 	args.in_args[1].value = name;
35 	args.in_args[2].size = size;
36 	args.in_args[2].value = value;
37 	err = fuse_simple_request(fm, &args);
38 	if (err == -ENOSYS) {
39 		fm->fc->no_setxattr = 1;
40 		err = -EOPNOTSUPP;
41 	}
42 	if (!err) {
43 		fuse_invalidate_attr(inode);
44 		fuse_update_ctime(inode);
45 	}
46 	return err;
47 }
48 
49 ssize_t fuse_getxattr(struct inode *inode, const char *name, void *value,
50 		      size_t size)
51 {
52 	struct fuse_mount *fm = get_fuse_mount(inode);
53 	FUSE_ARGS(args);
54 	struct fuse_getxattr_in inarg;
55 	struct fuse_getxattr_out outarg;
56 	ssize_t ret;
57 
58 	if (fm->fc->no_getxattr)
59 		return -EOPNOTSUPP;
60 
61 	memset(&inarg, 0, sizeof(inarg));
62 	inarg.size = size;
63 	args.opcode = FUSE_GETXATTR;
64 	args.nodeid = get_node_id(inode);
65 	args.in_numargs = 2;
66 	args.in_args[0].size = sizeof(inarg);
67 	args.in_args[0].value = &inarg;
68 	args.in_args[1].size = strlen(name) + 1;
69 	args.in_args[1].value = name;
70 	/* This is really two different operations rolled into one */
71 	args.out_numargs = 1;
72 	if (size) {
73 		args.out_argvar = true;
74 		args.out_args[0].size = size;
75 		args.out_args[0].value = value;
76 	} else {
77 		args.out_args[0].size = sizeof(outarg);
78 		args.out_args[0].value = &outarg;
79 	}
80 	ret = fuse_simple_request(fm, &args);
81 	if (!ret && !size)
82 		ret = min_t(ssize_t, outarg.size, XATTR_SIZE_MAX);
83 	if (ret == -ENOSYS) {
84 		fm->fc->no_getxattr = 1;
85 		ret = -EOPNOTSUPP;
86 	}
87 	return ret;
88 }
89 
90 static int fuse_verify_xattr_list(char *list, size_t size)
91 {
92 	size_t origsize = size;
93 
94 	while (size) {
95 		size_t thislen = strnlen(list, size);
96 
97 		if (!thislen || thislen == size)
98 			return -EIO;
99 
100 		size -= thislen + 1;
101 		list += thislen + 1;
102 	}
103 
104 	return origsize;
105 }
106 
107 ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
108 {
109 	struct inode *inode = d_inode(entry);
110 	struct fuse_mount *fm = get_fuse_mount(inode);
111 	FUSE_ARGS(args);
112 	struct fuse_getxattr_in inarg;
113 	struct fuse_getxattr_out outarg;
114 	ssize_t ret;
115 
116 	if (fuse_is_bad(inode))
117 		return -EIO;
118 
119 	if (!fuse_allow_current_process(fm->fc))
120 		return -EACCES;
121 
122 	if (fm->fc->no_listxattr)
123 		return -EOPNOTSUPP;
124 
125 	memset(&inarg, 0, sizeof(inarg));
126 	inarg.size = size;
127 	args.opcode = FUSE_LISTXATTR;
128 	args.nodeid = get_node_id(inode);
129 	args.in_numargs = 1;
130 	args.in_args[0].size = sizeof(inarg);
131 	args.in_args[0].value = &inarg;
132 	/* This is really two different operations rolled into one */
133 	args.out_numargs = 1;
134 	if (size) {
135 		args.out_argvar = true;
136 		args.out_args[0].size = size;
137 		args.out_args[0].value = list;
138 	} else {
139 		args.out_args[0].size = sizeof(outarg);
140 		args.out_args[0].value = &outarg;
141 	}
142 	ret = fuse_simple_request(fm, &args);
143 	if (!ret && !size)
144 		ret = min_t(ssize_t, outarg.size, XATTR_LIST_MAX);
145 	if (ret > 0 && size)
146 		ret = fuse_verify_xattr_list(list, ret);
147 	if (ret == -ENOSYS) {
148 		fm->fc->no_listxattr = 1;
149 		ret = -EOPNOTSUPP;
150 	}
151 	return ret;
152 }
153 
154 int fuse_removexattr(struct inode *inode, const char *name)
155 {
156 	struct fuse_mount *fm = get_fuse_mount(inode);
157 	FUSE_ARGS(args);
158 	int err;
159 
160 	if (fm->fc->no_removexattr)
161 		return -EOPNOTSUPP;
162 
163 	args.opcode = FUSE_REMOVEXATTR;
164 	args.nodeid = get_node_id(inode);
165 	args.in_numargs = 1;
166 	args.in_args[0].size = strlen(name) + 1;
167 	args.in_args[0].value = name;
168 	err = fuse_simple_request(fm, &args);
169 	if (err == -ENOSYS) {
170 		fm->fc->no_removexattr = 1;
171 		err = -EOPNOTSUPP;
172 	}
173 	if (!err) {
174 		fuse_invalidate_attr(inode);
175 		fuse_update_ctime(inode);
176 	}
177 	return err;
178 }
179 
180 static int fuse_xattr_get(const struct xattr_handler *handler,
181 			 struct dentry *dentry, struct inode *inode,
182 			 const char *name, void *value, size_t size)
183 {
184 	if (fuse_is_bad(inode))
185 		return -EIO;
186 
187 	return fuse_getxattr(inode, name, value, size);
188 }
189 
190 static int fuse_xattr_set(const struct xattr_handler *handler,
191 			  struct user_namespace *mnt_userns,
192 			  struct dentry *dentry, struct inode *inode,
193 			  const char *name, const void *value, size_t size,
194 			  int flags)
195 {
196 	if (fuse_is_bad(inode))
197 		return -EIO;
198 
199 	if (!value)
200 		return fuse_removexattr(inode, name);
201 
202 	return fuse_setxattr(inode, name, value, size, flags);
203 }
204 
205 static bool no_xattr_list(struct dentry *dentry)
206 {
207 	return false;
208 }
209 
210 static int no_xattr_get(const struct xattr_handler *handler,
211 			struct dentry *dentry, struct inode *inode,
212 			const char *name, void *value, size_t size)
213 {
214 	return -EOPNOTSUPP;
215 }
216 
217 static int no_xattr_set(const struct xattr_handler *handler,
218 			struct user_namespace *mnt_userns,
219 			struct dentry *dentry, struct inode *nodee,
220 			const char *name, const void *value,
221 			size_t size, int flags)
222 {
223 	return -EOPNOTSUPP;
224 }
225 
226 static const struct xattr_handler fuse_xattr_handler = {
227 	.prefix = "",
228 	.get    = fuse_xattr_get,
229 	.set    = fuse_xattr_set,
230 };
231 
232 const struct xattr_handler *fuse_xattr_handlers[] = {
233 	&fuse_xattr_handler,
234 	NULL
235 };
236 
237 const struct xattr_handler *fuse_acl_xattr_handlers[] = {
238 	&posix_acl_access_xattr_handler,
239 	&posix_acl_default_xattr_handler,
240 	&fuse_xattr_handler,
241 	NULL
242 };
243 
244 static const struct xattr_handler fuse_no_acl_access_xattr_handler = {
245 	.name  = XATTR_NAME_POSIX_ACL_ACCESS,
246 	.flags = ACL_TYPE_ACCESS,
247 	.list  = no_xattr_list,
248 	.get   = no_xattr_get,
249 	.set   = no_xattr_set,
250 };
251 
252 static const struct xattr_handler fuse_no_acl_default_xattr_handler = {
253 	.name  = XATTR_NAME_POSIX_ACL_DEFAULT,
254 	.flags = ACL_TYPE_ACCESS,
255 	.list  = no_xattr_list,
256 	.get   = no_xattr_get,
257 	.set   = no_xattr_set,
258 };
259 
260 const struct xattr_handler *fuse_no_acl_xattr_handlers[] = {
261 	&fuse_no_acl_access_xattr_handler,
262 	&fuse_no_acl_default_xattr_handler,
263 	&fuse_xattr_handler,
264 	NULL
265 };
266