1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
|
/*
* Copyright (C) 2018 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#ifdef WITH_SELINUX
# include <selinux/selinux.h>
# include <selinux/label.h>
#endif
#include "virmock.h"
#include "virfile.h"
#include "virthread.h"
#include "virhash.h"
#include "qemusecuritytest.h"
#include "security/security_manager.h"
#include "virhostuptime.h"
#define VIR_FROM_THIS VIR_FROM_NONE
/* Okay, here's the deal. The qemusecuritytest calls several
* virSecurityManager public APIs in order to check if XATTRs
* work as expected. Therefore there is a lot we have to mock
* (chown, stat, XATTR APIs, etc.). Since the test won't run as
* root chown() would fail, therefore we have to keep everything
* in memory. By default, all files are owned by 1:2 and have a
* SELinux label.
* By the way, since there are some cases where real stat needs
* to be called, the mocked functions are effective only if
* $ENVVAR is set.
*/
#define DEFAULT_UID 1
#define DEFAULT_GID 2
#define DEFAULT_SELINUX_LABEL "system_u:object_r:default_t:s0"
static int (*real_chown)(const char *path, uid_t uid, gid_t gid);
static int (*real_open)(const char *path, int flags, ...);
#if WITH___OPEN_2
static int (*real___open_2)(const char *path, int flags);
#endif
static int (*real_close)(int fd);
#ifdef WITH_SELINUX
static int (*real_setfilecon_raw)(const char *path, const char *context);
static int (*real_getfilecon_raw)(const char *path, char **context);
#endif
static bool (*real_virFileExists)(const char *file);
/* Global mutex to avoid races */
virMutex m = VIR_MUTEX_INITIALIZER;
/* Hash table to store XATTRs for paths. For simplicity, key is
* "$path:$name" and value is just XATTR "$value". We don't need
* to list XATTRs a path has, therefore we don't need something
* more clever. */
GHashTable *xattr_paths = NULL;
/* The UID:GID is stored in a hash table. Again, for simplicity,
* the path is the key and the value is an uint32_t , where
* the lower half is UID and the higher is GID. */
GHashTable *chown_paths = NULL;
/* The SELinux label is stored in a hash table. For simplicity,
* the path is the key and the value is the label. */
GHashTable *selinux_paths = NULL;
static void
init_hash(void)
{
/* The reason the init is split is that virHash calls
* virRandomBits() which in turn calls a gnutls function.
* However, when gnutls is initializing itself it calls
* stat() so we would call a gnutls function before it is
* initialized which will lead to a crash.
*/
if (xattr_paths)
return;
xattr_paths = virHashNew(g_free);
chown_paths = virHashNew(g_free);
selinux_paths = virHashNew(g_free);
}
static void
init_syms(void)
{
if (real_chown)
return;
VIR_MOCK_REAL_INIT(chown);
#if defined(__GLIBC__) && defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
VIR_MOCK_REAL_INIT_ALIASED(open, "open64");
#else
VIR_MOCK_REAL_INIT(open);
#endif
#if WITH___OPEN_2
VIR_MOCK_REAL_INIT(__open_2);
#endif
VIR_MOCK_REAL_INIT(close);
#ifdef WITH_SELINUX
VIR_MOCK_REAL_INIT(setfilecon_raw);
VIR_MOCK_REAL_INIT(getfilecon_raw);
#endif
VIR_MOCK_REAL_INIT(virFileExists);
/* Intentionally not calling init_hash() here */
}
static char *
get_key(const char *path,
const char *name)
{
return g_strdup_printf("%s:%s", path, name);
}
int
virFileGetXAttrQuiet(const char *path,
const char *name,
char **value)
{
g_autofree char *key = get_key(path, name);
char *val;
VIR_LOCK_GUARD lock = virLockGuardLock(&m);
init_syms();
init_hash();
if (!(val = virHashLookup(xattr_paths, key))) {
errno = ENODATA;
return -1;
}
*value = g_strdup(val);
return 0;
}
/*
* This may look redundant but is needed to work around an
* compiler quirk. The call from the real virFileGetXAttr
* to the real virFileGetXAttrQuiet has a quirk where the
* return value from virFileGetXAttrQuiet gets scrambled
* if we mock virFileGetXAttrQuiet, returning -1 instead
* of 0 despite succeeding. This happens on FreeBSD 11/12
* hosts with Clang, and is suspected to be some kind of
* compiler optimization. By mocking this function too we
* can workaround it.
*/
int
virFileGetXAttr(const char *path,
const char *name,
char **value)
{
int ret;
if ((ret = virFileGetXAttrQuiet(path, name, value)) < 0) {
virReportSystemError(errno,
"Unable to get XATTR %s on %s",
name, path);
}
return ret;
}
int virFileSetXAttr(const char *path,
const char *name,
const char *value)
{
g_autofree char *key = get_key(path, name);
g_autofree char *val = g_strdup(value);
VIR_LOCK_GUARD lock = virLockGuardLock(&m);
init_syms();
init_hash();
if (virHashUpdateEntry(xattr_paths, key, val) < 0)
return -1;
val = NULL;
return 0;
}
int virFileRemoveXAttr(const char *path,
const char *name)
{
int ret = -1;
g_autofree char *key = get_key(path, name);
VIR_LOCK_GUARD lock = virLockGuardLock(&m);
init_syms();
init_hash();
if ((ret = virHashRemoveEntry(xattr_paths, key)) < 0)
errno = ENODATA;
return ret;
}
#define VIR_MOCK_STAT_HOOK \
do { \
if (getenv(ENVVAR)) { \
uint32_t *val; \
\
virMutexLock(&m); \
init_hash(); \
\
memset(sb, 0, sizeof(*sb)); \
\
sb->st_mode = S_IFREG | 0666; \
sb->st_size = 123456; \
sb->st_ino = 1; \
\
if (!(val = virHashLookup(chown_paths, path))) { \
/* New path. Set the defaults */ \
sb->st_uid = DEFAULT_UID; \
sb->st_gid = DEFAULT_GID; \
} else { \
/* Known path. Set values passed to chown() earlier */ \
sb->st_uid = *val & 0xffff; \
sb->st_gid = *val >> 16; \
} \
\
virMutexUnlock(&m); \
\
return 0; \
} \
} while (0)
static int
mock_chown(const char *path,
uid_t uid,
gid_t gid)
{
g_autofree uint32_t *val = NULL;
VIR_LOCK_GUARD lock = virLockGuardLock(&m);
if (gid >> 16 || uid >> 16) {
fprintf(stderr, "Attempt to set too high UID or GID: %llu %llu",
(unsigned long long) uid, (unsigned long long) gid);
abort();
}
val = g_new0(uint32_t, 1);
*val = (gid << 16) + uid;
init_hash();
if (virHashUpdateEntry(chown_paths, path, val) < 0)
return -1;
val = NULL;
return 0;
}
#include "virmockstathelpers.c"
static int
virMockStatRedirect(const char *path G_GNUC_UNUSED, char **newpath G_GNUC_UNUSED)
{
return 0;
}
int
chown(const char *path, uid_t uid, gid_t gid)
{
int ret;
init_syms();
if (getenv(ENVVAR))
ret = mock_chown(path, uid, gid);
else
ret = real_chown(path, uid, gid);
return ret;
}
int
open(const char *path, int flags, ...)
{
int ret;
init_syms();
if (getenv(ENVVAR)) {
ret = 42; /* Some dummy FD */
} else if (flags & O_CREAT) {
va_list ap;
mode_t mode;
va_start(ap, flags);
mode = (mode_t) va_arg(ap, int);
va_end(ap);
ret = real_open(path, flags, mode);
} else {
ret = real_open(path, flags);
}
return ret;
}
#if WITH___OPEN_2
int
__open_2(const char *path, int flags)
{
int ret;
init_syms();
if (getenv(ENVVAR)) {
ret = 42; /* Some dummy FD */
} else {
ret = real___open_2(path, flags);
}
return ret;
}
#endif
int
close(int fd)
{
int ret;
init_syms();
if (fd == 42 && getenv(ENVVAR))
ret = 0;
else
ret = real_close(fd);
return ret;
}
int virFileLock(int fd G_GNUC_UNUSED,
bool shared G_GNUC_UNUSED,
off_t start G_GNUC_UNUSED,
off_t len G_GNUC_UNUSED,
bool waitForLock G_GNUC_UNUSED)
{
return 0;
}
int virFileUnlock(int fd G_GNUC_UNUSED,
off_t start G_GNUC_UNUSED,
off_t len G_GNUC_UNUSED)
{
return 0;
}
bool virFileExists(const char *path)
{
VIR_LOCK_GUARD lock = virLockGuardLock(&m);
if (getenv(ENVVAR) == NULL) {
init_syms();
return real_virFileExists(path);
}
init_hash();
if (virHashHasEntry(chown_paths, path))
return true;
if (virHashHasEntry(selinux_paths, path))
return true;
return false;
}
typedef struct _checkOwnerData checkOwnerData;
struct _checkOwnerData {
GHashTable *paths;
bool chown_fail;
bool selinux_fail;
};
static int
checkSELinux(void *payload,
const char *name,
void *opaque)
{
checkOwnerData *data = opaque;
char *label = payload;
if (STRNEQ(label, DEFAULT_SELINUX_LABEL) &&
!g_hash_table_contains(data->paths, name)) {
fprintf(stderr,
"Path %s wasn't restored back to its original SELinux label\n",
name);
data->selinux_fail = true;
}
return 0;
}
static int
checkOwner(void *payload,
const char *name,
void *opaque)
{
checkOwnerData *data = opaque;
uint32_t owner = *((uint32_t*) payload);
if ((owner % 16 != DEFAULT_UID ||
owner >> 16 != DEFAULT_GID) &&
!g_hash_table_contains(data->paths, name)) {
fprintf(stderr,
"Path %s wasn't restored back to its original owner\n",
name);
data->chown_fail = true;
}
return 0;
}
static int
printXATTR(void *payload,
const char *name,
void *data)
{
bool *xattr_fail = data;
/* The fact that we are in this function means that there are
* some XATTRs left behind. This is enough to claim an error. */
*xattr_fail = true;
/* Hash table key consists of "$path:$xattr_name", xattr
* value is then the value stored in the hash table. */
printf("key=%s val=%s\n", name, (const char *) payload);
return 0;
}
/**
* checkPaths:
* @paths: a NULL terminated list of paths expected not to be restored
*
* Check if all paths were restored and if no XATTR was left
* behind. Since restore is not done on all domain's paths, some
* paths are expected to be not restored. A list of such paths
* can be passed in @paths argument. If a path is not restored
* but it's on the list no error is indicated.
*/
int checkPaths(GHashTable *paths)
{
checkOwnerData data = { .paths = paths, .chown_fail = false, .selinux_fail = false };
bool xattr_fail = false;
GHashTableIter htitr;
void *key;
VIR_LOCK_GUARD lock = virLockGuardLock(&m);
init_hash();
g_hash_table_iter_init(&htitr, paths);
while (g_hash_table_iter_next(&htitr, &key, NULL)) {
if (!virHashLookup(chown_paths, key)) {
fprintf(stderr, "Unexpected path restored: %s\n", (const char *) key);
return -1;
}
}
if (virHashForEach(selinux_paths, checkSELinux, &data) < 0)
return -1;
if (virHashForEach(chown_paths, checkOwner, &data) < 0)
return -1;
if (virHashForEach(xattr_paths, printXATTR, &xattr_fail) < 0)
return -1;
if (data.chown_fail || data.selinux_fail || xattr_fail)
return -1;
return 0;
}
void freePaths(void)
{
VIR_LOCK_GUARD lock = virLockGuardLock(&m);
init_hash();
g_clear_pointer(&selinux_paths, g_hash_table_unref);
g_clear_pointer(&chown_paths, g_hash_table_unref);
g_clear_pointer(&xattr_paths, g_hash_table_unref);
}
int
virProcessRunInFork(virProcessForkCallback cb,
void *opaque)
{
return cb(-1, opaque);
}
/* We don't really need to mock this function. The qemusecuritytest doesn't
* care about the actual value. However, travis runs build and tests in a
* container where utmp is missing and thus this function fails. */
int
virHostGetBootTime(unsigned long long *when)
{
*when = 1234567890;
return 0;
}
#ifdef WITH_SELINUX
int
is_selinux_enabled(void)
{
return 1;
}
struct selabel_handle *
selabel_open(unsigned int backend G_GNUC_UNUSED,
const struct selinux_opt *opts G_GNUC_UNUSED,
unsigned nopts G_GNUC_UNUSED)
{
return (void*)((intptr_t) 0x1);
}
void
selabel_close(struct selabel_handle *rec G_GNUC_UNUSED)
{
/* nada */
}
const char *
selinux_virtual_domain_context_path(void)
{
return abs_srcdir "/qemusecuritydata/virtual_domain_context";
}
const char *
selinux_virtual_image_context_path(void)
{
return abs_srcdir "/qemusecuritydata/virtual_image_context";
}
int getcon_raw(char **context)
{
*context = g_strdup("system_u:system_r:virtd_t:s0-s0:c0.c1023");
return 0;
}
static int
mock_setfilecon_raw(const char *path,
const char *context)
{
g_autofree char *val = g_strdup(context);
VIR_LOCK_GUARD lock = virLockGuardLock(&m);
init_hash();
if (virHashUpdateEntry(selinux_paths, path, val) < 0)
return -1;
val = NULL;
return 0;
}
static int
mock_getfilecon_raw(const char *path,
char **context)
{
const char *val;
VIR_LOCK_GUARD lock = virLockGuardLock(&m);
init_hash();
val = virHashLookup(selinux_paths, path);
if (!val)
val = DEFAULT_SELINUX_LABEL;
*context = g_strdup(val);
return 0;
}
int
setfilecon_raw(const char *path,
const char *context)
{
int ret;
init_syms();
if (getenv(ENVVAR))
ret = mock_setfilecon_raw(path, context);
else
ret = real_setfilecon_raw(path, context);
return ret;
}
int
getfilecon_raw(const char *path,
char **context)
{
int ret;
init_syms();
if (getenv(ENVVAR))
ret = mock_getfilecon_raw(path, context);
else
ret = real_getfilecon_raw(path, context);
return ret;
}
int
selabel_lookup_raw(struct selabel_handle *hnd G_GNUC_UNUSED,
char **context,
const char *key G_GNUC_UNUSED,
int type G_GNUC_UNUSED)
{
/* This function will be called only if we haven't found original label in
* XATTRs. Return something else than DEFAULT_SELINUX_LABEL so that it is
* considered as error. */
*context = g_strdup("system_u:object_r:default_t:s1");
return 0;
}
#endif
|