From ea61e2e9bd435eba9faf114757a531b375869830 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 17 Feb 2023 07:18:42 +0900 Subject: [PATCH 1/4] fd-util: introduce a simple helper to check a file descriptor has O_PATH --- src/basic/fd-util.c | 12 ++++++++++++ src/basic/fd-util.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index 4d6d01cd992..430db0c8790 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -808,6 +808,18 @@ int fd_reopen_condition( return new_fd; } +int fd_is_opath(int fd) { + int r; + + assert(fd >= 0); + + r = fcntl(fd, F_GETFL); + if (r < 0) + return -errno; + + return FLAGS_SET(r, O_PATH); +} + int read_nr_open(void) { _cleanup_free_ char *nr_open = NULL; int r; diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h index 952afdd64f0..f5357860f55 100644 --- a/src/basic/fd-util.h +++ b/src/basic/fd-util.h @@ -104,6 +104,7 @@ static inline int make_null_stdio(void) { int fd_reopen(int fd, int flags); int fd_reopen_condition(int fd, int flags, int mask, int *ret_new_fd); +int fd_is_opath(int fd); int read_nr_open(void); int fd_get_diskseq(int fd, uint64_t *ret); From 5f904eb7511d036ce73d02c07561c62fcf122e15 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 17 Feb 2023 07:34:13 +0900 Subject: [PATCH 2/4] xattr-util: check if fd has O_PATH and do not try setxattr() twice Follow-up for a4d2461c46f40c9ae5002a2aea35b35ccb60ef9c. --- src/basic/xattr-util.c | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/basic/xattr-util.c b/src/basic/xattr-util.c index 746e9f369e0..b42bf68f580 100644 --- a/src/basic/xattr-util.c +++ b/src/basic/xattr-util.c @@ -330,8 +330,14 @@ int xsetxattr(int fd, if (fd == AT_FDCWD) /* Both unspecified? Then operate on current working directory */ path = "."; - else + else { + r = fd_is_opath(fd); + if (r < 0) + return r; + + by_procfs = r; path = NULL; + } } else if (fd != AT_FDCWD) { @@ -345,25 +351,14 @@ int xsetxattr(int fd, by_procfs = true; /* fsetxattr() is not going to work, go via /proc/ link right-away */ } - for (;;) { - if (path) - r = FLAGS_SET(flags, AT_SYMLINK_FOLLOW) ? setxattr(path, name, value, size, 0) - : lsetxattr(path, name, value, size, 0); - else - r = by_procfs ? setxattr(FORMAT_PROC_FD_PATH(fd), name, value, size, 0) - : fsetxattr(fd, name, value, size, 0); - if (r < 0) { - if (errno == EBADF) { - if (by_procfs || path) - return -EBADF; + if (path) + r = FLAGS_SET(flags, AT_SYMLINK_FOLLOW) ? setxattr(path, name, value, size, 0) + : lsetxattr(path, name, value, size, 0); + else + r = by_procfs ? setxattr(FORMAT_PROC_FD_PATH(fd), name, value, size, 0) + : fsetxattr(fd, name, value, size, 0); + if (r < 0) + return -errno; - by_procfs = true; /* Might be an O_PATH fd, try again via /proc/ link */ - continue; - } - - return -errno; - } - - return 0; - } + return 0; } From c56c26c906ad2f28847d0faec1087ba92fbab2a0 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 17 Feb 2023 11:12:13 +0900 Subject: [PATCH 3/4] test: use mkdtemp_open() --- src/test/test-xattr-util.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/test/test-xattr-util.c b/src/test/test-xattr-util.c index 04d23ef09b2..2d2d464eccb 100644 --- a/src/test/test-xattr-util.c +++ b/src/test/test-xattr-util.c @@ -10,30 +10,29 @@ #include "fd-util.h" #include "fs-util.h" #include "macro.h" +#include "rm-rf.h" #include "string-util.h" #include "tests.h" #include "tmpfile-util.h" #include "xattr-util.h" TEST(getxattr_at_malloc) { - char t[] = "/var/tmp/xattrtestXXXXXX"; + _cleanup_(rm_rf_physical_and_freep) char *t = NULL; _cleanup_free_ char *value = NULL; _cleanup_close_ int fd = -EBADF; const char *x; int r; - assert_se(mkdtemp(t)); + fd = mkdtemp_open("/var/tmp/test-xattrtestXXXXXX", O_RDONLY|O_NOCTTY, &t); + assert_se(fd >= 0); x = strjoina(t, "/test"); assert_se(touch(x) >= 0); r = setxattr(x, "user.foo", "bar", 3, 0); - if (r < 0 && ERRNO_IS_NOT_SUPPORTED(errno)) /* no xattrs supported on /var/tmp... */ - goto cleanup; + if (r < 0 && ERRNO_IS_NOT_SUPPORTED(errno)) + return (void) log_tests_skipped_errno(errno, "no xattrs supported on /var/tmp"); assert_se(r >= 0); - fd = open(t, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY); - assert_se(fd >= 0); - assert_se(getxattr_at_malloc(fd, "test", "user.foo", 0, &value) == 3); assert_se(memcmp(value, "bar", 3) == 0); value = mfree(value); @@ -53,21 +52,15 @@ TEST(getxattr_at_malloc) { assert_se(fd >= 0); assert_se(getxattr_at_malloc(fd, NULL, "user.foo", 0, &value) == 3); assert_se(streq(value, "bar")); - -cleanup: - assert_se(unlink(x) >= 0); - assert_se(rmdir(t) >= 0); } TEST(getcrtime) { + _cleanup_(rm_rf_physical_and_freep) char *t = NULL; _cleanup_close_ int fd = -EBADF; - const char *vt; usec_t usec, k; int r; - assert_se(var_tmp_dir(&vt) >= 0); - - fd = open_tmpfile_unlinkable(vt, O_RDWR); + fd = mkdtemp_open("/var/tmp/test-xattrtestXXXXXX", 0, &t); assert_se(fd >= 0); r = fd_getcrtime(fd, &usec); From d7e32d05585ce3500e580d4913a039b78326ef88 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 17 Feb 2023 11:42:14 +0900 Subject: [PATCH 4/4] test: add tests for xsetxattr() --- src/test/test-xattr-util.c | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/test/test-xattr-util.c b/src/test/test-xattr-util.c index 2d2d464eccb..85901c9a49b 100644 --- a/src/test/test-xattr-util.c +++ b/src/test/test-xattr-util.c @@ -79,4 +79,51 @@ TEST(getcrtime) { } } +static void verify_xattr(int dfd, const char *expected) { + _cleanup_free_ char *value = NULL; + + assert_se(getxattr_at_malloc(dfd, "test", "user.foo", 0, &value) == (int) strlen(expected)); + assert_se(streq(value, expected)); +} + +TEST(xsetxattr) { + _cleanup_(rm_rf_physical_and_freep) char *t = NULL; + _cleanup_close_ int dfd = -EBADF, fd = -EBADF; + const char *x; + int r; + + dfd = mkdtemp_open("/var/tmp/test-xattrtestXXXXXX", O_PATH, &t); + assert_se(dfd >= 0); + x = strjoina(t, "/test"); + assert_se(touch(x) >= 0); + + /* by full path */ + r = xsetxattr(AT_FDCWD, x, "user.foo", "fullpath", SIZE_MAX, 0); + if (r < 0 && ERRNO_IS_NOT_SUPPORTED(r)) + return (void) log_tests_skipped_errno(r, "no xattrs supported on /var/tmp"); + assert_se(r >= 0); + verify_xattr(dfd, "fullpath"); + + /* by dirfd */ + assert_se(xsetxattr(dfd, "test", "user.foo", "dirfd", SIZE_MAX, 0) >= 0); + verify_xattr(dfd, "dirfd"); + + /* by fd (O_PATH) */ + fd = openat(dfd, "test", O_PATH|O_CLOEXEC); + assert_se(fd >= 0); + assert_se(xsetxattr(fd, NULL, "user.foo", "fd_opath", SIZE_MAX, 0) >= 0); + verify_xattr(dfd, "fd_opath"); + assert_se(xsetxattr(fd, "", "user.foo", "fd_opath", SIZE_MAX, 0) == -EINVAL); + assert_se(xsetxattr(fd, "", "user.foo", "fd_opath_empty", SIZE_MAX, AT_EMPTY_PATH) >= 0); + verify_xattr(dfd, "fd_opath_empty"); + fd = safe_close(fd); + + fd = openat(dfd, "test", O_RDONLY|O_CLOEXEC); + assert_se(xsetxattr(fd, NULL, "user.foo", "fd_regular", SIZE_MAX, 0) >= 0); + verify_xattr(dfd, "fd_regular"); + assert_se(xsetxattr(fd, "", "user.foo", "fd_regular_empty", SIZE_MAX, 0) == -EINVAL); + assert_se(xsetxattr(fd, "", "user.foo", "fd_regular_empty", SIZE_MAX, AT_EMPTY_PATH) >= 0); + verify_xattr(dfd, "fd_regular_empty"); +} + DEFINE_TEST_MAIN(LOG_DEBUG);