Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 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> |
Seth Forshee | 60bcc88 | 2016-08-29 08:46:37 -0500 | [diff] [blame] | 12 | #include <linux/posix_acl_xattr.h> |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 13 | |
Seth Forshee | 60bcc88 | 2016-08-29 08:46:37 -0500 | [diff] [blame] | 14 | int fuse_setxattr(struct inode *inode, const char *name, const void *value, |
Vivek Goyal | 52a4c95 | 2021-03-25 11:18:22 -0400 | [diff] [blame] | 15 | size_t size, int flags, unsigned int extra_flags) |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 16 | { |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 17 | struct fuse_mount *fm = get_fuse_mount(inode); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 18 | FUSE_ARGS(args); |
| 19 | struct fuse_setxattr_in inarg; |
| 20 | int err; |
| 21 | |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 22 | if (fm->fc->no_setxattr) |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 23 | return -EOPNOTSUPP; |
| 24 | |
| 25 | memset(&inarg, 0, sizeof(inarg)); |
| 26 | inarg.size = size; |
| 27 | inarg.flags = flags; |
Vivek Goyal | 52a4c95 | 2021-03-25 11:18:22 -0400 | [diff] [blame] | 28 | inarg.setxattr_flags = extra_flags; |
| 29 | |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 30 | args.opcode = FUSE_SETXATTR; |
| 31 | args.nodeid = get_node_id(inode); |
| 32 | args.in_numargs = 3; |
Vivek Goyal | 52a4c95 | 2021-03-25 11:18:22 -0400 | [diff] [blame] | 33 | args.in_args[0].size = fm->fc->setxattr_ext ? |
| 34 | sizeof(inarg) : FUSE_COMPAT_SETXATTR_IN_SIZE; |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 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; |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 40 | err = fuse_simple_request(fm, &args); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 41 | if (err == -ENOSYS) { |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 42 | fm->fc->no_setxattr = 1; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 43 | err = -EOPNOTSUPP; |
| 44 | } |
Miklos Szeredi | 371e8fd | 2021-10-22 17:03:01 +0200 | [diff] [blame] | 45 | if (!err) |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 46 | fuse_update_ctime(inode); |
Miklos Szeredi | 371e8fd | 2021-10-22 17:03:01 +0200 | [diff] [blame] | 47 | |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 48 | return err; |
| 49 | } |
| 50 | |
Seth Forshee | 60bcc88 | 2016-08-29 08:46:37 -0500 | [diff] [blame] | 51 | ssize_t fuse_getxattr(struct inode *inode, const char *name, void *value, |
| 52 | size_t size) |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 53 | { |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 54 | struct fuse_mount *fm = get_fuse_mount(inode); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 55 | FUSE_ARGS(args); |
| 56 | struct fuse_getxattr_in inarg; |
| 57 | struct fuse_getxattr_out outarg; |
| 58 | ssize_t ret; |
| 59 | |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 60 | if (fm->fc->no_getxattr) |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 61 | return -EOPNOTSUPP; |
| 62 | |
| 63 | memset(&inarg, 0, sizeof(inarg)); |
| 64 | inarg.size = size; |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 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; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 72 | /* This is really two different operations rolled into one */ |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 73 | args.out_numargs = 1; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 74 | if (size) { |
Miklos Szeredi | 1f4e9d0 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 75 | args.out_argvar = true; |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 76 | args.out_args[0].size = size; |
| 77 | args.out_args[0].value = value; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 78 | } else { |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 79 | args.out_args[0].size = sizeof(outarg); |
| 80 | args.out_args[0].value = &outarg; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 81 | } |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 82 | ret = fuse_simple_request(fm, &args); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 83 | if (!ret && !size) |
Miklos Szeredi | 63401cc | 2016-10-03 11:06:05 +0200 | [diff] [blame] | 84 | ret = min_t(ssize_t, outarg.size, XATTR_SIZE_MAX); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 85 | if (ret == -ENOSYS) { |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 86 | fm->fc->no_getxattr = 1; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 87 | ret = -EOPNOTSUPP; |
| 88 | } |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 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 | |
| 109 | ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size) |
| 110 | { |
| 111 | struct inode *inode = d_inode(entry); |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 112 | struct fuse_mount *fm = get_fuse_mount(inode); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 113 | FUSE_ARGS(args); |
| 114 | struct fuse_getxattr_in inarg; |
| 115 | struct fuse_getxattr_out outarg; |
| 116 | ssize_t ret; |
| 117 | |
Miklos Szeredi | 5d069db | 2020-12-10 15:33:14 +0100 | [diff] [blame] | 118 | if (fuse_is_bad(inode)) |
| 119 | return -EIO; |
| 120 | |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 121 | if (!fuse_allow_current_process(fm->fc)) |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 122 | return -EACCES; |
| 123 | |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 124 | if (fm->fc->no_listxattr) |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 125 | return -EOPNOTSUPP; |
| 126 | |
| 127 | memset(&inarg, 0, sizeof(inarg)); |
| 128 | inarg.size = size; |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 129 | 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 Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 134 | /* This is really two different operations rolled into one */ |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 135 | args.out_numargs = 1; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 136 | if (size) { |
Miklos Szeredi | 1f4e9d0 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 137 | args.out_argvar = true; |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 138 | args.out_args[0].size = size; |
| 139 | args.out_args[0].value = list; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 140 | } else { |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 141 | args.out_args[0].size = sizeof(outarg); |
| 142 | args.out_args[0].value = &outarg; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 143 | } |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 144 | ret = fuse_simple_request(fm, &args); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 145 | if (!ret && !size) |
Miklos Szeredi | 63401cc | 2016-10-03 11:06:05 +0200 | [diff] [blame] | 146 | ret = min_t(ssize_t, outarg.size, XATTR_LIST_MAX); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 147 | if (ret > 0 && size) |
| 148 | ret = fuse_verify_xattr_list(list, ret); |
| 149 | if (ret == -ENOSYS) { |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 150 | fm->fc->no_listxattr = 1; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 151 | ret = -EOPNOTSUPP; |
| 152 | } |
| 153 | return ret; |
| 154 | } |
| 155 | |
Seth Forshee | 60bcc88 | 2016-08-29 08:46:37 -0500 | [diff] [blame] | 156 | int fuse_removexattr(struct inode *inode, const char *name) |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 157 | { |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 158 | struct fuse_mount *fm = get_fuse_mount(inode); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 159 | FUSE_ARGS(args); |
| 160 | int err; |
| 161 | |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 162 | if (fm->fc->no_removexattr) |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 163 | return -EOPNOTSUPP; |
| 164 | |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 165 | 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 Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 170 | err = fuse_simple_request(fm, &args); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 171 | if (err == -ENOSYS) { |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 172 | fm->fc->no_removexattr = 1; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 173 | err = -EOPNOTSUPP; |
| 174 | } |
Miklos Szeredi | 371e8fd | 2021-10-22 17:03:01 +0200 | [diff] [blame] | 175 | if (!err) |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 176 | fuse_update_ctime(inode); |
Miklos Szeredi | 371e8fd | 2021-10-22 17:03:01 +0200 | [diff] [blame] | 177 | |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 178 | return err; |
| 179 | } |
| 180 | |
| 181 | static 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 Szeredi | 5d069db | 2020-12-10 15:33:14 +0100 | [diff] [blame] | 185 | if (fuse_is_bad(inode)) |
| 186 | return -EIO; |
| 187 | |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 188 | return fuse_getxattr(inode, name, value, size); |
| 189 | } |
| 190 | |
| 191 | static int fuse_xattr_set(const struct xattr_handler *handler, |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 192 | struct user_namespace *mnt_userns, |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 193 | struct dentry *dentry, struct inode *inode, |
| 194 | const char *name, const void *value, size_t size, |
| 195 | int flags) |
| 196 | { |
Miklos Szeredi | 5d069db | 2020-12-10 15:33:14 +0100 | [diff] [blame] | 197 | if (fuse_is_bad(inode)) |
| 198 | return -EIO; |
| 199 | |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 200 | if (!value) |
| 201 | return fuse_removexattr(inode, name); |
| 202 | |
Vivek Goyal | 52a4c95 | 2021-03-25 11:18:22 -0400 | [diff] [blame] | 203 | return fuse_setxattr(inode, name, value, size, flags, 0); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 204 | } |
| 205 | |
Eric W. Biederman | e45b254 | 2018-05-04 11:47:28 -0500 | [diff] [blame] | 206 | static bool no_xattr_list(struct dentry *dentry) |
| 207 | { |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | static 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 | |
| 218 | static int no_xattr_set(const struct xattr_handler *handler, |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 219 | struct user_namespace *mnt_userns, |
Eric W. Biederman | e45b254 | 2018-05-04 11:47:28 -0500 | [diff] [blame] | 220 | 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 Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 227 | static const struct xattr_handler fuse_xattr_handler = { |
| 228 | .prefix = "", |
| 229 | .get = fuse_xattr_get, |
| 230 | .set = fuse_xattr_set, |
| 231 | }; |
| 232 | |
| 233 | const struct xattr_handler *fuse_xattr_handlers[] = { |
| 234 | &fuse_xattr_handler, |
| 235 | NULL |
| 236 | }; |
Seth Forshee | 60bcc88 | 2016-08-29 08:46:37 -0500 | [diff] [blame] | 237 | |
| 238 | const 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. Biederman | e45b254 | 2018-05-04 11:47:28 -0500 | [diff] [blame] | 244 | |
| 245 | static 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 | |
| 253 | static 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 | |
| 261 | const 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 | }; |