selinux_label.c 28.0 KB
Newer Older
W
wujing 已提交
1 2
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2019-2020. All rights reserved.
3 4 5 6
 * iSulad licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
W
wujing 已提交
7 8 9
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
10
 * See the Mulan PSL v2 for more details.
W
wujing 已提交
11 12 13 14 15 16
 * Author: wujing
 * Create: 2019-12-15
 * Description: provide selinux label handle function definition
 ******************************************************************************/

#include "selinux_label.h"
17

W
wujing 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30
#include <selinux/selinux.h>
#include <selinux/context.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <dirent.h>
#include <pthread.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/statvfs.h>
31 32 33 34 35
#include <errno.h>
#include <stdint.h>
#include <sys/statfs.h>
#include <syscall.h>

W
wujing 已提交
36
#include "map.h"
H
haozi007 已提交
37
#include "isula_libutils/log.h"
W
wujing 已提交
38
#include "utils.h"
L
lifeng68 已提交
39
#include "err_msg.h"
40 41 42
#include "utils_array.h"
#include "utils_file.h"
#include "utils_string.h"
W
wujing 已提交
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

#define SELINUXFS_MOUNT "/sys/fs/selinux"
#define SELINUXFS_MAGIC 0xf97cff8c

typedef struct selinux_state_t {
    bool enabled_set;
    bool enabled;
    bool selinuxf_set;
    char *selinuxfs;
    map_t *mcs_list; // map string boolean
    pthread_rwlock_t rwlock;
} selinux_state;

static selinux_state *g_selinux_state = NULL;

static bool set_state_enable(bool enabled)
{
    bool result = false;

    if (pthread_rwlock_rdlock(&g_selinux_state->rwlock) != 0) {
        ERROR("lock mcs list failed");
        return false;
    }

    g_selinux_state->enabled_set = true;
    g_selinux_state->enabled = enabled;
    result = g_selinux_state->enabled;

    if (pthread_rwlock_unlock(&g_selinux_state->rwlock) != 0) {
        ERROR("unlock mcs list failed");
    }

    return result;
}

static int set_state_selinux_fs(char *selinuxfs)
{
    if (pthread_rwlock_rdlock(&g_selinux_state->rwlock) != 0) {
        ERROR("lock selinux state failed");
        return -1;
    }

    g_selinux_state->selinuxf_set = true;
    free(g_selinux_state->selinuxfs);
    g_selinux_state->selinuxfs = util_strdup_s(selinuxfs);

    if (pthread_rwlock_unlock(&g_selinux_state->rwlock) != 0) {
        ERROR("unlock selinux state failed");
        return -1;
    }

    return 0;
}

/* Verify the mount point for selinux file system has a selinuxfs. */
static bool verify_selinuxfs_mount(const char *mnt)
{
    struct statfs sfbuf;

    while (true) {
        int rc = statfs(mnt, &sfbuf);
        if (rc == 0) {
            break;
        }
        if (errno == EINTR) {
            continue;
        }
        return false;
    }

    if ((uint32_t)sfbuf.f_type != (uint32_t)SELINUXFS_MAGIC) {
        return false;
    }

    if ((sfbuf.f_flags & ST_RDONLY) != 0) {
        return false;
    }

    return true;
}
// returns a next selinuxfs mount point found,
// if there is one, or an empty string in case of EOF or error.
static void find_selinux_fs_among_mounts(char **fs)
{
#define MOUNT_POOINT_FIFTH_FIELD 5
    FILE *fp = NULL;
    char *buf = NULL;
    char **fields = NULL;
    size_t len;

    fp = fopen("/proc/self/mountinfo", "re");
    if (fp == NULL) {
        INFO("/proc/self/mountinfo not exists");
        return;
    }
    __fsetlocking(fp, FSETLOCKING_BYCALLER);

W
wujing 已提交
140
    while (getline(&buf, &len, fp) != -1) {
W
wujing 已提交
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
        if (!strstr(buf, " - selinuxfs ")) {
            continue;
        }
        fields = util_string_split((const char *)buf, ' ');
        if (fields == NULL || util_array_len((const char **)fields) < MOUNT_POOINT_FIFTH_FIELD + 1) {
            util_free_array(fields);
            continue;
        }
        if (verify_selinuxfs_mount(fields[MOUNT_POOINT_FIFTH_FIELD - 1])) {
            *fs = util_strdup_s(fields[MOUNT_POOINT_FIFTH_FIELD - 1]);
        }
        goto out;
    }

out:
    util_free_array(fields);
    free(buf);
    fclose(fp);
}

