From 11c514a99bb960941535134f0587102855e8ddee Mon Sep 17 00:00:00 2001
From: Jan Kara <jack@suse.cz>
Date: Mon, 2 Nov 2020 16:16:29 +0100
Subject: [PATCH] quota: Sanity-check quota file headers on load
Git-commit: 11c514a99bb960941535134f0587102855e8ddee
Patch-mainline: v5.11-rc1
References: bsc#1182461
Perform basic sanity checks of quota headers to avoid kernel crashes on
corrupted quota files.
Cc: stable@vger.kernel.org
Reported-by: syzbot+f816042a7ae2225f25ba@syzkaller.appspotmail.com
Reviewed-by: Andreas Dilger <adilger@dilger.ca>
Signed-off-by: Jan Kara <jack@suse.cz>
Acked-by: Jan Kara <jack@suse.cz>
---
fs/quota/quota_v2.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
--- a/fs/quota/quota_v2.c
+++ b/fs/quota/quota_v2.c
@@ -147,6 +147,24 @@ static int v2_read_file_info(struct supe
qinfo->dqi_entry_size = sizeof(struct v2r1_disk_dqblk);
qinfo->dqi_ops = &v2r1_qtree_ops;
}
+ /* 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;
+ }
+ 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;
+ }
+ 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;
+ }
return 0;
}