Skip to content

Commit 9c66f73

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

2 files changed

Lines changed: 163 additions & 0 deletions

File tree

docs/sandbox-permissions.rst

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,103 @@ 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:false
545+
--device-if=input:has-input-device
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, while newer Flatpak versions which understand the
550+
conditional system will deny ``all`` due to
551+
``--device-if=all:false`` and allow only ``input`` due to
552+
``--device-if=input:has-input-device``.
553+
554+
To explicitly deny a permission that might be granted through runtime
555+
metadata or overrides ``--nosocket=NAME, --unshare=NAME`` etc. can be
556+
used::
557+
558+
--nosocket=x11
559+
560+
This denial can be combined with conditional grants to remove
561+
unconditional access while allowing conditional access::
562+
563+
--nosocket=x11
564+
--socket=x11
565+
--socket-if=x11:!has-wayland
566+
567+
This denies unconditional X11 access but allows X11 conditionally when
568+
Wayland is unavailable. Older Flatpak versions will see only the final
569+
``--socket=x11`` grant and allow X11 unconditionally, while newer
570+
versions recognise the conditional logic and evaluates it at runtime.
571+
475572
.. rubric:: Footnotes
476573

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

docs/under-the-hood.rst

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,72 @@ 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+
Permissions are written to metadata using the following rules:
89+
90+
- Unconditionally allowed permissions are written as ``NAME``
91+
- Unconditionally denied permissions are written as ``!NAME``
92+
- Conditionally allowed permissions are written as:
93+
94+
- unconditional ``NAME`` entry for compat
95+
- ``if:NAME:CONDITION`` entries
96+
97+
If the permission resets previously defined rules, an explicit ``!NAME``
98+
entry is written first, followed by the unconditional ``NAME`` entry and
99+
then the ``if:NAME:CONDITION`` entries. This is omitted when saving an
100+
application's own metadata, as opposed to overrides.
101+
102+
When parsing metadata, a non-negated unconditional ``NAME`` entry
103+
appearing before a ``if:NAME:CONDITION`` entry is treated as a
104+
compatibility fallback and does not affect the final permission
105+
state.
106+
107+
The ``fallback-x11`` socket, on pre-1.17 Flatpak versions implicitly
108+
granted ``x11`` access and at runtime X11 access was suppressed when
109+
Wayland was available, while on newer Flatpak (1.17+) it is internally
110+
converted to the conditional syntax ``if:x11:!has-wayland``. When saving
111+
the metadata, Flatpak converts ``if:x11:!has-wayland`` back to
112+
``fallback-x11`` only when it is the sole conditional on ``x11``. If
113+
additional conditionals are present, the new syntax is written directly
114+
and older Flatpak versions will not understand the conditional entries.
115+
50116
Underlying technologies
51117
-----------------------
52118

0 commit comments

Comments
 (0)