File tree 5 files changed +36
-1
lines changed
5 files changed +36
-1
lines changed Original file line number Diff line number Diff line change 1
1
# Changelog
2
2
3
+ ## Unreleased
4
+
5
+ * Fixed a panic when loading an invalid superblock.
6
+
3
7
## 0.6.0
4
8
5
9
* MSRV increased to ` 1.81 ` .
Original file line number Diff line number Diff line change @@ -154,6 +154,9 @@ pub enum Corrupt {
154
154
/// Superblock checksum is invalid.
155
155
SuperblockChecksum ,
156
156
157
+ /// The block size in the superblock is invalid.
158
+ InvalidBlockSize ,
159
+
157
160
/// The number of block groups does not fit in a [`u32`].
158
161
TooManyBlockGroups ,
159
162
@@ -232,6 +235,7 @@ impl Display for Corrupt {
232
235
Self :: SuperblockChecksum => {
233
236
write ! ( f, "invalid superblock checksum" )
234
237
}
238
+ Self :: InvalidBlockSize => write ! ( f, "invalid block size" ) ,
235
239
Self :: TooManyBlockGroups => write ! ( f, "too many block groups" ) ,
236
240
Self :: BlockGroupDescriptor ( block_group_num) => {
237
241
write ! ( f, "block group descriptor {block_group_num} is invalid" )
Original file line number Diff line number Diff line change @@ -65,7 +65,8 @@ impl Superblock {
65
65
66
66
let blocks_count = u64_from_hilo ( s_blocks_count_hi, s_blocks_count_lo) ;
67
67
68
- let block_size = 2u32 . pow ( 10 + s_log_block_size) ;
68
+ let block_size = calc_block_size ( s_log_block_size)
69
+ . ok_or ( Ext4Error :: Corrupt ( Corrupt :: InvalidBlockSize ) ) ?;
69
70
70
71
if s_magic != 0xef53 {
71
72
return Err ( Ext4Error :: Corrupt ( Corrupt :: SuperblockMagic ) ) ;
@@ -132,6 +133,11 @@ impl Superblock {
132
133
}
133
134
}
134
135
136
+ fn calc_block_size ( s_log_block_size : u32 ) -> Option < u32 > {
137
+ let exp = s_log_block_size. checked_add ( 10 ) ?;
138
+ 2u32 . checked_pow ( exp)
139
+ }
140
+
135
141
fn check_incompat_features (
136
142
s_feature_incompat : u32 ,
137
143
) -> Result < IncompatibleFeatures , Incompatible > {
Original file line number Diff line number Diff line change
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e2040d8cde75222a96eecd1e066e546cc0dd51435cdf5897e0d7379b6d6409b8
3
+ size 1024
Original file line number Diff line number Diff line change @@ -76,6 +76,24 @@ fn test_load_path_error() {
76
76
) ) ;
77
77
}
78
78
79
+ /// Test that loading the data from
80
+ /// https://github.com/nicholasbishop/ext4-view-rs/issues/280 does not
81
+ /// panic.
82
+ #[ test]
83
+ fn test_invalid_ext4_data ( ) {
84
+ // Fill in zeros for the first 1024 bytes, then add the test data.
85
+ let mut data = vec ! [ 0 ; 1024 ] ;
86
+ data. extend ( include_bytes ! ( "../../test_data/not_ext4.bin" ) ) ;
87
+
88
+ assert_eq ! (
89
+ * Ext4 :: load( Box :: new( data) )
90
+ . unwrap_err( )
91
+ . as_corrupt( )
92
+ . unwrap( ) ,
93
+ Corrupt :: InvalidBlockSize
94
+ ) ;
95
+ }
96
+
79
97
#[ test]
80
98
fn test_canonicalize ( ) {
81
99
let fs = load_test_disk1 ( ) ;
You can’t perform that action at this time.
0 commit comments