specs_security.c 31.5 KB
Newer Older
O
overweight 已提交
1 2
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2017-2019. 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
O
overweight 已提交
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.
O
overweight 已提交
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
 * Author: tanyifeng
 * Create: 2017-11-22
 * Description: provide container specs functions
 ******************************************************************************/
#include "specs_security.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/utsname.h>
#include <sched.h>
#include <ctype.h>
#ifdef HAVE_LIBCAP_H
#include <sys/capability.h>
#endif

#include "error.h"
H
haozi007 已提交
36 37 38 39
#include "isula_libutils/log.h"
#include "isula_libutils/oci_runtime_spec.h"
#include "isula_libutils/docker_seccomp.h"
#include "isula_libutils/host_config.h"
O
overweight 已提交
40 41
#include "utils.h"
#include "config.h"
L
LiuHao 已提交
42
#include "isulad_config.h"
H
haozi007 已提交
43
#include "isula_libutils/parse_common.h"
L
LiuHao 已提交
44
#include "libisulad.h"
O
overweight 已提交
45
#include "specs_extend.h"
W
wujing 已提交
46
#include "selinux_label.h"
47
#include "specs.h"
48
#include "constants.h"
O
overweight 已提交
49 50 51

#define MAX_CAP_LEN 32

52
static const char * const g_system_caps[] = { "SYS_BOOT",     "SETPCAP", "NET_RAW", "NET_BIND_SERVICE",
O
overweight 已提交
53
#ifdef CAP_AUDIT_WRITE
54
                                              "AUDIT_WRITE",
O
overweight 已提交
55
#endif
56 57 58
                                              "DAC_OVERRIDE", "SETFCAP", "SETGID",  "SETUID",           "MKNOD", "CHOWN",
                                              "FOWNER",       "FSETID",  "KILL",    "SYS_CHROOT"
                                            };
O
overweight 已提交
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

static int append_capability(char ***dstcaps, size_t *dstcaps_len, const char *cap)
{
    int ret = 0;
    char **tmp = NULL;

    if (*dstcaps_len > SIZE_MAX / sizeof(char *) - 1) {
        ERROR("Too many capabilities to append!");
        ret = -1;
        goto out;
    }
    ret = mem_realloc((void **)&tmp, sizeof(char *) * (*dstcaps_len + 1), *dstcaps, sizeof(char *) * (*dstcaps_len));
    if (ret != 0) {
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }
    *dstcaps = tmp;
    (*dstcaps_len)++;

    (*dstcaps)[*dstcaps_len - 1] = util_strdup_s(cap);
out:
    return ret;
}

static int copy_capabilities(char ***dstcaps, size_t *dstcaps_len, const char **srccaps, size_t srccaps_len)
{
    size_t i;
    int ret = 0;

    if (srccaps == NULL || srccaps_len == 0) {
        *dstcaps = NULL;
        *dstcaps_len = 0;
        return ret;
    }
    if (srccaps_len > SIZE_MAX / sizeof(char *)) {
        ERROR("Too many capabilities to copy!");
        return -1;
    }

    *dstcaps = util_common_calloc_s(srccaps_len * sizeof(char *));
    if (*dstcaps == NULL) {
        ret = -1;
        goto out;
    }

    *dstcaps_len = srccaps_len;

    for (i = 0; i < srccaps_len; i++) {
        (*dstcaps)[i] = util_strdup_s(srccaps[i]);
    }
out:
    return ret;
}

static int tweak_drops_capabilities(char ***new_caps, size_t *new_caps_len, char **basic_caps, size_t basic_caps_len,
                                    const char **drops, size_t drops_len)
{
    size_t i = 0;
    int ret = 0;

    if (strings_in_slice((const char **)drops, drops_len, "all")) {
        goto out;
    }

    for (i = 0; (basic_caps != NULL && i < basic_caps_len); i++) {
        // skip `all` already handled above
        if (!basic_caps[i] || !strcasecmp(basic_caps[i], "all")) {
            continue;
        }

        // if we don't drop `all`, add back all the non-dropped caps
        if (!strings_in_slice((const char **)drops, drops_len, basic_caps[i] + strlen("CAP_"))) {
            ret = append_capability(new_caps, new_caps_len, basic_caps[i]);
            if (ret != 0) {
                ERROR("Failed to append capabilities");
                ret = -1;
                goto out;
            }
        }
    }

out:
    return ret;
}

