ActsDacTest.cpp 13.5 KB
Newer Older
W
wenjun 已提交
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
/*
 * Copyright (c) 2020 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 "ActsDacTest.h"
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <dirent.h>
#include <fcntl.h>
#include <sched.h>
#include <securec.h>
#include <sys/ioctl.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "gtest/gtest.h"

#ifdef __LINUX__
#include <grp.h>
#include <sys/cdefs.h>
#include <linux/capability.h>
#else
#include <sys/capability.h>
#endif

#ifdef __LINUX__
__BEGIN_DECLS
extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
extern int capset(cap_user_header_t hdrp, const cap_user_data_t datap);
__END_DECLS
#endif

using namespace std;
using namespace testing::ext;

class DacTestSuite : public testing::Test {
protected:
    // SetUpTestCase:测试套预置动作,在第一个TestCase之前执行
    static void SetUpTestCase(void)
    {
        // uid=gid=555
        SetUid555Gid555();
        RemoveDir("/storage/DacTestSuite");
        RemoveDir("/storage/DacTestSuite1");
        // uid=gid=0
        SetUid0Gid0();
        RemoveDir("/storage/DacTestSuite");
        RemoveDir("/storage/DacTestSuite1");
    }
    // TearDownTestCase:测试套清理动作,在最后一个TestCase之后执行
    static void TearDownTestCase(void) {}
    // 用例的预置动作
    virtual void SetUp()
    {
        // uid=gid=0
        SetUid0Gid0();
    }
    // 用例的清理动作
    virtual void TearDown()
    {
        RemoveDir("/storage/DacTestSuite");
        RemoveDir("/storage/DacTestSuite1");
    }
};

static void NoChownCapFunc()
{
    struct __user_cap_header_struct capheader;
    struct __user_cap_data_struct capdata[CAP_NUM];
    int ret;
    memset_s(&capheader, sizeof(struct __user_cap_header_struct), 0, sizeof(struct __user_cap_header_struct));
    memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct), 0,
        CAP_NUM * sizeof(struct __user_cap_data_struct));
    capdata[0].permitted = 0xffffffff;
    capdata[1].permitted = 0xffffffff;
    capheader.version = _LINUX_CAPABILITY_VERSION_3;
    capdata[CAP_TO_INDEX(CAP_SETPCAP)].effective |= CAP_TO_MASK(CAP_SETPCAP);
    capdata[CAP_TO_INDEX(CAP_SETUID)].effective |= CAP_TO_MASK(CAP_SETUID);
    capdata[CAP_TO_INDEX(CAP_SETGID)].effective |= CAP_TO_MASK(CAP_SETGID);
    capdata[CAP_TO_INDEX(CAP_CHOWN)].effective &= ~CAP_TO_MASK(CAP_CHOWN);
    capdata[CAP_TO_INDEX(CAP_FOWNER)].effective &= ~CAP_TO_MASK(CAP_FOWNER);
    capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].effective &= ~CAP_TO_MASK(CAP_DAC_OVERRIDE);
    capdata[CAP_TO_INDEX(CAP_DAC_READ_SEARCH)].effective &= ~CAP_TO_MASK(CAP_DAC_READ_SEARCH);
    ret = capset(&capheader, &capdata[0]);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed in setting privileges";
}

static void ChownCapFunc()
{
    struct __user_cap_header_struct capheader;
    struct __user_cap_data_struct capdata[CAP_NUM];
    int ret;
    memset_s(&capheader, sizeof(struct __user_cap_header_struct), 0, sizeof(struct __user_cap_header_struct));
    memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct), 0,
        CAP_NUM * sizeof(struct __user_cap_data_struct));
    capdata[0].permitted = 0xffffffff;
    capdata[1].permitted = 0xffffffff;
    capheader.version = _LINUX_CAPABILITY_VERSION_3;
    capdata[CAP_TO_INDEX(CAP_SETPCAP)].effective |= CAP_TO_MASK(CAP_SETPCAP);
    capdata[CAP_TO_INDEX(CAP_SETUID)].effective |= CAP_TO_MASK(CAP_SETUID);
    capdata[CAP_TO_INDEX(CAP_SETGID)].effective |= CAP_TO_MASK(CAP_SETGID);
    capdata[CAP_TO_INDEX(CAP_CHOWN)].effective |= CAP_TO_MASK(CAP_CHOWN);
    capdata[CAP_TO_INDEX(CAP_FOWNER)].effective &= ~CAP_TO_MASK(CAP_FOWNER);
    capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].effective &= ~CAP_TO_MASK(CAP_DAC_OVERRIDE);
    capdata[CAP_TO_INDEX(CAP_DAC_READ_SEARCH)].effective &= ~CAP_TO_MASK(CAP_DAC_READ_SEARCH);
    ret = capset(&capheader, &capdata[0]);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed in setting privileges";
}

static void CreateTxt()
{
    int ret;
    int fd = 0;
    char cap[] = "DacTestSuite!\n";
    // uid=gid=0
    SetUid0Gid0();
    umask(ZEROUMASK);
    ret = mkdir("/storage/DacTestSuite", CHMOD700);
    EXPECT_EQ(ret, 0) << "ErrInfo: Now uid=" << getuid();
    ret = mkdir("/storage/DacTestSuite/Dac", RWX);
    EXPECT_EQ(ret, 0) << "ErrInfo: Now uid=" << getuid();
    fd = open("/storage/DacTestSuite/Dac.txt", O_WRONLY | O_CREAT | O_TRUNC, CHMOD700);
    if (fd >= 0) {
        write(fd, cap, sizeof(cap));
        close(fd);
    } else {
        EXPECT_GE(fd, 0) << "Creat txt error";
    }
}

static void CreateTxt1()
{
    int ret;
    int fd = 0;
    char cap[] = "DacTestSuite!\n";
    // uid=gid=0 & chown cap
    struct __user_cap_header_struct capheader;
    struct __user_cap_data_struct capdata[CAP_NUM];
    memset_s(&capheader, sizeof(struct __user_cap_header_struct), 0, sizeof(struct __user_cap_header_struct));
    memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct), 0,
        CAP_NUM * sizeof(struct __user_cap_data_struct));
    capdata[0].permitted = 0xffffffff;
    capdata[1].permitted = 0xffffffff;
    capheader.version = _LINUX_CAPABILITY_VERSION_3;
    capdata[CAP_TO_INDEX(CAP_SETPCAP)].effective |= CAP_TO_MASK(CAP_SETPCAP);
    capdata[CAP_TO_INDEX(CAP_SETUID)].effective |= CAP_TO_MASK(CAP_SETUID);
    capdata[CAP_TO_INDEX(CAP_SETGID)].effective |= CAP_TO_MASK(CAP_SETGID);
    capdata[CAP_TO_INDEX(CAP_CHOWN)].effective |= CAP_TO_MASK(CAP_CHOWN);
    ret = capset(&capheader, &capdata[0]);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed in setting privileges";
    ret = setuid(UID0);
    EXPECT_EQ(ret, 0) << "ErrInfo: Set uid failed,now uid=" << getuid();
    ret = setgid(GID0);
    EXPECT_EQ(ret, 0) << "ErrInfo: Set gid failed,now gid=" << getgid();
    umask(ZEROUMASK);
    ret = mkdir("/storage/DacTestSuite1", CHMOD700);
    EXPECT_EQ(ret, 0) << "ErrInfo: Now uid=" << getuid();
    ret = mkdir("/storage/DacTestSuite1/Dac1", RWX);
    EXPECT_EQ(ret, 0) << "ErrInfo: Now uid=" << getuid();
    fd = open("/storage/DacTestSuite1/Dac.txt", O_WRONLY | O_CREAT | O_TRUNC, CHMOD700);
    if (fd >= 0) {
        write(fd, cap, sizeof(cap));
        close(fd);
    } else {
        EXPECT_GE(fd, 0) << "Creat txt error";
    }
    ret = chown("/storage/DacTestSuite1/Dac.txt", UID555, GID555);
    EXPECT_EQ(ret, 0) << "ErrInfo: chown error,now uid=" << getuid();
    ret = chown("/storage/DacTestSuite1", UID555, GID555);
    EXPECT_EQ(ret, 0) << "ErrInfo: chown error,now uid=" << getuid();
}

static void ThreeProcessReadOneTxt()
{
    int status = 0;
    int fd = 0;
    pid_t pid;
    CreateTxt();
    for (int num = 0; num < NUM3; num++) {
        pid = fork();
        ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
        usleep(SLEEP_NUM);
        if (pid == 0) {
            break;
        }
    } // get one parent & Three children
    usleep(SLEEP_NUM);
    if (pid == 0) {
        SetUid0Gid0();
        int exitCode = 0;
        for (int number = 0; number < NUM1000; number++) {
            fd = open("/storage/DacTestSuite/Dac.txt", O_WRONLY);
            if (fd >= 0) {
                close(fd);
            } else {
                LOG("open txt error when the %d time", number);
                exitCode = 1;
                break;
            }
        }
        exit(exitCode);
    } else {
        SetUid555Gid555();
        for (int number2 = 0; number2 < NUM500; number2++) {
            fd = open("/storage/DacTestSuite/Dac.txt", O_WRONLY);
            if (fd >= 0) {
                EXPECT_EQ(fd, WRONGNUM) << "ErrInfo: Open txt without DAC,now uid=" << getuid() << ",No." << number2;
                close(fd);
                break;
            }
        }
        while (wait(&status) > 0) {
            usleep(SLEEP_NUM);
            EXPECT_TRUE(WIFEXITED(status) > 0) << "ErrInfo: Exit error";
            EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo:Pid = " << wait(&status) <<
                ",the WEXITSTATUS return code is " << WEXITSTATUS(status);
        }
    }
}

static void TwoProcessReadTwoTxt()
{
    int status = 0;
    int fd = 0;
    CreateTxt();
    CreateTxt1();
    pid_t pid = fork();
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
        SetUid0Gid0(); // uid=gid=0
        for (int number = 0; number < NUM1000; number++) {
            fd = open("/storage/DacTestSuite/Dac.txt", O_WRONLY); // uid=gid=0
            if (fd >= 0) {
                close(fd);
            } else {
                LOG("open txt error when the %d time", number);
                exitCode = 1;
                break;
            }
            fd = open("/storage/DacTestSuite1/Dac.txt", O_WRONLY); // uid=gid=555
            if (fd >= 0) {
                LOG("open txt with wrong uid&gid when the %d time", number);
                close(fd);
                exitCode = 1;
                break;
            }
        }
        exit(exitCode);
    } else {
        SetUid555Gid555(); // uid=gid=555
        for (int number2 = 0; number2 < NUM500; number2++) {
            fd = open("/storage/DacTestSuite/Dac.txt", O_WRONLY); // uid=gid=0
            if (fd >= 0) {
                EXPECT_EQ(fd, WRONGNUM) << "ErrInfo: Open txt,now uid=" << getuid() << ",No." << number2;
                close(fd);
                break;
            }
            fd = open("/storage/DacTestSuite1/Dac.txt", O_WRONLY); // uid=gid=1
            if (fd >= 0) {
                close(fd);
            } else {
                EXPECT_EQ(fd, 0) << "ErrInfo: Failed in open txt,now uid=" << getuid() << ",No." << number2;
                break;
            }
        }
        waitpid(pid, &status, 0);
        EXPECT_TRUE(WIFEXITED(status) > 0) << "ErrInfo: Exit error";
        EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: WEXITSTATUS is " << WEXITSTATUS(status);
    }
}

static void TenProcessReadSameTxt()
{
    int ret;
    int status = 0;
    int fd = 0;
    CreateTxt();
    pid_t pid;
    for (int num = 0; num < NUM3; num++) {
        pid = fork();
        ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
        usleep(SLEEP_NUM);
        if (pid == 0) {
            ret = setuid(num);
            EXPECT_EQ(ret, 0) << "ErrInfo: Set uid failed,now uid=" << getuid();
            break;
        }
    } // get one parent & three children
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
        for (int number = 0; number < NUM1000; number++) {
            fd = open("/storage/DacTestSuite/Dac.txt", O_WRONLY);
            if (getuid() == 0) {
                if (fd >= 0) {
                    close(fd);
                } else {
                    LOG("open txt error when the %d time", number);
                    exitCode = 1;
                    break;
                }
            } else {
                if (fd >= 0) {
                    LOG("open txt with wrong uid&gid when the %d time", number);
                    close(fd);
                    exitCode = 1;
                    break;
                }
            }
        }
        exit(exitCode);
    } else {
        waitpid(pid, &status, 0);
        usleep(SLEEP_NUM);
        EXPECT_TRUE(WIFEXITED(status) > 0) << "ErrInfo: Exit error";
        EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: WEXITSTATUS is " << WEXITSTATUS(status);
    }
}

/* *
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0140
 * @tc.name       : securityDACTest0140
 * @tc.desc       : [C- SECURITY -0200]
 * @tc.size       : MEDIUM
 * @tc.type       : FUNC
 */
