ActsCapabilityTest.cpp 53.0 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "ActsCapabilityTest.h"
#include <dirent.h>
#include <fcntl.h>
#include <securec.h>
#include <sys/capability.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "gtest/gtest.h"
H
hu-jixiang1 已提交
24
#include "CapabilityFileSystemTest.h"
M
mamingshuai 已提交
25 26 27 28

using namespace std;
using namespace testing::ext;

29
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
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
static void CreateTxt()
{
    int ret;
    int fd = 0;
    char cap[] = "CapabilityTestSuite!\n";
    // Initialize the process and set the uid and gid of the process to zero
    SetUidGid(UID0, GID0);
    // Create a directory 'CAPDIR0' in the directory 'TOP_DIR'
    ret = mkdir(TOP_DIR "/" CAPDIR0, NORWX);
    ASSERT_EQ(ret, 0) << "ErrInfo: Failed to create the directory 'TOP_DIR/CAPDIR0'";
    // Create a directory 'CAPDIR0_CAPDIR0' in the directory 'TOP_DIR/CAPDIR0'
    ret = mkdir(TOP_DIR "/" CAPDIR0 "/" CAPDIR0_CAPDIR0, RWX);
    ASSERT_EQ(ret, 0) << "ErrInfo: Failed to create the directory 'TOP_DIR/CAPDIR0/CAPDIR0_CAPDIR0'";
    //  Create a file 'CAPDIR0_CAPFILE0' in the directory 'CAPDIR0'
    fd = open(TOP_DIR "/" CAPDIR0 "/" CAPDIR0_CAPFILE0, O_WRONLY | O_CREAT | O_TRUNC, RWX);
    if (fd >= 0) {
        // File created successfully
        write(fd, cap, sizeof(cap));
        close(fd);
    } else {
        // Failed to create the file
        ASSERT_GE(fd, 0) << "ErrInfo: Failed to create the file 'TOP_DIR/CAPDIR0/CAPDIR0_CAPFILE0'";
    }
}

static int CapsetOnlySETPCAP(int num)
{
    struct __user_cap_header_struct capheader;
58 59
    (void)memset_s(&capheader, sizeof(struct __user_cap_header_struct),
        0, sizeof(struct __user_cap_header_struct));
M
mamingshuai 已提交
60 61 62
    capheader.version = _LINUX_CAPABILITY_VERSION_3;
    capheader.pid = 0;
    struct __user_cap_data_struct capdata[CAP_NUM];
63 64
    (void)memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct),
        0, CAP_NUM * sizeof(struct __user_cap_data_struct));
M
mamingshuai 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    capdata[CAP_TO_INDEX(CAP_SETPCAP)].permitted |= CAP_TO_MASK(CAP_SETPCAP);
    capdata[CAP_TO_INDEX(CAP_SETPCAP)].effective |= CAP_TO_MASK(CAP_SETPCAP);
    capdata[CAP_TO_INDEX(CAP_SETPCAP)].inheritable |= CAP_TO_MASK(CAP_SETPCAP);
    // Set capabilities
    int ret = capset(&capheader, &capdata[0]);
    if (ret != 0) {
        LOG("ErrInfo: Failed to drop all caps except CAP_SETPCAP during the %d time", num);
        return FALSE;
    }
    return 0;
}

static int AddCapUnauthorized(int num)
{
    struct __user_cap_header_struct capheader;
80 81
    (void)memset_s(&capheader, sizeof(struct __user_cap_header_struct),
        0, sizeof(struct __user_cap_header_struct));
M
mamingshuai 已提交
82 83 84
    capheader.version = _LINUX_CAPABILITY_VERSION_3;
    capheader.pid = 0;
    struct __user_cap_data_struct capdata[CAP_NUM];
85 86
    (void)memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct),
        0, CAP_NUM * sizeof(struct __user_cap_data_struct));
M
mamingshuai 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    capdata[0].permitted = LINUX_FULL_CAP;
    capdata[0].effective = LINUX_FULL_CAP;
    capdata[0].inheritable = LINUX_FULL_CAP;
    // Set capabilities
    int ret = capset(&capheader, &capdata[0]);
    if (ret != FALSE) {
        LOG("ErrInfo: Add unauthorized capability during the %d time", num);
        return FALSE;
    }
    return 0;
}

static int CapgetWithAllCap(int num)
{
    struct __user_cap_header_struct capheader = { 0 };
102
    errno_t result = memset_s(&capheader, sizeof(struct __user_cap_header_struct),
103 104 105 106 107
        0, sizeof(struct __user_cap_header_struct));
    if (result != EOK) {
        LOG("CapgetWithAllCap memset_s failed");
        return FALSE;
    };
M
mamingshuai 已提交
108 109 110
    capheader.version = _LINUX_CAPABILITY_VERSION_3;
    capheader.pid = 0;
    struct __user_cap_data_struct capdataget[CAP_NUM] = { { 0 }, { 0 } };
111 112 113 114 115 116
    result = memset_s(capdataget, CAP_NUM * sizeof(struct __user_cap_data_struct),
        0, CAP_NUM * sizeof(struct __user_cap_data_struct));
    if (result != EOK) {
        LOG("CapgetWithAllCap memset_s failed");
        return FALSE;
    };
M
mamingshuai 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    int ret = capget(&capheader, &capdataget[0]);
    if (ret != 0) {
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to get CAPs";
        LOG("ErrInfo: Failed to get CAPs during the %d time", num);
        return FALSE;
    }
    // The process has all capabilities
    if (capdataget[0].effective != OHOS_FULL_CAP) {
        EXPECT_EQ(capdataget[0].effective, OHOS_FULL_CAP) << "ErrInfo: Get wrong capabilities";
        LOG("ErrInfo: Get wrong capabilities during the %d time", num);
        return FALSE;
    }
    return 0;
}

static int CapgetWithNoCap(int num)
{
    struct __user_cap_header_struct capheader = { 0 };
135
    errno_t result = memset_s(&capheader, sizeof(struct __user_cap_header_struct),
136 137 138 139 140
        0, sizeof(struct __user_cap_header_struct));
    if (result != EOK) {
        LOG("CapgetWithAllCap memset_s failed");
        return FALSE;
    };
M
mamingshuai 已提交
141 142 143
    capheader.version = _LINUX_CAPABILITY_VERSION_3;
    capheader.pid = 0;
    struct __user_cap_data_struct capdataget[CAP_NUM] = { { 0 }, { 0 } };
144 145 146 147 148 149
    result = memset_s(capdataget, CAP_NUM * sizeof(struct __user_cap_data_struct),
        0, CAP_NUM * sizeof(struct __user_cap_data_struct));
    if (result != EOK) {
        LOG("CapgetWithAllCap memset_s failed");
        return FALSE;
    };
M
mamingshuai 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    int ret = capget(&capheader, &capdataget[0]);
    if (ret != 0) {
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to get CAPs";
        LOG("ErrInfo: Failed to get CAPs during the %d time", num);
        return FALSE;
    }
    // The process does not have any capabilities
    if (capdataget[0].effective != NO_CAP) {
        EXPECT_EQ(capdataget[0].effective, NO_CAP) << "ErrInfo: Get wrong capabilities";
        LOG("ErrInfo: Get wrong capabilities during the %d time", num);
        return FALSE;
    }
    return 0;
}

