los_container.c 13.0 KB
Newer Older
Z
zhushengle 已提交
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
/*
 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "los_container_pri.h"
#include "los_process_pri.h"
Z
zhushengle 已提交
32
#include "internal.h"
Z
zhushengle 已提交
33 34 35
#ifdef LOSCFG_KERNEL_CONTAINER

STATIC Container g_rootContainer;
36 37 38 39 40 41
STATIC Atomic g_containerCount = 0xF0000000U;

UINT32 OsAllocContainerID(VOID)
{
    return LOS_AtomicIncRet(&g_containerCount);
}
Z
zhushengle 已提交
42 43 44 45 46 47

VOID OsContainerInitSystemProcess(LosProcessCB *processCB)
{
    processCB->container = &g_rootContainer;
    LOS_AtomicInc(&processCB->container->rc);
#ifdef LOSCFG_PID_CONTAINER
Z
zhushengle 已提交
48
    (VOID)OsAllocSpecifiedVpidUnsafe(processCB->processID, processCB->container->pidContainer, processCB, NULL);
Z
zhushengle 已提交
49 50 51 52 53 54 55
#endif
    return;
}

VOID OsInitRootContainer(VOID)
{
#ifdef LOSCFG_PID_CONTAINER
Z
zhushengle 已提交
56
    (VOID)OsInitRootPidContainer(&g_rootContainer.pidContainer);
Z
zhushengle 已提交
57
    g_rootContainer.pidForChildContainer = g_rootContainer.pidContainer;
Z
zhushengle 已提交
58 59 60
#endif
#ifdef LOSCFG_UTS_CONTAINER
    (VOID)OsInitRootUtsContainer(&g_rootContainer.utsContainer);
61 62 63
#endif
#ifdef LOSCFG_MNT_CONTAINER
    (VOID)OsInitRootMntContainer(&g_rootContainer.mntContainer);
Z
zhushengle 已提交
64 65 66
#endif
#ifdef LOSCFG_IPC_CONTAINER
    (VOID)OsInitRootIpcContainer(&g_rootContainer.ipcContainer);
Z
zhushengle 已提交
67 68 69 70
#endif
#ifdef LOSCFG_TIME_CONTAINER
    (VOID)OsInitRootTimeContainer(&g_rootContainer.timeContainer);
    g_rootContainer.timeForChildContainer = g_rootContainer.timeContainer;
Z
zhushengle 已提交
71 72 73 74 75 76
#endif
    return;
}

STATIC INLINE Container *CreateContainer(VOID)
{
Z
zhushengle 已提交
77
    Container *container = (Container *)LOS_MemAlloc(m_aucSysMem1, sizeof(Container));
Z
zhushengle 已提交
78 79 80 81 82 83 84 85 86 87
    if (container == NULL) {
        return NULL;
    }

    (VOID)memset_s(container, sizeof(Container), 0, sizeof(Container));

    LOS_AtomicInc(&container->rc);
    return container;
}

Z
zhushengle 已提交
88
STATIC UINT32 CopyContainers(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent, UINT32 *processID)
Z
zhushengle 已提交
89 90 91 92 93 94 95 96 97
{
    UINT32 ret = LOS_OK;
    /* Pid container initialization must precede other container initialization. */
#ifdef LOSCFG_PID_CONTAINER
    ret = OsCopyPidContainer(flags, child, parent, processID);
    if (ret != LOS_OK) {
        return ret;
    }
#endif
Z
zhushengle 已提交
98 99 100 101 102
#ifdef LOSCFG_UTS_CONTAINER
    ret = OsCopyUtsContainer(flags, child, parent);
    if (ret != LOS_OK) {
        return ret;
    }
103 104 105 106 107 108
#endif
#ifdef LOSCFG_MNT_CONTAINER
    ret = OsCopyMntContainer(flags, child, parent);
    if (ret != LOS_OK) {
        return ret;
    }