static void find_selinux_fs(char **fs)
{
    // fast path: check the default mount first
    if (verify_selinuxfs_mount(SELINUXFS_MOUNT)) {
        *fs = util_strdup_s(SELINUXFS_MOUNT);
        return;
    }
    // check if selinuxfs is available before going the slow path
    if (selinuxfs_exists() == 0) {
        return;
    }
    // slow path: try to find among the mounts
    find_selinux_fs_among_mounts(fs);

    return;
}

static int get_state_selinuxfs(char **fs)
{
    bool selinuxfs_set = false;
    char *selinuxfs = NULL;

    if (pthread_rwlock_rdlock(&g_selinux_state->rwlock) != 0) {
        ERROR("lock mcs list failed");
        return -1;
    }

    selinuxfs_set = g_selinux_state->selinuxf_set;
    selinuxfs = g_selinux_state->selinuxfs;

    if (pthread_rwlock_unlock(&g_selinux_state->rwlock) != 0) {
        ERROR("unlock mcs list failed");
        return -1;
    }

    if (selinuxfs_set) {
        *fs = util_strdup_s(selinuxfs);
        return 0;
    }

    find_selinux_fs(fs);

    return set_state_selinux_fs(*fs);
}

static int get_selinux_mount_point(char **fs)
{
    return get_state_selinuxfs(fs);
}

static int read_con(const char *fpath, char **content)
{
    int ret = 0;
    char *tmp = NULL;
    char *trim_str = NULL;

    if (fpath == NULL) {
        ERROR("Empty path");
        return -1;
    }

H
haozi007 已提交
222
    tmp = isula_utils_read_file(fpath);
W
wujing 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    if (tmp == NULL) {
        ERROR("Failed to read file: %s", fpath);
        ret = -1;
        goto out;
    }

    trim_str = util_trim_space(tmp);
    *content = util_strdup_s(trim_str);

out:
    free(tmp);
    return ret;
}

// get_current_label returns the SELinux label of the current process thread.
static int get_current_label(char **content)
{
    int nret = 0;
L
lifeng68 已提交
241
    char path[PATH_MAX] = { 0 };
W
wujing 已提交
242

L
lifeng68 已提交
243
    nret = snprintf(path, sizeof(path), "/proc/self/task/%ld/attr/current", (long int)syscall(__NR_gettid));
W
wujing 已提交
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
    if (nret < 0 || nret >= sizeof(path)) {
        ERROR("Humanize sprintf failed!");
        return -1;
    }

    return read_con(path, content);
}

bool selinux_get_enable()
{
    bool enabled_set = false;
    bool enabled = false;
    char *fs = NULL;

    if (pthread_rwlock_rdlock(&g_selinux_state->rwlock) != 0) {
        ERROR("lock selinux state failed");
        return false;
    }

    enabled_set = g_selinux_state->enabled_set;
    enabled = g_selinux_state->enabled;

    if (pthread_rwlock_unlock(&g_selinux_state->rwlock) != 0) {
        ERROR("unlock selinux state failed");
        return false;
    }

    if (enabled_set) {
        return enabled;
    }

    enabled = false;

    if (get_selinux_mount_point(&fs) != 0) {
        ERROR("Failed to get selinux mount point");
        return false;
    }

    if (fs != NULL) {
        char *content = NULL;

        if (get_current_label(&content) != 0 || content == NULL) {
            ERROR("Failed to get current label");
            return false;
        }
        if (strcmp(content, "kernel") != 0) {
            enabled = true;
        }
        free(content);
    }

    free(fs);
    return set_state_enable(enabled);
}

// just disable selinux support for iSulad
void selinux_set_disabled()
{
    (void)set_state_enable(false);
}

static int get_random_value(unsigned int range, unsigned int *val)
{
    int ret = 0;
    int num = 0;
    int fd = open("/dev/urandom", O_RDONLY);
    if (fd == -1) {
        ERROR("Failed to open urandom device\n");
        return -1;
    }

    if (read(fd, &num, sizeof(int)) < 0) {
        ERROR("Failed to read urandom value\n");
        ret = -1;
        goto out;
    }

    *val = (unsigned)num % range;

out:
    close(fd);
    return ret;
}

