[PATCH] erofs-utils: sort shared xattr

Huang Jianan huangjianan at oppo.com
Thu Oct 21 13:58:47 AEDT 2021


Sort shared xattr before writing to disk to ensure the consistency
of reproducible builds.
---
 lib/xattr.c | 34 ++++++++++++++++++++++++++++++----
 1 file changed, 30 insertions(+), 4 deletions(-)

diff --git a/lib/xattr.c b/lib/xattr.c
index 196133a..f17e57e 100644
--- a/lib/xattr.c
+++ b/lib/xattr.c
@@ -171,7 +171,7 @@ static struct xattr_item *parse_one_xattr(const char *path, const char *key,
 	/* allocate key-value buffer */
 	len[0] = keylen - prefixlen;
 
-	kvbuf = malloc(len[0] + len[1]);
+	kvbuf = malloc(len[0] + len[1] + 1);
 	if (!kvbuf)
 		return ERR_PTR(-ENOMEM);
 	memcpy(kvbuf, key + prefixlen, len[0]);
@@ -196,6 +196,7 @@ static struct xattr_item *parse_one_xattr(const char *path, const char *key,
 			len[1] = ret;
 		}
 	}
+	kvbuf[len[0] + len[1]] = '\0';
 	return get_xattritem(prefix, kvbuf, len);
 }
 
@@ -398,7 +399,7 @@ static int erofs_droid_xattr_set_caps(struct erofs_inode *inode)
 	len[0] = sizeof("capability") - 1;
 	len[1] = sizeof(caps);
 
-	kvbuf = malloc(len[0] + len[1]);
+	kvbuf = malloc(len[0] + len[1] + 1);
 	if (!kvbuf)
 		return -ENOMEM;
 
@@ -409,6 +410,7 @@ static int erofs_droid_xattr_set_caps(struct erofs_inode *inode)
 	caps.data[1].permitted = (u32) (capabilities >> 32);
 	caps.data[1].inheritable = 0;
 	memcpy(kvbuf + len[0], &caps, len[1]);
+	kvbuf[len[0] + len[1]] = '\0';
 
 	item = get_xattritem(EROFS_XATTR_INDEX_SECURITY, kvbuf, len);
 	if (IS_ERR(item))
@@ -562,13 +564,23 @@ static struct erofs_bhops erofs_write_shared_xattrs_bhops = {
 	.flush = erofs_bh_flush_write_shared_xattrs,
 };
 
+static int comp_xattr_item(const void *a, const void *b)
+{
+	const struct xattr_item *ia, *ib;
+
+	ia = (*((const struct inode_xattr_node **)a))->item;
+	ib = (*((const struct inode_xattr_node **)b))->item;
+
+	return strcmp(ia->kvbuf, ib->kvbuf);
+}
+
 int erofs_build_shared_xattrs_from_path(const char *path)
 {
 	int ret;
 	struct erofs_buffer_head *bh;
-	struct inode_xattr_node *node, *n;
+	struct inode_xattr_node *node, *n, **sorted_n;
 	char *buf;
-	unsigned int p;
+	unsigned int p, i;
 	erofs_off_t off;
 
 	/* check if xattr or shared xattr is disabled */
@@ -606,6 +618,20 @@ int erofs_build_shared_xattrs_from_path(const char *path)
 	off %= EROFS_BLKSIZ;
 	p = 0;
 
+	sorted_n = malloc(shared_xattrs_count * sizeof(n));
+	if (!sorted_n)
+		return -ENOMEM;
+	i = 0;
+	list_for_each_entry_safe(node, n, &shared_xattrs_list, list) {
+		list_del(&node->list);
+		sorted_n[i++] = node;
+	}
+	DBG_BUGON(i != shared_xattrs_count);
+	qsort(sorted_n, shared_xattrs_count, sizeof(n), comp_xattr_item);
+	for (i = 0; i < shared_xattrs_count; i++)
+		list_add_tail(&sorted_n[i]->list, &shared_xattrs_list);
+	free(sorted_n);
+
 	list_for_each_entry_safe(node, n, &shared_xattrs_list, list) {
 		struct xattr_item *const item = node->item;
 		const struct erofs_xattr_entry entry = {
-- 
2.25.1



More information about the Linux-erofs mailing list