Commit ddd1cdae authored by Alex Brainman's avatar Alex Brainman

windows: fix race when allocating buffer for some windows syscalls

From main repo: https://go-review.googlesource.com/#/c/4940

Change-Id: I56fe7f6aedc0fd350abb94299ad500fcb80c049a
Reviewed-on: https://go-review.googlesource.com/8604Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent ea755264
...@@ -16,19 +16,17 @@ func Getenv(key string) (value string, found bool) { ...@@ -16,19 +16,17 @@ func Getenv(key string) (value string, found bool) {
if err != nil { if err != nil {
return "", false return "", false
} }
b := make([]uint16, 100) n := uint32(100)
n, e := GetEnvironmentVariable(keyp, &b[0], uint32(len(b))) for {
if n == 0 && e == ERROR_ENVVAR_NOT_FOUND { b := make([]uint16, n)
return "", false n, err = GetEnvironmentVariable(keyp, &b[0], uint32(len(b)))
} if n == 0 && err == ERROR_ENVVAR_NOT_FOUND {
if n > uint32(len(b)) { return "", false
b = make([]uint16, n) }
n, e = GetEnvironmentVariable(keyp, &b[0], uint32(len(b))) if n <= uint32(len(b)) {
if n > uint32(len(b)) { return string(utf16.Decode(b[:n])), true
n = 0
} }
} }
return string(utf16.Decode(b[0:n])), true
} }
func Setenv(key, value string) error { func Setenv(key, value string) error {
......
...@@ -6,8 +6,6 @@ ...@@ -6,8 +6,6 @@
package windows package windows
import "syscall"
// EscapeArg rewrites command line argument s as prescribed // EscapeArg rewrites command line argument s as prescribed
// in http://msdn.microsoft.com/en-us/library/ms880421. // in http://msdn.microsoft.com/en-us/library/ms880421.
// This function returns "" (2 double quotes) if s is empty. // This function returns "" (2 double quotes) if s is empty.
...@@ -85,21 +83,15 @@ func FullPath(name string) (path string, err error) { ...@@ -85,21 +83,15 @@ func FullPath(name string) (path string, err error) {
if err != nil { if err != nil {
return "", err return "", err
} }
buf := make([]uint16, 100) n := uint32(100)
n, err := GetFullPathName(p, uint32(len(buf)), &buf[0], nil) for {
if err != nil { buf := make([]uint16, n)
return "", err
}
if n > uint32(len(buf)) {
// Windows is asking for bigger buffer.
buf = make([]uint16, n)
n, err = GetFullPathName(p, uint32(len(buf)), &buf[0], nil) n, err = GetFullPathName(p, uint32(len(buf)), &buf[0], nil)
if err != nil { if err != nil {
return "", err return "", err
} }
if n > uint32(len(buf)) { if n <= uint32(len(buf)) {
return "", syscall.EINVAL return UTF16ToString(buf[:n]), nil
} }
} }
return UTF16ToString(buf[:n]), nil
} }
...@@ -42,21 +42,20 @@ func TranslateAccountName(username string, from, to uint32, initSize int) (strin ...@@ -42,21 +42,20 @@ func TranslateAccountName(username string, from, to uint32, initSize int) (strin
if e != nil { if e != nil {
return "", e return "", e
} }
b := make([]uint16, 50) n := uint32(50)
n := uint32(len(b)) for {
e = TranslateName(u, from, to, &b[0], &n) b := make([]uint16, n)
if e != nil { e = TranslateName(u, from, to, &b[0], &n)
if e == nil {
return UTF16ToString(b[:n]), nil
}
if e != ERROR_INSUFFICIENT_BUFFER { if e != ERROR_INSUFFICIENT_BUFFER {
return "", e return "", e
} }
// make receive buffers of requested size and try again if n <= uint32(len(b)) {
b = make([]uint16, n)
e = TranslateName(u, from, to, &b[0], &n)
if e != nil {
return "", e return "", e
} }
} }
return UTF16ToString(b), nil
} }
const ( const (
...@@ -137,26 +136,23 @@ func LookupSID(system, account string) (sid *SID, domain string, accType uint32, ...@@ -137,26 +136,23 @@ func LookupSID(system, account string) (sid *SID, domain string, accType uint32,
return nil, "", 0, e return nil, "", 0, e
} }
} }
db := make([]uint16, 50) n := uint32(50)
dn := uint32(len(db)) dn := uint32(50)
b := make([]byte, 50) for {
n := uint32(len(b)) b := make([]byte, n)
sid = (*SID)(unsafe.Pointer(&b[0])) db := make([]uint16, dn)
e = LookupAccountName(sys, acc, sid, &n, &db[0], &dn, &accType) sid = (*SID)(unsafe.Pointer(&b[0]))
if e != nil { e = LookupAccountName(sys, acc, sid, &n, &db[0], &dn, &accType)
if e == nil {
return sid, UTF16ToString(db), accType, nil
}
if e != ERROR_INSUFFICIENT_BUFFER { if e != ERROR_INSUFFICIENT_BUFFER {
return nil, "", 0, e return nil, "", 0, e
} }
// make receive buffers of requested size and try again if n <= uint32(len(b)) {
b = make([]byte, n)
sid = (*SID)(unsafe.Pointer(&b[0]))
db = make([]uint16, dn)
e = LookupAccountName(sys, acc, sid, &n, &db[0], &dn, &accType)
if e != nil {
return nil, "", 0, e return nil, "", 0, e
} }
} }
return sid, UTF16ToString(db), accType, nil
} }
// String converts sid to a string format // String converts sid to a string format
...@@ -198,24 +194,22 @@ func (sid *SID) LookupAccount(system string) (account, domain string, accType ui ...@@ -198,24 +194,22 @@ func (sid *SID) LookupAccount(system string) (account, domain string, accType ui
return "", "", 0, err return "", "", 0, err
} }
} }
b := make([]uint16, 50) n := uint32(50)
n := uint32(len(b)) dn := uint32(50)
db := make([]uint16, 50) for {
dn := uint32(len(db)) b := make([]uint16, n)
e := LookupAccountSid(sys, sid, &b[0], &n, &db[0], &dn, &accType) db := make([]uint16, dn)
if e != nil { e := LookupAccountSid(sys, sid, &b[0], &n, &db[0], &dn, &accType)
if e == nil {
return UTF16ToString(b), UTF16ToString(db), accType, nil
}
if e != ERROR_INSUFFICIENT_BUFFER { if e != ERROR_INSUFFICIENT_BUFFER {
return "", "", 0, e return "", "", 0, e
} }
// make receive buffers of requested size and try again if n <= uint32(len(b)) {
b = make([]uint16, n)
db = make([]uint16, dn)
e = LookupAccountSid(nil, sid, &b[0], &n, &db[0], &dn, &accType)
if e != nil {
return "", "", 0, e return "", "", 0, e
} }
} }
return UTF16ToString(b), UTF16ToString(db), accType, nil
} }
const ( const (
...@@ -327,21 +321,20 @@ func (t Token) Close() error { ...@@ -327,21 +321,20 @@ func (t Token) Close() error {
// getInfo retrieves a specified type of information about an access token. // getInfo retrieves a specified type of information about an access token.
func (t Token) getInfo(class uint32, initSize int) (unsafe.Pointer, error) { func (t Token) getInfo(class uint32, initSize int) (unsafe.Pointer, error) {
b := make([]byte, initSize) n := uint32(initSize)
var n uint32 for {
e := GetTokenInformation(t, class, &b[0], uint32(len(b)), &n) b := make([]byte, n)
if e != nil { e := GetTokenInformation(t, class, &b[0], uint32(len(b)), &n)
if e == nil {
return unsafe.Pointer(&b[0]), nil
}
if e != ERROR_INSUFFICIENT_BUFFER { if e != ERROR_INSUFFICIENT_BUFFER {
return nil, e return nil, e
} }
// make receive buffers of requested size and try again if n <= uint32(len(b)) {
b = make([]byte, n)
e = GetTokenInformation(t, class, &b[0], uint32(len(b)), &n)
if e != nil {
return nil, e return nil, e
} }
} }
return unsafe.Pointer(&b[0]), nil
} }
// GetTokenUser retrieves access token t user account information. // GetTokenUser retrieves access token t user account information.
...@@ -367,19 +360,18 @@ func (t Token) GetTokenPrimaryGroup() (*Tokenprimarygroup, error) { ...@@ -367,19 +360,18 @@ func (t Token) GetTokenPrimaryGroup() (*Tokenprimarygroup, error) {
// GetUserProfileDirectory retrieves path to the // GetUserProfileDirectory retrieves path to the
// root directory of the access token t user's profile. // root directory of the access token t user's profile.
func (t Token) GetUserProfileDirectory() (string, error) { func (t Token) GetUserProfileDirectory() (string, error) {
b := make([]uint16, 100) n := uint32(100)
n := uint32(len(b)) for {
e := GetUserProfileDirectory(t, &b[0], &n) b := make([]uint16, n)
if e != nil { e := GetUserProfileDirectory(t, &b[0], &n)
if e == nil {
return UTF16ToString(b), nil
}
if e != ERROR_INSUFFICIENT_BUFFER { if e != ERROR_INSUFFICIENT_BUFFER {
return "", e return "", e
} }
// make receive buffers of requested size and try again if n <= uint32(len(b)) {
b = make([]uint16, n)
e = GetUserProfileDirectory(t, &b[0], &n)
if e != nil {
return "", e return "", e
} }
} }
return UTF16ToString(b), nil
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment