Blame SOURCES/e2fsprogs-1.45.6-libext2fs-fix-segault-when-setting-an-xattr-with-an-.patch

a77133
From 9beb50cd2c05f69f1231eb1f4579d6770ef48359 Mon Sep 17 00:00:00 2001
a77133
From: Theodore Ts'o <tytso@mit.edu>
a77133
Date: Sun, 7 Feb 2021 23:21:58 -0500
a77133
Subject: [PATCH 18/46] libext2fs: fix segault when setting an xattr with an
a77133
 unknown prefix
a77133
Content-Type: text/plain
a77133
a77133
Also avoid unnecessary calls to find_ea_index() by caching the short
a77133
name and name index in the ext2_attr structure.
a77133
a77133
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
a77133
Signed-off-by: Lukas Czerner <lczerner@redhat.com>
a77133
---
a77133
 lib/ext2fs/ext_attr.c | 64 +++++++++++++++++++++----------------------
a77133
 1 file changed, 32 insertions(+), 32 deletions(-)
a77133
a77133
diff --git a/lib/ext2fs/ext_attr.c b/lib/ext2fs/ext_attr.c
a77133
index 871319a5..8a7a17cf 100644
a77133
--- a/lib/ext2fs/ext_attr.c
a77133
+++ b/lib/ext2fs/ext_attr.c
a77133
@@ -293,7 +293,9 @@ errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk,
a77133
 
a77133
 /* Manipulate the contents of extended attribute regions */
