1
0
mirror of https://github.com/golang/go synced 2024-11-18 09:14:43 -07:00

internal/fastwalk: don't cross Dirent.Reclen boundry while looking for NULL in parseDirEnt

Crossing Dirent.Reclen boundry was manifested in golang/go#28131 as garbaled filenames,
when Dirent.Name was not NULL terminated on FreeBSD due to a bug (parseDirEnt would find a NULL
in the following Dirent's Fileno/Reclen fields).

Only search for NULL on linux, when the Namlen field is available use it directly instead.

Updates golang/go#28131

Change-Id: I64090576c8bad2bd246d1561432bf73d5caee2a9
Reviewed-on: https://go-review.googlesource.com/c/141801
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Yuval Pavel Zholkover 2018-10-13 13:57:27 +03:00 committed by Brad Fitzpatrick
parent 13216ffa54
commit 5e66757b83
3 changed files with 38 additions and 5 deletions

View File

@ -0,0 +1,13 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin freebsd openbsd netbsd
package fastwalk
import "syscall"
func direntNamlen(dirent *syscall.Dirent) uint64 {
return uint64(dirent.Namlen)
}

View File

@ -0,0 +1,24 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build linux
// +build !appengine
package fastwalk
import (
"bytes"
"syscall"
"unsafe"
)
func direntNamlen(dirent *syscall.Dirent) uint64 {
const fixedHdr = uint16(unsafe.Offsetof(syscall.Dirent{}.Name))
nameBuf := (*[unsafe.Sizeof(dirent.Name)]byte)(unsafe.Pointer(&dirent.Name[0]))
nameLen := bytes.IndexByte(nameBuf[:dirent.Reclen-fixedHdr], 0)
if nameLen < 0 {
panic("failed to find terminating 0 byte in dirent")
}
return uint64(nameLen)
}

View File

@ -8,7 +8,6 @@
package fastwalk
import (
"bytes"
"fmt"
"os"
"syscall"
@ -114,10 +113,7 @@ func parseDirEnt(buf []byte) (consumed int, name string, typ os.FileMode) {
}
nameBuf := (*[unsafe.Sizeof(dirent.Name)]byte)(unsafe.Pointer(&dirent.Name[0]))
nameLen := bytes.IndexByte(nameBuf[:], 0)
if nameLen < 0 {
panic("failed to find terminating 0 byte in dirent")
}
nameLen := direntNamlen(dirent)
// Special cases for common things:
if nameLen == 1 && nameBuf[0] == '.' {