| 1 |
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2026 Red Hat, Inc. |
| 4 |
* SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "libglnx-config.h" |
| 8 |
#include "libglnx.h" |
| 9 |
#include <glib.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <gio/gio.h> |
| 12 |
#include <err.h> |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
#include "libglnx-testlib.h" |
| 16 |
|
| 17 |
#define GLNX_CHASE_DEBUG_NO_OPENAT2 (1U << 31) |
| 18 |
#define GLNX_CHASE_DEBUG_NO_OPEN_TREE (1U << 30) |
| 19 |
|
| 20 |
const char *test_paths[] = { |
| 21 |
"file/baz", |
| 22 |
"file/baz/", |
| 23 |
"file/baz/.", |
| 24 |
"file/baz/../baz", |
| 25 |
"file////baz/..//baz", |
| 26 |
"file////baz/..//../file/baz", |
| 27 |
}; |
| 28 |
|
| 29 |
static ino_t |
| 30 |
get_ino (int fd) |
| 31 |
{ |
| 32 |
int r; |
| 33 |
struct stat st; |
| 34 |
|
| 35 |
r = fstatat (fd, "", &st, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW); |
| 36 |
g_assert_cmpint (r, >=, 0); |
| 37 |
|
| 38 |
return st.st_ino; |
| 39 |
} |
| 40 |
|
| 41 |
static ino_t |
| 42 |
path_get_ino (const char *path) |
| 43 |
{ |
| 44 |
int r; |
| 45 |
struct stat st; |
| 46 |
|
| 47 |
r = fstatat (AT_FDCWD, path, &st, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW); |
| 48 |
g_assert_cmpint (r, >=, 0); |
| 49 |
|
| 50 |
return st.st_ino; |
| 51 |
} |
| 52 |
|
| 53 |
static char * |
| 54 |
get_abspath (int dfd, |
| 55 |
const char *path) |
| 56 |
{ |
| 57 |
g_autofree char *proc_fd_path = NULL; |
| 58 |
g_autofree char *abs = NULL; |
| 59 |
g_autoptr(GError) error = NULL; |
| 60 |
|
| 61 |
proc_fd_path = g_strdup_printf ("/proc/self/fd/%d", dfd); |
| 62 |
abs = glnx_readlinkat_malloc (AT_FDCWD, proc_fd_path, NULL, &error); |
| 63 |
g_assert_no_error (error); |
| 64 |
g_assert_nonnull (abs); |
| 65 |
|
| 66 |
return g_strdup_printf ("%s/%s", abs, path); |
| 67 |
} |
| 68 |
|
| 69 |
static void |
| 70 |
check_chase (int dfd, |
| 71 |
const char *path, |
| 72 |
GlnxChaseFlags flags, |
| 73 |
int expected_ino) |
| 74 |
{ |
| 75 |
g_autoptr(GError) error = NULL; |
| 76 |
glnx_autofd int chase_fd = -1; |
| 77 |
|
| 78 |
/* let's try to test the openat2 impl */ |
| 79 |
chase_fd = glnx_chaseat (dfd, path, flags, &error); |
| 80 |
g_assert_no_error (error); |
| 81 |
g_assert_cmpint (chase_fd, >=, 0); |
| 82 |
g_assert_cmpint (get_ino (chase_fd), ==, expected_ino); |
| 83 |
g_clear_fd (&chase_fd, NULL); |
| 84 |
|
| 85 |
/* let's try to test the open_tree impl */ |
| 86 |
chase_fd = glnx_chaseat (dfd, path, |
| 87 |
flags | GLNX_CHASE_DEBUG_NO_OPENAT2, |
| 88 |
&error); |
| 89 |
g_assert_no_error (error); |
| 90 |
g_assert_cmpint (chase_fd, >=, 0); |
| 91 |
g_assert_cmpint (get_ino (chase_fd), ==, expected_ino); |
| 92 |
g_clear_fd (&chase_fd, NULL); |
| 93 |
|
| 94 |
/* let's try to test the openat impl */ |
| 95 |
chase_fd = glnx_chaseat (dfd, path, |
| 96 |
flags | |
| 97 |
GLNX_CHASE_DEBUG_NO_OPENAT2 | |
| 98 |
GLNX_CHASE_DEBUG_NO_OPEN_TREE, |
| 99 |
&error); |
| 100 |
g_assert_no_error (error); |
| 101 |
g_assert_cmpint (chase_fd, >=, 0); |
| 102 |
g_assert_cmpint (get_ino (chase_fd), ==, expected_ino); |
| 103 |
g_clear_fd (&chase_fd, NULL); |
| 104 |
} |
| 105 |
|
| 106 |
static void |
| 107 |
check_chase_error (int dfd, |
| 108 |
const char *path, |
| 109 |
GlnxChaseFlags flags, |
| 110 |
GQuark err_domain, |
| 111 |
gint err_code) |
| 112 |
{ |
| 113 |
g_autoptr(GError) error = NULL; |
| 114 |
glnx_autofd int chase_fd = -1; |
| 115 |
|
| 116 |
/* let's try to test the openat2 impl */ |
| 117 |
chase_fd = glnx_chaseat (dfd, path, flags, &error); |
| 118 |
g_assert_cmpint (chase_fd, <, 0); |
| 119 |
g_assert_error (error, err_domain, err_code); |
| 120 |
g_clear_error (&error); |
| 121 |
|
| 122 |
/* let's try to test the open_tree impl */ |
| 123 |
chase_fd = glnx_chaseat (dfd, path, |
| 124 |
flags | GLNX_CHASE_DEBUG_NO_OPENAT2, |
| 125 |
&error); |
| 126 |
g_assert_cmpint (chase_fd, <, 0); |
| 127 |
g_assert_error (error, err_domain, err_code); |
| 128 |
g_clear_error (&error); |
| 129 |
|
| 130 |
/* let's try to test the openat impl */ |
| 131 |
chase_fd = glnx_chaseat (dfd, path, |
| 132 |
flags | |
| 133 |
GLNX_CHASE_DEBUG_NO_OPENAT2 | |
| 134 |
GLNX_CHASE_DEBUG_NO_OPEN_TREE, |
| 135 |
&error); |
| 136 |
g_assert_cmpint (chase_fd, <, 0); |
| 137 |
g_assert_error (error, err_domain, err_code); |
| 138 |
g_clear_error (&error); |
| 139 |
} |
| 140 |
|
| 141 |
static void |
| 142 |
test_chase_relative (void) |
| 143 |
{ |
| 144 |
g_autoptr(GError) error = NULL; |
| 145 |
glnx_autofd int dfd = -1; |
| 146 |
int expected_ino; |
| 147 |
|
| 148 |
g_assert_true (glnx_shutil_mkdir_p_at_open (AT_FDCWD, "file/baz", 0755, |
| 149 |
&dfd, |
| 150 |
NULL, &error)); |
| 151 |
g_assert_no_error (error); |
| 152 |
g_assert_cmpint (dfd, >=, 0); |
| 153 |
|
| 154 |
expected_ino = get_ino (dfd); |
| 155 |
|
| 156 |
for (size_t i = 0; i < G_N_ELEMENTS (test_paths); i++) |
| 157 |
check_chase (AT_FDCWD, test_paths[i], 0, expected_ino); |
| 158 |
|
| 159 |
check_chase_error (AT_FDCWD, "nope", 0, G_IO_ERROR, G_IO_ERROR_NOT_FOUND); |
| 160 |
} |
| 161 |
|
| 162 |
static void |
| 163 |
test_chase_relative_fd (void) |
| 164 |
{ |
| 165 |
g_autoptr(GError) error = NULL; |
| 166 |
glnx_autofd int dfd = -1; |
| 167 |
int expected_ino; |
| 168 |
glnx_autofd int cwdfd = -1; |
| 169 |
|
| 170 |
g_assert_true (glnx_shutil_mkdir_p_at_open (AT_FDCWD, "file/baz", 0755, |
| 171 |
&dfd, |
| 172 |
NULL, &error)); |
| 173 |
g_assert_no_error (error); |
| 174 |
g_assert_cmpint (dfd, >=, 0); |
| 175 |
|
| 176 |
expected_ino = get_ino (dfd); |
| 177 |
|
| 178 |
cwdfd = openat (AT_FDCWD, ".", O_PATH | O_CLOEXEC | O_NOFOLLOW); |
| 179 |
g_assert_cmpint (cwdfd, >=, 0); |
| 180 |
|
| 181 |
for (size_t i = 0; i < G_N_ELEMENTS (test_paths); i++) |
| 182 |
check_chase (cwdfd, test_paths[i], 0, expected_ino); |
| 183 |
|
| 184 |
check_chase_error (cwdfd, "nope", 0, G_IO_ERROR, G_IO_ERROR_NOT_FOUND); |
| 185 |
} |
| 186 |
|
| 187 |
static void |
| 188 |
test_chase_absolute (void) |
| 189 |
{ |
| 190 |
g_autoptr(GError) error = NULL; |
| 191 |
glnx_autofd int dfd = -1; |
| 192 |
int expected_ino; |
| 193 |
glnx_autofd int cwdfd = -1; |
| 194 |
g_autofree char *proc_fd_path = NULL; |
| 195 |
g_autofree char *cwd_path = NULL; |
| 196 |
|
| 197 |
g_assert_true (glnx_shutil_mkdir_p_at_open (AT_FDCWD, "file/baz", 0755, |
| 198 |
&dfd, |
| 199 |
NULL, &error)); |
| 200 |
g_assert_no_error (error); |
| 201 |
g_assert_cmpint (dfd, >=, 0); |
| 202 |
|
| 203 |
expected_ino = get_ino (dfd); |
| 204 |
|
| 205 |
cwdfd = openat (AT_FDCWD, ".", O_PATH | O_CLOEXEC | O_NOFOLLOW); |
| 206 |
g_assert_cmpint (cwdfd, >=, 0); |
| 207 |
|
| 208 |
cwd_path = get_abspath (cwdfd, ""); |
| 209 |
|
| 210 |
for (size_t i = 0; i < G_N_ELEMENTS (test_paths); i++) |
| 211 |
{ |
| 212 |
g_autofree char *abspath = NULL; |
| 213 |
|
| 214 |
abspath = g_strdup_printf ("%s/%s", cwd_path, test_paths[i]); |
| 215 |
check_chase (AT_FDCWD, abspath, 0, expected_ino); |
| 216 |
} |
| 217 |
|
| 218 |
check_chase_error (AT_FDCWD, "/nope/nope/nope/345298308497623012313243543", 0, |
| 219 |
G_IO_ERROR, G_IO_ERROR_NOT_FOUND); |
| 220 |
} |
| 221 |
|
| 222 |
static void |
| 223 |
test_chase_link (void) |
| 224 |
{ |
| 225 |
g_autoptr(GError) error = NULL; |
| 226 |
glnx_autofd int dfd = -1; |
| 227 |
int link_ino; |
| 228 |
int target_ino; |
| 229 |
|
| 230 |
g_assert_true (glnx_shutil_mkdir_p_at_open (AT_FDCWD, "file/baz", 0755, |
| 231 |
&dfd, |
| 232 |
NULL, &error)); |
| 233 |
g_assert_no_error (error); |
| 234 |
g_assert_cmpint (dfd, >=, 0); |
| 235 |
|
| 236 |
g_assert_cmpint (symlinkat ("file/baz", AT_FDCWD, "link"), ==, 0); |
| 237 |
|
| 238 |
target_ino = get_ino (dfd); |
| 239 |
link_ino = path_get_ino ("link"); |
| 240 |
|
| 241 |
check_chase (AT_FDCWD, "link", 0, target_ino); |
| 242 |
check_chase (AT_FDCWD, "link/", 0, target_ino); |
| 243 |
check_chase (AT_FDCWD, "link///", 0, target_ino); |
| 244 |
check_chase (AT_FDCWD, "link/.//.", 0, target_ino); |
| 245 |
check_chase (AT_FDCWD, "link", 0, target_ino); |
| 246 |
|
| 247 |
check_chase (AT_FDCWD, "link", GLNX_CHASE_NOFOLLOW, link_ino); |
| 248 |
check_chase (AT_FDCWD, "./file/../link", GLNX_CHASE_NOFOLLOW, link_ino); |
| 249 |
check_chase (AT_FDCWD, "link/", GLNX_CHASE_NOFOLLOW, target_ino); |
| 250 |
check_chase (AT_FDCWD, "././link/.", GLNX_CHASE_NOFOLLOW, target_ino); |
| 251 |
check_chase (AT_FDCWD, "link/.//", GLNX_CHASE_NOFOLLOW, target_ino); |
| 252 |
|
| 253 |
check_chase (AT_FDCWD, "link", |
| 254 |
GLNX_CHASE_NOFOLLOW | GLNX_CHASE_RESOLVE_NO_SYMLINKS, |
| 255 |
link_ino); |
| 256 |
check_chase_error (AT_FDCWD, "link", |
| 257 |
GLNX_CHASE_RESOLVE_NO_SYMLINKS, |
| 258 |
G_IO_ERROR, G_IO_ERROR_TOO_MANY_LINKS); |
| 259 |
} |
| 260 |
|
| 261 |
static void |
| 262 |
test_chase_resolve (void) |
| 263 |
{ |
| 264 |
g_autoptr(GError) error = NULL; |
| 265 |
glnx_autofd int foo_dfd = -1; |
| 266 |
glnx_autofd int bar_dfd = -1; |
| 267 |
g_autofree char *foo_abspath = NULL; |
| 268 |
int ino; |
| 269 |
|
| 270 |
g_assert_true (glnx_shutil_mkdir_p_at_open (AT_FDCWD, "foo", 0755, |
| 271 |
&foo_dfd, |
| 272 |
NULL, &error)); |
| 273 |
g_assert_no_error (error); |
| 274 |
g_assert_cmpint (foo_dfd, >=, 0); |
| 275 |
|
| 276 |
g_assert_true (glnx_shutil_mkdir_p_at_open (AT_FDCWD, "foo/bar", 0755, |
| 277 |
&bar_dfd, |
| 278 |
NULL, &error)); |
| 279 |
g_assert_no_error (error); |
| 280 |
g_assert_cmpint (bar_dfd, >=, 0); |
| 281 |
|
| 282 |
foo_abspath = get_abspath (foo_dfd, ""); |
| 283 |
|
| 284 |
g_assert_cmpint (symlinkat ("..", foo_dfd, "link1"), ==, 0); |
| 285 |
g_assert_cmpint (symlinkat ("bar/../..", foo_dfd, "link2"), ==, 0); |
| 286 |
g_assert_cmpint (symlinkat (foo_abspath, foo_dfd, "link3"), ==, 0); |
| 287 |
g_assert_cmpint (symlinkat ("/bar", foo_dfd, "link4"), ==, 0); |
| 288 |
g_assert_cmpint (symlinkat ("link1/foo", foo_dfd, "link5"), ==, 0); |
| 289 |
g_assert_cmpint (symlinkat ("link7", foo_dfd, "link6"), ==, 0); |
| 290 |
g_assert_cmpint (symlinkat ("link6", foo_dfd, "link7"), ==, 0); |
| 291 |
|
| 292 |
ino = get_ino (bar_dfd); |
| 293 |
|
| 294 |
/* A bunch of different ways to get from CWD and foo to bar */ |
| 295 |
check_chase (foo_dfd, "./bar", 0, ino); |
| 296 |
check_chase (foo_dfd, "../foo/bar", 0, ino); |
| 297 |
check_chase (foo_dfd, "link1/foo/bar", 0, ino); |
| 298 |
check_chase (AT_FDCWD, "foo/link1/foo/bar", 0, ino); |
| 299 |
check_chase (foo_dfd, "link2/foo/bar", 0, ino); |
| 300 |
check_chase (AT_FDCWD, ".///foo/./link2/foo/bar", 0, ino); |
| 301 |
check_chase (foo_dfd, "link3/bar", 0, ino); |
| 302 |
check_chase (AT_FDCWD, ".///foo/./link3/bar", 0, ino); |
| 303 |
check_chase (foo_dfd, "link5/bar", 0, ino); |
| 304 |
|
| 305 |
/* check that NO_SYMLINKS works with a component in the middle */ |
| 306 |
check_chase_error (AT_FDCWD, "foo/link3/bar", |
| 307 |
GLNX_CHASE_RESOLVE_NO_SYMLINKS, |
| 308 |
G_IO_ERROR, G_IO_ERROR_TOO_MANY_LINKS); |
| 309 |
|
| 310 |
/* link6 points to link 7, points to link6, ... This should error out! */ |
| 311 |
check_chase_error (foo_dfd, "link6/bar", 0, |
| 312 |
G_IO_ERROR, G_IO_ERROR_TOO_MANY_LINKS); |
| 313 |
|
| 314 |
/* Test with links which never go below the dfd */ |
| 315 |
check_chase (AT_FDCWD, "foo/link1/foo/bar", |
| 316 |
GLNX_CHASE_RESOLVE_BENEATH, |
| 317 |
ino); |
| 318 |
check_chase (AT_FDCWD, "foo/link2/foo/bar", |
| 319 |
GLNX_CHASE_RESOLVE_BENEATH, |
| 320 |
ino); |
| 321 |
/* An absolute link is always below the dfd */ |
| 322 |
check_chase_error (AT_FDCWD, "foo/link3/foo/bar", |
| 323 |
GLNX_CHASE_RESOLVE_BENEATH, |
| 324 |
G_IO_ERROR, G_IO_ERROR_FAILED); |
| 325 |
|
| 326 |
/* Same, but from foo instead of cwd */ |
| 327 |
check_chase_error (foo_dfd, "link1/foo/bar", |
| 328 |
GLNX_CHASE_RESOLVE_BENEATH, |
| 329 |
G_IO_ERROR, G_IO_ERROR_FAILED); |
| 330 |
check_chase_error (foo_dfd, "link2/foo/bar", |
| 331 |
GLNX_CHASE_RESOLVE_BENEATH, |
| 332 |
G_IO_ERROR, G_IO_ERROR_FAILED); |
| 333 |
check_chase_error (foo_dfd, "link3/foo/bar", |
| 334 |
GLNX_CHASE_RESOLVE_BENEATH, |
| 335 |
G_IO_ERROR, G_IO_ERROR_FAILED); |
| 336 |
|
| 337 |
/* Check that trying to be below the dfd with RESOLVE_IN_ROOT resolves to the |
| 338 |
* dfd itself */ |
| 339 |
check_chase (foo_dfd, "link1/bar", |
| 340 |
GLNX_CHASE_RESOLVE_IN_ROOT, |
| 341 |
ino); |
| 342 |
check_chase (foo_dfd, "link2/bar", |
| 343 |
GLNX_CHASE_RESOLVE_IN_ROOT, |
| 344 |
ino); |
| 345 |
/* The absolute link is relative to dfd with RESOLVE_IN_ROOT, so this |
| 346 |
* fails... */ |
| 347 |
check_chase_error (foo_dfd, "link3", |
| 348 |
GLNX_CHASE_RESOLVE_IN_ROOT, |
| 349 |
G_IO_ERROR, G_IO_ERROR_NOT_FOUND); |
| 350 |
/* ... but the link /bar resolves correctly from foo as dfd. */ |
| 351 |
check_chase (foo_dfd, "link4", |
| 352 |
GLNX_CHASE_RESOLVE_IN_ROOT, |
| 353 |
ino); |
| 354 |
} |
| 355 |
|
| 356 |
static void |
| 357 |
test_chase_resolve_in_root_absolute (void) |
| 358 |
{ |
| 359 |
g_autoptr(GError) error = NULL; |
| 360 |
glnx_autofd int foo_dfd = -1; |
| 361 |
glnx_autofd int bar_dfd = -1; |
| 362 |
glnx_autofd int baz_dfd = -1; |
| 363 |
|
| 364 |
g_assert_true (glnx_shutil_mkdir_p_at_open (AT_FDCWD, "foo", 0755, |
| 365 |
&foo_dfd, |
| 366 |
NULL, &error)); |
| 367 |
g_assert_no_error (error); |
| 368 |
g_assert_cmpint (foo_dfd, >=, 0); |
| 369 |
|
| 370 |
g_assert_true (glnx_shutil_mkdir_p_at_open (AT_FDCWD, "foo/bar", 0755, |
| 371 |
&bar_dfd, |
| 372 |
NULL, &error)); |
| 373 |
g_assert_no_error (error); |
| 374 |
g_assert_cmpint (bar_dfd, >=, 0); |
| 375 |
|
| 376 |
g_assert_true (glnx_shutil_mkdir_p_at_open (AT_FDCWD, "foo/bar/baz", 0755, |
| 377 |
&baz_dfd, |
| 378 |
NULL, &error)); |
| 379 |
g_assert_no_error (error); |
| 380 |
g_assert_cmpint (baz_dfd, >=, 0); |
| 381 |
|
| 382 |
/* Test the absolute symlink doesn't break tracking of the root level */ |
| 383 |
g_assert_cmpint (symlinkat ("/..", baz_dfd, "link1"), ==, 0); |
| 384 |
|
| 385 |
/* We should not be able to break out of the root! */ |
| 386 |
check_chase (bar_dfd, "./baz/link1", GLNX_CHASE_RESOLVE_IN_ROOT, get_ino (bar_dfd)); |
| 387 |
} |
| 388 |
|
| 389 |
static void |
| 390 |
check_chase_and_statxat (int dfd, |
| 391 |
const char *path, |
| 392 |
GlnxChaseFlags flags, |
| 393 |
ino_t expected_ino, |
| 394 |
mode_t expected_type) |
| 395 |
{ |
| 396 |
g_autoptr(GError) error = NULL; |
| 397 |
glnx_autofd int chase_fd = -1; |
| 398 |
struct glnx_statx stx; |
| 399 |
|
| 400 |
/* let's try to test the openat2 impl */ |
| 401 |
chase_fd = glnx_chase_and_statxat (dfd, path, flags, |
| 402 |
GLNX_STATX_TYPE | GLNX_STATX_INO, |
| 403 |
&stx, &error); |
| 404 |
g_assert_cmpint (chase_fd, >=, 0); |
| 405 |
g_assert_no_error (error); |
| 406 |
g_assert_cmpint (stx.stx_ino, ==, expected_ino); |
| 407 |
g_assert_cmpint (stx.stx_mode & S_IFMT, ==, expected_type); |
| 408 |
g_clear_fd (&chase_fd, NULL); |
| 409 |
|
| 410 |
/* let's try to test the open_tree impl */ |
| 411 |
chase_fd = glnx_chase_and_statxat (dfd, path, |
| 412 |
flags | GLNX_CHASE_DEBUG_NO_OPENAT2, |
| 413 |
GLNX_STATX_TYPE | GLNX_STATX_INO, |
| 414 |
&stx, &error); |
| 415 |
g_assert_cmpint (chase_fd, >=, 0); |
| 416 |
g_assert_no_error (error); |
| 417 |
g_assert_cmpint (stx.stx_ino, ==, expected_ino); |
| 418 |
g_assert_cmpint (stx.stx_mode & S_IFMT, ==, expected_type); |
| 419 |
g_clear_fd (&chase_fd, NULL); |
| 420 |
|
| 421 |
/* let's try to test the openat impl */ |
| 422 |
chase_fd = glnx_chase_and_statxat (dfd, path, |
| 423 |
flags | |
| 424 |
GLNX_CHASE_DEBUG_NO_OPENAT2 | |
| 425 |
GLNX_CHASE_DEBUG_NO_OPEN_TREE, |
| 426 |
GLNX_STATX_TYPE | GLNX_STATX_INO, |
| 427 |
&stx, &error); |
| 428 |
g_assert_cmpint (chase_fd, >=, 0); |
| 429 |
g_assert_no_error (error); |
| 430 |
g_assert_cmpint (stx.stx_ino, ==, expected_ino); |
| 431 |
g_assert_cmpint (stx.stx_mode & S_IFMT, ==, expected_type); |
| 432 |
g_clear_fd (&chase_fd, NULL); |
| 433 |
} |
| 434 |
|
| 435 |
static void |
| 436 |
check_chase_and_statxat_error (int dfd, |
| 437 |
const char *path, |
| 438 |
GlnxChaseFlags flags, |
| 439 |
GQuark err_domain, |
| 440 |
gint err_code) |
| 441 |
{ |
| 442 |
g_autoptr(GError) error = NULL; |
| 443 |
glnx_autofd int chase_fd = -1; |
| 444 |
struct glnx_statx stx; |
| 445 |
|
| 446 |
/* let's try to test the openat2 impl */ |
| 447 |
chase_fd = glnx_chase_and_statxat (dfd, path, flags, |
| 448 |
GLNX_STATX_TYPE | GLNX_STATX_INO, |
| 449 |
&stx, &error); |
| 450 |
g_assert_cmpint (chase_fd, <, 0); |
| 451 |
g_assert_error (error, err_domain, err_code); |
| 452 |
g_clear_error (&error); |
| 453 |
|
| 454 |
/* let's try to test the open_tree impl */ |
| 455 |
chase_fd = glnx_chase_and_statxat (dfd, path, |
| 456 |
flags | GLNX_CHASE_DEBUG_NO_OPENAT2, |
| 457 |
GLNX_STATX_TYPE | GLNX_STATX_INO, |
| 458 |
&stx, &error); |
| 459 |
g_assert_cmpint (chase_fd, <, 0); |
| 460 |
g_assert_error (error, err_domain, err_code); |
| 461 |
g_clear_error (&error); |
| 462 |
|
| 463 |
/* let's try to test the openat impl */ |
| 464 |
chase_fd = glnx_chase_and_statxat (dfd, path, |
| 465 |
flags | |
| 466 |
GLNX_CHASE_DEBUG_NO_OPENAT2 | |
| 467 |
GLNX_CHASE_DEBUG_NO_OPEN_TREE, |
| 468 |
GLNX_STATX_TYPE | GLNX_STATX_INO, |
| 469 |
&stx, &error); |
| 470 |
g_assert_cmpint (chase_fd, <, 0); |
| 471 |
g_assert_error (error, err_domain, err_code); |
| 472 |
g_clear_error (&error); |
| 473 |
} |
| 474 |
|
| 475 |
static void |
| 476 |
test_chase_and_statxat_basic (void) |
| 477 |
{ |
| 478 |
g_autoptr(GError) error = NULL; |
| 479 |
glnx_autofd int dfd = -1; |
| 480 |
glnx_autofd int file_fd = -1; |
| 481 |
ino_t expected_ino; |
| 482 |
|
| 483 |
g_assert_true (glnx_shutil_mkdir_p_at_open (AT_FDCWD, "file/baz", 0755, |
| 484 |
&dfd, |
| 485 |
NULL, &error)); |
| 486 |
g_assert_no_error (error); |
| 487 |
g_assert_cmpint (dfd, >=, 0); |
| 488 |
|
| 489 |
expected_ino = get_ino (dfd); |
| 490 |
|
| 491 |
/* Test with various path forms */ |
| 492 |
for (size_t i = 0; i < G_N_ELEMENTS (test_paths); i++) |
| 493 |
check_chase_and_statxat (AT_FDCWD, test_paths[i], 0, expected_ino, S_IFDIR); |
| 494 |
|
| 495 |
/* Create a regular file and test it */ |
| 496 |
file_fd = openat (dfd, "testfile", O_WRONLY | O_CREAT | O_CLOEXEC, 0644); |
| 497 |
g_assert_cmpint (file_fd, >=, 0); |
| 498 |
g_clear_fd (&file_fd, NULL); |
| 499 |
|
| 500 |
expected_ino = path_get_ino ("file/baz/testfile"); |
| 501 |
check_chase_and_statxat (AT_FDCWD, "file/baz/testfile", 0, expected_ino, S_IFREG); |
| 502 |
|
| 503 |
/* Test error cases */ |
| 504 |
check_chase_and_statxat_error (AT_FDCWD, "nope", 0, G_IO_ERROR, G_IO_ERROR_NOT_FOUND); |
| 505 |
} |
| 506 |
|
| 507 |
static void |
| 508 |
test_chase_and_statxat_symlink (void) |
| 509 |
{ |
| 510 |
g_autoptr(GError) error = NULL; |
| 511 |
glnx_autofd int dfd = -1; |
| 512 |
glnx_autofd int chase_fd = -1; |
| 513 |
ino_t link_ino; |
| 514 |
ino_t target_ino; |
| 515 |
struct glnx_statx stx; |
| 516 |
|
| 517 |
g_assert_true (glnx_shutil_mkdir_p_at_open (AT_FDCWD, "file/baz", 0755, |
| 518 |
&dfd, |
| 519 |
NULL, &error)); |
| 520 |
g_assert_no_error (error); |
| 521 |
g_assert_cmpint (dfd, >=, 0); |
| 522 |
|
| 523 |
g_assert_cmpint (symlinkat ("file/baz", AT_FDCWD, "fstatlink"), ==, 0); |
| 524 |
|
| 525 |
target_ino = get_ino (dfd); |
| 526 |
link_ino = path_get_ino ("fstatlink"); |
| 527 |
|
| 528 |
/* Following symlinks should give us the directory */ |
| 529 |
check_chase_and_statxat (AT_FDCWD, "fstatlink", 0, target_ino, S_IFDIR); |
| 530 |
check_chase_and_statxat (AT_FDCWD, "fstatlink/", 0, target_ino, S_IFDIR); |
| 531 |
|
| 532 |
/* With NOFOLLOW, we should get the symlink itself */ |
| 533 |
check_chase_and_statxat (AT_FDCWD, "fstatlink", GLNX_CHASE_NOFOLLOW, link_ino, S_IFLNK); |
| 534 |
|
| 535 |
/* Verify we can distinguish between regular files, directories, and symlinks */ |
| 536 |
chase_fd = glnx_chase_and_statxat (AT_FDCWD, "fstatlink", GLNX_CHASE_NOFOLLOW, |
| 537 |
GLNX_STATX_TYPE | GLNX_STATX_INO, |
| 538 |
&stx, &error); |
| 539 |
g_assert_cmpint (chase_fd, >=, 0); |
| 540 |
g_assert_no_error (error); |
| 541 |
g_assert_true (S_ISLNK (stx.stx_mode)); |
| 542 |
g_clear_fd (&chase_fd, NULL); |
| 543 |
|
| 544 |
chase_fd = glnx_chase_and_statxat (AT_FDCWD, "fstatlink", 0, |
| 545 |
GLNX_STATX_TYPE | GLNX_STATX_INO, |
| 546 |
&stx, &error); |
| 547 |
g_assert_cmpint (chase_fd, >=, 0); |
| 548 |
g_assert_no_error (error); |
| 549 |
g_assert_true (S_ISDIR (stx.stx_mode)); |
| 550 |
g_clear_fd (&chase_fd, NULL); |
| 551 |
|
| 552 |
/* Test with RESOLVE_NO_SYMLINKS */ |
| 553 |
check_chase_and_statxat_error (AT_FDCWD, "fstatlink", |
| 554 |
GLNX_CHASE_RESOLVE_NO_SYMLINKS, |
| 555 |
G_IO_ERROR, G_IO_ERROR_TOO_MANY_LINKS); |
| 556 |
} |
| 557 |
|
| 558 |
static void |
| 559 |
test_chase_and_statxat_permissions (void) |
| 560 |
{ |
| 561 |
g_autoptr(GError) error = NULL; |
| 562 |
glnx_autofd int dfd = -1; |
| 563 |
glnx_autofd int file_fd = -1; |
| 564 |
glnx_autofd int chase_fd = -1; |
| 565 |
struct glnx_statx stx; |
| 566 |
mode_t expected_mode = 0640; |
| 567 |
|
| 568 |
g_assert_true (glnx_shutil_mkdir_p_at_open (AT_FDCWD, "permtest", 0755, |
| 569 |
&dfd, |
| 570 |
NULL, &error)); |
| 571 |
g_assert_no_error (error); |
| 572 |
|
| 573 |
/* Create a file with specific permissions */ |
| 574 |
file_fd = openat (dfd, "testfile", O_WRONLY | O_CREAT | O_CLOEXEC, expected_mode); |
| 575 |
g_assert_cmpint (file_fd, >=, 0); |
| 576 |
g_clear_fd (&file_fd, NULL); |
| 577 |
|
| 578 |
/* Verify that glnx_chase_and_statxat returns the correct permissions */ |
| 579 |
chase_fd = glnx_chase_and_statxat (dfd, "testfile", 0, |
| 580 |
GLNX_STATX_TYPE | GLNX_STATX_MODE, |
| 581 |
&stx, &error); |
| 582 |
g_assert_cmpint (chase_fd, >=, 0); |
| 583 |
g_assert_no_error (error); |
| 584 |
g_assert_cmpint (stx.stx_mode & 0777, ==, expected_mode); |
| 585 |
g_assert_true (S_ISREG (stx.stx_mode)); |
| 586 |
g_clear_fd (&chase_fd, NULL); |
| 587 |
} |
| 588 |
|
| 589 |
int main (int argc, char **argv) |
| 590 |
{ |
| 591 |
_GLNX_TEST_SCOPED_TEMP_DIR; |
| 592 |
int ret; |
| 593 |
|
| 594 |
g_test_init (&argc, &argv, NULL); |
| 595 |
|
| 596 |
g_test_add_func ("/chase-relative", test_chase_relative); |
| 597 |
g_test_add_func ("/chase-relative-fd", test_chase_relative_fd); |
| 598 |
g_test_add_func ("/chase-absolute", test_chase_absolute); |
| 599 |
g_test_add_func ("/chase-link", test_chase_link); |
| 600 |
g_test_add_func ("/chase-resolve", test_chase_resolve); |
| 601 |
g_test_add_func ("/chase-resolve-in-root-absolute", test_chase_resolve_in_root_absolute); |
| 602 |
g_test_add_func ("/chase-and-statxat-basic", test_chase_and_statxat_basic); |
| 603 |
g_test_add_func ("/chase-and-statxat-symlink", test_chase_and_statxat_symlink); |
| 604 |
g_test_add_func ("/chase-and-statxat-permissions", test_chase_and_statxat_permissions); |
| 605 |
|
| 606 |
ret = g_test_run(); |
| 607 |
|
| 608 |
return ret; |
| 609 |
} |