Skip to content

Commit cb4efa1

Browse files
committed
sandbox-permissions: Add docs for conditional permissions
Fixes: flatpak/flatpak#6543
1 parent 9199ef2 commit cb4efa1

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed

docs/sandbox-permissions.rst

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,104 @@ Note that these should not have subpaths in them unless the value
472472
of the subpath can be consistently pre-determined. Block device naming
473473
depends on the kernel/fstab configuration and cannot be pre-determined.
474474

475+
Conditional permissions
476+
-----------------------
477+
478+
Since 1.17.0, Flatpak supports conditional permissions which allows
479+
them to be granted only when certain runtime conditions are satisfied
480+
and fallback otherwise. The intention of the system is to allow
481+
users or developers to specify tighter permission grants (as they are
482+
added in new Flatpak versions) while fallback to older grants for
483+
backwards compatibility at run time.
484+
485+
.. note::
486+
487+
Older Flatpak versions will fail when encountering unknown
488+
commandline options, while unrecognized metadata entries will be
489+
silently ignored.
490+
491+
Flatpak manifests using conditional flags (for example,
492+
``--socket-if=`` etc.) will require Flatpak 1.17.0 or newer to
493+
build and attempting to build them with older Flatpak versions will
494+
produce an error.
495+
496+
The following flags are available to specify conditional
497+
permissions in CLI and in Flatpak manifests::
498+
499+
--socket-if=
500+
--device-if=
501+
--share-if=
502+
--allow-if=
503+
504+
The syntax of all the options are ``--socket-if=PERMISSION:CONDITION``
505+
and so on where ``PERMISSION`` is the available grants for that flag
506+
(e.g., those listed for ``--socket=``). Conditions can be negated by
507+
prefixing with ``!``. The following conditions are supported:
508+
509+
- ``true`` - Always evaluates to true
510+
- ``false``- Always evaluates to false
511+
- ``has-input-device`` - True if the Flatpak version supports
512+
``--device=input``
513+
- ``has-wayland`` - True if the current desktop session supports
514+
Wayland
515+
516+
Multiple conditionals can be specified for the same grant, in which
517+
case the permission is granted if any condition matches. If no
518+
conditional rule evaluates to ``true``, the grant is denied
519+
unless it is also unconditionally allowed. Duplicate conditions are
520+
ignored.
521+
522+
Examples
523+
`````````
524+
525+
#. The unconditional grant ``--socket=x11`` can be tightened using
526+
conditional permissions to ``--socket-if=x11:!has-wayland``. This
527+
allows access to X11 only when a Wayland desktop session is not
528+
available. To preserve backwards compatibility, the following
529+
pattern can be used::
530+
531+
--socket=x11
532+
--socket-if=x11:!has-wayland
533+
534+
This allows older Flatpak versions which do not understand the
535+
conditional permissions function by allowing X11 access always
536+
while newer Flatpak, which understands the conditional system will
537+
allow X11 access only when the session is not Wayland.
538+
539+
#. If an application requires only access to ``input`` device permission,
540+
the following flags can be used to move away from ``--device=all``
541+
to ``--device=input``::
542+
543+
--device=all
544+
--device-if=all:!has-input-device
545+
--device=input
546+
547+
This allows older Flatpak versions which do not understand the
548+
``input`` device permission to function by having the broader
549+
``all`` access. Newer Flatpak versions which understand the
550+
conditional system (and therefore understands the ``input``
551+
permission) will deny ``all`` due to
552+
``--device-if=all:!has-input-device`` and allow only ``input`` due
553+
to ``--device=input``.
554+
555+
To explicitly deny a permission that might be granted through runtime
556+
metadata or overrides ``--nosocket=NAME, --unshare=NAME`` etc. can be
557+
used::
558+
559+
--nosocket=x11
560+
561+
This denial can be combined with conditional grants to remove
562+
unconditional access while allowing conditional access::
563+
564+
--nosocket=x11
565+
--socket=x11
566+
--socket-if=x11:!has-wayland
567+
568+
This denies unconditional X11 access but allows X11 conditionally when
569+
Wayland is unavailable. Older Flatpak versions will see only the final
570+
``--socket=x11`` grant and allow X11 unconditionally, while newer
571+
versions recognise the conditional logic and evaluates it at runtime.
572+
475573
.. rubric:: Footnotes
476574

477575
.. [#f1] This is not necessarily required, but without it the X11 shared

docs/under-the-hood.rst

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,85 @@ stored once.
4747
This `blog post <https://blogs.gnome.org/alexl/2017/10/02/on-application-sizes-and-bloat-in-flatpak/>`_
4848
explains the underlying structure of a Flatpak repository in more detail.
4949

