Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Validate WASM before extraction also
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint committed Aug 9, 2024
1 parent a9d2f1b commit bcf6d94
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions wasmextractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,20 @@ func ReadWasmFile(wasmFilePath string) ([]byte, error) {
return nil, fmt.Errorf("error reading wasm file: %v", err)
}

magic := []byte{0x00, 0x61, 0x73, 0x6D} // "\0asm"
if len(wasmBytes) < 8 || !bytes.Equal(wasmBytes[:4], magic) {
return nil, fmt.Errorf("invalid wasm file")
}

if binary.LittleEndian.Uint32(wasmBytes[4:8]) != 1 {
return nil, fmt.Errorf("unsupported wasm version")
if err := validateWasm(wasmBytes); err != nil {
return nil, err
}

return wasmBytes, nil
}

func ExtractWasmInfo(wasmBytes []byte) (*WasmInfo, error) {
info := &WasmInfo{}

if err := validateWasm(wasmBytes); err != nil {
return nil, err
}

info := &WasmInfo{}
offset := 8
for offset < len(wasmBytes) {
sectionID := wasmBytes[offset]
Expand All @@ -94,6 +93,21 @@ func ExtractWasmInfo(wasmBytes []byte) (*WasmInfo, error) {
return info, nil
}

var magic = []byte{0x00, 0x61, 0x73, 0x6D} // "\0asm"

func validateWasm(data []byte) error {

if len(data) < 8 || !bytes.Equal(data[:4], magic) {
return fmt.Errorf("invalid wasm file")
}

if binary.LittleEndian.Uint32(data[4:8]) != 1 {
return fmt.Errorf("unsupported wasm version")
}

return nil
}

func readImports(data []byte) []WasmItem {

numItems, n := binary.Uvarint(data)
Expand Down

0 comments on commit bcf6d94

Please sign in to comment.