HWTEST_F(DacTestSuite, securityDACTest0140, TestSize.Level2)
{
    int ret;
    int status = 0;
    CreateTxt();
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
        NoChownCapFunc();
        // uid=gid=0
        setuid(UID0);
        setgid(GID0);
        ret = chmod("/storage/DacTestSuite/Dac.txt", CHMOD700);
        if (ret != 0) {
            LOG("Failed in chmod 700");
            exitCode = 1;
        }
        ret = chmod("/storage/DacTestSuite/Dac.txt", CHMOD111);
        if (ret != 0) {
            LOG("Failed in chmod 111");
            exitCode = 1;
        }
        ret = chmod("/storage/DacTestSuite/Dac.txt", -1);
        if (ret != 0) {
            LOG("Failed in chmod -1");
            exitCode = 1;
        }
        ret = chmod("/storage/DacTestSuite/Dac.txt", -777);
        if (ret != 0) {
            LOG("Failed in chmod -777");
            exitCode = 1;
        }
        // uid=gid=10000
        setuid(UID10000);
        setgid(GID10000);
        EXPECT_EQ(ret, 0) << "ErrInfo: Set gid failed,now gid=" << getgid();
        ret = chmod("/storage/DacTestSuite/Dac.txt", CHMOD777);
        EXPECT_EQ(ret, -1) << "ErrInfo: Chmod 777 with wrong uid,now uid=" << getuid();
        // restore
        setuid(UID0);
        setgid(GID0);
        chmod("/storage/DacTestSuite/Dac.txt", CHMOD777);
        exit(exitCode);
    } else {
        waitpid(pid, &status, 0);
        usleep(SLEEP_NUM);
        EXPECT_TRUE(WIFEXITED(status) > 0) << "ErrInfo: Exit error";
        EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: WEXITSTATUS is " << WEXITSTATUS(status);
    }
}