File tree Expand file tree Collapse file tree
uefi/src/proto/device_path Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -358,7 +358,8 @@ impl<'a> TryFrom<&'a [u8]> for &'a DevicePathNode {
358358
359359 fn try_from ( bytes : & [ u8 ] ) -> Result < Self , Self :: Error > {
360360 let dp = <& DevicePathHeader >:: try_from ( bytes) ?;
361- if usize:: from ( dp. length ( ) ) <= bytes. len ( ) {
361+ let length = usize:: from ( dp. length ( ) ) ;
362+ if length >= size_of :: < DevicePathHeader > ( ) && length <= bytes. len ( ) {
362363 unsafe { Ok ( DevicePathNode :: from_ffi_ptr ( bytes. as_ptr ( ) . cast ( ) ) ) }
363364 } else {
364365 Err ( ByteConversionError :: InvalidLength )
@@ -1123,6 +1124,10 @@ mod tests {
11231124 // [`DevicePathNode`] data length exceeds the raw_data slice.
11241125 raw_data[ 2 ] += 1 ;
11251126 assert ! ( <& DevicePathNode >:: try_from( raw_data. as_slice( ) ) . is_err( ) ) ;
1127+
1128+ // [`DevicePathNode`] data length is shorter than the fixed header.
1129+ raw_data[ 2 ..4 ] . copy_from_slice ( & 3_u16 . to_le_bytes ( ) ) ;
1130+ assert ! ( <& DevicePathNode >:: try_from( raw_data. as_slice( ) ) . is_err( ) ) ;
11261131 }
11271132
11281133 #[ test]
@@ -1149,6 +1154,18 @@ mod tests {
11491154 assert_eq ! ( nodes. len( ) , 5 ) ;
11501155 }
11511156
1157+ #[ test]
1158+ fn test_device_path_from_bytes_rejects_short_node_length ( ) {
1159+ #[ rustfmt:: skip]
1160+ let raw_data = [
1161+ 0xa0 , 0xb0 ,
1162+ // Length shorter than the fixed header.
1163+ 0x03 , 0x00 ,
1164+ ] ;
1165+
1166+ assert ! ( <& DevicePath >:: try_from( raw_data. as_slice( ) ) . is_err( ) ) ;
1167+ }
1168+
11521169 /// Test converting from `&DevicePathNode` to a specific node type.
11531170 #[ test]
11541171 fn test_specific_node_from_device_path_node ( ) {
You can’t perform that action at this time.
0 commit comments