gomonkey icon indicating copy to clipboard operation
gomonkey copied to clipboard

macOS 10.15 syscall.Mprotect panic: permission denied

Open luckyzachary opened this issue 6 years ago • 30 comments

Hi: After I was update my macOS to about 10.14, It will report panic: permission denied when I run ApplyFunc or ApplyMethod. I searched this problem in google, but I don't have answer. Which permission I should set? Thanks a lot. This is panic stack.

panic: permission denied [recovered] panic: permission denied

goroutine 13 [running]: testing.tRunner.func1(0xc4202081e0) /.../goroot/go/src/testing/testing.go:742 +0x29d panic(0x4e697e0, 0xc4205a7c18) /.../goroot/go/src/runtime/panic.go:502 +0x229 github.com/agiledragon/gomonkey.modifyBinary(0x4bf4570, 0xc420229a7c, 0xc, 0xc) /.../gopath/src/github.com/agiledragon/gomonkey/modify_binary_darwin.go:11 +0x198 github.com/agiledragon/gomonkey.replace(0x4bf4570, 0x5099668, 0xc420229b58, 0x59aa580, 0x4dd7500) /.../gopath/src/github.com/agiledragon/gomonkey/patch.go:164 +0x112 github.com/agiledragon/gomonkey.(*Patches).applyCore(0xc420086c40, 0x4dd7560, 0x5099ba8, 0x13, 0x4dd7560, 0x5099668, 0x13, 0x10) /.../gopath/src/github.com/agiledragon/gomonkey/patch.go:140 +0x161 github.com/agiledragon/gomonkey.(*Patches).ApplyFunc(0xc420086c40, 0x4dd7560, 0x5099ba8, 0x4dd7560, 0x5099668, 0x4009d7d) /.../gopath/src/github.com/agiledragon/gomonkey/patch.go:60 +0xbf github.com/agiledragon/gomonkey.ApplyFunc(0x4dd7560, 0x5099ba8, 0x4dd7560, 0x5099668, 0x2ae) /.../gopath/src/github.com/agiledragon/gomonkey/patch.go:22 +0xa2

luckyzachary avatar Oct 18 '19 10:10 luckyzachary

My Mac OS is 10.13.6, it has no this problem. You can try it with the root user.

agiledragon avatar Oct 18 '19 12:10 agiledragon

My Mac OS is 10.13.6, it has no this problem. You can try it with the root user.

Catalina(10.15.2) has this problem. Sudo can't solve the problem

cheaterlin avatar Jan 10 '20 13:01 cheaterlin

does anybody figure this out

kakisong avatar Jan 12 '20 10:01 kakisong

getting the same problem as well. anyone solved it?

itaymeller avatar Jan 14 '20 19:01 itaymeller

I am also facing this issue any idea what's the resolution?

abhineet-plivo avatar Jan 22 '20 11:01 abhineet-plivo

me too. By another patch tool: https://github.com/bouk/monkey#notes, bouk said Monkey won't work on some security-oriented operating system that don't allow memory pages to be both write and execute at the same time. With the current approach there's not really a reliable fix for this. Maybe this is the cause.

larryyu2 avatar Feb 03 '20 09:02 larryyu2

I don't know how to fix this ,but I found a way to bypass this. Use the docker with official golang image, map your gopath to the container's gopath, then in the container's terminal, you can run the tests likes in your mac.

larryyu2 avatar Feb 05 '20 09:02 larryyu2

+1 macos 10.15.4 go version go1.14.2 darwin/amd64

hello2mao avatar Apr 21 '20 09:04 hello2mao

// modify_binary_darwin.go err := syscall.Mprotect(page, syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC)

If your macOS is Catalina 10.15.x, please try to replace "err := syscall.Mprotect(page, syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC)" with "err := syscall.Mprotect(page, syscall.PROT_READ|syscall.PROT_WRITE)" or "err := syscall.Mprotect(page, syscall.PROT_WRITE)".

agiledragon avatar May 31 '20 23:05 agiledragon

Please try printf '\x07' | dd of=<executable> bs=1 seek=160 count=1 conv=notrunc.

Refer to https://stackoverflow.com/questions/60654834/using-mprotect-to-make-text-segment-writable-on-macos .

tobegit3hub avatar Jun 01 '20 02:06 tobegit3hub

I don't know how to fix this ,but I found a way to bypass this. Use the docker with official golang image, map your gopath to the container's gopath, then in the container's terminal, you can run the tests likes in your mac.

Good idea! Maybe it is the best way to fix this.

agiledragon avatar Jun 04 '20 14:06 agiledragon

Please try printf '\x07' | dd of=<executable> bs=1 seek=160 count=1 conv=notrunc.

