Commit Graph
5383 Commits
Author SHA1 Message Date
Yu Watanabe 2485b7e2b8 tree-wide: use memstream-util 2023-06-01 06:48:47 +09:00
Yu Watanabe abe72100cf util: introduce memstream-util
There is many pitfalls in using memstream.
Let's introduce a wrapper to make us safely use it.
2023-06-01 06:48:43 +09:00
Daan De Meyer 05d1cbb33c Merge pull request #26969 from DaanDeMeyer/xopenat-label
fs-util: Add labelling support to xopenat()
2023-05-31 14:35:50 +02:00
Daan De Meyer 420d2e3136 fs-util: Add XOpenFlags with XO_LABEL flag to have xopenat() MAC label files/dirs 2023-05-31 13:15:56 +02:00
Daan De Meyer a452c807a4 label: Introduce LabelOps to do pre/post labelling operations
By default, label_ops is initialized with a NULL pointer which translates
to noop labelling operations. In mac_selinux_init() and the new mac_smack_init(),
we initialize label_ops with a MAC specific LabelOps pointer.

We also introduce mac_init() to initialize any configured MACs and replace all
usages of mac_selinux_init() with mac_init().
2023-05-31 13:15:53 +02:00
Zbigniew Jędrzejewski-Szmek 78435d620c basic/user-util: attach pointer symbol to return type, not function
Surrounding code was predominantly using this style already, let's make things
consistent.
2023-05-30 16:03:32 +02:00
Yu Watanabe 7b975e9f45 basic/syscall: update syscall list
Only notable change is that memfd_secret is now defined on s390(x).
2023-05-29 06:59:10 +08:00
Mike Yuan d65c289fbf time-util,format-table: add relative_monotonic variant for timestamp 2023-05-26 13:29:54 +08:00
Lennart Poettering 0f50da0f6b Merge pull request #26959 from poettering/creds-mount-dep-fix
credential ramfs mount order fixes
2023-05-25 02:06:47 -07:00
Yu Watanabe cd8910f053 Merge pull request #27770 from mrc0mmand/more-nallocfuzz-shenanigans
A couple of fixes for potential issues during OOM situations
2023-05-25 17:15:37 +09:00
Frantisek Sumsal f392dfb5a1 tree-wide: check memstream buffer after closing the handle
When closing the FILE handle attached to a memstream, it may attempt to
do a realloc() that may fail during OOM situations, in which case we are
left with the buffer pointer pointing to NULL and buffer size > 0. For
example:

```
    #include <errno.h>
    #include <stdio.h>
    #include <stdlib.h>

    void *realloc(void *ptr, size_t size) {
        return NULL;
    }

    int main(int argc, char *argv[])
    {
        FILE *f;
        char *buf;
        size_t sz = 0;

        f = open_memstream(&buf, &sz);
        if (!f)
            return -ENOMEM;

        fputs("Hello", f);

        fflush(f);
        printf("buf: 0x%lx, sz: %lu, errno: %d\n",
                    (unsigned long) buf, sz, errno);
        fclose(f);
        printf("buf: 0x%lx, sz: %lu, errno: %d\n",
                    (unsigned long) buf, sz, errno);

        return 0;
    }
```

```
$ gcc -o main main.c
$ ./main
buf: 0x74d4a0, sz: 5, errno: 0
buf: 0x0, sz: 5, errno: 0
```

This might do unexpected things if the underlying code expects a valid
pointer to the memstream buffer after closing the handle.

Found by Nallocfuzz.
2023-05-24 21:59:10 +02:00
David Tardon d6f2cd671c tree-wide: use free_and_replace() more 2023-05-24 14:59:07 +02:00
Frantisek Sumsal 1469386b13 env-file: use free_and_replace() 2023-05-24 13:56:22 +02:00
Lennart Poettering 1483892a42 mount: check right before invoking /bin/umount if it makes sense
Notifications from /proc/self/mountinfo are async, so if we stop a
service (and while doing so get rid of the credentials mount point of
it), then it will take a while until the notification reaches us and we
actually scan the table again. In particular as we nowadays ratelimit
notifications on the table, since it's so inefficient. And as I learnt
the ratelimiting is actually quite regularly hit during shutdown, where
a flurry of umount events are genreated. Hence, let's check if a mount
point is actually a mountpoint before trying to unmount it. And if it
isn't let's wait for the notification to come in.

(This race might be triggred not just by us on ourselves btw: there are
other daemons that unmount stuff when stopping where the race also
exists, but might simply be harder to trigger: if during service
shutdown these services remove some mount then they might collide with
us doing the same. After all, we have the rule to unmount everything
mounted automatically for you during shutdown.)

In the long run we should also start making us of this when it becomes
available: https://github.com/util-linux/util-linux/issues/2132 With
that we can make issues like this go away entirely from our side of
things at least.