/* selinux state free */
static void selinux_state_free(selinux_state *state)
{
    if (state == NULL) {
        return;
    }

    map_free(state->mcs_list);
    state->mcs_list = NULL;
    free(state->selinuxfs);
    pthread_rwlock_destroy(&(state->rwlock));
    free(state);
}

/* memory store new */
static selinux_state *selinux_state_new(void)
{
    selinux_state *state = util_common_calloc_s(sizeof(selinux_state));
    if (state == NULL) {
        ERROR("Out of memory");
        return NULL;
    }

    if (pthread_rwlock_init(&(state->rwlock), NULL) != 0) {
        ERROR("Failed to init memory store rwlock");
        free(state);
        return NULL;
    }

    state->mcs_list = map_new(MAP_STR_BOOL, MAP_DEFAULT_CMP_FUNC, MAP_DEFAULT_FREE_FUNC);
    if (state->mcs_list == NULL) {
        ERROR("Out of memory");
        goto error_out;
    }

    return state;

error_out:
    selinux_state_free(g_selinux_state);
    return NULL;
}

/* selinux state init */
int selinux_state_init(void)
{
    g_selinux_state = selinux_state_new();
    if (g_selinux_state == NULL) {
        return -1;
    }

    return 0;
}

/* MCS already exists */
static bool is_mcs_already_exists(const char *mcs)
{
    char *val = NULL;

    if (mcs == NULL) {
        return false;
    }
    if (pthread_rwlock_rdlock(&g_selinux_state->rwlock) != 0) {
        ERROR("lock selinux state failed");
        return false;
    }

    val = map_search(g_selinux_state->mcs_list, (void *)mcs);

    if (pthread_rwlock_unlock(&g_selinux_state->rwlock) != 0) {
        ERROR("unlock selinux state failed");
    }

    return val != NULL;
}

/* MCS list add */
static bool mcs_add(const char *mcs)
{
    bool ret = false;
    bool val = true;

    if (pthread_rwlock_wrlock(&g_selinux_state->rwlock)) {
        ERROR("lock memory store failed");
        return false;
    }

    ret = map_replace(g_selinux_state->mcs_list, (void *)mcs, (void *)&val);

    if (pthread_rwlock_unlock(&g_selinux_state->rwlock)) {
        ERROR("unlock memory store failed");
        return false;
    }

    return ret;
}

static bool mcs_delete(const char *mcs)
{
    bool ret = false;
    bool val = true;

    if (mcs == NULL) {
        return 0;
    }

    if (pthread_rwlock_wrlock(&g_selinux_state->rwlock) != 0) {
        ERROR("lock name index failed");
        return false;
    }

    ret = map_replace(g_selinux_state->mcs_list, (void *)mcs, (void *)&val);

    if (pthread_rwlock_unlock(&g_selinux_state->rwlock) != 0) {
        ERROR("unlock name index failed");
        return false;
    }

    return ret;
}

static int add_mcs_to_global_list(const char *mcs)
{
    if (is_mcs_already_exists(mcs)) {
        DEBUG("MCS label already exists");
        return -1;
    }

    if (!mcs_add(mcs)) {
        ERROR("Failed to add mcs to global list");
        return -1;
    }

    return 0;
}

static int uniq_mcs(unsigned int range, char *mcs, size_t len)
{
    unsigned int c1, c2;

    while (true) {
        int nret;

        if (get_random_value(range, &c1) != 0 || get_random_value(range, &c2) != 0) {
            return -1;
        }
        if (c1 == c2) {
            continue;
        } else if (c1 > c2) {
            unsigned int tmp = c1;
            c1 = c2;
            c2 = tmp;
        }

        nret = snprintf(mcs, len, "s0:c%d,c%d", c1, c2);
        if (nret < 0 || nret >= len) {
            ERROR("Failed to compose mcs");
            return -1;
        }

        if (add_mcs_to_global_list(mcs)) {
            continue;
        }

        break;
    }

    return 0;
}

static bool should_skip_in_lxc_contexts(const char *line)
{
    // skip blank lines
    if (strlen(line) == 0) {
        return true;
    }

    // skip comments
    if (line[0] == ';' || line[0] == '#') {
        return true;
    }

    return false;
}