static int tweak_adds_capabilities(char ***new_caps, size_t *new_caps_len, const char **adds, size_t adds_len)
{
    size_t i = 0;
    int ret = 0;
    int nret = 0;
    size_t all_caps_len = 0;
    char tmpcap[MAX_CAP_LEN] = { 0 };

    all_caps_len = util_get_all_caps_len();

    for (i = 0; i < adds_len; i++) {
        // skip `all` already handled above
        if (strcasecmp(adds[i], "all") == 0) {
            continue;
        }

O
openeuler-iSula 已提交
161 162
        nret = snprintf(tmpcap, sizeof(tmpcap), "CAP_%s", adds[i]);
        if (nret < 0 || (size_t)nret >= sizeof(tmpcap)) {
O
overweight 已提交
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
            ERROR("Failed to print string");
            ret = -1;
            goto out;
        }
        if (!strings_in_slice(g_all_caps, all_caps_len, tmpcap)) {
            ERROR("Unknown capability to add: '%s'", tmpcap);
            ret = -1;
            goto out;
        }

        // add cap if not already in the list
        if (!strings_in_slice((const char **)*new_caps, *new_caps_len, tmpcap)) {
            ret = append_capability(new_caps, new_caps_len, tmpcap);
            if (ret != 0) {
                ERROR("Failed to append capabilities");
                ret = -1;
                goto out;
            }
        }
    }

out:
    return ret;
}

static bool valid_drops_cap(const char **drops, size_t drops_len)
{
    int nret = 0;
    size_t i;
    size_t all_caps_len = 0;
    char tmpcap[MAX_CAP_LEN] = { 0 };

    all_caps_len = util_get_all_caps_len();
    // look for invalid cap in the drop list
    for (i = 0; i < drops_len; i++) {
        if (strcasecmp(drops[i], "all") == 0) {
            continue;
        }

O
openeuler-iSula 已提交
202 203
        nret = snprintf(tmpcap, sizeof(tmpcap), "CAP_%s", drops[i]);
        if (nret < 0 || (size_t)nret >= sizeof(tmpcap)) {
O
overweight 已提交
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
            ERROR("Failed to print string");
            return false;
        }
        if (!strings_in_slice(g_all_caps, all_caps_len, tmpcap)) {
            ERROR("Unknown capability to drop: '%s'", drops[i]);
            return false;
        }
    }

    return true;
}

// tweak_capabilities can tweak capabilities by adding or dropping capabilities
// based on the basic capabilities.
static int tweak_capabilities(char ***caps, size_t *caps_len, const char **adds, size_t adds_len, const char **drops,
                              size_t drops_len)
{
    size_t i;
    size_t all_caps_len = 0;
    int ret = 0;
    char **new_caps = NULL;
    char **basic_caps = NULL;
    size_t new_caps_len = 0;
    size_t basic_caps_len = 0;

    all_caps_len = util_get_all_caps_len();
    if (!valid_drops_cap(drops, drops_len)) {
        return -1;
    }

    if (strings_in_slice((const char **)adds, adds_len, "all")) {
        ret = copy_capabilities(&basic_caps, &basic_caps_len, g_all_caps, all_caps_len);
    } else {
        ret = copy_capabilities(&basic_caps, &basic_caps_len, (const char **)*caps, *caps_len);
    }
    if (ret != 0) {
        ERROR("Failed to copy capabilities");
        ret = -1;
        goto free_out;
    }

    ret = tweak_drops_capabilities(&new_caps, &new_caps_len, basic_caps, basic_caps_len, drops, drops_len);
    if (ret != 0) {
        ret = -1;
        goto free_out;
    }

    ret = tweak_adds_capabilities(&new_caps, &new_caps_len, adds, adds_len);
    if (ret != 0) {
        ret = -1;
        goto free_out;
    }

free_out:
    for (i = 0; i < basic_caps_len; i++) {
        free(basic_caps[i]);
    }
    free(basic_caps);

    // free old caps
    for (i = 0; i < *caps_len; i++) {
        free((*caps)[i]);
        (*caps)[i] = NULL;
    }
    free(*caps);

    // set new caps
    *caps = new_caps;
    *caps_len = new_caps_len;

    return ret;
}

277
int refill_oci_process_capabilities(defs_process_capabilities **caps, const char **src_caps, size_t src_caps_len)
O
overweight 已提交
278 279 280 281 282
{
    int ret = 0;
    size_t i = 0;

    if (*caps == NULL) {
283
        *caps = util_common_calloc_s(sizeof(defs_process_capabilities));
O
overweight 已提交
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
        if (*caps == NULL) {
            ret = -1;
            goto out;
        }
    }

    if ((*caps)->bounding != NULL) {
        // free current capabilities
        for (i = 0; i < ((*caps)->bounding_len); i++) {
            free((*caps)->bounding[i]);
            (*caps)->bounding[i] = NULL;
        }
        free((*caps)->bounding);
        (*caps)->bounding = NULL;
    }
    (*caps)->bounding_len = 0;

    // copy capabilities
    ret = copy_capabilities(&((*caps)->bounding), &((*caps)->bounding_len), src_caps, src_caps_len);
    if (ret != 0) {
        ERROR("Failed to copy all capabilities");
    }
out:
    return ret;
}

