@@ -35,28 +35,39 @@ export const parseExecutableHeader = (
3535 bytes . length >= 4 &&
3636 bytes . subarray ( 0 , 4 ) . equals ( Buffer . from ( [ 0x7f , 0x45 , 0x4c , 0x46 ] ) )
3737 )
38- return parseElf ( bytes ) ;
38+ return parseElf ( bytes , fileSize ) ;
3939 if ( bytes . length >= 2 && bytes [ 0 ] === 0x4d && bytes [ 1 ] === 0x5a )
4040 return parseMz ( bytes , fileSize ) ;
4141 if ( bytes . length < 8 ) return err ( "truncated or unsupported binary header" ) ;
4242 const magic = bytes . readUInt32BE ( 0 ) ;
4343 if ( [ 0xfeedface , 0xfeedfacf , 0xcefaedfe , 0xcffaedfe ] . includes ( magic ) )
44- return parseThinMachO ( bytes , magic ) ;
44+ return parseThinMachO ( bytes , magic , fileSize ) ;
4545 if ( [ 0xcafebabe , 0xbebafeca , 0xcafebabf , 0xbfbafeca ] . includes ( magic ) )
46- return parseFatMachO ( bytes , magic , hostArchitecture ) ;
46+ return parseFatMachO ( bytes , magic , hostArchitecture , fileSize ) ;
4747 return err ( "unsupported binary format" ) ;
4848} ;
4949
5050const parseThinMachO = (
5151 bytes : Buffer ,
5252 magic : number ,
53+ fileSize : number ,
5354) : Result < ExecutableMetadata , string > => {
54- if ( bytes . length < 8 ) return err ( "truncated Mach-O header" ) ;
55+ const is64 = magic === 0xfeedfacf || magic === 0xcffaedfe ;
56+ const headerSize = is64 ? 32 : 28 ;
57+ if ( bytes . length < headerSize )
58+ return err (
59+ `truncated Mach-O header: a ${ is64 ? 64 : 32 } -bit header needs ${ headerSize } bytes; the file has ${ fileSize } ` ,
60+ ) ;
5561 const little = magic === 0xcefaedfe || magic === 0xcffaedfe ;
56- const architecture = machArchitecture (
57- little ? bytes . readUInt32LE ( 4 ) : bytes . readUInt32BE ( 4 ) ,
58- ) ;
62+ const read = ( offset : number ) : number =>
63+ little ? bytes . readUInt32LE ( offset ) : bytes . readUInt32BE ( offset ) ;
64+ const architecture = machArchitecture ( read ( 4 ) ) ;
5965 if ( architecture === undefined ) return err ( "unsupported Mach-O architecture" ) ;
66+ const commandBytes = read ( 20 ) ;
67+ if ( headerSize + commandBytes > fileSize )
68+ return err (
69+ `truncated Mach-O load commands: the header declares ${ commandBytes } bytes after its ${ headerSize } -byte header; the file has ${ fileSize } ` ,
70+ ) ;
6071 return ok ( {
6172 format : "mach-o" ,
6273 architecture,
@@ -68,6 +79,7 @@ const parseFatMachO = (
6879 bytes : Buffer ,
6980 magic : number ,
7081 host : NodeJS . Architecture ,
82+ fileSize : number ,
7183) : Result < ExecutableMetadata , string > => {
7284 const little = magic === 0xbebafeca || magic === 0xbfbafeca ;
7385 const is64 = magic === 0xcafebabf || magic === 0xbfbafeca ;
@@ -79,10 +91,24 @@ const parseFatMachO = (
7991 if ( count === 0 || count > 128 || bytes . length < 8 + count * entrySize )
8092 return err ( "truncated or invalid FAT architecture table" ) ;
8193 const architectures : BinaryArchitecture [ ] = [ ] ;
94+ const sliceEnds = new Map < BinaryArchitecture , bigint > ( ) ;
8295 for ( let index = 0 ; index < count ; index += 1 ) {
83- const architecture = machArchitecture ( read ( 8 + index * entrySize ) ) ;
84- if ( architecture !== undefined && ! architectures . includes ( architecture ) )
85- architectures . push ( architecture ) ;
96+ const entry = 8 + index * entrySize ;
97+ const architecture = machArchitecture ( read ( entry ) ) ;
98+ if ( architecture === undefined || architectures . includes ( architecture ) )
99+ continue ;
100+ architectures . push ( architecture ) ;
101+ const offset = is64
102+ ? little
103+ ? bytes . readBigUInt64LE ( entry + 8 )
104+ : bytes . readBigUInt64BE ( entry + 8 )
105+ : BigInt ( read ( entry + 8 ) ) ;
106+ const size = is64
107+ ? little
108+ ? bytes . readBigUInt64LE ( entry + 16 )
109+ : bytes . readBigUInt64BE ( entry + 16 )
110+ : BigInt ( read ( entry + 12 ) ) ;
111+ sliceEnds . set ( architecture , offset + size ) ;
86112 }
87113 const preferred =
88114 host === "arm64"
@@ -96,18 +122,31 @@ const parseFatMachO = (
96122 : undefined ;
97123 if ( preferred === undefined || ! architectures . includes ( preferred ) )
98124 return err ( `FAT binary has no host-compatible ${ host } architecture` ) ;
125+ const sliceEnd = sliceEnds . get ( preferred ) ?? 0n ;
126+ if ( sliceEnd > BigInt ( fileSize ) )
127+ return err (
128+ `truncated FAT slice: the ${ preferred } slice ends at byte ${ sliceEnd } ; the file has ${ fileSize } ` ,
129+ ) ;
99130 return ok ( {
100131 format : "mach-o" ,
101132 architecture : preferred ,
102133 availableArchitectures : architectures ,
103134 } ) ;
104135} ;
105136
106- const parseElf = ( bytes : Buffer ) : Result < ExecutableMetadata , string > => {
107- if ( bytes . length < 20 ) return err ( "truncated ELF header" ) ;
137+ const parseElf = (
138+ bytes : Buffer ,
139+ fileSize : number ,
140+ ) : Result < ExecutableMetadata , string > => {
141+ if ( bytes . length < 6 ) return err ( "truncated ELF header" ) ;
108142 if ( bytes [ 4 ] !== 1 && bytes [ 4 ] !== 2 ) return err ( "unsupported ELF class" ) ;
109143 const little = bytes [ 5 ] === 1 ;
110144 if ( ! little && bytes [ 5 ] !== 2 ) return err ( "unsupported ELF endianness" ) ;
145+ const headerSize = bytes [ 4 ] === 1 ? 52 : 64 ;
146+ if ( bytes . length < headerSize )
147+ return err (
148+ `truncated ELF header: an ELF${ bytes [ 4 ] === 1 ? 32 : 64 } header needs ${ headerSize } bytes; the file has ${ fileSize } ` ,
149+ ) ;
111150 const machine = little ? bytes . readUInt16LE ( 18 ) : bytes . readUInt16BE ( 18 ) ;
112151 const architecture = elfArchitecture ( machine ) ;
113152 if ( architecture === undefined ) return err ( "unsupported ELF architecture" ) ;
0 commit comments