Skip to content

Commit 90c8f94

Browse files
Bryan C. Millsgopherbot
authored andcommitted
unix: avoid converting non-pointers to unsafe.Pointer in PtraceIO
Despite having the misleading type "void*" in the C API, the "offs" field of the ptrace_io_desc struct is an offset within the child process, and thus is not necessarily a valid pointer at all in the parent process. The Go unsafe.Pointer type must refer only to valid pointers, so converting this field through unsafe.Pointer is incorrect and (in some cases) dangerous. While we're here, let's also rename the "addr" function argument to "offs", since that's the corresponding ptrace_io_desc field. It's very confusing to have a function argument named "attr" that doesn't map to the struct field of the same name! For golang/go#58351. Change-Id: Id899f823e8d398b51fb0c42f466d7ae2f957c26b Reviewed-on: https://go-review.googlesource.com/c/sys/+/465675 Run-TryBot: Bryan Mills <bcmills@google.com> Auto-Submit: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
1 parent 4e121b1 commit 90c8f94

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

unix/syscall_freebsd_386.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,13 @@ func PtraceGetFsBase(pid int, fsbase *int64) (err error) {
6060
return ptrace(PT_GETFSBASE, pid, uintptr(unsafe.Pointer(fsbase)), 0)
6161
}
6262

63-
func PtraceIO(req int, pid int, addr uintptr, out []byte, countin int) (count int, err error) {
64-
ioDesc := PtraceIoDesc{Op: int32(req), Offs: uintptr(unsafe.Pointer(addr)), Addr: uintptr(unsafe.Pointer(&out[0])), Len: uint32(countin)}
63+
func PtraceIO(req int, pid int, offs uintptr, out []byte, countin int) (count int, err error) {
64+
ioDesc := PtraceIoDesc{
65+
Op: int32(req),
66+
Offs: offs,
67+
Addr: uintptr(unsafe.Pointer(&out[0])), // TODO(#58351): this is not safe.
68+
Len: uint32(countin),
69+
}
6570
err = ptrace(PT_IO, pid, uintptr(unsafe.Pointer(&ioDesc)), 0)
6671
return int(ioDesc.Len), err
6772
}

unix/syscall_freebsd_amd64.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,13 @@ func PtraceGetFsBase(pid int, fsbase *int64) (err error) {
6060
return ptrace(PT_GETFSBASE, pid, uintptr(unsafe.Pointer(fsbase)), 0)
6161
}
6262

63-
func PtraceIO(req int, pid int, addr uintptr, out []byte, countin int) (count int, err error) {
64-
ioDesc := PtraceIoDesc{Op: int32(req), Offs: uintptr(unsafe.Pointer(addr)), Addr: uintptr(unsafe.Pointer(&out[0])), Len: uint64(countin)}
63+
func PtraceIO(req int, pid int, offs uintptr, out []byte, countin int) (count int, err error) {
64+
ioDesc := PtraceIoDesc{
65+
Op: int32(req),
66+
Offs: offs,
67+
Addr: uintptr(unsafe.Pointer(&out[0])), // TODO(#58351): this is not safe.
68+
Len: uint64(countin),
69+
}
6570
err = ptrace(PT_IO, pid, uintptr(unsafe.Pointer(&ioDesc)), 0)
6671
return int(ioDesc.Len), err
6772
}

unix/syscall_freebsd_arm.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ func sendfile(outfd int, infd int, offset *int64, count int) (written int, err e
5656

5757
func Syscall9(num, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err syscall.Errno)
5858

59-
func PtraceIO(req int, pid int, addr uintptr, out []byte, countin int) (count int, err error) {
60-
ioDesc := PtraceIoDesc{Op: int32(req), Offs: uintptr(unsafe.Pointer(addr)), Addr: uintptr(unsafe.Pointer(&out[0])), Len: uint32(countin)}
59+
func PtraceIO(req int, pid int, offs uintptr, out []byte, countin int) (count int, err error) {
60+
ioDesc := PtraceIoDesc{
61+
Op: int32(req),
62+
Offs: offs,
63+
Addr: uintptr(unsafe.Pointer(&out[0])), // TODO(#58351): this is not safe.
64+
Len: uint32(countin),
65+
}
6166
err = ptrace(PT_IO, pid, uintptr(unsafe.Pointer(&ioDesc)), 0)
6267
return int(ioDesc.Len), err
6368
}

unix/syscall_freebsd_arm64.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ func sendfile(outfd int, infd int, offset *int64, count int) (written int, err e
5656

5757
func Syscall9(num, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err syscall.Errno)
5858

59-
func PtraceIO(req int, pid int, addr uintptr, out []byte, countin int) (count int, err error) {
60-
ioDesc := PtraceIoDesc{Op: int32(req), Offs: uintptr(unsafe.Pointer(addr)), Addr: uintptr(unsafe.Pointer(&out[0])), Len: uint64(countin)}
59+
func PtraceIO(req int, pid int, offs uintptr, out []byte, countin int) (count int, err error) {
60+
ioDesc := PtraceIoDesc{
61+
Op: int32(req),
62+
Offs: offs,
63+
Addr: uintptr(unsafe.Pointer(&out[0])), // TODO(#58351): this is not safe.
64+
Len: uint64(countin),
65+
}
6166
err = ptrace(PT_IO, pid, uintptr(unsafe.Pointer(&ioDesc)), 0)
6267
return int(ioDesc.Len), err
6368
}

unix/syscall_freebsd_riscv64.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ func sendfile(outfd int, infd int, offset *int64, count int) (written int, err e
5656

5757
func Syscall9(num, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err syscall.Errno)
5858

59-
func PtraceIO(req int, pid int, addr uintptr, out []byte, countin int) (count int, err error) {
60-
ioDesc := PtraceIoDesc{Op: int32(req), Offs: uintptr(unsafe.Pointer(addr)), Addr: uintptr(unsafe.Pointer(&out[0])), Len: uint64(countin)}
59+
func PtraceIO(req int, pid int, offs uintptr, out []byte, countin int) (count int, err error) {
60+
ioDesc := PtraceIoDesc{
61+
Op: int32(req),
62+
Offs: offs,
63+
Addr: uintptr(unsafe.Pointer(&out[0])), // TODO(#58351): this is not safe.
64+
Len: uint64(countin),
65+
}
6166
err = ptrace(PT_IO, pid, uintptr(unsafe.Pointer(&ioDesc)), 0)
6267
return int(ioDesc.Len), err
6368
}

0 commit comments

Comments
 (0)