static int CapgetOnlySETPCAP(int num)
{
    struct __user_cap_header_struct capheader = { 0 };
168
    errno_t result = memset_s(&capheader, sizeof(struct __user_cap_header_struct),
169 170 171 172 173
        0, sizeof(struct __user_cap_header_struct));
    if (result != EOK) {
        LOG("CapgetWithAllCap memset_s failed");
        return FALSE;
    };
M
mamingshuai 已提交
174 175 176
    capheader.version = _LINUX_CAPABILITY_VERSION_3;
    capheader.pid = 0;
    struct __user_cap_data_struct capdataget[CAP_NUM] = { { 0 }, { 0 } };
177 178 179 180 181 182
    result = memset_s(capdataget, CAP_NUM * sizeof(struct __user_cap_data_struct),
        0, CAP_NUM * sizeof(struct __user_cap_data_struct));
    if (result != EOK) {
        LOG("CapgetWithAllCap memset_s failed");
        return FALSE;
    };
M
mamingshuai 已提交
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
    int ret = capget(&capheader, &capdataget[0]);
    if (ret != 0) {
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to get CAPs";
        LOG("ErrInfo: Failed to get CAPs during the %d time", num);
        return FALSE;
    }
    // The process only has CAP_SETPCAP
    if (capdataget[0].effective != ONLY_SETPCAP_CAP) {
        EXPECT_EQ(capdataget[0].effective, ONLY_SETPCAP_CAP) << "ErrInfo: Get wrong capabilities";
        LOG("ErrInfo: Get wrong capabilities during the %d time", num);
        return FALSE;
    }
    return 0;
}

static int CapsetWithoutSETPCAP()
{
    // Drop the capabilities of CAP_SETPCAP 8
    int retsetpcap = DropCAPSETPCAP();
    EXPECT_EQ(retsetpcap, 0) << "ErrInfo: Failed to drop CAP_SETPCAP";
    // Drop the capabilities of CAP_CHOWN 0
    int retchown = DropCAPCHOWN();
    EXPECT_EQ(retchown, FALSE) << "ErrInfo: Drop CAP_CHOWN without CAP_SETPCAP";
    // Drop the capabilities of CAP_DAC_OVERRIDE 1
    int retdacoverride = DropCAPDACOVERRIDE();
    EXPECT_EQ(retdacoverride, FALSE) << "ErrInfo: Drop CAP_DAC_OVERRIDE without CAP_SETPCAP";
    // Drop the capabilities of CAP_DAC_READ_SEARCH 2
    int retdacreadsearch = DropCAPDACREADSEARCH();
    EXPECT_EQ(retdacreadsearch, FALSE) << "ErrInfo: Drop CAP_DAC_READ_SEARCH without CAP_SETPCAP";
    // Drop the capabilities of CAP_FOWNER 3
    int retfowner = DropCAPFOWNER();
    EXPECT_EQ(retfowner, FALSE) << "ErrInfo: Drop CAP_FOWNER without CAP_SETPCAP";
    // Drop the capabilities of CAP_KILL 5
    int retkill = DropCAPKILL();
    EXPECT_EQ(retkill, FALSE) << "ErrInfo: Drop CAP_KILL without CAP_SETPCAP";
    // Drop the capabilities of CAP_SETGID 6
    int retsetgid = DropCAPSETGID();
    EXPECT_EQ(retsetgid, FALSE) << "ErrInfo: Drop CAP_SETGID without CAP_SETPCAP";
    // Drop the capabilities of CAP_SETUID 7
    int retsetuid = DropCAPSETUID();
    EXPECT_EQ(retsetuid, FALSE) << "ErrInfo: Drop CAP_SETUID without CAP_SETPCAP";
    // Drop the capabilities of CAP_SETPCAP 8
    int retsetpcapfailed = DropCAPSETPCAP();
    EXPECT_EQ(retsetpcapfailed, FALSE) << "ErrInfo: Drop CAP_SETPCAP without CAP_SETPCAP";
    // Drop the capabilities of CAP_SYS_NICE 23
    int retsysnice = DropCAPSYSNICE();
    EXPECT_EQ(retsysnice, FALSE) << "ErrInfo: Drop CAP_SYS_NICE without CAP_SETPCAP";
    // Drop the capabilities of CAP_SYS_TIME 25
    int retsystime = DropCAPSYSTIME();
    EXPECT_EQ(retsystime, FALSE) << "ErrInfo: Drop CAP_SYS_TIME without CAP_SETPCAP";
    if ((retchown != FALSE) || (retdacoverride != FALSE) || (retdacreadsearch != FALSE) || (retfowner != FALSE) ||
        (retkill != FALSE) || (retsetgid != FALSE) || (retsetuid != FALSE) || (retsetpcapfailed != FALSE) ||
        (retsysnice != FALSE) || (retsystime != FALSE) || (retsetpcap == FALSE)) {
        LOG("ErrInfo: Drop the capabilities without CAP_SETPCAP");
        return FALSE;
    }
    return 0;
}

static int CapsetWithVersion(pid_t pid, unsigned int version)
{
    struct __user_cap_header_struct capheader = { 0 };
245
    errno_t result = memset_s(&capheader, sizeof(struct __user_cap_header_struct),
246 247 248 249 250
        0, sizeof(struct __user_cap_header_struct));
    if (result != EOK) {
        LOG("CapgetWithAllCap memset_s failed");
        return FALSE;
    };
M
mamingshuai 已提交
251 252 253
    capheader.pid = pid;
    capheader.version = version;
    struct __user_cap_data_struct capdata[CAP_NUM] = { { 0 }, { 0 } };
254 255 256 257 258 259
    result = memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct),
        0xff, CAP_NUM * sizeof(struct __user_cap_data_struct));
    if (result != EOK) {
        LOG("CapgetWithAllCap memset_s failed");
        return FALSE;
    };
M
mamingshuai 已提交
260 261 262 263 264 265 266 267 268 269 270 271
    // Capget based on input parameters
    int ret = capset(&capheader, &capdata[0]);
    if (ret != 0) {
        // Capset with abnormal parameter
        return FALSE;
    }
    return 0;
}

