Btrfs: make xattr replace operations atomic
PATCH 1/1 Btrfs: make xattr replace operations atomic
Replacing a xattr consists of doing a lookup for its existing value, delete the current value from the respective leaf, release the search path and then finally insert the new value. This leaves a time window where readers (getxattr, listxattrs) won't see any value for the xattr. Xattrs are used to store ACLs, so this has security implications.
This change also fixes 2 other existing issues which were:
*) Deleting the old xattr value without verifying first if the new xattr will
fit in the existing leaf item (in case multiple xattrs are packed in the same item due to name hash collision);
*) Returning -EEXIST when the flag XATTR_CREATE is given and the xattr doesn't
exist but we have have an existing item that packs muliple xattrs with the same name hash as the input xattr. In this case we should return ENOSPC.
A test case for xfstests follows soon.
Thanks to Alexandre Oliva for reporting the non-atomicity of the xattr replace implementation.
--- fs/btrfs/ctree.h | 4 ++ fs/btrfs/dir-item.c | 10 ++-- fs/btrfs/xattr.c | 142 ++++++++++++++++++++++++++++++---------------------- 3 files changed, 88 insertions(+), 68 deletions(-)
fs/btrfs/ctree.h +4 -0
@@ -3687,6 +3687,10 @@ struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,int verify_dir_item (struct btrfs_root *root,struct extent_buffer *leaf,struct btrfs_dir_item *dir_item); +struct btrfs_dir_item *btrfs_match_dir_item_name (struct btrfs_root *root, +struct btrfs_path *path, +const char *name, +int name_len);/* orphan.c */ int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
fs/btrfs/dir-item.c +3 -7
@@ -21,10 +21,6 @@#include "hash.h" #include "transaction.h" -static struct btrfs_dir_item *btrfs_match_dir_item_name (struct btrfs_root *root, -struct btrfs_path *path, -const char *name,int name_len); -/* * insert a name into a directory, doing overflow properly if there is a hash * collision. data_size indicates how big the item inserted should be. On
@@ -385,9 +381,9 @@ struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,* this walks through all the entries in a dir item and finds one * for a specific name. */ -static struct btrfs_dir_item *btrfs_match_dir_item_name (struct btrfs_root *root, -struct btrfs_path *path, -const char *name,int name_len) +struct btrfs_dir_item *btrfs_match_dir_item_name (struct btrfs_root *root, +struct btrfs_path *path, +const char *name,int name_len) {struct btrfs_dir_item *dir_item;unsigned long name_ptr;
fs/btrfs/xattr.c +81 -61
171 lines not on this page · raw message
Signature · 1 line
1.9.1
Quoted 19 lines · Filipe Manana
Replacing a xattr consists of doing a lookup for its existing value, delete the current value from the respective leaf, release the search path and then finally insert the new value. This leaves a time window where readers (getxattr, listxattrs) won't see any value for the xattr. Xattrs are used to store ACLs, so this has security implications.
This change also fixes 2 other existing issues which were:
*) Deleting the old xattr value without verifying first if the new xattr will
fit in the existing leaf item (in case multiple xattrs are packed
in the
same item due to name hash collision);*) Returning
-EEXISTwhen the flagXATTR_CREATEis given and the xattr doesn't
exist but we have have an existing item that packs muliple xattrs
with
the same name hash as the input xattr. In this case we should
return ENOSPC.
Quoted 4 lines
A test case for xfstests follows soon.
Thanks to Alexandre Oliva for reporting the non-atomicity of the xattr replace implementation.
Thanks Filipe, one problem:
fs/btrfs/xattr.c static int do_setxattr(struct btrfs_trans_handle *trans, · line 174
Quoted 8 lines
fs/btrfs/xattr.c +7
-0+ +if (size > old_data_len) { +if (btrfs_leaf_free_space (root, leaf) < + (size - old_data_len)) { + ret = -ENOSPC ; + goto out; + } }
This means replacing an xattr item can fail if the leaf doesn't happen to have free space. btrfs_search_slot already has code meant to extend the item, even if the item you're searching for is already found. (see the call to split_leaf with extend == 1).
So what you want to do is find out the current size of the item, and call btrfs search_slot again with that size + the extra needed for the new name. From there you should be able to call btrfs_extend_item like you currently are.
-chris
Quoted 52 lines · Chris Mason
Quoted 25 lines · Filipe Manana
Replacing a xattr consists of doing a lookup for its existing value, delete
the current value from the respective leaf, release the search path and then
finally insert the new value. This leaves a time window where readers (getxattr,
listxattrs) won't see any value for the xattr. Xattrs are used to store ACLs,
so this has security implications.This change also fixes 2 other existing issues which were:
*) Deleting the old xattr value without verifying first if the new xattr will
fit in the existing leaf item (in case multiple xattrs are packed in
the
same item due to name hash collision);*) Returning
-EEXISTwhen the flagXATTR_CREATEis given and the xattr doesn't
exist but we have have an existing item that packs muliple xattrs with the same name hash as the input xattr. In this case we should return
ENOSPC.Quoted 5 lines
A test case for xfstests follows soon.
Thanks to Alexandre Oliva for reporting the non-atomicity of the xattr replace
implementation.Thanks Filipe, one problem:
fs/btrfs/xattr.c static int do_setxattr(struct btrfs_trans_handle *trans, · line 174
Quoted 8 lines
fs/btrfs/xattr.c +7
-0+ +if (size > old_data_len) { +if (btrfs_leaf_free_space (root, leaf) < + (size - old_data_len)) { + ret = -ENOSPC ; + goto out; + } }This means replacing an xattr item can fail if the leaf doesn't happen to have free space.
btrfs_search_slotalready has code meant to extend the item, even if the item you're searching for is already found. (see the call tosplit_leafwith extend == 1).
Quoted 4 lines
So what you want to do is find out the current size of the item, and call btrfs
search_slotagain with that size + the extra needed for the new name. From there you should be able to callbtrfs_extend_itemlike you currently are.
So what I do is call btrfs_seach_slot with ins_size == new_xattr_size. If it returns -eoverflow (it couldn't extent by +new_size, which includes the name, dir_item and btrfs_item structs), I don't return immediately because after deleting the existing xattr we may have enough space in the leaf (that is, we no longer to extend by new_size, but by a smaller amount == new_size - old_size, and excluding the name, dir_item and btrfs_item structs), effectively not requiring the original size passed to btrfs_search_slot. So if I correctly understood you, it's almost equivalent to your suggestion but without releasing paths and keep retrying.
Did I miss something? :)
Quoted 10 lines
-chris
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 5 lines
Filipe David Manana, "Reasonable men adapt themselves to the world. Unreasonable men adapt the world to themselves. That's why all progress depends on unreasonable men."
PATCH v2 1/1 Btrfs: make xattr replace operations atomic
In reply to Filipe MananaReplacing a xattr consists of doing a lookup for its existing value, delete the current value from the respective leaf, release the search path and then finally insert the new value. This leaves a time window where readers (getxattr, listxattrs) won't see any value for the xattr. Xattrs are used to store ACLs, so this has security implications.
This change also fixes 2 other existing issues which were:
*) Deleting the old xattr value without verifying first if the new xattr will
fit in the existing leaf item (in case multiple xattrs are packed in the same item due to name hash collision);
*) Returning -EEXIST when the flag XATTR_CREATE is given and the xattr doesn't
exist but we have have an existing item that packs muliple xattrs with the same name hash as the input xattr. In this case we should return ENOSPC.
A test case for xfstests follows soon.
Thanks to Alexandre Oliva for reporting the non-atomicity of the xattr replace implementation.
---
V2: Added missing btrfs_mark_buffer_dirty() call for the case where a replace
happens for an item with no other xattrs and the new xattr value size is the same as the old xattr value size;
Made btrfs_search_slot not release the path if it returns EOVERFLOW (from split_leaf), so that we can get the current size of the item when doing the replace and extend the leaf to a size smaller then what we passed to btrfs_search_slot if the xattr exists in the item (and the new extend size is new_size - old_size, excluding xattr's name and sizeof struct btrfs_dir_item).
fs/btrfs/ctree.c | 2 +- fs/btrfs/ctree.h | 4 ++ fs/btrfs/dir-item.c | 10 ++-- fs/btrfs/xattr.c | 145 ++++++++++++++++++++++++++++++---------------------- 4 files changed, 92 insertions(+), 69 deletions(-)
fs/btrfs/ctree.c +1 -1
@@ -2939,7 +2939,7 @@ done:*/ if (!p->leave_spinning)btrfs_set_path_blocking (p); -if (ret <0 ) +if (ret <0 && ret != -EOVERFLOW )btrfs_release_path (p);return ret; }
fs/btrfs/ctree.h +4 -0
@@ -3687,6 +3687,10 @@ struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,int verify_dir_item (struct btrfs_root *root,struct extent_buffer *leaf,struct btrfs_dir_item *dir_item); +struct btrfs_dir_item *btrfs_match_dir_item_name (struct btrfs_root *root, +struct btrfs_path *path, +const char *name, +int name_len);/* orphan.c */ int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
fs/btrfs/dir-item.c +3 -7
@@ -21,10 +21,6 @@#include "hash.h" #include "transaction.h" -static struct btrfs_dir_item *btrfs_match_dir_item_name (struct btrfs_root *root, -struct btrfs_path *path, -const char *name,int name_len); -/* * insert a name into a directory, doing overflow properly if there is a hash * collision. data_size indicates how big the item inserted should be. On
@@ -383,9 +379,9 @@ struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,* this walks through all the entries in a dir item and finds one * for a specific name. */ -static struct btrfs_dir_item *btrfs_match_dir_item_name (struct btrfs_root *root, -struct btrfs_path *path, -const char *name,int name_len) +struct btrfs_dir_item *btrfs_match_dir_item_name (struct btrfs_root *root, +struct btrfs_path *path, +const char *name,int name_len) {struct btrfs_dir_item *dir_item;unsigned long name_ptr;
fs/btrfs/xattr.c +84 -61
180 lines not on this page · raw message
Signature · 1 line
1.9.1
Quoted 8 lines · Filipe Manana
V2: Added missing
btrfs_mark_buffer_dirty()call for the case where a replace
happens for an item with no other xattrs and the new xattr value size is the same as the old xattr value size;
Madebtrfs_search_slotnot release the path if it returns EOVERFLOW (fromsplit_leaf), so that we can get the current size of the item when doing the replace and extend the leaf to a size smaller then what we passed tobtrfs_search_slotif the xattr exists in the item (and the new extend size isnew_size-old_size, excluding xattr's name and sizeof structbtrfs_dir_item).
Stupid Question Here: How does this "replace and extend" affect the whole copy-on-write paradigm. Aren't you now overwriting the contents of the old data instead of doing a COW?
Not a passive-aggressive commentary question, I genuinely don't understand this piece of code so I am seeking a learning opportunity.
Quoted 19 lines · Robert White
Quoted 16 lines · Filipe Manana
V2: Added missing
btrfs_mark_buffer_dirty()call for the case where a replace
happens for an item with no other xattrs and the new xattr value size
is
the same as the old xattr value size;
Madebtrfs_search_slotnot release the path if it returns EOVERFLOW
(from
split_leaf), so that we can get the current size of the item when
doing
the replace and extend the leaf to a size smaller then what we passed
to
btrfs_search_slotif the xattr exists in the item (and the new extend
size
isnew_size-old_size, excluding xattr's name and sizeof structbtrfs_dir_item).
Quoted 3 lines
Stupid Question Here: How does this "replace and extend" affect the whole copy-on-write paradigm. Aren't you now overwriting the contents of the old data instead of doing a COW?
Btree nodes are always COWed.
Quoted 8 lines
Not a passive-aggressive commentary question, I genuinely don't understand this piece of code so I am seeking a learning opportunity.
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 5 lines
Filipe David Manana, "Reasonable men adapt themselves to the world. Unreasonable men adapt the world to themselves. That's why all progress depends on unreasonable men."
PATCH v3 1/1 Btrfs: make xattr replace operations atomic
In reply to Filipe MananaReplacing a xattr consists of doing a lookup for its existing value, delete the current value from the respective leaf, release the search path and then finally insert the new value. This leaves a time window where readers (getxattr, listxattrs) won't see any value for the xattr. Xattrs are used to store ACLs, so this has security implications.
This change also fixes 2 other existing issues which were:
*) Deleting the old xattr value without verifying first if the new xattr will
fit in the existing leaf item (in case multiple xattrs are packed in the same item due to name hash collision);
*) Returning -EEXIST when the flag XATTR_CREATE is given and the xattr doesn't
exist but we have have an existing item that packs muliple xattrs with the same name hash as the input xattr. In this case we should return ENOSPC.
A test case for xfstests follows soon.
Thanks to Alexandre Oliva for reporting the non-atomicity of the xattr replace implementation.
---
V2: Added missing btrfs_mark_buffer_dirty() call for the case where a replace
happens for an item with no other xattrs and the new xattr value size is the same as the old xattr value size;
Made btrfs_search_slot not release the path if it returns EOVERFLOW (from split_leaf), so that we can get the current size of the item when doing the replace and extend the leaf to a size smaller then what we passed to btrfs_search_slot if the xattr exists in the item (and the new extend size is new_size - old_size, excluding xattr's name and sizeof struct btrfs_dir_item).
V3: Made btrfs_search_slot not release the path on error logic more explicit
and generic.
fs/btrfs/ctree.c | 2 +- fs/btrfs/ctree.h | 5 ++ fs/btrfs/dir-item.c | 10 ++-- fs/btrfs/xattr.c | 146 ++++++++++++++++++++++++++++++---------------------- 4 files changed, 94 insertions(+), 69 deletions(-)
fs/btrfs/ctree.c +1 -1
@@ -2939,7 +2939,7 @@ done:*/ if (!p->leave_spinning)btrfs_set_path_blocking (p); -if (ret <0 ) +if (ret <0 && !p->skip_release_on_error)btrfs_release_path (p);return ret; }
fs/btrfs/ctree.h +5 -0
@@ -607,6 +607,7 @@ struct btrfs_path { unsigned int leave_spinning:1; unsigned int search_commit_root:1; unsigned int need_commit_sem:1; + unsigned int skip_release_on_error:1; };/*
@@ -3687,6 +3688,10 @@ struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,int verify_dir_item (struct btrfs_root *root,struct extent_buffer *leaf,struct btrfs_dir_item *dir_item); +struct btrfs_dir_item *btrfs_match_dir_item_name (struct btrfs_root *root, +struct btrfs_path *path, +const char *name, +int name_len);/* orphan.c */ int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
fs/btrfs/dir-item.c +3 -7
@@ -21,10 +21,6 @@#include "hash.h" #include "transaction.h" -static struct btrfs_dir_item *btrfs_match_dir_item_name (struct btrfs_root *root, -struct btrfs_path *path, -const char *name,int name_len); -/* * insert a name into a directory, doing overflow properly if there is a hash * collision. data_size indicates how big the item inserted should be. On
@@ -383,9 +379,9 @@ struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,* this walks through all the entries in a dir item and finds one * for a specific name. */ -static struct btrfs_dir_item *btrfs_match_dir_item_name (struct btrfs_root *root, -struct btrfs_path *path, -const char *name,int name_len) +struct btrfs_dir_item *btrfs_match_dir_item_name (struct btrfs_root *root, +struct btrfs_path *path, +const char *name,int name_len) {struct btrfs_dir_item *dir_item;unsigned long name_ptr;
fs/btrfs/xattr.c +85 -61
182 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v4 1/1 Btrfs: make xattr replace operations atomic
In reply to Filipe MananaReplacing a xattr consists of doing a lookup for its existing value, delete the current value from the respective leaf, release the search path and then finally insert the new value. This leaves a time window where readers (getxattr, listxattrs) won't see any value for the xattr. Xattrs are used to store ACLs, so this has security implications.
This change also fixes 2 other existing issues which were:
*) Deleting the old xattr value without verifying first if the new xattr will
fit in the existing leaf item (in case multiple xattrs are packed in the same item due to name hash collision);
*) Returning -EEXIST when the flag XATTR_CREATE is given and the xattr doesn't
exist but we have have an existing item that packs muliple xattrs with the same name hash as the input xattr. In this case we should return ENOSPC.
A test case for xfstests follows soon.
Thanks to Alexandre Oliva for reporting the non-atomicity of the xattr replace implementation.
---
V2: Added missing btrfs_mark_buffer_dirty() call for the case where a replace
happens for an item with no other xattrs and the new xattr value size is the same as the old xattr value size;
Made btrfs_search_slot not release the path if it returns EOVERFLOW (from split_leaf), so that we can get the current size of the item when doing the replace and extend the leaf to a size smaller then what we passed to btrfs_search_slot if the xattr exists in the item (and the new extend size is new_size - old_size, excluding xattr's name and sizeof struct btrfs_dir_item).
V3: Made btrfs_search_slot not release the path on error logic more explicit
and generic.
V4: Added missing ENOSPC condition - if overflow detected, there's no matching
dir_item for our xattr in the leaf item and there are no flags. Ensure a replace can't insert a new value.
fs/btrfs/ctree.c | 2 +- fs/btrfs/ctree.h | 5 ++ fs/btrfs/dir-item.c | 10 ++-- fs/btrfs/xattr.c | 150 ++++++++++++++++++++++++++++++++-------------------- 4 files changed, 102 insertions(+), 65 deletions(-)
fs/btrfs/ctree.c +1 -1
@@ -2939,7 +2939,7 @@ done:*/ if (!p->leave_spinning)btrfs_set_path_blocking (p); -if (ret <0 ) +if (ret <0 && !p->skip_release_on_error)btrfs_release_path (p);return ret; }
fs/btrfs/ctree.h +5 -0
@@ -607,6 +607,7 @@ struct btrfs_path { unsigned int leave_spinning:1; unsigned int search_commit_root:1; unsigned int need_commit_sem:1; + unsigned int skip_release_on_error:1; };/*
@@ -3687,6 +3688,10 @@ struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,int verify_dir_item (struct btrfs_root *root,struct extent_buffer *leaf,struct btrfs_dir_item *dir_item); +struct btrfs_dir_item *btrfs_match_dir_item_name (struct btrfs_root *root, +struct btrfs_path *path, +const char *name, +int name_len);/* orphan.c */ int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
fs/btrfs/dir-item.c +3 -7
@@ -21,10 +21,6 @@#include "hash.h" #include "transaction.h" -static struct btrfs_dir_item *btrfs_match_dir_item_name (struct btrfs_root *root, -struct btrfs_path *path, -const char *name,int name_len); -/* * insert a name into a directory, doing overflow properly if there is a hash * collision. data_size indicates how big the item inserted should be. On
@@ -383,9 +379,9 @@ struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,* this walks through all the entries in a dir item and finds one * for a specific name. */ -static struct btrfs_dir_item *btrfs_match_dir_item_name (struct btrfs_root *root, -struct btrfs_path *path, -const char *name,int name_len) +struct btrfs_dir_item *btrfs_match_dir_item_name (struct btrfs_root *root, +struct btrfs_path *path, +const char *name,int name_len) {struct btrfs_dir_item *dir_item;unsigned long name_ptr;
fs/btrfs/xattr.c +93 -57
190 lines not on this page · raw message
Signature · 1 line
1.9.1