Commit graph

1525 commits

Author SHA1 Message Date
Zbigniew Jędrzejewski-Szmek c3de717e51
Merge pull request #7675 from shawnl/unaligned
Issue #7654 (unaligned loads on sparc64)
2018-01-20 10:00:14 +11:00
Yu Watanabe 22bc57c58a fs-util: chase_symlinks(): support empty root
The commit b1bfb84804 makes chase_symlinks()
recognize empty string for root as an invalid parameter. However,
empty root is often used e.g. systemd-nspawn.
This makes chase_symlinks() support empty string safely.

Fixes #7927.
2018-01-19 11:41:28 +01:00
Lennart Poettering b79fe07243
Merge pull request #7910 from poettering/getcwd
some getcwd() fixes, and other path-util tweaks
2018-01-17 19:16:42 +01:00
Lennart Poettering 382a5078a6 fs-util: refuse taking a relative path to chase if "root" is specified and CHASE_PREFIX_ROOT is set
If we take a relative path we first make it absolute, based on the
current working directory. But if CHASE_PREFIX_ROOT is passe we are
supposed to make the path absolute taking the specified root path into
account, but that makes no sense if we talk about the current working
directory as that is relative to the host's root in any case. Hence,
let's refuse this politely.
2018-01-17 12:04:15 +01:00
Lennart Poettering a49424af6a fs-util: extra chase_symlink() safety check on "path" parameter
It's not clear what an empty "path" is even supposed to mean, hence
refuse.
2018-01-17 12:04:15 +01:00
Lennart Poettering b1bfb84804 fs-util: extra safety checks on chase_symlinks() root parameter
Let's handle root="" and root="/" safely.
2018-01-17 12:04:15 +01:00
Lennart Poettering 7aeeb313ad path-util: don't insert duplicate "/" in path_make_absolute_cwd()
When the working directory is "/" it's prettier not to insert a second
"/" in the path, even though it is technically correct.
2018-01-17 11:17:55 +01:00
Lennart Poettering d72495759b tree-wide: port all code to use safe_getcwd() 2018-01-17 11:17:38 +01:00
Lennart Poettering a2556d25ae path-util: introduce new safe_getcwd() wrapper
It's like get_current_dir_name() but protects us from
CVE-2018-1000001-style exploits:

