Commit Graph

46 Commits

Author SHA1 Message Date
Daan De Meyer
7efaab482a chase: Tighten "." and "./" check
Currently the check also succeeds if the input path starts with a dot, whereas
we only want it to succeed for "." and "./". Tighten the check and add a test.
2024-06-11 13:55:35 +01:00
Zbigniew Jędrzejewski-Szmek
454318d3e8 various: use strdup_to() in various obvious cases
strdup_to() returns 0 on success and here we convert obvious blocks
which either return -ENOMEM or 0.
2024-03-20 15:13:44 +01:00
Yu Watanabe
e40b11be04 fs-util: rename xopenat() -> xopanat_full() 2024-02-16 00:25:37 +09:00
Yu Watanabe
bec1383623 chase: do not wrap xopenat() with RET_NERRNO()
Follow-up for 47f0e1b5e0.
2024-02-16 00:25:37 +09:00
Lennart Poettering
9c21cfdd7d chase: fix corner case when using CHASE_PARENT with a path ending in ".."
If we use CHASE_PARENT on a path ending in ".." then things are a bit
weird, because we the last path we look at is actually the *parent* and not
the *child* of the preceeding path. Hence we cannot just return the 2nd
to last fd we look at. We have to correct it, by going *two* levels up,
to get to the actual parent, and make sure CHASE_PARENT does what it
should.

Example: for the path /a/b/c chase() with CHASE_PARENT will return
/a/b/c as path, and the fd returned points to /a/b. All good.  But now,
for the path /a/b/c/.. chase() with CHASE_PARENT would previously return
/a/b as path (which is OK) but the fd would point to /a/b/c, which is
*not* the parent of /a/b, after all! To get to the actual parent of
/a/b we have to go *two* levels up to get to /a.

Very confusing. But that's what we here for, no?

@mrc0mmand ran into this in https://github.com/systemd/systemd/pull/28891#issuecomment-1782833722
2023-11-01 14:43:24 +00:00
Yu Watanabe
4445242a1e chase: propagate error in dir_fd_is_root()
Otherwise, if it fails, chaseat() may return unexpected result and
triggers an assertion in chase().
2023-07-29 22:09:32 +09:00
Yu Watanabe
83c57d8cde chase: check root path in more detail
In chaseat() we call dir_fd_is_root() several places, and the final
result depends on it. If the root path specified to `chase()` is not
normalized but points to "/", e.g. "/../", assertions in `chaseat()` or
`chase()` may be triggered.
2023-07-29 22:09:28 +09:00
Yu Watanabe
4de5b4e375 chase: carefully handle result of extracting parent directory
Should not change any behavior.
2023-07-29 21:58:30 +09:00
Yu Watanabe
1c13bdf342 chase: add two more assertions
When 'need_absolute' is true, 'done' should always contain "/" at the
beginning, and thus should not be NULL.
2023-07-29 21:58:30 +09:00
Yu Watanabe
b7e957d30f chase: drop CHASE_AT_RESOLVE_IN_ROOT earlier
The flag will be anyway dropped in chaseat(), but let's shortcut.
2023-07-29 21:58:29 +09:00
Yu Watanabe
5f0bae7bff chase: fix CHASE_STEP with ".." 2023-07-29 21:58:29 +09:00
Yu Watanabe
00a050b395 chase: reuse "done" to open fd of starting point
For readability that 'done' and 'fd' are always consistent with each other.

- dir_fd == AT_FDCWD:
  - path is absolute:
    - previous: fd = open("/")
    - current:  fd = openat(AT_FDCWD, "/")
  - path is relative:
    - previous: fd = openat(AT_FDCWD, ".")
    - current:  fd = openat(AT_FDCWD, ".")
- dir_fd >= 0:
  - dir_fd points to "/":
    - previous: fd = openat(dir_fd, ".")
    - current:  fd = openat(dir_fd, "/")
  - dir_fd does not point to "/":
    - previous: fd = openat(dir_fd, ".")
    - current:  fd = openat(dir_fd, ".")