a77133
 struct ext2_xattr {
a77133
+	int name_index;
a77133
 	char *name;
a77133
+	char *short_name;
a77133
 	void *value;
a77133
 	unsigned int value_len;
a77133
 	ext2_ino_t ea_ino;
a77133
@@ -643,29 +645,23 @@ write_xattrs_to_buffer(ext2_filsys fs, struct ext2_xattr *attrs, int count,
a77133
 	struct ext2_xattr *x;
a77133
 	struct ext2_ext_attr_entry *e = entries_start;
a77133
 	char *end = (char *) entries_start + storage_size;
a77133
-	const char *shortname;
a77133
 	unsigned int value_size;
a77133
-	int idx, ret;
a77133
 	errcode_t err;
a77133
 
a77133
 	memset(entries_start, 0, storage_size);
a77133
 	for (x = attrs; x < attrs + count; x++) {
a77133
-		/* Calculate index and shortname position */
a77133
-		shortname = x->name;
a77133
-		ret = find_ea_index(x->name, &shortname, &idx);
a77133
-
a77133
 		value_size = ((x->value_len + EXT2_EXT_ATTR_PAD - 1) /
a77133
 			      EXT2_EXT_ATTR_PAD) * EXT2_EXT_ATTR_PAD;
a77133
 
a77133
 		/* Fill out e appropriately */
a77133
-		e->e_name_len = strlen(shortname);
a77133
-		e->e_name_index = (ret ? idx : 0);
a77133
+		e->e_name_len = strlen(x->short_name);
a77133
+		e->e_name_index = x->name_index;
a77133
 
a77133
 		e->e_value_size = x->value_len;
a77133
 		e->e_value_inum = x->ea_ino;
a77133
 
a77133
 		/* Store name */
a77133
-		memcpy((char *)e + sizeof(*e), shortname, e->e_name_len);
a77133
+		memcpy((char *)e + sizeof(*e), x->short_name, e->e_name_len);
a77133
 		if (x->ea_ino) {
a77133
 			e->e_value_offs = 0;
a77133
 		} else {
a77133
@@ -875,6 +871,8 @@ static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
a77133
 			memcpy(x->name + prefix_len,
a77133
 			       (char *)entry + sizeof(*entry),
a77133
 			       entry->e_name_len);
a77133
+		x->short_name = x->name + prefix_len;
a77133
+		x->name_index = entry->e_name_index;
a77133
 
a77133
 		/* Check & copy value */
a77133
 		if (!ext2fs_has_feature_ea_inode(handle->fs->super) &&
a77133
@@ -1302,7 +1300,8 @@ out:
a77133
 }
a77133
 
a77133
 static errcode_t xattr_update_entry(ext2_filsys fs, struct ext2_xattr *x,
a77133
-				    const char *name, const void *value,
a77133
+				    const char *name, const char *short_name,
a77133
+				    int index, const void *value,
a77133
 				    size_t value_len, int in_inode)
a77133
 {
a77133
 	ext2_ino_t ea_ino = 0;
a77133
@@ -1336,8 +1335,11 @@ static errcode_t xattr_update_entry(ext2_filsys fs, struct ext2_xattr *x,
a77133
 			goto fail;
a77133
 	}
a77133
 
a77133
-	if (!x->name)
a77133
+	if (!x->name) {
a77133
 		x->name = new_name;
a77133
+		x->short_name = new_name + (short_name  - name);
a77133
+	}
a77133
+	x->name_index = index;
a77133
 
a77133
 	if (x->value)
a77133
 		ext2fs_free_mem(&x->value);
a77133
@@ -1356,31 +1358,27 @@ fail:
a77133
 }
a77133
 
a77133
 static int xattr_find_position(struct ext2_xattr *attrs, int count,
a77133
-			       const char *name)
a77133
+			       const char *shortname, int name_idx)
a77133
 {
a77133
 	struct ext2_xattr *x;
a77133
 	int i;
a77133
-	const char *shortname, *x_shortname;
a77133
-	int name_idx, x_name_idx;
a77133
 	int shortname_len, x_shortname_len;
a77133
 
a77133
-	find_ea_index(name, &shortname, &name_idx);
a77133
 	shortname_len = strlen(shortname);
a77133
 
a77133
 	for (i = 0, x = attrs; i < count; i++, x++) {
a77133
-		find_ea_index(x->name, &x_shortname, &x_name_idx);
a77133
-		if (name_idx < x_name_idx)
a77133
+		if (name_idx < x->name_index)
a77133
 			break;
a77133
-		if (name_idx > x_name_idx)
a77133
+		if (name_idx > x->name_index)
a77133
 			continue;
a77133
 
a77133
-		x_shortname_len = strlen(x_shortname);
a77133
+		x_shortname_len = strlen(x->short_name);
a77133
 		if (shortname_len < x_shortname_len)
a77133
 			break;
a77133
 		if (shortname_len > x_shortname_len)
a77133
 			continue;
a77133
 
a77133
-		if (memcmp(shortname, x_shortname, shortname_len) <= 0)
a77133
+		if (memcmp(shortname, x->short_name, shortname_len) <= 0)
a77133
 			break;
a77133
 	}
a77133
 	return i;
a77133
@@ -1395,8 +1393,8 @@ static errcode_t xattr_array_update(struct ext2_xattr_handle *h,
a77133
 	struct ext2_xattr tmp;
a77133
 	int add_to_ibody;
a77133
 	int needed;
a77133
-	int name_len, name_idx;
a77133
-	const char *shortname;
a77133
+	int name_len, name_idx = 0;
a77133
+	const char *shortname = name;
a77133
 	int new_idx;
a77133
 	int ret;
a77133
 
a77133
@@ -1423,7 +1421,8 @@ static errcode_t xattr_array_update(struct ext2_xattr_handle *h,
a77133
 
a77133
 		/* Update the existing entry. */
a77133
 		ret = xattr_update_entry(h->fs, &h->attrs[old_idx], name,
a77133
-					 value, value_len, in_inode);
a77133
+					 shortname, name_idx, value,
a77133
+					 value_len, in_inode);
a77133
 		if (ret)
a77133
 			return ret;
a77133
 		if (h->ibody_count <= old_idx) {
a77133
@@ -1451,7 +1450,8 @@ static errcode_t xattr_array_update(struct ext2_xattr_handle *h,
a77133
 	if (old_idx >= 0) {
a77133
 		/* Update the existing entry. */
a77133
 		ret = xattr_update_entry(h->fs, &h->attrs[old_idx], name,
a77133
-					 value, value_len, in_inode);
a77133
+					 shortname, name_idx, value,
a77133
+					 value_len, in_inode);
a77133
 		if (ret)
a77133
 			return ret;
a77133
 		if (old_idx < h->ibody_count) {
a77133
@@ -1460,7 +1460,8 @@ static errcode_t xattr_array_update(struct ext2_xattr_handle *h,
a77133
 			 * entries in the block are sorted.
a77133
 			 */
a77133
 			new_idx = xattr_find_position(h->attrs + h->ibody_count,
a77133
-				h->count - h->ibody_count, name);
a77133
+						      h->count - h->ibody_count,
a77133
+						      shortname, name_idx);
a77133
 			new_idx += h->ibody_count - 1;
a77133
 			tmp = h->attrs[old_idx];
a77133
 			memmove(h->attrs + old_idx, h->attrs + old_idx + 1,
a77133
@@ -1472,7 +1473,8 @@ static errcode_t xattr_array_update(struct ext2_xattr_handle *h,
a77133
 	}
a77133
 
a77133
 	new_idx = xattr_find_position(h->attrs + h->ibody_count,
a77133
-				      h->count - h->ibody_count, name);
a77133
+				      h->count - h->ibody_count,
a77133
+				      shortname, name_idx);
a77133
 	new_idx += h->ibody_count;
a77133
 	add_to_ibody = 0;
a77133
 
a77133
@@ -1483,8 +1485,8 @@ add_new:
a77133
 			return ret;
a77133
 	}
a77133
 
a77133
-	ret = xattr_update_entry(h->fs, &h->attrs[h->count], name, value,
a77133
-				 value_len, in_inode);
a77133
+	ret = xattr_update_entry(h->fs, &h->attrs[h->count], name, shortname,
a77133
+				 name_idx, value, value_len, in_inode);
a77133
 	if (ret)
a77133
 		return ret;
a77133
 
a77133
@@ -1502,12 +1504,10 @@ static int space_used(struct ext2_xattr *attrs, int count)
a77133
 {
a77133
 	int total = 0;
a77133
 	struct ext2_xattr *x;
a77133
-	const char *shortname;
a77133
-	int i, len, name_idx;
a77133
+	int i, len;
a77133
 
a77133
 	for (i = 0, x = attrs; i < count; i++, x++) {
a77133
-		find_ea_index(x->name, &shortname, &name_idx);
a77133
-		len = strlen(shortname);
a77133
+		len = strlen(x->short_name);
a77133
 		total += EXT2_EXT_ATTR_LEN(len);
a77133
 		if (!x->ea_ino)
a77133
 			total += EXT2_EXT_ATTR_SIZE(x->value_len);
a77133
-- 
a77133
2.35.1
a77133