static int parse_lxc_context_info(const char *line, char **process_label, char **file_label)
{
    int ret = 0;
    size_t groups_len = 0;
    char **groups = NULL;
    char *key = NULL;
    char *val = NULL;

    groups = util_string_split(line, '=');
    if (groups == NULL) {
        ERROR("Out of memory");
        return -1;
    }

    groups_len = util_array_len((const char **)groups);
    if (groups_len != 2) {
        ERROR("Invalid context");
        ret = -1;
        goto out;
    }

    key = util_trim_space(groups[0]);
    val = util_trim_space(groups[1]);

    if (strcmp(key, "process") == 0) {
        free(*process_label);
        *process_label = util_string_delchar(val, '"');
    } else if (strcmp(key, "file") == 0) {
        free(*file_label);
        *file_label = util_string_delchar(val, '"');
    }

out:
    util_free_array(groups);
    return ret;
}

static void update_process_and_mount_label_range(char **process_label, char **file_label)
{
#define MCS_MAX_LEN 20
    context_t scon = context_new(*process_label);

    if (context_range_get(scon) != NULL) {
        char mcs[MCS_MAX_LEN] = { 0x00 };

        uniq_mcs(1024, mcs, MCS_MAX_LEN);
        context_range_set(scon, mcs);
        free(*process_label);
        *process_label = util_strdup_s(context_str(scon));

        context_t mcon = context_new(*file_label);
        context_range_set(mcon, mcs);
        free(*file_label);
        *file_label = util_strdup_s(context_str(mcon));
        context_free(mcon);
    }

    context_free(scon);
}

static int container_label(char **process_label, char **file_label)
{
    int ret = 0;
    size_t len;
    ssize_t num;
    FILE *file = NULL;
    char *buf = NULL;
    const char *lxc_path = NULL;

    if (!selinux_get_enable()) {
        return 0;
    }

    lxc_path = selinux_lxc_contexts_path();
    if (lxc_path == NULL) {
        ERROR("Failed to get selinux lxc contexts path");
        return -1;
    }

    file = fopen(lxc_path, "re");
    if (file == NULL) {
        ERROR("Failed to open '%s'", lxc_path);
        return -1;
    }
    __fsetlocking(file, FSETLOCKING_BYCALLER);

    for (num = getline(&buf, &len, file); num != -1; num = getline(&buf, &len, file)) {
        char *line = util_strdup_s(buf);
        char *tmp_line = util_trim_space(line);

        if (should_skip_in_lxc_contexts(tmp_line)) {
            free(line);
            continue;
        }

        if (parse_lxc_context_info(tmp_line, process_label, file_label) != 0) {
            ERROR("Failed to parse lxc context info");
            free(line);
            ret = -1;
            goto out;
        }

        free(line);
    }

    if (*process_label == NULL || *file_label == NULL) {
        ret = 0;
        goto out;
    }

    update_process_and_mount_label_range(process_label, file_label);

out:
    free(buf);
    fclose(file);
    return ret;
}

static bool valid_options(const char *opt)
{
W
wujing 已提交
632
    size_t i;
W
wujing 已提交
633 634
    const char *opts[] = { "disable", "type", "user", "role", "level" };

W
wujing 已提交
635
    for (i = 0; i < sizeof(opts) / sizeof(char *); i++) {
W
wujing 已提交
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 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
        if (strcmp(opt, opts[i]) == 0) {
            return true;
        }
    }

    return false;
}

static int release_label(const char *label)
{
    int ret = 0;
    const char *range = NULL;

    if (label == NULL) {
        ERROR("Invalid label");
        return -1;
    }

    context_t tmp = context_new(label);
    range = context_range_get(tmp);
    if (range != NULL) {
        if (!mcs_delete(range)) {
            ERROR("delete mcs '%s' failed", range);
            ret = -1;
            goto out;
        }
    }

out:
    context_free(tmp);
    return ret;
}

static int reserve_label(const char *label)
{
    int ret = 0;
    const char *range = NULL;

    if (label == NULL) {
        ERROR("Invalid label");
        return -1;
    }

    context_t tmp = context_new(label);
    range = context_range_get(tmp);
    if (range != NULL) {
        if (!mcs_add(range)) {
            ERROR("add mcs '%s' failed", range);
            ret = -1;
            goto out;
        }
    }

out:
    context_free(tmp);
    return ret;
}

