ActsDacPreTest.cpp 41.0 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
 * 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"
M
mamingshuai 已提交
16
#include <cstddef>
W
wenjun 已提交
17 18 19 20 21
#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
static int TestSetUid()
{
M
mamingshuai 已提交
31
    // Test the 'setuid' interface
X
xialiyun 已提交
32 33 34 35
    int ret = 0;
    uid_t ruid = 0;
    uid_t euid = 0;
    uid_t suid = 0;
M
mamingshuai 已提交
36 37
    // Step 1: Verify that UID is set to 0
    ret = setuid(UID0);
W
wenjun 已提交
38
    if (ret != 0) {
M
mamingshuai 已提交
39 40 41
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set uid=0, now process uid=%d", getuid());
        return FALSE;
W
wenjun 已提交
42
    }
M
mamingshuai 已提交
43 44 45 46 47 48
    // To test the function of invoking the setuid interface to set the UID=0
    getresuid(&ruid, &euid, &suid);
    EXPECT_EQ(ruid, UID0) << "Failed to set ruid=0: ruid=" << ruid;
    EXPECT_EQ(euid, UID0) << "Failed to set euid=0: euid=" << euid;
    EXPECT_EQ(suid, UID0) << "Failed to set suid=0: suid=" << suid;
    // Step 2: Verify that UID is set to 1000
W
wenjun 已提交
49 50
    ret = setuid(UID1000);
    if (ret != 0) {
M
mamingshuai 已提交
51 52 53
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set uid=1000, now process uid=%d", getuid());
        return FALSE;
W
wenjun 已提交
54
    }
M
mamingshuai 已提交
55 56 57 58 59 60
    // To test the function of invoking the setuid interface to set the UID=1000
    getresuid(&ruid, &euid, &suid);
    EXPECT_EQ(ruid, UID1000) << "Failed to set ruid=1000: ruid=" << ruid;
    EXPECT_EQ(euid, UID1000) << "Failed to set euid=1000: euid=" << euid;
    EXPECT_EQ(suid, UID1000) << "Failed to set suid=1000: suid=" << suid;
    // Step 3: Verify that UID is set to 10000
W
wenjun 已提交
61 62
    ret = setuid(UID10000);
    if (ret != 0) {
M
mamingshuai 已提交
63 64 65
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set uid=10000, now process uid=%d", getuid());
        return FALSE;
W
wenjun 已提交
66
    }
M
mamingshuai 已提交
67 68 69 70 71 72
    // To test the function of invoking the setuid interface to set the UID=10000
    getresuid(&ruid, &euid, &suid);
    EXPECT_EQ(ruid, UID10000) << "Failed to set ruid=10000: ruid=" << ruid;
    EXPECT_EQ(euid, UID10000) << "Failed to set euid=10000: euid=" << euid;
    EXPECT_EQ(suid, UID10000) << "Failed to set suid=10000: suid=" << suid;
    // Step 4: Verify that UID is set to 2147483647
W
wenjun 已提交
73 74
    ret = setuid(MAX_INT);
    if (ret != 0) {
M
mamingshuai 已提交
75 76 77
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set uid=2147483647, now process uid=%d", getuid());
        return FALSE;
W
wenjun 已提交
78
    }
M
mamingshuai 已提交
79 80 81 82 83 84
    // To test the function of invoking the setuid interface to set the UID=2147483647
    getresuid(&ruid, &euid, &suid);
    EXPECT_EQ(ruid, MAX_INT) << "Failed to set ruid=2147483647: ruid=" << ruid;
    EXPECT_EQ(euid, MAX_INT) << "Failed to set euid=2147483647: euid=" << euid;
    EXPECT_EQ(suid, MAX_INT) << "Failed to set suid=2147483647: suid=" << suid;
    return 0;
W
wenjun 已提交
85 86
}

M
mamingshuai 已提交
87
static int TestSetREUid()
W
wenjun 已提交
88
{
M
mamingshuai 已提交
89
    // Test the 'setreuid' interface
X
xialiyun 已提交
90 91 92 93
    int ret = 0;
    uid_t ruid = 0;
    uid_t euid = 0;
    uid_t suid = 0;
M
mamingshuai 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106
    // Step 1: Verify that UID is set to 0
    ret = setreuid(UID0, UID0);
    if (ret != 0) {
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set uid=0, now process uid=%d", getuid());
        return FALSE;
    }
    // To test the function of invoking the setuid interface to set the UID=0
    getresuid(&ruid, &euid, &suid);
    EXPECT_EQ(ruid, UID0) << "Failed to set ruid=0: ruid=" << ruid;
    EXPECT_EQ(euid, UID0) << "Failed to set euid=0: euid=" << euid;
    EXPECT_EQ(suid, UID0) << "Failed to set suid=0: suid=" << suid;
    // Step 2: Verify that UID is set to 1000
W
wenjun 已提交
107
    ret = setreuid(UID1000, UID1000);
M
mamingshuai 已提交
108 109 110 111 112 113 114 115 116 117 118
    if (ret != 0) {
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set uid=1000, now process uid=%d", getuid());
        return FALSE;
    }
    // To test the function of invoking the setuid interface to set the UID=1000
    getresuid(&ruid, &euid, &suid);
    EXPECT_EQ(ruid, UID1000) << "Failed to set ruid=1000: ruid=" << ruid;
    EXPECT_EQ(euid, UID1000) << "Failed to set euid=1000: euid=" << euid;
    EXPECT_EQ(suid, UID1000) << "Failed to set suid=1000: suid=" << suid;
    // Step 3: Verify that UID is set to 10000
W
wenjun 已提交
119
    ret = setreuid(UID10000, UID10000);
M
mamingshuai 已提交
120 121 122 123 124 125 126 127 128 129 130
    if (ret != 0) {
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set uid=10000, now process uid=%d", getuid());
        return FALSE;
    }
    // To test the function of invoking the setuid interface to set the UID=10000
    getresuid(&ruid, &euid, &suid);
    EXPECT_EQ(ruid, UID10000) << "Failed to set ruid=10000: ruid=" << ruid;
    EXPECT_EQ(euid, UID10000) << "Failed to set euid=10000: euid=" << euid;
    EXPECT_EQ(suid, UID10000) << "Failed to set suid=10000: suid=" << suid;
    // Step 4: Verify that UID is set to 2147483647
W
wenjun 已提交
131
    ret = setreuid(MAX_INT, MAX_INT);
M
mamingshuai 已提交
132 133 134 135 136 137 138 139 140 141 142
    if (ret != 0) {
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set uid=2147483647, now process uid=%d", getuid());
        return FALSE;
    }
    // To test the function of invoking the setuid interface to set the UID=2147483647
    getresuid(&ruid, &euid, &suid);
    EXPECT_EQ(ruid, MAX_INT) << "Failed to set ruid=2147483647: ruid=" << ruid;
    EXPECT_EQ(euid, MAX_INT) << "Failed to set euid=2147483647: euid=" << euid;
    EXPECT_EQ(suid, MAX_INT) << "Failed to set suid=2147483647: suid=" << suid;
    return 0;
W
wenjun 已提交
143 144
}

