From a4db1072e1a3bd7a8d9c356e1902b13ac5deb8ef Mon Sep 17 00:00:00 2001
From: Jan Kara <jack@suse.cz>
Date: Tue, 22 Dec 2020 12:09:53 +0100
Subject: [PATCH] quota: Fix memory leak when handling corrupted quota file
Git-commit: a4db1072e1a3bd7a8d9c356e1902b13ac5deb8ef
Patch-mainline: v5.12-rc1
References: bsc#1182650
When checking corrupted quota file we can bail out and leak allocated
info structure. Properly free info structure on error return.
Reported-by: syzbot+77779c9b52ab78154b08@syzkaller.appspotmail.com
Fixes: 11c514a99bb9 ("quota: Sanity-check quota file headers on load")
Signed-off-by: Jan Kara <jack@suse.cz>
Acked-by: Jan Kara <jack@suse.cz>
---
fs/quota/quota_v2.c | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
--- a/fs/quota/quota_v2.c
+++ b/fs/quota/quota_v2.c
@@ -94,6 +94,7 @@ static int v2_read_file_info(struct supe
struct qtree_mem_dqinfo *qinfo;
ssize_t size;
unsigned int version;
+ int ret;
if (!v2_read_header(sb, type, &dqhead))
return -EIO;
@@ -147,25 +148,32 @@ static int v2_read_file_info(struct supe
qinfo->dqi_entry_size = sizeof(struct v2r1_disk_dqblk);
qinfo->dqi_ops = &v2r1_qtree_ops;
}
+ ret = -EUCLEAN;
/* Some sanity checks of the read headers... */
if ((loff_t)qinfo->dqi_blocks << qinfo->dqi_blocksize_bits >
i_size_read(sb_dqopt(sb)->files[type])) {
quota_error(sb, "Number of blocks too big for quota file size (%llu > %llu).",
(loff_t)qinfo->dqi_blocks << qinfo->dqi_blocksize_bits,
i_size_read(sb_dqopt(sb)->files[type]));
- return -EUCLEAN;
+ goto out_free;
}
if (qinfo->dqi_free_blk >= qinfo->dqi_blocks) {
quota_error(sb, "Free block number too big (%u >= %u).",
qinfo->dqi_free_blk, qinfo->dqi_blocks);
- return -EUCLEAN;
+ goto out_free;
}
if (qinfo->dqi_free_entry >= qinfo->dqi_blocks) {
quota_error(sb, "Block with free entry too big (%u >= %u).",
qinfo->dqi_free_entry, qinfo->dqi_blocks);
- return -EUCLEAN;
+ goto out_free;
}
- return 0;
+ ret = 0;
+out_free:
+ if (ret) {
+ kfree(info->dqi_priv);
+ info->dqi_priv = NULL;
+ }
+ return ret;
}
/* Write information header to quota file */