Refer to https://stackoverflow.com/questions/60654834/using-mprotect-to-make-text-segment-writable-on-macos .

Does anyone try this?

agiledragon avatar Jun 08 '20 16:06 agiledragon

Please try printf '\x07' | dd of=<executable> bs=1 seek=160 count=1 conv=notrunc. Refer to https://stackoverflow.com/questions/60654834/using-mprotect-to-make-text-segment-writable-on-macos .

Does anyone try this?

I don't know how to set <executable>

brzyangg avatar Jun 16 '20 12:06 brzyangg

Please try printf '\x07' | dd of=<executable> bs=1 seek=160 count=1 conv=notrunc. Refer to https://stackoverflow.com/questions/60654834/using-mprotect-to-make-text-segment-writable-on-macos .

Does anyone try this?

It works on my Mac with macOS 10.15.5.

Please try printf '\x07' | dd of=<executable> bs=1 seek=160 count=1 conv=notrunc. Refer to https://stackoverflow.com/questions/60654834/using-mprotect-to-make-text-segment-writable-on-macos .

Does anyone try this?

I don't know how to set <executable>

go test actually includes three steps:

  1. compile the test functions into a binary
  2. execute the binary
  3. delete the binary

To work around this issue, we need to first generate the test binary, the modify the compiled binary with dd. e.g.

$ go test -c -o test-bin mytest/abc

With -c and -o option, go test will generate a binary named test-bin. Then modify the binary with dd according to https://stackoverflow.com/questions/60654834/using-mprotect-to-make-text-segment-writable-on-macos#:

$  printf '\x07' | dd of=test-bin bs=1 seek=160 count=1 conv=notrunc

Finally, you can run the test binary:

./test-bin

paco0x avatar Jun 22 '20 09:06 paco0x

// modify_binary_darwin.go err := syscall.Mprotect(page, syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC)

If your macOS is Catalina 10.15.x, please try to replace "err := syscall.Mprotect(page, syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC)" with "err := syscall.Mprotect(page, syscall.PROT_READ|syscall.PROT_WRITE)" or "err := syscall.Mprotect(page, syscall.PROT_WRITE)".

i try this ,but i can't solve the problem,

LaryCaka avatar Jun 23 '20 14:06 LaryCaka

// modify_binary_darwin.go err := syscall.Mprotect(page, syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC) If your macOS is Catalina 10.15.x, please try to replace "err := syscall.Mprotect(page, syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC)" with "err := syscall.Mprotect(page, syscall.PROT_READ|syscall.PROT_WRITE)" or "err := syscall.Mprotect(page, syscall.PROT_WRITE)".

i try this ,but i can't solve the problem,

There are two methods:

  1. Use the docker with official golang image, map your gopath to the container's gopath, then in the container's terminal, you can run the tests likes in your mac.

  2. A brute workaround is to manually modify and set __TEXT(max_prot) to 0x7 after linking. printf '\x07' | dd of= bs=1 seek=160 count=1 conv=notrunc Refer to https://stackoverflow.com/questions/60654834/using-mprotect-to-make-text-segment-writable-on-macos .

agiledragon avatar Jun 25 '20 04:06 agiledragon

Please try printf '\x07' | dd of=<executable> bs=1 seek=160 count=1 conv=notrunc. Refer to https://stackoverflow.com/questions/60654834/using-mprotect-to-make-text-segment-writable-on-macos .

Does anyone try this?

It works on my Mac with macOS 10.15.5.

Please try printf '\x07' | dd of=<executable> bs=1 seek=160 count=1 conv=notrunc. Refer to https://stackoverflow.com/questions/60654834/using-mprotect-to-make-text-segment-writable-on-macos .

Does anyone try this?

I don't know how to set <executable>

go test actually includes three steps:

  1. compile the test functions into a binary
  2. execute the binary
  3. delete the binary

To work around this issue, we need to first generate the test binary, the modify the compiled binary with dd. e.g.

$ go test -c -o test-bin mytest/abc

With -c and -o option, go test will generate a binary named test-bin. Then modify the binary with dd according to https://stackoverflow.com/questions/60654834/using-mprotect-to-make-text-segment-writable-on-macos#:

$  printf '\x07' | dd of=test-bin bs=1 seek=160 count=1 conv=notrunc

Finally, you can run the test binary:

./test-bin

can solve the problem,But it's too cumbersome to execute every time

So,It is recommended to use docker to solve this problem。

zhangzhen8230 avatar Jul 09 '20 09:07 zhangzhen8230

Please try printf '\x07' | dd of=<executable> bs=1 seek=160 count=1 conv=notrunc. Refer to https://stackoverflow.com/questions/60654834/using-mprotect-to-make-text-segment-writable-on-macos .

Does anyone try this?