Fixes: #25527
2023-05-24 10:57:16 +02:00
Lennart Poettering 13f37e6e97 log: propagate max log level into glibc's setlogmask()
Follow-up for: #27734

It makes sense to propagate the select log level we maintain also into
glibc, so that any code that uses syslog() directly that ends up in our
processes (libraries and such) are affected by our settings the same way
as we are ourselves.
2023-05-23 09:53:58 -07:00
Zbigniew Jędrzejewski-Szmek a777a59243 firstboot: process the root account after sysusers created it
We would create root account from sysusers or from firstboot, depending on
which one ran earlier. Since firstboot offers more options, in particular can
set the root password, we needed to order it earlier. This created an ugly
ordering requirement:

systemd-sysusers.service > systemd-firstboot.service > ... >
  systemd-remount-fs.service > systemd-tmpfiles-setup-dev.service >
  systemd-sysusers.service

We want sysusers.service to create basic users, so we can create nodes in dev,
so we can operate on block devices and such, so that we can resize and remount
things. But at the same time, systemd-firstboot.service can only work if it is
run early, before systemd-sysusers.service has created /etc/passwd. We can't
have it both ways: the units that want to have a fully writable root file
system cannot be ordered before units which are required to do file system
preparation.

Instead of trying to order firstboot very early, let's let it do its thing even
if it is started later. Instead of refusing to create to the root account if
/etc/passwd and /etc/shadow exist, actually check if the account is configured.
Now sysusers writes root account with password PASSWORD_UNPROVISIONED
("!unprovisioned"), and then firstboot checks for this, and will configure root
in this case.

This allows sysusers to be executed earlier (or accounts to be set up earlier
in another way).

This effectively reverts b825ab1a99.
2023-05-23 15:09:39 +02:00
Zbigniew Jędrzejewski-Szmek c42bac6a60 basic/user-util: return -ESRCH if passwd/group are missing 2023-05-23 14:41:24 +02:00
Zbigniew Jędrzejewski-Szmek cb1cc1acd4 Merge pull request #27703 from keszybz/systemd-vconsole-ordering
Order systemd-vconsole-setup before getty units
2023-05-22 13:27:33 +02:00
Mike Yuan 35c0e3444d conf-parser: move config_get_dropin_files to conf-files 2023-05-21 15:46:25 +08:00
Yu Watanabe 24d724087f Merge pull request #27701 from poettering/switch-root-same-file
switch-root: use same_files() logic when checking whether new and old root dir are actually the same
2023-05-20 05:39:28 +09:00
Luca Boccassi 382e18fd95 Merge pull request #27438 from bluca/dump_ratelimit
manager: restrict Dump*() to privileged callers or ratelimit
2023-05-19 21:29:58 +01:00
Zbigniew Jędrzejewski-Szmek e019ea738d pid1: order units using TTYVHangup= after vconsole setup
The goal of this change is to delay getty services until after
systemd-vconsole-setup has finished. systemd-vconsole-setup starts loadkeys,
and it seems that when loadkeys is interrupted by the TTY hangup call we do
when starting tty services [1], so that loadkeys starts getting EIO from the
ioctl("/dev/tty1", KDSKBENT) syscall it does.

Fixes #26908.

[1] https://github.com/legionus/kbd/issues/92#issuecomment-1554451788

