Commit Graph

112 Commits

Author SHA1 Message Date
Lennart Poettering 6dd16814a5
Merge pull request #17079 from keszybz/late-exec-resolution
Resolve executable paths before execution, use fexecve()
2020-12-03 14:58:20 +01:00
Yu Watanabe db9ecf0501 license: LGPL-2.1+ -> LGPL-2.1-or-later 2020-11-09 13:23:58 +09:00
Zbigniew Jędrzejewski-Szmek 5ca9139ace basic/path-util: let find_executable_full() optionally return an fd 2020-11-06 15:12:54 +01:00
Lennart Poettering 66032ef489 macro: introduce POINTER_MAX as define for (void*) -1
Just add a safer, prettier way to write (void*) -1, that doesn't rely on
two's complement, but uses the correct underlying C constructs.
2020-10-22 08:33:20 +09:00
Zbigniew Jędrzejewski-Szmek 92673045b5 basic/path-util: enhance find_executable() for the fixed path case 2020-09-18 15:28:48 +02:00
Zbigniew Jędrzejewski-Szmek cd4ff5aa11 basic/path-util: inline two trivial functions
While at it, add assert() for the argument.
2020-09-18 15:28:48 +02:00
Zbigniew Jędrzejewski-Szmek f7bc0c324a Rename find_binary to find_executable
"executable" is more correct than "binary", since scripts are OK too.
2020-09-18 15:28:48 +02:00
Chris Down 33e1a5d8d3 path: Improve $PATH search directory case
Previously:

1. last_error wouldn't be updated with errors from is_dir;
2. We'd always issue a stat(), even for binaries without execute;
3. We used stat() instead of access(), which is cheaper.

This change avoids all of those, by only checking inside X_OK-positive
case whether access() works on the path with an extra slash appended.
Thanks to Lennart for the suggestion.
2020-08-27 00:52:20 +01:00
Zbigniew Jędrzejewski-Szmek b6abc2acb4
Merge pull request #16568 from poettering/creds-store
credentials logic to pass privileged data to services
2020-08-26 10:32:30 +02:00
Chris Down 8b5cb69bc8 path: Skip directories when finalising $PATH search
Imagine $PATH /a:/b. There is an echo command at /b/echo. Under this
configuration, this works fine:

    % systemd-run --user --scope echo .
    Running scope as unit: run-rfe98e0574b424d63a641644af511ff30.scope
    .

However, if I do `mkdir /a/echo`, this happens:

    % systemd-run --user --scope echo .
    Running scope as unit: run-rcbe9369537ed47f282ee12ce9f692046.scope
    Failed to execute: Permission denied

We check whether the resulting file is executable for the performing
user, but of course, most directories are anyway, since that's needed to
list within it. As such, another is_dir() check is needed prior to
considering the search result final.

Another approach might be to check S_ISREG, but there may be more gnarly
edge cases there than just eliminating this obviously pathological
example, so let's just do this for now.
2020-08-26 10:22:02 +02:00
Lennart Poettering bb0c0d6f29 core: add credentials logic
Fixes: #15778 #16060
2020-08-25 19:45:35 +02:00
Lennart Poettering c95f9a2351 shared: introduce mkfs-util.c/.h
Let's move the "mkfs" code from homed there, plus other related code.

This way we can easily reuse it from other places.
2020-08-24 21:59:49 +02:00
Lennart Poettering 2f2c60d7fe path-util: make use of TAKE_PTR() where we can 2020-07-30 16:42:58 +02:00
Lennart Poettering 544e146b0e journalctl,elsewhere: make sure --file=foo fails with sane error msg if foo is not readable
It annoyed me for quite a while that running "journalctl --file=…" on a
file that is not readable failed with a "File not found" error instead
of a permission error. Let's fix that.

