From d6a8e9d4dc8d6b5eb5b2e0341ef6e43aeb9c5c49 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Thu, 6 May 2021 10:45:43 +0200 Subject: tunnel: avoid race between shutdown and stats wgTurnOff can block for a while, in which case, calling getStatistics will use a stale handle and stale tunnel. Not only that, but wgGetConfig might return null, in which case string.split throws. java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] java.lang.String.split(java.lang.String)' on a null at com.wireguard.android.backend.GoBackend.getStatistics Reported-by: tomt@adslweb.co.uk Link: https://lists.zx2c4.com/pipermail/wireguard/2021-May/006709.html Signed-off-by: Jason A. Donenfeld --- .../main/java/com/wireguard/android/backend/GoBackend.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java index c7381487..8b9213db 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java @@ -68,7 +68,7 @@ public final class GoBackend implements Backend { alwaysOnCallback = cb; } - private static native String wgGetConfig(int handle); + @Nullable private static native String wgGetConfig(int handle); private static native int wgGetSocketV4(int handle); @@ -115,10 +115,11 @@ public final class GoBackend implements Backend { @Override public Statistics getStatistics(final Tunnel tunnel) { final Statistics stats = new Statistics(); - if (tunnel != currentTunnel) { + if (tunnel != currentTunnel || currentTunnelHandle == -1) return stats; - } final String config = wgGetConfig(currentTunnelHandle); + if (config == null) + return stats; Key key = null; long rx = 0; long tx = 0; @@ -294,11 +295,11 @@ public final class GoBackend implements Backend { Log.w(TAG, "Tunnel already down"); return; } - - wgTurnOff(currentTunnelHandle); + int handleToClose = currentTunnelHandle; currentTunnel = null; currentTunnelHandle = -1; currentConfig = null; + wgTurnOff(handleToClose); } tunnel.onStateChange(state); -- cgit v1.2.3-59-g8ed1b