static int CapgetWithVersion(pid_t pid, unsigned int version)
{
    struct __user_cap_header_struct capheader = { 0 };
272
    errno_t result = memset_s(&capheader, sizeof(struct __user_cap_header_struct),
273 274 275 276 277
        0, sizeof(struct __user_cap_header_struct));
    if (result != EOK) {
        LOG("CapgetWithAllCap memset_s failed");
        return FALSE;
    };
M
mamingshuai 已提交
278 279 280
    capheader.pid = pid;
    capheader.version = version;
    struct __user_cap_data_struct capdataget[CAP_NUM] = { { 0 }, { 0 } };
281 282 283 284 285 286
    result = memset_s(capdataget, CAP_NUM * sizeof(struct __user_cap_data_struct),
        0xff, CAP_NUM * sizeof(struct __user_cap_data_struct));
    if (result != EOK) {
        LOG("CapgetWithAllCap memset_s failed");
        return FALSE;
    };
M
mamingshuai 已提交
287 288 289 290 291 292 293 294
    // Capget based on input parameters
    int ret = capget(&capheader, &capdataget[0]);
    if (ret != 0) {
        // Capget with abnormal parameter
        return FALSE;
    }
    return 0;
}
295
#endif
M
mamingshuai 已提交
296

297
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
298 299 300
static int CapgetWithCaps(pid_t pid, unsigned int caps)
{
    struct __user_cap_header_struct capheader = { 0 };
301
    errno_t result = memset_s(&capheader, sizeof(struct __user_cap_header_struct),
302 303 304 305 306
        0, sizeof(struct __user_cap_header_struct));
    if (result != EOK) {
        LOG("CapgetWithAllCap memset_s failed");
        return FALSE;
    };
M
mamingshuai 已提交
307 308 309
    capheader.pid = pid;
    capheader.version = _LINUX_CAPABILITY_VERSION_3;
    struct __user_cap_data_struct capdataget[CAP_NUM] = { { 0 }, { 0 } };
310 311 312 313 314 315
    result = memset_s(capdataget, CAP_NUM * sizeof(struct __user_cap_data_struct),
        0xff, CAP_NUM * sizeof(struct __user_cap_data_struct));
    if (result != EOK) {
        LOG("CapgetWithAllCap memset_s failed");
        return FALSE;
    };
M
mamingshuai 已提交
316 317 318 319 320 321 322 323 324 325
    // Capget based on input parameters and check whether the capability is the same as the input parameter
    int ret = capget(&capheader, &capdataget[0]);
    if (ret != 0 || capdataget[0].effective != caps) {
        EXPECT_EQ(capdataget[0].effective, caps) << "ErrInfo: Pid = " << pid << ", process has wrong capability";
        return FALSE;
    }
    return 0;
}
#endif

326
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
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
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_0600
 * @tc.name       : Processes with the CAP_KILL capability can invoke their management
                    and control interfaces to kill other processes
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest0600, Function | MediumTest | Level2)
{
    int ret;
    int status1 = 0;
    int status2 = 0;
    // Preset action: Fork a sub process pid1
    pid_t pid1 = fork();
    ASSERT_TRUE(pid1 >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    // Preset action: Change the UID&GID of the sub process pid1 and keep it hibernated
    if (pid1 == 0) {
        SetUidGid(UID555, GID555);
        ChildSleep();
    } else {
        // Preset action: Fork a sub process pid2
        pid_t pid2 = fork();
        ASSERT_TRUE(pid2 >= 0) << "======== Fork Error! =========";
        usleep(SLEEP_NUM);
        if (pid2 == 0) {
            int exitCode = 0;
            // Step 1: Drop the capabilities of CAP_KILL
            ret = DropCAPKILL();
            if (ret != 0) {
                LOG("ErrInfo: Failed to drop CAP_KILL");
                exitCode = 1;
            }
            // Step 2: Failed to kill the sub process
H
hujixiang1 已提交
360
            ret = kill(pid1, SIGXFSZ);
M
mamingshuai 已提交
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
            if (ret != FALSE) {
                LOG("ErrInfo: Kill process without CAP_KILL");
                exitCode = 1;
            }
            // Step 3: Change the UID&GID of the sub process pid1 and keep it hibernated
            ret = SetUidGid(UID555, GID555);
            if (ret != 0) {
                LOG("ErrInfo: Failed to set uid and gid");
                exitCode = 1;
            }
            // Step 4: The sub process exit with the exitCode
            exit(exitCode);
        } else {
            // Step 4: Kill the sub process pid1 successfully
            ret = kill(pid1, SIGXFSZ);
            EXPECT_EQ(ret, 0) << "ErrInfo: Failed to Kill pid1 with CAP_KILL";
            // Cleanup action: Wait for the sub process pid1 and pid2 to be killed
            waitpid(pid1, &status1, 0);
            waitpid(pid2, &status2, 0);
            EXPECT_NE(WIFEXITED(status2), 0) << "ErrInfo: The sub process exit error, child_pid = " << pid2;
            EXPECT_EQ(WEXITSTATUS(status2), 0) << "ErrInfo: The exitCode is wrong, please query logs, child_pid = "
            << pid2;
        }
    }
}
#endif

388
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
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
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_0700
 * @tc.name       : Processes with the CAP_SETGID capability can invoke their management
                    and control interfaces to change the group IDs of other processes
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest0700, Function | MediumTest | Level2)
{
    int ret;
    int status = 0;
    pid_t pid = fork(); // Preset action: Fork a sub process
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
        ret = setgid(NUM500); // Step 1.1: Set GID with interface 'setgid'
        if (ret != 0) {LOG("ErrInfo: Failed to set GID with interface 'setgid', now process gid=%d", getpid());
            exitCode = 1; }
        ret = setregid(NUM600, NUM600); // Step 1.2: Set reGID with interface 'setregid'
        if (ret != 0) {LOG("ErrInfo: Failed to set reGID with interface 'setregid', now process gid=%d", getpid());
            exitCode = 1; }
        ret = setresgid(NUM700, NUM700, NUM700); // Step 1.3: Set resGID with interface 'setresgid'
        if (ret != 0) {LOG("ErrInfo: Failed to set resGID with interface 'setresgid', now process gid=%d", getpid());
            exitCode = 1; }
        ret = setgroups(NUM3, GROUPLIST); // Step 1.4: Set groups with interface 'setgroups'
        if (ret != 0) {LOG("ErrInfo: Failed to set groups with interface 'setgroups'");
            exitCode = 1; }
        ret = DropCAPSETGID(); // Step 2: Drop the capabilities of CAP_SETGID
        if (ret != 0) {LOG("ErrInfo: Failed to drop CAP_SETGID");
            exitCode = 1; }
        ret = setgid(NUM500); // Step 3.1: Failed to set GID with interface 'setgid'
        if (ret != FALSE) {LOG("ErrInfo: Set GID without CAP_SETGID, now process gid=%d", getpid());
            exitCode = 1; }
        ret = setregid(NUM500, NUM500); // Step 3.2: Failed to set reGID with interface 'setregid'
        if (ret != FALSE) {LOG("ErrInfo: Set reGID without CAP_SETGID, now process gid=%d", getpid());
            exitCode = 1; }
        ret = setresgid(NUM500, NUM500, NUM500); // Step 3.3: Failed to set resGID with interface 'setregid'
        if (ret != FALSE) {LOG("ErrInfo: Set resGID without CAP_SETGID, now process gid=%d", getpid());
            exitCode = 1; }
        ret = setgroups(NUM3, GROUPLIST); // Step 3.4: Failed to set groups with interface 'setgroups'
        if (ret != FALSE) {LOG("ErrInfo: Set groups without CAP_SETGID");
            exitCode = 1; } // Step 4: The sub process exit with the exitCode
        exit(exitCode);
    } else {
        waitpid(pid, &status, 0); // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
        EXPECT_NE(WIFEXITED(status), 0) << "ErrInfo: The sub process exit error, child_pid = " << pid;
        EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: The exitCode is wrong, please query logs, child_pid = " << pid;
    }
}
#endif

