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 | } |
| 45 | if (!err) { |
| 46 | fuse_invalidate_attr(inode); |
| 47 | fuse_update_ctime(inode); |
| 48 | } |
| 49 | return err; |
| 50 | } |
| 51 | |
Seth Forshee | 60bcc88 | 2016-08-29 08:46:37 -0500 | [diff] [blame] | 52 | ssize_t fuse_getxattr(struct inode *inode, const char *name, void *value, |
| 53 | size_t size) |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 54 | { |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 55 | struct fuse_mount *fm = get_fuse_mount(inode); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 56 | FUSE_ARGS(args); |
| 57 | struct fuse_getxattr_in inarg; |
| 58 | struct fuse_getxattr_out outarg; |
| 59 | ssize_t ret; |
| 60 | |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 61 | if (fm->fc->no_getxattr) |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 62 | return -EOPNOTSUPP; |
| 63 | |
| 64 | memset(&inarg, 0, sizeof(inarg)); |
| 65 | inarg.size = size; |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 66 | args.opcode = FUSE_GETXATTR; |
| 67 | args.nodeid = get_node_id(inode); |
| 68 | args.in_numargs = 2; |
| 69 | args.in_args[0].size = sizeof(inarg); |
| 70 | args.in_args[0].value = &inarg; |
| 71 | args.in_args[1].size = strlen(name) + 1; |
| 72 | args.in_args[1].value = name; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 73 | /* This is really two different operations rolled into one */ |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 74 | args.out_numargs = 1; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 75 | if (size) { |
Miklos Szeredi | 1f4e9d0 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 76 | args.out_argvar = true; |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 77 | args.out_args[0].size = size; |
| 78 | args.out_args[0].value = value; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 79 | } else { |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 80 | args.out_args[0].size = sizeof(outarg); |
| 81 | args.out_args[0].value = &outarg; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 82 | } |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 83 | ret = fuse_simple_request(fm, &args); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 84 | if (!ret && !size) |
Miklos Szeredi | 63401cc | 2016-10-03 11:06:05 +0200 | [diff] [blame] | 85 | ret = min_t(ssize_t, outarg.size, XATTR_SIZE_MAX); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 86 | if (ret == -ENOSYS) { |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 87 | fm->fc->no_getxattr = 1; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 88 | ret = -EOPNOTSUPP; |
| 89 | } |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | static int fuse_verify_xattr_list(char *list, size_t size) |
| 94 | { |
| 95 | size_t origsize = size; |
| 96 | |
| 97 | while (size) { |
| 98 | size_t thislen = strnlen(list, size); |
| 99 | |
| 100 | if (!thislen || thislen == size) |
| 101 | return -EIO; |
| 102 | |
| 103 | size -= thislen + 1; |
| 104 | list += thislen + 1; |
| 105 | } |
| 106 | |
| 107 | return origsize; |
| 108 | } |
| 109 | |
| 110 | ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size) |
| 111 | { |
| 112 | struct inode *inode = d_inode(entry); |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 113 | struct fuse_mount *fm = get_fuse_mount(inode); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 114 | FUSE_ARGS(args); |
| 115 | struct fuse_getxattr_in inarg; |
| 116 | struct fuse_getxattr_out outarg; |
| 117 | ssize_t ret; |
| 118 | |
Miklos Szeredi | 5d069db | 2020-12-10 15:33:14 +0100 | [diff] [blame] | 119 | if (fuse_is_bad(inode)) |
| 120 | return -EIO; |
| 121 | |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 122 | if (!fuse_allow_current_process(fm->fc)) |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 123 | return -EACCES; |
| 124 | |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 125 | if (fm->fc->no_listxattr) |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 126 | return -EOPNOTSUPP; |
| 127 | |
| 128 | memset(&inarg, 0, sizeof(inarg)); |
| 129 | inarg.size = size; |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 130 | args.opcode = FUSE_LISTXATTR; |
| 131 | args.nodeid = get_node_id(inode); |
| 132 | args.in_numargs = 1; |
| 133 | args.in_args[0].size = sizeof(inarg); |
| 134 | args.in_args[0].value = &inarg; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 135 | /* This is really two different operations rolled into one */ |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 136 | args.out_numargs = 1; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 137 | if (size) { |
Miklos Szeredi | 1f4e9d0 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 138 | args.out_argvar = true; |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 139 | args.out_args[0].size = size; |
| 140 | args.out_args[0].value = list; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 141 | } else { |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 142 | args.out_args[0].size = sizeof(outarg); |
| 143 | args.out_args[0].value = &outarg; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 144 | } |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 145 | ret = fuse_simple_request(fm, &args); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 146 | if (!ret && !size) |
Miklos Szeredi | 63401cc | 2016-10-03 11:06:05 +0200 | [diff] [blame] | 147 | ret = min_t(ssize_t, outarg.size, XATTR_LIST_MAX); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 148 | if (ret > 0 && size) |
| 149 | ret = fuse_verify_xattr_list(list, ret); |
| 150 | if (ret == -ENOSYS) { |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 151 | fm->fc->no_listxattr = 1; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 152 | ret = -EOPNOTSUPP; |
| 153 | } |
| 154 | return ret; |
| 155 | } |
| 156 | |
Seth Forshee | 60bcc88 | 2016-08-29 08:46:37 -0500 | [diff] [blame] | 157 | int fuse_removexattr(struct inode *inode, const char *name) |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 158 | { |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 159 | struct fuse_mount *fm = get_fuse_mount(inode); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 160 | FUSE_ARGS(args); |
| 161 | int err; |
| 162 | |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 163 | if (fm->fc->no_removexattr) |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 164 | return -EOPNOTSUPP; |
| 165 | |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 166 | args.opcode = FUSE_REMOVEXATTR; |
| 167 | args.nodeid = get_node_id(inode); |
| 168 | args.in_numargs = 1; |
| 169 | args.in_args[0].size = strlen(name) + 1; |
| 170 | args.in_args[0].value = name; |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 171 | err = fuse_simple_request(fm, &args); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 172 | if (err == -ENOSYS) { |
Max Reitz | fcee216 | 2020-05-06 17:44:12 +0200 | [diff] [blame] | 173 | fm->fc->no_removexattr = 1; |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 174 | err = -EOPNOTSUPP; |
| 175 | } |
| 176 | if (!err) { |
| 177 | fuse_invalidate_attr(inode); |
| 178 | fuse_update_ctime(inode); |
| 179 | } |
| 180 | return err; |
| 181 | } |
| 182 | |
| 183 | static int fuse_xattr_get(const struct xattr_handler *handler, |
| 184 | struct dentry *dentry, struct inode *inode, |
| 185 | const char *name, void *value, size_t size) |
| 186 | { |
Miklos Szeredi | 5d069db | 2020-12-10 15:33:14 +0100 | [diff] [blame] | 187 | if (fuse_is_bad(inode)) |
| 188 | return -EIO; |
| 189 | |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 190 | return fuse_getxattr(inode, name, value, size); |
| 191 | } |
| 192 | |
| 193 | static int fuse_xattr_set(const struct xattr_handler *handler, |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 194 | struct user_namespace *mnt_userns, |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 195 | struct dentry *dentry, struct inode *inode, |
| 196 | const char *name, const void *value, size_t size, |
| 197 | int flags) |
| 198 | { |
Miklos Szeredi | 5d069db | 2020-12-10 15:33:14 +0100 | [diff] [blame] | 199 | if (fuse_is_bad(inode)) |
| 200 | return -EIO; |
| 201 | |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 202 | if (!value) |
| 203 | return fuse_removexattr(inode, name); |
| 204 | |
Vivek Goyal | 52a4c95 | 2021-03-25 11:18:22 -0400 | [diff] [blame] | 205 | return fuse_setxattr(inode, name, value, size, flags, 0); |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 206 | } |
| 207 | |
Eric W. Biederman | e45b254 | 2018-05-04 11:47:28 -0500 | [diff] [blame] | 208 | static bool no_xattr_list(struct dentry *dentry) |
| 209 | { |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | static int no_xattr_get(const struct xattr_handler *handler, |
| 214 | struct dentry *dentry, struct inode *inode, |
| 215 | const char *name, void *value, size_t size) |
| 216 | { |
| 217 | return -EOPNOTSUPP; |
| 218 | } |
| 219 | |
| 220 | static int no_xattr_set(const struct xattr_handler *handler, |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 221 | struct user_namespace *mnt_userns, |
Eric W. Biederman | e45b254 | 2018-05-04 11:47:28 -0500 | [diff] [blame] | 222 | struct dentry *dentry, struct inode *nodee, |
| 223 | const char *name, const void *value, |
| 224 | size_t size, int flags) |
| 225 | { |
| 226 | return -EOPNOTSUPP; |
| 227 | } |
| 228 | |
Seth Forshee | 703c736 | 2016-08-29 08:46:36 -0500 | [diff] [blame] | 229 | static const struct xattr_handler fuse_xattr_handler = { |
| 230 | .prefix = "", |
| 231 | .get = fuse_xattr_get, |
| 232 | .set = fuse_xattr_set, |
| 233 | }; |
| 234 | |
| 235 | const struct xattr_handler *fuse_xattr_handlers[] = { |
| 236 | &fuse_xattr_handler, |
| 237 | NULL |
| 238 | }; |
Seth Forshee | 60bcc88 | 2016-08-29 08:46:37 -0500 | [diff] [blame] | 239 | |
| 240 | const struct xattr_handler *fuse_acl_xattr_handlers[] = { |
| 241 | &posix_acl_access_xattr_handler, |
| 242 | &posix_acl_default_xattr_handler, |
| 243 | &fuse_xattr_handler, |
| 244 | NULL |
| 245 | }; |
Eric W. Biederman | e45b254 | 2018-05-04 11:47:28 -0500 | [diff] [blame] | 246 | |
| 247 | static const struct xattr_handler fuse_no_acl_access_xattr_handler = { |
| 248 | .name = XATTR_NAME_POSIX_ACL_ACCESS, |
| 249 | .flags = ACL_TYPE_ACCESS, |
| 250 | .list = no_xattr_list, |
| 251 | .get = no_xattr_get, |
| 252 | .set = no_xattr_set, |
| 253 | }; |
| 254 | |
| 255 | static const struct xattr_handler fuse_no_acl_default_xattr_handler = { |
| 256 | .name = XATTR_NAME_POSIX_ACL_DEFAULT, |
| 257 | .flags = ACL_TYPE_ACCESS, |
| 258 | .list = no_xattr_list, |
| 259 | .get = no_xattr_get, |
| 260 | .set = no_xattr_set, |
| 261 | }; |
| 262 | |
| 263 | const struct xattr_handler *fuse_no_acl_xattr_handlers[] = { |
| 264 | &fuse_no_acl_access_xattr_handler, |
| 265 | &fuse_no_acl_default_xattr_handler, |
| 266 | &fuse_xattr_handler, |
| 267 | NULL |
| 268 | }; |