50+
Conditional permission system
51+
-----------------------------
52+
53+
Since Flatpak 1.17.0, conditional permissions allow permissions to be
54+
granted only when certain runtime conditions are satisfied, with
55+
fallback to unconditional grants for compatibility with older versions.
56+
57+
Permissions are internally represented as:
58+
59+
- unconditionally allowed or denied
60+
- a reset flag indicating whether the current layer overrides rules
61+
from lower layers
62+
- a set of conditional rules under which the permission may be allowed
63+
64+
For example:
65+
66+
- ``--socket=NAME`` unconditionally allows the permission and resets any
67+
previously defined rules for that permission
68+
- ``--nosocket=NAME`` unconditionally denies the permission and resets
69+
any previously defined rules
70+
- ``--socket-if=NAME:CONDITION`` adds a conditional rule without
71+
resetting existing rules
72+
73+
Conditions may be negated using ``!``.
74+
75+
Multiple conditional rules can be specified for the same permission. In
76+
this case, the permission is granted if any condition evaluates to true.
77+
78+
Duplicate conditions are ignored. The order of conditions does not
79+
affect evaluation.
80+
81+
If no conditional rules are present, the permission is granted only if
82+
it is unconditionally allowed.
83+
84+
If conditional rules are present, the permission is granted if any
85+
condition evaluates to true, and denied otherwise, unless it is also
86+
unconditionally allowed.
87+
88+
If an unconditional entry follows a conditional entry for the same
89+
grant in commandline flags, the earlier unconditional entry is treated
90+
as backwards compatibility fallback and does not affect the final
91+
permission state. So the following is effectively treated as
92+
``--socket-if=x11:!has-wayland`` in Flatpak versions supporting
93+
conditional permissions::
94+
95+
--socket=x11
96+
--socket-if=x11:!has-wayland
97+
98+
Permissions are written to metadata using the following rules:
99+
100+
- Unconditionally allowed permissions are written as ``NAME``
101+
- Unconditionally denied permissions are written as ``!NAME``
102+
- Conditionally allowed permissions are written as:
103+
104+
- unconditional ``NAME`` entry for compat
105+
- ``if:NAME:CONDITION`` entries
106+
107+
If the permission resets previously defined rules, an explicit ``!NAME``
108+
entry is written first, followed by the unconditional ``NAME`` entry and
109+
then the ``if:NAME:CONDITION`` entries. This is omitted when saving an
110+
application's own metadata, as opposed to overrides.
111+
112+
When parsing metadata, a non-negated unconditional ``NAME`` entry
113+
appearing before a ``if:NAME:CONDITION`` entry is treated as a
114+
compatibility fallback and does not affect the final permission
115+
state. Eg. ``sockets=x11;if:x11:!has-wayland;`` is effectively treated
116+
as ``if:x11:!has-wayland`` in Flatpak versions supporting
117+
conditional permissions.
118+
119+
The ``fallback-x11`` socket, on pre-1.17 Flatpak versions implicitly
120+
granted ``x11`` access and at runtime X11 access was suppressed when
121+
Wayland was available, while on newer Flatpak (1.17+) it is internally
122+
converted to the conditional syntax ``if:x11:!has-wayland``. When saving
123+
the metadata, Flatpak converts ``if:x11:!has-wayland`` back to
124+
``fallback-x11`` only when it is the sole conditional on ``x11``. If
125+
additional conditionals are present, the new syntax is written directly
126+
and older Flatpak versions will not understand the conditional entries.
127+
A conditional grant for ``fallback-x11`` is not allowed.
128+
50129
Underlying technologies
51130
-----------------------
52131

0 commit comments

Comments
 (0)