440
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
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
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_0800
 * @tc.name       : Processes with the CAP_SETUID capability can invoke their management
                    and control interfaces to change user IDs of other processes
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest0800, Function | MediumTest | Level2)
{
    int ret;
    int status = 0;
    // Preset action: Fork a sub process
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
        // Step 1.1: Set UID with interface 'setuid'
        ret = setuid(NUM500);
        if (ret != 0) {
            LOG("ErrInfo: Failed to set UID with interface 'setuid', now process uid=%d", getpid());
            exitCode = 1;
        }
        // Step 1.2: Set reUID with interface 'setreuid'
        ret = setreuid(NUM600, NUM600);
        if (ret != 0) {
            LOG("ErrInfo: Failed to set reUID with interface 'setreuid', now process uid=%d", getpid());
            exitCode = 1;
        }
        // Step 1.3: Set resUID with interface 'setresuid'
        ret = setresuid(NUM700, NUM700, NUM700);
        if (ret != 0) {
            LOG("ErrInfo: Failed to set resUID with interface 'setresuid', now process uid=%d", getpid());
            exitCode = 1;
        }
        // Step 2: Drop the capabilities of CAP_SETUID
        ret = DropCAPSETUID();
        if (ret != 0) {
            LOG("ErrInfo: Failed to drop CAP_SETUID");
            exitCode = 1;
        }
        // Step 3.1: Failed to set UID with interface 'setuid'
        ret = setuid(NUM500);
        if (ret != FALSE) {
            LOG("ErrInfo: Set UID without CAP_SETUID, now process uid=%d", getpid());
            exitCode = 1;
        }
        // Step 3.2: Failed to set reUID with interface 'setreuid'
        ret = setreuid(NUM500, NUM500);
        if (ret != FALSE) {
            LOG("ErrInfo: Set reUID without CAP_SETUID, now process uid=%d", getpid());
            exitCode = 1;
        }
        // Step 3.3: Failed to set resUID with interface 'setreuid'
        ret = setresuid(NUM500, NUM500, NUM500);
        if (ret != FALSE) {
            LOG("ErrInfo: Set resUID without CAP_SETUID, now process uid=%d", getpid());
            exitCode = 1;
        }
        // Step 4: The sub process exit with the exitCode
        exit(exitCode);
    } else {
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
        waitpid(pid, &status, 0);
        EXPECT_NE(WIFEXITED(status), 0) << "ErrInfo: The sub process exit error, child_pid = " << pid;
        EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: The exitCode is wrong, please query logs, child_pid = " << pid;
    }
}
#endif

510
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
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
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_0900
 * @tc.name       : Processes with the CAP_SETPCCAP capability can invoke their management
                    and control interfaces to add and delete capabilities for other processes
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest0900, Security | MediumTest | Level2)
{
    int ret;
    int status = 0;
    // Preset action: Fork a sub process
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
        // Step 1: Drop the capabilities of CAP_SETPCAP
        ret = DropCAPSETPCAP();
        if (ret != 0) {
            LOG("ErrInfo: Failed to drop CAP_SETPCAP");
            exitCode = 1;
        }
        // Step 2: Failed to add the capabilities of CAP_SETPCAP
        exitCode = AddCapUnauthorized(1);
        if (exitCode != 0) {
            LOG("ErrInfo: Add capabilities without CAP_SETPCAP");
        }
        // Step 3: The sub process exit with the exitCode
        exit(exitCode);
    } else {
        // Step 4: The parent process wait for the sub process to exit and obtain the exitCode
        waitpid(pid, &status, 0);
        EXPECT_NE(WIFEXITED(status), 0) << "ErrInfo: The sub process exit error, child_pid = " << pid;
        EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: The exitCode is wrong, please query logs, child_pid = " << pid;
    }
}
#endif