M
mamingshuai 已提交
145
static int TestSetRESUid()
W
wenjun 已提交
146
{
M
mamingshuai 已提交
147
    // Test the 'setresuid' interface
X
xialiyun 已提交
148 149 150 151
    int ret = 0;
    uid_t ruid = 0;
    uid_t euid = 0;
    uid_t suid = 0;
M
mamingshuai 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164
    // Step 1: Verify that UID is set to 0
    ret = setresuid(UID0, UID0, UID0);
    if (ret != 0) {
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set uid=0, now process uid=%d", getuid());
        return FALSE;
    }
    // To test the function of invoking the setuid interface to set the UID=0
    getresuid(&ruid, &euid, &suid);
    EXPECT_EQ(ruid, UID0) << "Failed to set ruid=0: ruid=" << ruid;
    EXPECT_EQ(euid, UID0) << "Failed to set euid=0: euid=" << euid;
    EXPECT_EQ(suid, UID0) << "Failed to set suid=0: suid=" << suid;
    // Step 2: Verify that UID is set to 1000
W
wenjun 已提交
165
    ret = setresuid(UID1000, UID1000, UID1000);
M
mamingshuai 已提交
166 167 168 169 170 171 172 173 174 175 176
    if (ret != 0) {
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set uid=1000, now process uid=%d", getuid());
        return FALSE;
    }
    // To test the function of invoking the setuid interface to set the UID=1000
    getresuid(&ruid, &euid, &suid);
    EXPECT_EQ(ruid, UID1000) << "Failed to set ruid=1000: ruid=" << ruid;
    EXPECT_EQ(euid, UID1000) << "Failed to set euid=1000: euid=" << euid;
    EXPECT_EQ(suid, UID1000) << "Failed to set suid=1000: suid=" << suid;
    // Step 3: Verify that UID is set to 10000
W
wenjun 已提交
177
    ret = setresuid(UID10000, UID10000, UID10000);
M
mamingshuai 已提交
178 179 180 181 182 183 184 185 186 187 188
    if (ret != 0) {
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set uid=10000, now process uid=%d", getuid());
        return FALSE;
    }
    // To test the function of invoking the setuid interface to set the UID=10000
    getresuid(&ruid, &euid, &suid);
    EXPECT_EQ(ruid, UID10000) << "Failed to set ruid=10000: ruid=" << ruid;
    EXPECT_EQ(euid, UID10000) << "Failed to set euid=10000: euid=" << euid;
    EXPECT_EQ(suid, UID10000) << "Failed to set suid=10000: suid=" << suid;
    // Step 4: Verify that UID is set to 2147483647
W
wenjun 已提交
189
    ret = setresuid(MAX_INT, MAX_INT, MAX_INT);
M
mamingshuai 已提交
190 191 192 193 194 195 196 197 198 199 200
    if (ret != 0) {
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set uid=2147483647, now process uid=%d", getuid());
        return FALSE;
    }
    // To test the function of invoking the setuid interface to set the UID=2147483647
    getresuid(&ruid, &euid, &suid);
    EXPECT_EQ(ruid, MAX_INT) << "Failed to set ruid=2147483647: ruid=" << ruid;
    EXPECT_EQ(euid, MAX_INT) << "Failed to set euid=2147483647: euid=" << euid;
    EXPECT_EQ(suid, MAX_INT) << "Failed to set suid=2147483647: suid=" << suid;
    return 0;
W
wenjun 已提交
201 202
}

M
mamingshuai 已提交
203
static int TestSetUidAbnormal()
W
wenjun 已提交
204
{
M
mamingshuai 已提交
205
    // Enter the exception parameter when invoke the 'setuid','setreuid','setresuid' interface
X
xialiyun 已提交
206 207 208 209 210 211 212
    int ret = 0;
    uid_t newruid = 0;
    uid_t neweuid = 0;
    uid_t newsuid = 0;
    uid_t ruid = 0;
    uid_t euid = 0;
    uid_t suid = 0;
M
mamingshuai 已提交
213
    // Obtain the ruid, euid, suid of the current process
W
wenjun 已提交
214
    getresuid(&ruid, &euid, &suid);
M
mamingshuai 已提交
215
    // Step 1: Verify that UID is set to -100 with the 'setuid' interface
W
wenjun 已提交
216
    ret = setuid(ABNORMALINT);
M
mamingshuai 已提交
217 218 219 220 221 222 223 224 225 226 227
    if (ret != FALSE) {
        EXPECT_EQ(ret, FALSE);
        LOG("ErrInfo: Set uid=-100, now process uid=%d", getuid());
        return FALSE;
    }
    // To test the function of invoking the 'setuid' interface to set the UID=-100
    getresuid(&newruid, &neweuid, &newsuid);
    EXPECT_EQ(newruid, ruid) << "The value of ruid changes after an invalid parameter is entered: ruid=" << ruid;
    EXPECT_EQ(neweuid, euid) << "The value of euid changes after an invalid parameter is entered: euid=" << euid;
    EXPECT_EQ(newsuid, suid) << "The value of suid changes after an invalid parameter is entered: suid=" << suid;
    // Step 2: Verify that UID is set to -100 with the 'setreuid' interface
W
wenjun 已提交
228
    ret = setreuid(ABNORMALINT, ABNORMALINT);
M
mamingshuai 已提交
229 230 231 232 233 234 235 236 237 238 239
    if (ret != FALSE) {
        EXPECT_EQ(ret, FALSE);
        LOG("ErrInfo: Set uid=-100, now process uid=%d", getuid());
        return FALSE;
    }
    // To test the function of invoking the 'setuid' interface to set the UID=-100
    getresuid(&newruid, &neweuid, &newsuid);
    EXPECT_EQ(newruid, ruid) << "The value of ruid changes after an invalid parameter is entered: ruid=" << ruid;
    EXPECT_EQ(neweuid, euid) << "The value of euid changes after an invalid parameter is entered: euid=" << euid;
    EXPECT_EQ(newsuid, suid) << "The value of suid changes after an invalid parameter is entered: suid=" << suid;
    // Step 3: Verify that UID is set to -100 with the 'setreuid' interface
W
wenjun 已提交
240
    ret = setresuid(ABNORMALINT, ABNORMALINT, ABNORMALINT);
M
mamingshuai 已提交
241 242 243 244 245 246 247 248 249 250 251
    if (ret != FALSE) {
        EXPECT_EQ(ret, FALSE);
        LOG("ErrInfo: Set uid=-100, now process uid=%d", getuid());
        return FALSE;
    }
    // To test the function of invoking the 'setuid' interface to set the UID=-100
    getresuid(&newruid, &neweuid, &newsuid);
    EXPECT_EQ(newruid, ruid) << "The value of ruid changes after an invalid parameter is entered: ruid=" << ruid;
    EXPECT_EQ(neweuid, euid) << "The value of euid changes after an invalid parameter is entered: euid=" << euid;
    EXPECT_EQ(newsuid, suid) << "The value of suid changes after an invalid parameter is entered: suid=" << suid;
    return 0;
W
wenjun 已提交
252 253 254 255
}