Hence, this should not change any behavior. Just refactoring.
2023-07-29 21:58:29 +09:00
Zbigniew Jędrzejewski-Szmek
d1579f0b70 Merge pull request #27885 from DaanDeMeyer/please-dont-make-me-write-more-openat-helpers
Add more openat like helper functions
2023-06-07 13:25:33 +02:00
Daan De Meyer
0bb5653530 Merge pull request #27918 from yuwata/chase-filename
chase: handle root path more carefully in chase_and_open()
2023-06-06 15:01:37 +02:00
Daan De Meyer
3c8f449cc8 chase: Allow passing NULL as the empty path to chaseat()
Per coding style, we should accept NULL as the empty path.
2023-06-06 14:42:03 +02:00
Yu Watanabe
308b189511 chase: fix triggering assertion 2023-06-05 10:22:47 +02:00
Yu Watanabe
3991f35fbd chase: handle root path more carefully in chase_and_open()
chase_and_open() may be called with relative root path.
2023-06-05 15:50:46 +09:00
Daan De Meyer
420d2e3136 fs-util: Add XOpenFlags with XO_LABEL flag to have xopenat() MAC label files/dirs 2023-05-31 13:15:56 +02:00
Yu Watanabe
60e761d8f3 chase: replace path_prefix_root_cwd() with chaseat_prefix_root()
The function path_prefix_root_cwd() was introduced for prefixing the
result from chaseat() with root, but
- it is named slightly generic,
- the logic is different from what chase() does.

This makes the name more explanative and specific for the result of the
chaseat(), and make the logic consistent with chase().

Fixes https://github.com/systemd/systemd/pull/27199#issuecomment-1511387731.

Follow-up for #27199.
2023-04-19 03:38:59 +09:00
Yu Watanabe
9a0dcf03fa chase: prefix with the root directory only when it is not "/" 2023-04-19 03:28:34 +09:00
Yu Watanabe
237bf933de chase: drop repeated call of empty_to_root() 2023-04-19 03:28:34 +09:00
Yu Watanabe
b3ef56bc8e chase: update outdated comment about result path 2023-04-19 03:28:34 +09:00
Yu Watanabe
24be89ebd8 chase: make the result absolute when a symlink is absolute
As the path may be outside of the specified dir_fd.
2023-04-19 03:28:34 +09:00
Yu Watanabe
c0552b359c chase: make chaseat() provides absolute path also when dir_fd points to the root directory
Usually, we pass the file descriptor of the root directory to chaseat()
when `--root=` is not specified. Previously, even in such case, the
result was relative, and we need to prefix the path with "/" when we
want to pass the path to other functions that do not support dir_fd, or
log or show the path. That's inconvenient.
2023-04-19 03:28:34 +09:00
Yu Watanabe
4ea0bcb922 chase: CHASE_MKDIR_0755 requires CHASE_NONEXISTENT and/or CHASE_PARENT
When CHASE_MKDIR_0755 is specified without CHASE_NONEXISTENT and
CHASE_PARENT, then chase() succeeds only when the file specified by
the path already exists, and in that case, chase() does not create
any parent directories, and CHASE_MKDIR_0755 is meaningless.

