# GHSA-355R-R4C4-7VR8: 1.24.0: heap corruption when a Scanner scans on more than one OS thread (C API, Go)

- Database: GitHub
- Severity: medium (stated by ghsa, no CVSS score)
- Published: 2026-11-26
- Modified: 2026-11-26

## Affected versions

- yara-x (crates.io): 1.24.0
- github.com/VirusTotal/yara-x/go (Go): 1.24.0

## Description

## Summary

Since v1.24.0, a `Scanner` that scans on more than one OS thread during its lifetime corrupts the heap. The scanner is never used by two threads at once.

The Go binding hits this in its documented usage, one scanner per goroutine, because Go moves goroutines between OS threads and `Scan` locks the thread only for the duration of the call. v1.23.0 is not affected.

It needs a rule that imports a module. A rule set without imports does not crash.

## Reproducer

`main.go`:

```go
package main

import (
	"flag"
	"fmt"
	"runtime"
	"sync"

	yarax "github.com/VirusTotal/yara-x/go"
)

const rule = `
import "pe"

rule uses_pe {
  condition:
    pe.number_of_sections > 100
}`

func main() {
	workers := flag.Int("workers", 16, "goroutines, one Scanner each")
	scans := flag.Int("scans", 20000, "scans per goroutine")
	pin := flag.Bool("pin", false, "lock each goroutine to its OS thread")
	flag.Parse()

	rules, err := yarax.Compile(rule)
	if err != nil {
		panic(err)
	}
	data := []byte("not a PE file")

	var wg sync.WaitGroup
	for i := 0; i < *workers; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			if *pin {
				runtime.LockOSThread()
				defer runtime.UnlockOSThread()
			}
			s := yarax.NewScanner(rules)
			defer s.Destroy()
			for j := 0; j < *scans; j++ {
				if _, err := s.Scan(data); err != nil {
					panic(err)
				}
				runtime.Gosched() // encourage migration between threads
			}
		}()
	}
	wg.Wait()
	rules.Destroy()
	fmt.Printf("ok: %d goroutines x %d scans, pin=%v\n", *workers, *scans, *pin)
}
```

`go.mod` requires `github.com/VirusTotal/yara-x/go v1.24.0`. The binding's Go code is the same at `go/v1.23.0` and `go/v1.24.0`, so the same program was linked against each library version.

## Results

Linux x86_64, glibc 2.44, Go 1.27.4, 16 cores, `libyara_x_capi.a` from the Wolfi `yara-x` packages.

| Library | Flags | Result |
|---|---|---|
| 1.24.0 | (default: 16 goroutines) | 20 of 20 runs abort |
| 1.24.0 | `-pin` | 0 of 20 abort |
| 1.24.0 | `-workers 1` | 0 of 10 abort |
| 1.23.0 | (default) | 0 of 20 abort |

The same program with a rule that imports nothing (one regexp string) ran 40 of 40 times clean on 1.24.0.

## Symptoms

The process dies inside `yrx_scanner_scan` with one of:

- `malloc(): unaligned tcache chunk detected` (11 of 20)
- `SIGSEGV: segmentation violation` (5 of 20)
- `corrupted double-linked list` (3 of 20)
- `free(): invalid pointer` (1 of 20)

With a large rule set (about 1,200 rule files, several module imports) the same runs also end in Rust panics at `lib/src/types/array.rs:71` and `lib/src/types/array.rs:75:53`, and in aborts inside `yrx_rule_iter_metadata` and `yrx_compiler_add_source_with_origin` on other threads.

```
free(): invalid pointer
SIGABRT: abort
signal arrived during cgo execution

goroutine 33 [syscall, locked to thread]:
runtime.cgocall(...)
github.com/VirusTotal/yara-x/go._Cfunc_yrx_scanner_scan(0x7f3cc80105f0, 0x2b5cf729c070, 0xd)
github.com/VirusTotal/yara-x/go.(*Scanner).Scan(...)
	github.com/VirusTotal/yara-x/go@v1.24.0/scanner.go:255
main.main.func1()
	main.go:50
```

## Likely cause

This is from reading the source. I have not confirmed it with a patched build or a sanitizer.

02583789f ("perf: reduce memory usage and improve performance of Struct and dotnet module") added thread-local caches of `Rc` values in `lib/src/types/structure.rs`:

- `PROTO_STRUCT_SCHEMAS`, whose `field_meta: Rc<IndexMap<..>>` is cloned into every `Struct` built from a protobuf message.
- `EMPTY_ARRAY_INTEGERS`, `EMPTY_ARRAY_FLOATS`, `EMPTY_ARRAY_BOOLS`, `EMPTY_ARRAY_STRINGS`, `EMPTY_ARRAY_STRUCTS`, cloned into a struct for each empty repeated field.

A module struct built during a scan on thread A holds clones of thread A's `Rc`s. When that scanner next scans on thread B, or is destroyed there, those clones are dropped on B while thread A clones and drops the same `Rc`s for another scanner. The reference count is not atomic, so updates are lost and the value is freed early or twice.

That matches what the reproducer shows: it needs a module import, it needs two or more threads, keeping each scanner on one thread avoids it, and 1.23.0 has none of these thread-locals.

## Why it matters for the bindings

- `go/scanner.go` documents `NewScanner` as: "use each scanner on a separate goroutine for performing multiple scans in parallel". A Go program cannot keep a goroutine on one OS thread without `runtime.LockOSThread` for the goroutine's whole life, and a scanner pool hands scanners to whichever goroutine asks next.
- `yara_x.h` says of `yrx_scanner_create`: "use each scanner from a different thread". If a scanner is now meant to stay on the thread that first used it, the header and the Go binding need to say so and enforce it. Before 1.24.0, moving a scanner between threads worked.

A cache owned by the `Rules` or the scanner, or `Arc` in place of `Rc` for the shared values, would remove the thread affinity.

## Disclosure

Reported privately because it is heap corruption in a library that scans untrusted input. The trigger is thread scheduling, not file content, so I have not rated the severity. I am fine with this being handled as a public issue if you prefer. A downstream project has already said publicly that 1.24.0 aborts with heap corruption under concurrent scans; the trigger and the cause above are not public.

## References

- https://github.com/VirusTotal/yara-x/security/advisories/GHSA-355r-r4c4-7vr8

Page: https://cve.now/GHSA-355R-R4C4-7VR8

Wrong or missing data? Send feedback: POST https://cve.now/api/v1/feedback (see https://cve.now/agents#feedback).