static int TestSetGid()
{
M
mamingshuai 已提交
256
    // Test the 'setgid' interface
X
xialiyun 已提交
257 258 259 260
    int ret = 0;
    gid_t rgid = 0;
    gid_t egid = 0;
    gid_t sgid = 0;
M
mamingshuai 已提交
261 262
    // Step 1: Verify that GID is set to 0
    ret = setgid(GID0);
W
wenjun 已提交
263
    if (ret != 0) {
M
mamingshuai 已提交
264 265 266
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set gid=0, now process gid=%d", getgid());
        return FALSE;
W
wenjun 已提交
267
    }
M
mamingshuai 已提交
268 269 270 271 272 273
    // To test the function of invoking the setgid interface to set the GID=0
    getresgid(&rgid, &egid, &sgid);
    EXPECT_EQ(rgid, GID0) << "Failed to set rgid=0: rgid=" << rgid;
    EXPECT_EQ(egid, GID0) << "Failed to set egid=0: egid=" << egid;
    EXPECT_EQ(sgid, GID0) << "Failed to set sgid=0: sgid=" << sgid;
    // Step 2: Verify that GID is set to 1000
W
wenjun 已提交
274 275
    ret = setgid(GID1000);
    if (ret != 0) {
M
mamingshuai 已提交
276 277 278
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set gid=1000, now process gid=%d", getgid());
        return FALSE;
W
wenjun 已提交
279
    }
M
mamingshuai 已提交
280 281 282 283 284 285
    // To test the function of invoking the setgid interface to set the GID=1000
    getresgid(&rgid, &egid, &sgid);
    EXPECT_EQ(rgid, GID1000) << "Failed to set rgid=1000: rgid=" << rgid;
    EXPECT_EQ(egid, GID1000) << "Failed to set egid=1000: egid=" << egid;
    EXPECT_EQ(sgid, GID1000) << "Failed to set sgid=1000: sgid=" << sgid;
    // Step 3: Verify that GID is set to 10000
W
wenjun 已提交
286 287
    ret = setgid(GID10000);
    if (ret != 0) {
M
mamingshuai 已提交
288 289 290
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set gid=10000, now process gid=%d", getgid());
        return FALSE;
W
wenjun 已提交
291
    }
M
mamingshuai 已提交
292 293 294 295 296 297
    // To test the function of invoking the setgid interface to set the GID=10000
    getresgid(&rgid, &egid, &sgid);
    EXPECT_EQ(rgid, GID10000) << "Failed to set rgid=10000: rgid=" << rgid;
    EXPECT_EQ(egid, GID10000) << "Failed to set egid=10000: egid=" << egid;
    EXPECT_EQ(sgid, GID10000) << "Failed to set sgid=10000: sgid=" << sgid;
    // Step 4: Verify that GID is set to 2147483647
W
wenjun 已提交
298 299
    ret = setgid(MAX_INT);
    if (ret != 0) {
M
mamingshuai 已提交
300 301 302
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set gid=2147483647, now process gid=%d", getgid());
        return FALSE;
W
wenjun 已提交
303
    }
M
mamingshuai 已提交
304 305 306 307 308 309
    // To test the function of invoking the setgid interface to set the GID=2147483647
    getresgid(&rgid, &egid, &sgid);
    EXPECT_EQ(rgid, MAX_INT) << "Failed to set rgid=2147483647: rgid=" << rgid;
    EXPECT_EQ(egid, MAX_INT) << "Failed to set egid=2147483647: egid=" << egid;
    EXPECT_EQ(sgid, MAX_INT) << "Failed to set sgid=2147483647: sgid=" << sgid;
    return 0;
W
wenjun 已提交
310 311
}

M
mamingshuai 已提交
312
static int TestSetREGid()
W
wenjun 已提交
313
{
M
mamingshuai 已提交
314
    // Test the 'setregid' interface
X
xialiyun 已提交
315 316 317 318
    int ret = 0;
    gid_t rgid = 0;
    gid_t egid = 0;
    gid_t sgid = 0;
M
mamingshuai 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331
    // Step 1: Verify that GID is set to 0
    ret = setregid(GID0, GID0);
    if (ret != 0) {
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set gid=0, now process gid=%d", getgid());
        return FALSE;
    }
    // To test the function of invoking the setgid interface to set the GID=0
    getresgid(&rgid, &egid, &sgid);
    EXPECT_EQ(rgid, GID0) << "Failed to set rgid=0: rgid=" << rgid;
    EXPECT_EQ(egid, GID0) << "Failed to set egid=0: egid=" << egid;
    EXPECT_EQ(sgid, GID0) << "Failed to set sgid=0: sgid=" << sgid;
    // Step 2: Verify that GID is set to 1000
W
wenjun 已提交
332
    ret = setregid(GID1000, GID1000);
M
mamingshuai 已提交
333 334 335 336 337 338 339 340 341 342 343
    if (ret != 0) {
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set gid=1000, now process gid=%d", getgid());
        return FALSE;
    }
    // To test the function of invoking the setgid interface to set the GID=1000
    getresgid(&rgid, &egid, &sgid);
    EXPECT_EQ(rgid, GID1000) << "Failed to set rgid=1000: rgid=" << rgid;
    EXPECT_EQ(egid, GID1000) << "Failed to set egid=1000: egid=" << egid;
    EXPECT_EQ(sgid, GID1000) << "Failed to set sgid=1000: sgid=" << sgid;
    // Step 3: Verify that GID is set to 10000
W
wenjun 已提交
344
    ret = setregid(GID10000, GID10000);
M
mamingshuai 已提交
345 346 347 348 349 350 351 352 353 354 355
    if (ret != 0) {
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set gid=10000, now process gid=%d", getgid());
        return FALSE;
    }
    // To test the function of invoking the setgid interface to set the GID=10000
    getresgid(&rgid, &egid, &sgid);
    EXPECT_EQ(rgid, GID10000) << "Failed to set rgid=10000: rgid=" << rgid;
    EXPECT_EQ(egid, GID10000) << "Failed to set egid=10000: egid=" << egid;
    EXPECT_EQ(sgid, GID10000) << "Failed to set sgid=10000: sgid=" << sgid;
    // Step 4: Verify that GID is set to 2147483647
W
wenjun 已提交
356
    ret = setregid(MAX_INT, MAX_INT);
M
mamingshuai 已提交
357 358 359 360 361 362 363 364 365 366 367
    if (ret != 0) {
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set gid=2147483647, now process gid=%d", getgid());
        return FALSE;
    }
    // To test the function of invoking the setgid interface to set the GID=2147483647
    getresgid(&rgid, &egid, &sgid);
    EXPECT_EQ(rgid, MAX_INT) << "Failed to set rgid=2147483647: rgid=" << rgid;
    EXPECT_EQ(egid, MAX_INT) << "Failed to set egid=2147483647: egid=" << egid;
    EXPECT_EQ(sgid, MAX_INT) << "Failed to set sgid=2147483647: sgid=" << sgid;
    return 0;
W
wenjun 已提交
368 369
}

