| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
|
| 5 |
#include "af-list.h" |
| 6 |
#include "alloc-util.h" |
| 7 |
#include "dlfcn-util.h" |
| 8 |
#include "env-util.h" |
| 9 |
#include "errno-list.h" |
| 10 |
#include "format-ifname.h" |
| 11 |
#include "hashmap.h" |
| 12 |
#include "hexdecoct.h" |
| 13 |
#include "hostname-setup.h" |
| 14 |
#include "in-addr-util.h" |
| 15 |
#include "local-addresses.h" |
| 16 |
#include "log.h" |
| 17 |
#include "main-func.h" |
| 18 |
#include "nss-test-util.h" |
| 19 |
#include "nss-util.h" |
| 20 |
#include "parse-util.h" |
| 21 |
#include "path-util.h" |
| 22 |
#include "process-util.h" |
| 23 |
#include "seccomp-util.h" |
| 24 |
#include "socket-util.h" |
| 25 |
#include "string-util.h" |
| 26 |
#include "strv.h" |
| 27 |
#include "tests.h" |
| 28 |
|
| 29 |
static size_t arg_bufsize = 1024; |
| 30 |
|
| 31 |
static const char* af_to_string(int family, char *buf, size_t buf_len) { |
| 32 |
const char *name; |
| 33 |
|
| 34 |
if (family == AF_UNSPEC) |
| 35 |
return "*"; |
| 36 |
|
| 37 |
name = af_to_name(family); |
| 38 |
if (name) |
| 39 |
return name; |
| 40 |
|
| 41 |
(void) snprintf(buf, buf_len, "%i", family); |
| 42 |
return buf; |
| 43 |
} |
| 44 |
|
| 45 |
static int print_gaih_addrtuples(const struct gaih_addrtuple *tuples) { |
| 46 |
int r, n = 0; |
| 47 |
|
| 48 |
for (const struct gaih_addrtuple *it = tuples; it; it = it->next) { |
| 49 |
_cleanup_free_ char *a = NULL; |
| 50 |
union in_addr_union u; |
| 51 |
char family_name[DECIMAL_STR_MAX(int)]; |
| 52 |
|
| 53 |
memcpy(&u, it->addr, 16); |
| 54 |
r = in_addr_to_string(it->family, &u, &a); |
| 55 |
assert_se(IN_SET(r, 0, -EAFNOSUPPORT)); |
| 56 |
if (r == -EAFNOSUPPORT) |
| 57 |
assert_se(a = hexmem(it->addr, 16)); |
| 58 |
|
| 59 |
log_info(" \"%s\" %s %s %s", |
| 60 |
it->name, |
| 61 |
af_to_string(it->family, family_name, sizeof family_name), |
| 62 |
a, |
| 63 |
FORMAT_IFNAME_FULL(it->scopeid, FORMAT_IFNAME_IFINDEX_WITH_PERCENT)); |
| 64 |
|
| 65 |
n++; |
| 66 |
} |
| 67 |
return n; |
| 68 |
} |
| 69 |
|
| 70 |
static void print_struct_hostent(struct hostent *host, const char *canon) { |
| 71 |
log_info(" \"%s\"", host->h_name); |
| 72 |
STRV_FOREACH(s, host->h_aliases) |
| 73 |
log_info(" alias \"%s\"", *s); |
| 74 |
STRV_FOREACH(s, host->h_addr_list) { |
| 75 |
union in_addr_union u; |
| 76 |
_cleanup_free_ char *a = NULL; |
| 77 |
char family_name[DECIMAL_STR_MAX(int)]; |
| 78 |
int r; |
| 79 |
|
| 80 |
assert_se((unsigned) host->h_length == FAMILY_ADDRESS_SIZE(host->h_addrtype)); |
| 81 |
memcpy(&u, *s, host->h_length); |
| 82 |
r = in_addr_to_string(host->h_addrtype, &u, &a); |
| 83 |
assert_se(r == 0); |
| 84 |
log_info(" %s %s", |
| 85 |
af_to_string(host->h_addrtype, family_name, sizeof family_name), |
| 86 |
a); |
| 87 |
} |
| 88 |
if (canon) |
| 89 |
log_info(" canonical: \"%s\"", canon); |
| 90 |
} |
| 91 |
|
| 92 |
static void test_gethostbyname4_r(void *handle, const char *module, const char *name) { |
| 93 |
const char *fname; |
| 94 |
_nss_gethostbyname4_r_t f; |
| 95 |
char buffer[arg_bufsize]; |
| 96 |
struct gaih_addrtuple *pat = NULL; |
| 97 |
int errno1 = 999, errno2 = 999; /* nss-dns doesn't set those */ |
| 98 |
int32_t ttl = INT32_MAX; /* nss-dns wants to return the lowest ttl, |
| 99 |
and will access this variable through *ttlp, |
| 100 |
so we need to set it to something. |
| 101 |
I'm not sure if this is a bug in nss-dns |
| 102 |
or not. */ |
| 103 |
enum nss_status status; |
| 104 |
char pretty_status[DECIMAL_STR_MAX(enum nss_status)]; |
| 105 |
int n; |
| 106 |
|
| 107 |
fname = strjoina("_nss_", module, "_gethostbyname4_r"); |
| 108 |
f = dlsym(handle, fname); |
| 109 |
log_debug("dlsym(0x%p, %s) → 0x%p", handle, fname, f); |
| 110 |
if (!f) { |
| 111 |
log_info("%s not defined", fname); |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
status = f(name, &pat, buffer, sizeof buffer, &errno1, &errno2, &ttl); |
| 116 |
if (status == NSS_STATUS_SUCCESS) { |
| 117 |
log_info("%s(\"%s\") → status=%s%-20spat=buffer+0x%"PRIxPTR" errno=%d/%s h_errno=%d/%s ttl=%"PRIi32, |
| 118 |
fname, name, |
| 119 |
nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n", |
| 120 |
pat ? (uintptr_t) pat - (uintptr_t) buffer : 0, |
| 121 |
errno1, errno1 > 0 ? ERRNO_NAME(errno1) : "---", |
| 122 |
errno2, hstrerror(errno2), |
| 123 |
ttl); |
| 124 |
n = print_gaih_addrtuples(pat); |
| 125 |
} else { |
| 126 |
log_info("%s(\"%s\") → status=%s%-20spat=0x%p errno=%d/%s h_errno=%d/%s", |
| 127 |
fname, name, |
| 128 |
nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n", |
| 129 |
pat, |
| 130 |
errno1, errno1 > 0 ? ERRNO_NAME(errno1) : "---", |
| 131 |
errno2, hstrerror(errno2)); |
| 132 |
n = 0; |
| 133 |
} |
| 134 |
|
| 135 |
if (STR_IN_SET(module, "resolve", "mymachines") && status == NSS_STATUS_UNAVAIL) |
| 136 |
return; |
| 137 |
|
| 138 |
if (streq(name, "localhost")) { |
| 139 |
if (streq(module, "myhostname")) { |
| 140 |
assert_se(status == NSS_STATUS_SUCCESS); |
| 141 |
assert_se(n == socket_ipv6_is_enabled() + 1); |
| 142 |
|
| 143 |
} else if (streq(module, "resolve") && secure_getenv_bool("SYSTEMD_NSS_RESOLVE_SYNTHESIZE") != 0) { |
| 144 |
assert_se(status == NSS_STATUS_SUCCESS); |
| 145 |
if (socket_ipv6_is_enabled()) |
| 146 |
assert_se(n == 2); |
| 147 |
else |
| 148 |
assert_se(n <= 2); /* Even if IPv6 is disabled, /etc/hosts may contain ::1. */ |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
static void test_gethostbyname3_r(void *handle, const char *module, const char *name, int af) { |
| 154 |
const char *fname; |
| 155 |
_nss_gethostbyname3_r_t f; |
| 156 |
char buffer[arg_bufsize]; |
| 157 |
int errno1 = 999, errno2 = 999; /* nss-dns doesn't set those */ |
| 158 |
int32_t ttl = INT32_MAX; /* nss-dns wants to return the lowest ttl, |
| 159 |
and will access this variable through *ttlp, |
| 160 |
so we need to set it to something. |
| 161 |
I'm not sure if this is a bug in nss-dns |
| 162 |
or not. */ |
| 163 |
enum nss_status status; |
| 164 |
char pretty_status[DECIMAL_STR_MAX(enum nss_status)]; |
| 165 |
struct hostent host; |
| 166 |
char *canon; |
| 167 |
char family_name[DECIMAL_STR_MAX(int)]; |
| 168 |
|
| 169 |
fname = strjoina("_nss_", module, "_gethostbyname3_r"); |
| 170 |
f = dlsym(handle, fname); |
| 171 |
log_debug("dlsym(0x%p, %s) → 0x%p", handle, fname, f); |
| 172 |
if (!f) { |
| 173 |
log_info("%s not defined", fname); |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
status = f(name, af, &host, buffer, sizeof buffer, &errno1, &errno2, &ttl, &canon); |
| 178 |
log_info("%s(\"%s\", %s) → status=%s%-20serrno=%d/%s h_errno=%d/%s ttl=%"PRIi32, |
| 179 |
fname, name, af_to_string(af, family_name, sizeof family_name), |
| 180 |
nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n", |
| 181 |
errno1, errno1 > 0 ? ERRNO_NAME(errno1) : "---", |
| 182 |
errno2, hstrerror(errno2), |
| 183 |
ttl); |
| 184 |
if (status == NSS_STATUS_SUCCESS) |
| 185 |
print_struct_hostent(&host, canon); |
| 186 |
} |
| 187 |
|
| 188 |
static void test_gethostbyname2_r(void *handle, const char *module, const char *name, int af) { |
| 189 |
const char *fname; |
| 190 |
_nss_gethostbyname2_r_t f; |
| 191 |
char buffer[arg_bufsize]; |
| 192 |
int errno1 = 999, errno2 = 999; /* nss-dns doesn't set those */ |
| 193 |
enum nss_status status; |
| 194 |
char pretty_status[DECIMAL_STR_MAX(enum nss_status)]; |
| 195 |
struct hostent host; |
| 196 |
char family_name[DECIMAL_STR_MAX(int)]; |
| 197 |
|
| 198 |
fname = strjoina("_nss_", module, "_gethostbyname2_r"); |
| 199 |
f = dlsym(handle, fname); |
| 200 |
log_debug("dlsym(0x%p, %s) → 0x%p", handle, fname, f); |
| 201 |
if (!f) { |
| 202 |
log_info("%s not defined", fname); |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
status = f(name, af, &host, buffer, sizeof buffer, &errno1, &errno2); |
| 207 |
log_info("%s(\"%s\", %s) → status=%s%-20serrno=%d/%s h_errno=%d/%s", |
| 208 |
fname, name, af_to_string(af, family_name, sizeof family_name), |
| 209 |
nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n", |
| 210 |
errno1, errno1 > 0 ? ERRNO_NAME(errno1) : "---", |
| 211 |
errno2, hstrerror(errno2)); |
| 212 |
if (status == NSS_STATUS_SUCCESS) |
| 213 |
print_struct_hostent(&host, NULL); |
| 214 |
} |
| 215 |
|
| 216 |
static void test_gethostbyname_r(void *handle, const char *module, const char *name) { |
| 217 |
const char *fname; |
| 218 |
_nss_gethostbyname_r_t f; |
| 219 |
char buffer[arg_bufsize]; |
| 220 |
int errno1 = 999, errno2 = 999; /* nss-dns doesn't set those */ |
| 221 |
enum nss_status status; |
| 222 |
char pretty_status[DECIMAL_STR_MAX(enum nss_status)]; |
| 223 |
struct hostent host; |
| 224 |
|
| 225 |
fname = strjoina("_nss_", module, "_gethostbyname_r"); |
| 226 |
f = dlsym(handle, fname); |
| 227 |
log_debug("dlsym(0x%p, %s) → 0x%p", handle, fname, f); |
| 228 |
if (!f) { |
| 229 |
log_info("%s not defined", fname); |
| 230 |
return; |
| 231 |
} |
| 232 |
|
| 233 |
status = f(name, &host, buffer, sizeof buffer, &errno1, &errno2); |
| 234 |
log_info("%s(\"%s\") → status=%s%-20serrno=%d/%s h_errno=%d/%s", |
| 235 |
fname, name, |
| 236 |
nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n", |
| 237 |
errno1, errno1 > 0 ? ERRNO_NAME(errno1) : "---", |
| 238 |
errno2, hstrerror(errno2)); |
| 239 |
if (status == NSS_STATUS_SUCCESS) |
| 240 |
print_struct_hostent(&host, NULL); |
| 241 |
} |
| 242 |
|
| 243 |
static void test_gethostbyaddr2_r(void *handle, |
| 244 |
const char *module, |
| 245 |
const void* addr, socklen_t len, |
| 246 |
int af) { |
| 247 |
|
| 248 |
const char *fname; |
| 249 |
_nss_gethostbyaddr2_r_t f; |
| 250 |
char buffer[arg_bufsize]; |
| 251 |
int errno1 = 999, errno2 = 999; /* nss-dns doesn't set those */ |
| 252 |
enum nss_status status; |
| 253 |
char pretty_status[DECIMAL_STR_MAX(enum nss_status)]; |
| 254 |
struct hostent host; |
| 255 |
int32_t ttl = INT32_MAX; |
| 256 |
_cleanup_free_ char *addr_pretty = NULL; |
| 257 |
|
| 258 |
fname = strjoina("_nss_", module, "_gethostbyaddr2_r"); |
| 259 |
f = dlsym(handle, fname); |
| 260 |
|
| 261 |
log_full_errno(f ? LOG_DEBUG : LOG_INFO, errno, |
| 262 |
"dlsym(0x%p, %s) → 0x%p: %m", handle, fname, f); |
| 263 |
if (!f) { |
| 264 |
log_info("%s not defined", fname); |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
assert_se(in_addr_to_string(af, addr, &addr_pretty) >= 0); |
| 269 |
|
| 270 |
status = f(addr, len, af, &host, buffer, sizeof buffer, &errno1, &errno2, &ttl); |
| 271 |
log_info("%s(\"%s\") → status=%s%-20serrno=%d/%s h_errno=%d/%s ttl=%"PRIi32, |
| 272 |
fname, addr_pretty, |
| 273 |
nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n", |
| 274 |
errno1, errno1 > 0 ? ERRNO_NAME(errno1) : "---", |
| 275 |
errno2, hstrerror(errno2), |
| 276 |
ttl); |
| 277 |
if (status == NSS_STATUS_SUCCESS) |
| 278 |
print_struct_hostent(&host, NULL); |
| 279 |
} |
| 280 |
|
| 281 |
static void test_gethostbyaddr_r(void *handle, |
| 282 |
const char *module, |
| 283 |
const void* addr, socklen_t len, |
| 284 |
int af) { |
| 285 |
|
| 286 |
const char *fname; |
| 287 |
_nss_gethostbyaddr_r_t f; |
| 288 |
char buffer[arg_bufsize]; |
| 289 |
int errno1 = 999, errno2 = 999; /* nss-dns doesn't set those */ |
| 290 |
enum nss_status status; |
| 291 |
char pretty_status[DECIMAL_STR_MAX(enum nss_status)]; |
| 292 |
struct hostent host; |
| 293 |
_cleanup_free_ char *addr_pretty = NULL; |
| 294 |
|
| 295 |
fname = strjoina("_nss_", module, "_gethostbyaddr_r"); |
| 296 |
f = dlsym(handle, fname); |
| 297 |
|
| 298 |
log_full_errno(f ? LOG_DEBUG : LOG_INFO, errno, |
| 299 |
"dlsym(0x%p, %s) → 0x%p: %m", handle, fname, f); |
| 300 |
if (!f) { |
| 301 |
log_info("%s not defined", fname); |
| 302 |
return; |
| 303 |
} |
| 304 |
|
| 305 |
assert_se(in_addr_to_string(af, addr, &addr_pretty) >= 0); |
| 306 |
|
| 307 |
status = f(addr, len, af, &host, buffer, sizeof buffer, &errno1, &errno2); |
| 308 |
log_info("%s(\"%s\") → status=%s%-20serrno=%d/%s h_errno=%d/%s", |
| 309 |
fname, addr_pretty, |
| 310 |
nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n", |
| 311 |
errno1, errno1 > 0 ? ERRNO_NAME(errno1) : "---", |
| 312 |
errno2, hstrerror(errno2)); |
| 313 |
if (status == NSS_STATUS_SUCCESS) |
| 314 |
print_struct_hostent(&host, NULL); |
| 315 |
} |
| 316 |
|
| 317 |
static void test_byname(void *handle, const char *module, const char *name) { |
| 318 |
test_gethostbyname4_r(handle, module, name); |
| 319 |
puts(""); |
| 320 |
|
| 321 |
test_gethostbyname3_r(handle, module, name, AF_INET); |
| 322 |
puts(""); |
| 323 |
test_gethostbyname3_r(handle, module, name, AF_INET6); |
| 324 |
puts(""); |
| 325 |
test_gethostbyname3_r(handle, module, name, AF_UNSPEC); |
| 326 |
puts(""); |
| 327 |
test_gethostbyname3_r(handle, module, name, AF_UNIX); |
| 328 |
puts(""); |
| 329 |
|
| 330 |
test_gethostbyname2_r(handle, module, name, AF_INET); |
| 331 |
puts(""); |
| 332 |
test_gethostbyname2_r(handle, module, name, AF_INET6); |
| 333 |
puts(""); |
| 334 |
test_gethostbyname2_r(handle, module, name, AF_UNSPEC); |
| 335 |
puts(""); |
| 336 |
test_gethostbyname2_r(handle, module, name, AF_UNIX); |
| 337 |
puts(""); |
| 338 |
|
| 339 |
test_gethostbyname_r(handle, module, name); |
| 340 |
puts(""); |
| 341 |
} |
| 342 |
|
| 343 |
static void test_byaddr(void *handle, |
| 344 |
const char *module, |
| 345 |
const void* addr, socklen_t len, |
| 346 |
int af) { |
| 347 |
test_gethostbyaddr2_r(handle, module, addr, len, af); |
| 348 |
puts(""); |
| 349 |
|
| 350 |
test_gethostbyaddr_r(handle, module, addr, len, af); |
| 351 |
puts(""); |
| 352 |
} |
| 353 |
|
| 354 |
static int make_addresses(struct local_address **addresses) { |
| 355 |
int n; |
| 356 |
_cleanup_free_ struct local_address *addrs = NULL; |
| 357 |
|
| 358 |
n = local_addresses(NULL, 0, AF_UNSPEC, &addrs); |
| 359 |
if (n < 0) |
| 360 |
log_info_errno(n, "Failed to query local addresses: %m"); |
| 361 |
|
| 362 |
assert_se(GREEDY_REALLOC(addrs, n + 3)); |
| 363 |
|
| 364 |
addrs[n++] = (struct local_address) { .family = AF_INET, |
| 365 |
.address.in = { htobe32(0x7F000001) } }; |
| 366 |
addrs[n++] = (struct local_address) { .family = AF_INET, |
| 367 |
.address.in = { htobe32(0x7F000002) } }; |
| 368 |
addrs[n++] = (struct local_address) { .family = AF_INET6, |
| 369 |
.address.in6 = in6addr_loopback }; |
| 370 |
|
| 371 |
*addresses = TAKE_PTR(addrs); |
| 372 |
return n; |
| 373 |
} |
| 374 |
|
| 375 |
static int test_one_module(const char *dir, |
| 376 |
const char *module, |
| 377 |
char **names, |
| 378 |
struct local_address *addresses, |
| 379 |
int n_addresses) { |
| 380 |
|
| 381 |
log_info("======== %s ========", module); |
| 382 |
|
| 383 |
_cleanup_(dlclosep) void *handle = nss_open_handle(dir, module, RTLD_NOW|RTLD_NODELETE); |
| 384 |
if (!handle) |
| 385 |
return -EINVAL; |
| 386 |
|
| 387 |
STRV_FOREACH(name, names) |
| 388 |
test_byname(handle, module, *name); |
| 389 |
|
| 390 |
for (int i = 0; i < n_addresses; i++) |
| 391 |
test_byaddr(handle, module, |
| 392 |
&addresses[i].address, |
| 393 |
FAMILY_ADDRESS_SIZE(addresses[i].family), |
| 394 |
addresses[i].family); |
| 395 |
|
| 396 |
log_info(" "); |
| 397 |
return 0; |
| 398 |
} |
| 399 |
|
| 400 |
static int parse_argv(int argc, char **argv, |
| 401 |
char ***the_modules, |
| 402 |
char ***the_names, |
| 403 |
struct local_address **the_addresses, int *n_addresses) { |
| 404 |
|
| 405 |
_cleanup_strv_free_ char **modules = NULL, **names = NULL; |
| 406 |
_cleanup_free_ struct local_address *addrs = NULL; |
| 407 |
const char *p; |
| 408 |
int r, n = 0; |
| 409 |
|
| 410 |
p = getenv("SYSTEMD_TEST_NSS_BUFSIZE"); |
| 411 |
if (p) { |
| 412 |
r = safe_atozu(p, &arg_bufsize); |
| 413 |
if (r < 0) |
| 414 |
return log_error_errno(r, "Failed to parse $SYSTEMD_TEST_NSS_BUFSIZE"); |
| 415 |
} |
| 416 |
|
| 417 |
if (argc > 1) |
| 418 |
modules = strv_new(argv[1]); |
| 419 |
else |
| 420 |
modules = strv_new( |
| 421 |
#if ENABLE_NSS_MYHOSTNAME |
| 422 |
"myhostname", |
| 423 |
#endif |
| 424 |
#if ENABLE_NSS_RESOLVE |
| 425 |
"resolve", |
| 426 |
#endif |
| 427 |
#if ENABLE_NSS_MYMACHINES |
| 428 |
"mymachines", |
| 429 |
#endif |
| 430 |
NULL); |
| 431 |
assert_se(modules); |
| 432 |
|
| 433 |
if (argc > 2) { |
| 434 |
int family; |
| 435 |
union in_addr_union address; |
| 436 |
|
| 437 |
STRV_FOREACH(name, argv + 2) { |
| 438 |
r = in_addr_from_string_auto(*name, &family, &address); |
| 439 |
if (r < 0) { |
| 440 |
/* assume this is a name */ |
| 441 |
r = strv_extend(&names, *name); |
| 442 |
if (r < 0) |
| 443 |
return r; |
| 444 |
} else { |
| 445 |
assert_se(GREEDY_REALLOC0(addrs, n + 1)); |
| 446 |
|
| 447 |
addrs[n++] = (struct local_address) { .family = family, |
| 448 |
.address = address }; |
| 449 |
} |
| 450 |
} |
| 451 |
} else { |
| 452 |
_cleanup_free_ char *hostname = NULL; |
| 453 |
assert_se(hostname = gethostname_malloc()); |
| 454 |
assert_se(names = strv_new("localhost", |
| 455 |
"_gateway", |
| 456 |
"_outbound", |
| 457 |
hostname, |
| 458 |
slow_tests_enabled() ? "foo_no_such_host" : NULL)); |
| 459 |
|
| 460 |
n = make_addresses(&addrs); |
| 461 |
assert_se(n >= 0); |
| 462 |
} |
| 463 |
|
| 464 |
*the_modules = TAKE_PTR(modules); |
| 465 |
*the_names = TAKE_PTR(names); |
| 466 |
*the_addresses = TAKE_PTR(addrs); |
| 467 |
*n_addresses = n; |
| 468 |
return 0; |
| 469 |
} |
| 470 |
|
| 471 |
static int run(int argc, char **argv) { |
| 472 |
_cleanup_free_ char *dir = NULL; |
| 473 |
_cleanup_strv_free_ char **modules = NULL, **names = NULL; |
| 474 |
_cleanup_free_ struct local_address *addresses = NULL; |
| 475 |
int n_addresses = 0; |
| 476 |
int r; |
| 477 |
|
| 478 |
test_setup_logging(LOG_DEBUG); |
| 479 |
|
| 480 |
r = parse_argv(argc, argv, &modules, &names, &addresses, &n_addresses); |
| 481 |
if (r < 0) |
| 482 |
return log_error_errno(r, "Failed to parse arguments: %m"); |
| 483 |
|
| 484 |
assert_se(path_extract_directory(argv[0], &dir) >= 0); |
| 485 |
|
| 486 |
#if HAVE_SECCOMP |
| 487 |
if (geteuid() != 0 || !is_seccomp_available()) |
| 488 |
log_tests_skipped("Not privileged or seccomp is not available"); |
| 489 |
else { |
| 490 |
/* Testing with several syscalls filtered, and check if the nss modules gracefully handle failures in |
| 491 |
* masked syscalls. See issue #38582. */ |
| 492 |
|
| 493 |
r = ASSERT_OK(pidref_safe_fork("(with-seccomp)", FORK_LOG|FORK_WAIT, /* ret= */ NULL)); |
| 494 |
if (r == 0) { |
| 495 |
_cleanup_hashmap_free_ Hashmap *filter = NULL; |
| 496 |
ASSERT_NOT_NULL(filter = hashmap_new(NULL)); |
| 497 |
FOREACH_STRING(s, "uname", "olduname", "oldolduname", "sigprocmask", "rt_sigprocmask", "osf_sigprocmask") |
| 498 |
ASSERT_OK(seccomp_filter_set_add_by_name(filter, /* add= */ true, s)); |
| 499 |
ASSERT_OK(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, filter, SCMP_ACT_ERRNO(ENOSYS), /* log_missing= */ true)); |
| 500 |
|
| 501 |
/* To make assert_return() and friends not call abort(), even built as developer mode. */ |
| 502 |
ASSERT_OK_ERRNO(setenv("SYSTEMD_ASSERT_RETURN_IS_CRITICAL", "0", /* overwrite= */ true)); |
| 503 |
/* Let's also make nss modules output debugging logs. */ |
| 504 |
ASSERT_OK_ERRNO(setenv("SYSTEMD_LOG_LEVEL", "debug", /* overwrite= */ true)); |
| 505 |
|
| 506 |
STRV_FOREACH(module, modules) |
| 507 |
ASSERT_OK(test_one_module(dir, *module, names, addresses, n_addresses)); |
| 508 |
|
| 509 |
_exit(EXIT_SUCCESS); |
| 510 |
} |
| 511 |
} |
| 512 |
#endif |
| 513 |
|
| 514 |
STRV_FOREACH(module, modules) { |
| 515 |
r = test_one_module(dir, *module, names, addresses, n_addresses); |
| 516 |
if (r < 0) |
| 517 |
return r; |
| 518 |
} |
| 519 |
|
| 520 |
return 0; |
| 521 |
} |
| 522 |
|
| 523 |
DEFINE_MAIN_FUNCTION(run); |