btrfs-progs: mkfs: fix xattr enumeration

Use the return value of listxattr instead of tokenizing.

The end of the extended attribute list is indicated by the return value,
not an empty list item (two consecutive NULs). Using strtok in this way
thus sometimes caused add_xattr_item to reuse stack data in xattr_list
from the previous invocation, thus querying attributes that are not
actually in the file's xattr list.

Issue: #194
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: Vladimir Panteleev <git@vladimir.panteleev.md>
Signed-off-by: David Sterba <dsterba@suse.com>
master
Vladimir Panteleev 2019-09-06 09:58:46 +00:00 committed by David Sterba
parent 95c49ef7f7
commit 11e126b162
1 changed files with 5 additions and 6 deletions

View File

@ -228,10 +228,9 @@ static int add_xattr_item(struct btrfs_trans_handle *trans,
int ret;
int cur_name_len;
char xattr_list[XATTR_LIST_MAX];
char *xattr_list_end;
char *cur_name;
char cur_value[XATTR_SIZE_MAX];
char delimiter = '\0';
char *next_location = xattr_list;
ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
if (ret < 0) {
@ -243,10 +242,10 @@ static int add_xattr_item(struct btrfs_trans_handle *trans,
if (ret == 0)
return ret;
cur_name = strtok(xattr_list, &delimiter);
while (cur_name != NULL) {
xattr_list_end = xattr_list + ret;
cur_name = xattr_list;
while (cur_name < xattr_list_end) {
cur_name_len = strlen(cur_name);
next_location += cur_name_len + 1;
ret = lgetxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
if (ret < 0) {
@ -266,7 +265,7 @@ static int add_xattr_item(struct btrfs_trans_handle *trans,
file_name);
}
cur_name = strtok(next_location, &delimiter);
cur_name += cur_name_len + 1;
}
return ret;