It works on my Mac with macOS 10.15.5.

Please try printf '\x07' | dd of=<executable> bs=1 seek=160 count=1 conv=notrunc. Refer to https://stackoverflow.com/questions/60654834/using-mprotect-to-make-text-segment-writable-on-macos .

Does anyone try this?

I don't know how to set <executable>

go test actually includes three steps:

  1. compile the test functions into a binary
  2. execute the binary
  3. delete the binary

To work around this issue, we need to first generate the test binary, the modify the compiled binary with dd. e.g.

$ go test -c -o test-bin mytest/abc

With -c and -o option, go test will generate a binary named test-bin. Then modify the binary with dd according to https://stackoverflow.com/questions/60654834/using-mprotect-to-make-text-segment-writable-on-macos#:

$  printf '\x07' | dd of=test-bin bs=1 seek=160 count=1 conv=notrunc

Finally, you can run the test binary:

./test-bin

can solve the problem,But it's too cumbersome to execute every time

So,It is recommended to use docker to solve this problem。

i also meet the problem and use this method printf '\x07' | dd of=test-bin bs=1 seek=160 count=1 conv=notrunc fix it .

but i want to know that if gomonkey can fix it ?

kamly avatar Jul 25 '20 07:07 kamly

Please try printf '\x07' | dd of=<executable> bs=1 seek=160 count=1 conv=notrunc. Refer to https://stackoverflow.com/questions/60654834/using-mprotect-to-make-text-segment-writable-on-macos .

Does anyone try this?

It works on my Mac with macOS 10.15.5.

Please try printf '\x07' | dd of=<executable> bs=1 seek=160 count=1 conv=notrunc. Refer to https://stackoverflow.com/questions/60654834/using-mprotect-to-make-text-segment-writable-on-macos .

Does anyone try this?

I don't know how to set <executable>

go test actually includes three steps:

  1. compile the test functions into a binary
  2. execute the binary
  3. delete the binary

To work around this issue, we need to first generate the test binary, the modify the compiled binary with dd. e.g.

$ go test -c -o test-bin mytest/abc

With -c and -o option, go test will generate a binary named test-bin. Then modify the binary with dd according to https://stackoverflow.com/questions/60654834/using-mprotect-to-make-text-segment-writable-on-macos#:

$  printf '\x07' | dd of=test-bin bs=1 seek=160 count=1 conv=notrunc

Finally, you can run the test binary:

./test-bin

can solve the problem,But it's too cumbersome to execute every time So,It is recommended to use docker to solve this problem。

i also meet the problem and use this method printf '\x07' | dd of=test-bin bs=1 seek=160 count=1 conv=notrunc fix it .

but i want to know that if gomonkey can fix it ?

+1

ddavidzhang avatar Sep 21 '20 09:09 ddavidzhang

https://github.com/eisenxp/macos-golink-wrapper may help.

eisenxp avatar Dec 22 '20 05:12 eisenxp

https://github.com/eisenxp/macos-golink-wrapper may help.

It may be the best solution of this problem.

aduan avatar Feb 01 '21 06:02 aduan

https://github.com/eisenxp/macos-golink-wrapper may help.

It's work well!

JasonRock avatar Feb 24 '21 14:02 JasonRock

The best way is to use docker. Another way is https://github.com/eisenxp/macos-golink-wrapper . Thanks a lot @eisenxp .

luckyzachary avatar May 18 '21 08:05 luckyzachary

i found a repository: https://github.com/eisenxp/macos-golink-wrapper my colleagues have tried this method, all problems were settled

hsqStephenZhang avatar Aug 06 '21 08:08 hsqStephenZhang

arm64 MacOS can use amd64 version go, I change to go1.16.11 darwin/amd64,and https://github.com/eisenxp/macos-golink-wrapper

butcoder avatar Apr 01 '22 09:04 butcoder

谢谢哟,愿你开心每一天~

LaryCaka avatar Apr 01 '22 09:04 LaryCaka

arm64 MacOS可以使用amd64版本go,我换成go1.16.11 darwin/amd64,和https://github.com/eisenxp/macos-golink-wrapper

it will be get other question. such as: can not debug. golang version: go version go1.16.15 darwin/arm64 system : mac m1 goland: 2021.3(m1 soc) 如果把GOARCH改成amd64,会导致无法debug。我新遇到的问题

guyunzh avatar Jul 13 '22 06:07 guyunzh

谢谢哟,愿你开心每一天~

LaryCaka avatar Jul 13 '22 06:07 LaryCaka

v2.11.0 has been released!

agiledragon avatar Nov 10 '23 14:11 agiledragon

谢谢哟,愿你开心每一天~

LaryCaka avatar Nov 10 '23 14:11 LaryCaka