Skip to content

Evade DBusException - #1346

Merged
infeo merged 5 commits into
cryptomator:developfrom
purejava:fix-isSupported
Oct 21, 2020
Merged

infeo merged 5 commits into
cryptomator:developfrom
purejava:fix-isSupported

Conversation

@purejava

Copy link
Copy Markdown
Contributor

Fixes #1345

@overheadhunter

Copy link
Copy Markdown
Member

@overheadhunter overheadhunter added this to the 1.5.9 milestone Sep 15, 2020
@purejava

Copy link
Copy Markdown
Contributor Author

While I like this change (as it avoids unnecessary reflection), I wonder why a DBusException didn't get caught here:

https://github.com/cryptomator/cryptomator/blob/8874f8b3f03998359606d02afbfa2c08a58929a2/main/keychain/src/main/java/org/cryptomator/keychain/LinuxSystemKeychainAccess.java#L34-L36

That's a very good question. Thanks for pointing that out. I changed the wrong piece of code. Looking at the code again shows that this is not an uncaught exception, but a printed stack trace.

Changing the right piece of code leads to this:

06:48:34.007 [main] INFO  org.cryptomator.launcher.Cryptomator - Starting Cryptomator SNAPSHOT on Mac OS X 10.15.6 (x86_64)
06:48:34.819 [JavaFX Application Thread] INFO  o.c.ui.launcher.FxApplicationStarter - JavaFX Runtime started.
06:48:34.891 [JavaFX Application Thread] ERROR o.f.secret.simple.SimpleCollection - org.freedesktop.dbus.exceptions.DBusException: Cannot Resolve Session Bus Address: MachineId file can not be found
06:48:34.892 [JavaFX Application Thread] ERROR o.c.k.LinuxKDEWalletKeychainAccessImpl - org.freedesktop.dbus.exceptions.DBusException: Cannot Resolve Session Bus Address: MachineId file can not be found
06:48:41.329 [JavaFX Application Thread] INFO  o.cryptomator.ui.fxapp.UpdateChecker - Current version: null, lastest version: 1.5.8

I think that's much better.

@overheadhunter overheadhunter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

On a different note: Change private final Logger log to private static final Logger LOG 😉 While you're at it anyway.

@purejava

Copy link
Copy Markdown
Contributor Author

No problem.