Z
zhushengle 已提交
109 110 111 112 113 114
#endif
#ifdef LOSCFG_IPC_CONTAINER
    ret = OsCopyIpcContainer(flags, child, parent);
    if (ret != LOS_OK) {
        return ret;
    }
Z
zhushengle 已提交
115 116 117 118 119 120
#endif
#ifdef LOSCFG_TIME_CONTAINER
    ret = OsCopyTimeContainer(flags, child, parent);
    if (ret != LOS_OK) {
        return ret;
    }
Z
zhushengle 已提交
121
#endif
Z
zhushengle 已提交
122 123 124
    return ret;
}

Z
zhushengle 已提交
125 126 127 128 129 130 131 132
/*
 * called from clone.  This now handles copy for Container and all
 * namespaces therein.
 */
UINT32 OsCopyContainers(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent, UINT32 *processID)
{
    UINT32 intSave;

Z
zhushengle 已提交
133 134 135 136
#ifdef LOSCFG_TIME_CONTAINER
    flags &= ~CLONE_NEWTIME;
#endif

Z
zhushengle 已提交
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
    if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWPID | CLONE_NEWNET | CLONE_NEWTIME))) {
#ifdef LOSCFG_PID_CONTAINER
        if (parent->container->pidContainer != parent->container->pidForChildContainer) {
            goto CREATE_CONTAINER;
        }
#endif
#ifdef LOSCFG_TIME_CONTAINER
        if (parent->container->timeContainer != parent->container->timeForChildContainer) {
            goto CREATE_CONTAINER;
        }
#endif
        SCHEDULER_LOCK(intSave);
        child->container = parent->container;
        LOS_AtomicInc(&child->container->rc);
        SCHEDULER_UNLOCK(intSave);
        goto COPY_CONTAINERS;
    }

#if defined(LOSCFG_PID_CONTAINER) || defined(LOSCFG_TIME_CONTAINER)
CREATE_CONTAINER:
#endif
    child->container = CreateContainer();
    if (child->container == NULL) {
        return ENOMEM;
    }

COPY_CONTAINERS:
    return CopyContainers(flags, child, parent, processID);
}

Z
zhushengle 已提交
167
VOID OsContainerFree(LosProcessCB *processCB)
Z
zhushengle 已提交
168 169 170 171 172 173 174 175
{
    LOS_AtomicDec(&processCB->container->rc);
    if (LOS_AtomicRead(&processCB->container->rc) == 0) {
        (VOID)LOS_MemFree(m_aucSysMem1, processCB->container);
        processCB->container = NULL;
    }
}

Z
zhushengle 已提交
176 177 178 179
VOID OsContainersDestroy(LosProcessCB *processCB)
{
   /* All processes in the container must be destroyed before the container is destroyed. */
#ifdef LOSCFG_PID_CONTAINER
180
    if (processCB->processID == OS_USER_ROOT_PROCESS_ID) {
Z
zhushengle 已提交
181
        OsPidContainerDestroyAllProcess(processCB);
Z
zhushengle 已提交
182 183 184
    }
#endif

Z
zhushengle 已提交
185
#ifdef LOSCFG_UTS_CONTAINER
Z
zhushengle 已提交
186
    OsUtsContainerDestroy(processCB->container);
Z
zhushengle 已提交
187 188
#endif

189
#ifdef LOSCFG_MNT_CONTAINER
Z
zhushengle 已提交
190
    OsMntContainerDestroy(processCB->container);
191 192
#endif

Z
zhushengle 已提交
193
#ifdef LOSCFG_IPC_CONTAINER
Z
zhushengle 已提交
194
    OsIpcContainerDestroy(processCB->container);
Z
zhushengle 已提交
195 196
#endif

Z
zhushengle 已提交
197
#ifdef LOSCFG_TIME_CONTAINER
Z
zhushengle 已提交
198
    OsTimeContainerDestroy(processCB->container);
Z
zhushengle 已提交
199 200
#endif

Z
zhushengle 已提交
201
#ifndef LOSCFG_PID_CONTAINER
Z
zhushengle 已提交
202 203 204 205
    UINT32 intSave;
    SCHEDULER_LOCK(intSave);
    OsContainerFree(processCB);
    SCHEDULER_UNLOCK(intSave);
Z
zhushengle 已提交
206 207
#endif
}
208

Z
zhushengle 已提交
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
STATIC VOID DeInitContainers(UINT32 flags, Container *container, LosProcessCB *processCB)
{
    UINT32 intSave;
    if (container == NULL) {
        return;
    }

#ifdef LOSCFG_PID_CONTAINER
    SCHEDULER_LOCK(intSave);
    if ((flags & CLONE_NEWPID) != 0) {
        OsPidContainerDestroy(container, processCB);
    } else {
        OsPidContainerDestroy(container, NULL);
    }
    SCHEDULER_UNLOCK(intSave);
#endif

#ifdef LOSCFG_UTS_CONTAINER
    OsUtsContainerDestroy(container);
#endif
#ifdef LOSCFG_MNT_CONTAINER
    OsMntContainerDestroy(container);
#endif
#ifdef LOSCFG_IPC_CONTAINER
    OsIpcContainerDestroy(container);
#endif

#ifdef LOSCFG_TIME_CONTAINER
    OsTimeContainerDestroy(container);
#endif

    SCHEDULER_LOCK(intSave);
    LOS_AtomicDec(&container->rc);
    if (LOS_AtomicRead(&container->rc) == 0) {
        (VOID)LOS_MemFree(m_aucSysMem1, container);
    }
    SCHEDULER_UNLOCK(intSave);
}

248 249 250 251 252 253 254
UINT32 OsGetContainerID(Container *container, ContainerType type)
{
    if (container == NULL) {
        return OS_INVALID_VALUE;
    }

    switch (type) {
Z
zhushengle 已提交
255
#ifdef LOSCFG_PID_CONTAINER
256 257
        case PID_CONTAINER:
            return OsGetPidContainerID(container->pidContainer);
Z
zhushengle 已提交
258 259
        case PID_CHILD_CONTAINER:
            return OsGetPidContainerID(container->pidForChildContainer);
Z
zhushengle 已提交
260 261
#endif
#ifdef LOSCFG_UTS_CONTAINER
262 263
        case UTS_CONTAINER:
            return OsGetUtsContainerID(container->utsContainer);
Z
zhushengle 已提交
264 265
#endif
#ifdef LOSCFG_MNT_CONTAINER
266 267
        case MNT_CONTAINER:
            return OsGetMntContainerID(container->mntContainer);
Z
zhushengle 已提交
268 269 270 271
#endif
#ifdef LOSCFG_IPC_CONTAINER
        case IPC_CONTAINER:
            return OsGetIpcContainerID(container->ipcContainer);
Z
zhushengle 已提交
272 273 274 275 276 277
#endif
#ifdef LOSCFG_TIME_CONTAINER
        case TIME_CONTAINER:
            return OsGetTimeContainerID(container->timeContainer);
        case TIME_CHILD_CONTAINER:
            return OsGetTimeContainerID(container->timeForChildContainer);
Z
zhushengle 已提交
278
#endif
279 280 281 282 283
        default:
            break;
    }
    return OS_INVALID_VALUE;
}
Z
zhushengle 已提交
284

