File tree Expand file tree Collapse file tree 1 file changed +21
-7
lines changed Expand file tree Collapse file tree 1 file changed +21
-7
lines changed Original file line number Diff line number Diff line change @@ -353,13 +353,27 @@ where
353353 let entry_size = rela_ent. ok_or ( "RelaEnt entry is missing" ) ?;
354354
355355 // Apply the mappings.
356- let entries = total_size / entry_size;
357- let relas = unsafe {
358- core:: slice:: from_raw_parts :: < Rela < u64 > > (
359- elf_file. input . as_ptr ( ) . add ( offset as usize ) . cast ( ) ,
360- entries as usize ,
361- )
362- } ;
356+ let entries = ( total_size / entry_size) as usize ;
357+ let rela_start = elf_file
358+ . input
359+ . as_ptr ( )
360+ . wrapping_add ( offset as usize )
361+ . cast :: < Rela < u64 > > ( ) ;
362+
363+ // Make sure the relocations are inside the elf file.
364+ let rela_end = rela_start. wrapping_add ( entries) ;
365+ assert ! ( rela_start <= rela_end) ;
366+ let file_ptr_range = elf_file. input . as_ptr_range ( ) ;
367+ assert ! (
368+ file_ptr_range. start <= rela_start. cast( ) ,
369+ "the relocation table must start in the elf file"
370+ ) ;
371+ assert ! (
372+ rela_end. cast( ) <= file_ptr_range. end,
373+ "the relocation table must end in the elf file"
374+ ) ;
375+
376+ let relas = unsafe { core:: slice:: from_raw_parts ( rela_start, entries) } ;
363377 for rela in relas {
364378 let idx = rela. get_symbol_table_index ( ) ;
365379 assert_eq ! (
You can’t perform that action at this time.
0 commit comments