Skip to content

USB portal (cont.) - #1354

Merged
GeorgesStavracas merged 8 commits into
flatpak:mainfrom
hfiguiere:usb-portal
Dec 5, 2024
Merged

USB portal (cont.)#1354
GeorgesStavracas merged 8 commits into
flatpak:mainfrom
hfiguiere:usb-portal

Conversation

@hfiguiere

@hfiguiere hfiguiere commented Apr 30, 2024

Copy link
Copy Markdown
Collaborator

This supercede #1238

Close #1238

@hfiguiere
hfiguiere force-pushed the usb-portal branch 2 times, most recently from cb66d92 to f28ffdf Compare May 6, 2024 02:29
@hfiguiere
hfiguiere force-pushed the usb-portal branch 3 times, most recently from ce2b214 to 4907506 Compare May 20, 2024 00:51
@hfiguiere

Copy link
Copy Markdown
Collaborator Author

After a painful rebasing.

@hfiguiere hfiguiere self-assigned this Sep 5, 2024
@hfiguiere
hfiguiere marked this pull request as ready for review September 14, 2024 03:05
@hfiguiere

Copy link
Copy Markdown
Collaborator Author

Feature is linked to flatpak/flatpak#5620

I think this is ready for review.

@hfiguiere
hfiguiere force-pushed the usb-portal branch 2 times, most recently from 69843ce to 4dcfc94 Compare September 14, 2024 18:35

@swick swick left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Checked the bits outside of usb.c so far.

Comment thread src/xdp-app-info-flatpak.c Outdated
Comment thread src/xdp-utils.h Outdated
Comment thread src/xdp-utils.h Outdated
Comment thread src/xdp-app-info-flatpak.c Outdated

