82 Commits

Author SHA1 Message Date
Zygmunt Krynicki
5f7575b1d4 interfaces/systemd: replace ancient copy-pasted comment
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2024-06-18 12:37:31 +02:00
Andrew Phelps
71d22420df many: add a *SnapAppSet to ConnectedPlug/Slot types and use it to build label expressions in interfaces (#13773)
Now that we have app sets in the interfaces repo, keep a pointer to them in ConnectedPlug/Slot types. Use this to build label expressions in the interfaces. 

* many: add a pointer to a SnapAppSet into Connected(Plug|Slot) to that interfaces can build a complete label expression, including component hooks

* interfaces: update doc comments on ConnectedPlug/Slot.AppSet

* interfaces: remove TODO that has been addressed

* interfaces: use app set pointer for instance name check

* snap: add Runnable type that represents the runnable parts of a snap

* interfaces, o/ifacestate: use snap.Runnable rather than interfaces.Runnable

* interfaces, i/builtin, o/ifacestate: panic on failed invariant check in NewConnectedPlug/Slot

* interfaces: add methods to app set for getting runnables that can connect to plug/slot

* interfaces: build label expressions using runnables

* interfaces: doc comment for SlotRunnables

* interfaces: implement Slot/PlugRunnables with shared helper

* interfaces: log and skip security tags that do not match expected pattern

* snap, interfaces: move runnable constructors to methods on AppInfo and HookInfo

* interfaces: refactor to allow labelExpr to operate directly on a ConnectedPlug/Slot

* snap: move around Runnable methods
2024-06-14 18:37:26 +02:00
Oliver Calder
0ff642e82e many: include prompt prefix in apparmor rules (#13822)
* features,i/{apparmor,builtin}: include prompt prefix in home interface

If prompting is supported and enabled, include the prompt prefix in
AppArmor rules for the home interface, which will cause AppArmor to send
a prompt when accessing any file in $HOME.

In the future, if other interfaces include the ###PROMPT### prefix in
their rule snippets, this will also be handled accordingly.

At the moment, the status of prompting support is checked whenever the
AppArmor backend prepares profiles. This is okay, since AppArmor support
for prompting depends on kernel and parser features, which are only
probed once after snapd starts. However, to ensure that the same
supported value is used even if that were not the case, and in case we
wish to only use the prompt prefix for some snaps or interfaces, we may
wish to embed whether to use the prompt prefix in the AppArmor
Specification instead.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* features: adjust unsupported messages when checking apparmor features errors

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* interfaces: add prompting status to system key

Include whether prompting is supported and enabled as a single field in
the system key. This way, if `(supported && enabled)` changes, security
profiles will be regenerated when snapd starts up.

Currently, prompting support only changes when the AppArmor kernel or
parser features change, and profile regeneration is the only other place
where it is checked whether AppArmor prompting is supported and enabled.
Thus, including whether prompting is supported and enabled in the system
key ensures that security profiles are regenerated when necessary during
snapd startup, and only when necessary (e.g. not if support changed but
prompting flag remained disabled nor if flag changed but prompting
remained unsupported).

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* o/c/configcore: restart snapd when prompting value changes

When the prompting flag changes and the result entails that security
profiles should be regenerated, restart snapd to do so.

This is required iff prompting is supported and the experimental
apparmor-prompting flag changes -- if prompting is not supported,
prompting can't be used, so no need to regenerate profiles. Importantly,
prompting support is based entirely on the available AppArmor kernel and
parser features, and these are only probed once during snapd startup, so
prompting support cannot change (under the current implementation)
except when snapd restarts.

Since `(supported && enabled)` is part of the system key, and a restart
is only triggered if prompting is supported and the flag value changes
(which is equivalent to `(supported && enabled)`, since the supported
value cannot change while snapd is running), restarting after the flag
has changed causes the system key to be different, and thus to trigger a
security profile regeneration, as desired.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* interfaces,o/ifacestate: set prompting in apparmor spec

Rather than checking whether AppArmor prompting is supported and enabled
whenever the AppArmor backend is processing a snippet, instead include
that precomputed value in the Specification itself, and place it there
via `buildConfinementOptions`. This way, any spec created with the same
`confinementOptions` will make the same decision as to whether to
include prompt prefixes on relevant rules.

Currently, `buildConfinementOptions` simply checks whether prompting is
supported and enabled via the methods on `features.AppArmorPrompting`,
but ideally, this value would be looked up from either the system key
or by checking whether the prompting listener is running. It remains to
be seen how the value computed as part of the system key can be
guaranteed to be the same as that used elsewhere, either in
`buildConfinementOptions` or when deciding whether to start the
listener.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* o/c/configcore: request snapd restart whenever prompting flag changes

Previously, a snapd restart was only requested when the status of the
"apparmor-prompting" experimental feature flag changed and prompting was
supported. However, since prompting support is dependent on AppArmor
kernel and parser features which are probed only once during startup,
and systems which do not use vendored AppArmor may have had an update to
the system AppArmor package which newly supports AppArmor prompting, it
is safer to request a restart of snapd to re-check for prompting
support.

This way, if one is enabling prompting for the first time on a system
without prompting support, they can have snapd installed, update their
kernel or apparmor installation to support prompting, and then set the
prompting flag to enable prompting without needing to manually restart
snapd.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* interfaces: support optional trailing space after ###PROMPT###

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

i/apparmor: move promptReplacer definition to before its use

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* tests: add test of restart behavior when setting experimental.apparmor-prompting

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* tests: refactor prompting test to reset failed count and safely check for restarts

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

tests: add shellcheck exception for apparmor prompting flag restart test

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* tests: check that snapd PID != 0 and use snap changes to wait for feature change to complete

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* tests: check for start-limit-hit before calling reset-failed

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* tests: add ubuntu core to apparmor prompting flag restart test

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* tests: check apparmor-prompting value after setting it unchanged

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* Revert "tests: check for start-limit-hit before calling reset-failed"

This reverts commit bea68516c3287fa44d6718f0794484746ae99ac5.

* tests: check systemd start-limit-hit when apparmor-prompting flag changed

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* o/configstate/configcore: add unit tests for doExperimentalApparmorPromptingDaemonRestart

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* i/builtin: add missing prompt prefix and adjust test

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* i/apparmor: add test for prompt prefix substitution

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* many: pass prompting value into system key functions

As such, we now precompute whether prompting is supported and enabled in
`InterfaceManager.StartUp()`, store it in the `InterfaceManager`
instance, and pass it into the call to `WriteSystemKey()`.

Additionally, we make `buildConfinementOptions` a method of
`InterfaceManager`, thus eliminating the need to check within the system
key functions whether prompting is supported and enabled.

However, there remains a problem that `snap run` calls
`SystemKeyMismatch()`, which previously invoked
`apparmor.ParserFeatures()` via `AppArmorPrompting.IsSupported()`, and
now calls `AppArmorPrompting.IsSupported()` directly and passes the
result into `SystemKeyMismatch()`. In either case, we really want this
to be avoided if at all possible, since `snap run` does not have access
to the cached value from the `InterfaceManager`, and thus must invoke
the `apparmor_parser` binary to check parser features whenever we want
to run any snap.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* many: don't probe parser features when checking system key mismatch

Since `snap run` calls `SystemKeyMismatch()`, we want to avoid needing
to call `AppArmorPrompting.IsSupported()` if at all possible, since this
calls `apparmor.ParserFeatures()`, which executes the `apparmor_parser`
binary. We can and should call `AppArmorPrompting.IsSupported()` when
writing the system key, but not when checking for a mismatch.

The system key written to disk should correctly hold the list of kernel
and parser features, the parser mtime, and whether or not prompting was
previously supported and enabled. We can check whether apparmor parser
features have changed by checking the parser mtime, without needing to
probe parser features -- this optimization is already used by
`SystemKeyMismatch()`. If we knew whether prompting was previously
supported (regardless of whether it was enabled), then so long as the
parser and kernel features are unchanged, we know that prompting support
is also unchanged.

Thus, if we add a second prompting-related field to the system key, this
one storing whether prompting is supported (ignoring enabled), we can
check if prompting support is unchanged without needing to call
`AppArmorPrompting.IsSupported()`.

Furthermore, `SystemKeyMismatch()` is the function in question, and if
there is any mismatch detected, it should return such as soon as
possible, regardless of what the mismatch is. Therefore, if we know that
either kernel or parser features have changed, then we can immediately
return that there is a mismatch, and we don't need to check whether
those feature changes affect prompting support.

Therefore, the new cases which we must worry about when checking for a
system key mismatch are the following, when all other system key fields
are unchanged (note that prompting must be supported in order to be
supported&&enabled):

1. supported: F, supported&&enabled: F, newFlag: F, mismatch: F
2. supported: F, supported&&enabled: F, newFlag: T, mismatch: F
3. supported: T, supported&&enabled: F, newFlag: F, mismatch: F
4. supported: T, supported&&enabled: F, newFlag: T, mismatch: T
5. supported: T, supported&&enabled: T, newFlag: F, mismatch: T
6. supported: T, supported&&enabled: T, newFlag: T, mismatch: F

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* interfaces: fix test string formatting

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* o/configstate/configcore: adjust prompting-related comments

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* tests: increase prompting check_snapd_restarted timeout and add systemd show

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* tests: reset start limit when checking if snapd restarted after prompting change

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* many: add system key extra data to hold prompting enabled status

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* many: only store one apparmor prompting value in system key

When checking for a system key mismatch, use the stored AppArmor
parser features from the system key on disk (along with the kernel
features from the newly-generated key) to check whether prompting is
supported, and AND that with the `AppArmorPrompting` value passed in
with the `SystemKeyExtraData`. If the kernel or parser features have
changed, the system key will be a mismatch anyway, so it is perfectly
safe to use the existing parser features to check for prompting support.

As such, the functions to check for prompting support have been moved
from `features` to `sandbox/apparmor`, and the support check has been
separated from the call to get `ParserFeatures()` and
`KernelFeatures()`, so that the values from the system key can be passed
in instead of invoking those functions.

Using the system key's stored parser and kernel features, there is no
need to save whether prompting is supported as part of the system key,
simplifying the key and the logic used to set the prompting value.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* tests: explicitly install jq in apparmor-prompting-flag-restart test

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* many: consolidate checks for apparmor prompting support

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* interfaces,s/apparmor: use features struct when checking prompting support

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* tests: improve logging in apparmor-prompting-flag-restart test

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* tests: fix prompting flag restart test on core18

For some reason, when snapd fails due to start-limit-hit on core18, the
snapd.failure.service starts and acquires the state lock, thus
preventing snapd from successfully becoming "active" again and leaving
it retrying at "activating". It is unclear why this happens on core18
and not elsewhere.

As a fix, when resetting the start limit, stop snapd.failure.service
manually to ensure that snapd can successfully start.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

---------

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>
2024-06-11 18:13:00 +01:00
Andrew Phelps
069b7f684e many: use interfaces.SnapAppSet in security backends (#13587) 2024-02-27 14:52:02 -05:00
Andrew Phelps
d8a2e847e5 many: introduce SnapAppSet for use in security backends (#13574)
* interfaces: introduce SnapAppSet and forward one to the security backends

* snap: implement methods on snap.Info for getting apps and hooks for slots and plugs

This will enable us to remove the Hooks fields from the SlotInfo and
PlugInfo structs.

* interfaces: implement methods on SnapAppSet in terms of methods on snap.Info

* snap, interfaces: replace usage of {Plug,Slot}Info.SecurityTags with methods on SnapAppSet

* i/builtin: replace slotAppLabelExpr and plugAppLabelExpr with corresponding methods on SnapAppSet

* snap, o/snapstate, interfaces: remove Hooks field on snap.PlugInfo and snap.SlotInfo

* builtin, interfaces: fix tests that use Specification that now have a SnapAppSet

* snap: add tests for new methods on Info

* interfaces, i/builtin: port over some tests for SnapAppSet methods {Plug,Slot}LabelExpression

* interfaces: test PlugSecurityTags and SlotSecurityTags methods

* interfaces: add doc comments to SnapAppSet and methods

* i/builtin: remove ported over tests

* interfaces, many: require that SnapAppSet methods for getting security tags are called with plug/slot that comes from correct snap

Many tests did not properly adhere to this requirment, so they had to be
modifed to modify this rule.

Additionally, a hack was inroduced in the methods for getting label
expressions on the SnapAppSet. If a plug/slot did not originate from the
same snap that the SnapAppSet was created from, then we will use the
snap.Info that the plug/slot carries in the method instead. This will
fail to work once component hooks are introduced, so this will need to
be resolved by then.

* interfaces: test fallback for using LabelExpr methods with mismatch plug/slot

* snap: correct placement of TODOs to preserve doc comments

* snap: add doc comments for Plug/Slot.Unscoped

* interfaces: test for using SecurityTagsForPlug and SecurityTagsForSlot with wrong snap

* interfaces: tweak error messages in SnapAppSet SecurityTags methods

* i/builtin: fix missed conflict

* i/apparmor: add doc comment to Specification.appSet

* snap: fix doc coment on PlugInfo.Unscoped
2024-02-19 11:57:42 +01:00
Frederik Lotter
27b88f8362 systemd: fix snapd systemd-unit stop progress notifications (#11340)
* systemd: fix snapd systemd-unit stop progress notifications

The current behaviour of the systemd stop progress code is to print
the list of systemd units not stopped after 250ms, and then after
every 20 seconds (notice ticker), erroneously print out the same list
even though some of the units may have stopped in the meantime.

However, the bigger issue is that the non-mocked (real) code starts the
stop sequence by making a blocking systemctl stop request and then
afterwords (once all units are stopped) proceed to the progress code,
which has no effect and exits immediately.

The unit tests does not pick this up because the mock systemctl does
not no anything, and therefore the simulation of progress works as
expected because the output of the 'systemctl show' progress calls are
mocked to simulate certain outcomes.

Finally, the 'Stop()' function provided a 'timeout' argument which
suggested the user could control when a stop attempt should timeout
and fail. However, this feature did not work as the original call was
blocking. Furthermore, this features does not make sense because it
creates a second place where timeouts are introduced. The first place
is in the systemd back-end where 'TimeoutStopSec' is optionally
configured by snapd. In contrast with systemd's actual timeout
mechanism described, the 'Stop()' call offered another timeout, but
this mechanism is inconsistently used all over the code-base with
different hard-coded timeout values, not matching the underlying
service defaults to configured values. In the worse case scenario
the snapd 'Stop()' would return an error due to timeout while systemd
is still busy stopping a unit, because it was a longer unit timeout
configure. The timeout value offered by snapd has no real purpose if
carefully analysed.

The following changes were made:

1. Remove the systemd Stop() timeout argument. The timeout is now
   automatically provided by systemdctl when it returns (blocking).
   This allows the timeout to always be aligned with the actual
   timeout 'TimeoutStopSec' provided by the unit, and takes into
   account systemd timeout behaviour related to 'ExecStop' commands.

2. Change the blocking systemctl stop command to async run in a go
   routine allowing progress tracking to continue in parallel. Note
   that the call to Stop() is still blocking because the progress
   code waits until either units are all stopped or the stop command
   returns.

3. Change the notice ticker to rather implement an initial notice
   silence period (1s). After this expires, the first notification
   is generated with the list of units still not stopped. This
   delay was chosen based on empirical tests performed on a
   Raspberry Pi 3 (representing slower targets) under heavy IO and
   CPU load (where it took up to 750ms for 4 services to stop).

4. Change the progress logic to always send a notification after
   the notice silence period on any change to the list of unit
   we are still waiting for, and always only poll the state of the
   remaining units.

5. Add additional unit tests to better cover the new progress and
   notification logic, and make existing tests more thorough.

6. Add critical sections inside the mocked systemctl functions to
   prevent corruption during concurrent modifications to some of
   the test suite logs and counters. This was needed because the
   Stop() is now issued from a go routine.

The following example shows the expected output for 6 systemd units
terminated with the 'Stop' function. The last four (test3 - test6)
has a random post SIGTERM delay before the process exits. The following
notifications are generated for this example after the 1s notification
silence period expires:

Waiting for "test3", "test4", "test5", "test6" to stop.
Waiting for "test3", "test4", "test5" to stop.
Waiting for "test3", "test5" to stop.
Waiting for "test3" to stop.

This patch is required by https://github.com/snapcore/snapd/pull/11113

Signed-off-by: Fred Lotter <fred.lotter@canonical.com>

* typos

* Improved the system stop progress logic.

* Number of small review improvements

* Remove redundant channel write

* reviews: add thread safety into MockSystemctl

* many: have a separate systemd.MockSystemctlWithDelay

keep MockSystemctl signature as before

as we need to introduce controlled delays only in the systemd package
own tests so far

Co-authored-by: Samuele Pedroni <pedronis@lucediurna.net>
2022-05-20 17:29:05 +02:00
Paweł Stołowski
1f6f1ee740 Fixes related to inclusive language. 2022-04-14 12:51:08 +02:00
Maciej Borzecki
1a7f6fad73 systemd, many: rename Enable/Disable to EnableNoReload/DisableNoReload
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2022-03-14 14:35:28 +01:00
Maciej Borzecki
9a397aaeb5 Merge remote-tracking branch 'upstream/master' into bboozzoo/systemd-enable-disable-no-relod 2022-03-04 11:11:26 +01:00
Maciej Borzecki
409743d9f0 interfaces/systemd: drive by typo fix
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2022-02-11 14:09:58 +01:00
Maciej Borzecki
f1bd31bbe1 interfaces/systemd: tweak error messages and comments
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2022-02-11 14:09:45 +01:00
Maciej Borzecki
8494692aed interfaces/systemd: tweak if checks
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2022-02-11 09:58:57 +01:00
Maciej Borzecki
e5595d27ce interfaces/systemd: use batched calls only, no new API calls
Drop the changes that require new API calls to DaemonReloadIfNeeded(), and only
enable batched operations.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2022-02-10 14:02:16 +01:00
Ondrej Kubik
5ed438b776 interfaces: systemd: use batched calls when setting up interfaces
Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>
2022-02-10 13:20:44 +01:00
Ondrej Kubik
d5c67ec82c interfaces: systemd: use batched calls when dismantling interfaces
Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>
2022-02-10 13:20:44 +01:00
Maciej Borzecki
afe3b455b3 interfaces/systemd: only reload systemd state when necessary
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2022-02-08 09:35:30 +01:00
Maciej Borzecki
4a89ea6e5a many: update unit tests to account for --no-reload in enable/disable
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2022-02-07 13:43:58 +01:00
Maciej Borzecki
916ba680dc systemd, many: use slice of unit names rather than vararg
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2022-01-14 17:07:27 +01:00
Ondrej Kubik
3cca8db3d5 interfaces, overlord, usersession, wrappers: update systemd Start,Stop,Restart calls with batched option
Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>
2022-01-12 10:33:56 +00:00
Samuele Pedroni
d1b236a676 interfaces/systemd: fix typos
thanks @anonymouse64
2021-07-16 18:10:25 +02:00
Samuele Pedroni
d6dfd88a0c interfaces/systemd: service name uniqueness across snaps is implicit
add comment to clarify this
2021-07-16 16:29:45 +02:00
Samuele Pedroni
02121299a5 interfaces: s/affix/suffix/
it makes sense anyway and is simpler. Thanks @jhenstridge

also bump the copyrights
2021-07-16 16:27:37 +02:00
Samuele Pedroni
3562d23173 interfaces/systemd: fix typo
thanks @mardy
2021-07-16 15:59:17 +02:00
Samuele Pedroni
73c26e8567 interfaces/systemd: slightly more informative clashing def internal errors
track which interface we are operating on to allow for this
2021-07-09 18:47:12 +02:00
Samuele Pedroni
1e9efcdb90 interfaces: make the service naming entirely internal to systemd BE
this is somewhat simpler for the callers and more consistent because
the systemd backend needs to control the naming pattern to allow
update/removal anyway
2021-07-09 18:47:12 +02:00