mirror of
https://github.com/rwinkhart/sys.git
synced 2026-08-31 14:26:40 -04:00
windows: avoid uint16 overflow in NewNTUnicodeString
Fixes CVE-2026-39824 Fixes #78916 Change-Id: I344518a17d59fd81c4bb39da0b3e13be6a6a6964 Reviewed-on: https://go-review.googlesource.com/c/sys/+/770080 Reviewed-by: Neal Patel <nealpatel@google.com> Reviewed-by: Quim Muntal <quimmuntal@gmail.com> LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Damien Neil <dneil@google.com>
This commit is contained in:
committed by
Gopher Robot
parent
94ad893e1e
commit
fb1facd76f
@@ -1697,10 +1697,13 @@ func NewNTUnicodeString(s string) (*NTUnicodeString, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
n := uint16(len(s16) * 2)
|
n := len(s16) * 2
|
||||||
|
if n > (1<<16)-1 {
|
||||||
|
return nil, syscall.EINVAL
|
||||||
|
}
|
||||||
return &NTUnicodeString{
|
return &NTUnicodeString{
|
||||||
Length: n - 2, // subtract 2 bytes for the NULL terminator
|
Length: uint16(n) - 2, // subtract 2 bytes for the NULL terminator
|
||||||
MaximumLength: n,
|
MaximumLength: uint16(n),
|
||||||
Buffer: &s16[0],
|
Buffer: &s16[0],
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"debug/pe"
|
"debug/pe"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -1475,21 +1476,37 @@ func TestToUnicodeEx(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRoundtripNTUnicodeString(t *testing.T) {
|
func TestRoundtripNTUnicodeString(t *testing.T) {
|
||||||
for _, s := range []string{
|
// NTUnicodeString maximum string length must fit in a uint16, less for terminal NUL.
|
||||||
"",
|
maxString := strings.Repeat("*", (math.MaxUint16/2)-1)
|
||||||
"hello",
|
for _, test := range []struct {
|
||||||
"Ƀ",
|
s string
|
||||||
strings.Repeat("*", 32000), // NTUnicodeString works up to 2^16 byte lengths == 32768 uint16s.
|
wantErr bool
|
||||||
// TODO: various encoding errors?
|
}{{
|
||||||
} {
|
s: "",
|
||||||
ntus, err := windows.NewNTUnicodeString(s)
|
}, {
|
||||||
|
s: "hello",
|
||||||
|
}, {
|
||||||
|
s: "Ƀ",
|
||||||
|
}, {
|
||||||
|
s: maxString,
|
||||||
|
}, {
|
||||||
|
s: maxString + "*",
|
||||||
|
wantErr: true,
|
||||||
|
}, {
|
||||||
|
s: "a\x00a",
|
||||||
|
wantErr: true,
|
||||||
|
}} {
|
||||||
|
ntus, err := windows.NewNTUnicodeString(test.s)
|
||||||
|
if (err != nil) != test.wantErr {
|
||||||
|
t.Errorf("NewNTUnicodeString(%q): %v, wantErr:%v", test.s, err, test.wantErr)
|
||||||
|
continue
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("encoding %q failed: %v", s, err)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
s2 := ntus.String()
|
s2 := ntus.String()
|
||||||
if s != s2 {
|
if test.s != s2 {
|
||||||
t.Errorf("round trip of %q = %q, wanted original", s, s2)
|
t.Errorf("round trip of %q = %q, wanted original", test.s, s2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user