Commit Graph

68 Commits

Author SHA1 Message Date
Lennart Poettering c14ebe07a9
Merge pull request #17172 from keszybz/read-login-defs
Read /etc/login.defs
2020-10-02 11:01:30 +02:00
Zbigniew Jędrzejewski-Szmek d338bfff4a basic/fileio: add chase_symlinks_and_fopen_unlocked() 2020-10-01 17:52:50 +02:00
Lennart Poettering f25bff5eaf fs-util: add new futimens_opath() helper
futimens() that works for O_PATH fds.
2020-09-28 18:45:54 +02:00
Lennart Poettering 1e5bfa2ac8 fs-util: drop chmod_and_chown_unsafe() which is unused now 2020-09-23 18:00:19 +02:00
Lennart Poettering 22ed4a6d9a fs-util: add stat_warn_permissions() that operates on struct stat instead of fd 2020-06-02 19:31:36 +02:00
Lennart Poettering 053e0626db fs-util: teach unlinkat_deallocate() a simple scheme for overwriting for erasing
With that it becomes useful for deleting password files and such.
2020-05-19 17:27:13 +02:00
Lennart Poettering ed9c0851e5 fs-util: add helper path_is_encrypted() that checks if a file system is encrypted 2020-05-07 23:31:36 +02:00
Zbigniew Jędrzejewski-Szmek 162392b75a tree-wide: spellcheck using codespell
Fixes #15436.
2020-04-16 18:00:40 +02:00
Zbigniew Jędrzejewski-Szmek 6cb356ca9f basic/fs-util: add a version of chmod_and_chown that doesn not use /proc 2020-03-03 11:56:44 +01: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
Franck Bui 27c3112dcb fs-util: introduce inotify_add_watch_and_warn() helper
The default message for ENOSPC is very misleading: it says that the disk is
filled, but in fact the inotify watch limit is the problem.

So let's introduce and use a wrapper that simply calls inotify_add_watch(2) and
which fixes the error message up in case ENOSPC is returned.
2019-10-05 08:08:20 +02:00
Lennart Poettering 17f8fd9cf7
Merge pull request #13194 from keszybz/chase-symlinks-testing
Chase symlinks testing
2019-07-26 18:42:31 +02:00
Zbigniew Jędrzejewski-Szmek 22f62b1274 fs-util: CHASE_NOFOLLOW is not limited to CHASE_OPEN
$ build/test-chase-symlinks /etc/os-release
/etc/os-release → /usr/lib/os-release

$ build/test-chase-symlinks --nofollow /etc/os-release /etc/../etc/os-release /etc/../etc/./././os-release
/etc/os-release → /etc/os-release
/etc/../etc/os-release → /etc/os-release
/etc/../etc/./././os-release → /etc/os-release
2019-07-26 15:29:11 +02:00
Lennart Poettering 63d59b8dcf fs-util: add fsync_full() helper
We usually combine an fsync() with fsync_directory_of_file() hence let's
add a helper that does both in one.
2019-07-25 17:10:51 +02:00
Zbigniew Jędrzejewski-Szmek 4293c32b0e fs-util: adjust punctuation in comments 2019-07-19 16:51:14 +02:00
Yu Watanabe 961189af2a util: introduce PTR_TO_MODE and MODE_TO_PTR macros 2019-05-30 21:08:51 +09:00
Andrej Valek 56e3c9581e fs-util.h: add missing sys/stat include
fix error:
| error: passing argument 5 of 'chase_symlinks_and_stat' from incompatible pointer type [-Werror=incompatible-pointer-types]
2019-05-17 09:40:46 +03:00
Lennart Poettering 2b2fec7db0 util: split out errno related stuff 2019-03-14 13:25:51 +01:00
Lennart Poettering 71f514169b fs-util: add new helper syncfs_path() 2019-03-01 12:41:32 +01:00
Franck Bui fd74c6f3f8 fs-util: add new CHASE_WARN flag to chase_symlinks()
This flag can be used to make chase_symlinks() emit a warning when it
encounters an error.