static int parse_label_security_opt(const char *label_opt, context_t pcon, context_t mcon)
{
    int ret = 0;
    bool failure = false;
    char **items = NULL;
    size_t items_len = 0;

    items = util_string_split_n(label_opt, ':', 2);
    if (items == NULL) {
        ERROR("split label '%s' failed", label_opt);
        ret = -1;
        goto out;
    }

    items_len = util_array_len((const char **)items);
    if (items_len != 2 || strlen(items[1]) == 0) {
        isulad_set_error_message("Bad security label option \"%s\"", label_opt);
        ERROR("Bad security label option \"%s\"", label_opt);
        ret = -1;
        goto out;
    }

    if (!valid_options(items[0])) {
        isulad_set_error_message("Bad label option \"%s\", valid options 'disable, user, role, level, type'", items[0]);
        ERROR("Bad label option \"%s\", valid options 'disable, user, role, level, type'", items[0]);
        ret = -1;
        goto out;
    }

    if (strcmp(items[0], "type") == 0) {
        failure = (context_type_set(pcon, items[1]) != 0);
    } else if (strcmp(items[0], "user") == 0) {
        failure = (context_user_set(pcon, items[1]) != 0 || context_user_set(mcon, items[1]) != 0);
    } else if (strcmp(items[0], "role") == 0) {
        failure = (context_role_set(pcon, items[1]) != 0);
    } else if (strcmp(items[0], "level") == 0) {
        failure = (context_range_set(pcon, items[1]) != 0 || context_range_set(mcon, items[1]) != 0);
    } else {
        failure = true;
    }

    if (failure) {
        isulad_set_error_message("Failed to set selinux context: %s", label_opt);
        ERROR("Failed to set selinux context: %s", label_opt);
        ret = -1;
        goto out;
    }

out:
    util_free_array(items);
    return ret;
}

// InitLabels returns the process label and file labels to be used within
// the container.  A list of options can be passed into this function to alter
// the labels.  The labels returned will include a random MCS String, that is
// guaranteed to be unique.
int init_label(const char **label_opts, size_t label_opts_len, char **dst_process_label, char **dst_mount_label)
{
    int ret = 0;
    char *process_label = NULL;
    char *mount_label = NULL;
    context_t pcon = NULL;
    context_t mcon = NULL;

    if (!selinux_get_enable()) {
        return 0;
    }

    if (container_label(&process_label, &mount_label) != 0) {
        ret = -1;
        goto out;
    }

    if (process_label != NULL) {
W
wujing 已提交
769
        size_t i;
W
wujing 已提交
770 771
        pcon = context_new(process_label);
        mcon = context_new(mount_label);
W
wujing 已提交
772
        for (i = 0; i < label_opts_len; i++) {
W
wujing 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
            if (strcmp(label_opts[i], "disable") == 0) {
                goto out;
            }
            if (strstr(label_opts[i], ":") == NULL) {
                isulad_set_error_message("Bad label option %s, valid options 'disable"
                                         " or user, role, level, type' followed by ':' and a value",
                                         label_opts[i]);
                ERROR("Bad label option %s, valid options 'disable' or \n"
                      "'user, role, level, type' followed by ':' and a value",
                      label_opts[i]);
                ret = -1;
                goto out;
            }

            if (parse_label_security_opt(label_opts[i], pcon, mcon) != 0) {
                ERROR("Failed to parse security label option");
                ret = -1;
                goto out;
            }
        }
        if (release_label(process_label) != 0) {
H
haozi007 已提交
794
            ERROR("Failed to release process label: %s", process_label);
W
wujing 已提交
795 796 797 798 799 800 801 802
            ret = -1;
            goto out;
        }
        free(process_label);
        process_label = util_strdup_s(context_str(pcon));
        free(mount_label);
        mount_label = util_strdup_s(context_str(mcon));
        if (reserve_label(process_label) != 0) {
H
haozi007 已提交
803
            ERROR("Failed to release process label: %s", process_label);
W
wujing 已提交
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
            ret = -1;
            goto out;
        }
    }

    *dst_process_label = process_label;
    *dst_mount_label = mount_label;
    process_label = NULL;
    mount_label = NULL;

out:
    context_free(pcon);
    context_free(mcon);
    free(process_label);
    free(mount_label);
    return ret;
}