We make this work by using the GLOB_NOCHECK flag for glob() which means
that files are not accessible will be returned in the array as they are
instead of being filtered away. This then means that our later attemps
to open the files will fail cleanly with a good error message.
2020-05-19 15:26:51 +02:00
Topi Miettinen de46b2be07
namespace: ignore prefix chars when comparing paths
Other callers of path_strv_contains() or PATH_IN_SET() don't seem to handle
paths prefixed with -+.
2020-03-10 16:48:34 +02:00
Lennart Poettering 3841fee822 path-util: introduce path_strv_contains() helper
it's like strv_contains() but uses path_equal() rather than streq() to
compare strings.
2020-01-09 11:17:21 +01:00
Yu Watanabe e30e8b5073 tree-wide: drop stat.h or statfs.h when stat-util.h is included 2019-11-04 00:30:32 +09:00
Yu Watanabe 455fa9610c tree-wide: drop string.h when string-util.h or friends are included 2019-11-04 00:30:32 +09:00
Yu Watanabe f5947a5e92 tree-wide: drop missing.h 2019-10-31 17:57:03 +09:00
Zbigniew Jędrzejewski-Szmek a5648b8094 basic/fs-util: change CHASE_OPEN flag into a separate output parameter
chase_symlinks() would return negative on error, and either a non-negative status
or a non-negative fd when CHASE_OPEN was given. This made the interface quite
complicated, because dependning on the flags used, we would get two different
"types" of return object. Coverity was always confused by this, and flagged
every use of chase_symlinks() without CHASE_OPEN as a resource leak (because it
would this that an fd is returned). This patch uses a saparate output parameter,
so there is no confusion.

(I think it is OK to have functions which return either an error or an fd. It's
only returning *either* an fd or a non-fd that is confusing.)
2019-10-24 22:44:24 +09:00
Chris Down 4e1ddb6612 util-lib: Don't propagate EACCES from find_binary PATH lookup to caller
On one of my test machines, test-path-util was failing because the
find_binary("xxxx-xxxx") was returning -EACCES instead of -ENOENT. This
happens because the PATH entry on that host contains a directory which
the user in question doesn't have access to. Typically applications
ignore permission errors when searching through PATH, for example in
bash:

    $ whoami
    cdown
    $ PATH=/root:/bin type sh
    sh is /bin/sh

This behaviour is present on zsh and other shells as well, though. This
patch brings our PATH search behaviour closer to other major Unix tools.
2019-09-26 09:19:20 +01:00
Zbigniew Jędrzejewski-Szmek b47aa73a9a basic/path-util: move two path_simplify* functions to be adjacent
No functional change.
2019-07-19 16:51:14 +02:00
Zbigniew Jędrzejewski-Szmek cc4d7d818d path-util: add path_startswith_strv() 2019-07-19 16:49:41 +02:00
Lennart Poettering cee97d5768
Merge pull request #12836 from yuwata/tree-wide-replace-strjoin
tree-wide: replace strjoin() with path_join()
2019-06-22 20:02:46 +02:00
Lennart Poettering c6134d3e2f path-util: get rid of prefix_root()
prefix_root() is equivalent to path_join() in almost all ways, hence
let's remove it.

There are subtle differences though: prefix_root() will try shorten
multiple "/" before and after the prefix. path_join() doesn't do that.
This means prefix_root() might return a string shorter than both its
inputs combined, while path_join() never does that. I like the
path_join() semantics better, hence I think dropping prefix_root() is
totally OK. In the end the strings generated by both functon should
always be identical in terms of path_equal() if not streq().

This leaves prefix_roota() in place. Ideally we'd have path_joina(), but
I don't think we can reasonably implement that as a macro. or maybe we
can? (if so, sounds like something for a later PR)

Also add in a few missing OOM checks
2019-06-21 08:42:55 +09:00
Yu Watanabe 657ee2d82b tree-wide: replace strjoin() with path_join() 2019-06-21 03:26:16 +09:00
Lennart Poettering d8b4d14df4 util: split out nulstr related stuff to nulstr-util.[ch] 2019-03-14 13:25:52 +01:00
Lennart Poettering 11de56b9fa user-util: use SYNTHETIC_ERRNO() where we can 2019-03-12 16:08:57 +01:00
Lennart Poettering 0b78b1370c path-util: minimize variable scope 2019-03-12 16:08:57 +01:00
Lennart Poettering 458e60b3a9 path-util: check validity before normalization in path_simplify_and_warn()
As the normalization check includes a validation check the order
matters.
2019-03-12 16:08:57 +01:00
Zbigniew Jędrzejewski-Szmek 296acffe45 When parsing paths, reject anything above PATH_MAX
The check for length is done after path_simplify(), to be nice to paths which
are constructed using specifiers, and have duplicate slashes and stuff.
2018-12-10 11:57:26 +01:00
Zbigniew Jędrzejewski-Szmek 652ef29887 path-util: allow NULLs in arguments to path_join()
This removes the need to remember to put strempty() in places, thus reducing
the likelihood of a stupid mistake.
2018-11-30 22:21:17 +01:00
Zbigniew Jędrzejewski-Szmek 62a85ee0a9 tree-wide: rename path_join_many() to path_join()
$ git grep -e path_join_many -l|xargs sed -r -i 's/path_join_many/path_join/g'

