Re: [PATCH 6/7] erofs: handle long xattr name prefixes properly

From: Gao Xiang
Date: Mon Apr 10 2023 - 01:54:08 EST




On 2023/4/7 22:17, Jingbo Xu wrote:
Make .{list,get}xattr routines adapted to long xattr name prefixes.
When the bit 7 of erofs_xattr_entry.e_name_index is set, it indicates
that it refers to a long xattr name prefix.

Signed-off-by: Jingbo Xu <jefflexu@xxxxxxxxxxxxxxxxx>
---
fs/erofs/xattr.c | 60 +++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 52 insertions(+), 8 deletions(-)

diff --git a/fs/erofs/xattr.c b/fs/erofs/xattr.c
index 684571e83a2c..8d81593655e8 100644
--- a/fs/erofs/xattr.c
+++ b/fs/erofs/xattr.c
@@ -301,11 +301,39 @@ struct getxattr_iter {
struct qstr name;
};
+static int erofs_xattr_long_entrymatch(struct getxattr_iter *it,
+ struct erofs_xattr_entry *entry)
+{
+ struct erofs_sb_info *sbi = EROFS_SB(it->it.sb);
+ u8 idx = entry->e_name_index & EROFS_XATTR_LONG_PREFIX_MASK;
+ struct erofs_xattr_prefix_item *pf;
+
+ if (idx >= sbi->xattr_prefix_count)
+ return -ENOATTR;
+
+ pf = &sbi->xattr_prefixes[idx];

struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes + idx;

if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
return -ENOATTR;

?


+ if (it->index != pf->prefix->base_index)
+ return -ENOATTR;
+
+ if (strncmp(it->name.name, pf->prefix->infix, pf->infix_len))
+ return -ENOATTR;
+
+ it->name.name += pf->infix_len;
+ it->name.len -= pf->infix_len;
+ if (it->name.len != entry->e_name_len)
+ return -ENOATTR;
+ return 0;
+}
+
static int xattr_entrymatch(struct xattr_iter *_it,
struct erofs_xattr_entry *entry)
{
struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
+ /* should also match the infix for long name prefixes */
+ if (entry->e_name_index & EROFS_XATTR_LONG_PREFIX)
+ return erofs_xattr_long_entrymatch(it, entry);
+
return (it->index != entry->e_name_index ||
it->name.len != entry->e_name_len) ? -ENOATTR : 0;
}
@@ -487,12 +515,26 @@ static int xattr_entrylist(struct xattr_iter *_it,
{
struct listxattr_iter *it =
container_of(_it, struct listxattr_iter, it);
- unsigned int prefix_len;
- const char *prefix;
-
- const struct xattr_handler *h =
- erofs_xattr_handler(entry->e_name_index);
+ unsigned int base_index = entry->e_name_index;
+ unsigned int prefix_len, infix_len = 0;
+ const char *prefix, *infix = NULL;
+ const struct xattr_handler *h;
+
+ if (entry->e_name_index & EROFS_XATTR_LONG_PREFIX) {
+ struct erofs_sb_info *sbi = EROFS_SB(_it->sb);
+ u8 idx = entry->e_name_index & EROFS_XATTR_LONG_PREFIX_MASK;
+ struct erofs_xattr_prefix_item *pf;
+
+ if (idx >= sbi->xattr_prefix_count)
+ return 1;
+
+ pf = &sbi->xattr_prefixes[idx];

struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes + idx;

if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
return 1;
?


Otherwise it looks good to me,
Reviewed-by: Gao Xiang <hsiangkao@xxxxxxxxxxxxxxxxx>

Thanks,
Gao Xiang


+ infix = pf->prefix->infix;
+ infix_len = pf->infix_len;
+ base_index = pf->prefix->base_index;
+ }
+ h = erofs_xattr_handler(base_index);
if (!h || (h->list && !h->list(it->dentry)))
return 1;
@@ -500,16 +542,18 @@ static int xattr_entrylist(struct xattr_iter *_it,
prefix_len = strlen(prefix);
if (!it->buffer) {
- it->buffer_ofs += prefix_len + entry->e_name_len + 1;
+ it->buffer_ofs += prefix_len + infix_len +
+ entry->e_name_len + 1;
return 1;
}
- if (it->buffer_ofs + prefix_len
+ if (it->buffer_ofs + prefix_len + infix_len +
+ entry->e_name_len + 1 > it->buffer_size)
return -ERANGE;
memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
- it->buffer_ofs += prefix_len;
+ memcpy(it->buffer + it->buffer_ofs + prefix_len, infix, infix_len);
+ it->buffer_ofs += prefix_len + infix_len;
return 0;
}