connection = DBusConnection.getConnection(DBusConnection.DBusBusType.SESSION);
} catch (DBusException e) {
e.printStackTrace();
LOG.error(e.toString(), e.getCause());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

e.toString() most of the time is not going to give you everything that happened with that exception. If there are no security constraints on what's being thrown, just print it all over: LOG.error("Connection couldn’t be acquired because:", e); — and yes, no space after : and no need to parametrize that field either 😉

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Your logging statement produces:

06:46:52.599 [main] INFO  org.cryptomator.launcher.Cryptomator - Starting Cryptomator SNAPSHOT on Mac OS X 10.15.6 (x86_64)
06:46:52.926 [JavaFX Application Thread] INFO  o.c.ui.launcher.FxApplicationStarter - JavaFX Runtime started.
06:46:53.006 [JavaFX Application Thread] ERROR o.f.secret.simple.SimpleCollection - org.freedesktop.dbus.exceptions.DBusException: Cannot Resolve Session Bus Address: MachineId file can not be found
06:46:53.009 [JavaFX Application Thread] ERROR o.c.k.LinuxKDEWalletKeychainAccessImpl - Connection couldn’t be acquired because:
org.freedesktop.dbus.exceptions.DBusException: Cannot Resolve Session Bus Address: MachineId file can not be found
	at org.freedesktop.dbus.connections.impl.DBusConnection.lambda$determineMachineIdFile$5(DBusConnection.java:320)
	at java.base/java.util.Optional.orElseThrow(Optional.java:401)
	at org.freedesktop.dbus.connections.impl.DBusConnection.determineMachineIdFile(DBusConnection.java:320)
	at org.freedesktop.dbus.connections.impl.DBusConnection.getDbusMachineId(DBusConnection.java:303)
	at org.freedesktop.dbus.connections.impl.DBusConnection.getConnection(DBusConnection.java:149)
	at org.freedesktop.dbus.connections.impl.DBusConnection.getConnection(DBusConnection.java:169)
	at org.freedesktop.dbus.connections.impl.DBusConnection.getConnection(DBusConnection.java:228)
	at org.freedesktop.dbus.connections.impl.DBusConnection.getConnection(DBusConnection.java:184)
	at org.cryptomator.keychain.LinuxKDEWalletKeychainAccessImpl.<init>(LinuxKDEWalletKeychainAccessImpl.java:23)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
	at org.cryptomator.keychain.LinuxSystemKeychainAccess.constructKeychainAccess(LinuxSystemKeychainAccess.java:31)
	at org.cryptomator.keychain.LinuxSystemKeychainAccess.<init>(LinuxSystemKeychainAccess.java:22)
	at org.cryptomator.keychain.LinuxSystemKeychainAccess_Factory.get(LinuxSystemKeychainAccess_Factory.java:16)
	at org.cryptomator.keychain.LinuxSystemKeychainAccess_Factory.get(LinuxSystemKeychainAccess_Factory.java:6)
	at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
	at dagger.internal.SetFactory.get(SetFactory.java:126)
	at dagger.internal.SetFactory.get(SetFactory.java:37)
	at org.cryptomator.keychain.KeychainModule_ProvideSupportedKeychainFactory.get(KeychainModule_ProvideSupportedKeychainFactory.java:25)
	at org.cryptomator.keychain.KeychainModule_ProvideSupportedKeychainFactory.get(KeychainModule_ProvideSupportedKeychainFactory.java:10)
	at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
	at org.cryptomator.keychain.KeychainModule_ProvideKeychainManagerFactory.get(KeychainModule_ProvideKeychainManagerFactory.java:24)
	at org.cryptomator.keychain.KeychainModule_ProvideKeychainManagerFactory.get(KeychainModule_ProvideKeychainManagerFactory.java:9)
	at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
	at org.cryptomator.ui.common.VaultService_Factory.get(VaultService_Factory.java:28)
	at org.cryptomator.ui.common.VaultService_Factory.get(VaultService_Factory.java:10)
	at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
	at org.cryptomator.ui.fxapp.FxApplication_Factory.get(FxApplication_Factory.java:73)
	at org.cryptomator.ui.fxapp.FxApplication_Factory.get(FxApplication_Factory.java:20)
	at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
	at org.cryptomator.launcher.DaggerCryptomatorComponent$FxApplicationComponentImpl.application(DaggerCryptomatorComponent.java:1055)
	at org.cryptomator.ui.launcher.FxApplicationStarter.lambda$start$0(FxApplicationStarter.java:47)
	at com.sun.javafx.application.PlatformImpl.lambda$startup$5(PlatformImpl.java:271)
	at com.sun.glass.ui.Application.invokeAndWait(Application.java:464)
	at com.sun.javafx.tk.quantum.QuantumToolkit.runToolkit(QuantumToolkit.java:366)
	at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$startup$10(QuantumToolkit.java:280)
	at com.sun.glass.ui.Application.lambda$run$1(Application.java:153)
06:46:59.004 [JavaFX Application Thread] INFO  o.cryptomator.ui.fxapp.UpdateChecker - Current version: null, lastest version: 1.5.8

In this part of the code we basically get noticed, whether we are on a system with D-bus (Linux) or not (Windows, Mac). On an system without D-Bus, the full exception won't give us any more useful information.

So here, I'd stick with a log message that is one line long.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In this part of the code we basically get noticed, whether we are on a system with D-bus (Linux) or not (Windows, Mac). On an system without D-Bus, the full exception won't give us any more useful information.

Not entirely true. If the running OS is windows, this method does not throw a DBusException but a RuntimeException and the failure is not logged at all (good!). This would also desireable on MacOS.

On the other hand, if we are on a system with linux kernel, the stacktrace might be of interest. Also, if the connection to DBus fails, we still create the object since the exception in the constructor is not rethrown. This would lead to several other exceptions where calls on a null reference are made.

My suggestion is to change this to something similar as in LinuxSecretServiceKeychainAccessImpl with default, empty constructor and the isSupported() function acutally init the object. This is not the best solution (i would prefer making the construtor private and create a factory method to prevent reflection), but for the PR sufficient.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[...] RuntimeException and the failure is not logged at all (good!) [...]

Whether this is "good" is debatable (see #1276).

@purejava purejava Sep 27, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

On the other hand, if we are on a system with linux kernel, the stacktrace might be of interest. Also, if the connection to DBus fails, we still create the object since the exception in the constructor is not rethrown. This would lead to several other exceptions where calls on a null reference are made.

I don't think so. On a Linux system no one ever configures or installs D-Bus. It's just there as a very basic part of the Linux system that get's configured by the Linux package maintainer. Most people even don't know what D-Bus is. So why having more information on a D-Bus fail? This just tells us that D-Bus not there and so we aren't on Linux.

My suggestion is to change this to something similar as in LinuxSecretServiceKeychainAccessImpl with default, empty constructor and the isSupported() function acutally init the object. This is not the best solution (i would prefer making the construtor private and create a factory method to prevent reflection), but for the PR sufficient.

The kdewallet lib does not provide a default, empty constructor, because you cannot suppose that kdewallet is the only D-Bus functionality you'll use in your app and cannot create a new D-Bus connection for every D-Bus functionality that is required. A DBusConnection represents a connection to an application.

@infeo infeo Oct 6, 2020 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

On a Linux system no one ever configures or installs D-Bus.It's just there

Putting the stacktrace aside: I'm just wondering if there are cases, where a connection to the D-Bus system fails initially (for whatever reason). If this is possible (looking at the code it seems possible), then imho this object should not be constructed but throwing an exception in the constructor rather catching it and pretending to be something one can work with.

Of course, throwing just the exception could be dangerous, but it will be catched in the calling factory method LinuxSystemKeyChainAccess.constructKeychainAccess()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Putting the stacktrace aside: I'm just wondering if there are cases, where a connection to the D-Bus system fails initially (for whatever reason). If this is possible (looking at the code it seems possible), then imho this object should not be constructed but throwing an exception in the constructor rather catching it and pretending to be something one can work with.

Of course, throwing just the exception could be dangerous, but it will be catched in the calling factory method LinuxSystemKeyChainAccess.constructKeychainAccess()

Ok, since we are talking about software, a connection to the D-Bus system could certainly fail initially. I agree that an exception should be thrown in the catch clause after a DBus exception happened. Just as a side note: the code wasn't written to pretend to have something one can work with although an exception occurred. When you look at KeychainModule and KeychainManager you'll see, that all keychain implementations are only made available to Cryptomator if isSupported() returns true.

@infeo infeo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

An instance of this class should not be created, if we cannot establish a DBus connection, but in the current implementation we allow this.

connection = DBusConnection.getConnection(DBusConnection.DBusBusType.SESSION);
} catch (DBusException e) {
e.printStackTrace();
LOG.error(e.toString(), e.getCause());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In this part of the code we basically get noticed, whether we are on a system with D-bus (Linux) or not (Windows, Mac). On an system without D-Bus, the full exception won't give us any more useful information.

Not entirely true. If the running OS is windows, this method does not throw a DBusException but a RuntimeException and the failure is not logged at all (good!). This would also desireable on MacOS.

On the other hand, if we are on a system with linux kernel, the stacktrace might be of interest. Also, if the connection to DBus fails, we still create the object since the exception in the constructor is not rethrown. This would lead to several other exceptions where calls on a null reference are made.

My suggestion is to change this to something similar as in LinuxSecretServiceKeychainAccessImpl with default, empty constructor and the isSupported() function acutally init the object. This is not the best solution (i would prefer making the construtor private and create a factory method to prevent reflection), but for the PR sufficient.

}
} catch (Exception e) {
log.error(e.toString(), e.getCause());
LOG.error(e.toString(), e.getCause());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See my comment above. If the object is set up correctly, also here the stacktrace can be interesting.

Reconsider, if this should be changed to LOG.error("Some msg:", e)

@purejava

purejava commented Oct 7, 2020 •

Copy link
Copy Markdown
Contributor Author

I also added some more log statements, as this is strongly encouraged by devs here and hope this fits your needs.
Just as a final remark: this will produce quite a big log file on running the develop branch in an IDE on an OS other than Linux (therefore I opened this PR at all).

@infeo infeo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As long as the messages are only debug level, everything is good 😉

@infeo
infeo requested a review from overheadhunter October 8, 2020 08:38
@purejava purejava closed this Oct 8, 2020
@purejava

purejava commented Oct 8, 2020

Copy link
Copy Markdown
Contributor Author

Sorry, big fingers on my smartphone GitHub app

@purejava purejava reopened this Oct 8, 2020
@infeo
infeo merged commit 26160e9 into cryptomator:develop Oct 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Running the develop branch in an IDE on an OS other than Linux prints a DbusException

4 participants