• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
fuse_setxattr(struct inode * inode,const char * name,const void * value,size_t size,int flags,unsigned int extra_flags)14 int fuse_setxattr(struct inode *inode, const char *name, const void *value,
15 		  size_t size, int flags, unsigned int extra_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 	inarg.setxattr_flags = extra_flags;
29 
30 	args.opcode = FUSE_SETXATTR;
31 	args.nodeid = get_node_id(inode);
32 	args.in_numargs = 3;
33 	args.in_args[0].size = fm->fc->setxattr_ext ?
34 		sizeof(inarg) : FUSE_COMPAT_SETXATTR_IN_SIZE;
35 	args.in_args[0].value = &inarg;
36 	args.in_args[1].size = strlen(name) + 1;
37 	args.in_args[1].value = name;
38 	args.in_args[2].size = size;
39 	args.in_args[2].value = value;
40 	err = fuse_simple_request(fm, &args);
41 	if (err == -ENOSYS) {
42 		fm->fc->no_setxattr = 1;
43 		err = -EOPNOTSUPP;
44 	}
45 	if (!err)
46 		fuse_update_ctime(inode);
47 
48 	return err;
49 }
50 
fuse_getxattr(struct inode * inode,const char * name,void * value,size_t size)51 ssize_t fuse_getxattr(struct inode *inode, const char *name, void *value,
52 		      size_t size)
53 {
54 	struct fuse_mount *fm = get_fuse_mount(inode);
55 	FUSE_ARGS(args);
56 	struct fuse_getxattr_in inarg;
57 	struct fuse_getxattr_out outarg;
58 	ssize_t ret;
59 
60 	if (fm->fc->no_getxattr)
61 		return -EOPNOTSUPP;
62 
63 	memset(&inarg, 0, sizeof(inarg));
64 	inarg.size = size;
65 	args.opcode = FUSE_GETXATTR;
66 	args.nodeid = get_node_id(inode);
67 	args.in_numargs = 2;
68 	args.in_args[0].size = sizeof(inarg);
69 	args.in_args[0].value = &inarg;
70 	args.in_args[1].size = strlen(name) + 1;
71 	args.in_args[1].value = name;
72 	/* This is really two different operations rolled into one */
73 	args.out_numargs = 1;
74 	if (size) {
75 		args.out_argvar = true;
76 		args.out_args[0].size = size;
77 		args.out_args[0].value = value;
78 	} else {
79 		args.out_args[0].size = sizeof(outarg);
80 		args.out_args[0].value = &outarg;
81 	}
82 	ret = fuse_simple_request(fm, &args);
83 	if (!ret && !size)
84 		ret = min_t(ssize_t, outarg.size, XATTR_SIZE_MAX);
85 	if (ret == -ENOSYS) {
86 		fm->fc->no_getxattr = 1;
87 		ret = -EOPNOTSUPP;
88 	}
89 	return ret;
90 }
91 
fuse_verify_xattr_list(char * list,size_t size)92 static int fuse_verify_xattr_list(char *list, size_t size)
93 {
94 	size_t origsize = size;
95 
96 	while (size) {
97 		size_t thislen = strnlen(list, size);
98 
99 		if (!thislen || thislen == size)
100 			return -EIO;
101 
102 		size -= thislen + 1;
103 		list += thislen + 1;
104 	}
105 
106 	return origsize;
107 }
108 
fuse_listxattr(struct dentry * entry,char * list,size_t size)109 ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
110 {
111 	struct inode *inode = d_inode(entry);
112 	struct fuse_mount *fm = get_fuse_mount(inode);
113 	FUSE_ARGS(args);
114 	struct fuse_getxattr_in inarg;
115 	struct fuse_getxattr_out outarg;
116 	ssize_t ret;
117 
118 #ifdef CONFIG_FUSE_BPF
119 	struct fuse_err_ret fer;
120 
121 	fer = fuse_bpf_backing(inode, struct fuse_getxattr_io,
122 			       fuse_listxattr_initialize,
123 			       fuse_listxattr_backing, fuse_listxattr_finalize,
124 			       entry, list, size);
125 	if (fer.ret)
126 		return PTR_ERR(fer.result);
127 #endif
128 
129 	if (fuse_is_bad(inode))
130 		return -EIO;
131 
132 	if (!fuse_allow_current_process(fm->fc))
133 		return -EACCES;
134 
135 	if (fm->fc->no_listxattr)
136 		return -EOPNOTSUPP;
137 
138 	memset(&inarg, 0, sizeof(inarg));
139 	inarg.size = size;
140 	args.opcode = FUSE_LISTXATTR;
141 	args.nodeid = get_node_id(inode);
142 	args.in_numargs = 1;
143 	args.in_args[0].size = sizeof(inarg);
144 	args.in_args[0].value = &inarg;
145 	/* This is really two different operations rolled into one */
146 	args.out_numargs = 1;
147 	if (size) {
148 		args.out_argvar = true;
149 		args.out_args[0].size = size;
150 		args.out_args[0].value = list;
151 	} else {
152 		args.out_args[0].size = sizeof(outarg);
153 		args.out_args[0].value = &outarg;
154 	}
155 	ret = fuse_simple_request(fm, &args);
156 	if (!ret && !size)
157 		ret = min_t(ssize_t, outarg.size, XATTR_LIST_MAX);
158 	if (ret > 0 && size)
159 		ret = fuse_verify_xattr_list(list, ret);
160 	if (ret == -ENOSYS) {
161 		fm->fc->no_listxattr = 1;
162 		ret = -EOPNOTSUPP;
163 	}
164 	return ret;
165 }
166 
fuse_removexattr(struct inode * inode,const char * name)167 int fuse_removexattr(struct inode *inode, const char *name)
168 {
169 	struct fuse_mount *fm = get_fuse_mount(inode);
170 	FUSE_ARGS(args);
171 	int err;
172 
173 	if (fm->fc->no_removexattr)
174 		return -EOPNOTSUPP;
175 
176 	args.opcode = FUSE_REMOVEXATTR;
177 	args.nodeid = get_node_id(inode);
178 	args.in_numargs = 1;
179 	args.in_args[0].size = strlen(name) + 1;
180 	args.in_args[0].value = name;
181 	err = fuse_simple_request(fm, &args);
182 	if (err == -ENOSYS) {
183 		fm->fc->no_removexattr = 1;
184 		err = -EOPNOTSUPP;
185 	}
186 	if (!err)
187 		fuse_update_ctime(inode);
188 
189 	return err;
190 }
191 
fuse_xattr_get(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,void * value,size_t size)192 static int fuse_xattr_get(const struct xattr_handler *handler,
193 			 struct dentry *dentry, struct inode *inode,
194 			 const char *name, void *value, size_t size)
195 {
196 #ifdef CONFIG_FUSE_BPF
197 	struct fuse_err_ret fer;
198 
199 	fer = fuse_bpf_backing(inode, struct fuse_getxattr_io,
200 			       fuse_getxattr_initialize, fuse_getxattr_backing,
201 			       fuse_getxattr_finalize,
202 			       dentry, name, value, size);
203 	if (fer.ret)
204 		return PTR_ERR(fer.result);
205 #endif
206 
207 	if (fuse_is_bad(inode))
208 		return -EIO;
209 
210 	return fuse_getxattr(inode, name, value, size);
211 }
212 
fuse_xattr_set(const struct xattr_handler * handler,struct user_namespace * mnt_userns,struct dentry * dentry,struct inode * inode,const char * name,const void * value,size_t size,int flags)213 static int fuse_xattr_set(const struct xattr_handler *handler,
214 			  struct user_namespace *mnt_userns,
215 			  struct dentry *dentry, struct inode *inode,
216 			  const char *name, const void *value, size_t size,
217 			  int flags)
218 {
219 #ifdef CONFIG_FUSE_BPF
220 	struct fuse_err_ret fer;
221 
222 	if (value)
223 		fer = fuse_bpf_backing(inode, struct fuse_setxattr_in,
224 			       fuse_setxattr_initialize, fuse_setxattr_backing,
225 			       fuse_setxattr_finalize, dentry, name, value,
226 			       size, flags);
227 	else
228 		fer = fuse_bpf_backing(inode, struct fuse_dummy_io,
229 				       fuse_removexattr_initialize,
230 				       fuse_removexattr_backing,
231 				       fuse_removexattr_finalize,
232 				       dentry, name);
233 	if (fer.ret)
234 		return PTR_ERR(fer.result);
235 #endif
236 
237 	if (fuse_is_bad(inode))
238 		return -EIO;
239 
240 	if (!value)
241 		return fuse_removexattr(inode, name);
242 
243 	return fuse_setxattr(inode, name, value, size, flags, 0);
244 }
245 
no_xattr_list(struct dentry * dentry)246 static bool no_xattr_list(struct dentry *dentry)
247 {
248 	return false;
249 }
250 
no_xattr_get(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,void * value,size_t size)251 static int no_xattr_get(const struct xattr_handler *handler,
252 			struct dentry *dentry, struct inode *inode,
253 			const char *name, void *value, size_t size)
254 {
255 	return -EOPNOTSUPP;
256 }
257 
no_xattr_set(const struct xattr_handler * handler,struct user_namespace * mnt_userns,struct dentry * dentry,struct inode * nodee,const char * name,const void * value,size_t size,int flags)258 static int no_xattr_set(const struct xattr_handler *handler,
259 			struct user_namespace *mnt_userns,
260 			struct dentry *dentry, struct inode *nodee,
261 			const char *name, const void *value,
262 			size_t size, int flags)
263 {
264 	return -EOPNOTSUPP;
265 }
266 
267 static const struct xattr_handler fuse_xattr_handler = {
268 	.prefix = "",
269 	.get    = fuse_xattr_get,
270 	.set    = fuse_xattr_set,
271 };
272 
273 const struct xattr_handler *fuse_xattr_handlers[] = {
274 	&fuse_xattr_handler,
275 	NULL
276 };
277 
278 const struct xattr_handler *fuse_acl_xattr_handlers[] = {
279 	&posix_acl_access_xattr_handler,
280 	&posix_acl_default_xattr_handler,
281 	&fuse_xattr_handler,
282 	NULL
283 };
284 
285 static const struct xattr_handler fuse_no_acl_access_xattr_handler = {
286 	.name  = XATTR_NAME_POSIX_ACL_ACCESS,
287 	.flags = ACL_TYPE_ACCESS,
288 	.list  = no_xattr_list,
289 	.get   = no_xattr_get,
290 	.set   = no_xattr_set,
291 };
292 
293 static const struct xattr_handler fuse_no_acl_default_xattr_handler = {
294 	.name  = XATTR_NAME_POSIX_ACL_DEFAULT,
295 	.flags = ACL_TYPE_ACCESS,
296 	.list  = no_xattr_list,
297 	.get   = no_xattr_get,
298 	.set   = no_xattr_set,
299 };
300 
301 const struct xattr_handler *fuse_no_acl_xattr_handlers[] = {
302 	&fuse_no_acl_access_xattr_handler,
303 	&fuse_no_acl_default_xattr_handler,
304 	&fuse_xattr_handler,
305 	NULL
306 };
307