blob: 0d3e7177fce0a70dc99db3e1c02ac8bcf0dfbd8e [file] [log] [blame]
Seth Forshee703c7362016-08-29 08:46:36 -05001/*
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>
Seth Forshee60bcc882016-08-29 08:46:37 -050012#include <linux/posix_acl_xattr.h>
Seth Forshee703c7362016-08-29 08:46:36 -050013
Seth Forshee60bcc882016-08-29 08:46:37 -050014int fuse_setxattr(struct inode *inode, const char *name, const void *value,
Vivek Goyal52a4c952021-03-25 11:18:22 -040015 size_t size, int flags, unsigned int extra_flags)
Seth Forshee703c7362016-08-29 08:46:36 -050016{
Max Reitzfcee2162020-05-06 17:44:12 +020017 struct fuse_mount *fm = get_fuse_mount(inode);
Seth Forshee703c7362016-08-29 08:46:36 -050018 FUSE_ARGS(args);
19 struct fuse_setxattr_in inarg;
20 int err;
21
Max Reitzfcee2162020-05-06 17:44:12 +020022 if (fm->fc->no_setxattr)
Seth Forshee703c7362016-08-29 08:46:36 -050023 return -EOPNOTSUPP;
24
25 memset(&inarg, 0, sizeof(inarg));
26 inarg.size = size;
27 inarg.flags = flags;
Vivek Goyal52a4c952021-03-25 11:18:22 -040028 inarg.setxattr_flags = extra_flags;
29
Miklos Szeredid5b48542019-09-10 15:04:08 +020030 args.opcode = FUSE_SETXATTR;
31 args.nodeid = get_node_id(inode);
32 args.in_numargs = 3;
Vivek Goyal52a4c952021-03-25 11:18:22 -040033 args.in_args[0].size = fm->fc->setxattr_ext ?
34 sizeof(inarg) : FUSE_COMPAT_SETXATTR_IN_SIZE;
Miklos Szeredid5b48542019-09-10 15:04:08 +020035 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;
Max Reitzfcee2162020-05-06 17:44:12 +020040 err = fuse_simple_request(fm, &args);
Seth Forshee703c7362016-08-29 08:46:36 -050041 if (err == -ENOSYS) {
Max Reitzfcee2162020-05-06 17:44:12 +020042 fm->fc->no_setxattr = 1;
Seth Forshee703c7362016-08-29 08:46:36 -050043 err = -EOPNOTSUPP;
44 }
Miklos Szeredi371e8fd2021-10-22 17:03:01 +020045 if (!err)
Seth Forshee703c7362016-08-29 08:46:36 -050046 fuse_update_ctime(inode);
Miklos Szeredi371e8fd2021-10-22 17:03:01 +020047
Seth Forshee703c7362016-08-29 08:46:36 -050048 return err;
49}
50
Seth Forshee60bcc882016-08-29 08:46:37 -050051ssize_t fuse_getxattr(struct inode *inode, const char *name, void *value,
52 size_t size)
Seth Forshee703c7362016-08-29 08:46:36 -050053{
Max Reitzfcee2162020-05-06 17:44:12 +020054 struct fuse_mount *fm = get_fuse_mount(inode);
Seth Forshee703c7362016-08-29 08:46:36 -050055 FUSE_ARGS(args);
56 struct fuse_getxattr_in inarg;
57 struct fuse_getxattr_out outarg;
58 ssize_t ret;
59
Max Reitzfcee2162020-05-06 17:44:12 +020060 if (fm->fc->no_getxattr)
Seth Forshee703c7362016-08-29 08:46:36 -050061 return -EOPNOTSUPP;
62
63 memset(&inarg, 0, sizeof(inarg));
64 inarg.size = size;
Miklos Szeredid5b48542019-09-10 15:04:08 +020065 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;
Seth Forshee703c7362016-08-29 08:46:36 -050072 /* This is really two different operations rolled into one */
Miklos Szeredid5b48542019-09-10 15:04:08 +020073 args.out_numargs = 1;
Seth Forshee703c7362016-08-29 08:46:36 -050074 if (size) {
Miklos Szeredi1f4e9d02019-09-10 15:04:08 +020075 args.out_argvar = true;
Miklos Szeredid5b48542019-09-10 15:04:08 +020076 args.out_args[0].size = size;
77 args.out_args[0].value = value;
Seth Forshee703c7362016-08-29 08:46:36 -050078 } else {
Miklos Szeredid5b48542019-09-10 15:04:08 +020079 args.out_args[0].size = sizeof(outarg);
80 args.out_args[0].value = &outarg;
Seth Forshee703c7362016-08-29 08:46:36 -050081 }
Max Reitzfcee2162020-05-06 17:44:12 +020082 ret = fuse_simple_request(fm, &args);
Seth Forshee703c7362016-08-29 08:46:36 -050083 if (!ret && !size)
Miklos Szeredi63401cc2016-10-03 11:06:05 +020084 ret = min_t(ssize_t, outarg.size, XATTR_SIZE_MAX);
Seth Forshee703c7362016-08-29 08:46:36 -050085 if (ret == -ENOSYS) {
Max Reitzfcee2162020-05-06 17:44:12 +020086 fm->fc->no_getxattr = 1;
Seth Forshee703c7362016-08-29 08:46:36 -050087 ret = -EOPNOTSUPP;
88 }
89 return ret;
90}
91
92static 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
109ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
110{
111 struct inode *inode = d_inode(entry);
Max Reitzfcee2162020-05-06 17:44:12 +0200112 struct fuse_mount *fm = get_fuse_mount(inode);
Seth Forshee703c7362016-08-29 08:46:36 -0500113 FUSE_ARGS(args);
114 struct fuse_getxattr_in inarg;
115 struct fuse_getxattr_out outarg;
116 ssize_t ret;
117
Miklos Szeredi5d069db2020-12-10 15:33:14 +0100118 if (fuse_is_bad(inode))
119 return -EIO;
120
Max Reitzfcee2162020-05-06 17:44:12 +0200121 if (!fuse_allow_current_process(fm->fc))
Seth Forshee703c7362016-08-29 08:46:36 -0500122 return -EACCES;
123
Max Reitzfcee2162020-05-06 17:44:12 +0200124 if (fm->fc->no_listxattr)
Seth Forshee703c7362016-08-29 08:46:36 -0500125 return -EOPNOTSUPP;
126
127 memset(&inarg, 0, sizeof(inarg));
128 inarg.size = size;
Miklos Szeredid5b48542019-09-10 15:04:08 +0200129 args.opcode = FUSE_LISTXATTR;
130 args.nodeid = get_node_id(inode);
131 args.in_numargs = 1;
132 args.in_args[0].size = sizeof(inarg);
133 args.in_args[0].value = &inarg;
Seth Forshee703c7362016-08-29 08:46:36 -0500134 /* This is really two different operations rolled into one */
Miklos Szeredid5b48542019-09-10 15:04:08 +0200135 args.out_numargs = 1;
Seth Forshee703c7362016-08-29 08:46:36 -0500136 if (size) {
Miklos Szeredi1f4e9d02019-09-10 15:04:08 +0200137 args.out_argvar = true;
Miklos Szeredid5b48542019-09-10 15:04:08 +0200138 args.out_args[0].size = size;
139 args.out_args[0].value = list;
Seth Forshee703c7362016-08-29 08:46:36 -0500140 } else {
Miklos Szeredid5b48542019-09-10 15:04:08 +0200141 args.out_args[0].size = sizeof(outarg);
142 args.out_args[0].value = &outarg;
Seth Forshee703c7362016-08-29 08:46:36 -0500143 }
Max Reitzfcee2162020-05-06 17:44:12 +0200144 ret = fuse_simple_request(fm, &args);
Seth Forshee703c7362016-08-29 08:46:36 -0500145 if (!ret && !size)
Miklos Szeredi63401cc2016-10-03 11:06:05 +0200146 ret = min_t(ssize_t, outarg.size, XATTR_LIST_MAX);
Seth Forshee703c7362016-08-29 08:46:36 -0500147 if (ret > 0 && size)
148 ret = fuse_verify_xattr_list(list, ret);
149 if (ret == -ENOSYS) {
Max Reitzfcee2162020-05-06 17:44:12 +0200150 fm->fc->no_listxattr = 1;
Seth Forshee703c7362016-08-29 08:46:36 -0500151 ret = -EOPNOTSUPP;
152 }
153 return ret;
154}
155
Seth Forshee60bcc882016-08-29 08:46:37 -0500156int fuse_removexattr(struct inode *inode, const char *name)
Seth Forshee703c7362016-08-29 08:46:36 -0500157{
Max Reitzfcee2162020-05-06 17:44:12 +0200158 struct fuse_mount *fm = get_fuse_mount(inode);
Seth Forshee703c7362016-08-29 08:46:36 -0500159 FUSE_ARGS(args);
160 int err;
161
Max Reitzfcee2162020-05-06 17:44:12 +0200162 if (fm->fc->no_removexattr)
Seth Forshee703c7362016-08-29 08:46:36 -0500163 return -EOPNOTSUPP;
164
Miklos Szeredid5b48542019-09-10 15:04:08 +0200165 args.opcode = FUSE_REMOVEXATTR;
166 args.nodeid = get_node_id(inode);
167 args.in_numargs = 1;
168 args.in_args[0].size = strlen(name) + 1;
169 args.in_args[0].value = name;
Max Reitzfcee2162020-05-06 17:44:12 +0200170 err = fuse_simple_request(fm, &args);
Seth Forshee703c7362016-08-29 08:46:36 -0500171 if (err == -ENOSYS) {
Max Reitzfcee2162020-05-06 17:44:12 +0200172 fm->fc->no_removexattr = 1;
Seth Forshee703c7362016-08-29 08:46:36 -0500173 err = -EOPNOTSUPP;
174 }
Miklos Szeredi371e8fd2021-10-22 17:03:01 +0200175 if (!err)
Seth Forshee703c7362016-08-29 08:46:36 -0500176 fuse_update_ctime(inode);
Miklos Szeredi371e8fd2021-10-22 17:03:01 +0200177
Seth Forshee703c7362016-08-29 08:46:36 -0500178 return err;
179}
180
181static int fuse_xattr_get(const struct xattr_handler *handler,
182 struct dentry *dentry, struct inode *inode,
183 const char *name, void *value, size_t size)
184{
Miklos Szeredi5d069db2020-12-10 15:33:14 +0100185 if (fuse_is_bad(inode))
186 return -EIO;
187
Seth Forshee703c7362016-08-29 08:46:36 -0500188 return fuse_getxattr(inode, name, value, size);
189}
190
191static int fuse_xattr_set(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +0100192 struct user_namespace *mnt_userns,
Seth Forshee703c7362016-08-29 08:46:36 -0500193 struct dentry *dentry, struct inode *inode,
194 const char *name, const void *value, size_t size,
195 int flags)
196{
Miklos Szeredi5d069db2020-12-10 15:33:14 +0100197 if (fuse_is_bad(inode))
198 return -EIO;
199
Seth Forshee703c7362016-08-29 08:46:36 -0500200 if (!value)
201 return fuse_removexattr(inode, name);
202
Vivek Goyal52a4c952021-03-25 11:18:22 -0400203 return fuse_setxattr(inode, name, value, size, flags, 0);
Seth Forshee703c7362016-08-29 08:46:36 -0500204}
205
Eric W. Biedermane45b2542018-05-04 11:47:28 -0500206static bool no_xattr_list(struct dentry *dentry)
207{
208 return false;
209}
210
211static int no_xattr_get(const struct xattr_handler *handler,
212 struct dentry *dentry, struct inode *inode,
213 const char *name, void *value, size_t size)
214{
215 return -EOPNOTSUPP;
216}
217
218static int no_xattr_set(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +0100219 struct user_namespace *mnt_userns,
Eric W. Biedermane45b2542018-05-04 11:47:28 -0500220 struct dentry *dentry, struct inode *nodee,
221 const char *name, const void *value,
222 size_t size, int flags)
223{
224 return -EOPNOTSUPP;
225}
226
Seth Forshee703c7362016-08-29 08:46:36 -0500227static const struct xattr_handler fuse_xattr_handler = {
228 .prefix = "",
229 .get = fuse_xattr_get,
230 .set = fuse_xattr_set,
231};
232
233const struct xattr_handler *fuse_xattr_handlers[] = {
234 &fuse_xattr_handler,
235 NULL
236};
Seth Forshee60bcc882016-08-29 08:46:37 -0500237
238const struct xattr_handler *fuse_acl_xattr_handlers[] = {
239 &posix_acl_access_xattr_handler,
240 &posix_acl_default_xattr_handler,
241 &fuse_xattr_handler,
242 NULL
243};
Eric W. Biedermane45b2542018-05-04 11:47:28 -0500244
245static const struct xattr_handler fuse_no_acl_access_xattr_handler = {
246 .name = XATTR_NAME_POSIX_ACL_ACCESS,
247 .flags = ACL_TYPE_ACCESS,
248 .list = no_xattr_list,
249 .get = no_xattr_get,
250 .set = no_xattr_set,
251};
252
253static const struct xattr_handler fuse_no_acl_default_xattr_handler = {
254 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
255 .flags = ACL_TYPE_ACCESS,
256 .list = no_xattr_list,
257 .get = no_xattr_get,
258 .set = no_xattr_set,
259};
260
261const struct xattr_handler *fuse_no_acl_xattr_handlers[] = {
262 &fuse_no_acl_access_xattr_handler,
263 &fuse_no_acl_default_xattr_handler,
264 &fuse_xattr_handler,
265 NULL
266};