Initially I wanted to add ordering dependencies to individual units, but
TTYVHangup= can be added to other various external units too. The solution with
an implicit dependency should cover those cases too.
2023-05-19 17:46:30 +02:00
Lennart Poettering 3530fd8913 namespace-util: use inode_same_at() instead of FORMAT_PROC_FD_PATH()
Doesn't matter much, but this makes it leas magic and independent of
/proc/ mounts. (Well, it actually doesn't, since the right-hand path is
also in /proc/, but still...
2023-05-19 17:42:41 +02:00
Lennart Poettering 563e684689 stat-util: rename files_same() → inode_same()
Let's be more accurate about what this function does: it checks whether
the underlying reported inode is the same. Internally, this already uses
a better named stat_inode_same() call, hence let's similarly name the
wrapping function following the same logic.

Similar for files_same_at() and path_equal_or_same_files().

No code changes, just some renaming.
2023-05-19 17:42:41 +02:00
Lennart Poettering 676ade310a stat-util: add _at() variant for files_same() 2023-05-19 17:41:08 +02:00
Luca Boccassi 53d6987f9e ratelimit: add ratelimit_left helper 2023-05-19 15:17:35 +01:00
Lennart Poettering b7106af465 tree-wide: remove (void) cast around sync()
The call is void anyway, it doesn't return an failure indication. Hence,
no need to cast void to (void)...

(We got this right in most cases, but forgot some)
2023-05-19 14:21:21 +02:00
Mike Yuan 329f4b06f5 Merge pull request #27659 from yuwata/memfd-seal
memfd-util: handle F_SEAL_EXEC flag
2023-05-16 22:00:57 +08:00
Yu Watanabe 52e2672af2 memfd-util: set F_SEAL_EXEC flag if supported 2023-05-16 18:50:39 +09:00
Yu Watanabe 17915ea5b3 memfd-util: memfd may also have F_SEAL_EXEC flag
Follow-up for c29715a8f7.

Fixes #27608.
2023-05-16 18:48:32 +09:00
Yu Watanabe 8e3303333f missing: add more F_SEAL_XYZ flags 2023-05-16 18:46:16 +09:00
Mike Yuan 38f901791f unit-file: support UpheldBy= in [Install] settings (adding Upholds= deps
from .upholds/)

Closes #26896
2023-05-15 15:04:38 +08:00
Daan De Meyer 32b0be0eb8 Merge pull request #27565 from yuwata/static-destruct
static-destruct: support clearing array on exit
2023-05-12 08:36:43 +02:00
Yu Watanabe bc52801034 tree-wide: drop _pure_ attribute from non-pure functions
Prompted by #27595.
Follow-ups for 6723c28f36.
2023-05-10 15:06:24 +09:00
Sam James eb29296937 dirent: conditionalize dirent assert based on dirent64 existence
>=musl-1.2.4 doesn't define dirent64 and its LFS friends as its "native"
functions are already LFS-aware.

Check for dirent64 in meson.build and only assert if it exists.

Bug: https://bugs.gentoo.org/905900
Closes: https://github.com/systemd/systemd/pull/25809
2023-05-10 15:05:18 +09:00
Yu Watanabe 9695b0c01b static-destruct: introduce STATIC_ARRAY_DESTRUCTOR_REGISTER() 2023-05-09 17:53:42 +09:00
Yu Watanabe 555ead8985 static-destruct: several cleanups
No functional changes, preparation for later commits.
2023-05-09 17:53:42 +09:00
Yu Watanabe 3facdc7da8 memory-util: make ArrayCleanup passed to array_cleanup() const
Should not change any behavior, preparation for later commits.
2023-05-09 17:53:42 +09:00
Yu Watanabe 16cda99c73 nulstr-util: make ret_size in strv_make_nulstr() optional 2023-05-08 21:40:56 +08:00
Yu Watanabe d2132d3d8d parse-util: make parse_fd() return -EBADF
The previous error code -ERANGE is slightly ambiguous, and use more
specific one. This also drops unnecessary error handlings.

Follow-up for 754d8b9c33 and
e652663a04.
2023-05-08 09:49:55 +02:00
Nick Rosbrook 362235bf59 basic/audit-util: make a test request before enabling use of audit
If a container manager does not follow the guidance in
https://systemd.io/CONTAINER_INTERFACE/ regarding audit capabilities,
then the current check may not be sufficient to determine that audit
will function properly. In particular, when calling bind() on the audit
fd, we will get EPERM if running in a user-namespaced container.

Expand the check to make an AUDIT_GET_FEATURE request on the audit fd to
test if it is working. If this fails with ECONNREFUSED, we know it is
because the kernel does not support the use of audit outside of the
initial user namespace.

Note that the approach of this patch was suggested here:
https://github.com/systemd/systemd/pull/19443#issuecomment-829566659

Fixes: #6519
2023-05-05 21:06:16 +01:00
David Tardon e652663a04 tree-wide: use parse_fd() 2023-05-05 09:10:56 +02:00
David Tardon b8f83d7f0c fd-util: introduce parse_fd()
It's a simple wrapper for safe_atoi() that returns error if the parsed
fd is < 0 .
2023-05-05 08:54:28 +02:00
Yu Watanabe 8c59feed5e missing_fs: also define struct file_clone_range
Follow-up for b640e274a7.

Addresses https://github.com/systemd/systemd/commit/b640e274a7c363a2b6394c9dce5671d9404d2e2a#r110996661.
2023-04-29 07:03:09 +02:00
Luca Boccassi 91010bd6cc Merge pull request #27440 from yuwata/reflink-follow-ups
copy: follow ups for reflink()
2023-04-28 11:34:41 +01:00
Yu Watanabe 75fd8ad008 Merge pull request #27424 from dtardon/auto-cleanup
More automatic cleanup
2023-04-28 18:46:36 +09:00
Yu Watanabe d6f96778b1 missing_fs: mention commit hash and version of ioctl commands introduced 2023-04-28 14:18:48 +09:00
Daan De Meyer b640e274a7 copy: Introduce reflink() and reflink_full()
The kernel has had filesystem independent reflink ioctls for a
while now, let's try to use them and fall back to the btrfs specific
ones if they're not supported.
2023-04-28 00:57:03 +01:00
David Tardon 3cbbefc2a7 path-util: use _cleanup_ 2023-04-27 21:42:38 +02:00
David Tardon 93eceb59e6 env-util: use more _cleanup_ in replace_env_argv() 2023-04-27 21:42:38 +02:00