aboutsummaryrefslogtreecommitdiff
path: root/src/syscall/syscall_openbsd.go
diff options
context:
space:
mode:
authorDamien Neil <dneil@google.com>2016-06-03 15:04:53 -0700
committerDamien Neil <dneil@google.com>2016-09-20 19:27:57 +0000
commitf5f7d6e32d5384a3638325ff8393bf94ec8d6971 (patch)
treef03ae9f878d3fbcd85b419e238f19e5e512d38e5 /src/syscall/syscall_openbsd.go
parentab5923572984651af05a47755109642bfc529cb5 (diff)
downloadgo-f5f7d6e32d5384a3638325ff8393bf94ec8d6971.tar.xz
syscall: validate ParseDirent inputs
Don't panic, crash, or return references to uninitialized memory when ParseDirent is passed invalid input. Move common dirent parsing to syscall.go with minimal platform-specific functions in syscall_$GOOS.go. Fixes #15653 Change-Id: I5602475e02321fe381064488401c14b33bec6886 Reviewed-on: https://go-review.googlesource.com/23780 Run-TryBot: Damien Neil <dneil@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/syscall/syscall_openbsd.go')
-rw-r--r--src/syscall/syscall_openbsd.go36
1 files changed, 10 insertions, 26 deletions
diff --git a/src/syscall/syscall_openbsd.go b/src/syscall/syscall_openbsd.go
index 68218cf93b..bd25fbf87a 100644
--- a/src/syscall/syscall_openbsd.go
+++ b/src/syscall/syscall_openbsd.go
@@ -50,32 +50,16 @@ func nametomib(name string) (mib []_C_int, err error) {
return nil, EINVAL
}
-// ParseDirent parses up to max directory entries in buf,
-// appending the names to names. It returns the number
-// bytes consumed from buf, the number of entries added
-// to names, and the new names slice.
-func ParseDirent(buf []byte, max int, names []string) (consumed int, count int, newnames []string) {
- origlen := len(buf)
- for max != 0 && len(buf) > 0 {
- dirent := (*Dirent)(unsafe.Pointer(&buf[0]))
- if dirent.Reclen == 0 {
- buf = nil
- break
- }
- buf = buf[dirent.Reclen:]
- if dirent.Fileno == 0 { // File absent in directory.
- continue
- }
- bytes := (*[10000]byte)(unsafe.Pointer(&dirent.Name[0]))
- var name = string(bytes[0:dirent.Namlen])
- if name == "." || name == ".." { // Useless names
- continue
- }
- max--
- count++
- names = append(names, name)
- }
- return origlen - len(buf), count, names
+func direntIno(buf []byte) (uint64, bool) {
+ return readInt(buf, unsafe.Offsetof(Dirent{}.Fileno), unsafe.Sizeof(Dirent{}.Fileno))
+}
+
+func direntReclen(buf []byte) (uint64, bool) {
+ return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
+}
+
+func direntNamlen(buf []byte) (uint64, bool) {
+ return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen))
}
//sysnb pipe(p *[2]_C_int) (err error)