549
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
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
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_1000
 * @tc.name       : Processes with the CAP_SYS_NICE capability can invoke their management
                    and control interfaces to set the process priority
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest1000, Function | MediumTest | Level2)
{
    int ret;
    int status1 = 0;
    int status2 = 0;
    // Preset action: Fork a sub process pid1
    pid_t pid1 = fork();
    ASSERT_TRUE(pid1 >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    // Preset action: keep the sub process pid1 hibernated
    if (pid1 == 0) {
        ChildSleep();
    } else {
        // Preset action: Fork a sub process pid2
        pid_t pid2 = fork();
        ASSERT_TRUE(pid2 >= 0) << "======== Fork Error! =========";
        usleep(SLEEP_NUM);
        if (pid2 == 0) {
            int exitCode = 0;
            // Preset action: Obtains the test priority
            struct sched_param param = { 0 };
            ret = sched_getparam(pid1, &param);
            param.sched_priority--;
            // Step 1: Set the priority of the sub process successfully
            ret = sched_setparam(pid1, &param);
            if (ret != 0) {
                LOG("ErrInfo: Failed to set priority with CAP_SYS_NICE");
                exitCode = 1;
            }
            // Step 2: Drop the capabilities of CAP_SYS_NICE
            ret = DropCAPSYSNICE();
            if (ret != 0) {
                LOG("ErrInfo: Failed to drop CAP_SYS_NICE");
                exitCode = 1;
            }
            // Step 3: Failed to set the priority of the sub process
            param.sched_priority++;
            ret = sched_setparam(pid1, &param);
            if (ret != FALSE) {
                LOG("ErrInfo: Set priority without CAP_SYS_NICE");
                exitCode = 1;
            }
            // Step 4: The sub process exit with the exitCode
            exit(exitCode);
        } else {
            // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
            waitpid(pid2, &status2, 0);
            EXPECT_NE(WIFEXITED(status2), 0) << "ErrInfo: The sub process exit error, child_pid = " << pid2;
            EXPECT_EQ(WEXITSTATUS(status2), 0) << "ErrInfo: The exitCode is wrong, please query logs, child_pid = "
            << pid2;
            // Cleanup action: Kill the sub process and wait for the sub process to be killed
            ret = kill(pid1, SIGXFSZ);
            EXPECT_EQ(ret, 0) << "ErrInfo: Failed to kill the sub process pid1";
            waitpid(pid1, &status1, 0);
        }
    }
}
#endif

615
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_1100
 * @tc.name       : Processes with the CAP_SYS_TIME capability can call their management
                    and control interfaces and change the system clock
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest1100, Function | MediumTest | Level2)
{
    int ret;
    int status = 0;
    struct timespec tp = { 0 };
    // Preset action: Fork a sub process
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
        // Preset action: Obtains the system time and the test time
        clock_gettime(CLOCK_REALTIME, &tp);
        tp.tv_sec += 1;
        // Step 1: Set the System Time with the test time successfully
        ret = clock_settime(CLOCK_REALTIME, &tp);
        if (ret != 0) {
            LOG("ErrInfo: Failed to set clocktime with CAP_SYS_TIME");
            exitCode = 1;
        }
        // Step 2: Drop the capabilities of CAP_SYS_TIME
        ret = DropCAPSYSTIME();
        if (ret != 0) {
            LOG("ErrInfo: Failed to drop CAP_SYS_TIME");
            exitCode = 1;
        }
        // Step 3: Failed to set the system time with the test time
        tp.tv_sec += 1;
        ret = clock_settime(CLOCK_REALTIME, &tp);
        if (ret != FALSE) {
            LOG("ErrInfo: Set clocktime without CAP_SYS_TIME");
            exitCode = 1;
        }
        // Step 4: The sub process exit with the exitCode
        exit(exitCode);
    } else {
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
        waitpid(pid, &status, 0);
        EXPECT_NE(WIFEXITED(status), 0) << "ErrInfo: The sub process exit error, child_pid = " << pid;
        EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: The exitCode is wrong, please query logs, child_pid = " << pid;
    }
}
#endif

666
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
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
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_1200
 * @tc.name       : Processes without the CAP_SETPCAP capability cannot drop any capability
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest1200, Function | MediumTest | Level3)
{
    int status = 0;
    // Preset action: Fork a sub process
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
        // Step 1: Failed to add capabilities without CAP_SETPCAP
        exitCode = CapsetWithoutSETPCAP();
        // Step 2: The sub process exit with the exitCode
        exit(exitCode);
    } else {
        // Step 3: The parent process wait for the sub process to exit and obtain the exitCode
        waitpid(pid, &status, 0);
        EXPECT_NE(WIFEXITED(status), 0) << "ErrInfo: The sub process exit error, child_pid = " << pid;
        EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: The exitCode is wrong, please query logs, child_pid = " << pid;
    }
}
#endif

694
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
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
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_1300
 * @tc.name       : Inheritance of process capabilities
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest1300, Function | MediumTest | Level1)
{
    int status = 0;
    // Preset action: Fork a sub process
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
        // Step 1: Query the sub process capabilities for 10000 times
        for (int number = 0; number < NUM10000; number++) {
            exitCode = CapgetWithAllCap(number);
            if (exitCode != 0) {
                LOG("ErrInfo: CapgetWithAllCap error during the %d time", number);
                break;
            }
        }
        // Step 2: The sub process exit with the exitCode
        exit(exitCode);
    } else {
        // Step 3: The parent process wait for the sub process to exit and obtain the exitCode
        waitpid(pid, &status, 0);
        EXPECT_NE(WIFEXITED(status), 0) << "ErrInfo: The sub process exit error, child_pid = " << pid;
        EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: The exitCode is wrong, please query logs, child_pid = " << pid;
        // Step 4: Query the parent process capabilities
        int ret = CapgetWithAllCap(1);
        EXPECT_EQ(ret, 0) << "ErrInfo: CapgetWithAllCap error";
    }
}
#endif

731
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
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 769
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_1400
 * @tc.name       : Invoke the capset interface to add and drop the process capabilities for 10000 times
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest1400, Reliability | MediumTest | Level2)
{
    int status = 0;
    // Preset action: Fork a sub process
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
        // Step 1: Drop and add the sub process capabilities for 10000 times
        for (int number = 0; number < NUM10000; number++) {
            exitCode = CapsetOnlySETPCAP(number);
            if (exitCode != 0) {
                LOG("ErrInfo: CapsetOnlySETPCAP error during the %d time", number);
                break;
            }
            exitCode = AddCapUnauthorized(number);
            if (exitCode != 0) {
                LOG("ErrInfo: AddCapUnauthorized error during the %d time", number);
                break;
            }
        }
        // Step 2: The sub process exit with the exitCode
        exit(exitCode);
    } else {
        // Step 3: The parent process wait for the sub process to exit and obtain the exitCode
        waitpid(pid, &status, 0);
        EXPECT_NE(WIFEXITED(status), 0) << "ErrInfo: The sub process exit error, child_pid = " << pid;
        EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: The exitCode is wrong, please query logs, child_pid = " << pid;
    }
}
#endif

770
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_1500
 * @tc.name       : Invoke the capset interface to revoke the process capabilities which not exist for 10000 times
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest1500, Reliability | MediumTest | Level2)
{
    int status = 0;
    // Preset action: Fork a sub process
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
        struct __user_cap_header_struct capheader = { 0 };
786 787
        (void)memset_s(&capheader, sizeof(struct __user_cap_header_struct),
            0, sizeof(struct __user_cap_header_struct));
M
mamingshuai 已提交
788 789 790
        capheader.version = _LINUX_CAPABILITY_VERSION_3;
        capheader.pid = 0;
        struct __user_cap_data_struct capdata[CAP_NUM] = { { 0 }, { 0 } };
791 792
        (void)memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct),
            LINUX_FULL_CAP, CAP_NUM * sizeof(struct __user_cap_data_struct));