M
mamingshuai 已提交
370
static int TestSetRESGid()
W
wenjun 已提交
371
{
M
mamingshuai 已提交
372
    // Test the 'setresgid' interface
X
xialiyun 已提交
373 374 375 376
    int ret = 0;
    gid_t rgid = 0;
    gid_t egid = 0;
    gid_t sgid = 0;
M
mamingshuai 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389
    // Step 1: Verify that GID is set to 0
    ret = setresgid(GID0, GID0, GID0);
    if (ret != 0) {
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set gid=0, now process gid=%d", getgid());
        return FALSE;
    }
    // To test the function of invoking the setgid interface to set the GID=0
    getresgid(&rgid, &egid, &sgid);
    EXPECT_EQ(rgid, GID0) << "Failed to set rgid=0: rgid=" << rgid;
    EXPECT_EQ(egid, GID0) << "Failed to set egid=0: egid=" << egid;
    EXPECT_EQ(sgid, GID0) << "Failed to set sgid=0: sgid=" << sgid;
    // Step 2: Verify that GID is set to 1000
W
wenjun 已提交
390
    ret = setresgid(GID1000, GID1000, GID1000);
M
mamingshuai 已提交
391 392 393 394 395 396 397 398 399 400 401
    if (ret != 0) {
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set gid=1000, now process gid=%d", getgid());
        return FALSE;
    }
    // To test the function of invoking the setgid interface to set the GID=1000
    getresgid(&rgid, &egid, &sgid);
    EXPECT_EQ(rgid, GID1000) << "Failed to set rgid=1000: rgid=" << rgid;
    EXPECT_EQ(egid, GID1000) << "Failed to set egid=1000: egid=" << egid;
    EXPECT_EQ(sgid, GID1000) << "Failed to set sgid=1000: sgid=" << sgid;
    // Step 3: Verify that GID is set to 10000
W
wenjun 已提交
402
    ret = setresgid(GID10000, GID10000, GID10000);
M
mamingshuai 已提交
403 404 405 406 407 408 409 410 411 412 413
    if (ret != 0) {
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set gid=10000, now process gid=%d", getgid());
        return FALSE;
    }
    // To test the function of invoking the setgid interface to set the GID=10000
    getresgid(&rgid, &egid, &sgid);
    EXPECT_EQ(rgid, GID10000) << "Failed to set rgid=10000: rgid=" << rgid;
    EXPECT_EQ(egid, GID10000) << "Failed to set egid=10000: egid=" << egid;
    EXPECT_EQ(sgid, GID10000) << "Failed to set sgid=10000: sgid=" << sgid;
    // Step 4: Verify that GID is set to 2147483647
W
wenjun 已提交
414
    ret = setresgid(MAX_INT, MAX_INT, MAX_INT);
M
mamingshuai 已提交
415 416 417 418 419 420 421 422 423 424 425
    if (ret != 0) {
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set gid=2147483647, now process gid=%d", getgid());
        return FALSE;
    }
    // To test the function of invoking the setgid interface to set the GID=2147483647
    getresgid(&rgid, &egid, &sgid);
    EXPECT_EQ(rgid, MAX_INT) << "Failed to set rgid=2147483647: rgid=" << rgid;
    EXPECT_EQ(egid, MAX_INT) << "Failed to set egid=2147483647: egid=" << egid;
    EXPECT_EQ(sgid, MAX_INT) << "Failed to set sgid=2147483647: sgid=" << sgid;
    return 0;
W
wenjun 已提交
426 427
}

M
mamingshuai 已提交
428
static int TestSetGidAbnormal()
W
wenjun 已提交
429
{
M
mamingshuai 已提交
430
    // Enter the exception parameter when invoke the 'setgid','setregid','setresgid' interface
X
xialiyun 已提交
431 432 433 434 435 436 437
    int ret = 0;
    gid_t newrgid = 0;
    gid_t newegid = 0;
    gid_t newsgid = 0;
    gid_t rgid = 0;
    gid_t egid = 0;
    gid_t sgid = 0;
M
mamingshuai 已提交
438
    // Obtain the rgid, egid, sgid of the current process
W
wenjun 已提交
439
    getresgid(&rgid, &egid, &sgid);
M
mamingshuai 已提交
440
    // Step 1: Verify that GID is set to -100 with the 'setgid' interface
W
wenjun 已提交
441
    ret = setgid(ABNORMALINT);
M
mamingshuai 已提交
442 443 444 445 446 447 448 449 450 451 452
    if (ret != FALSE) {
        EXPECT_EQ(ret, FALSE);
        LOG("ErrInfo: Set gid=-100, now process gid=%d", getgid());
        return FALSE;
    }
    // To test the function of invoking the 'setgid' interface to set the GID=-100
    getresgid(&newrgid, &newegid, &newsgid);
    EXPECT_EQ(newrgid, rgid) << "The value of rgid changes after an invalid parameter is entered: rgid=" << rgid;
    EXPECT_EQ(newegid, egid) << "The value of egid changes after an invalid parameter is entered: egid=" << egid;
    EXPECT_EQ(newsgid, sgid) << "The value of sgid changes after an invalid parameter is entered: sgid=" << sgid;
    // Step 2: Verify that GID is set to -100 with the 'setregid' interface
W
wenjun 已提交
453
    ret = setregid(ABNORMALINT, ABNORMALINT);
M
mamingshuai 已提交
454 455 456 457 458 459 460 461 462 463 464
    if (ret != FALSE) {
        EXPECT_EQ(ret, FALSE);
        LOG("ErrInfo: Set gid=-100, now process gid=%d", getgid());
        return FALSE;
    }
    // To test the function of invoking the 'setgid' interface to set the GID=-100
    getresgid(&newrgid, &newegid, &newsgid);
    EXPECT_EQ(newrgid, rgid) << "The value of rgid changes after an invalid parameter is entered: rgid=" << rgid;
    EXPECT_EQ(newegid, egid) << "The value of egid changes after an invalid parameter is entered: egid=" << egid;
    EXPECT_EQ(newsgid, sgid) << "The value of sgid changes after an invalid parameter is entered: sgid=" << sgid;
    // Step 3: Verify that GID is set to -100 with the 'setregid' interface
W
wenjun 已提交
465
    ret = setresgid(ABNORMALINT, ABNORMALINT, ABNORMALINT);
M
mamingshuai 已提交
466 467 468 469 470 471 472 473 474 475 476
    if (ret != FALSE) {
        EXPECT_EQ(ret, FALSE);
        LOG("ErrInfo: Set gid=-100, now process gid=%d", getgid());
        return FALSE;
    }
    // To test the function of invoking the 'setgid' interface to set the GID=-100
    getresgid(&newrgid, &newegid, &newsgid);
    EXPECT_EQ(newrgid, rgid) << "The value of rgid changes after an invalid parameter is entered: rgid=" << rgid;
    EXPECT_EQ(newegid, egid) << "The value of egid changes after an invalid parameter is entered: egid=" << egid;
    EXPECT_EQ(newsgid, sgid) << "The value of sgid changes after an invalid parameter is entered: sgid=" << sgid;
    return 0;
W
wenjun 已提交
477 478 479 480
}

static int TestSetGroups()
{
M
mamingshuai 已提交
481
    // Test the 'setgroups' interface
W
wenjun 已提交
482 483 484 485 486 487 488 489
    int ret;
    gid_t list[SIZE255];
    for (size_t num = 0; num < SIZE253; num++) {
        list[num] = num;
    }
    list[SIZE254] = MAX_INT;
    ret = setgroups(SIZE255, list);
    if (ret != 0) {
M
mamingshuai 已提交
490 491 492
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set groups");
        return FALSE;
W
wenjun 已提交
493
    }
M
mamingshuai 已提交
494
    return 0;
W
wenjun 已提交
495 496 497 498
}

static void TsetFork(uid_t uid, gid_t gid, size_t size, const gid_t list[])
{
M
mamingshuai 已提交
499
    // The sub process inherits the UID, GID, and groups of the parent process
W
wenjun 已提交
500 501
    int ret;
    int status = 0;
M
mamingshuai 已提交
502 503
    gid_t reallist[SIZE255];
    // Preset action: Adjust the UID, GID, and groups of the parent process
W
wenjun 已提交
504
    ret = setuid(uid);
M
mamingshuai 已提交
505
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set uid, now process uid=" << getuid();
W
wenjun 已提交
506
    ret = setgid(gid);
M
mamingshuai 已提交
507
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set gid, now process gid=" << getgid();
W
wenjun 已提交
508 509
    setgroups(0, nullptr);
    ret = setgroups(size, list);
M
mamingshuai 已提交
510 511
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set groups";
    // Preset action: Fork a sub process
W
wenjun 已提交
512 513 514 515 516
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
517
        // Step 1: Inheritance test of sub process UID
W
wenjun 已提交
518 519
        uid_t retuid = getuid();
        if (retuid != uid) {
M
mamingshuai 已提交
520
            LOG("ErrInfo: The sub process UID changes when fork process, now process uid=%d", getuid());
W
wenjun 已提交
521 522
            exitCode = 1;
        }
M
mamingshuai 已提交
523
        // Step 2: Inheritance test of sub process GID
W
wenjun 已提交
524 525
        gid_t retgid = getgid();
        if (retgid != gid) {
M
mamingshuai 已提交
526
            LOG("ErrInfo: The sub process GID changes when fork process, now process gid=%d", getgid());
W
wenjun 已提交
527 528
            exitCode = 1;
        }
M
mamingshuai 已提交
529
        // Step 3: Inheritance test of sub process groups
W
wenjun 已提交
530
        int retgroups = getgroups(0, reallist);
M
mamingshuai 已提交
531 532
        if (retgroups == FALSE) {
            LOG("ErrInfo: Failed to get groups");
W
wenjun 已提交
533 534
            exitCode = 1;
        }
M
mamingshuai 已提交
535
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
536 537
        exit(exitCode);
    } else {
M
mamingshuai 已提交
538
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
539
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
540 541
        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 已提交
542 543 544
    }
}

M
mamingshuai 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
static int ChangeSensitiveInformation()
{
    int fd = 0;
    int ret = 0;
    int exitCode = 0;
    char dac[] = "DACPreTest!\n";
    // Failed to read sensitive information
    fd = open("/proc/process", O_WRONLY);
    if (fd >= 0) {
        ret = write(fd, dac, sizeof(dac));
        if (ret != FALSE) {
            LOG("ErrInfo: Change sensitive information, ret = %d", ret);
            exitCode = 1;
        }
        close(fd);
    }
    return exitCode;
}

/*
W
wenjun 已提交
565
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0010
M
mamingshuai 已提交
566
 * @tc.name       : Invoke the interface to set the process UID
W
wenjun 已提交
567 568
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
569
HWTEST_F(DacTestSuite, DACTest0010, Function | MediumTest | Level1)
W
wenjun 已提交
570
{
X
xialiyun 已提交
571 572 573 574 575 576 577
    int ret = 0;
    uid_t ruid = 0;
    uid_t euid = 0;
    uid_t suid = 0;
    uid_t newruid = 0;
    uid_t neweuid = 0;
    uid_t newsuid = 0;
M
mamingshuai 已提交
578
    // Preset action: Obtain the ruid, euid, suid of the current process
W
wenjun 已提交
579
    getresuid(&ruid, &euid, &suid);
M
mamingshuai 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592
    // Step 1: Test the 'setuid' interface
    ret = TestSetUid();
    EXPECT_EQ(ret, 0) << "ErrInfo: TestSetUid() exit error";
    // Step 2: Test the 'setreuid' interface
    ret = TestSetREUid();
    EXPECT_EQ(ret, 0) << "ErrInfo: TestSetREUid() exit error";
    // Step 3: Test the 'setreuid' interface
    ret = TestSetRESUid();
    EXPECT_EQ(ret, 0) << "ErrInfo: TestSetRESUid() exit error";
    // Step 4: Enter the exception parameter when invoke the 'setuid','setreuid','setresuid' interface
    ret = TestSetUidAbnormal();
    EXPECT_EQ(ret, 0) << "ErrInfo: TestSetUidAbnormal() exit error";
    // Cleanup action: Restore the initial UID of the process
W
wenjun 已提交
593
    ret = setresuid(ruid, euid, suid);
M
mamingshuai 已提交
594 595 596 597 598
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to restore the initial UID of the process";
    getresuid(&newruid, &neweuid, &newsuid);
    EXPECT_EQ(newruid, ruid) << "The value of ruid changes after testcase: ruid=" << ruid;
    EXPECT_EQ(neweuid, euid) << "The value of euid changes after testcase: euid=" << euid;
    EXPECT_EQ(newsuid, suid) << "The value of suid changes after testcase: suid=" << suid;
W
wenjun 已提交
599 600
}

M
mamingshuai 已提交
601
/*
W
wenjun 已提交
602
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0020
M
mamingshuai 已提交
603
 * @tc.name       : Invoke the interface to set the process GID
W
wenjun 已提交
604 605
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
606
HWTEST_F(DacTestSuite, DACTest0020, Function | MediumTest | Level1)
W
wenjun 已提交
607
{
X
xialiyun 已提交
608 609 610 611 612 613 614
    int ret = 0;
    gid_t rgid = 0;
    gid_t egid = 0;
    gid_t sgid = 0;
    gid_t newrgid = 0;
    gid_t newegid = 0;
    gid_t newsgid = 0;
M
mamingshuai 已提交
615
    // Preset action: Obtain the rgid, egid, sgid of the current process
W
wenjun 已提交
616
    getresgid(&rgid, &egid, &sgid);
M
mamingshuai 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629
    // Step 1: Test the 'setgid' interface
    ret = TestSetGid();
    EXPECT_EQ(ret, 0) << "ErrInfo: TestSetGid() exit error";
    // Step 2: Test the 'setregid' interface
    ret = TestSetREGid();
    EXPECT_EQ(ret, 0) << "ErrInfo: TestSetREGid() exit error";
    // Step 3: Test the 'setregid' interface
    ret = TestSetRESGid();
    EXPECT_EQ(ret, 0) << "ErrInfo: TestSetRESGid() exit error";
    // Step 4: Enter the exception parameter when invoke the 'setgid','setregid','setresgid' interface
    ret = TestSetGidAbnormal();
    EXPECT_EQ(ret, 0) << "ErrInfo: TestSetGidAbnormal() exit error";
    // Cleanup action: Restore the initial GID of the process
W
wenjun 已提交
630
    ret = setresgid(rgid, egid, sgid);
M
mamingshuai 已提交
631 632 633 634 635
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to restore the initial GID of the process";
    getresgid(&newrgid, &newegid, &newsgid);
    EXPECT_EQ(newrgid, rgid) << "The value of rgid changes after testcase: rgid=" << rgid;
    EXPECT_EQ(newegid, egid) << "The value of egid changes after testcase: egid=" << egid;
    EXPECT_EQ(newsgid, sgid) << "The value of sgid changes after testcase: sgid=" << sgid;
W
wenjun 已提交
636 637
}

M
mamingshuai 已提交
638
/*
W
wenjun 已提交
639
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0030
M
mamingshuai 已提交
640
 * @tc.name       : Invoke the setgroups interface to set the process groups that contain a single GID or an empty value
W
wenjun 已提交
641 642
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
643
HWTEST_F(DacTestSuite, DACTest0030, Function | MediumTest | Level1)
W
wenjun 已提交
644 645 646
{
    int ret;
    gid_t grouplist[SIZE255];
M
mamingshuai 已提交
647
    // Preset action: Obtain the groups of the current process
X
xialiyun 已提交
648
    unsigned int groupsize = getgroups(0, grouplist);
W
wenjun 已提交
649 650
    if (groupsize >= 0) {
        getgroups(groupsize, grouplist);
X
xialiyun 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
        // Preset action: Obtain the group lists required for the testcase
        gid_t list1[SIZE1] = {1};
        gid_t list2[SIZE1] = {MAX_INT};
        gid_t list3[SIZE1] = {};
        // Step 1: Set the group list to {1}
        ret = setgroups(SIZE1, list1);
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set the group list to {1}";
        // Step 2: Set the group list to {2147483647}
        ret = setgroups(SIZE1, list2);
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set the group list to {2147483647}";
        // Step 3: Set the group list to {}
        ret = setgroups(SIZE1, list3);
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set the group list to {}";
        // Cleanup action: Restore the initial groups of the process
        setgroups(0, nullptr);
        ret = setgroups(groupsize, grouplist);
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to restore the initial groups of the process";
W
wenjun 已提交
668
    } else {
M
mamingshuai 已提交
669
        EXPECT_GE(groupsize, 0) << "ErrInfo: Failed to get groups";
W
wenjun 已提交
670 671 672
    }
}

M
mamingshuai 已提交
673
/*
W
wenjun 已提交
674
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0040
M
mamingshuai 已提交
675
 * @tc.name       : Invoke the setgroups interface to set the process groups that contain the same GID
W
wenjun 已提交
676 677
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
678
HWTEST_F(DacTestSuite, DACTest0040, Function | MediumTest | Level2)
W
wenjun 已提交
679 680 681
{
    int ret;
    gid_t grouplist[SIZE255];
M
mamingshuai 已提交
682
    // Preset action: Obtain the groups of the current process
X
xialiyun 已提交
683
    unsigned int groupsize = getgroups(0, grouplist);
W
wenjun 已提交
684 685
    if (groupsize >= 0) {
        getgroups(groupsize, grouplist);
X
xialiyun 已提交
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
        // Preset action: Obtain the group lists required for the testcase
        gid_t list1[SIZE2]={GID0, GID0};
        gid_t list2[SIZE2]={GID1, GID1};
        gid_t list3[SIZE2]={MAX_INT, MAX_INT};
        // Step 1: Set the group list to {0, 0}
        ret = setgroups(SIZE2, list1);
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set the group list to {0, 0}";
        // Step 2: Set the group list to {1, 1}
        ret = setgroups(SIZE2, list2);
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set the group list to {1, 1}";
        // Step 3: Set the group list to {2147483647, 2147483647}
        ret = setgroups(SIZE2, list3);
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set the group list to {2147483647, 2147483647}";
        // Cleanup action: Restore the initial groups of the process
        setgroups(0, nullptr);
        ret = setgroups(groupsize, grouplist);
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to restore the initial groups of the process";
W
wenjun 已提交
703
    } else {
M
mamingshuai 已提交
704
        EXPECT_GE(groupsize, 0) << "ErrInfo: Failed to get groups";
W
wenjun 已提交
705 706 707
    }
}

M
mamingshuai 已提交
708
/*
W
wenjun 已提交
709
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0050
M
mamingshuai 已提交
710
 * @tc.name       : Invoke the setgroups interface to set the process groups that contain the duplicate GIDs
W
wenjun 已提交
711 712
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
713
HWTEST_F(DacTestSuite, DACTest0050, Function | MediumTest | Level3)
W
wenjun 已提交
714 715 716
{
    int ret;
    gid_t grouplist[SIZE255];
M
mamingshuai 已提交
717
    // Preset action: Obtain the groups of the current process
X
xialiyun 已提交
718
    unsigned int groupsize = getgroups(0, grouplist);
W
wenjun 已提交
719 720
    if (groupsize >= 0) {
        getgroups(groupsize, grouplist);
X
xialiyun 已提交
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
        // Preset action: Obtain the group lists required for the testcase
        gid_t list1[SIZE3]={GID0, GID0, MAX_INT};
        gid_t list2[SIZE3]={GID10000, GID10000, MAX_INT};
        gid_t list3[SIZE3]={GID0, MAX_INT, MAX_INT};
        // Step 1: Set the group list to {0, 0, 2147483647}
        ret = setgroups(SIZE3, list1);
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set the group list to {0, 0, 2147483647}";
        // Step 2: Set the group list to {10000, 10000, 2147483647}
        ret = setgroups(SIZE3, list2);
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set the group list to {10000, 10000, 2147483647}";
        // Step 3: Set the group list to {0, 2147483647, 2147483647}
        ret = setgroups(SIZE3, list3);
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set the group list to {0, 2147483647, 2147483647}";
        // Cleanup action: Restore the initial groups of the process
        setgroups(0, nullptr);
        ret = setgroups(groupsize, grouplist);
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to restore the initial groups of the process";
W
wenjun 已提交
738
    } else {
M
mamingshuai 已提交
739
        EXPECT_GE(groupsize, 0) << "ErrInfo: Failed to get groups";
W
wenjun 已提交
740 741 742
    }
}

M
mamingshuai 已提交
743
/*
W
wenjun 已提交
744
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0060
M
mamingshuai 已提交
745
 * @tc.name       : Invoke the setgroups interface to set the process groups that contain all different GIDs
W
wenjun 已提交
746 747
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
748
HWTEST_F(DacTestSuite, DACTest0060, Function | MediumTest | Level3)
W
wenjun 已提交
749 750 751
{
    int ret;
    gid_t grouplist[SIZE255];
M
mamingshuai 已提交
752
    // Preset action: Obtain the groups of the current process
X
xialiyun 已提交
753
    size_t groupsize = getgroups(0, grouplist);
W
wenjun 已提交
754 755
    if (groupsize >= 0) {
        getgroups(groupsize, grouplist);
X
xialiyun 已提交
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
        // Preset action: Obtain the group lists required for the testcase
        gid_t list0[SIZE255];
        for (size_t num0 = 0; num0 < SIZE254; num0++) {
            list0[num0] = num0;
        }
        list0[SIZE254] = MAX_INT;
        gid_t list1[INVAILD_SIZE];
        for (size_t num1 = 0; num1 < MAX_SIZE; num1++) {
            list1[num1] = num1;
        }
        list1[MAX_SIZE] = MAX_INT;
        // Step 1: Set 255 different group lists
        ret = setgroups(SIZE255, list0);
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set the group list to {0, 1, 2, ..., 255}";
        // Step 2: Set the number of groups that exceed the upper limit
        ret = setgroups(INVAILD_SIZE, list1);
        EXPECT_EQ(ret, FALSE) << "ErrInfo: Set groups size over max, size=65537";
        // Cleanup action: Restore the initial groups of the process
        setgroups(0, nullptr);
        ret = setgroups(groupsize, grouplist);
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to restore the initial groups of the process";
W
wenjun 已提交
777
    } else {
M
mamingshuai 已提交
778
        EXPECT_GE(groupsize, 0) << "ErrInfo: Failed to get groups";
W
wenjun 已提交
779 780 781
    }
}

M
mamingshuai 已提交
782
/*
W
wenjun 已提交
783
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0070
M
mamingshuai 已提交
784 785
 * @tc.name       : Invoke the setuid, gid, and groups interfaces to set the uid, gid,
                    and groups of processes concurrently
W
wenjun 已提交
786 787
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
788
HWTEST_F(DacTestSuite, DACTest0070, Security | MediumTest | Level2)
W
wenjun 已提交
789 790 791
{
    int ret;
    int status = 0;
M
mamingshuai 已提交
792
    // Preset action: Fork three sub processes
W
wenjun 已提交
793 794 795 796 797 798 799 800 801
    pid_t pid;
    for (int num = 0; num < NUM3; num++) {
        pid = fork();
        ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
        usleep(SLEEP_NUM);
        if (pid == 0) {
            break;
        }
    }
M
mamingshuai 已提交
802
    // get one parent & three children
W
wenjun 已提交
803 804 805
    if (pid == 0) {
        int exitCode = 0;
        for (int number = 0; number < NUM3000; number++) {
M
mamingshuai 已提交
806 807 808 809 810
            // Preset action: Initialize the subprocess UID, GID and groups
            setuid(0);
            setgid(0);
            setgroups(0, nullptr);
            // Step 1: Test the 'setuid' interface concurrently
W
wenjun 已提交
811 812
            ret = TestSetUid();
            if (ret != 0) {
M
mamingshuai 已提交
813
                LOG("ErrInfo: TestSetUid error during the %d time", number);
W
wenjun 已提交
814 815 816
                exitCode = 1;
                break;
            }
M
mamingshuai 已提交
817
            // Step 2: Test the 'setgid' interface concurrently
W
wenjun 已提交
818 819
            ret = TestSetGid();
            if (ret != 0) {
M
mamingshuai 已提交
820
                LOG("ErrInfo: TestSetGid error during the %d time", number);
W
wenjun 已提交
821 822 823
                exitCode = 1;
                break;
            }
M
mamingshuai 已提交
824
            // Step 2: Test the 'setgroups' interface concurrently
W
wenjun 已提交
825 826
            ret = TestSetGroups();
            if (ret != 0) {
M
mamingshuai 已提交
827
                LOG("ErrInfo: TestSetGroups error during the %d time", number);
W
wenjun 已提交
828 829 830 831
                exitCode = 1;
                break;
            }
        }
M
mamingshuai 已提交
832
        // Step 3: Three sub processes exit with the exitCode
W
wenjun 已提交
833 834
        exit(exitCode);
    } else {
M
mamingshuai 已提交
835 836 837 838 839 840
        // Step 4: The parent process wait for three sub processes to exit and obtain the exitCode
        for (int num2 = 0; num2 < NUM3; num2++) {
            wait(&status);
            EXPECT_NE(WIFEXITED(status), 0) << "ErrInfo: The sub process exit error, child_pid = " << pid;
            EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: Pid = "<< pid
            << ", its exitCode is wrong and test case failed, please query logs";
W
wenjun 已提交
841 842 843 844
        }
    }
}

M
mamingshuai 已提交
845
/*
W
wenjun 已提交
846
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0080
M
mamingshuai 已提交
847
 * @tc.name       : Inheritance of process UID, GID and groups
W
wenjun 已提交
848 849
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
850
HWTEST_F(DacTestSuite, DACTest0080, Function | MediumTest | Level2)
W
wenjun 已提交
851
{
M
mamingshuai 已提交
852
    int ret;
X
xialiyun 已提交
853 854 855 856 857
    uid_t ruid = 0; uid_t euid = 0; uid_t suid = 0;
    uid_t newruid = 0; uid_t neweuid = 0; uid_t newsuid = 0;
    gid_t rgid = 0; gid_t egid = 0; gid_t sgid = 0;
    gid_t newrgid = 0; gid_t newegid = 0; gid_t newsgid = 0;
    gid_t grouplist[SIZE255];
M
mamingshuai 已提交
858 859 860 861 862
    // Preset action: Obtain the ruid, euid, suid of the current process
    getresuid(&ruid, &euid, &suid);
    // Preset action: Obtain the rgid, egid, sgid of the current process
    getresgid(&rgid, &egid, &sgid);
    // Preset action: Obtain the groups of the current process
W
wenjun 已提交
863 864 865
    int groupsize = getgroups(0, grouplist);
    if (groupsize >= 0) {
        getgroups(groupsize, grouplist);
X
xialiyun 已提交
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
        // Preset action: Obtain the group lists required for the testcase
        gid_t list1[SIZE1] = {GID10000};
        gid_t list2[SIZE1] = {};
        gid_t list3[SIZE255];
        for (size_t num = 0; num < SIZE254; num++) {
            list3[num] = num;
        }
        list3[SIZE254] = MAX_INT;
        // Step 1: Factor combination test of UID, GID, and groups
        TsetFork(UID0, GID10000, SIZE1, list1);
        TsetFork(UID10000, GID10000, SIZE1, list1);
        TsetFork(MAX_INT, GID10000, SIZE1, list1);
        TsetFork(UID10000, GID0, SIZE1, list1);
        TsetFork(UID10000, MAX_INT, SIZE1, list1);
        TsetFork(UID10000, GID10000, SIZE1, list2);
        TsetFork(UID10000, GID10000, SIZE255, list3);
        // Cleanup action: Restore the initial UID of the process
        ret = setresuid(ruid, euid, suid);
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to restore the initial UID of the process";
        getresuid(&newruid, &neweuid, &newsuid);
        EXPECT_EQ(newruid, ruid) << "The value of ruid changes after testcase: ruid=" << ruid;
        EXPECT_EQ(neweuid, euid) << "The value of euid changes after testcase: euid=" << euid;
        EXPECT_EQ(newsuid, suid) << "The value of suid changes after testcase: suid=" << suid;
        // Cleanup action: Restore the initial GID of the process
        ret = setresgid(rgid, egid, sgid);
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to restore the initial GID of the process";
        getresgid(&newrgid, &newegid, &newsgid);
        EXPECT_EQ(newrgid, rgid) << "The value of rgid changes after testcase: rgid=" << rgid;
        EXPECT_EQ(newegid, egid) << "The value of egid changes after testcase: egid=" << egid;
        EXPECT_EQ(newsgid, sgid) << "The value of sgid changes after testcase: sgid=" << sgid;
        // Cleanup action: Restore the initial groups of the process
        setgroups(0, nullptr);
        ret = setgroups(groupsize, grouplist);
        EXPECT_EQ(ret, 0) << "ErrInfo: Failed to restore the initial groups of the process";
W
wenjun 已提交
900
    } else {
M
mamingshuai 已提交
901
        EXPECT_GE(groupsize, 0) << "ErrInfo: Failed to get groups";
W
wenjun 已提交
902
    }
M
mamingshuai 已提交
903
}
W
wenjun 已提交
904

M
mamingshuai 已提交
905 906 907 908 909 910 911 912 913 914 915 916
/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0120
 * @tc.name       : Failed to use the third-party app UID to change sensitive information
 * @tc.desc       : [C- SECURITY -0200]
 */
HWTEST_F(DacTestSuite, DACTest0120, Security | MediumTest | Level2)
{
    int ret;
    int status = 0;
    // Preset action: Fork a sub process
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
W
wenjun 已提交
917
    usleep(SLEEP_NUM);
M
mamingshuai 已提交
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
    if (pid == 0) {
        int exitCode = 0;
        // Step 1: Set the process uid and gid to the third-party application uid and gid
        ret = SetUidGid(UID20000, GID20000);
        if (ret != 0) {
            LOG("ErrInfo: Failed to set the process uid and gid");
            exitCode = 1;
        }
        // Step 2: Drop all the sub process capabilities
        ret = DropAllCAP();
        if (ret != 0) {
            LOG("ErrInfo: Failed to drop all the sub process capabilities");
            exitCode = 1;
        }
        // Step 3: Failed to change sensitive information
        ret = ChangeSensitiveInformation();
        if (ret != 0) {
            LOG("ErrInfo: change sensitive information");
            exitCode = 1;
        }
        // Step 4: The sub process exit with the exitCode
        exit(exitCode);
    } else {
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
        waitpid(pid, &status, 0);
        EXPECT_NE(WIFEXITED(status), 0) << "ErrInfo: The sub process exit error, child_pid = " << pid;
        EXPECT_EQ(WEXITSTATUS(status), 0) << "ErrInfo: The exitCode is wrong, please query logs, child_pid = " << pid;
    }
}

