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

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