los_user_container.c 11.4 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 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
/*
 * 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_user_container_pri.h"
#include "errno.h"
#include "ctype.h"
#include "los_config.h"
#include "los_memory.h"
#include "proc_fs.h"
#include "user_copy.h"
#include "los_seq_buf.h"
#include "capability_type.h"
#include "capability_api.h"
#include "internal.h"

#define DEFAULT_OVERFLOWUID    65534
#define DEFAULT_OVERFLOWGID    65534
#define LEVEL_MAX 3
#define HEX 16
#define OCT 8
#define DEC 10

#ifdef LOSCFG_USER_CONTAINER
UINT32 g_currentUserContainerNum = 1;

UINT32 OsCreateUserContainer(Credentials *newCredentials, UserContainer *parentUserContainer)
{
    if ((parentUserContainer != NULL) && (parentUserContainer->level >= LEVEL_MAX)) {
        return EINVAL;
    }

    if ((newCredentials->euid < 0) || (newCredentials->egid < 0)) {
        return EINVAL;
    }

    UserContainer *userContainer = LOS_MemAlloc(m_aucSysMem1, sizeof(UserContainer));
    if (userContainer == NULL) {
        return ENOMEM;
    }
    (VOID)memset_s(userContainer, sizeof(UserContainer), 0, sizeof(UserContainer));

    g_currentUserContainerNum++;
    userContainer->containerID = OsAllocContainerID();
    userContainer->parent = parentUserContainer;
    newCredentials->userContainer = userContainer;
    if (parentUserContainer != NULL) {
        LOS_AtomicInc(&parentUserContainer->rc);
        LOS_AtomicSet(&userContainer->rc, 1);
        userContainer->level = parentUserContainer->level + 1;
        userContainer->owner = newCredentials->euid;
        userContainer->group = newCredentials->egid;
    } else {
        LOS_AtomicSet(&userContainer->rc, 3); /* 3: Three system processes */
        userContainer->uidMap.extentCount = 1;
        userContainer->uidMap.extent[0].count = 4294967295U;
        userContainer->gidMap.extentCount = 1;
        userContainer->gidMap.extent[0].count = 4294967295U;
    }
    return LOS_OK;
}

VOID FreeUserContainer(UserContainer *userContainer)
{
    UserContainer *parent = NULL;
    do {
        parent = userContainer->parent;
        (VOID)LOS_MemFree(m_aucSysMem1, userContainer);
        userContainer->parent = NULL;
        userContainer = parent;
        g_currentUserContainerNum--;
    } while ((userContainer != NULL) && (LOS_AtomicRead(&userContainer->rc) <= 0));
}

STATIC UidGidExtent *MapIdUpBase(UINT32 extents, UidGidMap *map, UINT32 id)
{
    UINT32 idx;
    UINT32 first;
    UINT32 last;

    for (idx = 0; idx < extents; idx++) {
        first = map->extent[idx].lowerFirst;
        last = first + map->extent[idx].count - 1;
        if ((id >= first) && (id <= last)) {
            return &map->extent[idx];
        }
    }
    return NULL;
}

STATIC UINT32 MapIdUp(UidGidMap *map, UINT32 id)
{
    UINT32 extents = map->extentCount;
    if (extents > UID_GID_MAP_MAX_EXTENTS) {
        return (UINT32)-1;
    }

    UidGidExtent *extent = MapIdUpBase(extents, map, id);
    if (extent != NULL) {
        return ((id - extent->lowerFirst) + extent->first);
    }

    return (UINT32)-1;
}

UINT32 FromKuid(UserContainer *userContainer, UINT32 kuid)
{
    return MapIdUp(&userContainer->uidMap, kuid);
}

UINT32 FromKgid(UserContainer *userContainer, UINT32 kgid)
{
    return MapIdUp(&userContainer->gidMap, kgid);
}

UINT32 OsFromKuidMunged(UserContainer *userContainer, UINT32 kuid)
{
    UINT32 uid = FromKuid(userContainer, kuid);
    if (uid == (UINT32)-1) {
        uid = DEFAULT_OVERFLOWUID;
    }
    return uid;
}

