Commit Graph

132 Commits

Author SHA1 Message Date
Frantisek Sumsal 15dd451535 tree-wide: codespell fixes
Another batch of codespell fixes as reported by Fossies.org
2020-05-25 10:29:28 +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 622e1cdb31 fs-util: beef up path_is_encrypted() to deal with LVM block devices
Let's iterate through the slaves/ directory to find backing devices of
the block devices we care about.
2020-05-10 09:23:30 +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
Lennart Poettering f8606626ed tmpfiles: if we get ENOENT when opening /proc/self/fd/, check if /proc is mounted
let's return ENOSYS in that case, to make things a bit less confusng.

Previously we'd just propagate ENOENT, which people might mistake as
applying to the object being modified rather than /proc/ just not being
there.

Let's return ENOSYS instead, i.e. an error clearly indicating that some
kernel API is not available. This hopefully should put people on a
better track.

Note that we only do the procfs check in the error path, which hopefully
means it's the less likely path.

We probably can add similar bits to more suitable codepaths dealing with
/proc/self/fd, but for now, let's pick to the ones noticed in #14745.

Fixes: #14745
2020-04-23 14:52:10 +02:00
Balaji Punnuru f6f4f5fe53 util: return the correct correct wd from inotify helpers
We need to propagate the acquired watch descriptors because our callers
are counting on them.

