| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <sys/xattr.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "errno-util.h" |
| 9 |
#include "fd-util.h" |
| 10 |
#include "fs-util.h" |
| 11 |
#include "nulstr-util.h" |
| 12 |
#include "rm-rf.h" |
| 13 |
#include "string-util.h" |
| 14 |
#include "strv.h" |
| 15 |
#include "tests.h" |
| 16 |
#include "time-util.h" |
| 17 |
#include "tmpfile-util.h" |
| 18 |
#include "xattr-util.h" |
| 19 |
|
| 20 |
TEST(getxattr_at_malloc) { |
| 21 |
_cleanup_(rm_rf_physical_and_freep) char *t = NULL; |
| 22 |
_cleanup_free_ char *value = NULL; |
| 23 |
_cleanup_close_ int fd = -EBADF; |
| 24 |
const char *x; |
| 25 |
int r; |
| 26 |
|
| 27 |
ASSERT_OK(fd = mkdtemp_open("/var/tmp/test-xattrtestXXXXXX", O_RDONLY|O_NOCTTY, &t)); |
| 28 |
x = strjoina(t, "/test"); |
| 29 |
ASSERT_OK(touch(x)); |
| 30 |
|
| 31 |
r = setxattr(x, "user.foo", "bar", 3, 0); |
| 32 |
if (r < 0 && ERRNO_IS_NOT_SUPPORTED(errno)) |
| 33 |
return (void) log_tests_skipped_errno(errno, "no xattrs supported on /var/tmp"); |
| 34 |
ASSERT_OK_ERRNO(r); |
| 35 |
|
| 36 |
ASSERT_OK(getxattr_at_malloc(fd, "test", "user.foo", 0, &value, /* ret_size= */ NULL)); |
| 37 |
ASSERT_EQ(memcmp(value, "bar", 3), 0); |
| 38 |
value = mfree(value); |
| 39 |
|
| 40 |
ASSERT_OK(getxattr_at_malloc(AT_FDCWD, x, "user.foo", 0, &value, /* ret_size= */ NULL)); |
| 41 |
ASSERT_EQ(memcmp(value, "bar", 3), 0); |
| 42 |
value = mfree(value); |
| 43 |
|
| 44 |
safe_close(fd); |
| 45 |
ASSERT_OK_ERRNO(fd = open("/", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY)); |
| 46 |
r = getxattr_at_malloc(fd, "usr", "user.idontexist", 0, &value, /* ret_size= */ NULL); |
| 47 |
ASSERT_TRUE(ERRNO_IS_NEG_XATTR_ABSENT(r)); |
| 48 |
|
| 49 |
/* A non-existent path component must also be treated as xattr-absent. The kernel |
| 50 |
* returns -ENOENT in this case, which is inherited from stat(2)'s error list |
| 51 |
* (see ERRORS in getxattr(2)). Without this, callers that semantically just want |
| 52 |
* to know "is the xattr there?" end up logging spurious errors for paths that |
| 53 |
* are simply gone — e.g. journald reading a unit's cgroup xattr after the |
| 54 |
* cgroup directory has been torn down. */ |
| 55 |
r = getxattr_at_malloc(fd, "this-path-does-not-exist-XXXXXX", "user.idontexist", |
| 56 |
0, &value, /* ret_size= */ NULL); |
| 57 |
ASSERT_TRUE(ERRNO_IS_NEG_XATTR_ABSENT(r)); |
| 58 |
|
| 59 |
safe_close(fd); |
| 60 |
ASSERT_OK_ERRNO(fd = open(x, O_PATH|O_CLOEXEC)); |
| 61 |
ASSERT_OK(getxattr_at_malloc(fd, NULL, "user.foo", 0, &value, /* ret_size= */ NULL)); |
| 62 |
ASSERT_STREQ(value, "bar"); |
| 63 |
value = mfree(value); |
| 64 |
|
| 65 |
ASSERT_OK_ERRNO(setxattr(x, "user.foozu", "bar\0qux\0wal\0\0", 13, /* flags= */ 0)); |
| 66 |
ASSERT_ERROR(getxattr_at_malloc(fd, /* path= */ NULL, "user.foozu", 0, &value, /* ret_size= */ NULL), EBADMSG); |
| 67 |
size_t value_size; |
| 68 |
ASSERT_OK(getxattr_at_malloc(fd, /* path= */ NULL, "user.foozu", 0, &value, &value_size)); |
| 69 |
ASSERT_EQ(value_size, 13U); |
| 70 |
ASSERT_EQ(memcmp(value, "bar\0qux\0wal\0\0", 13), 0); |
| 71 |
ASSERT_EQ(value[13], 0); /* check extra NUL */ |
| 72 |
} |
| 73 |
|
| 74 |
TEST(getcrtime) { |
| 75 |
_cleanup_(rm_rf_physical_and_freep) char *t = NULL; |
| 76 |
_cleanup_close_ int fd = -EBADF; |
| 77 |
usec_t usec, k; |
| 78 |
int r; |
| 79 |
|
| 80 |
ASSERT_OK(fd = mkdtemp_open("/var/tmp/test-xattrtestXXXXXX", 0, &t)); |
| 81 |
|
| 82 |
r = fd_getcrtime(fd, &usec); |
| 83 |
if (r < 0) |
| 84 |
log_debug_errno(r, "btime: %m"); |
| 85 |
else |
| 86 |
log_debug("btime: %s", FORMAT_TIMESTAMP(usec)); |
| 87 |
|
| 88 |
k = now(CLOCK_REALTIME); |
| 89 |
|
| 90 |
r = fd_setcrtime(fd, 1519126446UL * USEC_PER_SEC); |
| 91 |
if (!IN_SET(r, -EOPNOTSUPP, -ENOTTY)) { |
| 92 |
ASSERT_OK(fd_getcrtime(fd, &usec)); |
| 93 |
ASSERT_TRUE(k < 1519126446UL * USEC_PER_SEC || |
| 94 |
usec == 1519126446UL * USEC_PER_SEC); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
static void verify_xattr(int dfd, const char *expected) { |
| 99 |
_cleanup_free_ char *value = NULL; |
| 100 |
|
| 101 |
ASSERT_OK(getxattr_at_malloc(dfd, "test", "user.foo", 0, &value, /* ret_size= */ NULL)); |
| 102 |
ASSERT_STREQ(value, expected); |
| 103 |
value = mfree(value); |
| 104 |
|
| 105 |
size_t size; |
| 106 |
ASSERT_OK(getxattr_at_malloc(dfd, "test", "user.foo", 0, &value, &size)); |
| 107 |
ASSERT_EQ(size, strlen(expected)); |
| 108 |
ASSERT_STREQ(value, expected); |
| 109 |
} |
| 110 |
|
| 111 |
static void xattr_symlink_test_one(int fd, const char *path) { |
| 112 |
_cleanup_free_ char *value = NULL, *list = NULL; |
| 113 |
_cleanup_strv_free_ char **list_split = NULL; |
| 114 |
int r; |
| 115 |
|
| 116 |
ASSERT_ERROR(xsetxattr_full(fd, path, 0, "trusted.bar", "bogus", SIZE_MAX, XATTR_CREATE), EEXIST); |
| 117 |
|
| 118 |
ASSERT_OK(xsetxattr(fd, path, 0, "trusted.test", "schaffen")); |
| 119 |
ASSERT_OK(getxattr_at_malloc(fd, path, "trusted.test", 0, &value, /* ret_size= */ NULL)); |
| 120 |
ASSERT_STREQ(value, "schaffen"); |
| 121 |
value = mfree(value); |
| 122 |
size_t size; |
| 123 |
ASSERT_OK(getxattr_at_malloc(fd, path, "trusted.test", 0, &value, &size)); |
| 124 |
ASSERT_EQ(size, strlen(value)); |
| 125 |
ASSERT_STREQ(value, "schaffen"); |
| 126 |
|
| 127 |
r = listxattr_at_malloc(fd, path, 0, &list); |
| 128 |
ASSERT_OK(r); |
| 129 |
ASSERT_GE(r, (int) sizeof("trusted.test\0trusted.bar")); |
| 130 |
ASSERT_NOT_NULL((list_split = strv_parse_nulstr(list, r))); |
| 131 |
ASSERT_TRUE(strv_contains(list_split, "trusted.bar")); |
| 132 |
ASSERT_TRUE(strv_contains(list_split, "trusted.test")); |
| 133 |
|
| 134 |
ASSERT_OK(xremovexattr(fd, path, 0, "trusted.test")); |
| 135 |
ASSERT_ERROR(getxattr_at_malloc(fd, path, "trusted.test", 0, &value, /* ret_size= */ NULL), ENODATA); |
| 136 |
} |
| 137 |
|
| 138 |
TEST(xsetxattr) { |
| 139 |
_cleanup_(rm_rf_physical_and_freep) char *t = NULL; |
| 140 |
_cleanup_close_ int dfd = -EBADF, fd = -EBADF; |
| 141 |
const char *x; |
| 142 |
int r; |
| 143 |
|
| 144 |
ASSERT_OK(dfd = mkdtemp_open("/var/tmp/test-xattrtestXXXXXX", O_PATH, &t)); |
| 145 |
x = strjoina(t, "/test"); |
| 146 |
ASSERT_OK(touch(x)); |
| 147 |
|
| 148 |
/* by full path */ |
| 149 |
r = xsetxattr(AT_FDCWD, x, 0, "user.foo", "fullpath"); |
| 150 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 151 |
return (void) log_tests_skipped_errno(r, "no xattrs supported on /var/tmp"); |
| 152 |
ASSERT_OK(r); |
| 153 |
verify_xattr(dfd, "fullpath"); |
| 154 |
|
| 155 |
/* by dirfd */ |
| 156 |
ASSERT_ERROR(xsetxattr_full(dfd, "test", 0, "user.foo", "dirfd", SIZE_MAX, XATTR_CREATE), EEXIST); |
| 157 |
verify_xattr(dfd, "fullpath"); |
| 158 |
|
| 159 |
ASSERT_OK(xsetxattr_full(dfd, "test", 0, "user.foo", "dirfd", SIZE_MAX, XATTR_REPLACE)); |
| 160 |
verify_xattr(dfd, "dirfd"); |
| 161 |
|
| 162 |
/* by fd (O_PATH) */ |
| 163 |
ASSERT_OK_ERRNO(fd = openat(dfd, "test", O_PATH|O_CLOEXEC)); |
| 164 |
|
| 165 |
ASSERT_OK(xremovexattr(fd, "", 0, "user.foo")); |
| 166 |
|
| 167 |
ASSERT_OK(xsetxattr_full(fd, NULL, AT_EMPTY_PATH, "user.foo", "fd_opath", SIZE_MAX, XATTR_CREATE)); |
| 168 |
verify_xattr(dfd, "fd_opath"); |
| 169 |
|
| 170 |
ASSERT_OK(xsetxattr(fd, "", 0, "user.foo", "fd_opath_empty")); |
| 171 |
verify_xattr(dfd, "fd_opath_empty"); |
| 172 |
|
| 173 |
fd = safe_close(fd); |
| 174 |
|
| 175 |
fd = openat(dfd, "test", O_RDONLY|O_CLOEXEC); |
| 176 |
|
| 177 |
ASSERT_OK(xsetxattr_full(fd, NULL, 0, "user.foo", "fd_regular", SIZE_MAX, XATTR_REPLACE)); |
| 178 |
verify_xattr(dfd, "fd_regular"); |
| 179 |
|
| 180 |
ASSERT_OK(xsetxattr(fd, "", 0, "user.foo", "fd_regular_empty")); |
| 181 |
verify_xattr(dfd, "fd_regular_empty"); |
| 182 |
|
| 183 |
fd = safe_close(fd); |
| 184 |
|
| 185 |
/* user.* xattrs are not supported on symlinks. Use trusted.* which requires privilege. */ |
| 186 |
ASSERT_OK_ERRNO(symlinkat("empty", dfd, "symlink")); |
| 187 |
ASSERT_OK_ERRNO(fd = openat(dfd, "symlink", O_NOFOLLOW|O_PATH|O_CLOEXEC)); |
| 188 |
|
| 189 |
ASSERT_ERROR(xsetxattr(dfd, "symlink", AT_SYMLINK_FOLLOW, "trusted.test", "bogus"), ENOENT); |
| 190 |
|
| 191 |
r = xsetxattr_full(dfd, "symlink", 0, "trusted.bar", "baz", SIZE_MAX, XATTR_CREATE); |
| 192 |
if (ERRNO_IS_NEG_PRIVILEGE(r)) |
| 193 |
return (void) log_tests_skipped_errno(r, "Unable to set trusted.* xattr"); |
| 194 |
ASSERT_OK(r); |
| 195 |
|
| 196 |
xattr_symlink_test_one(dfd, "symlink"); |
| 197 |
xattr_symlink_test_one(fd, NULL); |
| 198 |
xattr_symlink_test_one(fd, ""); |
| 199 |
|
| 200 |
x = strjoina(t, "/symlink"); |
| 201 |
xattr_symlink_test_one(AT_FDCWD, x); |
| 202 |
} |
| 203 |
|
| 204 |
DEFINE_TEST_MAIN(LOG_DEBUG); |