Skip to content
Snippets Groups Projects
Commit ac6b7a33 authored by akwizgran's avatar akwizgran
Browse files

Use a lock to prevent concurrent native calls.

The lock is static to ensure that if the TorService instance is destroyed and a new instance is created (eg when unbinding and then re-binding), the background threads created by the two instances don't concurrently call into the process's Tor library.
parent 09de98dd
Branches
No related tags found
1 merge request!2update upstream
......@@ -7,6 +7,7 @@ import android.os.Binder;
import android.os.FileObserver;
import android.os.IBinder;
import android.os.Process;
import android.util.Log;
import net.freehaven.tor.control.RawEventListener;
import net.freehaven.tor.control.TorControlCommands;
......@@ -24,6 +25,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
......@@ -167,6 +169,12 @@ public class TorService extends Service {
private TorControlConnection torControlConnection;
/**
* This lock must be acquired before calling createTorConfiguration() and
* held until mainConfigurationFree() has been called.
*/
private static final ReentrantLock runLock = new ReentrantLock();
private native String apiGetProviderVersion();
private native boolean createTorConfiguration();
......@@ -314,6 +322,12 @@ public class TorService extends Service {
httpTunnelPort = Integer.toString(8118);
}
if (runLock.isLocked()) {
Log.i(TAG, "Waiting for lock");
}
runLock.lock();
Log.i(TAG, "Acquired lock");
try {
createTorConfiguration();
ArrayList<String> lines = new ArrayList<>(Arrays.asList("tor", "--verify-config", // must always be here
"--RunAsDaemon", "0",
......@@ -359,6 +373,11 @@ public class TorService extends Service {
if (runMain() != 0) {
throw new IllegalStateException("Tor could not start!");
}
} finally {
mainConfigurationFree();
Log.i(TAG, "Releasing lock");
runLock.unlock();
}
} catch (IllegalStateException | IllegalArgumentException | InterruptedException e) {
e.printStackTrace();
......@@ -377,7 +396,6 @@ public class TorService extends Service {
torControlConnection.removeRawEventListener(startedEventListener);
}
shutdownTor(TorControlCommands.SIGNAL_SHUTDOWN);
mainConfigurationFree();
broadcastStatus(TorService.this, STATUS_OFF);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment