ActsSystemDevDacTest.cpp 85.5 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
W
wenjun 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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 <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "gtest/gtest.h"
M
mamingshuai 已提交
22 23
#include "ActsCapabilityTest.h"
#include "DACFileSystemTest.h"
W
wenjun 已提交
24 25 26 27

using namespace std;
using namespace testing::ext;

M
mamingshuai 已提交
28
#if defined(LITE_FS_JFFS2) || defined(LITE_FS_VFAT)
W
wenjun 已提交
29 30 31
static void CreateDevDir()
{
    int ret;
M
mamingshuai 已提交
32 33 34 35 36 37 38 39
    // Initialize the process and set the uid and gid of the process to zero
    SetUidGid(UID0, GID0);
    // Create a directory 'DACDIR0' in the directory '/dev'
    ret = mkdir("/dev/" DACDIR0, CHMOD777);
    ASSERT_EQ(ret, 0) << "ErrInfo: Failed to create the directory '/dev/DACDIR0'";
    // Create a directory 'DACDIR0_DACDIR0' in the directory '/dev/DACDIR0'
    ret = mkdir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0, CHMOD777);
    ASSERT_EQ(ret, 0) << "ErrInfo: Failed to create the directory '/dev/DACDIR0/DACDIR0_DACDIR0'";
W
wenjun 已提交
40 41 42 43 44 45
}

static void CreateDevTxt()
{
    int ret;
    int fd = 0;
M
mamingshuai 已提交
46 47 48 49 50 51 52
    // Initialize the process and set the uid and gid of the process to zero
    SetUidGid(UID0, GID0);
    // Create a directory 'DACDIR0' in the directory '/dev'
    ret = mkdir("/dev/" DACDIR0, CHMOD777);
    ASSERT_EQ(ret, 0) << "ErrInfo: Failed to create the directory '/dev/DACDIR0'";
    //  Failed to create a file 'DACDIR0_DACFILE0' in the directory '/dev/DACDIR0'
    fd = open("/dev/" DACDIR0 "/" DACDIR0_DACFILE0, O_WRONLY | O_CREAT | O_TRUNC, CHMOD777);
W
wenjun 已提交
53
    if (fd >= 0) {
M
mamingshuai 已提交
54
        EXPECT_EQ(fd, FALSE) << "ErrInfo: Create a file 'DACDIR0_DACFILE0' in the directory '/dev/DACDIR0'";
W
wenjun 已提交
55 56 57 58
        close(fd);
    }
}

