Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame^] | 1 | From b060c53503339c45808efeb4294a03105a2999a5 Mon Sep 17 00:00:00 2001 |
| 2 | From: Yu Watanabe <watanabe.yu+github@gmail.com> |
| 3 | Date: Wed, 2 Feb 2022 14:05:45 +0900 |
| 4 | Subject: [PATCH] mkdir: allow to create directory whose path contains symlink |
| 5 | Cc: pavel@zhukoff.net |
| 6 | |
| 7 | Upstream-Status: Backport |
| 8 | Upstream-Url: https://github.com/systemd/systemd/pull/22359 |
| 9 | |
| 10 | Signed-off-by: Pavel Zhukov <pavel.zhukov@huawei.com> |
| 11 | |
| 12 | |
| 13 | core/mount: fail early if directory cannot be created |
| 14 | |
| 15 | Prompted by #22334. |
| 16 | |
| 17 | mkdir: CHASE_NONEXISTENT cannot used in chase_symlinks_and_stat() |
| 18 | |
| 19 | mkdir: allow to create directory whose path contains symlink |
| 20 | |
| 21 | Fixes a regression caused by 3008a6f21c1c42efe852d69798a2fdd63fe657ec. |
| 22 | |
| 23 | Before the commit, when `mkdir_parents_internal()` is called from `mkdir_p()`, |
| 24 | it uses `_mkdir()` as `flag` is zero. But after the commit, `mkdir_safe_internal()` |
| 25 | is always used. Hence, if the path contains a symlink, it fails with -ENOTDIR. |
| 26 | |
| 27 | To fix the issue, this makes `mkdir_p()` calls `mkdir_parents_internal()` with |
| 28 | MKDIR_FOLLOW_SYMLINK flag. |
| 29 | |
| 30 | Fixes #22334. |
| 31 | |
| 32 | test: add a test for mkdir_p() |
| 33 | --- |
| 34 | src/basic/mkdir.c | 4 ++-- |
| 35 | src/core/mount.c | 4 +++- |
| 36 | src/test/meson.build | 2 ++ |
| 37 | src/test/test-mkdir.c | 30 ++++++++++++++++++++++++++++++ |
| 38 | 4 files changed, 37 insertions(+), 3 deletions(-) |
| 39 | create mode 100644 src/test/test-mkdir.c |
| 40 | |
| 41 | diff --git a/src/basic/mkdir.c b/src/basic/mkdir.c |
| 42 | index 6e2b94d024..51a0d74e87 100644 |
| 43 | --- a/src/basic/mkdir.c |
| 44 | +++ b/src/basic/mkdir.c |
| 45 | @@ -42,7 +42,7 @@ int mkdir_safe_internal( |
| 46 | if ((flags & MKDIR_FOLLOW_SYMLINK) && S_ISLNK(st.st_mode)) { |
| 47 | _cleanup_free_ char *p = NULL; |
| 48 | |
| 49 | - r = chase_symlinks_and_stat(path, NULL, CHASE_NONEXISTENT, &p, &st, NULL); |
| 50 | + r = chase_symlinks_and_stat(path, NULL, 0, &p, &st, NULL); |
| 51 | if (r < 0) |
| 52 | return r; |
| 53 | if (r == 0) |
| 54 | @@ -162,7 +162,7 @@ int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, uid_t ui |
| 55 | |
| 56 | assert(_mkdirat != mkdirat); |
| 57 | |
| 58 | - r = mkdir_parents_internal(prefix, path, mode, uid, gid, flags, _mkdirat); |
| 59 | + r = mkdir_parents_internal(prefix, path, mode, uid, gid, flags | MKDIR_FOLLOW_SYMLINK, _mkdirat); |
| 60 | if (r < 0) |
| 61 | return r; |
| 62 | |
| 63 | diff --git a/src/core/mount.c b/src/core/mount.c |
| 64 | index 0170406351..c650b5abe2 100644 |
| 65 | --- a/src/core/mount.c |
| 66 | +++ b/src/core/mount.c |
| 67 | @@ -1027,8 +1027,10 @@ static void mount_enter_mounting(Mount *m) { |
| 68 | r = mkdir_p_label(p->what, m->directory_mode); |
| 69 | /* mkdir_p_label() can return -EEXIST if the target path exists and is not a directory - which is |
| 70 | * totally OK, in case the user wants us to overmount a non-directory inode. */ |
| 71 | - if (r < 0 && r != -EEXIST) |
| 72 | + if (r < 0 && r != -EEXIST) { |
| 73 | log_unit_error_errno(UNIT(m), r, "Failed to make bind mount source '%s': %m", p->what); |
| 74 | + goto fail; |
| 75 | + } |
| 76 | } |
| 77 | |
| 78 | if (p) { |
| 79 | diff --git a/src/test/meson.build b/src/test/meson.build |
| 80 | index 9a1c481f22..7aa1d9c6ea 100644 |
| 81 | --- a/src/test/meson.build |
| 82 | +++ b/src/test/meson.build |
| 83 | @@ -193,6 +193,8 @@ tests += [ |
| 84 | |
| 85 | [['src/test/test-macro.c']], |
| 86 | |
| 87 | + [['src/test/test-mkdir.c']], |
| 88 | + |
| 89 | [['src/test/test-json.c']], |
| 90 | |
| 91 | [['src/test/test-modhex.c']], |
| 92 | diff --git a/src/test/test-mkdir.c b/src/test/test-mkdir.c |
| 93 | new file mode 100644 |
| 94 | index 0000000000..c715d5f096 |
| 95 | --- /dev/null |
| 96 | +++ b/src/test/test-mkdir.c |
| 97 | @@ -0,0 +1,30 @@ |
| 98 | +/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 99 | + |
| 100 | +#include <unistd.h> |
| 101 | + |
| 102 | +#include "mkdir.h" |
| 103 | +#include "path-util.h" |
| 104 | +#include "rm-rf.h" |
| 105 | +#include "tests.h" |
| 106 | +#include "tmpfile-util.h" |
| 107 | + |
| 108 | +TEST(mkdir_p) { |
| 109 | + _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL; |
| 110 | + _cleanup_free_ char *p = NULL; |
| 111 | + |
| 112 | + assert_se(mkdtemp_malloc("/tmp/test-mkdir-XXXXXX", &tmp) >= 0); |
| 113 | + |
| 114 | + assert_se(p = path_join(tmp, "run")); |
| 115 | + assert_se(mkdir_p(p, 0755) >= 0); |
| 116 | + |
| 117 | + p = mfree(p); |
| 118 | + assert_se(p = path_join(tmp, "var/run")); |
| 119 | + assert_se(mkdir_parents(p, 0755) >= 0); |
| 120 | + assert_se(symlink("../run", p) >= 0); |
| 121 | + |
| 122 | + p = mfree(p); |
| 123 | + assert_se(p = path_join(tmp, "var/run/hoge/foo/baz")); |
| 124 | + assert_se(mkdir_p(p, 0755) >= 0); |
| 125 | +} |
| 126 | + |
| 127 | +DEFINE_TEST_MAIN(LOG_DEBUG); |
| 128 | -- |
| 129 | 2.34.1 |
| 130 | |