Z
zhushengle 已提交
285
STATIC UINT32 UnshareCreateNewContainers(UINT32 flags, LosProcessCB *curr, Container *newContainer)
Z
zhushengle 已提交
286
{
Z
zhushengle 已提交
287
    UINT32 ret = LOS_OK;
Z
zhushengle 已提交
288
#ifdef LOSCFG_PID_CONTAINER
Z
zhushengle 已提交
289 290 291 292
    ret = OsUnsharePidContainer(flags, curr, newContainer);
    if (ret != LOS_OK) {
        return ret;
    }
Z
zhushengle 已提交
293
#endif
Z
zhushengle 已提交
294
#ifdef LOSCFG_UTS_CONTAINER
Z
zhushengle 已提交
295 296 297
    ret = OsUnshareUtsContainer(flags, curr, newContainer);
    if (ret != LOS_OK) {
        return ret;
Z
zhushengle 已提交
298 299 300
    }
#endif
#ifdef LOSCFG_MNT_CONTAINER
Z
zhushengle 已提交
301 302 303
    ret = OsUnshareMntContainer(flags, curr, newContainer);
    if (ret != LOS_OK) {
        return ret;
Z
zhushengle 已提交
304 305 306
    }
#endif
#ifdef LOSCFG_IPC_CONTAINER
Z
zhushengle 已提交
307 308 309
    ret = OsUnshareIpcContainer(flags, curr, newContainer);
    if (ret != LOS_OK) {
        return ret;
Z
zhushengle 已提交
310 311 312
    }
#endif
#ifdef LOSCFG_TIME_CONTAINER
Z
zhushengle 已提交
313 314 315 316
    ret = OsUnshareTimeContainer(flags, curr, newContainer);
    if (ret != LOS_OK) {
        return ret;
    }
Z
zhushengle 已提交
317
#endif
Z
zhushengle 已提交
318 319
    return ret;
}
Z
zhushengle 已提交
320

Z
zhushengle 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
INT32 OsUnshare(UINT32 flags)
{
    UINT32 ret;
    UINT32 intSave;
    LosProcessCB *curr = OsCurrProcessGet();
    Container *oldContainer = curr->container;
    UINT32 unshareFlags = CLONE_NEWPID | CLONE_NEWTIME | CLONE_NEWUTS | CLONE_NEWNS | CLONE_NEWIPC;

    if (!(flags & unshareFlags) || ((flags & (~unshareFlags)) != 0)) {
        return -EINVAL;
    }

    Container *newContainer = CreateContainer();
    if (newContainer == NULL) {
        return -ENOMEM;
Z
zhushengle 已提交
336
    }
Z
zhushengle 已提交
337 338 339 340 341 342 343 344 345

    ret = UnshareCreateNewContainers(flags, curr, newContainer);
    if (ret != LOS_OK) {
        goto EXIT;
    }

    SCHEDULER_LOCK(intSave);
    oldContainer = curr->container;
    curr->container = newContainer;
Z
zhushengle 已提交
346
    SCHEDULER_UNLOCK(intSave);
Z
zhushengle 已提交
347 348 349 350 351 352 353

    DeInitContainers(flags, oldContainer, NULL);
    return LOS_OK;

EXIT:
    DeInitContainers(flags, newContainer, NULL);
    return -ret;
Z
zhushengle 已提交
354 355
}

Z
zhushengle 已提交
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
STATIC UINT32 SetNsGetFlagByContainerType(UINT32 containerType)
{
    if (containerType >= (UINT32)CONTAINER_MAX) {
        return 0;
    }
    ContainerType type = (ContainerType)containerType;
    switch (type) {
        case PID_CONTAINER:
        case PID_CHILD_CONTAINER:
            return CLONE_NEWPID;
        case UTS_CONTAINER:
            return CLONE_NEWUTS;
        case MNT_CONTAINER:
            return CLONE_NEWNS;
        case IPC_CONTAINER:
            return CLONE_NEWIPC;
        case TIME_CONTAINER:
        case TIME_CHILD_CONTAINER:
            return CLONE_NEWTIME;
        default:
            break;
    }
    return 0;
}