static bool is_exclude_relabel_path(const char *path)
{
    const char *exclude_path[] = { "/", "/usr", "/etc", "/tmp", "/home", "/run", "/var", "/root" };
    size_t i;

    for (i = 0; i < sizeof(exclude_path) / sizeof(char *); i++) {
        if (strcmp(path, exclude_path[i]) == 0) {
            return true;
        }
    }

    return false;
}

// Prevent users from relabing system files
static int bad_prefix(const char *fpath)
{
    const char *bad_prefixes = "/usr";

    if (fpath == NULL) {
        ERROR("Empty file path");
        return -1;
    }

    if (strncmp(fpath, bad_prefixes, strlen(bad_prefixes)) == 0) {
        ERROR("relabeling content in %s is not allowed", bad_prefixes);
        return -1;
    }

    return 0;
}

static int recurse_set_file_label(const char *basePath, const char *label)
{
    int ret = 0;
    DIR *dir = NULL;
    struct dirent *ptr = NULL;
L
lifeng68 已提交
859
    char base[PATH_MAX] = { 0 };
W
wujing 已提交
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073

    if ((dir = opendir(basePath)) == NULL) {
        ERROR("Failed to Open dir: %s", basePath);
        return -1;
    }

    ret = lsetfilecon(basePath, label);
    if (ret != 0) {
        ERROR("Failed to set file label");
        goto out;
    }

    while ((ptr = readdir(dir)) != NULL) {
        if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) {
            continue;
        } else {
            int nret = snprintf(base, sizeof(base), "%s/%s", basePath, ptr->d_name);
            if (nret < 0 || nret >= sizeof(base)) {
                ERROR("Failed to get path");
                ret = -1;
                goto out;
            }
            if (ptr->d_type == DT_DIR) {
                ret = recurse_set_file_label(base, label);
                if (ret != 0) {
                    ERROR("Failed to set dir label");
                    goto out;
                }
            } else {
                ret = lsetfilecon(base, label);
                if (ret != 0) {
                    ERROR("Failed to set file label");
                    goto out;
                }
            }
        }
    }

out:
    closedir(dir);
    return ret;
}

// Chcon changes the `fpath` file object to the SELinux label `label`.
// If `fpath` is a directory and `recurse`` is true, Chcon will walk the
// directory tree setting the label.
static int selinux_chcon(const char *fpath, const char *label, bool recurse)
{
    struct stat s_buf;

    if (fpath == NULL) {
        ERROR("Empty file path");
        return -1;
    }

    if (label == NULL) {
        return 0;
    }

    if (bad_prefix(fpath) != 0) {
        return -1;
    }
    if (stat(fpath, &s_buf) != 0) {
        return -1;
    }
    if (recurse && S_ISDIR(s_buf.st_mode)) {
        return recurse_set_file_label(fpath, label);
    }

    if (lsetfilecon(fpath, label) != 0) {
        ERROR("Failed to set file label");
        return -1;
    }

    return 0;
}

// Relabel changes the label of path to the filelabel string.
// It changes the MCS label to s0 if shared is true.
// This will allow all containers to share the content.
int relabel(const char *path, const char *file_label, bool shared)
{
    int ret = 0;
    char *tmp_file_label = NULL;

    if (!selinux_get_enable()) {
        return 0;
    }

    if (file_label == NULL) {
        return 0;
    }

    tmp_file_label = util_strdup_s(file_label);
    if (is_exclude_relabel_path(path)) {
        ERROR("SELinux relabeling of %s is not allowed", path);
        ret = -1;
        goto out;
    }

    if (shared) {
        context_t c = context_new(file_label);
        context_range_set(c, "s0");
        free(tmp_file_label);
        tmp_file_label = util_strdup_s(context_str(c));
        context_free(c);
    }

    if (selinux_chcon(path, tmp_file_label, true) != 0) {
        ERROR("Failed to modify %s's selinux context: %s", path, tmp_file_label);
        ret = -1;
        goto out;
    }

out:
    free(tmp_file_label);
    return ret;
}

