Debian Codegraph @

doc/usb-portal.md

raw symbols SCIP
1 # USB portal
2
3 The USB portal is the middleman between sandboxed apps, and the
4 devices connected and available to the host system. This is the first
5 version of the portal.
6
7 This part was implemented by
8
9 - Hubert Figuière
10 - Georges Basile Stavracas Neto
11
12 funded by the STF (Sovereign Tech Fund) effort, based upon initial
13 work by
14
15 - Ryan Gonzalez
16
17 and with help from other contributors.
18
19 ## Device filtering
20
21 Sandboxed apps must declare which USB devices they support ahead of
22 time. This information is read by the XDG Desktop Portal and used to
23 determine which USB devices will be exposed to requesting apps. On
24 Flatpak, these enumerable and hidden devices are set by the `--usb`
25 and `--nousb` arguments against `flatpak build-finish` and `flatpak
26 run`. Neither `--devices=all` nor `--device=usb` do influence the
27 portal.
28
29 Hiding a device always take precedence over making them enumerable,
30 even when a blanket permission (`--usb=all`) is set.
31
32 However out of the sandbox we assume all devices are allowed as there
33 is no restriction that can prevent seeing anything.
34
35 Individual devices are assigned a unique identifier by the portal,
36 which is used for all further interactions. This unique identifier is
37 completely random and independent of the device. Permission checks are
38 in place to not allow apps to try and guess device ids without having
39 permission to access then.
40
41 ## Permissions
42
43 There are 2 dynamic permissions managed by the USB portal in the
44 permission store:
45
46 1. Blanket USB permission: per-app permission to use any methods of
47 the USB portal. Without this permission, apps must not be able to do
48 anything - enumerate, monitor, or acquire - with the USB portal.[^1]
49
50 2. Specific device permission: per-app permission to acquire a
51 specific USB device, down to the serial number.
52
53 ## Enumerating devices
54
55 There are 2 ways for apps to learn about devices:
56
57 - Apps can call the EnumerateDevices() method, which gives a snapshot
58 of the current devices to the app.
59
60 - Apps can create a device monitoring session with CreateSession()
61 which sends the list of available devices on creation, and also
62 notifies the app about connected and disconnected devices.
63
64 Only devices that the app is allowed to see are reported in both
65 cases.
66
67 The udev properties exposed by device enumeration is limited to a
68 well known subset of properties.[^2]
69
70 ## Device acquisition & release
71
72 Once an app has determined which devices it wants to access, the app
73 can call the AcquireDevices() method. This method may prompt a dialog
74 for the user to allow or deny the app from accessing specific devices.
75
76 If permission is granted, XDG Desktop Portal tries to open the device
77 file on the behalf of the requesting app, and pass down the file
78 descriptor to that file.[^3]
79
80 The caller must then call FinishAcquireDevices() until it indicate it
81 is finished. It's only necessary to call it more than once if there
82 are too many file descriptors to return. This is a D-Bus
83 limitation. Check the `finished` return argument.
84
85 ### Using the file descriptor
86
87 The file descriptors returned by the portal are meant to be used with
88 USB library you use.
89
90 In the case of libusb 1.0.23 and later, use `libusb_wrap_sys_device()`
91 and pass the file descriptor as the `sys_dev` argument. It should be
92 noted that libusb must be compiled with udev support (it is the
93 default) in order to be able to work. Without this it looks for the
94 nonexistent `/dev/usb` that is not present.
95
96 If you use libhidapi, you need to use the function
97 `hid_libusb_wrap_sys_device()` provided by libhidapi-usb.
98
99 ---
100
101 [^1]: Exceptionally, apps can release previously acquired devices,
102 even when this permission is disabled. This is so because we don't yet
103 have kernel-side USB revoking. With USB revoking in place, it would be
104 possible to hard-cut app access right when the app permission changes.
105
106 [^2]: This patch uses a hardcoded list. There is no mechanism for apps
107 to influence which other udev properties are fetched. This approach is
108 open to suggestions - it may be necessary to expose more information
109 more liberally through the portal. The initial attempt has been made
110 to provide sensible information useful for display in a user
111 interface.
112
113 [^3]: This is clearly not ideal. The ideal approach is to go through
114 logind's TakeDevice() method. However, that will add significant
115 complexity to the portal, since this logind method can only be called
116 by the session controller (i.e. the only executable capable of calling
117 TakeControl() in the session - usually the compositor). This can and
118 probably should be implemented in a subsequent round of improvements
119 to the USB portal.