M
mamingshuai 已提交
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
        capdata[CAP_TO_INDEX(INVALID_CAP_TO_INDEX)].permitted &= ~CAP_TO_MASK(INVALID_CAP_TO_INDEX);
        capdata[CAP_TO_INDEX(INVALID_CAP_TO_INDEX)].effective &= ~CAP_TO_MASK(INVALID_CAP_TO_INDEX);
        capdata[CAP_TO_INDEX(INVALID_CAP_TO_INDEX)].inheritable &= ~CAP_TO_MASK(INVALID_CAP_TO_INDEX);
        for (int number = 0; number < NUM10000; number++) {
            // Step 1: Drop an abnormal capability for 10000 times
            int ret = capset(&capheader, &capdata[0]);
            if (ret != 0) {
                LOG("ErrInfo: Drop an abnormal capability during the %d time", number);
                exitCode = 1;
                break;
            }
        }
        // Step 2: The sub process exit with the exitCode
        exit(exitCode);
    } else {
        // Step 3: The parent process wait for the sub process to exit and obtain the exitCode
        waitpid(pid, &status, 0);
        EXPECT_NE(WIFEXITED(status), 0) << "ErrInfo: The sub process exit error, child_pid = " << pid;
        EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: The exitCode is wrong, please query logs, child_pid = " << pid;
    }
}
#endif

816
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
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 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_1600
 * @tc.name       : Enter the exception parameter for 10000 times when invoke the capset interface
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest1600, Reliability | MediumTest | Level3)
{
    int ret;
    int status = 0;
    // Preset action: Fork a sub process
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
        for (int number = 0; number < NUM10000; number++) {
            // Step 1.1: Capset the abnormal capheader.pid '-1' for 10000 times
            ret = CapsetWithVersion(FALSE, _LINUX_CAPABILITY_VERSION_3);
            if (ret != FALSE) {
                LOG("ErrInfo: Capset with the abnormal capheader.pid '-1' during the %d time", number);
                exitCode = 1;
                break;
            }
            // Step 1.2: Capset the abnormal capheader.pid '65536' for 10000 times
            ret = CapsetWithVersion(INVAILD_PID, _LINUX_CAPABILITY_VERSION_3);
            if (ret != FALSE) {
                LOG("ErrInfo: Capset with the abnormal capheader.pid '65536' during the %d time", number);
                exitCode = 1;
                break;
            }
            // Step 1.3: Capset the abnormal capheader.version '1' for 10000 times
            ret = CapsetWithVersion(0, 1);
            if (ret != FALSE) {
                LOG("ErrInfo: Capset with the abnormal normal capheader.version '1' during the %d time", number);
                exitCode = 1;
                break;
            }
            // Step 1.4: Capset the abnormal capheader.version '_LINUX_CAPABILITY_VERSION_1' for 10000 times
            ret = CapsetWithVersion(0, _LINUX_CAPABILITY_VERSION_1);
            if (ret != 0) {
                LOG("ErrInfo: Capset with the abnormal capheader.version '_LINUX_CAPABILITY_VERSION_1'"
                "during the %d time", number);
                exitCode = 1;
                break;
            }
            // Step 1.5: Add normal capheader.version '_LINUX_CAPABILITY_VERSION_3' for 10000 times
            ret = CapsetWithVersion(0, _LINUX_CAPABILITY_VERSION_3);
            if (ret != 0) {
                LOG("ErrInfo: Failed to capset with normal capheader.version during the %d time", number);
                exitCode = 1;
                break;
            }
        }
        // Step 2: The sub process exit with the exitCode
        exit(exitCode);
    } else {
        // Step 3: The parent process wait for the sub process to exit and obtain the exitCode
        waitpid(pid, &status, 0);
        EXPECT_NE(WIFEXITED(status), 0) << "ErrInfo: The sub process exit error, child_pid = " << pid;
        EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: The exitCode is wrong, please query logs, child_pid = " << pid;
    }
}
#endif

881
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_1700
 * @tc.name       : Invoke the capget interface to query the process capabilities for 10000 times
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest1700, Reliability | MediumTest | Level2)
{
    // Step 1: Query the process capabilities for 10000 times
    for (int number = 0; number < NUM10000; number++) {
        int ret = CapgetWithAllCap(number);
        if (ret != 0) {
            EXPECT_EQ(ret, 0) << "ErrInfo: CapgetWithAllCap error during the " << number << " time";
            break;
        }
    }
}
#endif

900
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
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
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_1800
 * @tc.name       : Invoke the capget interface to query the process capabilities which not exist for 10000 times
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest1800, Reliability | MediumTest | Level3)
{
    int ret;
    int status = 0;
    // Preset action: Fork a sub process
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
        // Step 1: Drop all the sub process capabilities
        DropAllCAP();
        // Step 2: Obtain the sub process capabilities
        for (int number = 0; number < NUM10000; number++) {
            exitCode = CapgetWithNoCap(number);
            if (exitCode != 0) {
                LOG("ErrInfo: CapgetWithNoCap error during the %d time", number);
                break;
            }
        }
        // Step 3: The sub process exit with the exitCode
        exit(exitCode);
    } else {
        // Step 4: The parent process wait for the sub process to exit and obtain the exitCode
        waitpid(pid, &status, 0);
        EXPECT_NE(WIFEXITED(status), 0) << "ErrInfo: The sub process exit error, child_pid = " << pid;
        EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: The exitCode is wrong, please query logs, child_pid = " << pid;
        // Step 5: Query the parent process capabilities
        ret = CapgetWithAllCap(1);
        EXPECT_EQ(ret, 0) << "ErrInfo: CapgetWithAllCap error";
    }
}
#endif

940
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
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
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_1900
 * @tc.name       : Enter the exception parameter for 10000 times when invoke the capget interface
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest1900, Reliability | MediumTest | Level2)
{
    int ret;
    int status = 0;
    // Preset action: Fork a sub process
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
        for (int number = 0; number < NUM10000; number++) {
            // Step 1.1: Capget the abnormal capheader.pid '-1' for 10000 times
            ret = CapgetWithVersion(FALSE, _LINUX_CAPABILITY_VERSION_3);
            if (ret != FALSE) {
                LOG("ErrInfo: Capget with the abnormal capheader.pid '-1' during the %d time", number);
                exitCode = 1;
                break;
            }
            // Step 1.2: Capget the abnormal capheader.pid '65536' for 10000 times
            ret = CapgetWithVersion(INVAILD_PID, _LINUX_CAPABILITY_VERSION_3);
            if (ret != FALSE) {
                LOG("ErrInfo: Capget with the abnormal capheader.pid '65536' during the %d time", number);
                exitCode = 1;
                break;
            }
            // Step 1.3: Capget the abnormal capheader.version '1' for 10000 times
            ret = CapgetWithVersion(0, 1);
            if (ret != FALSE) {
                LOG("ErrInfo: Capget with the abnormal capheader.version '1' during the %d time", number);
                exitCode = 1;
                break;
            }
            // Step 1.4: Capget with the abnormal capheader.version '_LINUX_CAPABILITY_VERSION_1' for 10000 times
            ret = CapgetWithVersion(0, _LINUX_CAPABILITY_VERSION_1);
            if (ret != 0) {
                LOG("ErrInfo: Capget with the abnormal capheader.version '_LINUX_CAPABILITY_VERSION_1'"
                "during the %d time", number);
                exitCode = 1;
                break;
            }
            // Step 1.5: Capget the normal capheader.version '_LINUX_CAPABILITY_VERSION_3' for 10000 times
            ret = CapgetWithVersion(0, _LINUX_CAPABILITY_VERSION_3);
            if (ret != 0) {
                LOG("ErrInfo: Failed to capget with the normal capheader.version '_LINUX_CAPABILITY_VERSION_3'"
                "during the %d time", number);
                exitCode = 1;
                break;
            }
        }
        // Step 2: The sub process exit with the exitCode
        exit(exitCode);
    } else {
        // Step 3: The parent process wait for the sub process to exit and obtain the exitCode
        waitpid(pid, &status, 0);
        EXPECT_NE(WIFEXITED(status), 0) << "ErrInfo: The sub process exit error, child_pid = " << pid;
        EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: The exitCode is wrong, please query logs, child_pid = " << pid;
    }
}
#endif