[Lennart: this is split out of #15381 and simplified]
2020-04-22 08:57:17 +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
Lennart Poettering 47d7ab727c fs-util: make sure we output normalized paths in chase_symlinks()
Let's eat up multiple slashes.

Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1787089
Replaces: #14687
2020-01-28 22:53:59 +01:00
Lennart Poettering c2595d3b02 fs-util: when calling chase_symlinks() with root path, leave root part unresolved
Previously there was a weird asymmetry: initially we'd resolve the
specified prefix path when chasing symlinks together with the actual
path we were supposed to cover, except when we hit an absolute symlink
where we'd use the root as it was. Let's unify handling here: the prefix
path is never resolved, and always left as it is.

This in particular fixes issues with symlinks in the prefix path, as
that confused the check that made sure we never left the root directory.

Fixes: #14634
Replaces: #14635
2020-01-28 22:53:59 +01:00
Anita Zhang 0499585ffe include missing_fcntl.h where needed
f5947a5e92 dropped missing.h and
replaced with the more specific headers but did not add
missing_fcntl.h in places that use O_TMPFILE. This is needed for
some older versions of glibc.
2019-11-07 10:17:44 +00:00
Yu Watanabe 1405cb653a tree-wide: drop stdio.h when stdio-util.h is included 2019-11-04 00:30:32 +09: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 f21e58b47d tree-wide: drop time.h when time-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
Lennart Poettering fe573a798d fs-util: let's avoid unnecessary strerror()
strerror() is not thread safe. Let's avoid it where it is easy hence.

(Ideally we'd not use it at all anymore, but that's sometimes a bit
nasty, not in this case though, where it is very easy to avoid)

Follow-up for: 27c3112dcb
2019-11-03 22:04:39 +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
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
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 2d9b74ba87 tree-wide: replace strjoin() with path_join() 2019-06-24 23:59:38 +09:00
Franck Bui 71ec74d193 fs-util: no need for fchmod_and_chown() to access /proc/self/fd directly
fstat(2) is fine with O_PATH fds.

For changing owership of a file opened with O_PATH, there's fchownat(2).

Only changing permissions is problematic but we introduced fchmod_opath() for
that purpose.
2019-06-18 07:54:51 +02:00
Lennart Poettering 4b3b5bc71b tree-wide: port various places over to use chmod_and_chown()
Doing this properly is hard, hence let's unify the code.
2019-05-24 15:07:55 +02:00
Lennart Poettering 2dbb7e94af fs-util: rewrite chmod_and_chown()
Inspired by #12431 let's also rework chmod_and_chown() and make sure we
never add more rights to a file not owned by the right user.

Also, let's make chmod_and_chown() just a wrapper arond
fchmod_and_chown().

let's also change strategy: instead of chown()ing first and stating
after on failure and supressing errors, let's avoid the chown in the
firts place, in the interest on keeping things minimal.
2019-05-24 15:07:55 +02:00
Lennart Poettering 0c21dafb54 util-lib: use FLAGS_SET() where appropriate 2019-04-02 14:54:38 +02:00
Lennart Poettering b6cceaae30 fs-util: suppress world-writable warnings if we read /dev/null
Fixes: #12165
2019-04-01 22:07:41 +02:00
Lennart Poettering 30ff18d8a2 fs-util: change chmod_and_chown() to not complain if stat data already matches
Let's reduce the chance of failure: if we can't apply the chmod/chown
requested, check if it's applied anyway, and if so, supress the error.

This is even race-free since we operate on an O_PATH fd anyway.
2019-03-19 16:52:28 +01:00
Lennart Poettering 46d4d67d79
Merge pull request #11820 from dm0-/chase
Allow tmpfiles to create files in a root under an unprivileged directory
2019-03-04 13:04:45 +01:00
Lennart Poettering 71f514169b fs-util: add new helper syncfs_path() 2019-03-01 12:41:32 +01:00
David Michael cc14a6c011 fs-util: exempt root prefix directories from UID checks
When chase_symlinks is given a root path, it is assumed that all
processed symlinks are restricted under that path.  It should not
be necessary to verify components of that prefix path since they
are not relevant to the symlinks.

This change skips unsafe UID transitions in this root prefix, i.e.
it now ignores when an unprivileged user's directory contains a
root-owned directory above the symlink root.
2019-02-28 13:05:58 -05:00
Davide Cavalca 1c73b06926 fs-util: add missing linux/falloc.h include 2019-02-27 11:04:45 +09:00
Lennart Poettering 9a6f746fb6 locale-util: prefix special glyph enum values with SPECIAL_GLYPH_
This has been irritating me for quite a while: let's prefix these enum
values with a common prefix, like we do for almost all other enums.

No change in behaviour, just some renaming.
2018-12-14 08:22:54 +01:00
Lennart Poettering 2327f95499
Merge pull request #10984 from fbuihuu/tmpfiles-be-more-explicit-with-unsafe-transition
tmpfiles: be more explicit when an unsafe path transition is met
2018-12-10 12:31:56 +01:00
Franck Bui 145b8d0f68 fs-util: make CHASE_WARN effective with CHASE_NO_AUTOFS
This has the side effect to upgrade the log level at which the log is emitted
from debug to warning.

This might be better since after all we didn't apply a tmpfiles.d/ rule and
that actually might end up being problematic eventually.
2018-12-10 09:22:28 +01:00
Franck Bui b85ee2ec95 fs-util: rename safe_transition() into unsafe_transition()
We're always interested into finding unsafe transitions so let's make the
helper return true when it finds such transitions so we don't need to negate
its results.

No functional changes.
2018-12-10 09:19:14 +01:00
Franck Bui 36c97decbe fs-util: make chase_symlink() returns -ENOLINK when unsafe transitions are met
We previously returned -EPERM but it can be returned for various other reasons
too.

Let's use -ENOLINK instead as this value shouldn't be used currently. This
allows users of CHASE_SAFE to detect without any ambiguities when unsafe
transitions are encountered by chase_symlinks().

All current users of CHASE_SAFE that explicitly reacted on -EPERM have been
converted to react on -ENOLINK.
2018-12-10 09:18:27 +01:00
Lennart Poettering e4de72876e util-lib: split out all temporary file related calls into tmpfiles-util.c
This splits out a bunch of functions from fileio.c that have to do with
temporary files. Simply to make the header files a bit shorter, and to
group things more nicely.

No code changes, just some rearranging of source files.
2018-12-02 13:22:29 +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 de321f5228 fs-util: beef up chmod_and_chown() a bit 2018-11-29 20:21:39 +01:00
Lennart Poettering 8e060ec225 fs-util: increase start buffer size in readlinkat_malloc()
I noticed while profiling journald that we invoke readlinkat() a ton on
open /proc/self/fd/<fd>, and that the returned paths are more often than
not longer than the 99 chars used before, when we look at archived
journal files. This means for these cases we generally need to execute
two rather than one syscalls.

Let's increase the buffer size a tiny bit, so that we reduce the number
of syscalls executed. This is really a low-hanging fruit of
optimization.
2018-10-25 21:37:14 +02:00
Lennart Poettering a2689fa5fc
Merge pull request #9437 from poettering/sd-boot-count
many sd-boot/bootctl fixes, and a new "boot counting" concept, for automatic fallback to older kernels on persistent failures
2018-10-19 23:04:22 +02: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 011723a451 fs-util: include error cause in log message 2018-10-20 00:48:21 +09:00
Lennart Poettering 2f15b6253a fs-util: add racy RENAME_NOREPLACE fallback using access()
Apparently FAT on some recent kernels can't do RENAME_NOREPLACE, and of
course cannot do linkat()/unlinkat() either (as the hard link concept
does not exist on FAT). Add a fallback using an explicit beforehand
faccessat() check. This sucks, but what we can do if the safe operations
are not available?

Fixes: #10063
2018-10-02 16:11:10 +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 0c462ea4ef tree-wide: port various bits over to open_parent() 2018-07-23 13:38:18 +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