enumerable_devices = g_key_file_get_string_list (app_info_flatpak->flatpak_info,
"USB Devices",
"allowed-devices",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

enumerable-devices/hidden-devices

Might want to use defines for those constants.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

fixed the literal.

Comment thread src/xdg-desktop-portal.c Outdated
Comment thread src/xdp-utils.c Outdated
Comment thread src/xdp-utils.c Outdated
Comment thread src/xdp-utils.c Outdated
Comment thread src/xdp-utils.c Outdated
Comment thread src/xdp-utils.c Outdated

@swick swick left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Bunch of comments on the interface definition. Mostly about documentation but I'm a bit worried about what device removal means and how we should deal with udev properties.

Comment thread data/org.freedesktop.portal.Usb.xml Outdated
Comment thread data/org.freedesktop.portal.Usb.xml Outdated

* ``properties`` (``a{sv}``)

A list of udev properties that this device has. These properties

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm really worried about this. It essentially makes udev a stable API even though it really is not.

Comment thread data/org.freedesktop.portal.Usb.xml Outdated
Comment on lines +146 to +157
This method can only be called once, and only after calling
org.freedesktop.portal.Usb.AcquireDevices().

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should mention the #org.freedesktop.portal.Request::Response signal

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think the response signal should carry an id that should be passed to FinishAcquireDevices. That way there is no problem with calling AcquireDevices multiple times before calling FinishAcquireDevices.


There are no supported keys in the @options vardict.
-->
<method name="ReleaseDevices">

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should probably say something about the relation to the session and connection. I assume closing the session or connection implicitly also releases all devices?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Eh, there is no session involved with acquiring and releasing devices, only a connection. The question still remains: what happens when the connection is closed. Same as release? Nothing?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

when the dbus connection is closed all the devices are removed (ie released)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think my suggestion on FinishAcquireDevices would resolve this thread.


* ``action`` (``s``)

Type of event that occurred. One of "add", "change", or "remove".

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What happens when one has acquired a device and it got removed? Is the fd still usable? Only sometimes?

We don't have a mechanism for revoking the fd so a real device unplug will result in a remove with the fd becoming unusable but when the user wants to revoke a device, we can't do anything with the existing fd on the client and so it won't be.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think my suggestion on FinishAcquireDevices would resolve this thread as well.

Comment thread data/org.freedesktop.portal.Usb.xml Outdated
@devices: Array of device identifiers
@options: Vardict with optional further information

Releases previously acquired devices.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What does it mean to release a device? Is the fd still usable?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It will close the file descriptor when we release the "owned device" object. I haven't checked how libusb, for example, reacts to this.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Well, it closes the fd on the portal side which is good because we need to do that to not leak things, but it doesn't close the fd on the client side. In fact, you cannot close the fd on the client side from the portal side. The only thing we can do is revoke file descriptors but that currently only works for evdev and hidraw and not for usb. So as long as we don't get kernel support for that, clients will be able to continue to use the fd that they acquired even if the connection is closed. We should however say something in the interface description here which will allow us to revoke those USB devices if the connection gets closed, or if the user wants to revoke access manually, when we get USB revoke from the kernel.

@swick swick left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looked through the usb.c file now. Mostly nickpicks but also a few actual issues we have to work out.

Comment thread src/usb.c Outdated
Comment thread src/usb.c Outdated
Comment thread src/usb.c Outdated
Comment thread src/usb.c Outdated
Comment thread src/usb.c
Comment thread src/usb.c Outdated
g_dbus_method_invocation_return_error (invocation,
XDG_DESKTOP_PORTAL_ERROR,
XDG_DESKTOP_PORTAL_ERROR_FAILED,
"Cannot call AcquireDevices() with an unfinished "

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why not? The interface description doesn't mention this restriction at all.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The changes I proposed on AcquireDevices would resolve this thread.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

this is done in the current code.

Comment thread src/usb.c Outdated
Comment thread data/org.freedesktop.portal.Usb.xml Outdated
Comment on lines +146 to +157
This method can only be called once, and only after calling
org.freedesktop.portal.Usb.AcquireDevices().

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think the response signal should carry an id that should be passed to FinishAcquireDevices. That way there is no problem with calling AcquireDevices multiple times before calling FinishAcquireDevices.

Comment thread src/usb.c
owned_device = g_hash_table_lookup (sender_info->owned_devices, access_data->device_id);
if (!owned_device)
{
fd = open (device_file, access_data->writable ? O_RDWR : O_RDONLY);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why would opening the file fail? Is udev already telling us that we should be able to open the device node (i.e. we have permission to do it) or is do we discover about bad permissions just here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

many reason. It's IO nothing is guaranteed. The device could have disappear, could be unitialized. If it can go wrong it will go wrong.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Right, but that means we'll show USB devices to the portal clients, letting users go through the UI to approve access, just to then eventually notice that we actually can't make the device available. Would be much nicer if we didn't advertise those devices in the first place.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

there is not a lot we can do. We can call access on the device is_gudev_device_suitable() and return FALSE if the call fail. That will check for existance and read permission. At that leve we don't know if we want the write permissions.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

And we already check in handle_acquire_devices() as well.

Still a lot of race condition can occur it acquiring the device.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

we no longer do. the change just hadn't been pushed yet.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe I'm blind but I can't seem to find it? Are you sure you pushed the latest version now?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

https://github.com/flatpak/xdg-desktop-portal/pull/1354/files#diff-4ce7d34e66e5ca9b52f8e5666f0d4efe11e48278f34a7cdba6df390c14012a27R163

Which mean if it can't access it it won't list it. So open won't be attempted. In case of race condition, ie the device is no longer accessible between the list and this, then this will error normally.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ah, I missed that one. Perfect!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

actually it caused a serious regression as it didn't detect removals, and a few other things.

Comment thread src/usb.c Outdated
@hfiguiere
hfiguiere force-pushed the usb-portal branch 2 times, most recently from a258338 to b40ccee Compare September 21, 2024 00:12
@hfiguiere

Copy link
Copy Markdown
Collaborator Author

(I haven't addressed everything yet)

@hfiguiere
hfiguiere force-pushed the usb-portal branch 7 times, most recently from bdaa78e to 27f10e2 Compare September 26, 2024 03:40
@hfiguiere
hfiguiere force-pushed the usb-portal branch 3 times, most recently from a8ce8e9 to a3123a1 Compare October 9, 2024 01:47
@hfiguiere

Copy link
Copy Markdown
Collaborator Author

The test passes as I work around skipping in CI the one that fails.

Comment thread data/org.freedesktop.portal.Usb.xml Outdated
Comment thread src/xdp-app-info-flatpak.c Outdated
Comment thread src/usb.c Outdated
Comment thread src/xdp-usb-query.h Outdated
@hfiguiere
hfiguiere force-pushed the usb-portal branch 3 times, most recently from 14e2e57 to 70f9647 Compare December 3, 2024 03:05

@swick swick left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

There are a bunch of commits which fix up previous commits. I would prefer them to be squashed down.

Comment thread src/usb.c Outdated
Comment thread tests/__init__.py
Comment thread .github/workflows/container.yml Outdated
@hfiguiere

Copy link
Copy Markdown
Collaborator Author

There are a bunch of commits which fix up previous commits. I would prefer them to be squashed down.

I will squash all the USB portal stuff into one and keep the test commits as they are.

Also I will shorten the commit message but copy that content into a separate file in doc that will be added to the commit. It makes more sense to have it there.

@swick

swick commented Dec 3, 2024

Copy link
Copy Markdown
Collaborator

LGTM

@swick swick left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just some nit picking about the commits...

Comment thread src/xdp-request.c
Comment thread src/xdp-utils.c
Comment thread src/xdp-usb-query.c
hfiguiere and others added 8 commits December 4, 2024 20:19
Signed-off-by: Hubert Figuière <hub@figuiere.net>
Signed-off-by: Hubert Figuière <hub@figuiere.net>
Implement XdpUsbQuery

Co-Authored By: Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
Co-Authored-By: Ryan Gonzalez <rymg19@gmail.com>

Signed-off-by: Hubert Figuière <hub@figuiere.net>
Add a new D-Bus interface org.freedesktop.portal.Usb
See doc/usb-portal.md

Co-Authored By: Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
Co-Authored-By: Ryan Gonzalez <rymg19@gmail.com>

Signed-off-by: Hubert Figuière <hub@figuiere.net>
We want to test the USB portal which requires USB queries to determine
which USB devices should be enumerable and could potentially be
acquired. This adds an environment variable similar to the one for the
app id that the test harness can set.
This lets us control which USB devices are enumerable by setting the
fixture to valid xdp USB query.
This is because it fails as we never get the removal event. In both podman and docker

Signed-off-by: Hubert Figuière <hub@figuiere.net>
@GeorgesStavracas
GeorgesStavracas merged commit 06a5e50 into flatpak:main Dec 5, 2024
@GeorgesStavracas GeorgesStavracas added this to the 1.20 milestone Dec 5, 2024
@hfiguiere
hfiguiere deleted the usb-portal branch December 5, 2024 13:50
org.freedesktop.impl.portal.Usb:
@short_description: USB portal backend interface

This portal lets applications register global shortcuts so they can

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The description seems wrong, this has afaik nothing to do with global shortcut

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Indeed.

It also re-introduced annotations for a{sv} arguments and doesn't have annotations for the more complex types (a(sa{sv}a{sv})). Mind fixing that up?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

oops. I missed that too. Will address this.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

See #1529

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

not sure about the annotation. Will followup too.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

added the annotations to the same PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Status: Triaged

Development

Successfully merging this pull request may close these issues.

4 participants