1006
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
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
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_2100
 * @tc.name       : Five processes concurrently invoke APIs managed by the capability for 5000 times
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest2100, Reliability | MediumTest | Level2)
{
    int status = 0;
    // Preset action: Fork five sub processes
    pid_t pid;
    for (int num = 0; num < NUM5; num++) {
        pid = fork();
        ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
        usleep(SLEEP_NUM);
        if (pid == 0) {
            break;
        }
    }
    // get one parent & five children
    if (pid == 0) {
        int exitCode = 0;
        for (int number = 0; number < NUM5000; number++) {
            // Step 1: Five sub processes simultaneously drop capabilities for 5000 times
            exitCode = CapsetOnlySETPCAP(number);
            if (exitCode != 0) {
                LOG("ErrInfo: CapsetOnlySETPCAP Error during the %d time", number);
                break;
            }
            // Step 2: Five sub processes simultaneously add capabilities for 5000 times
            exitCode = AddCapUnauthorized(number);
            if (exitCode != 0) {
                LOG("ErrInfo: AddCapUnauthorized Error during the %d time", number);
                break;
            }
            // Step 3: Five sub processes simultaneously capget for 5000 times
            exitCode = CapgetOnlySETPCAP(number);
            if (exitCode != 0) {
                LOG("ErrInfo: CapgetOnlySETPCAP Error during the %d time", number);
                break;
            }
        }
        // Step 4: Five sub processes exit with the exitCode
        exit(exitCode);
    } else {
        // Step 5: The parent process wait for five sub processes to exit and obtain the exitCode
        for (int num2 = 0; num2 < NUM5; num2++) {
            wait(&status);
            EXPECT_NE(WIFEXITED(status), 0) << "ErrInfo: The sub process exit error, child_pid = " << pid;
            EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: Pid = "<< pid
            << ", its exitCode is wrong and test case failed, please query logs";
        }
    }
}
#endif

1062
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 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
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_2200
 * @tc.name       : Check whether the default configuration of the system process capabilities
                    is the same as that described in the design document
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest2200, Security | MediumTest | Level1)
{
    int ret;
    // Step 1: Check the capability of process 'init', pid = 1
    ret = CapgetWithCaps(INIT_PID, INIT_CAP);
    EXPECT_EQ(ret, 0) << "ErrInfo: Pid = 1, process init has wrong capability";
    // Step 2: Check the capability of process 'KProcess', pid = 2
    ret = CapgetWithCaps(KPROCESS_PID, KPROCESS_CAP);
    EXPECT_EQ(ret, 0) << "ErrInfo: Pid = 2, process KProcess has wrong capability";
    // Step 3: Check the capability of process 'shell', pid = 3
    ret = CapgetWithCaps(SHELL_PID, SHELL_CAP);
    EXPECT_EQ(ret, 0) << "ErrInfo: Pid = 3, process shell has wrong capability";
    // Step 4: Check the capability of process 'apphilogcat', pid = 4
    ret = CapgetWithCaps(HILOGCAT_PID, HILOGCAT_CAP);
    EXPECT_EQ(ret, 0) << "ErrInfo: Pid = 4, process apphilogcat has wrong capability";
    // Step 5: Check the capability of process 'foundation', pid = 5
    ret = CapgetWithCaps(FOUNDATION_PID, FOUNDATION_CAP);
    EXPECT_EQ(ret, 0) << "ErrInfo: Pid = 5, process foundation has wrong capability";
    // Step 6: Check the capability of process 'bundle_daemon', pid = 6
    ret = CapgetWithCaps(BUNDLE_DAEMON_PID, BUNDLE_DAEMON_CAP);
    EXPECT_EQ(ret, 0) << "ErrInfo: Pid = 6, process bundle_daemon has wrong capability";
    // Step 7: Check the capability of process 'appspawn', pid = 7
    ret = CapgetWithCaps(APPSPAWN_PID, APPSPAWN_CAP);
    EXPECT_EQ(ret, 0) << "ErrInfo: Pid = 7, process appspawn has wrong capability";
    // Step 8: Check the capability of process 'media_server', pid = 8
    ret = CapgetWithCaps(MEDIA_SERVER_PID, MEDIA_SERVER_CAP);
    EXPECT_EQ(ret, 0) << "ErrInfo: Pid = 8, process media_server has wrong capability";
    // Step 9: Check the capability of process 'wms_server' or 'ai_server', pid = 9
    ret = CapgetWithCaps(WMS_SERVER_OR_AI_SERVER_PID, WMS_SERVER_OR_AI_SERVER_CAP);
    EXPECT_EQ(ret, 0) << "ErrInfo: Pid = 9, process wms_server or ai_server has wrong capability";
}
#endif
X
xialiyun 已提交
1101

