xref: /aosp_15_r20/external/e2fsprogs/lib/ext2fs/check_desc.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * check_desc.c --- Check the group descriptors of an ext2 filesystem
3*6a54128fSAndroid Build Coastguard Worker  *
4*6a54128fSAndroid Build Coastguard Worker  * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
5*6a54128fSAndroid Build Coastguard Worker  *
6*6a54128fSAndroid Build Coastguard Worker  * %Begin-Header%
7*6a54128fSAndroid Build Coastguard Worker  * This file may be redistributed under the terms of the GNU Library
8*6a54128fSAndroid Build Coastguard Worker  * General Public License, version 2.
9*6a54128fSAndroid Build Coastguard Worker  * %End-Header%
10*6a54128fSAndroid Build Coastguard Worker  */
11*6a54128fSAndroid Build Coastguard Worker 
12*6a54128fSAndroid Build Coastguard Worker #include "config.h"
13*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
14*6a54128fSAndroid Build Coastguard Worker #include <string.h>
15*6a54128fSAndroid Build Coastguard Worker #if HAVE_UNISTD_H
16*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
17*6a54128fSAndroid Build Coastguard Worker #endif
18*6a54128fSAndroid Build Coastguard Worker #include <fcntl.h>
19*6a54128fSAndroid Build Coastguard Worker #include <time.h>
20*6a54128fSAndroid Build Coastguard Worker #if HAVE_SYS_STAT_H
21*6a54128fSAndroid Build Coastguard Worker #include <sys/stat.h>
22*6a54128fSAndroid Build Coastguard Worker #endif
23*6a54128fSAndroid Build Coastguard Worker #if HAVE_SYS_TYPES_H
24*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
25*6a54128fSAndroid Build Coastguard Worker #endif
26*6a54128fSAndroid Build Coastguard Worker 
27*6a54128fSAndroid Build Coastguard Worker #include "ext2_fs.h"
28*6a54128fSAndroid Build Coastguard Worker #include "ext2fs.h"
29*6a54128fSAndroid Build Coastguard Worker 
30*6a54128fSAndroid Build Coastguard Worker /*
31*6a54128fSAndroid Build Coastguard Worker  * This routine sanity checks the group descriptors
32*6a54128fSAndroid Build Coastguard Worker  */
ext2fs_check_desc(ext2_filsys fs)33*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_check_desc(ext2_filsys fs)
34*6a54128fSAndroid Build Coastguard Worker {
35*6a54128fSAndroid Build Coastguard Worker 	ext2fs_block_bitmap bmap;
36*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
37*6a54128fSAndroid Build Coastguard Worker 	dgrp_t i;
38*6a54128fSAndroid Build Coastguard Worker 	blk64_t first_block = fs->super->s_first_data_block;
39*6a54128fSAndroid Build Coastguard Worker 	blk64_t last_block = ext2fs_blocks_count(fs->super)-1;
40*6a54128fSAndroid Build Coastguard Worker 	blk64_t blk, b;
41*6a54128fSAndroid Build Coastguard Worker 	unsigned int j;
42*6a54128fSAndroid Build Coastguard Worker 
43*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
44*6a54128fSAndroid Build Coastguard Worker 
45*6a54128fSAndroid Build Coastguard Worker 	if (EXT2_DESC_SIZE(fs->super) & (EXT2_DESC_SIZE(fs->super) - 1))
46*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_BAD_DESC_SIZE;
47*6a54128fSAndroid Build Coastguard Worker 
48*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_allocate_subcluster_bitmap(fs, "check_desc map", &bmap);
49*6a54128fSAndroid Build Coastguard Worker 	if (retval)
50*6a54128fSAndroid Build Coastguard Worker 		return retval;
51*6a54128fSAndroid Build Coastguard Worker 
52*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < fs->group_desc_count; i++)
53*6a54128fSAndroid Build Coastguard Worker 		ext2fs_reserve_super_and_bgd(fs, i, bmap);
54*6a54128fSAndroid Build Coastguard Worker 
55*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < fs->group_desc_count; i++) {
56*6a54128fSAndroid Build Coastguard Worker 		if (!ext2fs_has_feature_flex_bg(fs->super)) {
57*6a54128fSAndroid Build Coastguard Worker 			first_block = ext2fs_group_first_block2(fs, i);
58*6a54128fSAndroid Build Coastguard Worker 			last_block = ext2fs_group_last_block2(fs, i);
59*6a54128fSAndroid Build Coastguard Worker 		}
60*6a54128fSAndroid Build Coastguard Worker 
61*6a54128fSAndroid Build Coastguard Worker 		/*
62*6a54128fSAndroid Build Coastguard Worker 		 * Check to make sure the block bitmap for group is sane
63*6a54128fSAndroid Build Coastguard Worker 		 */
64*6a54128fSAndroid Build Coastguard Worker 		blk = ext2fs_block_bitmap_loc(fs, i);
65*6a54128fSAndroid Build Coastguard Worker 		if (blk < first_block || blk > last_block ||
66*6a54128fSAndroid Build Coastguard Worker 		    ext2fs_test_block_bitmap2(bmap, blk)) {
67*6a54128fSAndroid Build Coastguard Worker 			retval = EXT2_ET_GDESC_BAD_BLOCK_MAP;
68*6a54128fSAndroid Build Coastguard Worker 			goto errout;
69*6a54128fSAndroid Build Coastguard Worker 		}
70*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_block_bitmap2(bmap, blk);
71*6a54128fSAndroid Build Coastguard Worker 
72*6a54128fSAndroid Build Coastguard Worker 		/*
73*6a54128fSAndroid Build Coastguard Worker 		 * Check to make sure the inode bitmap for group is sane
74*6a54128fSAndroid Build Coastguard Worker 		 */
75*6a54128fSAndroid Build Coastguard Worker 		blk = ext2fs_inode_bitmap_loc(fs, i);
76*6a54128fSAndroid Build Coastguard Worker 		if (blk < first_block || blk > last_block ||
77*6a54128fSAndroid Build Coastguard Worker 		    ext2fs_test_block_bitmap2(bmap, blk)) {
78*6a54128fSAndroid Build Coastguard Worker 			retval = EXT2_ET_GDESC_BAD_INODE_MAP;
79*6a54128fSAndroid Build Coastguard Worker 			goto errout;
80*6a54128fSAndroid Build Coastguard Worker 		}
81*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_block_bitmap2(bmap, blk);
82*6a54128fSAndroid Build Coastguard Worker 
83*6a54128fSAndroid Build Coastguard Worker 		/*
84*6a54128fSAndroid Build Coastguard Worker 		 * Check to make sure the inode table for group is sane
85*6a54128fSAndroid Build Coastguard Worker 		 */
86*6a54128fSAndroid Build Coastguard Worker 		blk = ext2fs_inode_table_loc(fs, i);
87*6a54128fSAndroid Build Coastguard Worker 		if (blk < first_block ||
88*6a54128fSAndroid Build Coastguard Worker 		    ((blk + fs->inode_blocks_per_group - 1) > last_block)) {
89*6a54128fSAndroid Build Coastguard Worker 			retval = EXT2_ET_GDESC_BAD_INODE_TABLE;
90*6a54128fSAndroid Build Coastguard Worker 			goto errout;
91*6a54128fSAndroid Build Coastguard Worker 		}
92*6a54128fSAndroid Build Coastguard Worker 		for (j = 0, b = blk; j < fs->inode_blocks_per_group;
93*6a54128fSAndroid Build Coastguard Worker 		     j++, b++) {
94*6a54128fSAndroid Build Coastguard Worker 			if (ext2fs_test_block_bitmap2(bmap, b)) {
95*6a54128fSAndroid Build Coastguard Worker 				retval = EXT2_ET_GDESC_BAD_INODE_TABLE;
96*6a54128fSAndroid Build Coastguard Worker 				goto errout;
97*6a54128fSAndroid Build Coastguard Worker 			}
98*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_block_bitmap2(bmap, b);
99*6a54128fSAndroid Build Coastguard Worker 		}
100*6a54128fSAndroid Build Coastguard Worker 	}
101*6a54128fSAndroid Build Coastguard Worker errout:
102*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_block_bitmap(bmap);
103*6a54128fSAndroid Build Coastguard Worker 	return retval;
104*6a54128fSAndroid Build Coastguard Worker }
105