Such flag can be useful for generating a comprehensive and detailed warning
since chase_symlinks() can generate a warning with a full context.

For now only warnings for unsafe transitions are produced.
2018-11-30 13:30:26 +01:00
Lennart Poettering 36695e880a add new systemd-bless-boot.service that marks boots as successful
This is the counterpiece to the boot counting implemented in
systemd-boot: if a boot is detected as successful we mark drop the
counter again from the booted snippet or kernel image.
2018-10-19 22:34:50 +02:00
Yu Watanabe 6c9c51e5e2 fs-util: make symlink_idempotent() optionally create relative link 2018-09-24 18:52:53 +03:00
Franck Bui 1f56e4ce77 fs-util: add new CHASE_NOFOLLOW flag to chase_symlinks()
This flag mimics what "O_NOFOLLOW|O_PATH" does for open(2) that is
chase_symlinks() will not resolve the final pathname component if it's a
symlink and instead will return a file descriptor referring to the symlink
itself.

Note: if CHASE_SAFE is also passed, no safety checking is performed on the
transition done if the symlink would have been followed.
2018-07-30 15:54:03 +02:00
Franck Bui 4fe3828c58 fs-util: introduce mkfifoat_atomic() helper 2018-07-30 15:54:03 +02:00
Lennart Poettering ef8becfac5 fs-util: introduce open_parent() helper
We often open the parent directory of a path. Let's add a common helper
for that, that shortens our code a bit and adds some extra safety
checks, for example it will fail if used on the root directory (which
doesn't really have a parent).

The helper is actually generalized from a function in btrfs-util.[ch]
which already existed for this purpose.
2018-07-23 13:38:18 +02:00
Zbigniew Jędrzejewski-Szmek b0450864f1
Merge pull request #9274 from poettering/comment-header-cleanup
drop "this file is part of systemd" and lennart's copyright from header
2018-06-14 11:26:50 +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
Yu Watanabe b8da477eaf fs-util: introduce fchmod_and_chown()
The new function fchmod_and_chown() is almost same as chmod_and_chown()
except it takes file descriptor instead of file path.
2018-06-14 11:47:57 +09:00
Lennart Poettering ef31828d06 tree-wide: unify how we define bit mak enums
Let's always write "1 << 0", "1 << 1" and so on, except where we need
more than 31 flag bits, where we write "UINT64(1) << 0", and so on to force
64bit values.
2018-06-12 21:44:00 +02:00
Zbigniew Jędrzejewski-Szmek 627d2bac24 fs-util,test: add helper to remove tempfiles
This simplifies the use of tempfiles in tests and fixes "leaked"
temporary files in test-fileio, test-catalog, test-conf-parser.

Not the whole tree is converted.
2018-05-18 20:11:31 +02:00
Franck Bui 4dfaa528d4 fs-util: introduce fchmod_opath()
fchmod(2) still doesn't take file descriptors opened with O_PATH.
2018-04-24 11:57:48 +02:00
Lennart Poettering 7aab22308e
Merge pull request #8708 from poettering/namespace-repeat
pid1 namespacing fixes
2018-04-18 18:46:44 +02:00
Lennart Poettering d2bcd0ba75
path-lookup: properly chase paths when reducing with root dir (#8750)
Let's make this correct.
2018-04-18 16:19:46 +02:00
Lennart Poettering f10f4215b5 fs-util: export how many iterations chase_symlinks() executes at max 2018-04-18 14:15:48 +02:00
Lennart Poettering 49eb36596b fs-util: add new CHASE_STEP flag to chase_symlinks()
If the flag is set only a single step of the normalization is executed,
and the resulting path is returned.

This allows callers to normalize piecemeal, taking into account every
single intermediary path of the normalization.
2018-04-18 14:15:48 +02:00
Zbigniew Jędrzejewski-Szmek 11a1589223 tree-wide: drop license boilerplate
Files which are installed as-is (any .service and other unit files, .conf
files, .policy files, etc), are left as is. My assumption is that SPDX
identifiers are not yet that well known, so it's better to retain the
extended header to avoid any doubt.

I also kept any copyright lines. We can probably remove them, but it'd nice to
obtain explicit acks from all involved authors before doing that.
2018-04-06 18:58:55 +02:00
Lennart Poettering 21c692e9bf fs-util: add calls that combine chase_symlinks() and open()/opendir() in one
This is useful when opening files within disk images, as we'll then take
the relative root directory properly into account.
2018-03-29 15:33:12 +02:00
Lennart Poettering 62570f6f03 fs-util: add new CHASE_TRAIL_SLASH flag for chase_symlinks()
This rearranges chase_symlinks() a bit: if no special flags are
specified it will now revert to behaviour before
b12d25a8d6. However, if the new
CHASE_TRAIL_SLASH flag is specified it will follow the behaviour
introduced by that commit.

I wasn't sure which one to make the beaviour that requires specification
of a flag to enable. I opted to make the "append trailing slash"
behaviour the one to enable by a flag, following the thinking that the
function should primarily be used to generate a normalized path, and I
am pretty sure a path without trailing slash is the more "normalized"
one, as the trailing slash is not really a part of it, but merely a
"decorator" that tells various system calls to generate ENOTDIR if the
path doesn't refer to a path.

Or to say this differently: if the slash was part of normalization then
we really should add it in all cases when the final path is a directory,
not just when the user originally specified it.

Fixes: #8544
Replaces: #8545
2018-03-22 19:54:24 +01:00
Lennart Poettering 11b29a96e9 fs-util: move fsync_directory_of_file() into generic code
This function used by the journal code is pretty useful generically,
let's move it to fs-util.c to make it useful for other code too.
2018-02-20 15:39:31 +01:00
Zbigniew Jędrzejewski-Szmek af229d7a5b login,user-sessions: always warn when we fail to remove nologin file
This usually is very annoying to users who then cannot log in, so
make sure we always warn if that happens (selinux, or whatever other reason).

This reverts a790812cb3.
2018-02-16 10:44:22 +01:00
Lennart Poettering 43767d9d5e fs-util: add new unlinkat_deallocate() helper
This new helper not only removes a file from a directory but also
ensures its space on disk is deallocated, by either punching a hole over
the full file or truncating the file afterwards if the file's link
counter is 0. This is useful in "vacuuming" algorithms to ensure that
client's can't keep the disk space the vacuuming is supposed to recover
pinned simply by keeping an fd open to it.
2018-02-12 11:27:11 +01:00
Yu Watanabe 01e4793de6 fs-util: drop readlink_and_make_absolute_root() 2018-02-09 16:58:56 +09:00
Yu Watanabe 842e456e79 fs-util: drop readlink_and_canonicalize() 2018-02-09 16:49:54 +09: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 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
Zbigniew Jędrzejewski-Szmek 53e1b68390 Add SPDX license identifiers to source files under the LGPL
This follows what the kernel is doing, c.f.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5fd54ace4721fc5ce2bb5aef6318fcf17f421460.
2017-11-19 19:08:15 +01:00
Lennart Poettering 57a4359ee0 fs-util: add access_fd() which is like access() but for fds
Linux doesn't have faccess(), hence let's emulate it. Linux has access()
and faccessat() but neither allows checking the access rights of an fd
passed in directly.
2017-11-17 11:12:33 +01:00
NeilBrown 655f2da079 tmpfiles: silently ignore any path that passes through autofs (#6506)
If a path passes though an autofs filesystem, then accessing
the path might trigger and automount.  As systemd-tmpfiles is run before
the network is up, and as automounts are often used for networked
filesystems, this can cause a deadlock.

So chase_symlinks is enhance to accept a new flag which tells it
to check for autofs, and return -EREMOTE if autofs is found.

tmpfiles is changed to check just before acting on a path so that it
can avoid autofs even if a symlink was created earlier by tmpfiles
that would send this path through an autofs.

This fixes a deadlock that happens when /home is listed in /etc/fstab as
x-systemd.automount for an NFS directory.
2017-09-04 15:35:07 +02:00