]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-xattr-util: add test cases for symlinks
authorMike Yuan <me@yhndnzj.com>
Sun, 9 Feb 2025 12:23:27 +0000 (13:23 +0100)
committerMike Yuan <me@yhndnzj.com>
Sun, 9 Feb 2025 13:51:04 +0000 (14:51 +0100)
src/test/test-xattr-util.c

index 57b5939d9394d3f2c3c7bffc51ac44c8c14a9506..25f982e1fd909c565a0a36911ae6aa5cfaec57bb 100644 (file)
@@ -7,6 +7,7 @@
 #include <unistd.h>
 
 #include "alloc-util.h"
+#include "capability-util.h"
 #include "fd-util.h"
 #include "fs-util.h"
 #include "macro.h"
@@ -86,6 +87,20 @@ static void verify_xattr(int dfd, const char *expected) {
         ASSERT_STREQ(value, expected);
 }
 
+static void xattr_symlink_test_one(int fd, const char *path) {
+        _cleanup_free_ char *value = NULL, *list = NULL;
+
+        ASSERT_OK(xsetxattr(fd, path, 0, "trusted.test", "schaffen"));
+        ASSERT_OK_EQ(getxattr_at_malloc(fd, path, "trusted.test", 0, &value), (int) STRLEN("schaffen"));
+        ASSERT_STREQ(value, "schaffen");
+
+        ASSERT_OK_EQ(listxattr_at_malloc(fd, path, 0, &list), (int) sizeof("trusted.test"));
+        ASSERT_STREQ(list, "trusted.test");
+
+        ASSERT_OK(xremovexattr(fd, path, 0, "trusted.test"));
+        ASSERT_ERROR(getxattr_at_malloc(fd, path, "trusted.test", 0, &value), ENODATA);
+}
+
 TEST(xsetxattr) {
         _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
         _cleanup_close_ int dfd = -EBADF, fd = -EBADF;
@@ -131,6 +146,23 @@ TEST(xsetxattr) {
 
         ASSERT_OK(xsetxattr(fd, "", 0, "user.foo", "fd_regular_empty"));
         verify_xattr(dfd, "fd_regular_empty");
+
+        fd = safe_close(fd);
+
+        /* user.* xattrs are not supported on symlinks. Use trusted.* which requires privilege. */
+        if (have_effective_cap(CAP_SYS_ADMIN) > 0) {
+                ASSERT_OK_ERRNO(symlinkat("empty", dfd, "symlink"));
+                ASSERT_OK_ERRNO(fd = openat(dfd, "symlink", O_NOFOLLOW|O_PATH|O_CLOEXEC));
+
+                ASSERT_ERROR(xsetxattr(dfd, "symlink", AT_SYMLINK_FOLLOW, "trusted.test", "bogus"), ENOENT);
+
+                xattr_symlink_test_one(dfd, "symlink");
+                xattr_symlink_test_one(fd, NULL);
+                xattr_symlink_test_one(fd, "");
+
+                x = strjoina(t, "/symlink");
+                xattr_symlink_test_one(AT_FDCWD, x);
+        }
 }
 
 DEFINE_TEST_MAIN(LOG_DEBUG);