https://www.halfdog.net/Security/2017/LibcRealpathBufferUnderflow/
2018-01-17 11:16:31 +01:00
Lennart Poettering cddd2ce106 path-util: don't add extra "/" when prefix already is suffixed by slash
No need to insert duplicate "/" if we can avoid it. This is particularly
relevant if the prefix passed in is the root directory.
2018-01-17 11:15:00 +01:00
Lennart Poettering 81cce8ded5 path-util: do something useful if the prefix is "" in path_make_absolute()
Do not insert a "/" if the prefix we shall use is empty. It's a corner
case we should probably take care of.
2018-01-17 11:14:28 +01:00
Zbigniew Jędrzejewski-Szmek 4e4e3d9766
Merge pull request #7893 from poettering/parse-tweaks
parsing tweaks
2018-01-17 20:22:17 +11:00
Yu Watanabe 47dbb99ad7 virt: add comment that we need to use sscanf()
Follow-up for 13e0f9fe83.
See PR #7890 and comment in PR #7581.
2018-01-16 23:00:39 +09:00
Lennart Poettering bac794f607 parse-util: detect overflows in parse_percent_unbounded()
We shouldn't accept percentages beyon INT32_MAX and consider them
valid.
2018-01-16 11:53:43 +01:00
Lennart Poettering b5ffbc5579 parse-util: coding style fix
Let's not rely on C's downgrade-to-bool feature to check for NUL bytes
2018-01-16 11:53:43 +01:00
Lennart Poettering e520e0fc2c locale-util: add freelocale() cleanup helper 2018-01-16 11:53:43 +01:00
Olaf Hering 13e0f9fe83 Fix parsing of features in detect_vm_xen_dom0 (#7890)
Use sscanf instead of the built-in safe_atolu because the scanned string
lacks the leading "0x", it is generated with snprintf(b, "%08x", val).
As a result strtoull handles it as octal, and parsing fails.

The initial submission already used sscanf, then parsing was replaced by
safe_atolu without retesting the updated PR.

Fixes 575e6588d ("virt: use XENFEAT_dom0 to detect the hardware domain
(#6442, #6662) (#7581)")
2018-01-16 20:24:37 +11:00
Zbigniew Jędrzejewski-Szmek e0b6d3cabe
Merge pull request #7816 from poettering/chase-pid
Make MAINPID= and PIDFile= handling more restrictive (and other stuff)
2018-01-15 14:14:34 +04:00
Zbigniew Jędrzejewski-Szmek 67ddb52432
Merge pull request #7855 from poettering/log-h-includes
log.h #include cleanups
2018-01-15 13:43:09 +04:00
Michal Sekletar 8647283e45 process-util: make our freeze() routine do something useful
When we crash we freeze() our-self (or possibly we reboot the machine if
that is configured). However, calling pause() is very unhelpful thing to
do. We should at least continue to do what init systems being doing
since 70's and that is reaping zombies. Otherwise zombies start to
accumulate on the system which is a very bad thing. As that can prevent
admin from taking manual steps to reboot the machine in somewhat
graceful manner (e.g. manually stopping services, unmounting data
volumes  and calling reboot -f).

Fixes #7783
2018-01-12 17:50:02 +01:00
Lennart Poettering 1ed34d75d4 fs-util: add new chase_symlinks() flag CHASE_OPEN
The new flag returns the O_PATH fd of the final component, which may be
converted into a proper fd by open()ing it again through the
/proc/self/fd/xyz path.

Together with O_SAFE this provides us with a somewhat safe way to open()
files in directories potentially owned by unprivileged code, where we
want to refuse operation if any symlink tricks are played pointing to
privileged files.
2018-01-11 15:12:16 +01:00
Lennart Poettering f14f1806e3 fs-util: add new CHASE_SAFE flag to chase_symlinks()
When the flag is specified we won't transition to a privilege-owned
file or directory from an unprivileged-owned one. This is useful when
privileged code wants to load data from a file unprivileged users have
write access to, and validates the ownership, but want's to make sure
that no symlink games are played to read a root-owned system file
belonging to a different context.
2018-01-11 15:12:16 +01:00
Lennart Poettering c1e83af84e log: assign the realm to a variable first in log_full_errno_realm()
Let's avoid duplicate evaluation (which might have side-effects) and
assign the realm to an internal variable first, as we need it twice.
2018-01-11 14:44:31 +01:00
Lennart Poettering d04ce5a9c4 log: rework log_syntax_invalid_utf8() a bit
The macro used utf8.h functions without including that. Let's clean this
up, by moving that code inside of log.c.

Let's also make the call return -EINVAL in all cases. This is in line
with log_oom() which also returns a well-defined error code even though
it doesn#t take one.
2018-01-11 14:44:31 +01:00
Lennart Poettering a6ab236595 log: minor modernizations to log_received_signal() 2018-01-11 14:44:31 +01:00
Lennart Poettering dccca82b1a log: minimize includes in log.h
log.h really should only include the bare minimum of other headers, as
it is really pulled into pretty much everything else and already in
itself one of the most basic pieces of code we have.

Let's hence drop inclusion of:

1. sd-id128.h because it's entirely unneeded in current log.h
2. errno.h, dito.
3. sys/signalfd.h which we can replace by a simple struct forward
   declaration
4. process-util.h which was needed for getpid_cached() which we now hide
   in a funciton log_emergency_level() instead, which nicely abstracts
   the details away.
5. sys/socket.h which was needed for struct iovec, but a simple struct
   forward declaration suffices for that too.

Ultimately this actually makes our source tree larger (since users of
the functionality above must now include it themselves, log.h won't do
that for them), but I think it helps to untangle our web of includes a
tiny bit.

(Background: I'd like to isolate the generic bits of src/basic/ enough
so that we can do a git submodule import into casync for it)
2018-01-11 14:44:31 +01:00
Zbigniew Jędrzejewski-Szmek a9883559e3
Merge pull request #7846 from poettering/nobody-getenv
some assorted fixes and additions, in particular a way to turn off "nobody" synthesizing on a specific system
2018-01-10 20:18:51 +01:00
Lennart Poettering 0a7eed95dd
Merge pull request #7843 from hbakken/work/old-kernel-support
Work/old kernel support
2018-01-10 18:59:14 +01:00
Lennart Poettering e557b1a655 util: minor tweaks to disable_core_dumps()
First, let's rename it to disable_coredumps(), as in the rest of our
codebase we spell it "coredump" rather than "core_dump", so let's stick
to that.

However, also log about failures to turn off core dumpling on LOG_DEBUG,
because debug logging is always a good idea.
2018-01-10 18:44:09 +01:00
Lennart Poettering 24eccc3414 nss-systemd,user-util: add a way how synthesizing "nobody" can be turned off
This is quite ugly, but provides us with an avenue for moving
distributions to define the "nobody" user properly without breaking legacy
systems that us the name for other stuff.

The idea is basically, that the distribution adopts the new definition
of "nobody" (and thus recompiles systemd with it) and then touches
/etc/systemd/dont-synthesize-nobody on legacy systems to turn off
possibly conflicting synthesizing of the nobody name by systemd.
2018-01-10 18:26:03 +01:00
Lennart Poettering dfd14786b5 util-lib: save/restore errno in cleanup calls
We should be careful with errno in cleanup functions, and not alter it
under any circumstances. In the safe_close cleanup handlers we are
already safe in that regard, but let's add similar protections on other
cleanup handlers that invoke system calls.

Why bother? Cleanup handlers insert code at function return in
non-obvious ways. Hence, code that sets errno and returns should not be
confused by us overrding the errno from a cleanup handler.

This is a paranoia fix only, I am not aware where this actually mattered
in real-life situations.
2018-01-10 17:21:15 +01:00
Lennart Poettering ad5d4b1703 cocci: use strempty() at more places
This shortens the code by a few lines.
2018-01-10 17:11:19 +01:00
Lennart Poettering 4631116dd9
Merge pull request #7836 from jkloetzke/shutdown-no-coredumps
shutdown: prevent core dumps in final shutdown stage
2018-01-10 15:15:07 +01:00
Yu Watanabe 0a664f1b15 exec-util: cast to void when return value is ignored
CID 1384262.
2018-01-10 20:52:55 +09:00
Yu Watanabe a41f6217ba process-util: initialize block_signals
CID 1384240.
2018-01-10 20:52:46 +09:00
Hans-Christian Noren Egtvedt d07ba5d790 missing: add bpf() define for ARM definition 2018-01-10 12:31:39 +01:00
Hans-Christian Noren Egtvedt 66ffb275fa missing: fix bpf() define for Tilera
__NR_bpf syscall uses the default generic value for Tilera, e.g. 280.
2018-01-10 10:59:01 +01:00
Jan Klötzke 9ce1759311 tree-wide: introduce disable_core_dumps helper and port existing users
Changes the core_pattern to prevent any core dumps by the kernel. Does
nothing if we're in a container environment as this is system wide
setting.
2018-01-10 10:54:40 +01:00
Lennart Poettering c944ca8e82 verbs: suppress debug log message if SYSTEMD_OFFLINE is not set
If SYSTEMD_OFFLINE is not set getenv_bool() for it will return -ENXIO,
which is nothing we should log about, not even at LOG_DEBUG level.
2018-01-05 13:58:33 +01:00
Lennart Poettering 15ca0a42c7 sd-bus: drop references to legacy /var/run D-Bus socket
Let's directly reference /run instead, so that we can work without /var
being around, or with /var/run being incorrectly set up.

Note that we keep the old socket path in place when referencing the
system bus of containers, as they might be foreign operating systems,
that still don't have adopted /run, and where it makes sense to use the
standardized name instead. On local systems, we insist on /run being set
up properly however, hence this limitation does not apply.

Also, get rid of the UNIX_SYSTEM_BUS_ADDRESS and
UNIX_USER_BUS_ADDRESS_FMT defines. They had a purpose when we still did
kdbus, as we then had to support two different backends. But since
that's gone, we don't need this indirection anymore, hence settle on a
one define only.
2018-01-05 13:58:32 +01:00
Lennart Poettering 5b5e6deabb bus: touch() the AF_UNIX sockets we listen() on after the fact
We'd like to use inotify to get notified when AF_UNIX sockets become
connectable. That happens at the moment of listen(), but this is doesn't
necessarily create in a watchable inotify event. Hence, let's synthesize
one whenever we generically create a socket, or when we know we created
it for a D-Bus server.

Ideally we wouldn't have to do this, and the kernel would generate an
event anyway for this. Doing this explicitly isn't too bad however, as
the event is still nicely associated with the AF_UNIX socket node, and
we generate all D-Bus sockets in our code hence it's safe.
2018-01-05 13:55:08 +01:00
Lennart Poettering 8b7f989a58 socket-util: explicitly ensure there's one trailing NUL byte on AF_UNIX socket addresses
AF_UNIX socket addresses aren't necessarily NUL terminated, however
they are usually used as strings which are assumed to be NUL terminated.
Let's hence add an extra byte to the end of the sockaddr_un structure,
that contains this NUL byte, simply for safety reasons.

Note that actually this patch changes exactly nothing IRL, as the other
sockaddr structures already are large enough to accomodate for an extra
NUL byte. The size of the union hence doesn't change at all by doing
this. The entire value of this patch is hence in the philosophical
feeling of safety, and by making something explicit that before was
implicit.
2018-01-05 13:55:08 +01:00
Lennart Poettering 89220e2fb6 socket-util: use parse_ip_port() for parsing IP ports
Let's unify some code here, and also use parse_ip_port() for all our IP
port parsing needs in socket_address_parse().
2018-01-05 13:55:08 +01:00
Lennart Poettering 706d7c27ad socket-label: tweak socket_address_listen() a bit
This changes two things when binding to AF_UNIX file system sockets:

1. When wethe socket already exists in the fs, and unlink() on it fails,
   don't bother to bind() a second time: since nothing changed it won't
   work either.

2. Also use SELinux-aware bind() for the second attempt.
2018-01-05 13:55:08 +01:00
Lennart Poettering 294d46f138 socket-label: simplify things a bit by using socket_address_get_path()
Let's make this more generic and descriptive, and let's reuse our
existing utility functions.
2018-01-05 13:55:08 +01:00
Lennart Poettering 9e3fa6e827 fs-util: rework touch_file() so that it can touch socket file nodes
Let's rework touch_file() so that it works correctly on sockets, fifos,
and device nodes: let's open an O_PATH file descriptor first and operate
based on that, if we can. This is usually the better option as it this
means we can open AF_UNIX nodes in the file system, and update their
timestamps and ownership correctly. It also means we can correctly touch
symlinks and block/character devices without triggering their drivers.

Moreover, by operating on an O_PATH fd we can make sure that we
operate on the same inode the whole time, and it can't be swapped out in
the middle.

While we are at it, rework the call so that we try to adjust as much as
we can before returning on error. This is a good idea as we call the
function quite often without checking its result, and hence it's best to
leave the files around in the most "correct" fashion possible.
2018-01-05 13:55:08 +01:00
Lennart Poettering 665dfe9318 io-util: make flush_fd() return how many bytes where flushed
This is useful so that callers know whether anything at all and how much
was flushed.

This patches through users of this functions to ensure that the return
values > 0 which may be returned now are not propagated in public APIs.

Also, users that ignore the return value are changed to do so explicitly
now.
2018-01-05 13:55:08 +01:00
Zbigniew Jędrzejewski-Szmek bbc79ae5c9
Merge pull request #7767 from poettering/fork-wait
A variety of smaller improvements all over the place
2018-01-04 15:25:07 +01:00
Zbigniew Jędrzejewski-Szmek 963c6c90af coverity: don't use (void)0 under coverity
I'm not sure why this is needed, but apparrently coverity doesn't like
(void)0. With this change, coverity can (almost) build systemd:

CFLAGS='-D_Float128="long double"' meson cov-build -Dman=false && \
  CCACHE_DISABLE=1 COVERITY_UNSUPPORTED=1 cov-build --dir cov-int ninja -C cov-build

Patch originially by Marek Cermak <macermak@redhat.com>.
2018-01-04 13:44:39 +01:00
Lennart Poettering 99c61f6b00 fileio: minor tweak to executable_is_script()
If read_line() returns ENOBFUS this means the line was overly long. When
we use this for checking whether an executable is a script, then this
shouldn't be propagated as-is, but simply as "this is not a script".
2018-01-04 13:28:24 +01:00