678 Commits

Author SHA1 Message Date
Maciej Borzecki
faa6d3bcaf osutil/fips: helper for testing whether FIPS mode is enabled (#14080)
Add a package with a helper for testing whether FIPS mode is enabled.

Signed-off-by: Maciej Borzecki <maciej.borzecki@canonical.com>
2024-06-17 13:46:30 +02:00
Zygmunt Bazyli Krynicki
7706a79721 osutil: switch to -u UID:GID for strace-static (#13950)
* osutil: switch to -u UID:GID for strace-static

This moves us off the custom patch and onto an upstream feature
we've heled develop. The feature is not released yet but the
patch has been integrated into the strace-static snap.

Jira: SNAPDENG-19870

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

* tests: make strace test less fragile

The test tried to carefully match the error message to the version of strace
used, which in turn depends on the host OS. It's much easier to just check both
error mesages.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

* tests: avoid systemctl kill which has issues on systemd 255

On systemd 255 systemctl kill fails after attempting to kill snapd.service with
the following message:

  $ sudo systemctl kill --signal=SIGKILL snapd.service
  Failed to kill unit snapd.service: Failed to send signal SIGKILL to auxiliary processes: Invalid argument

Kill the pid by hand to avoid triggering this behavior.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

---------

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2024-05-22 21:26:44 +02:00
Zygmunt Bazyli Krynicki
d78bbedcb1 many: generalize wording of NFS workaround (#13758)
Ahead of introduction of CIFS workaround, generalize the names so that we use
more general language rather instead of focusing on NFS.

As a special exception the externally visible wording related to NFS is kept
intact in two places:

 - The apparmor "nfs-support" file name
 - The system key "nfs-home" key.

From points of view this is all an elaborate internal rename that should nto be
observable outside of snapd, apart from log messages that may no longer speak of
NFS but of remote file systems.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2024-04-04 19:22:17 +02:00
Zygmunt Krynicki
7a45e851ae osutil: extend nfs-based home to support cifs
NFS and CIFS share the same shortcoming, that on kernels older than 6.8 (but I
could be wrong on the version number), accessing files there causes apparmor to
require network permissions even though the process performing the access is
entirely unaware of the fact that network file system is in use.

Snapd contained a work-around for this behavior, with specific detection logic
looking for NFS file system mounted or declared at a specific place. Extend the
logic to handle NFS and CIFS equally, to give better support to SAMBA or
Windows Server-based home directories.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2024-04-04 08:46:18 +02:00
Miguel Pires
79c5ac14b2 many: remove usages of deprecated io/ioutil package (#13768)
* many: remove usages of deprecated io/ioutil package

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>

* .golangci.yml: remove errcheck ignore rule for io/ioutil

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>

* run-checks: prevent new usages of io/ioutil

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>

---------

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2024-04-03 23:23:24 +02:00
Zygmunt Krynicki
2ab0074172 strace: Re-format comment regarding strace compatiblity.
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2024-03-08 09:22:50 +01:00
Zygmunt Krynicki
c50bb3c3ea strace: break out strace user handling to a helper
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2024-03-08 09:22:50 +01:00
Tony Espy
87069b1bc5 strace: use --gid/--uid options
This commit updates snapd's useage of strace to use the new
--gid/--uid cmdline options instead of -u to work around an
issue that causes issues if strace is statically linked (due
to libnss).

Signed-off-by: Tony Espy <espy@canonical.com>
2024-03-08 09:22:50 +01:00
Alfonso Sánchez-Beato
9712748dff kernel,overlord: modify EnsureKernelDriversTree so it can handle
kernel-modules components.
2024-02-21 07:55:42 +00:00
Philip Meulengracht
54073169f5 i/b: dont have polkit interface being implicit on core read-only filesystems (#13568)
* i/b: dont have polkit interface being implicit on core read-only filesystems

* i/b: fix TestStaticInfo

* osutil: add some defs for macos

* osutil: update copyright date
2024-02-16 09:02:28 +01:00
Ondra Kubik
99d5b808cd osutil: move to useradd from adduser (#13236)
* osutil: move to useradd from adduser

adduser is perl based tool. Moving to useradd removes this dependency.

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* osutil: useradd: minor fixes as recomended in PR

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* osutil: useradd: remove redundant userTool variable

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* osutil: useradd: set default  tool and alter if needed

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* osutil: move to useradd: improve comment text

Co-authored-by: Maciej Borzecki <maciek.borzecki@gmail.com>

* osutil: remove --badname from the useradd call as it was doing nothing for our use-case. The regex used by snapd is already sufficient and a lot more strict.

Remove + from allowed characters in usernames, both normal and system, as they were not allowed by adduser anyway.

* osutil: fix typo of available

* osutil: remove obsolete part of the comment

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* osutil: mention disabled passwords for useradd

Signed-off-by: Maciej Borzecki <maciej.borzecki@canonical.com>

---------

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>
Signed-off-by: Maciej Borzecki <maciej.borzecki@canonical.com>
Co-authored-by: Maciej Borzecki <maciek.borzecki@gmail.com>
Co-authored-by: Philip Meulengracht <philip.meulengracht@canonical.com>
Co-authored-by: Maciej Borzecki <maciej.borzecki@canonical.com>
2024-02-08 20:07:59 +01:00
Andrew Phelps
933c138a64 seed: add Copier interface and implement it for seed20 (#13495) 2024-01-30 10:32:22 -05:00
Ernest Lotter
fdc90dfe41 many: ensure-dir mounts for personal-files missing dirs (#13260)
* many: ensure-dir mount entries from personal-files write attrs

* many: review improvements

* strutil: make pathiter current path slash trimming use existing method

* osutil, strutil: more review fixes

* i, i/apparmor, i/builtin, osutil: improve unit test coverage

* i, i/apparmor, i/builtin, i/mount: review improvements

* strutil: improve comment

* interfaces/apparmor: allow snap-update-ns to open home directory

* tests: revert interfaces-personal-files changes to simplify merge

* interfaces/builtin: improve plug connect error message

Co-authored-by: Miguel Pires <miguelpires94@gmail.com>

* interfaces/builtin: fixed ut

---------

Co-authored-by: Miguel Pires <miguelpires94@gmail.com>
2023-12-15 15:25:49 +02:00
Valentin David
0954b5ac79 gadget: add kernel command-line remove filter to gadget yaml 2023-10-26 09:34:20 +02:00
Alfonso Sánchez-Beato
b4f263bba8 many: replace exec.CombinedOutput when output is parsed
Some invocations to external programs used exec.CombinedOutput, that
combines stdout and strerr into a single byte array. This can be an
issue if this output is parsed, as many programs print debug output or
warnings to stderr and that data is unexpected by the parsers. This
patch changes to using osutil.RunSplitOutput or osutil.RunCmd (that
return separately stdout and stderr) when we need to parse stdout, and
also in some other cases when printing separately both streams could
be helpful. Fixes LP #1885597.
2023-10-05 14:14:28 +02:00
Alfonso Sánchez-Beato
83b705f2a7 osutil: add methods to combine std{out/err} into a single error object 2023-10-05 14:14:28 +02:00
Alfonso Sánchez-Beato
bad061e63f osutil: add RunCmd and RunSplitOutput functions
These functions return separately std{err,out}, and will be used to
replace calls to exec.CombinedOuput.
2023-10-05 14:14:28 +02:00
alfonsosanchezbeato
8b353cc46b gadget,osutil: add support for fat16 partitions (#13123)
* gadget,osutil: support FAT16 as filesystem

Add additional filesystem fat16 in the gadget specification.

* gadget: add method to check if filesystem identified by linux

matches the one in the gadget. This is not an exact match anymore
because of the introduction of fat16 as filesystem in gadget, which is
still seen as vfat by linux (udev shows fat bits in ID_FS_VERSION
while keeping ID_FS_TYPE=vfat).

* gadget,osutil: use vfat-16 instead of fat16, and vfat-32 as

alternative to vfat.

* gadget: add method to convert gadget to linux filesystem

Now the filesystem field in the gadget is not exactly the same as the
string used in Linux, so we need conversions in differente places.

* gadget: replace some simple methods with calls to LinuxFilesystem()
2023-09-27 12:10:07 +02:00
Oliver Calder
402fddec36 osutil/epoll: make e.Wait{,Timeout}() return immediately when e.Close() is called (#13204)
* osutil/epoll: make `e.Wait{,Timeout}()` return immediately when `e.Close()` is called

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

* osutil/epoll: make `WaitTimeout()` reference initial time

Previously, the elapsed time was computed as the total time spent
waiting on the `epoll_wait` syscall, but when waiting for a timeout, one
is generally more concerned with the time which has elapsed since
initiating the call, rather than the total time internally spent waiting
on the syscall (to the extent that can even be accurately measured).

This commit ensures that the function returns once the time since the
start of the call exceeds the given duration.

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

* osutil/epoll: added explicit `IsClosed()` function

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

* osutil/epoll: made epoll file descriptor thread safe

Importantly, this change also makes `Close()` thread safe, eliminating
the previous risk that `close(e.closed)` could be called more than once,
thus causing a panic.

Additionally, the `zeroRegisteredFdCount()` function has been removed,
since guaranteeing that `e.registeredFdCount` is `0` after `Close()`
while other threads may be about to increment/decrement the count
requires additional overhead.  It may also be useful to query the number
of registered file descriptors at time of close, and the function
otherwise served no purpose.

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

* osutil/epoll: use `chan struct{}` instead of `chan interface{}`

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

* osutil/epoll: make `IsClosed()` atomic

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

* osutil/epoll: make `IsClosed()` use `closed` channel

Thus, it is not necessary to set `e.fd = -1` when the channel is closed,
which is redundant information with the `closed` channel and meant that
reads and writes of `e.fd` needed to be atomic. Now, `e.fd` is never
changed after the epoll instance is created, so there is no need for
atomic reads/writes of `e.fd`.

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

* osutil/epoll: remove public `Fd()` method

Since `e.fd` is no longer modified, there is no need to internally
control reads of it in any special way, and thus no internal need for a
helper function.

Additionally, we very much do not want outside callers to close the
epoll file descriptor directly, as this should always be done via the
`Close()` method. Therefore, by unexporting access to internal `e.fd`,
this discourages users from closing `e.fd` directly.

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

* osutil/epoll: improved race condition checks in tests

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

* osutil/epoll: added doc comment to `IsClosed()`

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

* osutil/epoll: adjusted file header

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

* osutil/epoll: added `IsClosed()` checks after syscalls

If the epoll instance is closed during a syscall, this could cause the
syscall to return EBADF.  However, since closing the epoll instance is
the cause of this, we want to return `ErrEpollClosed` instead of
`EBADF`.

By checking `IsClosed()` immediately after the syscall returns (and
before checking the error returned by the syscall), we are guaranteed to
return `ErrEpollClosed` if the epoll instance was closed during a
syscall, regardless of whether closing the instance was the cause of an
error during the syscall.

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

* osutil/epoll: Only return ErrEpollClosed if error occurred

For methods which invoke syscalls, the epoll instance may be closed
during the syscall, which may cause the syscall to return an error.

Previously, we checked (after the syscall returned) if the epoll
instance was now closed, and if so, returned ErrEpollClosed.  However,
there is a race between the end of the syscall and the check for whether
the epoll instance has been closed, so it may be that the syscall
completed successfully, either because the close occurred after the
syscall completed, or because the close did not trigger an error in the
syscall.  Regardless, we should not return an error if no error actually
occurred.

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

* osutil/epoll: remove unnecessary type from error variable

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

---------

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>
2023-09-26 12:13:57 -05:00
Miguel Pires
29c9752d66 many: s/ioutil.WriteFile/os.WriteFile (#13217)
Replace ioutil.WriteFile with os.WriteFile since the former has been
deprecated since go1.16 and simply calls the latter.

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2023-09-26 11:38:46 +01:00
Dimitri John Ledkov
d55f3123d4 go: go fix with 1.18
Apply `go fix ./...` to the whole code base using go-1.18 to remove
old build tags.
2023-09-25 13:22:14 +02:00
Andrew Phelps
40ad1bb7b7 osutil: unset SHELL variable to handle non-posix login shell in tests (#13187)
* osutil: unset SHELL variable to handle non-posix login shell in tests
2023-09-12 15:04:07 +01:00
Samuele Pedroni
17e2322358 osutil/epoll: clarify helper name 2023-09-08 13:28:08 +02:00
Samuele Pedroni
69f1a5f946 osutil/epoll: make TestEpollWaitEintrHandling -race clean 2023-09-08 13:28:08 +02:00
Valentin David
1def7c6bfd osutil/kcmdline: provide marshalers for ArgumentPattern (#13144)
* osutil/kcmdline: provide marshalers for ArgumentPattern

* fixup!: review comments
2023-09-06 10:20:59 +02:00