M
mamingshuai 已提交
59
/*
W
wenjun 已提交
60
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0290
M
mamingshuai 已提交
61 62
 * @tc.name       : DAC control mechanism-File system dev-System call rmdir-UID0-GID1-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 333
W
wenjun 已提交
63 64
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
65
HWTEST_F(DacTestSuite, DACTest0290, Function | MediumTest | Level2)
W
wenjun 已提交
66 67
{
    int ret;
M
mamingshuai 已提交
68
    // Preset action: Create a directory
W
wenjun 已提交
69
    CreateDevDir();
M
mamingshuai 已提交
70 71 72 73
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD333);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 333";
    // Preset action: Fork a sub process
W
wenjun 已提交
74 75 76 77 78
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
79 80 81 82 83 84
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID555);
        // Step 3: Invoke the interface to operate the file system
        ret = rmdir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0);
W
wenjun 已提交
85
        if (ret != 0) {
M
mamingshuai 已提交
86
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
87 88
            exitCode = 1;
        }
M
mamingshuai 已提交
89
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
90 91 92
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
93
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
94
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
95 96
        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;
W
wenjun 已提交
97 98 99
    }
}

M
mamingshuai 已提交
100
/*
W
wenjun 已提交
101
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0300
M
mamingshuai 已提交
102 103
 * @tc.name       : DAC control mechanism-File system dev-System call rmdir-UID1-GID0-Capability
                    CAPDACOVERRIDE-Permission 611
W
wenjun 已提交
104 105
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
106
HWTEST_F(DacTestSuite, DACTest0300, Function | MediumTest | Level2)
W
wenjun 已提交
107 108
{
    int ret;
M
mamingshuai 已提交
109
    // Preset action: Create a directory
W
wenjun 已提交
110
    CreateDevDir();
M
mamingshuai 已提交
111 112 113 114
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD611);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 611";
    // Preset action: Fork a sub process
W
wenjun 已提交
115 116 117 118 119
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
120 121 122 123 124 125
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID0);
        // Step 3: Invoke the interface to operate the file system
        ret = rmdir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0);
W
wenjun 已提交
126
        if (ret != 0) {
M
mamingshuai 已提交
127
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
128 129
            exitCode = 1;
        }
M
mamingshuai 已提交
130
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
131 132 133
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
134
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
135
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
136 137
        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;
W
wenjun 已提交
138 139 140
    }
}

M
mamingshuai 已提交
141
/*
W
wenjun 已提交
142
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0310
M
mamingshuai 已提交
143 144
 * @tc.name       : DAC control mechanism-File system dev-System call rmdir-UID1-GID1-Capability
                    CAPDACOVERRIDE-Permission 105
W
wenjun 已提交
145 146
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
147
HWTEST_F(DacTestSuite, DACTest0310, Function | MediumTest | Level2)
W
wenjun 已提交
148 149
{
    int ret;
M
mamingshuai 已提交
150
    // Preset action: Create a directory
W
wenjun 已提交
151
    CreateDevDir();
M
mamingshuai 已提交
152 153 154 155
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD105);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 105";
    // Preset action: Fork a sub process
W
wenjun 已提交
156 157 158 159 160
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
161 162 163 164 165 166
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
        // Step 3: Invoke the interface to operate the file system
        ret = rmdir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0);
W
wenjun 已提交
167
        if (ret != 0) {
M
mamingshuai 已提交
168
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
169 170
            exitCode = 1;
        }
M
mamingshuai 已提交
171
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
172 173 174
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
175
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
176
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
177 178
        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;
W
wenjun 已提交
179 180 181
    }
}

M
mamingshuai 已提交
182
/*
W
wenjun 已提交
183
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0320
M
mamingshuai 已提交
184 185
 * @tc.name       : DAC control mechanism-File system dev-System call rmdir-UID1-GID1-Capability
                    CAPDACOVERRIDE-Permission 555
W
wenjun 已提交
186 187
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
188
HWTEST_F(DacTestSuite, DACTest0320, Function | MediumTest | Level2)
W
wenjun 已提交
189 190
{
    int ret;
M
mamingshuai 已提交
191
    // Preset action: Create a directory
W
wenjun 已提交
192
    CreateDevDir();
M
mamingshuai 已提交
193 194 195 196
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD555);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 555";
    // Preset action: Fork a sub process
W
wenjun 已提交
197 198 199 200 201
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
202 203 204 205 206 207
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
        // Step 3: Invoke the interface to operate the file system
        ret = rmdir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0);
W
wenjun 已提交
208
        if (ret != 0) {
M
mamingshuai 已提交
209
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
210 211
            exitCode = 1;
        }
M
mamingshuai 已提交
212
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
213 214 215
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
216
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
217
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
218 219
        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;
W
wenjun 已提交
220 221 222
    }
}

M
mamingshuai 已提交
223
/*
W
wenjun 已提交
224
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0330
M
mamingshuai 已提交
225 226
 * @tc.name       : DAC control mechanism-File system dev-System call rmdir-UID1-GID1-Groups contain-Capability
                    CAPDACREADSEARCH-Permission 666
W
wenjun 已提交
227 228
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
229
HWTEST_F(DacTestSuite, DACTest0330, Function | MediumTest | Level2)
W
wenjun 已提交
230 231 232
{
    int ret;
    gid_t list[SINGLESIZE] = {0};
M
mamingshuai 已提交
233
    // Preset action: Create a directory
W
wenjun 已提交
234
    CreateDevDir();
M
mamingshuai 已提交
235 236 237 238
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD666);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 666";
    // Preset action: Fork a sub process
W
wenjun 已提交
239 240 241 242 243
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
244 245 246 247
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACOVERRIDEAndREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
W
wenjun 已提交
248
        setgroups(SINGLESIZE, list);
M
mamingshuai 已提交
249 250 251 252
        // Step 3: Invoke the interface to operate the file system
        ret = rmdir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0);
        if (ret != FALSE) {
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
253 254
            exitCode = 1;
        }
M
mamingshuai 已提交
255
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
256 257 258
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
259
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
260
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
261 262
        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;
W
wenjun 已提交
263 264 265
    }
}

M
mamingshuai 已提交
266
/*
W
wenjun 已提交
267
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0340
M
mamingshuai 已提交
268
 * @tc.name       : DAC control mechanism-File system dev-System call stat-UID0-GID1-Capability NULL-Permission 777
W
wenjun 已提交
269 270
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
271
HWTEST_F(DacTestSuite, DACTest0340, Function | MediumTest | Level2)
W
wenjun 已提交
272 273
{
    int ret;
M
mamingshuai 已提交
274
    // Preset action: Create a directory
W
wenjun 已提交
275
    CreateDevDir();
M
mamingshuai 已提交
276 277 278 279
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD777);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 777";
    // Preset action: Fork a sub process
W
wenjun 已提交
280 281 282 283 284
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
285 286 287 288 289 290
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACOVERRIDEAndREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID555);
        // Step 3: Invoke the interface to operate the file system
        ret = rmdir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0);
W
wenjun 已提交
291
        if (ret != 0) {
M
mamingshuai 已提交
292
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
293 294
            exitCode = 1;
        }
M
mamingshuai 已提交
295
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
296 297 298
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
299
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
300
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
301 302
        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;
W
wenjun 已提交
303 304 305
    }
}

M
mamingshuai 已提交
306
/*
W
wenjun 已提交
307
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0350
M
mamingshuai 已提交
308 309
 * @tc.name       : DAC control mechanism-File system dev-System call stat-UID1-GID0-Capability
                    CAPDACOVERRIDE-Permission 166
W
wenjun 已提交
310 311
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
312
HWTEST_F(DacTestSuite, DACTest0350, Function | MediumTest | Level2)
W
wenjun 已提交
313 314 315
{
    int ret;
    struct stat buf = { 0 };
M
mamingshuai 已提交
316
    // Preset action: Create a directory
W
wenjun 已提交
317
    CreateDevDir();
M
mamingshuai 已提交
318 319 320 321
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD166);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 166";
    // Preset action: Fork a sub process
W
wenjun 已提交
322 323 324 325 326
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
327 328 329 330 331 332
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID0);
        // Step 3: Invoke the interface to operate the file system
        ret = stat("/dev/" DACDIR0 "/" DACDIR0_DACDIR0, &buf);
W
wenjun 已提交
333
        if (ret != 0) {
M
mamingshuai 已提交
334
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
335 336
            exitCode = 1;
        }
M
mamingshuai 已提交
337
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
338 339 340
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
341
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
342
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
343 344
        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;
W
wenjun 已提交
345 346 347
    }
}

M
mamingshuai 已提交
348
/*
W
wenjun 已提交
349
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0360
M
mamingshuai 已提交
350 351
 * @tc.name       : DAC control mechanism-File system dev-System call stat-UID0-GID0-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 111
W
wenjun 已提交
352 353
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
354
HWTEST_F(DacTestSuite, DACTest0360, Function | MediumTest | Level2)
W
wenjun 已提交
355 356 357
{
    int ret;
    struct stat buf = { 0 };
M
mamingshuai 已提交
358
    // Preset action: Create a directory
W
wenjun 已提交
359
    CreateDevDir();
M
mamingshuai 已提交
360 361 362 363
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD111);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 111";
    // Preset action: Fork a sub process
W
wenjun 已提交
364 365 366 367 368
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
369 370 371 372 373 374
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID0);
        // Step 3: Invoke the interface to operate the file system
        ret = stat("/dev/" DACDIR0 "/" DACDIR0_DACDIR0, &buf);
W
wenjun 已提交
375
        if (ret != 0) {
M
mamingshuai 已提交
376
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
377 378
            exitCode = 1;
        }
M
mamingshuai 已提交
379
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
380 381 382
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
383
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
384
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
385 386
        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;
W
wenjun 已提交
387 388 389
    }
}

M
mamingshuai 已提交
390
/*
W
wenjun 已提交
391
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0370
M
mamingshuai 已提交
392 393
 * @tc.name       : DAC control mechanism-File system dev-System call stat-UID0-GID0-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 210
W
wenjun 已提交
394 395
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
396
HWTEST_F(DacTestSuite, DACTest0370, Function | MediumTest | Level2)
W
wenjun 已提交
397 398 399
{
    int ret;
    struct stat buf = { 0 };
M
mamingshuai 已提交
400
    // Preset action: Create a directory
W
wenjun 已提交
401
    CreateDevDir();
M
mamingshuai 已提交
402 403 404 405
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD210);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 210";
    // Preset action: Fork a sub process
W
wenjun 已提交
406 407 408 409 410
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
411 412 413 414 415 416
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID0);
        // Step 3: Invoke the interface to operate the file system
        ret = stat("/dev/" DACDIR0 "/" DACDIR0_DACDIR0, &buf);
W
wenjun 已提交
417
        if (ret != 0) {
M
mamingshuai 已提交
418
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
419 420
            exitCode = 1;
        }
M
mamingshuai 已提交
421
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
422 423 424
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
425
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
426
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
427 428
        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;
W
wenjun 已提交
429 430 431
    }
}

M
mamingshuai 已提交
432
/*
W
wenjun 已提交
433
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0380
M
mamingshuai 已提交
434 435
 * @tc.name       : DAC control mechanism-File system dev-System call stat-UID1-GID1-Groups contain-Capability
                    CAPDACOVERRIDE-Permission 655
W
wenjun 已提交
436 437
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
438
HWTEST_F(DacTestSuite, DACTest0380, Function | MediumTest | Level2)
W
wenjun 已提交
439 440 441 442
{
    int ret;
    struct stat buf = { 0 };
    gid_t list[SINGLESIZE] = {0};
M
mamingshuai 已提交
443
    // Preset action: Create a directory
W
wenjun 已提交
444
    CreateDevDir();
M
mamingshuai 已提交
445 446 447 448
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD655);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 655";
    // Preset action: Fork a sub process
W
wenjun 已提交
449 450 451 452 453
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
454 455 456 457
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
W
wenjun 已提交
458
        setgroups(SINGLESIZE, list);
M
mamingshuai 已提交
459 460
        // Step 3: Invoke the interface to operate the file system
        ret = stat("/dev/" DACDIR0 "/" DACDIR0_DACDIR0, &buf);
W
wenjun 已提交
461
        if (ret != 0) {
M
mamingshuai 已提交
462
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
463 464
            exitCode = 1;
        }
M
mamingshuai 已提交
465
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
466 467 468
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
469
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
470
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
471 472
        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;
W
wenjun 已提交
473 474 475
    }
}

M
mamingshuai 已提交
476
/*
W
wenjun 已提交
477
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0390
M
mamingshuai 已提交
478 479
 * @tc.name       : DAC control mechanism-File system dev-System call rename-UID1-GID0-Capability
                    CAPDACREADSEARCH-Permission 570
W
wenjun 已提交
480 481
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
482
HWTEST_F(DacTestSuite, DACTest0390, Function | MediumTest | Level2)
W
wenjun 已提交
483 484
{
    int ret;
M
mamingshuai 已提交
485
    // Preset action: Create a directory
W
wenjun 已提交
486
    CreateDevDir();
M
mamingshuai 已提交
487 488 489 490
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD570);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 570";
    // Preset action: Fork a sub process
W
wenjun 已提交
491 492 493 494 495
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
496 497 498 499 500 501
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACOVERRIDE();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID0);
        // Step 3: Invoke the interface to operate the file system
        ret = rename("/dev/" DACDIR0 "/" DACDIR0_DACDIR0, "/dev/" DACDIR0 "/" DACDIR0_DACDIR1);
W
wenjun 已提交
502
        if (ret != 0) {
M
mamingshuai 已提交
503
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
504 505
            exitCode = 1;
        }
M
mamingshuai 已提交
506
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
507 508 509
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
510
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
511
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
512 513
        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;
W
wenjun 已提交
514 515 516
    }
}

M
mamingshuai 已提交
517
/*
W
wenjun 已提交
518
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0400
M
mamingshuai 已提交
519 520
 * @tc.name       : DAC control mechanism-File system dev-System call rename-UID1-GID0-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 306
W
wenjun 已提交
521 522
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
523
HWTEST_F(DacTestSuite, DACTest0400, Function | MediumTest | Level2)
W
wenjun 已提交
524 525
{
    int ret;
M
mamingshuai 已提交
526
    // Preset action: Create a directory
W
wenjun 已提交
527
    CreateDevDir();
M
mamingshuai 已提交
528 529 530 531
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD306);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 306";
    // Preset action: Fork a sub process
W
wenjun 已提交
532 533 534 535 536
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
537 538 539 540 541 542
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID0);
        // Step 3: Invoke the interface to operate the file system
        ret = rename("/dev/" DACDIR0 "/" DACDIR0_DACDIR0, "/dev/" DACDIR0 "/" DACDIR0_DACDIR1);
W
wenjun 已提交
543
        if (ret != 0) {
M
mamingshuai 已提交
544
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
545 546
            exitCode = 1;
        }
M
mamingshuai 已提交
547
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
548 549 550
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
551
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
552
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
553 554
        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;
W
wenjun 已提交
555 556 557
    }
}

M
mamingshuai 已提交
558
/*
W
wenjun 已提交
559
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0410
M
mamingshuai 已提交
560 561
 * @tc.name       : DAC control mechanism-File system dev-System call rename-UID1-GID1-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 027
W
wenjun 已提交
562 563
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
564
HWTEST_F(DacTestSuite, DACTest0410, Function | MediumTest | Level2)
W
wenjun 已提交
565 566
{
    int ret;
M
mamingshuai 已提交
567
    // Preset action: Create a directory
W
wenjun 已提交
568
    CreateDevDir();
M
mamingshuai 已提交
569 570 571 572
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD027);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 027";
    // Preset action: Fork a sub process
W
wenjun 已提交
573 574 575 576 577
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
578 579 580 581 582 583
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
        // Step 3: Invoke the interface to operate the file system
        ret = rename("/dev/" DACDIR0 "/" DACDIR0_DACDIR0, "/dev/" DACDIR0 "/" DACDIR0_DACDIR1);
W
wenjun 已提交
584
        if (ret != 0) {
M
mamingshuai 已提交
585
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
586 587
            exitCode = 1;
        }
M
mamingshuai 已提交
588
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
589 590 591
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
592
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
593
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
594 595
        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;
W
wenjun 已提交
596 597 598
    }
}

M
mamingshuai 已提交
599
/*
W
wenjun 已提交
600
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0420
M
mamingshuai 已提交
601 602
 * @tc.name       : DAC control mechanism-File system dev-System call chdir-UID0-GID1-Capability
                    CAPDACREADSEARCH-Permission 401
W
wenjun 已提交
603 604
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
605
HWTEST_F(DacTestSuite, DACTest0420, Function | MediumTest | Level2)
W
wenjun 已提交
606 607
{
    int ret;
M
mamingshuai 已提交
608
    // Preset action: Create a directory
W
wenjun 已提交
609
    CreateDevDir();
M
mamingshuai 已提交
610 611 612 613
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD401);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 401";
    // Preset action: Fork a sub process
W
wenjun 已提交
614 615 616 617 618
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
619 620 621 622 623 624 625 626
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACOVERRIDE();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID555);
        // Step 3: Invoke the interface to operate the file system
        ret = chdir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0);
        if (ret != FALSE) {
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
627 628
            exitCode = 1;
        }
M
mamingshuai 已提交
629
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
630 631 632
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
633
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
634
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
635 636
        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;
W
wenjun 已提交
637 638 639
    }
}

M
mamingshuai 已提交
640
/*
W
wenjun 已提交
641
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0430
M
mamingshuai 已提交
642 643
 * @tc.name       : DAC control mechanism-File system dev-System call chdir-UID0-GID1-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 507
W
wenjun 已提交
644 645
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
646
HWTEST_F(DacTestSuite, DACTest0430, Function | MediumTest | Level2)
W
wenjun 已提交
647 648
{
    int ret;
M
mamingshuai 已提交
649
    // Preset action: Create a directory
W
wenjun 已提交
650
    CreateDevDir();
M
mamingshuai 已提交
651 652 653 654
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD507);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 507";
    // Preset action: Fork a sub process
W
wenjun 已提交
655 656 657 658 659
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
660 661 662 663 664 665
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID555);
        // Step 3: Invoke the interface to operate the file system
        ret = chdir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0);
W
wenjun 已提交
666
        if (ret != 0) {
M
mamingshuai 已提交
667
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
668 669
            exitCode = 1;
        }
M
mamingshuai 已提交
670
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
671 672 673
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
674
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
675
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
676 677
        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;
W
wenjun 已提交
678 679 680
    }
}

M
mamingshuai 已提交
681
/*
W
wenjun 已提交
682
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0440
M
mamingshuai 已提交
683 684
 * @tc.name       : DAC control mechanism-File system dev-System call chdir-UID0-GID0-Capability
                    CAPDACOVERRIDE-Permission 347
W
wenjun 已提交
685 686
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
687
HWTEST_F(DacTestSuite, DACTest0440, Function | MediumTest | Level2)
W
wenjun 已提交
688 689
{
    int ret;
M
mamingshuai 已提交
690
    // Preset action: Create a directory
W
wenjun 已提交
691
    CreateDevDir();
M
mamingshuai 已提交
692 693 694 695
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD347);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 347";
    // Preset action: Fork a sub process
W
wenjun 已提交
696 697 698 699 700
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
701 702 703 704 705 706
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID0);
        // Step 3: Invoke the interface to operate the file system
        ret = chdir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0);
W
wenjun 已提交
707
        if (ret != 0) {
M
mamingshuai 已提交
708
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
709 710
            exitCode = 1;
        }
M
mamingshuai 已提交
711
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
712 713 714
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
715
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
716
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
717 718
        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;
W
wenjun 已提交
719 720 721
    }
}

M
mamingshuai 已提交
722
/*
W
wenjun 已提交
723
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0450
M
mamingshuai 已提交
724 725
 * @tc.name       : DAC control mechanism-File system dev-System call chdir-UID1-GID1-Capability
                    CAPDACOVERRIDE-Permission 063
W
wenjun 已提交
726 727
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
728
HWTEST_F(DacTestSuite, DACTest0450, Function | MediumTest | Level2)
W
wenjun 已提交
729 730
{
    int ret;
M
mamingshuai 已提交
731
    // Preset action: Create a directory
W
wenjun 已提交
732
    CreateDevDir();
M
mamingshuai 已提交
733 734 735 736
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD063);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 063";
    // Preset action: Fork a sub process
W
wenjun 已提交
737 738 739 740 741
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
742 743 744 745 746 747
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
        // Step 3: Invoke the interface to operate the file system
        ret = chdir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0);
W
wenjun 已提交
748
        if (ret != 0) {
M
mamingshuai 已提交
749
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
750 751
            exitCode = 1;
        }
M
mamingshuai 已提交
752
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
753 754 755
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
756
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
757
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
758 759
        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;
W
wenjun 已提交
760 761 762
    }
}

M
mamingshuai 已提交
763
/*
W
wenjun 已提交
764
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0460
M
mamingshuai 已提交
765 766
 * @tc.name       : DAC control mechanism-File system dev-System call chdir-UID1-GID1-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 230
W
wenjun 已提交
767 768
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
769
HWTEST_F(DacTestSuite, DACTest0460, Function | MediumTest | Level2)
W
wenjun 已提交
770 771
{
    int ret;
M
mamingshuai 已提交
772
    // Preset action: Create a directory
W
wenjun 已提交
773
    CreateDevDir();
M
mamingshuai 已提交
774 775 776 777
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD230);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 230";
    // Preset action: Fork a sub process
W
wenjun 已提交
778 779 780 781 782
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
783 784 785 786 787 788
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
        // Step 3: Invoke the interface to operate the file system
        ret = chdir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0);
W
wenjun 已提交
789
        if (ret != 0) {
M
mamingshuai 已提交
790
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
791 792
            exitCode = 1;
        }
M
mamingshuai 已提交
793
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
794 795 796
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
797
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
798
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
799 800
        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;
W
wenjun 已提交
801 802 803
    }
}

M
mamingshuai 已提交
804
/*
W
wenjun 已提交
805
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0470
M
mamingshuai 已提交
806 807
 * @tc.name       : DAC control mechanism-File system dev-System call chdir-UID1-GID1-Groups contain-Capability
                    CAPDACOVERRIDE-Permission 724
W
wenjun 已提交
808 809
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
810
HWTEST_F(DacTestSuite, DACTest0470, Function | MediumTest | Level2)
W
wenjun 已提交
811 812 813
{
    int ret;
    gid_t list[SINGLESIZE] = {0};
M
mamingshuai 已提交
814
    // Preset action: Create a directory
W
wenjun 已提交
815
    CreateDevDir();
M
mamingshuai 已提交
816 817 818 819
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD724);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 724";
    // Preset action: Fork a sub process
W
wenjun 已提交
820 821 822 823 824
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
825 826 827 828
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
W
wenjun 已提交
829
        setgroups(SINGLESIZE, list);
M
mamingshuai 已提交
830 831
        // Step 3: Invoke the interface to operate the file system
        ret = chdir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0);
W
wenjun 已提交
832
        if (ret != 0) {
M
mamingshuai 已提交
833
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
834 835
            exitCode = 1;
        }
M
mamingshuai 已提交
836
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
837 838 839
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
840
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
841
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
842 843
        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;
W
wenjun 已提交
844 845 846
    }
}

M
mamingshuai 已提交
847
/*
W
wenjun 已提交
848
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0480
M
mamingshuai 已提交
849 850
 * @tc.name       : DAC control mechanism-File system dev-System call execute-UID1-GID0-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 702
W
wenjun 已提交
851 852
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
853
HWTEST_F(DacTestSuite, DACTest0480, Function | MediumTest | Level2)
W
wenjun 已提交
854 855 856 857
{
    int ret;
    char *argv[] = {nullptr, nullptr};
    char *envp[] = {nullptr};
M
mamingshuai 已提交
858
    // Preset action: Create a file
W
wenjun 已提交
859
    CreateDevTxt();
M
mamingshuai 已提交
860 861
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD702);
W
wenjun 已提交
862
    // dev/shm can not change chmod
M
mamingshuai 已提交
863 864
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
865 866 867 868 869
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
870 871 872 873 874 875
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID0);
        // Step 3: Invoke the interface to operate the file system
        ret = execve("/dev/" DACDIR0 "/" DACDIR0_DACFILE0, argv, envp);
W
wenjun 已提交
876
        // the file is not elf , can not exec
M
mamingshuai 已提交
877 878
        if (ret != FALSE) {
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
879 880
            exitCode = 1;
        }
M
mamingshuai 已提交
881
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
882 883 884
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
885
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
886
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
887 888
        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;
W
wenjun 已提交
889 890 891
    }
}

M
mamingshuai 已提交
892
/*
W
wenjun 已提交
893
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0490
M
mamingshuai 已提交
894 895
 * @tc.name       : DAC control mechanism-File system dev-System call execute-UID0-GID0-Capability
                    CAPDACOVERRIDE-Permission 473
W
wenjun 已提交
896 897
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
898
HWTEST_F(DacTestSuite, DACTest0490, Function | MediumTest | Level2)
W
wenjun 已提交
899 900 901 902
{
    int ret;
    char *argv[] = {nullptr, nullptr};
    char *envp[] = {nullptr};
M
mamingshuai 已提交
903
    // Preset action: Create a file
W
wenjun 已提交
904
    CreateDevTxt();
M
mamingshuai 已提交
905 906
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD473);
W
wenjun 已提交
907
    // dev/shm can not change chmod
M
mamingshuai 已提交
908 909
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
910 911 912 913 914
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
915 916 917 918 919 920
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID0);
        // Step 3: Invoke the interface to operate the file system
        ret = execve("/dev/" DACDIR0 "/" DACDIR0_DACFILE0, argv, envp);
W
wenjun 已提交
921
        // the file is not elf , can not exec
M
mamingshuai 已提交
922 923
        if (ret != FALSE) {
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
924 925
            exitCode = 1;
        }
M
mamingshuai 已提交
926
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
927 928 929
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
930
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
931
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
932 933
        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;
W
wenjun 已提交
934 935 936
    }
}

M
mamingshuai 已提交
937
/*
W
wenjun 已提交
938
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0500
M
mamingshuai 已提交
939 940
 * @tc.name       : DAC control mechanism-File system dev-System call execute-UID1-GID1-Capability
                    CAPDACOVERRIDE-Permission 261
W
wenjun 已提交
941 942
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
943
HWTEST_F(DacTestSuite, DACTest0500, Function | MediumTest | Level2)
W
wenjun 已提交
944 945 946 947
{
    int ret;
    char *argv[] = {nullptr, nullptr};
    char *envp[] = {nullptr};
M
mamingshuai 已提交
948
    // Preset action: Create a file
W
wenjun 已提交
949
    CreateDevTxt();
M
mamingshuai 已提交
950 951
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD261);
W
wenjun 已提交
952
    // dev/shm can not change chmod
M
mamingshuai 已提交
953 954
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
955 956 957 958 959
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
960 961 962 963 964 965
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
        // Step 3: Invoke the interface to operate the file system
        ret = execve("/dev/" DACDIR0 "/" DACDIR0_DACFILE0, argv, envp);
W
wenjun 已提交
966
        // the file is not elf , can not exec
M
mamingshuai 已提交
967 968
        if (ret != FALSE) {
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
969 970
            exitCode = 1;
        }
M
mamingshuai 已提交
971
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
972 973 974
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
975
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
976
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
977 978
        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;
W
wenjun 已提交
979 980 981
    }
}

M
mamingshuai 已提交
982
/*
W
wenjun 已提交
983
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0510
M
mamingshuai 已提交
984 985
 * @tc.name       : DAC control mechanism-File system dev-System call access-UID0-GID1-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 460
W
wenjun 已提交
986 987
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
988
HWTEST_F(DacTestSuite, DACTest0510, Function | MediumTest | Level2)
W
wenjun 已提交
989 990
{
    int ret;
M
mamingshuai 已提交
991
    // Preset action: Create a file
W
wenjun 已提交
992
    CreateDevTxt();
M
mamingshuai 已提交
993 994
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD460);
W
wenjun 已提交
995
    // dev/shm can not change chmod
M
mamingshuai 已提交
996 997
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
998 999 1000 1001 1002
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1003 1004 1005 1006 1007 1008 1009 1010
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID555);
        // Step 3: Invoke the interface to operate the file system
        ret = access("/dev/" DACDIR0 "/" DACDIR0_DACFILE0, F_OK);
        if (ret != FALSE) {
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1011 1012
            exitCode = 1;
        }
M
mamingshuai 已提交
1013
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1014 1015 1016
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1017
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1018
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1019 1020
        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;
W
wenjun 已提交
1021 1022 1023
    }
}

M
mamingshuai 已提交
1024
/*
W
wenjun 已提交
1025
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0520
M
mamingshuai 已提交
1026 1027
 * @tc.name       : DAC control mechanism-File system dev-System call access-UID1-GID0-Capability
                    CAPDACOVERRIDE-Permission 562
W
wenjun 已提交
1028 1029
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1030
HWTEST_F(DacTestSuite, DACTest0520, Function | MediumTest | Level2)
W
wenjun 已提交
1031 1032
{
    int ret;
M
mamingshuai 已提交
1033
    // Preset action: Create a file
W
wenjun 已提交
1034
    CreateDevTxt();
M
mamingshuai 已提交
1035 1036
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD562);
W
wenjun 已提交
1037
    // dev/shm can not change chmod
M
mamingshuai 已提交
1038 1039
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
1040 1041 1042 1043 1044
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1045 1046 1047 1048 1049 1050 1051 1052
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID555);
        // Step 3: Invoke the interface to operate the file system
        ret = access("/dev/" DACDIR0 "/" DACDIR0_DACFILE0, F_OK);
        if (ret != FALSE) {
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1053 1054
            exitCode = 1;
        }
M
mamingshuai 已提交
1055
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1056 1057 1058
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1059
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1060
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1061 1062
        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;
W
wenjun 已提交
1063 1064 1065 1066
    }
}


M
mamingshuai 已提交
1067
/*
W
wenjun 已提交
1068
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0530
M
mamingshuai 已提交
1069 1070
 * @tc.name       : DAC control mechanism-File system dev-System call access-UID0-GID0-Capability
                    CAPDACOVERRIDE-Permission 076
W
wenjun 已提交
1071 1072
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1073
HWTEST_F(DacTestSuite, DACTest0530, Function | MediumTest | Level2)
W
wenjun 已提交
1074 1075
{
    int ret;
M
mamingshuai 已提交
1076
    // Preset action: Create a file
W
wenjun 已提交
1077
    CreateDevTxt();
M
mamingshuai 已提交
1078 1079
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD076);
W
wenjun 已提交
1080
    // dev/shm can not change chmod
M
mamingshuai 已提交
1081 1082
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
1083 1084 1085 1086 1087
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1088 1089 1090 1091 1092 1093 1094 1095
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID0);
        // Step 3: Invoke the interface to operate the file system
        ret = access("/dev/" DACDIR0 "/" DACDIR0_DACFILE0, F_OK);
        if (ret != FALSE) {
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1096 1097
            exitCode = 1;
        }
M
mamingshuai 已提交
1098
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1099 1100 1101
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1102
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1103
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1104 1105
        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;
W
wenjun 已提交
1106 1107 1108
    }
}

M
mamingshuai 已提交
1109
/*
W
wenjun 已提交
1110
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0540
M
mamingshuai 已提交
1111 1112
 * @tc.name       : DAC control mechanism-File system dev-System call access-UID1-GID1-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 305
W
wenjun 已提交
1113 1114
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1115
HWTEST_F(DacTestSuite, DACTest0540, Function | MediumTest | Level2)
W
wenjun 已提交
1116 1117 1118
{
    int ret;
    gid_t list[SINGLESIZE] = {0};
M
mamingshuai 已提交
1119
    // Preset action: Create a file
W
wenjun 已提交
1120
    CreateDevTxt();
M
mamingshuai 已提交
1121 1122
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD076);
W
wenjun 已提交
1123
    // dev/shm can not change chmod
M
mamingshuai 已提交
1124 1125
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
1126 1127 1128 1129 1130
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1131 1132 1133 1134
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
W
wenjun 已提交
1135
        setgroups(SINGLESIZE, list);
M
mamingshuai 已提交
1136 1137 1138 1139
        // Step 3: Invoke the interface to operate the file system
        ret = access("/dev/" DACDIR0 "/" DACDIR0_DACFILE0, F_OK);
        if (ret != FALSE) {
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1140 1141
            exitCode = 1;
        }
M
mamingshuai 已提交
1142
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1143 1144 1145
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1146
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1147
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1148 1149
        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;
W
wenjun 已提交
1150 1151 1152
    }
}

M
mamingshuai 已提交
1153
/*
W
wenjun 已提交
1154
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0550
M
mamingshuai 已提交
1155 1156
 * @tc.name       : DAC control mechanism-File system dev-System call access-UID1-GID1-Groups contain-Capability
                    CAPDACOVERRIDE-Permission 132
W
wenjun 已提交
1157 1158
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1159
HWTEST_F(DacTestSuite, DACTest0550, Function | MediumTest | Level2)
W
wenjun 已提交
1160 1161 1162
{
    int ret;
    gid_t list[SINGLESIZE] = {0};
M
mamingshuai 已提交
1163
    // Preset action: Create a file
W
wenjun 已提交
1164
    CreateDevTxt();
M
mamingshuai 已提交
1165 1166
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD132);
W
wenjun 已提交
1167
    // dev/shm can not change chmod
M
mamingshuai 已提交
1168 1169
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
1170 1171 1172 1173 1174
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1175 1176 1177 1178
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
W
wenjun 已提交
1179
        setgroups(SINGLESIZE, list);
M
mamingshuai 已提交
1180 1181 1182 1183
        // Step 3: Invoke the interface to operate the file system
        ret = access("/dev/" DACDIR0 "/" DACDIR0_DACFILE0, F_OK);
        if (ret != FALSE) {
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1184 1185
            exitCode = 1;
        }
M
mamingshuai 已提交
1186
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1187 1188 1189
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1190
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1191
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1192 1193
        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;
W
wenjun 已提交
1194 1195 1196
    }
}

M
mamingshuai 已提交
1197
/*
W
wenjun 已提交
1198
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0560
M
mamingshuai 已提交
1199 1200
 * @tc.name       : DAC control mechanism-File system dev-System call access-UID1-GID1-Groups contain-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 241
W
wenjun 已提交
1201 1202
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1203
HWTEST_F(DacTestSuite, DACTest0560, Function | MediumTest | Level2)
W
wenjun 已提交
1204 1205 1206
{
    int ret;
    gid_t list[SINGLESIZE] = {0};
M
mamingshuai 已提交
1207
    // Preset action: Create a file
W
wenjun 已提交
1208
    CreateDevTxt();
M
mamingshuai 已提交
1209 1210
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD241);
W
wenjun 已提交
1211
    // dev/shm can not change chmod
M
mamingshuai 已提交
1212 1213
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
1214 1215 1216 1217 1218
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1219 1220 1221 1222
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
W
wenjun 已提交
1223
        setgroups(SINGLESIZE, list);
M
mamingshuai 已提交
1224 1225 1226 1227
        // Step 3: Invoke the interface to operate the file system
        ret = access("/dev/" DACDIR0 "/" DACDIR0_DACFILE0, F_OK);
        if (ret != FALSE) {
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1228 1229
            exitCode = 1;
        }
M
mamingshuai 已提交
1230
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1231 1232 1233
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1234
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1235
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1236 1237
        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;
W
wenjun 已提交
1238 1239 1240
    }
}

M
mamingshuai 已提交
1241
/*
W
wenjun 已提交
1242
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0570
M
mamingshuai 已提交
1243 1244
 * @tc.name       : DAC control mechanism-File system dev-System call mkdir-UID0-GID1-Capability
                    CAPDACOVERRIDE-Permission 456
W
wenjun 已提交
1245 1246
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1247
HWTEST_F(DacTestSuite, DACTest0570, Function | MediumTest | Level2)
W
wenjun 已提交
1248 1249
{
    int ret;
M
mamingshuai 已提交
1250
    // Preset action: Create a directory
W
wenjun 已提交
1251
    CreateDevDir();
M
mamingshuai 已提交
1252 1253 1254 1255
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0 "/" DACDIR0_DACDIR0, CHMOD456);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 456";
    // Preset action: Fork a sub process
W
wenjun 已提交
1256 1257 1258 1259 1260
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1261 1262 1263 1264 1265 1266
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID555);
        // Step 3: Invoke the interface to operate the file system
        ret = mkdir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0 "/" DACDIR0_DACDIR0_DACDIR0, NORWX);
W
wenjun 已提交
1267
        if (ret != 0) {
M
mamingshuai 已提交
1268
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1269 1270
            exitCode = 1;
        }
M
mamingshuai 已提交
1271
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1272 1273 1274
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1275
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1276
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1277 1278
        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;
W
wenjun 已提交
1279 1280 1281
    }
}

M
mamingshuai 已提交
1282
/*
W
wenjun 已提交
1283
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0580
M
mamingshuai 已提交
1284 1285
 * @tc.name       : DAC control mechanism-File system dev-System call mkdir-UID1-GID0-Capability
                    CAPDACOVERRIDE-Permission 167
W
wenjun 已提交
1286 1287
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1288
HWTEST_F(DacTestSuite, DACTest0580, Function | MediumTest | Level2)
W
wenjun 已提交
1289 1290
{
    int ret;
M
mamingshuai 已提交
1291
    // Preset action: Create a directory
W
wenjun 已提交
1292
    CreateDevDir();
M
mamingshuai 已提交
1293 1294 1295 1296
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0 "/" DACDIR0_DACDIR0, CHMOD167);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 167";
    // Preset action: Fork a sub process
W
wenjun 已提交
1297 1298 1299 1300 1301
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1302 1303 1304 1305 1306 1307
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID0);
        // Step 3: Invoke the interface to operate the file system
        ret = mkdir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0 "/" DACDIR0_DACDIR0_DACDIR0, NORWX);
W
wenjun 已提交
1308
        if (ret != 0) {
M
mamingshuai 已提交
1309
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1310 1311
            exitCode = 1;
        }
M
mamingshuai 已提交
1312
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1313 1314 1315
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1316
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1317
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1318 1319
        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;
W
wenjun 已提交
1320 1321 1322
    }
}

M
mamingshuai 已提交
1323
/*
W
wenjun 已提交
1324
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0590
M
mamingshuai 已提交
1325 1326
 * @tc.name       : DAC control mechanism-File system dev-System call mkdir-UID1-GID1-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 511
W
wenjun 已提交
1327 1328
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1329
HWTEST_F(DacTestSuite, DACTest0590, Function | MediumTest | Level2)
W
wenjun 已提交
1330 1331
{
    int ret;
M
mamingshuai 已提交
1332
    // Preset action: Create a directory
W
wenjun 已提交
1333
    CreateDevDir();
M
mamingshuai 已提交
1334 1335 1336 1337
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0 "/" DACDIR0_DACDIR0, CHMOD511);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 511";
    // Preset action: Fork a sub process
W
wenjun 已提交
1338 1339 1340 1341 1342
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1343 1344 1345 1346 1347 1348
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
        // Step 3: Invoke the interface to operate the file system
        ret = mkdir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0 "/" DACDIR0_DACDIR0_DACDIR0, NORWX);
W
wenjun 已提交
1349
        if (ret != 0) {
M
mamingshuai 已提交
1350
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1351 1352
            exitCode = 1;
        }
M
mamingshuai 已提交
1353
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1354 1355 1356
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1357
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1358
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1359 1360
        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;
W
wenjun 已提交
1361 1362 1363
    }
}

M
mamingshuai 已提交
1364
/*
W
wenjun 已提交
1365
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0600
M
mamingshuai 已提交
1366 1367
 * @tc.name       : DAC control mechanism-File system dev-System call mkdir-UID1-GID1-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 640
W
wenjun 已提交
1368 1369
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1370
HWTEST_F(DacTestSuite, DACTest0600, Function | MediumTest | Level2)
W
wenjun 已提交
1371 1372
{
    int ret;
M
mamingshuai 已提交
1373
    // Preset action: Create a directory
W
wenjun 已提交
1374
    CreateDevDir();
M
mamingshuai 已提交
1375 1376 1377 1378
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0 "/" DACDIR0_DACDIR0, CHMOD640);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 640";
    // Preset action: Fork a sub process
W
wenjun 已提交
1379 1380 1381 1382 1383
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1384 1385 1386 1387 1388 1389 1390 1391
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
        // Step 3: Invoke the interface to operate the file system
        ret = mkdir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0 "/" DACDIR0_DACDIR0_DACDIR0, NORWX);
        if (ret != FALSE) {
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1392 1393
            exitCode = 1;
        }
M
mamingshuai 已提交
1394
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1395 1396 1397
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1398
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1399
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1400 1401
        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;
W
wenjun 已提交
1402 1403 1404
    }
}

M
mamingshuai 已提交
1405
/*
W
wenjun 已提交
1406
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0610
M
mamingshuai 已提交
1407 1408
 * @tc.name       : DAC control mechanism-File system dev-System call unlink-UID0-GID1-Capability
                    CAPDACOVERRIDE-Permission 362
W
wenjun 已提交
1409 1410
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1411
HWTEST_F(DacTestSuite, DACTest0610, Function | MediumTest | Level2)
W
wenjun 已提交
1412 1413
{
    int ret;
M
mamingshuai 已提交
1414
    // Preset action: Create a file
W
wenjun 已提交
1415
    CreateDevTxt();
M
mamingshuai 已提交
1416 1417
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD362);
W
wenjun 已提交
1418
    // dev/shm can not change chmod
M
mamingshuai 已提交
1419 1420
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
1421 1422 1423 1424 1425
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1426 1427 1428 1429 1430 1431 1432 1433
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID555);
        // Step 3: Invoke the interface to operate the file system
        ret = unlink("/dev/" DACDIR0 "/" DACDIR0_DACFILE0);
        if (ret != FALSE) {
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1434 1435
            exitCode = 1;
        }
M
mamingshuai 已提交
1436
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1437 1438 1439
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1440
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1441
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1442 1443
        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;
W
wenjun 已提交
1444 1445 1446
    }
}

M
mamingshuai 已提交
1447
/*
W
wenjun 已提交
1448
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0620
M
mamingshuai 已提交
1449 1450
 * @tc.name       : DAC control mechanism-File system dev-System call unlink-UID0-GID1-Capability
                    CAPDACOVERRIDE-Permission 526
W
wenjun 已提交
1451 1452
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1453
HWTEST_F(DacTestSuite, DACTest0620, Function | MediumTest | Level2)
W
wenjun 已提交
1454 1455
{
    int ret;
M
mamingshuai 已提交
1456
    // Preset action: Create a file
W
wenjun 已提交
1457
    CreateDevTxt();
M
mamingshuai 已提交
1458 1459
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD526);
W
wenjun 已提交
1460
    // dev/shm can not change chmod
M
mamingshuai 已提交
1461 1462
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
1463 1464 1465 1466 1467
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1468 1469 1470 1471 1472 1473 1474 1475
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID555);
        // Step 3: Invoke the interface to operate the file system
        ret = unlink("/dev/" DACDIR0 "/" DACDIR0_DACFILE0);
        if (ret != FALSE) {
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1476 1477
            exitCode = 1;
        }
M
mamingshuai 已提交
1478
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1479 1480 1481
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1482
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1483
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1484 1485
        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;
W
wenjun 已提交
1486 1487 1488
    }
}

M
mamingshuai 已提交
1489
/*
W
wenjun 已提交
1490
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0630
M
mamingshuai 已提交
1491 1492
 * @tc.name       : DAC control mechanism-File system dev-System call unlink-UID0-GID0-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 604
W
wenjun 已提交
1493 1494
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1495
HWTEST_F(DacTestSuite, DACTest0630, Function | MediumTest | Level2)
W
wenjun 已提交
1496 1497
{
    int ret;
M
mamingshuai 已提交
1498
    // Preset action: Create a file
W
wenjun 已提交
1499
    CreateDevTxt();
M
mamingshuai 已提交
1500 1501
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD604);
W
wenjun 已提交
1502
    // dev/shm can not change chmod
M
mamingshuai 已提交
1503 1504
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
1505 1506 1507 1508 1509
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1510 1511 1512 1513 1514 1515 1516 1517
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID0);
        // Step 3: Invoke the interface to operate the file system
        ret = unlink("/dev/" DACDIR0 "/" DACDIR0_DACFILE0);
        if (ret != FALSE) {
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1518 1519
            exitCode = 1;
        }
M
mamingshuai 已提交
1520
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1521 1522 1523
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1524
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1525
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1526 1527
        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;
W
wenjun 已提交
1528 1529 1530
    }
}

M
mamingshuai 已提交
1531
/*
W
wenjun 已提交
1532
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0640
M
mamingshuai 已提交
1533 1534
 * @tc.name       : DAC control mechanism-File system dev-System call unlink-UID0-GID0-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 671
W
wenjun 已提交
1535 1536
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1537
HWTEST_F(DacTestSuite, DACTest0640, Function | MediumTest | Level2)
W
wenjun 已提交
1538 1539
{
    int ret;
M
mamingshuai 已提交
1540
    // Preset action: Create a file
W
wenjun 已提交
1541
    CreateDevTxt();
M
mamingshuai 已提交
1542 1543
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD671);
W
wenjun 已提交
1544
    // dev/shm can not change chmod
M
mamingshuai 已提交
1545 1546
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
1547 1548 1549 1550 1551
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1552 1553 1554 1555 1556 1557 1558 1559
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID0);
        // Step 3: Invoke the interface to operate the file system
        ret = unlink("/dev/" DACDIR0 "/" DACDIR0_DACFILE0);
        if (ret != FALSE) {
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1560 1561
            exitCode = 1;
        }
M
mamingshuai 已提交
1562
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1563 1564 1565
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1566
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1567
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1568 1569
        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;
W
wenjun 已提交
1570 1571 1572
    }
}

M
mamingshuai 已提交
1573
/*
W
wenjun 已提交
1574
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0650
M
mamingshuai 已提交
1575 1576
 * @tc.name       : DAC control mechanism-File system dev-System call unlink-UID0-GID0-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 743
W
wenjun 已提交
1577 1578
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1579
HWTEST_F(DacTestSuite, DACTest0650, Function | MediumTest | Level2)
W
wenjun 已提交
1580 1581
{
    int ret;
M
mamingshuai 已提交
1582
    // Preset action: Create a file
W
wenjun 已提交
1583
    CreateDevTxt();
M
mamingshuai 已提交
1584 1585
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD743);
W
wenjun 已提交
1586
    // dev/shm can not change chmod
M
mamingshuai 已提交
1587 1588
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
1589 1590 1591 1592 1593
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1594 1595 1596 1597 1598 1599 1600 1601
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID0);
        // Step 3: Invoke the interface to operate the file system
        ret = unlink("/dev/" DACDIR0 "/" DACDIR0_DACFILE0);
        if (ret != FALSE) {
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1602 1603
            exitCode = 1;
        }
M
mamingshuai 已提交
1604
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1605 1606 1607
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1608
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1609
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1610 1611
        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;
W
wenjun 已提交
1612 1613 1614
    }
}

M
mamingshuai 已提交
1615
/*
W
wenjun 已提交
1616
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0660
M
mamingshuai 已提交
1617 1618
 * @tc.name       : DAC control mechanism-File system dev-System call unlink-UID1-GID1-Groups contain-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 235
W
wenjun 已提交
1619 1620
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1621
HWTEST_F(DacTestSuite, DACTest0660, Function | MediumTest | Level2)
W
wenjun 已提交
1622 1623 1624
{
    int ret;
    gid_t list[SINGLESIZE] = {0};
M
mamingshuai 已提交
1625
    // Preset action: Create a file
W
wenjun 已提交
1626
    CreateDevTxt();
M
mamingshuai 已提交
1627 1628
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD235);
W
wenjun 已提交
1629
    // dev/shm can not change chmod
M
mamingshuai 已提交
1630 1631
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
1632 1633 1634 1635 1636
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1637 1638 1639 1640
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
W
wenjun 已提交
1641
        setgroups(SINGLESIZE, list);
M
mamingshuai 已提交
1642 1643 1644 1645
        // Step 3: Invoke the interface to operate the file system
        ret = unlink("/dev/" DACDIR0 "/" DACDIR0_DACFILE0);
        if (ret != FALSE) {
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1646 1647
            exitCode = 1;
        }
M
mamingshuai 已提交
1648
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1649 1650 1651
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1652
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1653
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1654 1655
        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;
W
wenjun 已提交
1656 1657 1658
    }
}

M
mamingshuai 已提交
1659
/*
W
wenjun 已提交
1660
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0670
M
mamingshuai 已提交
1661 1662
 * @tc.name       : DAC control mechanism-File system dev-System call open-UID1-GID0-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 371
W
wenjun 已提交
1663 1664
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1665
HWTEST_F(DacTestSuite, DACTest0670, Function | MediumTest | Level2)
W
wenjun 已提交
1666 1667 1668
{
    int ret;
    int fd = 0;
M
mamingshuai 已提交
1669
    // Preset action: Create a file
W
wenjun 已提交
1670
    CreateDevTxt();
M
mamingshuai 已提交
1671 1672
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD371);
W
wenjun 已提交
1673
    // dev/shm can not change chmod
M
mamingshuai 已提交
1674 1675
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
1676 1677 1678 1679 1680
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1681 1682 1683 1684 1685 1686
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID0);
        // Step 3: Invoke the interface to operate the file system
        fd = open("/dev/" DACDIR0 "/" DACDIR0_DACFILE0, O_WRONLY);
W
wenjun 已提交
1687
        if (fd >= 0) {
M
mamingshuai 已提交
1688
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1689 1690 1691
            close(fd);
            exitCode = 1;
        }
M
mamingshuai 已提交
1692
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1693 1694 1695
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1696
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1697
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1698 1699
        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;
W
wenjun 已提交
1700 1701 1702
    }
}

M
mamingshuai 已提交
1703
/*
W
wenjun 已提交
1704
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0680
M
mamingshuai 已提交
1705 1706
 * @tc.name       : DAC control mechanism-File system dev-System call open-UID1-GID0-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 702
W
wenjun 已提交
1707 1708
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1709
HWTEST_F(DacTestSuite, DACTest0680, Function | MediumTest | Level2)
W
wenjun 已提交
1710 1711 1712
{
    int ret;
    int fd = 0;
M
mamingshuai 已提交
1713
    // Preset action: Create a file
W
wenjun 已提交
1714
    CreateDevTxt();
M
mamingshuai 已提交
1715 1716
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD702);
W
wenjun 已提交
1717
    // dev/shm can not change chmod
M
mamingshuai 已提交
1718 1719
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
1720 1721 1722 1723 1724
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1725 1726 1727 1728 1729 1730
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID0);
        // Step 3: Invoke the interface to operate the file system
        fd = open("/dev/" DACDIR0 "/" DACDIR0_DACFILE0, O_WRONLY);
W
wenjun 已提交
1731
        if (fd >= 0) {
M
mamingshuai 已提交
1732
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1733 1734 1735
            close(fd);
            exitCode = 1;
        }
M
mamingshuai 已提交
1736
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1737 1738 1739
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1740
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1741
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1742 1743
        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;
W
wenjun 已提交
1744 1745 1746
    }
}

M
mamingshuai 已提交
1747
/*
W
wenjun 已提交
1748
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0690
M
mamingshuai 已提交
1749 1750
 * @tc.name       : DAC control mechanism-File system dev-System call open-UID1-GID1-Capability
                    CAPDACOVERRIDE-Permission 406
W
wenjun 已提交
1751 1752
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1753
HWTEST_F(DacTestSuite, DACTest0690, Function | MediumTest | Level2)
W
wenjun 已提交
1754 1755 1756
{
    int ret;
    int fd = 0;
M
mamingshuai 已提交
1757
    // Preset action: Create a file
W
wenjun 已提交
1758
    CreateDevTxt();
M
mamingshuai 已提交
1759 1760
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD406);
W
wenjun 已提交
1761
    // dev/shm can not change chmod
M
mamingshuai 已提交
1762 1763
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
1764 1765 1766 1767 1768
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1769 1770 1771 1772 1773 1774
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
        // Step 3: Invoke the interface to operate the file system
        fd = open("/dev/" DACDIR0 "/" DACDIR0_DACFILE0, O_WRONLY);
W
wenjun 已提交
1775
        if (fd >= 0) {
M
mamingshuai 已提交
1776
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1777 1778 1779
            close(fd);
            exitCode = 1;
        }
M
mamingshuai 已提交
1780
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1781 1782 1783
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1784
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1785
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1786 1787
        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;
W
wenjun 已提交
1788 1789 1790
    }
}

M
mamingshuai 已提交
1791
/*
W
wenjun 已提交
1792
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0700
M
mamingshuai 已提交
1793 1794
 * @tc.name       : DAC control mechanism-File system dev-System call open-UID1-GID1-Groups contain-Capability
                    CAPDACOVERRIDE-Permission 257
W
wenjun 已提交
1795 1796
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1797
HWTEST_F(DacTestSuite, DACTest0700, Function | MediumTest | Level2)
W
wenjun 已提交
1798 1799 1800 1801
{
    int ret;
    int fd = 0;
    gid_t list[SINGLESIZE] = {0};
M
mamingshuai 已提交
1802
    // Preset action: Create a file
W
wenjun 已提交
1803
    CreateDevTxt();
M
mamingshuai 已提交
1804 1805
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0, CHMOD257);
W
wenjun 已提交
1806
    // dev/shm can not change chmod
M
mamingshuai 已提交
1807 1808
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod ";
    // Preset action: Fork a sub process
W
wenjun 已提交
1809 1810 1811 1812 1813
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1814 1815 1816 1817
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
W
wenjun 已提交
1818
        setgroups(SINGLESIZE, list);
M
mamingshuai 已提交
1819 1820
        // Step 3: Invoke the interface to operate the file system
        fd = open("/dev/" DACDIR0 "/" DACDIR0_DACFILE0, O_WRONLY);
W
wenjun 已提交
1821
        if (fd >= 0) {
M
mamingshuai 已提交
1822
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1823 1824 1825
            close(fd);
            exitCode = 1;
        }
M
mamingshuai 已提交
1826
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1827 1828 1829
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1830
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1831
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1832 1833
        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;
W
wenjun 已提交
1834 1835 1836
    }
}

M
mamingshuai 已提交
1837
/*
W
wenjun 已提交
1838
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0710
M
mamingshuai 已提交
1839 1840
 * @tc.name       : DAC control mechanism-File system dev-System call opendir-UID0-GID1-Capability
                    CAPDACREADSEARCH_CAPDACOVERRIDE-Permission 750
W
wenjun 已提交
1841 1842
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1843
HWTEST_F(DacTestSuite, DACTest0710, Function | MediumTest | Level2)
W
wenjun 已提交
1844 1845 1846
{
    int ret;
    DIR* dir = nullptr;
M
mamingshuai 已提交
1847
    // Preset action: Create a directory
W
wenjun 已提交
1848
    CreateDevDir();
M
mamingshuai 已提交
1849 1850 1851 1852
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0 "/" DACDIR0_DACDIR0, CHMOD750);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 750";
    // Preset action: Fork a sub process
W
wenjun 已提交
1853 1854 1855 1856 1857
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1858 1859 1860 1861 1862 1863
        // Step 1: Change the sub process capabilities according to the test procedure
        CapInit();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID555);
        // Step 3: Invoke the interface to operate the file system
        dir = opendir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0);
W
wenjun 已提交
1864
        if (dir == nullptr) {
M
mamingshuai 已提交
1865
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1866
            exitCode = 1;
M
mamingshuai 已提交
1867 1868
        } else {
            closedir(dir);
W
wenjun 已提交
1869
        }
M
mamingshuai 已提交
1870
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1871 1872 1873
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1874
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1875
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1876 1877
        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;
W
wenjun 已提交
1878 1879 1880
    }
}

M
mamingshuai 已提交
1881
/*
W
wenjun 已提交
1882
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0720
M
mamingshuai 已提交
1883 1884
 * @tc.name       : DAC control mechanism-File system dev-System call opendir-UID0-GID0-Capability
                    CAPDACOVERRIDE-Permission 143
W
wenjun 已提交
1885 1886
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1887
HWTEST_F(DacTestSuite, DACTest0720, Function | MediumTest | Level2)
W
wenjun 已提交
1888 1889 1890
{
    int ret;
    DIR* dir = nullptr;
M
mamingshuai 已提交
1891
    // Preset action: Create a directory
W
wenjun 已提交
1892
    CreateDevDir();
M
mamingshuai 已提交
1893 1894 1895 1896
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0 "/" DACDIR0_DACDIR0, CHMOD143);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 143";
    // Preset action: Fork a sub process
W
wenjun 已提交
1897 1898 1899 1900 1901
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1902 1903 1904 1905 1906 1907
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID0, GID0);
        // Step 3: Invoke the interface to operate the file system
        dir = opendir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0);
W
wenjun 已提交
1908
        if (dir == nullptr) {
M
mamingshuai 已提交
1909
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1910
            exitCode = 1;
M
mamingshuai 已提交
1911 1912
        } else {
            closedir(dir);
W
wenjun 已提交
1913
        }
M
mamingshuai 已提交
1914
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1915 1916 1917
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1918
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1919
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1920 1921
        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;
W
wenjun 已提交
1922 1923 1924
    }
}

M
mamingshuai 已提交
1925
/*
W
wenjun 已提交
1926
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0730
M
mamingshuai 已提交
1927 1928
 * @tc.name       : DAC control mechanism-File system dev-System call opendir-UID1-GID1-Capability
                    CAPDACOVERRIDE-Permission 521
W
wenjun 已提交
1929 1930
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1931
HWTEST_F(DacTestSuite, DACTest0730, Function | MediumTest | Level2)
W
wenjun 已提交
1932 1933 1934
{
    int ret;
    DIR* dir = nullptr;
M
mamingshuai 已提交
1935
    // Preset action: Create a directory
W
wenjun 已提交
1936
    CreateDevDir();
M
mamingshuai 已提交
1937 1938 1939 1940
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0 "/" DACDIR0_DACDIR0, CHMOD521);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 521";
    // Preset action: Fork a sub process
W
wenjun 已提交
1941 1942 1943 1944 1945
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1946 1947 1948 1949 1950 1951
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
        // Step 3: Invoke the interface to operate the file system
        dir = opendir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0);
W
wenjun 已提交
1952
        if (dir == nullptr) {
M
mamingshuai 已提交
1953
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
1954
            exitCode = 1;
M
mamingshuai 已提交
1955 1956
        } else {
            closedir(dir);
W
wenjun 已提交
1957
        }
M
mamingshuai 已提交
1958
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
1959 1960 1961
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
1962
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
1963
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
1964 1965
        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;
W
wenjun 已提交
1966 1967 1968
    }
}

M
mamingshuai 已提交
1969
/*
W
wenjun 已提交
1970
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0740
M
mamingshuai 已提交
1971 1972
 * @tc.name       : DAC control mechanism-File system dev-System call opendir-UID1-GID1-Groups contain-Capability
                    CAPDACOVERRIDE-Permission 016
W
wenjun 已提交
1973 1974
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
1975
HWTEST_F(DacTestSuite, DACTest0740, Function | MediumTest | Level2)
W
wenjun 已提交
1976 1977 1978 1979
{
    int ret;
    DIR* dir = nullptr;
    gid_t list[SINGLESIZE] = {0};
M
mamingshuai 已提交
1980
    // Preset action: Create a directory
W
wenjun 已提交
1981
    CreateDevDir();
M
mamingshuai 已提交
1982 1983 1984 1985
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0 "/" DACDIR0_DACDIR0, CHMOD016);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 016";
    // Preset action: Fork a sub process
W
wenjun 已提交
1986 1987 1988 1989 1990
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
1991 1992 1993 1994
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
W
wenjun 已提交
1995
        setgroups(SINGLESIZE, list);
M
mamingshuai 已提交
1996 1997
        // Step 3: Invoke the interface to operate the file system
        dir = opendir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0);
W
wenjun 已提交
1998
        if (dir == nullptr) {
M
mamingshuai 已提交
1999
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
2000
            exitCode = 1;
M
mamingshuai 已提交
2001 2002
        } else {
            closedir(dir);
W
wenjun 已提交
2003
        }
M
mamingshuai 已提交
2004
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
2005 2006 2007
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
2008
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
2009
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
2010 2011
        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;
W
wenjun 已提交
2012 2013 2014
    }
}

M
mamingshuai 已提交
2015
/*
W
wenjun 已提交
2016
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0750
M
mamingshuai 已提交
2017 2018
 * @tc.name       : DAC control mechanism-File system dev-System call opendir-UID1-GID1-Groups contain-Capability
                    CAPDACOVERRIDE-Permission 407
W
wenjun 已提交
2019 2020
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
2021
HWTEST_F(DacTestSuite, DACTest0750, Function | MediumTest | Level2)
W
wenjun 已提交
2022 2023 2024 2025
{
    int ret;
    DIR* dir = nullptr;
    gid_t list[SINGLESIZE] = {0};
M
mamingshuai 已提交
2026
    // Preset action: Create a directory
W
wenjun 已提交
2027
    CreateDevDir();
M
mamingshuai 已提交
2028 2029 2030 2031
    // Preset action: Change the file permission according to the test procedure
    ret = chmod("/dev/" DACDIR0 "/" DACDIR0_DACDIR0, CHMOD407);
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to chmod 407";
    // Preset action: Fork a sub process
W
wenjun 已提交
2032 2033 2034 2035 2036
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
2037 2038 2039 2040
        // Step 1: Change the sub process capabilities according to the test procedure
        DropCAPDACREADSEARCH();
        // Step 2: Change the sub process uid, gid and groups according to the test procedure
        SetUidGid(UID555, GID555);
W
wenjun 已提交
2041
        setgroups(SINGLESIZE, list);
M
mamingshuai 已提交
2042 2043
        // Step 3: Invoke the interface to operate the file system
        dir = opendir("/dev/" DACDIR0 "/" DACDIR0_DACDIR0);
W
wenjun 已提交
2044
        if (dir == nullptr) {
M
mamingshuai 已提交
2045
            LOG("ErrInfo: VFS error with DAC or Capability");
W
wenjun 已提交
2046
            exitCode = 1;
M
mamingshuai 已提交
2047 2048
        } else {
            closedir(dir);
W
wenjun 已提交
2049
        }
M
mamingshuai 已提交
2050
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
2051 2052 2053
        exit(exitCode);
    } else {
        int status = 0;
M
mamingshuai 已提交
2054
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
2055
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
2056 2057
        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;
W
wenjun 已提交
2058
    }
M
mamingshuai 已提交
2059 2060
}
#endif