mtd-utils: fixes double free in mkfs.ubifs

Message ID
State New
Delegated to: David Oberhollenzer
Headers show
  • mtd-utils: fixes double free in mkfs.ubifs
Related show

Commit Message

Yufen Yu Jan. 24, 2019, 9:06 a.m.
In inode_add_xattr(), it malloc a buffer for name, and then passes
the bufffer ptr to add_xattr(). The ptr will be used to create a new
idx_entry in add_to_index().

However, inode_add_xattr() will free the buffer before return.
which can cause double free in write_index(): free(idx_ptr[i]->name)

*** Error in `./mkfs.ubifs': double free or corruption (fasttop): 0x0000000000aae220 ***


David Oberhollenzer Feb. 11, 2019, 5:21 a.m. | #1
Applied to mtd-utils.git master

Sorry for the delay, I was looking into this in a bit more detail and also waiting for
some feedback on a related bug report.

Unfortunately some of the newer code (encryption support) assumes the current behaviour
and allocates the attribute name, so this patch will cause it to leak memory, which is
IMO still less of a problem than mkfs.ubifs failing entirely with a double free error
message, so I applied it for now.




======= Backtrace: =========

Signed-off-by: Yufen Yu <>
 ubifs-utils/mkfs.ubifs/mkfs.ubifs.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
index 6e11ec8..e0c42f3 100644
--- a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
+++ b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
@@ -1163,8 +1163,9 @@  static int add_xattr(struct ubifs_ino_node *host_ino, struct stat *st,
 	union ubifs_key xkey, nkey;
 	int len, ret;
- = name;
 	nm.len = strlen(name);
+ = xmalloc(nm.len + 1);
+	memcpy(, name, nm.len + 1);
 	host_ino->xattr_size += CALC_DENT_SIZE(nm.len);