Skip to contentSkip to conversation

erofs-utils: xattr: fix OOB access due to alignment

PATCH In review Gao Xiang 2 participants 2 messages Nov 27, 2020 – Dec 5, 2020 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/1 erofs-utils: xattr: fix OOB access due to alignment

From: Gao Xiang <[email protected]>

erofs_buf_write_bhops can only be safely used for block-aligned buffers, otherwise, it could write random out-of-bound data due to buffer alignment. Such random data is meaningless but it does harm to reproducable builds.

Fixes 116ac0a254fc ("erofs-utils: introduce shared xattr support")
---
 lib/xattr.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
lib/xattr.c +17 -1
index 2596601..49ebb9c 100644
@@ -18,6 +18,7 @@
 #include "erofs/hashtable.h"
 #include "erofs/xattr.h"
 #include "erofs/cache.h"
+#include "erofs/io.h"
 
 #define EA_HASHTABLE_BITS 16
 
@@ -522,6 +523,21 @@ static void erofs_cleanxattrs(bool sharedxattrs)
 	shared_xattrs_size = shared_xattrs_count = 0;
 }
 
+static bool erofs_bh_flush_write_shared_xattrs(struct erofs_buffer_head *bh)
+{
+	void *buf = bh->fsprivate;
+	int err = dev_write(buf, erofs_btell(bh, false), shared_xattrs_size);
+
+	if (err)
+		return false;
+	free(buf);
+	return erofs_bh_flush_generic_end(bh);
+}
+
+static struct erofs_bhops erofs_write_shared_xattrs_bhops = {
+	.flush = erofs_bh_flush_write_shared_xattrs,
+};
+
 int erofs_build_shared_xattrs_from_path(const char *path)
 {
 	int ret;
@@ -586,7 +602,7 @@ int erofs_build_shared_xattrs_from_path(const char *path)
 		free(node);
 	}
 	bh->fsprivate = buf;
-	bh->op = &erofs_buf_write_bhops;
+	bh->op = &erofs_write_shared_xattrs_bhops;
 out:
 	erofs_cleanxattrs(true);
 	return 0;
Signature · 1 line
2.18.4
Reviewed-by Li Guifu on v1 1/1
#1 lore raw
Quoted 7 lines · Gao Xiang

From: Gao Xiang <[email protected]>

erofs_buf_write_bhops can only be safely used for block-aligned buffers, otherwise, it could write random out-of-bound data due to buffer alignment. Such random data is meaningless but it does harm to reproducable builds.

Quoted 7 lines
Fixes 116ac0a254fc ("erofs-utils: introduce shared xattr support")
---
 lib/xattr.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

It looks good

Thanks,