Let's mention that CHASE_MKDIR_0755 needs to be specified with
CHASE_NONEXISTENT or CHASE_PARENT, and adds a assertion about that.
2023-04-14 16:36:13 +09:00
Yu Watanabe
5a2f674a00 chase: use FLAGS_SET() macro 2023-04-14 16:28:54 +09:00
Yu Watanabe
0d68cd72b6 chase: drop redundant call of delete_trailing_chars()
In that branch, 'root' is a non-root and absolute path.
Hence, delete_trailing_chars() does not make the path empty.
And, if the path contains redundant slashes at the end, that will be
dropped by path_simplify().
2023-04-11 09:06:05 +02:00
Yu Watanabe
a6ef5ef707 chase: use dir_fd_is_root() to check if fd points to the root directory
As commented in dir_fd_is_root(), comparing inode is not enough to
determine if we are at the root directory.
2023-04-10 09:55:29 +09:00
Yu Watanabe
4443e3003a chase: use stat_inode_same() at one more place 2023-04-10 09:45:21 +09:00
Yu Watanabe
75adfc3b17 chase: drop one redundant call of fstat()
Now, 'st' is always synced with 'fd'. Hence, we can use
stat_verify_directory().
2023-04-10 08:43:31 +09:00
Yu Watanabe
ad66c7f1d3 chase: make struct stat always sync with the opened fd
Previously, struct stat may not be correctly synced with the currently
opened fd, e.g. when a path contains symlink which points to an absolute
path.

This also rename variables for struct stat, to make them consistent with
the corresponding fd.
2023-04-10 08:40:14 +09:00
Yu Watanabe
db0096f2ad chase: drop unnecessary conditions for buffer 2023-04-10 08:17:07 +09:00
Yu Watanabe
e212f42279 fd-util: introduce dir_fd_is_root_or_cwd() 2023-04-05 00:52:56 +09:00
Yu Watanabe
21eac258f8 chase: fix error handling
Fixes a bug introduced by 63bfd52f48.
2023-04-04 09:20:09 +02:00
Daan De Meyer
5ccf06074a Merge pull request #27064 from DaanDeMeyer/chase-fixes
Chase fixes
2023-03-30 11:44:33 +02:00
Daan De Meyer
ee6fea2b7f Merge pull request #27063 from yuwata/chase-graceful
chase: gracefully drop CHASE_AT_RESOLVE_IN_ROOT when AT_FDCWD is specified
2023-03-30 11:44:17 +02:00
Yu Watanabe
8bf26bfe26 chase: add CHASE_AT_RESOLVE_IN_ROOT only when it is necessary 2023-03-30 16:29:45 +09:00
Yu Watanabe
e115daa6a8 chase: drop CHASE_AT_RESOLVE_IN_ROOT when AT_FDCWD or root dir fd is specified
If we get AT_FDCWD or root dir fd, we always resolve symlinks relative to the host's root.
Hence, the flag is meaningless.
2023-03-30 16:29:37 +09:00
Daan De Meyer
708e88704b chase: Simplify chase_and_open() and chase_and_openat()
xopenat() now calls fd_reopen() if an empty path is specified, so
let's make use of that to simplify the chase open helpers.
2023-03-30 09:04:14 +02:00
Daan De Meyer
16859751ca chase: Fix error code check 2023-03-30 09:04:14 +02:00
Daan De Meyer
11659e4830 chase: Fix formatting 2023-03-30 09:04:14 +02:00
Yu Watanabe
6d5d3e207b chase: fix indentation 2023-03-30 14:52:13 +09:00
Yu Watanabe
52576a7503 chase: fix comment 2023-03-30 14:52:13 +09:00
Daan De Meyer
670afc182a chase-symlinks: Add chase_and_open_parent() + at() variant
Helper to chase a path, pin its parent directory and return the filename
of the resolved path in its parent directory.
2023-03-26 20:31:10 +02:00
Daan De Meyer
e5f2490200 chase-symlinks: Allow optional path with CHASE_EXTRACT_FILENAME
This allows helpers to set it unconditionally even if the caller
is not interested in the path.
2023-03-24 13:53:45 +01:00
Daan De Meyer
f461a28da7 chase-symlinks: Rename chase_symlinks() to chase()
Chasing symlinks is a core function that's used in a lot of places
so it deservers a less verbose names so let's rename it to chase()
and chaseat().

We also slightly change the pattern used for the chaseat() helpers
so we get chase_and_openat() and similar.
2023-03-24 13:43:51 +01:00