static char *seccomp_trans_arch_for_docker(const char *arch)
{
    size_t i = 0;
313
    char *arch_map[][2] = { { "SCMP_ARCH_X86", "x86" },
O
overweight 已提交
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
        { "SCMP_ARCH_X86_64", "amd64" },
        { "SCMP_ARCH_X32", "x32" },
        { "SCMP_ARCH_ARM", "arm" },
        { "SCMP_ARCH_AARCH64", "arm64" },
        { "SCMP_ARCH_MIPS", "mips" },
        { "SCMP_ARCH_MIPS64", "mips64" },
        { "SCMP_ARCH_MIPS64N32", "mips64n32" },
        { "SCMP_ARCH_MIPSEL", "mipsel" },
        { "SCMP_ARCH_MIPSEL64", "mipsel64" },
        { "SCMP_ARCH_MIPSEL64N32", "mipsel64n32" },
        { "SCMP_ARCH_PPC", "ppc" },
        { "SCMP_ARCH_PPC64", "ppc64" },
        { "SCMP_ARCH_PPC64LE", "ppc64le" },
        { "SCMP_ARCH_S390", "s390" },
        { "SCMP_ARCH_S390X", "s390x" },
        { "SCMP_ARCH_PARISC", "parisc" },
        { "SCMP_ARCH_PARISC64", "parisc64" },
        { "SCMP_ARCH_ALL", "all" }
    };
    for (i = 0; i < sizeof(arch_map) / sizeof(arch_map[0]); i++) {
        if (strcmp(arch, arch_map[i][0]) == 0) {
            return util_strdup_s(arch_map[i][1]);
        }
    }

    return NULL;
}

static bool is_arch_in_seccomp(const docker_seccomp *seccomp, const char *arch)
{
    size_t i, j;
    char *arch_for_docker = NULL;

    for (i = 0; i < seccomp->arch_map_len; i++) {
        int nret = 0;
        arch_for_docker = seccomp_trans_arch_for_docker(seccomp->arch_map[i]->architecture);
        if (arch_for_docker == NULL) {
            return false;
        }
        nret = strcmp(arch_for_docker, arch);
        free(arch_for_docker);
        if (nret == 0) {
            return true;
        }
        for (j = 0; j < seccomp->arch_map[i]->sub_architectures_len; j++) {
            arch_for_docker = seccomp_trans_arch_for_docker(seccomp->arch_map[i]->sub_architectures[j]);
            if (arch_for_docker == NULL) {
                return false;
            }
            nret = strcmp(arch_for_docker, arch);
            free(arch_for_docker);
            if (nret == 0) {
                return true;
            }
        }
    }
    return false;
}

373
static bool is_cap_in_seccomp(const defs_process_capabilities *capabilites, const char *cap)
O
overweight 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
{
    size_t i = 0;

    if (capabilites == NULL) {
        return false;
    }

    for (i = 0; i < capabilites->bounding_len; i++) {
        if (strcasecmp(capabilites->bounding[i], cap) == 0) {
            return true;
        }
    }
    return false;
}

static void meet_include(const docker_seccomp *seccomp, const docker_seccomp_syscalls_element *syscall,
390
                         const defs_process_capabilities *capabilites, bool *meet_include_arch, bool *meet_include_cap)
O
overweight 已提交
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
{
    size_t i;

    if (syscall->includes == NULL) {
        *meet_include_arch = true;
        *meet_include_cap = true;
        return;
    }
    if (syscall->includes->arches == NULL) {
        *meet_include_arch = true;
    } else {
        for (i = 0; i < syscall->includes->arches_len; i++) {
            if (is_arch_in_seccomp(seccomp, syscall->includes->arches[i])) {
                *meet_include_arch = true;
                break;
            }
        }
    }
    if (syscall->includes->caps == NULL) {
        *meet_include_cap = true;
    } else {
        for (i = 0; i < syscall->includes->caps_len; i++) {
            if (is_cap_in_seccomp(capabilites, syscall->includes->caps[i])) {
                *meet_include_cap = true;
                break;
            }
        }
    }
}

static void meet_exclude(const docker_seccomp *seccomp, const docker_seccomp_syscalls_element *syscall,
422
                         const defs_process_capabilities *capabilites, bool *meet_exclude_arch, bool *meet_exclude_cap)
O
overweight 已提交
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
{
    size_t i;

    if (syscall->excludes == NULL) {
        *meet_exclude_arch = true;
        *meet_exclude_cap = true;
        return;
    }

    if (syscall->excludes->arches == NULL) {
        *meet_exclude_arch = true;
    } else {
        for (i = 0; i < syscall->excludes->arches_len; i++) {
            if (is_arch_in_seccomp(seccomp, syscall->excludes->arches[i])) {
                *meet_exclude_arch = false;
                break;
            }
        }
    }
    if (syscall->excludes->caps == NULL) {
        *meet_exclude_cap = true;
    } else {
        for (i = 0; i < syscall->excludes->caps_len; i++) {
            if (is_cap_in_seccomp(capabilites, syscall->excludes->caps[i])) {
                *meet_exclude_cap = false;
                break;
            }
        }
    }
}