UINT32 OsFromKgidMunged(UserContainer *userContainer, UINT32 kgid)
{
    UINT32 gid = FromKgid(userContainer, kgid);
    if (gid == (UINT32)-1) {
        gid = DEFAULT_OVERFLOWGID;
    }
    return gid;
}

STATIC UidGidExtent *MapIdRangeDownBase(UINT32 extents, UidGidMap *map, UINT32 id, UINT32 count)
{
    UINT32 idx;
    UINT32 first;
    UINT32 last;
    UINT32 id2;

    id2 = id + count - 1;
    for (idx = 0; idx < extents; idx++) {
        first = map->extent[idx].first;
        last = first + map->extent[idx].count - 1;
        if ((id >= first && id <= last) && (id2 >= first && id2 <= last)) {
            return &map->extent[idx];
        }
    }
    return NULL;
}

STATIC UINT32 MapIdRangeDown(UidGidMap *map, UINT32 id, UINT32 count)
{
    UINT32 extents = map->extentCount;
    if (extents > UID_GID_MAP_MAX_EXTENTS) {
        return (UINT32)-1;
    }

    UidGidExtent *extent = MapIdRangeDownBase(extents, map, id, count);
    if (extent != NULL) {
        return ((id - extent->first) + extent->lowerFirst);
    }

    return (UINT32)-1;
}

STATIC UINT32 MapIdDown(UidGidMap *map, UINT32 id)
{
    return MapIdRangeDown(map, id, 1);
}

UINT32 OsMakeKuid(UserContainer *userContainer, UINT32 uid)
{
    return MapIdDown(&userContainer->uidMap, uid);
}

UINT32 OsMakeKgid(UserContainer *userContainer, UINT32 gid)
{
    return MapIdDown(&userContainer->gidMap, gid);
}

STATIC INT32 InsertExtent(UidGidMap *idMap, UidGidExtent *extent)
{
    if (idMap->extentCount > UID_GID_MAP_MAX_EXTENTS) {
        return -EINVAL;
    }

    UidGidExtent *dest = &idMap->extent[idMap->extentCount];
    *dest = *extent;
    idMap->extentCount++;

    return 0;
}

STATIC BOOL MappingsOverlap(UidGidMap *idMap, UidGidExtent *extent)
{
    UINT32 upperFirst = extent->first;
    UINT32 lowerFirst = extent->lowerFirst;
    UINT32 upperLast = upperFirst + extent->count - 1;
    UINT32 lowerLast = lowerFirst + extent->count - 1;
    INT32 idx;

    for (idx = 0; idx < idMap->extentCount; idx++) {
        if (idMap->extentCount > UID_GID_MAP_MAX_EXTENTS) {
            return TRUE;
        }
        UidGidExtent *prev = &idMap->extent[idx];
        UINT32 prevUpperFirst = prev->first;
        UINT32 prevLowerFirst = prev->lowerFirst;
        UINT32 prevUpperLast = prevUpperFirst + prev->count - 1;
        UINT32 prevLowerLast = prevLowerFirst + prev->count - 1;

        if ((prevUpperFirst <= upperLast) && (prevUpperLast >= upperFirst)) {
            return TRUE;
        }

        if ((prevLowerFirst <= lowerLast) && (prevLowerLast >= lowerFirst)) {
            return TRUE;
        }
    }
    return FALSE;
}

STATIC CHAR *SkipSpaces(const CHAR *str)
{
    while (isspace(*str)) {
        ++str;
    }

    return (CHAR *)str;
}

STATIC UINTPTR StrToUInt(const CHAR *str, CHAR **endp, UINT32 base)
{
    unsigned long result = 0;
    unsigned long value;

    if (*str == '0') {
        str++;
        if ((*str == 'x') && isxdigit(str[1])) {
            base = HEX;
            str++;
        }
        if (!base) {
            base = OCT;
        }
    }
    if (!base) {
        base = DEC;
    }
    while (isxdigit(*str) && (value = isdigit(*str) ? *str - '0' : (islower(*str) ?
        toupper(*str) : *str) - 'A' + DEC) < base) {
        result = result * base + value;
        str++;
    }
    if (endp != NULL) {
        *endp = (CHAR *)str;
    }
    return result;
}

