Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace GoString with our own function to fix mte issue #6727

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion wireguard-go-rs/libwg/libwg.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func wgSetConfig(tunnelHandle int32, cSettings *C.char) C.int32_t {
tunnel.Logger.Errorf("cSettings is null\n")
return ERROR_INVALID_ARGUMENT
}
settings := C.GoString(cSettings)
settings := goStringFixed(cSettings)

setError := tunnel.Device.IpcSetOperation(bufio.NewReader(strings.NewReader(settings)))
if setError != nil {
Expand All @@ -114,3 +114,24 @@ func wgFreePtr(ptr unsafe.Pointer) {
}

func main() {}

//Instead of using the normal version of C.GoString we need to use the version that takes
//a length argument (C.GoStringN). That is because the normal C.GoString reads a whole
//page of memory to determine the length of the string. This causes a crash if
//mte is turned on. So instead we determine the length of the c string by reading
//each character until we reach the end of the string.
func goStringFixed(cString *C.char) string {
ptr := unsafe.Pointer(cString)
i := 0
for {
byte := (*C.char)(unsafe.Pointer(uintptr(ptr) + uintptr(i)))

if *byte == 0 {
break
}

i += 1
}

return C.GoStringN(cString, C.int(i))
}
3 changes: 2 additions & 1 deletion wireguard-go-rs/libwg/libwg_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func wgTurnOn(cSettings *C.char, fd int, logSink LogSink, logContext LogContext)
logger.Errorf("cSettings is null\n")
return ERROR_INVALID_ARGUMENT
}
settings := C.GoString(cSettings)

settings := goStringFixed(cSettings)

tunDevice, _, err := tun.CreateUnmonitoredTUNFromFD(fd)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion wireguard-go-rs/libwg/libwg_daita.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func wgActivateDaita(tunnelHandle C.int32_t, peerPubkey *C.uint8_t, machines *C.
return ERROR_UNKNOWN_PEER
}

if !peer.EnableDaita(C.GoString((*C.char)(machines)), uint(eventsCapacity), uint(actionsCapacity), maxPaddingBytes, maxBlockingBytes) {
if !peer.EnableDaita(goStringFixed((*C.char)(machines)), uint(eventsCapacity), uint(actionsCapacity), maxPaddingBytes, maxBlockingBytes) {
return ERROR_ENABLE_DAITA
}

Expand Down
2 changes: 1 addition & 1 deletion wireguard-go-rs/libwg/libwg_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func wgTurnOn(mtu int, cSettings *C.char, fd int, logSink LogSink, logContext Lo
logger.Errorf("cSettings is null\n")
return ERROR_INVALID_ARGUMENT
}
settings := C.GoString(cSettings)
settings := goStringFixed(cSettings)

file := os.NewFile(uintptr(fd), "")
tunDevice, err := tun.CreateTUNFromFile(file, mtu)
Expand Down
Loading