static bool meet_filtering_rules(const docker_seccomp *seccomp, const docker_seccomp_syscalls_element *syscall,
455
                                 const defs_process_capabilities *capabilites)
O
overweight 已提交
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
{
    bool meet_include_arch = false;
    bool meet_include_cap = false;
    bool meet_exclude_arch = true;
    bool meet_exclude_cap = true;

    meet_include(seccomp, syscall, capabilites, &meet_include_arch, &meet_include_cap);
    meet_exclude(seccomp, syscall, capabilites, &meet_exclude_arch, &meet_exclude_cap);

    return meet_include_arch && meet_include_cap && meet_exclude_arch && meet_exclude_cap;
}

static size_t docker_seccomp_arches_count(const docker_seccomp *docker_seccomp_spec)
{
    size_t count = 0;
    size_t i = 0;
    for (i = 0; i < docker_seccomp_spec->arch_map_len; i++) {
        count += docker_seccomp_spec->arch_map[i]->sub_architectures_len + 1;
    }
    return count;
}

static int dup_architectures_to_oci_spec(const docker_seccomp *docker_seccomp_spec,
                                         oci_runtime_config_linux_seccomp *oci_seccomp_spec)
{
    size_t arch_size = 0;

    arch_size = docker_seccomp_arches_count(docker_seccomp_spec);
    if (arch_size != 0) {
        size_t i;
        size_t j;
        if (arch_size > (SIZE_MAX / sizeof(char *))) {
            return -1;
        }
        oci_seccomp_spec->architectures = util_common_calloc_s(arch_size * sizeof(char *));
        if (oci_seccomp_spec->architectures == NULL) {
            return -1;
        }
        for (i = 0; i < docker_seccomp_spec->arch_map_len; i++) {
            oci_seccomp_spec->architectures[oci_seccomp_spec->architectures_len++] =
                util_strdup_s(docker_seccomp_spec->arch_map[i]->architecture);
            for (j = 0; j < docker_seccomp_spec->arch_map[i]->sub_architectures_len; j++) {
                oci_seccomp_spec->architectures[oci_seccomp_spec->architectures_len++] =
                    util_strdup_s(docker_seccomp_spec->arch_map[i]->sub_architectures[j]);
            }
        }
    }

    return 0;
}

static int dup_syscall_args_to_oci_spec(const docker_seccomp_syscalls_element *docker_syscall,
508
                                        defs_syscall *oci_syscall)
O
overweight 已提交
509 510 511 512 513 514 515
{
    size_t i = 0;

    if (docker_syscall->args_len == 0) {
        return 0;
    }

516
    if (docker_syscall->args_len > (SIZE_MAX / sizeof(defs_syscall_arg *))) {
O
overweight 已提交
517 518 519
        return -1;
    }

520
    oci_syscall->args = util_common_calloc_s(docker_syscall->args_len * sizeof(defs_syscall_arg *));
O
overweight 已提交
521 522 523 524
    if (oci_syscall->args == NULL) {
        return -1;
    }
    for (i = 0; i < docker_syscall->args_len; i++) {
525
        oci_syscall->args[i] = util_common_calloc_s(sizeof(defs_syscall_arg));
O
overweight 已提交
526 527 528
        if (oci_syscall->args[i] == NULL) {
            return -1;
        }
529
        defs_syscall_arg *args_element = oci_syscall->args[i];
O
overweight 已提交
530 531 532 533 534 535 536 537 538 539 540 541
        args_element->index = docker_syscall->args[i]->index;
        args_element->value = docker_syscall->args[i]->value;
        args_element->value_two = docker_syscall->args[i]->value_two;
        args_element->op = util_strdup_s(docker_syscall->args[i]->op);
        oci_syscall->args_len++;
    }

    return 0;
}

static int dup_syscall_to_oci_spec(const docker_seccomp *docker_seccomp_spec,
                                   oci_runtime_config_linux_seccomp *oci_seccomp_spec,
542
                                   const defs_process_capabilities *capabilites)