STATIC UINT32 SetNsCreateNewContainers(UINT32 flags, Container *newContainer, Container *container)
Z
zhushengle 已提交
382 383 384
{
    UINT32 ret = LOS_OK;
#ifdef LOSCFG_PID_CONTAINER
Z
zhushengle 已提交
385
    ret = OsSetNsPidContainer(flags, container, newContainer);
Z
zhushengle 已提交
386 387 388 389 390
    if (ret != LOS_OK) {
        return ret;
    }
#endif
#ifdef LOSCFG_UTS_CONTAINER
Z
zhushengle 已提交
391
    ret = OsSetNsUtsContainer(flags, container, newContainer);
Z
zhushengle 已提交
392 393 394 395 396
    if (ret != LOS_OK) {
        return ret;
    }
#endif
#ifdef LOSCFG_MNT_CONTAINER
Z
zhushengle 已提交
397
    ret = OsSetNsMntContainer(flags, container, newContainer);
Z
zhushengle 已提交
398 399 400 401 402
    if (ret != LOS_OK) {
        return ret;
    }
#endif
#ifdef LOSCFG_IPC_CONTAINER
Z
zhushengle 已提交
403
    ret = OsSetNsIpcContainer(flags, container, newContainer);
Z
zhushengle 已提交
404 405 406 407 408
    if (ret != LOS_OK) {
        return ret;
    }
#endif
#ifdef LOSCFG_TIME_CONTAINER
Z
zhushengle 已提交
409
    ret = OsSetNsTimeContainer(flags, container, newContainer);
Z
zhushengle 已提交
410 411 412 413
    if (ret != LOS_OK) {
        return ret;
    }
#endif
Z
zhushengle 已提交
414
    return LOS_OK;
Z
zhushengle 已提交
415 416
}

Z
zhushengle 已提交
417
INT32 OsSetNs(INT32 fd, INT32 type)
Z
zhushengle 已提交
418 419
{
    UINT32 intSave;
Z
zhushengle 已提交
420 421 422
    UINT32 typeMask = CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWIPC | CLONE_NEWTIME;
    UINT32 containerType = 0;
    UINT32 flag = (UINT32)(type & typeMask);
Z
zhushengle 已提交
423
    LosProcessCB *curr = OsCurrProcessGet();
Z
zhushengle 已提交
424

Z
zhushengle 已提交
425
    if (((type & (~typeMask)) != 0)) {
Z
zhushengle 已提交
426 427 428 429 430 431 432 433
        return -EINVAL;
    }

    Container *newContainer = CreateContainer();
    if (newContainer == NULL) {
        return -ENOMEM;
    }

Z
zhushengle 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
    LosProcessCB *processCB = (LosProcessCB *)ProcfsContainerGet(fd, &containerType);
    if (processCB == NULL) {
        (VOID)LOS_MemFree(m_aucSysMem1, newContainer);
        return -EBADF;
    }

    SCHEDULER_LOCK(intSave);
    Container *targetContainer = processCB->container;
    if (targetContainer == NULL) {
        SCHEDULER_UNLOCK(intSave);
        return -EBADF;
    }

    if (flag == 0) {
        flag = SetNsGetFlagByContainerType(containerType);
    }

    if ((flag == 0) || (flag != SetNsGetFlagByContainerType(containerType)) || (targetContainer == curr->container)) {
        SCHEDULER_UNLOCK(intSave);
        return -EBADF;
    }

    UINT32 ret = SetNsCreateNewContainers(flag, newContainer, targetContainer);
Z
zhushengle 已提交
457
    if (ret != LOS_OK) {
Z
zhushengle 已提交
458
        SCHEDULER_UNLOCK(intSave);
Z
zhushengle 已提交
459 460 461
        goto EXIT;
    }

Z
zhushengle 已提交
462
    Container *oldContainer = curr->container;
Z
zhushengle 已提交
463 464
    curr->container = newContainer;
    SCHEDULER_UNLOCK(intSave);
Z
zhushengle 已提交
465
    DeInitContainers(flag, oldContainer, NULL);
Z
zhushengle 已提交
466 467 468
    return LOS_OK;

EXIT:
Z
zhushengle 已提交
469 470
    DeInitContainers(flag, newContainer, curr);
    return ret;
Z
zhushengle 已提交
471
}
Z
zhushengle 已提交
472
#endif