Skip to content

Commit 2977c77

Browse files
dmgkgopherbot
authored andcommitted
unix: add ptracePtr that accepts pointer arg as unsafe.Pointer
The existing ptrace wrapper accepts pointer argument as an uintptr which often points to the memory allocated in Go. This violates unsafe.Pointer safety rules. For golang/go#58387 Change-Id: Ib3b4c50368725191f0862c6c7c6d46b0568523c7 Reviewed-on: https://go-review.googlesource.com/c/sys/+/469835 Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Run-TryBot: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
1 parent 6877dcc commit 2977c77

22 files changed

+127
-39
lines changed

unix/linux/mkall.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -790,13 +790,13 @@ func generatePtraceRegSet(arch string) error {
790790
fmt.Fprintf(buf, "// PtraceGetRegSet%s fetches the registers used by %s binaries.\n", uarch, arch)
791791
fmt.Fprintf(buf, "func PtraceGetRegSet%s(pid, addr int, regsout *PtraceRegs%s) error {\n", uarch, uarch)
792792
fmt.Fprintf(buf, "\tiovec := Iovec{(*byte)(unsafe.Pointer(regsout)), uint64(unsafe.Sizeof(*regsout))}\n")
793-
fmt.Fprintf(buf, "\treturn ptrace(PTRACE_GETREGSET, pid, uintptr(addr), uintptr(unsafe.Pointer(&iovec)))\n")
793+
fmt.Fprintf(buf, "\treturn ptracePtr(PTRACE_GETREGSET, pid, uintptr(addr), unsafe.Pointer(&iovec))\n")
794794
fmt.Fprintf(buf, "}\n")
795795
fmt.Fprintf(buf, "\n")
796796
fmt.Fprintf(buf, "// PtraceSetRegSet%s sets the registers used by %s binaries.\n", uarch, arch)
797797
fmt.Fprintf(buf, "func PtraceSetRegSet%s(pid, addr int, regs *PtraceRegs%s) error {\n", uarch, uarch)
798798
fmt.Fprintf(buf, "\tiovec := Iovec{(*byte)(unsafe.Pointer(regs)), uint64(unsafe.Sizeof(*regs))}\n")
799-
fmt.Fprintf(buf, "\treturn ptrace(PTRACE_SETREGSET, pid, uintptr(addr), uintptr(unsafe.Pointer(&iovec)))\n")
799+
fmt.Fprintf(buf, "\treturn ptracePtr(PTRACE_SETREGSET, pid, uintptr(addr), unsafe.Pointer(&iovec))\n")
800800
fmt.Fprintf(buf, "}\n")
801801
if err := buf.Flush(); err != nil {
802802
return err
@@ -834,12 +834,12 @@ func writeOnePtrace(w io.Writer, arch, def string) {
834834
fmt.Fprintf(w, "\n")
835835
fmt.Fprintf(w, "// PtraceGetRegs%s fetches the registers used by %s binaries.\n", uarch, arch)
836836
fmt.Fprintf(w, "func PtraceGetRegs%s(pid int, regsout *PtraceRegs%s) error {\n", uarch, uarch)
837-
fmt.Fprintf(w, "\treturn ptrace(PTRACE_GETREGS, pid, 0, uintptr(unsafe.Pointer(regsout)))\n")
837+
fmt.Fprintf(w, "\treturn ptracePtr(PTRACE_GETREGS, pid, 0, unsafe.Pointer(regsout))\n")
838838
fmt.Fprintf(w, "}\n")
839839
fmt.Fprintf(w, "\n")
840840
fmt.Fprintf(w, "// PtraceSetRegs%s sets the registers used by %s binaries.\n", uarch, arch)
841841
fmt.Fprintf(w, "func PtraceSetRegs%s(pid int, regs *PtraceRegs%s) error {\n", uarch, uarch)
842-
fmt.Fprintf(w, "\treturn ptrace(PTRACE_SETREGS, pid, 0, uintptr(unsafe.Pointer(regs)))\n")
842+
fmt.Fprintf(w, "\treturn ptracePtr(PTRACE_SETREGS, pid, 0, unsafe.Pointer(regs))\n")
843843
fmt.Fprintf(w, "}\n")
844844
}
845845

unix/ptrace_darwin.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
package unix
99

10+
import "unsafe"
11+
1012
func ptrace(request int, pid int, addr uintptr, data uintptr) error {
1113
return ptrace1(request, pid, addr, data)
1214
}
15+
16+
func ptracePtr(request int, pid int, addr uintptr, data unsafe.Pointer) error {
17+
return ptrace1Ptr(request, pid, addr, data)
18+
}

unix/ptrace_ios.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
package unix
99

10+
import "unsafe"
11+
1012
func ptrace(request int, pid int, addr uintptr, data uintptr) (err error) {
1113
return ENOTSUP
1214
}
15+
16+
func ptracePtr(request int, pid int, addr uintptr, data unsafe.Pointer) (err error) {
17+
return ENOTSUP
18+
}

unix/syscall_darwin_amd64.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ func Syscall9(num, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr,
4747
//sys getfsstat(buf unsafe.Pointer, size uintptr, flags int) (n int, err error) = SYS_GETFSSTAT64
4848
//sys Lstat(path string, stat *Stat_t) (err error) = SYS_LSTAT64
4949
//sys ptrace1(request int, pid int, addr uintptr, data uintptr) (err error) = SYS_ptrace
50+
//sys ptrace1Ptr(request int, pid int, addr unsafe.Pointer, data uintptr) (err error) = SYS_ptrace
5051
//sys Stat(path string, stat *Stat_t) (err error) = SYS_STAT64
5152
//sys Statfs(path string, stat *Statfs_t) (err error) = SYS_STATFS64

unix/syscall_darwin_arm64.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ func Syscall9(num, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr,
4747
//sys getfsstat(buf unsafe.Pointer, size uintptr, flags int) (n int, err error) = SYS_GETFSSTAT
4848
//sys Lstat(path string, stat *Stat_t) (err error)
4949
//sys ptrace1(request int, pid int, addr uintptr, data uintptr) (err error) = SYS_ptrace
50+
//sys ptrace1Ptr(request int, pid int, addr unsafe.Pointer, data uintptr) (err error) = SYS_ptrace
5051
//sys Stat(path string, stat *Stat_t) (err error)
5152
//sys Statfs(path string, stat *Statfs_t) (err error)

unix/syscall_freebsd.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err e
254254
}
255255

256256
//sys ptrace(request int, pid int, addr uintptr, data int) (err error)
257+
//sys ptracePtr(request int, pid int, addr unsafe.Pointer, data int) (err error)
257258

258259
func PtraceAttach(pid int) (err error) {
259260
return ptrace(PT_ATTACH, pid, 0, 0)
@@ -268,11 +269,11 @@ func PtraceDetach(pid int) (err error) {
268269
}
269270

270271
func PtraceGetFpRegs(pid int, fpregsout *FpReg) (err error) {
271-
return ptrace(PT_GETFPREGS, pid, uintptr(unsafe.Pointer(fpregsout)), 0)
272+
return ptracePtr(PT_GETFPREGS, pid, unsafe.Pointer(fpregsout), 0)
272273
}
273274

274275
func PtraceGetRegs(pid int, regsout *Reg) (err error) {
275-
return ptrace(PT_GETREGS, pid, uintptr(unsafe.Pointer(regsout)), 0)
276+
return ptracePtr(PT_GETREGS, pid, unsafe.Pointer(regsout), 0)
276277
}
277278

278279
func PtraceIO(req int, pid int, offs uintptr, out []byte, countin int) (count int, err error) {
@@ -288,18 +289,16 @@ func PtraceIO(req int, pid int, offs uintptr, out []byte, countin int) (count in
288289
}
289290
ioDesc.SetLen(countin)
290291

291-
// TODO(#58387): It's not actually safe to pass &ioDesc as a uintptr here.
292-
// Pass it as an unsafe.Pointer instead.
293-
err = ptrace(PT_IO, pid, uintptr(unsafe.Pointer(&ioDesc)), 0)
292+
err = ptracePtr(PT_IO, pid, unsafe.Pointer(&ioDesc), 0)
294293
return int(ioDesc.Len), err
295294
}
296295

297296
func PtraceLwpEvents(pid int, enable int) (err error) {
298297
return ptrace(PT_LWP_EVENTS, pid, 0, enable)
299298
}
300299

301-
func PtraceLwpInfo(pid int, info uintptr) (err error) {
302-
return ptrace(PT_LWPINFO, pid, info, int(unsafe.Sizeof(PtraceLwpInfoStruct{})))
300+
func PtraceLwpInfo(pid int, info *PtraceLwpInfoStruct) (err error) {
301+
return ptracePtr(PT_LWPINFO, pid, unsafe.Pointer(info), int(unsafe.Sizeof(*info)))
303302
}
304303

305304
func PtracePeekData(pid int, addr uintptr, out []byte) (count int, err error) {
@@ -319,7 +318,7 @@ func PtracePokeText(pid int, addr uintptr, data []byte) (count int, err error) {
319318
}
320319

321320
func PtraceSetRegs(pid int, regs *Reg) (err error) {
322-
return ptrace(PT_SETREGS, pid, uintptr(unsafe.Pointer(regs)), 0)
321+
return ptracePtr(PT_SETREGS, pid, unsafe.Pointer(regs), 0)
323322
}
324323

325324
func PtraceSingleStep(pid int) (err error) {

unix/syscall_freebsd_386.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,5 @@ func sendfile(outfd int, infd int, offset *int64, count int) (written int, err e
6161
func Syscall9(num, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err syscall.Errno)
6262

6363
func PtraceGetFsBase(pid int, fsbase *int64) (err error) {
64-
return ptrace(PT_GETFSBASE, pid, uintptr(unsafe.Pointer(fsbase)), 0)
64+
return ptracePtr(PT_GETFSBASE, pid, unsafe.Pointer(fsbase), 0)
6565
}

unix/syscall_freebsd_amd64.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,5 @@ func sendfile(outfd int, infd int, offset *int64, count int) (written int, err e
6161
func Syscall9(num, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err syscall.Errno)
6262

6363
func PtraceGetFsBase(pid int, fsbase *int64) (err error) {
64-
return ptrace(PT_GETFSBASE, pid, uintptr(unsafe.Pointer(fsbase)), 0)
64+
return ptracePtr(PT_GETFSBASE, pid, unsafe.Pointer(fsbase), 0)
6565
}

unix/syscall_linux.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,7 @@ func BindToDevice(fd int, device string) (err error) {
15791579
}
15801580

15811581
//sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error)
1582+
//sys ptracePtr(request int, pid int, addr uintptr, data unsafe.Pointer) (err error)
15821583

15831584
func ptracePeek(req int, pid int, addr uintptr, out []byte) (count int, err error) {
15841585
// The peek requests are machine-size oriented, so we wrap it
@@ -1596,7 +1597,7 @@ func ptracePeek(req int, pid int, addr uintptr, out []byte) (count int, err erro
15961597
// boundary.
15971598
n := 0
15981599
if addr%SizeofPtr != 0 {
1599-
err = ptrace(req, pid, addr-addr%SizeofPtr, uintptr(unsafe.Pointer(&buf[0])))
1600+
err = ptracePtr(req, pid, addr-addr%SizeofPtr, unsafe.Pointer(&buf[0]))
16001601
if err != nil {
16011602
return 0, err
16021603
}
@@ -1608,7 +1609,7 @@ func ptracePeek(req int, pid int, addr uintptr, out []byte) (count int, err erro
16081609
for len(out) > 0 {
16091610
// We use an internal buffer to guarantee alignment.
16101611
// It's not documented if this is necessary, but we're paranoid.
1611-
err = ptrace(req, pid, addr+uintptr(n), uintptr(unsafe.Pointer(&buf[0])))
1612+
err = ptracePtr(req, pid, addr+uintptr(n), unsafe.Pointer(&buf[0]))
16121613
if err != nil {
16131614
return n, err
16141615
}
@@ -1640,7 +1641,7 @@ func ptracePoke(pokeReq int, peekReq int, pid int, addr uintptr, data []byte) (c
16401641
n := 0
16411642
if addr%SizeofPtr != 0 {
16421643
var buf [SizeofPtr]byte
1643-
err = ptrace(peekReq, pid, addr-addr%SizeofPtr, uintptr(unsafe.Pointer(&buf[0])))
1644+
err = ptracePtr(peekReq, pid, addr-addr%SizeofPtr, unsafe.Pointer(&buf[0]))
16441645
if err != nil {
16451646
return 0, err
16461647
}
@@ -1667,7 +1668,7 @@ func ptracePoke(pokeReq int, peekReq int, pid int, addr uintptr, data []byte) (c
16671668
// Trailing edge.
16681669
if len(data) > 0 {
16691670
var buf [SizeofPtr]byte
1670-
err = ptrace(peekReq, pid, addr+uintptr(n), uintptr(unsafe.Pointer(&buf[0])))
1671+
err = ptracePtr(peekReq, pid, addr+uintptr(n), unsafe.Pointer(&buf[0]))
16711672
if err != nil {
16721673
return n, err
16731674
}
@@ -1696,11 +1697,11 @@ func PtracePokeUser(pid int, addr uintptr, data []byte) (count int, err error) {
16961697
}
16971698

16981699
func PtraceGetRegs(pid int, regsout *PtraceRegs) (err error) {
1699-
return ptrace(PTRACE_GETREGS, pid, 0, uintptr(unsafe.Pointer(regsout)))
1700+
return ptracePtr(PTRACE_GETREGS, pid, 0, unsafe.Pointer(regsout))
17001701
}
17011702

17021703
func PtraceSetRegs(pid int, regs *PtraceRegs) (err error) {
1703-
return ptrace(PTRACE_SETREGS, pid, 0, uintptr(unsafe.Pointer(regs)))
1704+
return ptracePtr(PTRACE_SETREGS, pid, 0, unsafe.Pointer(regs))
17041705
}
17051706

17061707
func PtraceSetOptions(pid int, options int) (err error) {
@@ -1709,7 +1710,7 @@ func PtraceSetOptions(pid int, options int) (err error) {
17091710

17101711
func PtraceGetEventMsg(pid int) (msg uint, err error) {
17111712
var data _C_long
1712-
err = ptrace(PTRACE_GETEVENTMSG, pid, 0, uintptr(unsafe.Pointer(&data)))
1713+
err = ptracePtr(PTRACE_GETEVENTMSG, pid, 0, unsafe.Pointer(&data))
17131714
msg = uint(data)
17141715
return
17151716
}

unix/zptrace_armnn_linux.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zptrace_linux_arm64.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zptrace_mipsnn_linux.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zptrace_mipsnnle_linux.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zptrace_x86_linux.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zsyscall_darwin_amd64.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zsyscall_darwin_arm64.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zsyscall_freebsd_386.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zsyscall_freebsd_amd64.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)