O
overweight 已提交
543 544 545 546
{
    int ret = 0;
    size_t i, j, k;
    size_t new_size, old_size;
547
    defs_syscall **tmp_syscalls = NULL;
O
overweight 已提交
548 549 550 551 552

    if (docker_seccomp_spec->syscalls_len == 0) {
        return 0;
    }

553
    if (docker_seccomp_spec->syscalls_len > (SIZE_MAX / sizeof(defs_syscall *))) {
O
overweight 已提交
554 555 556
        return -1;
    }

557
    oci_seccomp_spec->syscalls = util_common_calloc_s(docker_seccomp_spec->syscalls_len * sizeof(defs_syscall *));
O
overweight 已提交
558 559 560 561 562 563 564 565
    if (oci_seccomp_spec->syscalls == NULL) {
        return -1;
    }
    for (i = 0; i < docker_seccomp_spec->syscalls_len; i++) {
        if (!meet_filtering_rules(docker_seccomp_spec, docker_seccomp_spec->syscalls[i], capabilites)) {
            continue;
        }
        k = oci_seccomp_spec->syscalls_len;
566
        oci_seccomp_spec->syscalls[k] = util_common_calloc_s(sizeof(defs_syscall));
O
overweight 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
        if (oci_seccomp_spec->syscalls[k] == NULL) {
            return -1;
        }
        oci_seccomp_spec->syscalls_len++;

        if (docker_seccomp_spec->syscalls[i]->names_len > (SIZE_MAX / sizeof(char *))) {
            return -1;
        }

        oci_seccomp_spec->syscalls[k]->names =
            util_common_calloc_s(docker_seccomp_spec->syscalls[i]->names_len * sizeof(char *));
        if (oci_seccomp_spec->syscalls[k]->names == NULL) {
            return -1;
        }
        for (j = 0; j < docker_seccomp_spec->syscalls[i]->names_len; j++) {
            oci_seccomp_spec->syscalls[k]->names[j] = util_strdup_s(docker_seccomp_spec->syscalls[i]->names[j]);
            oci_seccomp_spec->syscalls[k]->names_len++;
        }
        oci_seccomp_spec->syscalls[k]->action = util_strdup_s(docker_seccomp_spec->syscalls[i]->action);
        if (dup_syscall_args_to_oci_spec(docker_seccomp_spec->syscalls[i], oci_seccomp_spec->syscalls[k])) {
            return -1;
        }
    }

591 592
    new_size = sizeof(defs_syscall *) * oci_seccomp_spec->syscalls_len;
    old_size = sizeof(defs_syscall *) * docker_seccomp_spec->syscalls_len;
O
overweight 已提交
593 594 595 596 597 598 599 600 601 602
    ret = mem_realloc((void **)&tmp_syscalls, new_size, oci_seccomp_spec->syscalls, old_size);
    if (ret < 0) {
        ERROR("Out of memory");
        return -1;
    }
    oci_seccomp_spec->syscalls = tmp_syscalls;

    return 0;
}

603 604 605
static oci_runtime_config_linux_seccomp *
trans_docker_seccomp_to_oci_format(const docker_seccomp *docker_seccomp_spec,
                                   const defs_process_capabilities *capabilites)
O
overweight 已提交
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
{
    oci_runtime_config_linux_seccomp *oci_seccomp_spec = NULL;

    oci_seccomp_spec = util_common_calloc_s(sizeof(oci_runtime_config_linux_seccomp));
    if (oci_seccomp_spec == NULL) {
        goto out;
    }

    // default action
    oci_seccomp_spec->default_action = util_strdup_s(docker_seccomp_spec->default_action);

    // architectures
    if (dup_architectures_to_oci_spec(docker_seccomp_spec, oci_seccomp_spec)) {
        goto out;
    }

    // syscalls
    if (dup_syscall_to_oci_spec(docker_seccomp_spec, oci_seccomp_spec, capabilites)) {
        goto out;
    }

    goto done;
out:
    if (oci_seccomp_spec != NULL) {
        free_oci_runtime_config_linux_seccomp(oci_seccomp_spec);
    }
    return NULL;

done:
    return oci_seccomp_spec;
}

638
int merge_default_seccomp_spec(oci_runtime_spec *oci_spec, const defs_process_capabilities *capabilites)
O
overweight 已提交
639 640 641 642 643 644 645 646 647 648 649
{
    oci_runtime_config_linux_seccomp *oci_seccomp_spec = NULL;
    docker_seccomp *docker_seccomp_spec = NULL;

    if (oci_spec->process == NULL || oci_spec->process->capabilities == NULL) {
        return 0;
    }

    docker_seccomp_spec = get_seccomp_security_opt_spec(SECCOMP_DEFAULT_PATH);
    if (docker_seccomp_spec == NULL) {
        ERROR("Failed to parse docker format seccomp specification file \"%s\"", SECCOMP_DEFAULT_PATH);
L
LiuHao 已提交
650
        isulad_set_error_message("failed to parse seccomp file: %s", SECCOMP_DEFAULT_PATH);
O
overweight 已提交
651 652 653 654 655 656
        return -1;
    }
    oci_seccomp_spec = trans_docker_seccomp_to_oci_format(docker_seccomp_spec, capabilites);
    free_docker_seccomp(docker_seccomp_spec);
    if (oci_seccomp_spec == NULL) {
        ERROR("Failed to trans docker format seccomp profile to oci standard");
L
LiuHao 已提交
657
        isulad_set_error_message("Failed to trans docker format seccomp profile to oci standard");
O
overweight 已提交
658 659 660 661 662 663 664 665
        return -1;
    }

    oci_spec->linux->seccomp = oci_seccomp_spec;

    return 0;
}