STATIC INT32 ParsePosData(CHAR *pos, UidGidExtent *extent)
{
    INT32 ret = -EINVAL;
    pos = SkipSpaces(pos);
    extent->first = StrToUInt(pos, &pos, DEC);
    if (!isspace(*pos)) {
        return ret;
    }

    pos = SkipSpaces(pos);
    extent->lowerFirst = StrToUInt(pos, &pos, DEC);
    if (!isspace(*pos)) {
        return ret;
    }

    pos = SkipSpaces(pos);
    extent->count = StrToUInt(pos, &pos, DEC);
    if (*pos && !isspace(*pos)) {
        return ret;
    }

    pos = SkipSpaces(pos);
    if (*pos != '\0') {
        return ret;
    }
    return LOS_OK;
}

STATIC INT32 ParseUserData(CHAR *kbuf, UidGidExtent *extent, UidGidMap *newMap)
{
    INT32 ret = -EINVAL;
    CHAR *pos = NULL;
    CHAR *nextLine = NULL;

    for (pos = kbuf; pos != NULL; pos = nextLine) {
        nextLine = strchr(pos, '\n');
        if (nextLine != NULL) {
            *nextLine = '\0';
            nextLine++;
            if (*nextLine == '\0') {
                nextLine = NULL;
            }
        }

        if (ParsePosData(pos, extent) != LOS_OK) {
            return ret;
        }

        if ((extent->first == (UINT32)-1) || (extent->lowerFirst == (UINT32)-1)) {
            return ret;
        }

        if ((extent->first + extent->count) <= extent->first) {
            return ret;
        }

        if ((extent->lowerFirst + extent->count) <= extent->lowerFirst) {
            return ret;
        }

        if (MappingsOverlap(newMap, extent)) {
            return ret;
        }

        if ((newMap->extentCount + 1) == UID_GID_MAP_MAX_EXTENTS && (nextLine != NULL)) {
            return ret;
        }

        ret = InsertExtent(newMap, extent);
        if (ret < 0) {
            return ret;
        }
        ret = 0;
    }

    if (newMap->extentCount == 0) {
        return -EINVAL;
    }

    return ret;
}

STATIC INT32 ParentMapIdRange(UidGidMap *newMap, UidGidMap *parentMap)
{
    UINT32 idx;
    INT32 ret = -EPERM;
    for (idx = 0; idx < newMap->extentCount; idx++) {
        if (newMap->extentCount > UID_GID_MAP_MAX_EXTENTS) {
            return ret;
        }

        UidGidExtent *extent = &newMap->extent[idx];
        UINT32 lowerFirst = MapIdRangeDown(parentMap, extent->lowerFirst, extent->count);
        if (lowerFirst == (UINT32) -1) {
            return ret;
        }

        extent->lowerFirst = lowerFirst;
    }
    return 0;
}

INT32 OsUserContainerMapWrite(struct ProcFile *fp, CHAR *kbuf, size_t count,
                              INT32 capSetid, UidGidMap *map, UidGidMap *parentMap)
{
    UidGidMap newMap = {0};
    UidGidExtent extent;
    INT32 ret;

    if (map->extentCount != 0) {
        return -EPERM;
    }

    if (!IsCapPermit(capSetid)) {
        return -EPERM;
    }

    ret = ParseUserData(kbuf, &extent, &newMap);
    if (ret < 0) {
        return -EPERM;
    }

    ret = ParentMapIdRange(&newMap, parentMap);
    if (ret < 0) {
        return -EPERM;
    }

    if (newMap.extentCount <= UID_GID_MAP_MAX_EXTENTS) {
        size_t mapSize = newMap.extentCount * sizeof(newMap.extent[0]);
        ret = memcpy_s(map->extent, sizeof(map->extent), newMap.extent, mapSize);
        if (ret != EOK) {
            return -EPERM;
        }
    }

    map->extentCount = newMap.extentCount;
    return count;
}
#endif