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

D-Bus: Send reply on auth failure #548

Merged
merged 2 commits into from
Aug 17, 2022

Conversation

hartwork
Copy link
Contributor

In reaction to comment #546 (comment) by @benzea.

The effect can be seen e.g. when running…

$ dbus-send --system --print-reply --dest=org.usbguard1 /org/usbguard1 org.usbguard1.getParameter string:ImplicitPolicyTarget

… (as non-root) and then hitting the Cancel button in the PolKit auth dialog. Previously, dbus-send would continue to run waiting for a reply that is not coming with this pull request applied it shuts down properly saying:

Error org.freedesktop.DBus.Error.AccessDenied: Not authorized.

as expected.

Note that there are no changes to whether interactive authentication is requested from Polkit.

CC @benzea @radosroka @muelli

@hartwork
Copy link
Contributor Author

hartwork commented Jun 5, 2022

@benzea @radosroka @muelli any thoughts? Thank you! 🙏

@benzea
Copy link

benzea commented Jun 7, 2022

I would say, two possibilities:

  1. Make the function itself return the invocation. As-is, you need to duplicate code for every call, which is just ugly.
  2. Exceptions seem quite natural here (and you already have exception handling in handle_method_call of gdbus-server.cpp. So, add an exception that contain the DBus error type and throw that. Then you can just have a call assertAuthorizedByPolikit without any further logic.

@hartwork
Copy link
Contributor Author

hartwork commented Jun 7, 2022

@benzea thanks for your review. Did you get a chance to try out the function aspect of this change — does it work for you as expected?
Before I reply to your comment more: I'm happy if someone refactors this function more, later. Personally, I'm aiming for "fix the problem without adding new bugs" here for the moment.

I would say, two possibilities:

  1. Make the function itself return the invocation. As-is, you need to duplicate code for every call, which is just ugly.

I considered that option, but pulling that into assertAuthorizedByPolikit would arguable make it do too much and would not longer fit that name; it also has many exit points, so doing more for each exit may even increase duplication. So I did consider it, and picked the lesser evil, or so I thought. PS: I should note that similar calls to g_dbus_method_invocation_return_error_* happen near and outside of assertAuthorizedByPolikit already, so the current additions match that current pattern or "approach to encapsulation", in a way.

  1. Exceptions seem quite natural here (and you already have exception handling in handle_method_call of gdbus-server.cpp. So, add an exception that contain the DBus error type and throw that. Then you can just have a call assertAuthorizedByPolikit without any further logic.

The current code in isAuthorizedByPolkit is quite C-like and was merged being as C-like as it is. Moving this code here to exceptions now without adding memory leaks and without introducing smart pointers without significant risk and effort is not something I can offer at the moment. I'm hoping we can fix the bug in the current C-like function in a C-like way, and then someone other than me can refactor this to be super modern C++ after, if desired. That way probably everyone will be happy. Or if an alternative implementation of this that is more C++-like is realistic to have better chances to be merged sooner and with not introducing any more bugs than this version here, please everyone feel encouraged to create an alternative pull request, and I'll be happy if your version gets merged and also fixes the bug and is closer to authentic C++.

What do you think?

@hartwork
Copy link
Contributor Author

@radosroka any thoughts?

2 similar comments
@hartwork
Copy link
Contributor Author

@radosroka any thoughts?

@hartwork
Copy link
Contributor Author

@radosroka any thoughts?

@Cropi Cropi self-requested a review August 15, 2022 06:22
@Cropi
Copy link
Member

Cropi commented Aug 15, 2022

Hello,

I like the idea of introducing an automatic reply on authentication failure. At first, I thought that this was a security measure similar to when you want to change to a different user but enter the wrong password and it lets you wait a few seconds.

@@ -93,7 +97,11 @@ namespace usbguard
}

if (method_name == "setParameter") {
if (! isAuthorizedByPolkit(invocation)) {
GDBusError authErrorCode = G_DBUS_ERROR_FAILED;
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to bundle the following lines? Doing so we could remove duplicate code as these occur many times.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't really see how, at least not in C++. Any concrete ideas how to?

Copy link
Member

Choose a reason for hiding this comment

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

I can see two possibilities here:

@Cropi Cropi merged commit 32a30ed into USBGuard:master Aug 17, 2022
@Cropi
Copy link
Member

Cropi commented Aug 17, 2022

Thanks for the PR!

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.

3 participants