Skip to content

Commit 4548fcc

Browse files
committed
[release-branch.go1.16] misc/wasm, cmd/link: do not let command line args overwrite global data
On Wasm, wasm_exec.js puts command line arguments at the beginning of the linear memory (following the "zero page"). Currently there is no limit for this, and a very long command line can overwrite the program's data section. Prevent this by limiting the command line to 4096 bytes, and in the linker ensuring the data section starts at a high enough address (8192). (Arguably our address assignment on Wasm is a bit confusing. This is the minimum fix I can come up with.) Thanks to Ben Lubar for reporting this issue. Change by Cherry Mui <cherryyz@google.com>. For #48797 Fixes #48799 Fixes CVE-2021-38297 Change-Id: I0f50fbb2a5b6d0d047e3c134a88988d9133e4ab3 Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/1205933 Reviewed-by: Roland Shoemaker <bracewell@google.com> Reviewed-by: Than McIntosh <thanm@google.com> Reviewed-on: https://go-review.googlesource.com/c/go/+/354591 Trust: Michael Knyszek <mknyszek@google.com> Reviewed-by: Heschi Kreinick <heschi@google.com>
1 parent 7d57324 commit 4548fcc

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

misc/wasm/wasm_exec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,13 @@
564564
offset += 8;
565565
});
566566

567+
// The linker guarantees global data starts from at least wasmMinDataAddr.
568+
// Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
569+
const wasmMinDataAddr = 4096 + 4096;
570+
if (offset >= wasmMinDataAddr) {
571+
throw new Error("command line too long");
572+
}
573+
567574
this._inst.exports.run(argc, argv);
568575
if (this.exited) {
569576
this._resolveExitPromise();

src/cmd/link/internal/ld/data.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2330,6 +2330,11 @@ func assignAddress(ctxt *Link, sect *sym.Section, n int, s loader.Sym, va uint64
23302330
return sect, n, va
23312331
}
23322332

2333+
// On Wasm, we reserve 4096 bytes for zero page, then 4096 bytes for wasm_exec.js
2334+
// to store command line args. Data sections starts from at least address 8192.
2335+
// Keep in sync with wasm_exec.js.
2336+
const wasmMinDataAddr = 4096 + 4096
2337+
23332338
// address assigns virtual addresses to all segments and sections and
23342339
// returns all segments in file order.
23352340
func (ctxt *Link) address() []*sym.Segment {
@@ -2339,10 +2344,14 @@ func (ctxt *Link) address() []*sym.Segment {
23392344
order = append(order, &Segtext)
23402345
Segtext.Rwx = 05
23412346
Segtext.Vaddr = va
2342-
for _, s := range Segtext.Sections {
2347+
for i, s := range Segtext.Sections {
23432348
va = uint64(Rnd(int64(va), int64(s.Align)))
23442349
s.Vaddr = va
23452350
va += s.Length
2351+
2352+
if ctxt.IsWasm() && i == 0 && va < wasmMinDataAddr {
2353+
va = wasmMinDataAddr
2354+
}
23462355
}
23472356

23482357
Segtext.Length = va - uint64(*FlagTextAddr)

0 commit comments

Comments
 (0)