1102
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_2300
 * @tc.name       : Check whether the default configuration of the capability of the third-party application process
                    is the same as that described in the design document
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest2300, Security | MediumTest | Level1)
{
    int ret;
    struct __user_cap_header_struct capheader = { 0 };
1113 1114
    (void)memset_s(&capheader, sizeof(struct __user_cap_header_struct),
        0, sizeof(struct __user_cap_header_struct));
M
mamingshuai 已提交
1115 1116
    capheader.version = _LINUX_CAPABILITY_VERSION_3;
    struct __user_cap_data_struct capdataget[CAP_NUM] = { { 0 }, { 0 } };
1117
    (void)memset_s(capdataget, CAP_NUM * sizeof(struct __user_cap_data_struct),
M
mamingshuai 已提交
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
    0, CAP_NUM * sizeof(struct __user_cap_data_struct));
    pid_t pid = getpid();
    for (int num = OTHER_PID; num <= pid; num++) {
        // Step 1: The current test process has all capabilities
        if (num == pid) {
            capheader.pid = pid;
            ret = capget(&capheader, &capdataget[0]);
            EXPECT_EQ(capdataget[0].effective, OHOS_FULL_CAP) <<"ErrInfo: Pid = " << num
            << ", test_process has wrong capability";
        } else {
            // Step 2: Check the capability of process from pid = 9
            capheader.pid = num;
            ret = capget(&capheader, &capdataget[0]);
            if (ret == 0) {
                // Step 2.1: Check the capability of process which exists now
                EXPECT_EQ(capdataget[0].effective, NO_CAP) << "ErrInfo: Pid = " << num
                << ", thirdPartyApp has wrong capability";
            } else {
                // Step 2.2: Check the capability of process which not exist now
                EXPECT_EQ(ret, FALSE) << "ErrInfo: Capget return error, now process uid=" << getuid();
            }
        }
    }
}
#endif

1144
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_2400
 * @tc.name       : The process continuously invokes the capset and capget interfaces,
 which does not affect the use of other processes
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest2400, Function | MediumTest | Level1)
{
    int status = 0;
    // Preset action: Create a txt
    CreateTxt();
    // Preset action: Fork two sub processes
    pid_t pid;
    for (int num = 0; num < NUM2; num++) {
        pid = fork();
        ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
        usleep(SLEEP_NUM);
        if (pid == 0) {
            break;
        }
    }
    // get one parent & two children
    if (pid == 0) {
        int exitCode = 0;
        for (int number = 0; number < NUM5000; number++) {
            // Step 1: Two sub processes simultaneously drop capabilities for 5000 times
            exitCode = CapsetOnlySETPCAP(number);
            if (exitCode != 0) {
                LOG("ErrInfo: CapsetOnlySETPCAP Error during the %d time", number);
                break;
            }
            // Step 2: Two sub processes simultaneously add capabilities for 5000 times
            exitCode = AddCapUnauthorized(number);
            if (exitCode != 0) {
                LOG("ErrInfo: AddCapUnauthorized Error during the %d time", number);
                break;
            }
            // Step 3: Two sub processes simultaneously capget for 5000 times
            exitCode = CapgetOnlySETPCAP(number);
            if (exitCode != 0) {
                LOG("ErrInfo: CapgetOnlySETPCAP Error during the %d time", number);
                break;
            }
        }
        // Step 4: Two sub processes exit with the exitCode
        exit(exitCode);
    } else {
        // Step 5: The parent process simultaneously invoke chown interfaces for 1000 times
        for (int number2 = 0; number2 < NUM1000; number2++) {
            int ret = chown(TOP_DIR "/" CAPDIR0 "/" CAPDIR0_CAPFILE0, number2, number2);
            if (ret != 0) {
                EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chown during the " << number2 << " time";
                break;
            }
        }
        // Step 6: The parent process wait for two sub processes to exit and obtain the exitCode
        for (int num2 = 0; num2 < NUM2; num2++) {
            wait(&status);
            EXPECT_NE(WIFEXITED(status), 0) << "ErrInfo: The sub process exit error, child_pid = " << pid;
            EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: Pid = "<< pid
            << ", its exitCode is wrong and test case failed, please query logs";
        }
    }
}
#endif

1211
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_2500
 * @tc.name       : Performance test of capset and capget interface
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest2500, Performance | MediumTest | Level2)
{
    int status = 0;
    // Preset action: Fork a sub process
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
        struct timespec tp = { 0 };
        struct timespec starttime = { 0 };
        struct timespec endtime = { 0 };
        tp.tv_sec = 0;
        tp.tv_nsec = 0;
        // Preset action: Obtains the system time -> starttime
        clock_gettime(CLOCK_REALTIME, &starttime);
        // Step 1: Capset and capget for 200000 times
        for (int number = 0; number < NUM10000; number++) {
            CapsetOnlySETPCAP(number);
            AddCapUnauthorized(number);
            CapgetOnlySETPCAP(number);
        }
        // Step 2: Obtains the system time again -> endtime
        clock_gettime(CLOCK_REALTIME, &endtime);
        // Step 3: Compare the starttime and the endtime -> tp
        tp = CompareTime(starttime, endtime);
        if (tp.tv_sec > NUM20) {
            LOG("ErrInfo: Capset and capget for 200000 times used %d.%d s", tp.tv_sec, tp.tv_nsec);
            exitCode = 1;
        }
        // Step 4: Two sub processes exit with the exitCode
        exit(exitCode);
    } else {
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
        waitpid(pid, &status, 0);
        EXPECT_NE(WIFEXITED(status), 0) << "ErrInfo: The sub process exit error, child_pid = " << pid;
        EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: The exitCode is wrong, please query logs, child_pid = " << pid;
    }
}
#endif

1258
#if defined(LITE_FS_VFAT)
M
mamingshuai 已提交
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_Capability_2600
 * @tc.name       : Performance test of the interface managed by Capability
 * @tc.desc       : [C-SECURITY-0100]
 */
HWTEST_F(CapabilityTestSuite, CapabilityTest2600, Performance | MediumTest | Level2)
{
    struct timespec tp = { 0 };
    struct timespec starttime = { 0 };
    struct timespec endtime = { 0 };
    tp.tv_sec = 0;
    tp.tv_nsec = 0;
    // Preset action: Create a txt
    CreateTxt();
    // Preset action: Obtains the system time -> starttime
    clock_gettime(CLOCK_REALTIME, &starttime);
    // Step 1: Chown for 10000 times
J
jiyong 已提交
1276
    for (int number = 0; number < NUM1000; number++) {
M
mamingshuai 已提交
1277 1278 1279 1280 1281 1282
        chown(TOP_DIR "/" CAPDIR0 "/" CAPDIR0_CAPFILE0, number, number);
    }
    // Step 2: Obtains the system time again -> endtime
    clock_gettime(CLOCK_REALTIME, &endtime);
    // Step 3: Compare the starttime and the endtime -> tp
    tp = CompareTime(starttime, endtime);
J
jiyong 已提交
1283
    EXPECT_LE(tp.tv_sec, NUM80) << "ErrInfo: Chown for 1000 times used " << tp.tv_sec << "." << tp.tv_nsec << "s";
M
mamingshuai 已提交
1284 1285 1286
    // Cleanup action: Restore the initial status of the file
    chown(TOP_DIR "/" CAPDIR0 "/" CAPDIR0_CAPFILE0, UID0, GID0);
}
J
jiyong 已提交
1287
#endif