The two test functions are merged into one.
2018-11-30 10:59:47 +01:00
Zbigniew Jędrzejewski-Szmek 30016f21b3 tree-wide: replace path_join with path_join_many 2018-11-30 10:40:38 +01:00
Lennart Poettering 61e0111df9 path-util: port path_join() over to path_join_many()
We should probably drop path_join() entirely in the long run (and
then rename path_join_many() to it?), but for now let's make one a
wrapper for the other.
2018-11-29 20:03:56 +01:00
Lennart Poettering cd8194a389 path-util: add new path_join_many() API 2018-11-29 20:03:56 +01:00
Lennart Poettering a60c8eee10 path-util: add helper for extracting last filename from path 2018-11-26 18:09:01 +01:00
Lennart Poettering 77e0a1b5e0 path-util: handle NULL inputs in last_path_component() 2018-11-26 18:09:01 +01:00
Lennart Poettering 49fe5c0996 tree-wide: port various places over to STARTSWITH_SET() 2018-11-26 14:08:46 +01:00
Lennart Poettering 656552ebb3 path-util: add new path_is_valid() helper 2018-10-17 21:13:02 +02:00
Thomas Haller afbae3e9f2 path-util: fix path_simplify() with kill_dots and "."
Previously, together with kill_dots true, patch like
".", "./.", ".//.//" would all return an empty string.

That is wrong. There must be one "." left to reference
the current directory.

Also, the comment with examples was wrong.
2018-10-05 21:41:33 +02:00
Thomas Haller f21f31b24b trivial: fix spelling in code comments
Based-on-patch-by: Rafael Fontenelle <rafaelff@gnome.org>
2018-09-30 21:32:33 +02:00
Franck Bui 7f6240fab1 path-util: make use of path_join() in path_make_absolute_cwd() 2018-07-30 16:06:02 +02:00
Lennart Poettering 0c69794138 tree-wide: remove Lennart's copyright lines
These lines are generally out-of-date, incomplete and unnecessary. With
SPDX and git repository much more accurate and fine grained information
about licensing and authorship is available, hence let's drop the
per-file copyright notice. Of course, removing copyright lines of others
is problematic, hence this commit only removes my own lines and leaves
all others untouched. It might be nicer if sooner or later those could
go away too, making git the only and accurate source of authorship
information.
2018-06-14 10:20:20 +02:00
Lennart Poettering 818bf54632 tree-wide: drop 'This file is part of systemd' blurb
This part of the copyright blurb stems from the GPL use recommendations:

https://www.gnu.org/licenses/gpl-howto.en.html

The concept appears to originate in times where version control was per
file, instead of per tree, and was a way to glue the files together.
Ultimately, we nowadays don't live in that world anymore, and this
information is entirely useless anyway, as people are very welcome to
copy these files into any projects they like, and they shouldn't have to
change bits that are part of our copyright header for that.

hence, let's just get rid of this old cruft, and shorten our codebase a
bit.
2018-06-14 10:20:20 +02:00
Lennart Poettering bae47ba7d2 tree-wide: use PATH_STARTSWITH_SET() at two more places 2018-06-11 18:01:06 +02:00
Lennart Poettering 57e84e7535 core: rework how we validate DeviceAllow= settings
Let's make sure we don't validate "char-*" and "block-*" expressions as
paths.
2018-06-11 18:01:06 +02:00
Zbigniew Jędrzejewski-Szmek 86ab333d00 basic/path-util: fix ordering in error message
Jun 11 14:29:12 krowka systemd[1]: /etc/systemd/system/workingdir.service:6: = path is not normalizedWorkingDirectory: /../../etc
   ↓
Jun 11 14:32:12 krowka systemd[1]: /etc/systemd/system/workingdir.service:6: WorkingDirectory= path is not normalized: /../../etc
2018-06-11 17:06:23 +02:00
Zbigniew Jędrzejewski-Szmek 5c270a18da basic/path-util: use FLAGS_SET in one more place 2018-06-04 11:50:44 +02:00
Yu Watanabe 58a53adde5 path-util: introduce path_simplify_and_warn() 2018-06-03 23:54:55 +09:00