Commit graph

3881 commits

Author SHA1 Message Date
Lennart Poettering ae2a15bc14 macro: introduce TAKE_PTR() macro
This macro will read a pointer of any type, return it, and set the
pointer to NULL. This is useful as an explicit concept of passing
ownership of a memory area between pointers.

This takes inspiration from Rust:

https://doc.rust-lang.org/std/option/enum.Option.html#method.take

and was suggested by Alan Jenkins (@sourcejedi).

It drops ~160 lines of code from our codebase, which makes me like it.
Also, I think it clarifies passing of ownership, and thus helps
readability a bit (at least for the initiated who know the new macro)
2018-03-22 20:21:42 +01:00
juergbi 39362f6f7d main: add NoNewPrivileges config option (#8475)
This makes it possible to disable new privileges for the whole system.
2018-03-21 23:41:19 +01:00
Zbigniew Jędrzejewski-Szmek 37cbc1d579 When mangling names, optionally emit a warning (#8400)
The warning is not emitted for absolute paths like /dev/sda or /home, which are
converted to .device and .mount unit names without any fuss.

Most of the time it's unlikely that users use invalid unit names on purpose,
so let's warn them. Warnings are silenced when --quiet is used.

$ build/systemctl show -p Id hello@foo-bar/baz
Invalid unit name "hello@foo-bar/baz" was escaped as "hello@foo-bar-baz" (maybe you should use systemd-escape?)
Id=hello@foo-bar-baz.service

$ build/systemd-run --user --slice foo-bar/baz --unit foo-bar/foo true
Invalid unit name "foo-bar/foo" was escaped as "foo-bar-foo" (maybe you should use systemd-escape?)
Invalid unit name "foo-bar/baz" was escaped as "foo-bar-baz" (maybe you should use systemd-escape?)
Running as unit: foo-bar-foo.service

Fixes #8302.
2018-03-21 15:26:47 +01:00
Zbigniew Jędrzejewski-Szmek 55c36ec0c1
Merge pull request #8508 from poettering/more-cocci
two new coccinelle rules files and their results
2018-03-21 12:50:49 +01:00
Lennart Poettering 0ba6791f46
Merge pull request #8368 from yuwata/nss-systemd-getpwent
nss-systemd: make dynamic users enumerable by `getent`
2018-03-21 12:36:47 +01:00
Lennart Poettering 31dc1ca3bf move MANAGER_IS_RELOADING() check into manager_recheck_{dbus|journal}() (#8510)
Let's better check this inside of the call than before it, so that we
never issue this while reloading, even should these calls be called due
to other reasons than just the unit notify.

This makes sure the reload state is unset a bit earlier in
manager_reload() so that we can safely call this function from there and
they do the right thing.

Follow-up for e63ebf71ed.
2018-03-21 12:03:45 +01:00
Lennart Poettering ed1738a24a
Merge pull request #8487 from keszybz/oss-fuzz-fixes
Oss fuzz fixes, another batch
2018-03-21 11:50:57 +01:00
Lennart Poettering 2062ada74c selinux: let's fully (and statically) initialize log callback union (#8512)
We can make this const and static, and initialize this ahead of time and
fully, hence let's do that.
2018-03-21 11:48:40 +01:00
Yu Watanabe f9bfa6962d core: add new dbus method GetDynamicUsers
This intruduces a new dbus method GetDynamicUsers for systemd1.Manager,
which enumerates all dynamic users realized in the system.
2018-03-21 13:11:01 +09:00
Zbigniew Jędrzejewski-Szmek e3c3d6761b core/load-fragment: reject overly long paths early
No need to go through the specifier_printf() if the path is already too long in
the unexpanded form (since specifiers increase the length of the string in all
practical cases).

In the oss-fuzz test case, valgrind reports:
  total heap usage: 179,044 allocs, 179,044 frees, 72,687,755,703 bytes allocated
and the original config file is ~500kb. This isn't really a security issue,
since the config file has to be trusted any way, but just a matter of
preventing accidental resource exhaustion.

https://oss-fuzz.com/v2/issue/4651449704251392/6977

While at it, fix order of arguments in the neighbouring log_syntax() call.
2018-03-21 00:46:13 +01:00
Lennart Poettering be6b0c2165 coccinelle: make use of DIV_ROUND_UP() wherever appropriate
Let's use our macros where we can
2018-03-20 20:59:02 +01:00
Lennart Poettering 8c637fe242
Merge pull request #8452 from keszybz/use-libmount-more
Use libmount in systemd-shutdown, add tests
2018-03-20 09:53:34 +01:00
Filipe Brandenburger c2887d565f macros: fix sysusers_create_inline (#8489)
This typo was introduced in commit dd2490ae12 when using
here-documents for the macro values.
2018-03-19 18:05:49 +01:00
Yu Watanabe ee5324aa04 tree-wide: voidify pager_open()
Even if pager_open() fails, in general, we should continue the operations.
All erroneous cases in pager_open() show log message in the function.
So, it is not necessary to check the returned value.
2018-03-19 21:04:02 +09:00
Yu Watanabe bcabcde5d2
Merge pull request #8408 from keszybz/ln-relative
bugs.fd.o bug archelogy
2018-03-19 18:32:30 +09:00
Jan Janssen ac9cea5ba3 shutdown: Don't limit unmount attempts prematurely (#8469)
Once upon a time shutdown.c didn't have the logic to check whether any
unmount attempts succeeded or not. So instead it kept looping for
a fixed amount and hoped all was right. Nowadays, we do know if we
changed anything during a iteration and also stop looping then, but
we still limit ourselves to FINALIZE_ATTEMPTS.

But, theoretically, we could have such a complicated and nested
setup that would survive that limit, leaving stuff around we
might actually be able to unmount. And we could also end up in a
situation where the extra loop with raised unmount error level could
be skipped too.

So let's just drop the retries logic and rely fully on the changed
flag.
2018-03-19 18:27:49 +09:00
Zbigniew Jędrzejewski-Szmek dd2490ae12 macros: use here-docs instead of echo (#8480)
It's common for sysusers files to contain quotes (in particular around the
comment/GECOS field), and using echo "..." is very likely to not work properly
in that case. Let's use <<EOF redirection. It's not bulletproof, but should
work in general.
2018-03-19 17:07:44 +09:00
Evgeny Vereshchagin e4711004d6
Merge pull request #8461 from keszybz/oss-fuzz-fixes
Oss fuzz fixes
2018-03-19 00:06:44 +03:00
Zbigniew Jędrzejewski-Szmek ca8700e922 core/unit: delay creating a stack variable until after length has been checked
path_is_normalized() will reject paths longer than 4095 bytes, so it's better
to not create a stack variable of unbounded size, but instead do the check first
and only then do that allocation.

Also use _cleanup_ to make things a bit shorter.

https://oss-fuzz.com/v2/issue/5424177403133952/7000
2018-03-18 21:07:01 +01:00
Rosen Penev 1e35c5ab27 systemd-link: Remove UDP Fragmentation Offload support. (#8183)
Support was killed in kernel 4.15 as well as ethtool 4.13.

Justification was lack of use by drivers and too much of a maintenance burden.
https://www.spinics.net/lists/netdev/msg443815.html

Also moved config_parse_warn_compat to conf-parser.[ch] to fix compile errors.
2018-03-18 14:28:14 +01:00
Zbigniew Jędrzejewski-Szmek 064c593899 core/service: fix memleak of USBFunctionStrings and USBFunctionDescriptors
oss-fuzz #6892.
2018-03-17 09:01:53 +01:00
Zbigniew Jędrzejewski-Szmek ba0c7754d8 core/manager: move some comments to a better place 2018-03-16 23:15:54 +01:00
Zbigniew Jędrzejewski-Szmek e63ebf71ed core: when reloading, delay any actions on journal and dbus connections
manager_recheck_journal() and manager_recheck_dbus() would be called to early
while we were deserialiazing units, before the systemd-journald.service and
dbus.service have been deserialized. In effect we'd disable logging to the
journald and close the bus connection. The first is not very noticable, it
mostly means that logs emitted during deserialization are lost. The second is
more noticeable, because manager_recheck_dbus() would call bus_done_api() and
bus_done_system() and close dbus connections. Logging and bus connection would
then be restored later after the respective units have been deserialized.

This is easily reproduced by calling:
  $ sudo gdbus call --system --dest org.freedesktop.systemd1 --object-path /org/freedesktop/systemd1 --method "org.freedesktop.systemd1.Manager.Reload"
which works fine before 8559b3b75c, and then starts failing with:
  Error: GDBus.Error:org.freedesktop.DBus.Error.NoReply: Remote peer disconnected

None of this should happen, and we should delay changing state until after
deserialization is complete when reloading. manager_reload() already included
the calls to manager_recheck_journal() and manager_recheck_dbus(), so the
connection state will be updated after deserialization during reloading is done.

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1554578.
2018-03-16 23:14:04 +01:00
Zbigniew Jędrzejewski-Szmek 71ae04c400 core/umount: use libmount to enumerate /proc/swaps
example.swaps with "(deleted)" does not cause bogus entries in the list now,
but a memleak in libmount instead. The memleaks is not very important since
this code is run just once.
Reported as https://github.com/karelzak/util-linux/issues/596.

$ build/test-umount
...
/* test_swap_list("/proc/swaps") */
path=/var/tmp/swap o= f=0x0 try-ro=no dev=0:0
path=/dev/dm-2 o= f=0x0 try-ro=no dev=0:0
/* test_swap_list("/home/zbyszek/src/systemd/test/test-umount/example.swaps") */
path=/some/swapfile o= f=0x0 try-ro=no dev=0:0
path=/dev/dm-2 o= f=0x0 try-ro=no dev=0:0
==26912==
==26912== HEAP SUMMARY:
==26912==     in use at exit: 16 bytes in 1 blocks
==26912==   total heap usage: 1,546 allocs, 1,545 frees, 149,008 bytes allocated
==26912==
==26912== 16 bytes in 1 blocks are definitely lost in loss record 1 of 1
==26912==    at 0x4C31C15: realloc (vg_replace_malloc.c:785)
==26912==    by 0x55C5D8C: _IO_vfscanf (in /usr/lib64/libc-2.26.so)
==26912==    by 0x55D8AEC: vsscanf (in /usr/lib64/libc-2.26.so)
==26912==    by 0x55D25C3: sscanf (in /usr/lib64/libc-2.26.so)
==26912==    by 0x53236D0: mnt_table_parse_stream (in /usr/lib64/libmount.so.1.1.0)
==26912==    by 0x53249B6: mnt_table_parse_file (in /usr/lib64/libmount.so.1.1.0)
==26912==    by 0x10D157: swap_list_get (umount.c:194)
==26912==    by 0x10B06E: test_swap_list (test-umount.c:34)
==26912==    by 0x10B24B: main (test-umount.c:56)
==26912==
==26912== LEAK SUMMARY:
==26912==    definitely lost: 16 bytes in 1 blocks
==26912==    indirectly lost: 0 bytes in 0 blocks
==26912==      possibly lost: 0 bytes in 0 blocks
==26912==    still reachable: 0 bytes in 0 blocks
==26912==         suppressed: 0 bytes in 0 blocks
2018-03-16 10:12:50 +01:00
Zbigniew Jędrzejewski-Szmek 1fd8edb53a test-umount: add a simple test for swap_list_get()
The implementation seems buggy:
/* test_swap_list("/home/zbyszek/src/systemd/test/test-umount/example.swaps") */
path=0 o= f=0x0 try-ro=no dev=0:0
path=/some/swapfile2 o= f=0x0 try-ro=no dev=0:0
path=/some/swapfile o= f=0x0 try-ro=no dev=0:0
path=/dev/dm-2 o= f=0x0 try-ro=no dev=0:0
2018-03-16 10:12:50 +01:00
Zbigniew Jędrzejewski-Szmek a6dcd22976 core/umount: use _cleanup_ 2018-03-16 10:12:50 +01:00
Zbigniew Jędrzejewski-Szmek 6fa392bf91 tests: add a simple test for the mountinfo parsing logic 2018-03-16 10:12:50 +01:00
Zbigniew Jędrzejewski-Szmek 95b862b054 shutdown: use libmount to enumerate /proc/self/mountinfo
This is analogous to 8d3ae2bd4c, except that now
src/core/umount.c not src/core/mount.c is converted.

Might help with https://bugzilla.redhat.com/show_bug.cgi?id=1554943, or not.

In the patch, mnt_free_tablep and mnt_free_iterp are declared twice. It'd
be nicer to define them just once in mount-setup.h, but then libmount.h would
have to be included there. libmount.h seems to be buggy, and declares some
defines which break other headers, and working around this is more pain than
the two duplicate lines. So let's live with the duplication for now.

This fixes memleak of MountPoint in mount_points_list_get() on error, not that
it matters any.
2018-03-16 10:09:46 +01:00
Franck Bui 848e863acc basic/macros: rename noreturn into _noreturn_ (#8456)
"noreturn" is reserved and can be used in other header files we include:

  [   16s] In file included from /usr/include/gcrypt.h:30:0,
  [   16s]                  from ../src/journal/journal-file.h:26,
  [   16s]                  from ../src/journal/journal-vacuum.c:31:
  [   16s] /usr/include/gpg-error.h:1544:46: error: expected ‘,’ or ‘;’ before ‘)’ token
  [   16s]  void gpgrt_log_bug (const char *fmt, ...)    GPGRT_ATTR_NR_PRINTF(1,2);

Here we include grcrypt.h (which in turns include gpg-error.h) *after* we
"noreturn" was defined in macro.h.
2018-03-15 14:23:46 +09:00
Evgeny Vereshchagin 3b71cf46be
Merge pull request #8441 from keszybz/oss-fuzz-fixes
Fixes for bugs found by oss-fuzz
2018-03-14 21:25:56 +03:00
Zbigniew Jędrzejewski-Szmek 20d52ab60e shared/conf-parser: fix crash when specifiers cannot be resolved in config_parse_device_allow()
oss-fuzz #6885.
2018-03-14 16:50:08 +01:00
Zbigniew Jędrzejewski-Szmek b93618644b core/umount: fix unitialized fields in MountPoint in dm_list_get()
This one might actually might cause a crash.
2018-03-14 12:38:43 +01:00
Zbigniew Jędrzejewski-Szmek d4f5c00153
Merge pull request #8429 from medhefgo/sd-shutdown
sd-shutdown improvements
2018-03-13 09:47:09 +01:00
Jan Janssen 456b2199f6 shutdown: Reduce log level of unmounts
There is little point in logging about unmounting errors if the
exact mountpoint will be successfully unmounted in a later retry
due unmounts below it having been removed.

Additionally, don't log those errors if we are going to switch back
to a initrd, because that one is also likely to finalize the remaining
mountpoints. If not, it will log errors then.
2018-03-12 18:32:26 +01:00
Jan Janssen e783b4902f umount: Don't bother remounting api and ro filesystems read-only 2018-03-12 18:32:26 +01:00
Jan Janssen 8645ffd12b umount: Try unmounting even if remounting read-only failed
In the case of some api filesystems remounting read-only fails
while unmounting succeeds.
2018-03-12 18:32:26 +01:00
Jan Janssen 3bc341bee9 umount: Provide the same mount flags too when remounting read-only
This most likely amounts to no real benefits and is just here for
completeness sake.
2018-03-12 18:32:26 +01:00
Jan Janssen 1d62d22d94 umount: Decide whether to remount read-only earlier 2018-03-12 18:32:26 +01:00
Jan Janssen 0494cae03d umount: Add more asserts and remove some unused arguments 2018-03-12 18:32:10 +01:00
Jan Janssen 659b15313b umount: Fix memory leak 2018-03-12 13:40:14 +01:00
Zbigniew Jędrzejewski-Szmek e8112e67e4 Make MANAGER_TEST_RUN_MINIMAL just allocate data structures
When running tests like test-unit-name, there is not point in setting
up the cgroup and signals and interacting with the environment. Similarly
when running fuzz testing of the parser.

Add new MANAGER_TEST_RUN_BASIC which takes the role of MANAGER_TEST_RUN_MINIMAL,
and redefine MANAGER_TEST_RUN_MINIMAL to just create the basic data structures.
2018-03-11 16:33:59 +01:00
Zbigniew Jędrzejewski-Szmek dc409696cf Introduce _cleanup_(unit_freep) 2018-03-11 16:33:58 +01:00
Zbigniew Jędrzejewski-Szmek c70cac548a Introduce _cleanup_(manager_freep) 2018-03-11 16:33:57 +01:00
Michal Sekletar aa77e234fc core: ignore errors from cg_create_and_attach() in test mode (#8401)
Reproducer:

$ meson build && cd build
$ ninja
$ sudo useradd test
$ sudo su test
$ ./systemd --system --test
...
Failed to create /user.slice/user-1000.slice/session-6.scope/init.scope control group: Permission denied
Failed to allocate manager object: Permission denied

Above error message is caused by the fact that user test didn't have its
own session and we tried to set up init.scope already running as user
test in the directory owned by different user.

Let's try to setup cgroup hierarchy, but if that fails return error only
when not running in the test mode.

Fixes #8072
2018-03-09 23:30:32 +01:00
Filipe Brandenburger 416be1a03b core/socket: support binary inside chroot when looking for SELinux label (#8405)
Otherwise having a .socket unit start a .service running a binary under
a chroot fails as the unit is unable to determine the SELinux label of
the binary.
2018-03-09 12:20:56 +01:00
Yu Watanabe 906bdbf5e7 core/cgroup: accepts MemorySwapMax=0 (#8366)
Also, this moves two macros from dbus-util.h to dbus-cgroup.c,
as they are only used in dbus-cgroup.c.

Fixes #8363.
2018-03-09 11:34:50 +01:00
Zbigniew Jędrzejewski-Szmek 8750ac0238 pid1: make use of high rt signals on hppa with newer kernels
Back in 4dffec1459 we stopped using SIGRTMIN+26
and higher on hppa because they were not available. Then they became available
in linux 3.18:

  https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1f25df2eff5b25f52c139d3ff31bc883eee9a0ab

Instead of hard-coding the list based on architecture, let's use a runtime
check like signal(7) says.

(A note about implementation: RTSIG_IF_AVAILABLE is defined to take the full
signal and not just an offset from SIGRTMIN so that it's still possible to
grep for SIGRTMIN\+.)

Add a simple "test" to print the signal values.

Fixes https://bugs.freedesktop.org/show_bug.cgi?id=84931.
2018-03-09 10:35:33 +01:00
Lennart Poettering 586fb20fd1
Merge pull request #8372 from keszybz/two-cleanups
Two cleanups
2018-03-08 23:23:43 +01:00
Yu Watanabe 5cbaad2f67 core: do not free heap-allocated strings (#8391)
Fixes #8387.
2018-03-08 14:21:54 +01:00
Yu Watanabe a1d32bac2a
Revert "core: don't setup init.scope in test mode (#8380)" (#8390)
This reverts commit a9e8ecf037,
as it breaks test-path.

Fixes #8389.
2018-03-08 15:29:19 +09:00