提交 8a64eb43 编写于 作者: Z Zhihao Cheng 提交者: Zheng Zengkai

quota: Add more checking after reading from quota file

hulk inclusion
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I5QH0X
CVE: NA

--------------------------------

It would be better to do more sanity checking (eg. dqdh_entries,
block no.) for the content read from quota file, which can prevent
corrupting the quota file.
Signed-off-by: NZhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: NLi Lingfeng <lilingfeng3@huawei.com>
Reviewed-by: NZhihao Cheng <chengzhihao1@huawei.com>
Reviewed-by: NZhang Yi <yi.zhang@huawei.com>
Signed-off-by: NZheng Zengkai <zhengzengkai@huawei.com>
上级 1043f89a
...@@ -80,12 +80,12 @@ static ssize_t write_blk(struct qtree_mem_dqinfo *info, uint blk, char *buf) ...@@ -80,12 +80,12 @@ static ssize_t write_blk(struct qtree_mem_dqinfo *info, uint blk, char *buf)
return ret; return ret;
} }
static inline int do_check_range(struct super_block *sb, uint val, static inline int do_check_range(struct super_block *sb, const char *val_name,
uint min_val, uint max_val) uint val, uint min_val, uint max_val)
{ {
if (val < min_val || val >= max_val) { if (val < min_val || val >= max_val) {
quota_error(sb, "Getting block %u out of range %u-%u", quota_error(sb, "Getting %s %u out of range %u-%u",
val, min_val, max_val); val_name, val, min_val, max_val);
return -EUCLEAN; return -EUCLEAN;
} }
...@@ -99,11 +99,13 @@ static int check_free_block(struct qtree_mem_dqinfo *info, ...@@ -99,11 +99,13 @@ static int check_free_block(struct qtree_mem_dqinfo *info,
uint nextblk, prevblk; uint nextblk, prevblk;
nextblk = le32_to_cpu(dh->dqdh_next_free); nextblk = le32_to_cpu(dh->dqdh_next_free);
err = do_check_range(info->dqi_sb, nextblk, 0, info->dqi_blocks); err = do_check_range(info->dqi_sb, "dqdh_next_free", nextblk, 0,
info->dqi_blocks);
if (err) if (err)
return err; return err;
prevblk = le32_to_cpu(dh->dqdh_prev_free); prevblk = le32_to_cpu(dh->dqdh_prev_free);
err = do_check_range(info->dqi_sb, prevblk, 0, info->dqi_blocks); err = do_check_range(info->dqi_sb, "dqdh_prev_free", prevblk, 0,
info->dqi_blocks);
if (err) if (err)
return err; return err;
...@@ -277,6 +279,11 @@ static uint find_free_dqentry(struct qtree_mem_dqinfo *info, ...@@ -277,6 +279,11 @@ static uint find_free_dqentry(struct qtree_mem_dqinfo *info,
*err = check_free_block(info, dh); *err = check_free_block(info, dh);
if (*err) if (*err)
goto out_buf; goto out_buf;
*err = do_check_range(info->dqi_sb, "dqdh_entries",
le16_to_cpu(dh->dqdh_entries), 0,
qtree_dqstr_in_blk(info));
if (*err)
goto out_buf;
} else { } else {
blk = get_free_dqblk(info); blk = get_free_dqblk(info);
if ((int)blk < 0) { if ((int)blk < 0) {
...@@ -358,6 +365,10 @@ static int do_insert_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot, ...@@ -358,6 +365,10 @@ static int do_insert_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot,
} }
ref = (__le32 *)buf; ref = (__le32 *)buf;
newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]); newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
ret = do_check_range(dquot->dq_sb, "block", newblk, 0,
info->dqi_blocks);
if (ret)
goto out_buf;
if (!newblk) if (!newblk)
newson = 1; newson = 1;
if (depth == info->dqi_qtree_depth - 1) { if (depth == info->dqi_qtree_depth - 1) {
...@@ -470,6 +481,11 @@ static int free_dqentry(struct qtree_mem_dqinfo *info, struct dquot *dquot, ...@@ -470,6 +481,11 @@ static int free_dqentry(struct qtree_mem_dqinfo *info, struct dquot *dquot,
} }
dh = (struct qt_disk_dqdbheader *)buf; dh = (struct qt_disk_dqdbheader *)buf;
ret = check_free_block(info, dh); ret = check_free_block(info, dh);
if (ret)
goto out_buf;
ret = do_check_range(info->dqi_sb, "dqdh_entries",
le16_to_cpu(dh->dqdh_entries), 1,
qtree_dqstr_in_blk(info) + 1);
if (ret) if (ret)
goto out_buf; goto out_buf;
le16_add_cpu(&dh->dqdh_entries, -1); le16_add_cpu(&dh->dqdh_entries, -1);
...@@ -528,7 +544,7 @@ static int remove_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot, ...@@ -528,7 +544,7 @@ static int remove_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot,
goto out_buf; goto out_buf;
} }
newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]); newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
ret = do_check_range(dquot->dq_sb, newblk, QT_TREEOFF, ret = do_check_range(dquot->dq_sb, "block", newblk, QT_TREEOFF,
info->dqi_blocks); info->dqi_blocks);
if (ret) if (ret)
goto out_buf; goto out_buf;
...@@ -632,7 +648,8 @@ static loff_t find_tree_dqentry(struct qtree_mem_dqinfo *info, ...@@ -632,7 +648,8 @@ static loff_t find_tree_dqentry(struct qtree_mem_dqinfo *info,
blk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]); blk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
if (!blk) /* No reference? */ if (!blk) /* No reference? */
goto out_buf; goto out_buf;
ret = do_check_range(dquot->dq_sb, blk, QT_TREEOFF, info->dqi_blocks); ret = do_check_range(dquot->dq_sb, "block", blk, QT_TREEOFF,
info->dqi_blocks);
if (ret) if (ret)
goto out_buf; goto out_buf;
...@@ -748,7 +765,13 @@ static int find_next_id(struct qtree_mem_dqinfo *info, qid_t *id, ...@@ -748,7 +765,13 @@ static int find_next_id(struct qtree_mem_dqinfo *info, qid_t *id,
goto out_buf; goto out_buf;
} }
for (i = __get_index(info, *id, depth); i < epb; i++) { for (i = __get_index(info, *id, depth); i < epb; i++) {
if (ref[i] == cpu_to_le32(0)) { uint blk_no = le32_to_cpu(ref[i]);
ret = do_check_range(info->dqi_sb, "block", blk_no, 0,
info->dqi_blocks);
if (ret)
goto out_buf;
if (blk_no == 0) {
*id += level_inc; *id += level_inc;
continue; continue;
} }
...@@ -756,7 +779,7 @@ static int find_next_id(struct qtree_mem_dqinfo *info, qid_t *id, ...@@ -756,7 +779,7 @@ static int find_next_id(struct qtree_mem_dqinfo *info, qid_t *id,
ret = 0; ret = 0;
goto out_buf; goto out_buf;
} }
ret = find_next_id(info, id, le32_to_cpu(ref[i]), depth + 1); ret = find_next_id(info, id, blk_no, depth + 1);
if (ret != -ENOENT) if (ret != -ENOENT)
break; break;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册