666
static int append_systemcall_to_seccomp(oci_runtime_config_linux_seccomp *seccomp, defs_syscall *element)
O
overweight 已提交
667 668 669
{
    int nret = 0;
    size_t old_size, new_size;
670
    defs_syscall **tmp_syscalls = NULL;
O
overweight 已提交
671 672 673 674
    if (seccomp == NULL || element == NULL) {
        return -1;
    }

675
    if (seccomp->syscalls_len > SIZE_MAX / sizeof(defs_syscall *) - 1) {
O
overweight 已提交
676 677 678
        CRIT("Too many syscalls to append!");
        return -1;
    }
679 680
    new_size = (seccomp->syscalls_len + 1) * sizeof(defs_syscall *);
    old_size = new_size - sizeof(defs_syscall *);
O
overweight 已提交
681 682 683 684 685 686 687 688 689 690 691
    nret = mem_realloc((void **)&tmp_syscalls, new_size, seccomp->syscalls, old_size);
    if (nret < 0) {
        CRIT("Memory allocation error.");
        return -1;
    }
    tmp_syscalls[seccomp->syscalls_len++] = element;
    seccomp->syscalls = tmp_syscalls;

    return 0;
}

692 693
static defs_syscall *make_seccomp_syscalls_element(const char **names, size_t names_len, const char *action,
                                                   size_t args_len, defs_syscall_arg **args)
O
overweight 已提交
694 695
{
    size_t i = 0;
696 697
    defs_syscall *ret = NULL;
    ret = util_common_calloc_s(sizeof(defs_syscall));
O
overweight 已提交
698 699 700 701 702 703 704
    if (ret == NULL) {
        CRIT("Memory allocation error.");
        goto out;
    }
    ret->action = util_strdup_s(action ? action : "");
    ret->args_len = args_len;
    if (args_len) {
705
        if (args_len > SIZE_MAX / sizeof(defs_syscall_arg *)) {
O
overweight 已提交
706 707 708
            CRIT("Too many seccomp syscalls!");
            goto out;
        }
709
        ret->args = util_common_calloc_s(args_len * sizeof(defs_syscall_arg *));
O
overweight 已提交
710 711 712 713 714
        if (ret->args == NULL) {
            CRIT("Memory allocation error.");
            goto out;
        }
        for (i = 0; i < args_len; i++) {
715
            ret->args[i] = util_common_calloc_s(sizeof(defs_syscall_arg));
O
overweight 已提交
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
            if (ret->args[i] == NULL) {
                CRIT("Memory allocation error.");
                goto out;
            }
            ret->args[i]->index = args[i]->index;
            ret->args[i]->value = args[i]->value;
            ret->args[i]->value_two = args[i]->value_two;
            ret->args[i]->op = util_strdup_s(args[i]->op);
        }
    }

    ret->names_len = names_len;
    if (names_len > SIZE_MAX / sizeof(char *)) {
        CRIT("Too many syscalls!");
        goto out;
    }
    ret->names = util_common_calloc_s(names_len * sizeof(char *));
    if (ret->names == NULL) {
        CRIT("Memory allocation error.");
        goto out;
    }
    for (i = 0; i < names_len; i++) {
        ret->names[i] = util_strdup_s(names[i]);
    }

    return ret;

out:
744
    free_defs_syscall(ret);
O
overweight 已提交
745 746 747 748 749 750 751 752 753 754 755 756
    ret = NULL;
    return ret;
}

static int make_sure_oci_spec_process_capabilities(oci_runtime_spec *oci_spec)
{
    int ret = make_sure_oci_spec_process(oci_spec);
    if (ret < 0) {
        return -1;
    }

    if (oci_spec->process->capabilities == NULL) {
757
        oci_spec->process->capabilities = util_common_calloc_s(sizeof(defs_process_capabilities));
O
overweight 已提交
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
        if (oci_spec->process->capabilities == NULL) {
            return -1;
        }
    }
    return 0;
}

int merge_caps(oci_runtime_spec *oci_spec, const char **adds, size_t adds_len, const char **drops, size_t drops_len)
{
    int ret = 0;

    if (adds == NULL && drops == NULL) {
        return 0;
    }

    ret = make_sure_oci_spec_process_capabilities(oci_spec);
    if (ret < 0) {
        goto out;
    }

    if (adds_len > LIST_SIZE_MAX || drops_len > LIST_SIZE_MAX) {
H
haozi007 已提交
779
        ERROR("Too many capabilities to add or drop, the limit is %lld", LIST_SIZE_MAX);
L
LiuHao 已提交
780
        isulad_set_error_message("Too many capabilities to add or drop, the limit is %d", LIST_SIZE_MAX);
O
overweight 已提交
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 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
        ret = -1;
        goto out;
    }

    ret = tweak_capabilities(&oci_spec->process->capabilities->bounding, &oci_spec->process->capabilities->bounding_len,
                             adds, adds_len, drops, drops_len);
    if (ret != 0) {
        ERROR("Failed to tweak capabilities");
        ret = -1;
        goto out;
    }

out:
    return ret;
}

