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

TorProcessManager: Check our permissions wrt Tor process #9960

Merged

Conversation

kiminuo
Copy link
Collaborator

@kiminuo kiminuo commented Jan 18, 2023

Fixes #9518

This is an idea how to fix the issue #9518. It's a bit hackish but I don't know about a better solution at the moment. Ideas are welcome.

Testing

On Windows:

  • I have tested the PR by running Tor under admin account and starting WW by my non-priviledged account.
  • I have tested starting WW to start a Tor process, terminating WW and starting WW again and it worked (with the running Tor process).

Clement is testing on macOS.

Not yet tested on Linux.

@kiminuo kiminuo changed the title TorProcessManager: Check our permissions wrt Tor process TorProcessManager: Check our permissions wrt Tor process Jan 18, 2023
@kiminuo kiminuo requested a review from yahiheb January 18, 2023 08:12
@kiminuo kiminuo requested a review from turbolay January 19, 2023 10:14
@turbolay
Copy link
Collaborator

turbolay commented Jan 19, 2023

This is not working if you start Wasabi on another account.
This line is throwing TorControlException: Cookie file doesn't exist:

controlClient = await InitTorControlAsync(cancellationToken).ConfigureAwait(false);

It's because the tor related files are created at a different than expected location.

@turbolay
Copy link
Collaborator

turbolay commented Jan 23, 2023

@kiminuo this code is working (yours not):

diff --git a/WalletWasabi/Tor/TorProcessManager.cs b/WalletWasabi/Tor/TorProcessManager.cs
index bb4d519dd..16911b02f 100644
--- a/WalletWasabi/Tor/TorProcessManager.cs
+++ b/WalletWasabi/Tor/TorProcessManager.cs
@@ -120,6 +120,10 @@ public class TorProcessManager : IAsyncDisposable
 
                                if (isAlreadyRunning)
                                {
+                               
+                                       Logger.LogInfo($"Tor is already running on {Settings.SocksEndpoint}");
+                                       controlClient = await InitTorControlAsync(cancellationToken).ConfigureAwait(false);
+                                       
                                        // Tor process can crash even between these two commands too.
                                        int processId = await controlClient.GetTorProcessIdAsync(cancellationToken).ConfigureAwait(false);
                                        process = new ProcessAsync(Process.GetProcessById(processId));
@@ -128,8 +132,6 @@ public class TorProcessManager : IAsyncDisposable
                                        // Especially, we want to make sure that Tor is running under our user and not a different one.
                                        nint _ = process.Handle;
 
-                                       Logger.LogInfo($"Tor is already running on {Settings.SocksEndpoint}");
-                                       controlClient = await InitTorControlAsync(cancellationToken).ConfigureAwait(false);
                                }
                                else
                                {
@@ -180,7 +182,16 @@ public class TorProcessManager : IAsyncDisposable
                                else
                                {
                                        // If Tor was already started, we don't have Tor process ID (pid), so it's harder to kill it.
-                                       foreach (Process torProcess in Process.GetProcessesByName(TorSettings.GetTorBinaryFileName()))
+                                       Process[] torProcesses = Process.GetProcessesByName(TorSettings.GetTorBinaryFileName());
+                                       
+                                       // Tor was started by another user and we can't kill 
+                                       if (torProcesses.Length == 0)
+                                       {
+                                               setNewTcs = false;
+                                               exception = new TorControlException("Tor was started by another user and we can't use nor kill it");
+                                               throw;
+                                       }
+                                       foreach (Process torProcess in torProcesses)
                                        {
                                                try
                                                {

I think this nint _ = process.Handle; is now useless? As the exception is raised by InitTorControlAsync

@yahiheb yahiheb requested a review from molnard January 23, 2023 08:22
@pull-request-size pull-request-size bot added size/M and removed size/S labels Jan 23, 2023
@@ -350,7 +379,10 @@ public async ValueTask DisposeAsync()

if (LoopTask is Task t)
{
await t.ConfigureAwait(false);
if (!t.IsFaulted)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to throw an exception doing dispose.

@@ -240,6 +264,11 @@ private async Task RestartingLoopAsync(CancellationToken globalCancellationToken
}
}

internal virtual Process[] GetTorProcesses()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To mock it.

@kiminuo
Copy link
Collaborator Author

kiminuo commented Jan 23, 2023

Clement: Thanks, very useful. I have modified it a bit more and added a test. Please test if you can.

I think this nint _ = process.Handle; is now useless? As the exception is raised by InitTorControlAsync

It works for the scenario with Tor being run under admin account on Windows and WW being run under a non-admin account. That's maybe not that common scenario but why not to cover it, I guess.

@kiminuo kiminuo marked this pull request as ready for review January 23, 2023 08:46
@@ -176,7 +190,17 @@ private async Task RestartingLoopAsync(CancellationToken globalCancellationToken
else
{
// If Tor was already started, we don't have Tor process ID (pid), so it's harder to kill it.
foreach (Process torProcess in Process.GetProcessesByName(TorSettings.GetTorBinaryFileName()))
Process[] torProcesses = GetTorProcesses();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a key bit because on macOS it gives only yours processes, on linux it should be the same.

@molnard
Copy link
Collaborator

molnard commented Jan 23, 2023

Before I review this: so with this, the user will get a crash report, that he will be able to see that somebody else running tor in a different user profile?

@kiminuo
Copy link
Collaborator Author

kiminuo commented Jan 23, 2023

That's how it is supposed to work. Yes.

@molnard
Copy link
Collaborator

molnard commented Jan 23, 2023

That's how it is supposed to work. Yes.

🙏 I will test it tomorrow, my mac is not here right now.

Copy link
Collaborator

@turbolay turbolay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK for specific case: Another user has wasabi (tor) started on macOS

I think this doesn't work in that case: Wasabi is launched using sudo.
With elevated right I think it would kill the tor process of the other Wasabi instance.
Not sure how big of a problem this is.

@molnard
Copy link
Collaborator

molnard commented Jan 24, 2023

Not sure how big of a problem this is.

It is not.

Copy link
Collaborator

@molnard molnard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK

@molnard molnard merged commit 206a25b into zkSNACKs:master Jan 24, 2023
@kiminuo kiminuo deleted the feature/2023-01-18-Tor-and-multiuser-scenario branch January 24, 2023 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tor making Wasabi to crash silently - multiuser
3 participants