static int append_security_opt_string(const char *field, const char *value, char ***security_opts)
{
    int ret = 0;
    int nret = 0;
    char *sec_opt = NULL;
    size_t temp_len = strlen(field) + strlen(value) + 1;

    sec_opt = util_common_calloc_s(temp_len);
    if (sec_opt == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }
    nret = snprintf(sec_opt, temp_len, "%s%s", field, value);
    if (nret < 0 || nret >= temp_len) {
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }
    if (util_array_append(security_opts, sec_opt) < 0) {
        ERROR("Failed to append element to array");
        ret = -1;
        goto out;
    }

out:
    free(sec_opt);
    return ret;
}

// DupSecOpt takes an SELinux process label and returns security options that
// can be used to set the SELinux Type and Level for future container processes.
int dup_security_opt(const char *src, char ***dst, size_t *len)
{
    int ret = 0;
    size_t new_len = 3;
    char **security_opts = NULL;

    if (src == NULL) {
        return 0;
    }

    context_t con = context_new(src);
    if (context_user_get(con) == NULL || context_role_get(con) == NULL || context_type_get(con) == NULL) {
        return 0;
    }
    if (context_range_get(con) != NULL) {
        new_len++;
    }

    if (append_security_opt_string("user:", context_user_get(con), &security_opts) != 0) {
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }
    if (append_security_opt_string("role:", context_role_get(con), &security_opts) != 0) {
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }
    if (append_security_opt_string("type:", context_type_get(con), &security_opts) != 0) {
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }

    if (context_range_get(con) != NULL) {
        if (append_security_opt_string("level:", context_range_get(con), &security_opts) != 0) {
            ERROR("Out of memory");
            ret = -1;
            goto out;
        }
    }
    *dst = security_opts;
    *len = new_len;

    security_opts = NULL;

out:
    util_free_array(security_opts);
    context_free(con);
    return ret;
}

int get_disable_security_opt(char ***labels, size_t *labels_len)
{
    if (util_array_append(labels, "disable") != 0) {
        ERROR("Failed to append label");
        return -1;
    }

    *labels_len = util_array_len((const char **)(*labels));

    return 0;
}
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164

#define MOUNT_CONTEXT "context="

static char *fill_selinux_label_with_src(const char *src, const char *mount_label)
{
    int nret = 0;
    char *result = NULL;
    size_t data_size = 0;

    if (strlen(mount_label) >= (INT_MAX - strlen(src) - strlen(MOUNT_CONTEXT) - 4)) {
        ERROR("mount_label string too large");
        goto err_out;
    }

    data_size = strlen(src) + 1 + strlen(MOUNT_CONTEXT) + 2 + strlen(mount_label) + 1;

    result = util_common_calloc_s(data_size);
    if (result == NULL) {
        ERROR("Memory out");
        goto err_out;
    }

    nret = snprintf(result, data_size, "%s,%s\"%s\"", src, MOUNT_CONTEXT, mount_label);
    if (nret < 0 || (size_t)nret >= data_size) {
        ERROR("failed to snprintf selinux label");
        goto err_out;
    }

    goto out;

err_out:
    free(result);
    result = NULL;

out:
    return result;
}

static char *fill_selinux_label_without_src(const char *mount_label)
{
    int nret = 0;
    char *result = NULL;
    size_t data_size = 0;

    if (strlen(mount_label) >= (INT_MAX - strlen(MOUNT_CONTEXT) - 3)) {
        ERROR("mount_label string too large");
        goto err_out;
    }

    data_size = strlen(MOUNT_CONTEXT) + strlen(mount_label) + 3;

    result = util_common_calloc_s(data_size);
    if (result == NULL) {
        ERROR("Memory out");
        goto err_out;
    }

    nret = snprintf(result, data_size, "%s\"%s\"", MOUNT_CONTEXT, mount_label);
    if (nret < 0 || (size_t)nret >= data_size) {
        ERROR("failed to snprintf selinux label");
        goto err_out;
    }

    goto out;

err_out:
    free(result);
    result = NULL;

out:
    return result;
}

char *selinux_format_mountlabel(const char *src, const char *mount_label)
{
    char *result = NULL;

    if (src == NULL && mount_label == NULL) {
        return NULL;
    }

    if (src != NULL && mount_label != NULL) {
        result = fill_selinux_label_with_src(src, mount_label);
    } else if (src == NULL) {
        result = fill_selinux_label_without_src(mount_label);
    } else {
        result = util_strdup_s(src);
    }

    return result;
}