static int make_sure_oci_spec_linux_sysctl(oci_runtime_spec *oci_spec)
{
    int ret = 0;

    ret = make_sure_oci_spec_linux(oci_spec);
    if (ret < 0) {
        return -1;
    }

    if (oci_spec->linux->sysctl == NULL) {
        oci_spec->linux->sysctl = util_common_calloc_s(sizeof(json_map_string_string));
        if (oci_spec->linux->sysctl == NULL) {
            return -1;
        }
    }
    return 0;
}

int merge_sysctls(oci_runtime_spec *oci_spec, const json_map_string_string *sysctls)
{
    int ret = 0;
    size_t i;

    if (sysctls == NULL || sysctls->len == 0) {
        return 0;
    }

    ret = make_sure_oci_spec_linux_sysctl(oci_spec);
    if (ret < 0) {
        goto out;
    }

    if (sysctls->len > LIST_SIZE_MAX) {
H
haozi007 已提交
830
        ERROR("Too many sysctls to add, the limit is %lld", LIST_SIZE_MAX);
L
LiuHao 已提交
831
        isulad_set_error_message("Too many sysctls to add, the limit is %d", LIST_SIZE_MAX);
O
overweight 已提交
832 833 834 835 836 837 838 839 840 841 842 843 844 845
        ret = -1;
        goto out;
    }

    for (i = 0; i < sysctls->len; i++) {
        if (append_json_map_string_string(oci_spec->linux->sysctl, sysctls->keys[i], sysctls->values[i]) != 0) {
            ERROR("Append string failed");
            goto out;
        }
    }
out:
    return ret;
}

W
wujing 已提交
846
int merge_no_new_privileges(oci_runtime_spec *oci_spec, bool value)
O
overweight 已提交
847 848 849 850 851 852 853 854
{
    int ret = 0;

    ret = make_sure_oci_spec_process(oci_spec);
    if (ret < 0) {
        goto out;
    }

W
wujing 已提交
855 856 857 858 859 860
    oci_spec->process->no_new_privileges = value;

out:
    return ret;
}

861 862
int merge_selinux(oci_runtime_spec *oci_spec, container_config_v2_common_config *v2_spec, const char **label_opts,
                  size_t label_opts_len)
W
wujing 已提交
863 864 865 866 867 868 869
{
    char *process_label = NULL;
    char *mount_label = NULL;

    int ret = make_sure_oci_spec_process(oci_spec);
    if (ret < 0) {
        goto out;
O
overweight 已提交
870 871
    }

W
wujing 已提交
872 873 874 875 876 877 878 879 880 881 882 883 884 885
    if (init_label(label_opts, label_opts_len, &process_label, &mount_label) != 0) {
        ERROR("Failed to append label");
        ret = -1;
        goto out;
    }

    if (mount_label != NULL) {
        oci_spec->linux->mount_label = util_strdup_s(mount_label);
        v2_spec->mount_label = util_strdup_s(mount_label);
    }

    if (process_label != NULL) {
        oci_spec->process->selinux_label = util_strdup_s(process_label);
        v2_spec->process_label = util_strdup_s(process_label);
O
overweight 已提交
886 887 888
    }

out:
W
wujing 已提交
889 890
    free(process_label);
    free(mount_label);
O
overweight 已提交
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
    return ret;
}

static int get_adds_cap_for_system_container(const host_config *host_spec, char ***adds, size_t *adds_len)
{
    size_t i = 0;
    int ret = 0;
    char **drops = NULL;
    size_t drops_len = 0;
    size_t system_caps_len = sizeof(g_system_caps) / sizeof(char *);

    if (host_spec == NULL || adds == NULL || adds_len == NULL) {
        return -1;
    }

    if (host_spec->cap_drop != NULL) {
        drops = host_spec->cap_drop;
        drops_len = host_spec->cap_drop_len;
    }

    // if cap_drop in g_system_caps, move it from g_system_caps
    for (i = 0; i < system_caps_len; i++) {
        if (!strings_in_slice((const char **)drops, drops_len, g_system_caps[i])) {
            ret = append_capability(adds, adds_len, g_system_caps[i]);
            if (ret != 0) {
                ERROR("Failed to append capabilities");
                ret = -1;
                goto out;
            }
        }
    }

out:
    return ret;
}

