1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2007 Red Hat. All rights reserved. 4 */ 5 6 #include <linux/init.h> 7 #include <linux/fs.h> 8 #include <linux/slab.h> 9 #include <linux/rwsem.h> 10 #include <linux/xattr.h> 11 #include <linux/security.h> 12 #include <linux/posix_acl_xattr.h> 13 #include <linux/iversion.h> 14 #include <linux/sched/mm.h> 15 #include "ctree.h" 16 #include "fs.h" 17 #include "messages.h" 18 #include "btrfs_inode.h" 19 #include "transaction.h" 20 #include "xattr.h" 21 #include "disk-io.h" 22 #include "props.h" 23 #include "locking.h" 24 25 int btrfs_getxattr(struct inode *inode, const char *name, 26 void *buffer, size_t size) 27 { 28 struct btrfs_dir_item *di; 29 struct btrfs_root *root = BTRFS_I(inode)->root; 30 struct btrfs_path *path; 31 struct extent_buffer *leaf; 32 int ret = 0; 33 unsigned long data_ptr; 34 35 path = btrfs_alloc_path(); 36 if (!path) 37 return -ENOMEM; 38 39 /* lookup the xattr by name */ 40 di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(BTRFS_I(inode)), 41 name, strlen(name), 0); 42 if (!di) { 43 ret = -ENODATA; 44 goto out; 45 } else if (IS_ERR(di)) { 46 ret = PTR_ERR(di); 47 goto out; 48 } 49 50 leaf = path->nodes[0]; 51 /* if size is 0, that means we want the size of the attr */ 52 if (!size) { 53 ret = btrfs_dir_data_len(leaf, di); 54 goto out; 55 } 56 57 /* now get the data out of our dir_item */ 58 if (btrfs_dir_data_len(leaf, di) > size) { 59 ret = -ERANGE; 60 goto out; 61 } 62 63 /* 64 * The way things are packed into the leaf is like this 65 * |struct btrfs_dir_item|name|data| 66 * where name is the xattr name, so security.foo, and data is the 67 * content of the xattr. data_ptr points to the location in memory 68 * where the data starts in the in memory leaf 69 */ 70 data_ptr = (unsigned long)((char *)(di + 1) + 71 btrfs_dir_name_len(leaf, di)); 72 read_extent_buffer(leaf, buffer, data_ptr, 73 btrfs_dir_data_len(leaf, di)); 74 ret = btrfs_dir_data_len(leaf, di); 75 76 out: 77 btrfs_free_path(path); 78 return ret; 79 } 80 81 int btrfs_setxattr(struct btrfs_trans_handle *trans, struct inode *inode, 82 const char *name, const void *value, size_t size, int flags) 83 { 84 struct btrfs_dir_item *di = NULL; 85 struct btrfs_root *root = BTRFS_I(inode)->root; 86 struct btrfs_fs_info *fs_info = root->fs_info; 87 struct btrfs_path *path; 88 size_t name_len = strlen(name); 89 int ret = 0; 90 91 ASSERT(trans); 92 93 if (name_len + size > BTRFS_MAX_XATTR_SIZE(root->fs_info)) 94 return -ENOSPC; 95 96 path = btrfs_alloc_path(); 97 if (!path) 98 return -ENOMEM; 99 path->skip_release_on_error = 1; 100 101 if (!value) { 102 di = btrfs_lookup_xattr(trans, root, path, 103 btrfs_ino(BTRFS_I(inode)), name, name_len, -1); 104 if (!di && (flags & XATTR_REPLACE)) 105 ret = -ENODATA; 106 else if (IS_ERR(di)) 107 ret = PTR_ERR(di); 108 else if (di) 109 ret = btrfs_delete_one_dir_name(trans, root, path, di); 110 goto out; 111 } 112 113 /* 114 * For a replace we can't just do the insert blindly. 115 * Do a lookup first (read-only btrfs_search_slot), and return if xattr 116 * doesn't exist. If it exists, fall down below to the insert/replace 117 * path - we can't race with a concurrent xattr delete, because the VFS 118 * locks the inode's i_mutex before calling setxattr or removexattr. 119 */ 120 if (flags & XATTR_REPLACE) { 121 ASSERT(inode_is_locked(inode)); 122 di = btrfs_lookup_xattr(NULL, root, path, 123 btrfs_ino(BTRFS_I(inode)), name, name_len, 0); 124 if (!di) 125 ret = -ENODATA; 126 else if (IS_ERR(di)) 127 ret = PTR_ERR(di); 128 if (ret) 129 goto out; 130 btrfs_release_path(path); 131 di = NULL; 132 } 133 134 ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(BTRFS_I(inode)), 135 name, name_len, value, size); 136 if (ret == -EOVERFLOW) { 137 /* 138 * We have an existing item in a leaf, split_leaf couldn't 139 * expand it. That item might have or not a dir_item that 140 * matches our target xattr, so lets check. 141 */ 142 ret = 0; 143 btrfs_assert_tree_write_locked(path->nodes[0]); 144 di = btrfs_match_dir_item_name(fs_info, path, name, name_len); 145 if (!di && !(flags & XATTR_REPLACE)) { 146 ret = -ENOSPC; 147 goto out; 148 } 149 } else if (ret == -EEXIST) { 150 ret = 0; 151 di = btrfs_match_dir_item_name(fs_info, path, name, name_len); 152 ASSERT(di); /* logic error */ 153 } else if (ret) { 154 goto out; 155 } 156 157 if (di && (flags & XATTR_CREATE)) { 158 ret = -EEXIST; 159 goto out; 160 } 161 162 if (di) { 163 /* 164 * We're doing a replace, and it must be atomic, that is, at 165 * any point in time we have either the old or the new xattr 166 * value in the tree. We don't want readers (getxattr and 167 * listxattrs) to miss a value, this is specially important 168 * for ACLs. 169 */ 170 const int slot = path->slots[0]; 171 struct extent_buffer *leaf = path->nodes[0]; 172 const u16 old_data_len = btrfs_dir_data_len(leaf, di); 173 const u32 item_size = btrfs_item_size(leaf, slot); 174 const u32 data_size = sizeof(*di) + name_len + size; 175 unsigned long data_ptr; 176 char *ptr; 177 178 if (size > old_data_len) { 179 if (btrfs_leaf_free_space(leaf) < 180 (size - old_data_len)) { 181 ret = -ENOSPC; 182 goto out; 183 } 184 } 185 186 if (old_data_len + name_len + sizeof(*di) == item_size) { 187 /* No other xattrs packed in the same leaf item. */ 188 if (size > old_data_len) 189 btrfs_extend_item(path, size - old_data_len); 190 else if (size < old_data_len) 191 btrfs_truncate_item(path, data_size, 1); 192 } else { 193 /* There are other xattrs packed in the same item. */ 194 ret = btrfs_delete_one_dir_name(trans, root, path, di); 195 if (ret) 196 goto out; 197 btrfs_extend_item(path, data_size); 198 } 199 200 ptr = btrfs_item_ptr(leaf, slot, char); 201 ptr += btrfs_item_size(leaf, slot) - data_size; 202 di = (struct btrfs_dir_item *)ptr; 203 btrfs_set_dir_data_len(leaf, di, size); 204 data_ptr = ((unsigned long)(di + 1)) + name_len; 205 write_extent_buffer(leaf, value, data_ptr, size); 206 btrfs_mark_buffer_dirty(leaf); 207 } else { 208 /* 209 * Insert, and we had space for the xattr, so path->slots[0] is 210 * where our xattr dir_item is and btrfs_insert_xattr_item() 211 * filled it. 212 */ 213 } 214 out: 215 btrfs_free_path(path); 216 if (!ret) { 217 set_bit(BTRFS_INODE_COPY_EVERYTHING, 218 &BTRFS_I(inode)->runtime_flags); 219 clear_bit(BTRFS_INODE_NO_XATTRS, &BTRFS_I(inode)->runtime_flags); 220 } 221 return ret; 222 } 223 224 /* 225 * @value: "" makes the attribute to empty, NULL removes it 226 */ 227 int btrfs_setxattr_trans(struct inode *inode, const char *name, 228 const void *value, size_t size, int flags) 229 { 230 struct btrfs_root *root = BTRFS_I(inode)->root; 231 struct btrfs_trans_handle *trans; 232 const bool start_trans = (current->journal_info == NULL); 233 int ret; 234 235 if (start_trans) { 236 /* 237 * 1 unit for inserting/updating/deleting the xattr 238 * 1 unit for the inode item update 239 */ 240 trans = btrfs_start_transaction(root, 2); 241 if (IS_ERR(trans)) 242 return PTR_ERR(trans); 243 } else { 244 /* 245 * This can happen when smack is enabled and a directory is being 246 * created. It happens through d_instantiate_new(), which calls 247 * smack_d_instantiate(), which in turn calls __vfs_setxattr() to 248 * set the transmute xattr (XATTR_NAME_SMACKTRANSMUTE) on the 249 * inode. We have already reserved space for the xattr and inode 250 * update at btrfs_mkdir(), so just use the transaction handle. 251 * We don't join or start a transaction, as that will reset the 252 * block_rsv of the handle and trigger a warning for the start 253 * case. 254 */ 255 ASSERT(strncmp(name, XATTR_SECURITY_PREFIX, 256 XATTR_SECURITY_PREFIX_LEN) == 0); 257 trans = current->journal_info; 258 } 259 260 ret = btrfs_setxattr(trans, inode, name, value, size, flags); 261 if (ret) 262 goto out; 263 264 inode_inc_iversion(inode); 265 inode->i_ctime = current_time(inode); 266 ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); 267 if (ret) 268 btrfs_abort_transaction(trans, ret); 269 out: 270 if (start_trans) 271 btrfs_end_transaction(trans); 272 return ret; 273 } 274 275 ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size) 276 { 277 struct btrfs_key found_key; 278 struct btrfs_key key; 279 struct inode *inode = d_inode(dentry); 280 struct btrfs_root *root = BTRFS_I(inode)->root; 281 struct btrfs_path *path; 282 int iter_ret = 0; 283 int ret = 0; 284 size_t total_size = 0, size_left = size; 285 286 /* 287 * ok we want all objects associated with this id. 288 * NOTE: we set key.offset = 0; because we want to start with the 289 * first xattr that we find and walk forward 290 */ 291 key.objectid = btrfs_ino(BTRFS_I(inode)); 292 key.type = BTRFS_XATTR_ITEM_KEY; 293 key.offset = 0; 294 295 path = btrfs_alloc_path(); 296 if (!path) 297 return -ENOMEM; 298 path->reada = READA_FORWARD; 299 300 /* search for our xattrs */ 301 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) { 302 struct extent_buffer *leaf; 303 int slot; 304 struct btrfs_dir_item *di; 305 u32 item_size; 306 u32 cur; 307 308 leaf = path->nodes[0]; 309 slot = path->slots[0]; 310 311 /* check to make sure this item is what we want */ 312 if (found_key.objectid != key.objectid) 313 break; 314 if (found_key.type > BTRFS_XATTR_ITEM_KEY) 315 break; 316 if (found_key.type < BTRFS_XATTR_ITEM_KEY) 317 continue; 318 319 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 320 item_size = btrfs_item_size(leaf, slot); 321 cur = 0; 322 while (cur < item_size) { 323 u16 name_len = btrfs_dir_name_len(leaf, di); 324 u16 data_len = btrfs_dir_data_len(leaf, di); 325 u32 this_len = sizeof(*di) + name_len + data_len; 326 unsigned long name_ptr = (unsigned long)(di + 1); 327 328 total_size += name_len + 1; 329 /* 330 * We are just looking for how big our buffer needs to 331 * be. 332 */ 333 if (!size) 334 goto next; 335 336 if (!buffer || (name_len + 1) > size_left) { 337 iter_ret = -ERANGE; 338 break; 339 } 340 341 read_extent_buffer(leaf, buffer, name_ptr, name_len); 342 buffer[name_len] = '\0'; 343 344 size_left -= name_len + 1; 345 buffer += name_len + 1; 346 next: 347 cur += this_len; 348 di = (struct btrfs_dir_item *)((char *)di + this_len); 349 } 350 } 351 352 if (iter_ret < 0) 353 ret = iter_ret; 354 else 355 ret = total_size; 356 357 btrfs_free_path(path); 358 359 return ret; 360 } 361 362 static int btrfs_xattr_handler_get(const struct xattr_handler *handler, 363 struct dentry *unused, struct inode *inode, 364 const char *name, void *buffer, size_t size) 365 { 366 name = xattr_full_name(handler, name); 367 return btrfs_getxattr(inode, name, buffer, size); 368 } 369 370 static int btrfs_xattr_handler_set(const struct xattr_handler *handler, 371 struct user_namespace *mnt_userns, 372 struct dentry *unused, struct inode *inode, 373 const char *name, const void *buffer, 374 size_t size, int flags) 375 { 376 if (btrfs_root_readonly(BTRFS_I(inode)->root)) 377 return -EROFS; 378 379 name = xattr_full_name(handler, name); 380 return btrfs_setxattr_trans(inode, name, buffer, size, flags); 381 } 382 383 static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler, 384 struct user_namespace *mnt_userns, 385 struct dentry *unused, struct inode *inode, 386 const char *name, const void *value, 387 size_t size, int flags) 388 { 389 int ret; 390 struct btrfs_trans_handle *trans; 391 struct btrfs_root *root = BTRFS_I(inode)->root; 392 393 name = xattr_full_name(handler, name); 394 ret = btrfs_validate_prop(BTRFS_I(inode), name, value, size); 395 if (ret) 396 return ret; 397 398 if (btrfs_ignore_prop(BTRFS_I(inode), name)) 399 return 0; 400 401 trans = btrfs_start_transaction(root, 2); 402 if (IS_ERR(trans)) 403 return PTR_ERR(trans); 404 405 ret = btrfs_set_prop(trans, inode, name, value, size, flags); 406 if (!ret) { 407 inode_inc_iversion(inode); 408 inode->i_ctime = current_time(inode); 409 ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); 410 if (ret) 411 btrfs_abort_transaction(trans, ret); 412 } 413 414 btrfs_end_transaction(trans); 415 416 return ret; 417 } 418 419 static const struct xattr_handler btrfs_security_xattr_handler = { 420 .prefix = XATTR_SECURITY_PREFIX, 421 .get = btrfs_xattr_handler_get, 422 .set = btrfs_xattr_handler_set, 423 }; 424 425 static const struct xattr_handler btrfs_trusted_xattr_handler = { 426 .prefix = XATTR_TRUSTED_PREFIX, 427 .get = btrfs_xattr_handler_get, 428 .set = btrfs_xattr_handler_set, 429 }; 430 431 static const struct xattr_handler btrfs_user_xattr_handler = { 432 .prefix = XATTR_USER_PREFIX, 433 .get = btrfs_xattr_handler_get, 434 .set = btrfs_xattr_handler_set, 435 }; 436 437 static const struct xattr_handler btrfs_btrfs_xattr_handler = { 438 .prefix = XATTR_BTRFS_PREFIX, 439 .get = btrfs_xattr_handler_get, 440 .set = btrfs_xattr_handler_set_prop, 441 }; 442 443 const struct xattr_handler *btrfs_xattr_handlers[] = { 444 &btrfs_security_xattr_handler, 445 #ifdef CONFIG_BTRFS_FS_POSIX_ACL 446 &posix_acl_access_xattr_handler, 447 &posix_acl_default_xattr_handler, 448 #endif 449 &btrfs_trusted_xattr_handler, 450 &btrfs_user_xattr_handler, 451 &btrfs_btrfs_xattr_handler, 452 NULL, 453 }; 454 455 static int btrfs_initxattrs(struct inode *inode, 456 const struct xattr *xattr_array, void *fs_private) 457 { 458 struct btrfs_trans_handle *trans = fs_private; 459 const struct xattr *xattr; 460 unsigned int nofs_flag; 461 char *name; 462 int err = 0; 463 464 /* 465 * We're holding a transaction handle, so use a NOFS memory allocation 466 * context to avoid deadlock if reclaim happens. 467 */ 468 nofs_flag = memalloc_nofs_save(); 469 for (xattr = xattr_array; xattr->name != NULL; xattr++) { 470 name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 471 strlen(xattr->name) + 1, GFP_KERNEL); 472 if (!name) { 473 err = -ENOMEM; 474 break; 475 } 476 strcpy(name, XATTR_SECURITY_PREFIX); 477 strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name); 478 err = btrfs_setxattr(trans, inode, name, xattr->value, 479 xattr->value_len, 0); 480 kfree(name); 481 if (err < 0) 482 break; 483 } 484 memalloc_nofs_restore(nofs_flag); 485 return err; 486 } 487 488 int btrfs_xattr_security_init(struct btrfs_trans_handle *trans, 489 struct inode *inode, struct inode *dir, 490 const struct qstr *qstr) 491 { 492 return security_inode_init_security(inode, dir, qstr, 493 &btrfs_initxattrs, trans); 494 } 495