Repository navigation
Description
Summary
The entry file can register a constructor in a sibling module's canonical namespace by importing that module under a different alias and declaring a name such as lib.None. CID(None) in the sibling's trusted foreign source is resolved only after all files have loaded, so it binds to the entry file's injected constructor instead of Base's None.
This is more than a name collision or wrong result. It lets Bend-only code control the runtime tags emitted by trusted C/JS effects. On the native backend, a captured tag can disagree with both the value's actual allocation and the type through which Bend consumes it. The reproducer below makes generated C:
- read allocator metadata at corpus slot 0 as a nonexistent
Some.value; and - pass slot 0 to
heap_free, which overwritesH_BUMP.
An arity variant described below makes a normal two-word list node carry an attacker-declared three-field tag. From the emitted term_drop implementation, dropping that value reads beyond the node and returns its two-word allocation to the four-word free list. The attacker supplies no C or JS; the foreign library is correct in isolation.
This is therefore a native memory-corruption issue under a trust model where an entry .bend file may use approved foreign libraries but may not provide native code itself. JS has the same namespace capture and type confusion, without the native allocator corruption.
Threat model and preconditions
An attacker needs control of the entry .bend file and access to a trusted local sibling module containing a foreign effect. The entry file:
- imports
./lib.bendunder an alias other thanlib; - declares
lib.X, where the library effect source containsCID(X)intended to fall back to a Base constructor; and - calls that trusted effect.
No attacker-authored .c or .js file is needed. The normal "relies on unsafe or foreign code" warning is still printed because main calls the trusted effect, but it does not distinguish this capture from the library's intended ABI.
The reachable namespace is narrow but useful: the target must be a sibling of the entry file whose canonical namespace contains no /. By inspection of the declared-name grammar, subdirectory and hub-package namespaces cannot be spelled by a declared Bend name. I did not separately execute a hub-package case.
Reproducer
Put these three files in one directory. lib.bend is an ordinary library with a correct foreign effect:
import Base
def peek() -> IO(Maybe<U32>):
import "./peek.js"
import "./peek.c"
peek.js:
function peek() {
return { $: CID(None) };
}
io_eff(CID(peek), peek);peek.c:
Term peek_run(Env e, Term* f, IoWork* w) {
return term_pak(CID(None), 0);
}
static void __attribute__((constructor)) peek_use(void) {
io_eff(CID(peek), peek_run, 0);
}main.bend imports the library under an alias other than its file name and declares constructors spelled in lib's namespace:
import Base
import ./lib.bend as Q
type Evil is Data:
lib.A{}
lib.B{}
lib.None{}
def tag(e: Evil) -> String:
match e:
case lib.A{}:
"a"
case lib.B{}:
"b"
case lib.None{}:
"n"
def show(m: Maybe<U32>) -> String:
match m:
case None{}:
"None"
case Some{v}:
"Some " ++ U32.show(v)
def main() -> IO(Unit):
do IO<Unit>:
m : Maybe<U32> <- Q.peek()
IO.print(show(m) ++ tag(lib.None{}))
Results
without type Evil (show(m) only) |
with type Evil |
|
|---|---|---|
bun bend2/main.ts main.bend |
None |
Some n |
-o main.js, node main.js |
None |
Some n |
-o main.bin (native) |
None |
Some 97n |
Both programs check. With the declaration present, the emitted effect source is changed to:
return term_pak(CID_LIB_NONE, 0);and the generated consumer still decodes the result as Maybe<U32>. Reading the generated C for this reproducer gives the following path (local names shortened only):
if (term_aux(x) == CID_NONE) {
// None
} else {
Loc at = term_loc(x); // 0
Term v = e.mem[at]; // e.mem[H_BUMP], observed as 97
heap_free(e, cls_fit(1), at); // overwrites H_BUMP at e.mem[0]
// continue as Some{v}
}97 is not a value returned by peek. In the emitted runtime, H_BUMP is defined as 0, a32_at(H, H_BUMP) addresses the low 32 bits of H[0], and the IO loop initializes Env e with e.mem = H. The allocator initializes and advances that cell as its bump cursor. The generated field load above therefore reads the same 64-bit corpus word, and the program's output shows that its value was 97 at that point. This conclusion comes from the emitted C plus the observed output, not from a debugger trace. heap_free then writes a free-list link to e.mem[0], overwriting the bump metadata.
On JS, the effect returns { $: "lib.None" }; the exhaustive two-arm match treats every non-"None" tag as Some and reads the absent value property.
If the captured constructor is declared but never kept live (for example, lib.Some{value: U32} with the effect returning CID(Some)), the native build instead fails to compile with use of undeclared identifier 'CID_LIB_SOME' inside the library's effect source.
Attacker-controlled arity variant
The corruption is not limited to the fixed metadata write above. A trusted effect that constructs a Base list normally does this:
return io_node(e, CID(Con), head, term_pak(CID(Nil), 0));If the entry file declares and keeps live:
type Evil is Data:
lib.Con{x: U32, y: U32, z: U32}
then the foreign source emits io_node(e, CID_LIB_CON, ...). io_node allocates two words, while CID_ARITY_T[CID_LIB_CON] is 3. If Bend discards the returned List<U32>, generated code calls term_sink. Following the emitted term_drop implementation from those observed facts, it:
- trusts the injected CID's arity and reads three fields from a two-word allocation;
- recursively interprets the out-of-bounds word as a
Term; and - calls
heap_free(e, cls_fit(3), loc), returning the two-word object to the four-word size class.
The entry file controls the injected constructor's arity. From the same term_drop code, larger arities imply a larger traversal and a different free-list size class. This gives the bug a direct path from namespace capture to native heap corruption, even when every foreign source file is trusted and correct for the module that owns it.
What I directly verified on the same commit is that the emitted CID table records arity 3 for CID_LIB_CON, the effect returns an io_node allocated for two fields with that tag, and the consumer passes the unused result to term_sink. I did not observe a crash or obtain an ASan report for this variant; the out-of-bounds third-field read and the two-word/four-word size-class mismatch follow from the emitted term_drop, cls_fit, and heap_free code.
Why it happens
#1042 (95317d9) made a file's namespace its real path and rejects a declaration whose first word is an import alias in parse_fresh. The entry file has namespace "", though, so a declaration spelled lib.None gets the global key lib.None, which belongs to lib.bend. The check catches this only when the import alias itself is lib; import ./lib.bend as Q leaves lib available for injection.
Effect-source ids are resolved late in c_ids (bend2/comp.ts, around line 1545). For an effect owned by namespace m, CID(k) first tries m + "." + k, then k. This is the same ns.k-first order that #1002 reported for parse_reso.
parse_reso has the same order for Bend source, but it runs while each module is parsed. At that point lib.bend has loaded before the entry file declares anything, so None correctly resolves to Base's None. c_ids runs after every file has loaded, sees the later lib.None, and silently changes the foreign ABI.
In short, the same spelling is resolved against two different symbol-table snapshots:
- the library's Bend signature resolves before the injected key exists;
- the library's foreign implementation resolves after it exists.
Security impact
This does not allow a proof to depend on the forged value: the checker reports main as relying on foreign code. That limits the proof-theoretic impact, but not the runtime impact. Calling approved foreign effects is normal, and the attacker does not modify those effects; a declaration in another file changes their generated ABI after they have checked.
Depending on the captured constructor and how the result is consumed, the observable consequences include:
- control-flow manipulation through the wrong arm of an otherwise exhaustive
match; - forged/missing fields on JS;
- reads from unrelated corpus objects or runtime metadata on native;
- traversal of words outside the actual constructor allocation; and
- allocator corruption through a reserved location or the wrong free-list size class.
The minimal reproducer demonstrates the metadata read/write. For the arity variant, the emitted inputs to term_drop were observed and the out-of-bounds node traversal plus size-class mismatch follow from its implementation; no resulting crash was observed. I have not established host code execution from these primitives, so this report does not claim it.
Expected behavior
The compiler should preserve the module boundary at both declaration and foreign-id resolution time. Any of these would address the immediate issue:
- reject a declaration whose canonical key falls in any other loaded file's namespace, regardless of the alias used to import that file;
- resolve and freeze each effect source's
CID/FIDnames against the declarations visible when its owning module is parsed; or - make
c_idsconsider only names actually owned by the effect's module, then the intended global/Base fallback, rather than every later declaration sharing the textual prefix.
The fix should cover both C and JS and both constructor/definition references and effect registration. A regression test should include a consumed Maybe result and a discarded List result with a captured Con of the wrong arity.
Version
bend 2.0.27
commit 95317d952c8fe5be18ef49f916b2a57250cfb0aa
Bun 1.4.2
Darwin arm64
Activity
Bend 2.0.32: the issue triage, root fixes, one verdict everywhere
A file's names are keyed ns:name, so no declaration lands in another …
A file's names are keyed ns:name inside the compiler, so no declarati…
A file's names are keyed ns:name inside the compiler, so no declarati…
A file's names are keyed ns:name inside the compiler, so no declarati…
A file's names are keyed ns:name inside the compiler, so no declarati…
Bend 2.0.35: M1 and M2 run the GPU again, a library's names stay its own