static void free_adds_cap_for_system_container(char **adds, size_t adds_len)
{
    size_t i = 0;

    if (adds == NULL) {
        return;
    }

    for (i = 0; i < adds_len; i++) {
        free(adds[i]);
    }
    free(adds);
}

static int make_sure_oci_spec_linux_seccomp(oci_runtime_spec *oci_spec)
{
    int ret = 0;

    ret = make_sure_oci_spec_linux(oci_spec);
    if (ret < 0) {
        return -1;
    }

    if (oci_spec->linux->seccomp == NULL) {
        oci_spec->linux->seccomp = util_common_calloc_s(sizeof(oci_runtime_config_linux_seccomp));
        if (oci_spec->linux->seccomp == NULL) {
            return -1;
        }
    }

    return 0;
}

int adapt_settings_for_system_container(oci_runtime_spec *oci_spec, const host_config *host_spec)
{
    int ret = 0;
    char *unblocked_systemcall_for_system_container[] = { "mount", "umount2", "reboot", "name_to_handle_at",
                                                          "unshare"
                                                        };
    char **adds = NULL;
    size_t adds_len = 0;
968 969 970 971
    bool no_new_privileges = false;
    char **label_opts = NULL;
    size_t label_opts_len = 0;
    char *seccomp_profile = NULL;
O
overweight 已提交
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986

    ret = get_adds_cap_for_system_container(host_spec, &adds, &adds_len);
    if (ret != 0) {
        ERROR("Failed to get adds cap for system container");
        ret = -1;
        goto out;
    }

    ret = merge_caps(oci_spec, (const char **)adds, adds_len, NULL, 0);
    if (ret != 0) {
        ERROR("Failed to merge capabilities");
        ret = -1;
        goto out;
    }

987
    ret = parse_security_opt(host_spec, &no_new_privileges, &label_opts, &label_opts_len, &seccomp_profile);
988 989 990 991
    if (ret != 0) {
        ERROR("Failed to parse security opt");
        goto out;
    }
992 993
    /* do not append to seccomp if seccomp profile unconfined */
    if (seccomp_profile != NULL && strcmp(seccomp_profile, "unconfined") == 0) {
994 995
        goto out;
    }
996 997 998 999 1000 1001 1002 1003 1004 1005 1006

    ret = make_sure_oci_spec_linux(oci_spec);
    if (ret < 0) {
        goto out;
    }

    ret = make_sure_oci_spec_linux_seccomp(oci_spec);
    if (ret < 0) {
        goto out;
    }

O
overweight 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
    ret = append_systemcall_to_seccomp(
              oci_spec->linux->seccomp,
              make_seccomp_syscalls_element((const char **)unblocked_systemcall_for_system_container,
                                            sizeof(unblocked_systemcall_for_system_container) /
                                            sizeof(unblocked_systemcall_for_system_container[0]),
                                            "SCMP_ACT_ALLOW", 0, NULL));
    if (ret != 0) {
        ERROR("Failed to append systemcall to seccomp file");
        ret = -1;
        goto out;
    }
out:
1019 1020
    util_free_array(label_opts);
    free(seccomp_profile);
O
overweight 已提交
1021 1022 1023 1024
    free_adds_cap_for_system_container(adds, adds_len);
    return ret;
}

W
wujing 已提交
1025
int merge_seccomp(oci_runtime_spec *oci_spec, const char *seccomp_profile)
O
overweight 已提交
1026 1027 1028 1029 1030
{
    int ret = 0;
    parser_error err = NULL;
    docker_seccomp *docker_seccomp = NULL;

W
wujing 已提交
1031
    if (seccomp_profile == NULL) {
O
overweight 已提交
1032 1033 1034
        return 0;
    }

W
wujing 已提交
1035 1036
    ret = make_sure_oci_spec_linux(oci_spec);
    if (ret < 0) {
O
overweight 已提交
1037 1038
        goto out;
    }
W
wujing 已提交
1039 1040 1041
    // free default seccomp
    free_oci_runtime_config_linux_seccomp(oci_spec->linux->seccomp);
    oci_spec->linux->seccomp = NULL;
O
overweight 已提交
1042

W
wujing 已提交
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
    if (strcmp(seccomp_profile, "unconfined") == 0) {
        goto out;
    }
    docker_seccomp = docker_seccomp_parse_data((const char *)seccomp_profile, NULL, &err);
    if (docker_seccomp == NULL) {
        ERROR("Failed to parse host config data:%s", err);
        ret = -1;
        goto out;
    }
    oci_spec->linux->seccomp = trans_docker_seccomp_to_oci_format(docker_seccomp, oci_spec->process->capabilities);
    if (oci_spec->linux->seccomp == NULL) {
        ERROR("Failed to trans docker seccomp format to oci profile");
        ret = -1;
        goto out;
O
overweight 已提交
1057 1058 1059 1060 1061 1062 1063
    }

out:
    free(err);
    free_docker_seccomp(docker_seccomp);
    return ret;
}