/*
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0130
 * @tc.name       : Performance test of the setuid, setgid and setgroups interface
 * @tc.desc       : [C- SECURITY -0200]
 */
HWTEST_F(DacTestSuite, DACTest0130, Performance | MediumTest | Level2)
{
    struct timespec tp = { 0 };
    struct timespec starttime = { 0 };
    struct timespec endtime = { 0 };
    tp.tv_sec = 0;
    tp.tv_nsec = 0;
    // Preset action: Obtain the group lists required for the testcase
    gid_t list[SIZE255];
    for (size_t num = 0; num < SIZE253; num++) {
        list[num] = num;
    }
    // Preset action: Obtains the system time -> starttime
    clock_gettime(CLOCK_REALTIME, &starttime);
    for (int number = 0; number < NUM10000; number++) {
        list[SIZE254] = number;
        // Step 1.1: Setuid for 10000 times
        setuid(number);
        // Step 1.2: Setgid for 10000 times
        setgid(number);
        // Step 1.3: Setgroups for 10000 times
        setgroups(SIZE255, list);
    }
    // Step 2: Obtains the system time again -> endtime
    clock_gettime(CLOCK_REALTIME, &endtime);
    // Step 3: Compare the starttime and the endtime -> tp
    tp = CompareTime(starttime, endtime);
    EXPECT_LE(tp.tv_sec, NUM20) << "ErrInfo: Chown for 10000 times used " << tp.tv_sec << "." << tp.tv_nsec << "s";
    // Cleanup action: Restore the uid, gid and groups of the process to zero
    SetUidGid(UID0, GID0);
    setgroups(0, nullptr);
}
X
xialiyun 已提交
985
#endif