ActsDacPreTest.cpp 39.9 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
W
wenjun 已提交
32
    int ret;
M
mamingshuai 已提交
33 34 35
    uid_t ruid, euid, suid;
    // Step 1: Verify that UID is set to 0
    ret = setuid(UID0);
W
wenjun 已提交
36
    if (ret != 0) {
M
mamingshuai 已提交
37 38 39
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set uid=0, now process uid=%d", getuid());
        return FALSE;
W
wenjun 已提交
40
    }
M
mamingshuai 已提交
41 42 43 44 45 46
    // 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 已提交
47 48
    ret = setuid(UID1000);
    if (ret != 0) {
M
mamingshuai 已提交
49 50 51
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set uid=1000, now process uid=%d", getuid());
        return FALSE;
W
wenjun 已提交
52
    }
M
mamingshuai 已提交
53 54 55 56 57 58
    // 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 已提交
59 60
    ret = setuid(UID10000);
    if (ret != 0) {
M
mamingshuai 已提交
61 62 63
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set uid=10000, now process uid=%d", getuid());
        return FALSE;
W
wenjun 已提交
64
    }
M
mamingshuai 已提交
65 66 67 68 69 70
    // 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 已提交
71 72
    ret = setuid(MAX_INT);
    if (ret != 0) {
M
mamingshuai 已提交
73 74 75
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set uid=2147483647, now process uid=%d", getuid());
        return FALSE;
W
wenjun 已提交
76
    }
M
mamingshuai 已提交
77 78 79 80 81 82
    // 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 已提交
83 84
}

M
mamingshuai 已提交
85
static int TestSetREUid()
W
wenjun 已提交
86
{
M
mamingshuai 已提交
87
    // Test the 'setreuid' interface
W
wenjun 已提交
88
    int ret;
M
mamingshuai 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102
    uid_t ruid, euid, suid;
    // 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 已提交
103
    ret = setreuid(UID1000, UID1000);
M
mamingshuai 已提交
104 105 106 107 108 109 110 111 112 113 114
    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 已提交
115
    ret = setreuid(UID10000, UID10000);
M
mamingshuai 已提交
116 117 118 119 120 121 122 123 124 125 126
    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 已提交
127
    ret = setreuid(MAX_INT, MAX_INT);
M
mamingshuai 已提交
128 129 130 131 132 133 134 135 136 137 138
    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 已提交
139 140
}

M
mamingshuai 已提交
141
static int TestSetRESUid()
W
wenjun 已提交
142
{
M
mamingshuai 已提交
143
    // Test the 'setresuid' interface
W
wenjun 已提交
144
    int ret;
M
mamingshuai 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158
    uid_t ruid, euid, suid;
    // 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 已提交
159
    ret = setresuid(UID1000, UID1000, UID1000);
M
mamingshuai 已提交
160 161 162 163 164 165 166 167 168 169 170
    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 已提交
171
    ret = setresuid(UID10000, UID10000, UID10000);
M
mamingshuai 已提交
172 173 174 175 176 177 178 179 180 181 182
    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 已提交
183
    ret = setresuid(MAX_INT, MAX_INT, MAX_INT);
M
mamingshuai 已提交
184 185 186 187 188 189 190 191 192 193 194
    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 已提交
195 196
}

M
mamingshuai 已提交
197
static int TestSetUidAbnormal()
W
wenjun 已提交
198
{
M
mamingshuai 已提交
199
    // Enter the exception parameter when invoke the 'setuid','setreuid','setresuid' interface
W
wenjun 已提交
200
    int ret;
M
mamingshuai 已提交
201 202
    uid_t newruid, neweuid, newsuid, ruid, euid, suid;
    // Obtain the ruid, euid, suid of the current process
W
wenjun 已提交
203
    getresuid(&ruid, &euid, &suid);
M
mamingshuai 已提交
204
    // Step 1: Verify that UID is set to -100 with the 'setuid' interface
W
wenjun 已提交
205
    ret = setuid(ABNORMALINT);
M
mamingshuai 已提交
206 207 208 209 210 211 212 213 214 215 216
    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 已提交
217
    ret = setreuid(ABNORMALINT, ABNORMALINT);
M
mamingshuai 已提交
218 219 220 221 222 223 224 225 226 227 228
    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 已提交
229
    ret = setresuid(ABNORMALINT, ABNORMALINT, ABNORMALINT);
M
mamingshuai 已提交
230 231 232 233 234 235 236 237 238 239 240
    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 已提交
241 242 243 244
}

static int TestSetGid()
{
M
mamingshuai 已提交
245
    // Test the 'setgid' interface
W
wenjun 已提交
246
    int ret;
M
mamingshuai 已提交
247 248 249
    gid_t rgid, egid, sgid;
    // Step 1: Verify that GID is set to 0
    ret = setgid(GID0);
W
wenjun 已提交
250
    if (ret != 0) {
M
mamingshuai 已提交
251 252 253
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set gid=0, now process gid=%d", getgid());
        return FALSE;
W
wenjun 已提交
254
    }
M
mamingshuai 已提交
255 256 257 258 259 260
    // 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 已提交
261 262
    ret = setgid(GID1000);
    if (ret != 0) {
M
mamingshuai 已提交
263 264 265
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set gid=1000, now process gid=%d", getgid());
        return FALSE;
W
wenjun 已提交
266
    }
M
mamingshuai 已提交
267 268 269 270 271 272
    // 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 已提交
273 274
    ret = setgid(GID10000);
    if (ret != 0) {
M
mamingshuai 已提交
275 276 277
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set gid=10000, now process gid=%d", getgid());
        return FALSE;
W
wenjun 已提交
278
    }
M
mamingshuai 已提交
279 280 281 282 283 284
    // 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 已提交
285 286
    ret = setgid(MAX_INT);
    if (ret != 0) {
M
mamingshuai 已提交
287 288 289
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set gid=2147483647, now process gid=%d", getgid());
        return FALSE;
W
wenjun 已提交
290
    }
M
mamingshuai 已提交
291 292 293 294 295 296
    // 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 已提交
297 298
}

M
mamingshuai 已提交
299
static int TestSetREGid()
W
wenjun 已提交
300
{
M
mamingshuai 已提交
301
    // Test the 'setregid' interface
W
wenjun 已提交
302
    int ret;
M
mamingshuai 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316
    gid_t rgid, egid, sgid;
    // 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 已提交
317
    ret = setregid(GID1000, GID1000);
M
mamingshuai 已提交
318 319 320 321 322 323 324 325 326 327 328
    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 已提交
329
    ret = setregid(GID10000, GID10000);
M
mamingshuai 已提交
330 331 332 333 334 335 336 337 338 339 340
    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 已提交
341
    ret = setregid(MAX_INT, MAX_INT);
M
mamingshuai 已提交
342 343 344 345 346 347 348 349 350 351 352
    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 已提交
353 354
}

M
mamingshuai 已提交
355
static int TestSetRESGid()
W
wenjun 已提交
356
{
M
mamingshuai 已提交
357
    // Test the 'setresgid' interface
W
wenjun 已提交
358
    int ret;
M
mamingshuai 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372
    gid_t rgid, egid, sgid;
    // 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 已提交
373
    ret = setresgid(GID1000, GID1000, GID1000);
M
mamingshuai 已提交
374 375 376 377 378 379 380 381 382 383 384
    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 已提交
385
    ret = setresgid(GID10000, GID10000, GID10000);
M
mamingshuai 已提交
386 387 388 389 390 391 392 393 394 395 396
    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 已提交
397
    ret = setresgid(MAX_INT, MAX_INT, MAX_INT);
M
mamingshuai 已提交
398 399 400 401 402 403 404 405 406 407 408
    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 已提交
409 410
}

M
mamingshuai 已提交
411
static int TestSetGidAbnormal()
W
wenjun 已提交
412
{
M
mamingshuai 已提交
413
    // Enter the exception parameter when invoke the 'setgid','setregid','setresgid' interface
W
wenjun 已提交
414
    int ret;
M
mamingshuai 已提交
415 416
    gid_t newrgid, newegid, newsgid, rgid, egid, sgid;
    // Obtain the rgid, egid, sgid of the current process
W
wenjun 已提交
417
    getresgid(&rgid, &egid, &sgid);
M
mamingshuai 已提交
418
    // Step 1: Verify that GID is set to -100 with the 'setgid' interface
W
wenjun 已提交
419
    ret = setgid(ABNORMALINT);
M
mamingshuai 已提交
420 421 422 423 424 425 426 427 428 429 430
    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 已提交
431
    ret = setregid(ABNORMALINT, ABNORMALINT);
M
mamingshuai 已提交
432 433 434 435 436 437 438 439 440 441 442
    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 已提交
443
    ret = setresgid(ABNORMALINT, ABNORMALINT, ABNORMALINT);
M
mamingshuai 已提交
444 445 446 447 448 449 450 451 452 453 454
    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 已提交
455 456 457 458
}

static int TestSetGroups()
{
M
mamingshuai 已提交
459
    // Test the 'setgroups' interface
W
wenjun 已提交
460 461 462 463 464 465 466 467
    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 已提交
468 469 470
        EXPECT_EQ(ret, 0);
        LOG("ErrInfo: Failed to set groups");
        return FALSE;
W
wenjun 已提交
471
    }
M
mamingshuai 已提交
472
    return 0;
W
wenjun 已提交
473 474 475 476
}

static void TsetFork(uid_t uid, gid_t gid, size_t size, const gid_t list[])
{
M
mamingshuai 已提交
477
    // The sub process inherits the UID, GID, and groups of the parent process
W
wenjun 已提交
478 479
    int ret;
    int status = 0;
M
mamingshuai 已提交
480 481
    gid_t reallist[SIZE255];
    // Preset action: Adjust the UID, GID, and groups of the parent process
W
wenjun 已提交
482
    ret = setuid(uid);
M
mamingshuai 已提交
483
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set uid, now process uid=" << getuid();
W
wenjun 已提交
484
    ret = setgid(gid);
M
mamingshuai 已提交
485
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set gid, now process gid=" << getgid();
W
wenjun 已提交
486 487
    setgroups(0, nullptr);
    ret = setgroups(size, list);
M
mamingshuai 已提交
488 489
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set groups";
    // Preset action: Fork a sub process
W
wenjun 已提交
490 491 492 493 494
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    usleep(SLEEP_NUM);
    if (pid == 0) {
        int exitCode = 0;
M
mamingshuai 已提交
495
        // Step 1: Inheritance test of sub process UID
W
wenjun 已提交
496 497
        uid_t retuid = getuid();
        if (retuid != uid) {
M
mamingshuai 已提交
498
            LOG("ErrInfo: The sub process UID changes when fork process, now process uid=%d", getuid());
W
wenjun 已提交
499 500
            exitCode = 1;
        }
M
mamingshuai 已提交
501
        // Step 2: Inheritance test of sub process GID
W
wenjun 已提交
502 503
        gid_t retgid = getgid();
        if (retgid != gid) {
M
mamingshuai 已提交
504
            LOG("ErrInfo: The sub process GID changes when fork process, now process gid=%d", getgid());
W
wenjun 已提交
505 506
            exitCode = 1;
        }
M
mamingshuai 已提交
507
        // Step 3: Inheritance test of sub process groups
W
wenjun 已提交
508
        int retgroups = getgroups(0, reallist);
M
mamingshuai 已提交
509 510
        if (retgroups == FALSE) {
            LOG("ErrInfo: Failed to get groups");
W
wenjun 已提交
511 512
            exitCode = 1;
        }
M
mamingshuai 已提交
513
        // Step 4: The sub process exit with the exitCode
W
wenjun 已提交
514 515
        exit(exitCode);
    } else {
M
mamingshuai 已提交
516
        // Step 5: The parent process wait for the sub process to exit and obtain the exitCode
W
wenjun 已提交
517
        waitpid(pid, &status, 0);
M
mamingshuai 已提交
518 519
        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 已提交
520 521 522
    }
}

M
mamingshuai 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
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 已提交
543
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0010
M
mamingshuai 已提交
544
 * @tc.name       : Invoke the interface to set the process UID
W
wenjun 已提交
545 546
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
547
HWTEST_F(DacTestSuite, DACTest0010, Function | MediumTest | Level1)
W
wenjun 已提交
548 549
{
    int ret;
M
mamingshuai 已提交
550 551
    uid_t ruid, euid, suid, newruid, neweuid, newsuid;
    // Preset action: Obtain the ruid, euid, suid of the current process
W
wenjun 已提交
552
    getresuid(&ruid, &euid, &suid);
M
mamingshuai 已提交
553 554 555 556 557 558 559 560 561 562 563 564 565
    // 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 已提交
566
    ret = setresuid(ruid, euid, suid);
M
mamingshuai 已提交
567 568 569 570 571
    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 已提交
572 573
}

M
mamingshuai 已提交
574
/*
W
wenjun 已提交
575
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0020
M
mamingshuai 已提交
576
 * @tc.name       : Invoke the interface to set the process GID
W
wenjun 已提交
577 578
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
579
HWTEST_F(DacTestSuite, DACTest0020, Function | MediumTest | Level1)
W
wenjun 已提交
580 581
{
    int ret;
M
mamingshuai 已提交
582 583
    gid_t rgid, egid, sgid, newrgid, newegid, newsgid;
    // Preset action: Obtain the rgid, egid, sgid of the current process
W
wenjun 已提交
584
    getresgid(&rgid, &egid, &sgid);
M
mamingshuai 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597
    // 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 已提交
598
    ret = setresgid(rgid, egid, sgid);
M
mamingshuai 已提交
599 600 601 602 603
    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 已提交
604 605
}

M
mamingshuai 已提交
606
/*
W
wenjun 已提交
607
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0030
M
mamingshuai 已提交
608
 * @tc.name       : Invoke the setgroups interface to set the process groups that contain a single GID or an empty value
W
wenjun 已提交
609 610
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
611
HWTEST_F(DacTestSuite, DACTest0030, Function | MediumTest | Level1)
W
wenjun 已提交
612 613 614
{
    int ret;
    gid_t grouplist[SIZE255];
M
mamingshuai 已提交
615
    // Preset action: Obtain the groups of the current process
W
wenjun 已提交
616 617 618 619
    int groupsize = getgroups(0, grouplist);
    if (groupsize >= 0) {
        getgroups(groupsize, grouplist);
    } else {
M
mamingshuai 已提交
620
        EXPECT_GE(groupsize, 0) << "ErrInfo: Failed to get groups";
W
wenjun 已提交
621
    }
M
mamingshuai 已提交
622
    // Preset action: Obtain the group lists required for the testcase
W
wenjun 已提交
623 624 625
    gid_t list1[SIZE1] = {1};
    gid_t list2[SIZE1] = {MAX_INT};
    gid_t list3[SIZE1] = {};
M
mamingshuai 已提交
626
    // Step 1: Set the group list to {1}
W
wenjun 已提交
627
    ret = setgroups(SIZE1, list1);
M
mamingshuai 已提交
628 629
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set the group list to {1}";
    // Step 2: Set the group list to {2147483647}
W
wenjun 已提交
630
    ret = setgroups(SIZE1, list2);
M
mamingshuai 已提交
631 632
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set the group list to {2147483647}";
    // Step 3: Set the group list to {}
W
wenjun 已提交
633
    ret = setgroups(SIZE1, list3);
M
mamingshuai 已提交
634 635 636
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set the group list to {}";
    // Cleanup action: Restore the initial groups of the process
    setgroups(0, nullptr);
W
wenjun 已提交
637
    ret = setgroups(groupsize, grouplist);
M
mamingshuai 已提交
638
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to restore the initial groups of the process";
W
wenjun 已提交
639 640
}

M
mamingshuai 已提交
641
/*
W
wenjun 已提交
642
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0040
M
mamingshuai 已提交
643
 * @tc.name       : Invoke the setgroups interface to set the process groups that contain the same GID
W
wenjun 已提交
644 645
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
646
HWTEST_F(DacTestSuite, DACTest0040, Function | MediumTest | Level2)
W
wenjun 已提交
647 648 649
{
    int ret;
    gid_t grouplist[SIZE255];
M
mamingshuai 已提交
650
    // Preset action: Obtain the groups of the current process
W
wenjun 已提交
651 652 653 654
    int groupsize = getgroups(0, grouplist);
    if (groupsize >= 0) {
        getgroups(groupsize, grouplist);
    } else {
M
mamingshuai 已提交
655
        EXPECT_GE(groupsize, 0) << "ErrInfo: Failed to get groups";
W
wenjun 已提交
656
    }
M
mamingshuai 已提交
657 658 659 660 661
    // 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}
W
wenjun 已提交
662
    ret = setgroups(SIZE2, list1);
M
mamingshuai 已提交
663 664
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set the group list to {0, 0}";
    // Step 2: Set the group list to {1, 1}
W
wenjun 已提交
665
    ret = setgroups(SIZE2, list2);
M
mamingshuai 已提交
666 667
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set the group list to {1, 1}";
    // Step 3: Set the group list to {2147483647, 2147483647}
W
wenjun 已提交
668
    ret = setgroups(SIZE2, list3);
M
mamingshuai 已提交
669 670
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set the group list to {2147483647, 2147483647}";
    // Cleanup action: Restore the initial groups of the process
W
wenjun 已提交
671 672
    setgroups(0, nullptr);
    ret = setgroups(groupsize, grouplist);
M
mamingshuai 已提交
673
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to restore the initial groups of the process";
W
wenjun 已提交
674 675
}

M
mamingshuai 已提交
676
/*
W
wenjun 已提交
677
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0050
M
mamingshuai 已提交
678
 * @tc.name       : Invoke the setgroups interface to set the process groups that contain the duplicate GIDs
W
wenjun 已提交
679 680
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
681
HWTEST_F(DacTestSuite, DACTest0050, Function | MediumTest | Level3)
W
wenjun 已提交
682 683 684
{
    int ret;
    gid_t grouplist[SIZE255];
M
mamingshuai 已提交
685
    // Preset action: Obtain the groups of the current process
W
wenjun 已提交
686 687 688 689
    int groupsize = getgroups(0, grouplist);
    if (groupsize >= 0) {
        getgroups(groupsize, grouplist);
    } else {
M
mamingshuai 已提交
690
        EXPECT_GE(groupsize, 0) << "ErrInfo: Failed to get groups";
W
wenjun 已提交
691
    }
M
mamingshuai 已提交
692 693 694 695 696
    // 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}
W
wenjun 已提交
697
    ret = setgroups(SIZE3, list1);
M
mamingshuai 已提交
698 699
    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}
W
wenjun 已提交
700
    ret = setgroups(SIZE3, list2);
M
mamingshuai 已提交
701 702
    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}
W
wenjun 已提交
703
    ret = setgroups(SIZE3, list3);
M
mamingshuai 已提交
704 705
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to set the group list to {0, 2147483647, 2147483647}";
    // Cleanup action: Restore the initial groups of the process
W
wenjun 已提交
706 707
    setgroups(0, nullptr);
    ret = setgroups(groupsize, grouplist);
M
mamingshuai 已提交
708
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to restore the initial groups of the process";
W
wenjun 已提交
709 710
}

M
mamingshuai 已提交
711
/*
W
wenjun 已提交
712
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0060
M
mamingshuai 已提交
713
 * @tc.name       : Invoke the setgroups interface to set the process groups that contain all different GIDs
W
wenjun 已提交
714 715
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
716
HWTEST_F(DacTestSuite, DACTest0060, Function | MediumTest | Level3)
W
wenjun 已提交
717 718 719
{
    int ret;
    gid_t grouplist[SIZE255];
M
mamingshuai 已提交
720
    // Preset action: Obtain the groups of the current process
W
wenjun 已提交
721 722 723 724
    int groupsize = getgroups(0, grouplist);
    if (groupsize >= 0) {
        getgroups(groupsize, grouplist);
    } else {
M
mamingshuai 已提交
725
        EXPECT_GE(groupsize, 0) << "ErrInfo: Failed to get groups";
W
wenjun 已提交
726
    }
M
mamingshuai 已提交
727
    // Preset action: Obtain the group lists required for the testcase
W
wenjun 已提交
728 729 730 731 732 733 734 735 736 737
    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;
M
mamingshuai 已提交
738 739 740 741
    // 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
W
wenjun 已提交
742
    ret = setgroups(INVAILD_SIZE, list1);
M
mamingshuai 已提交
743 744
    EXPECT_EQ(ret, FALSE) << "ErrInfo: Set groups size over max, size=65537";
    // Cleanup action: Restore the initial groups of the process
W
wenjun 已提交
745 746
    setgroups(0, nullptr);
    ret = setgroups(groupsize, grouplist);
M
mamingshuai 已提交
747
    EXPECT_EQ(ret, 0) << "ErrInfo: Failed to restore the initial groups of the process";
W
wenjun 已提交
748 749
}

M
mamingshuai 已提交
750
/*
W
wenjun 已提交
751
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0070
M
mamingshuai 已提交
752 753
 * @tc.name       : Invoke the setuid, gid, and groups interfaces to set the uid, gid,
                    and groups of processes concurrently
W
wenjun 已提交
754 755
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
756
HWTEST_F(DacTestSuite, DACTest0070, Security | MediumTest | Level2)
W
wenjun 已提交
757 758 759
{
    int ret;
    int status = 0;
M
mamingshuai 已提交
760
    // Preset action: Fork three sub processes
W
wenjun 已提交
761 762 763 764 765 766 767 768 769
    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 已提交
770
    // get one parent & three children
W
wenjun 已提交
771 772 773
    if (pid == 0) {
        int exitCode = 0;
        for (int number = 0; number < NUM3000; number++) {
M
mamingshuai 已提交
774 775 776 777 778
            // 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 已提交
779 780
            ret = TestSetUid();
            if (ret != 0) {
M
mamingshuai 已提交
781
                LOG("ErrInfo: TestSetUid error during the %d time", number);
W
wenjun 已提交
782 783 784
                exitCode = 1;
                break;
            }
M
mamingshuai 已提交
785
            // Step 2: Test the 'setgid' interface concurrently
W
wenjun 已提交
786 787
            ret = TestSetGid();
            if (ret != 0) {
M
mamingshuai 已提交
788
                LOG("ErrInfo: TestSetGid error during the %d time", number);
W
wenjun 已提交
789 790 791
                exitCode = 1;
                break;
            }
M
mamingshuai 已提交
792
            // Step 2: Test the 'setgroups' interface concurrently
W
wenjun 已提交
793 794
            ret = TestSetGroups();
            if (ret != 0) {
M
mamingshuai 已提交
795
                LOG("ErrInfo: TestSetGroups error during the %d time", number);
W
wenjun 已提交
796 797 798 799
                exitCode = 1;
                break;
            }
        }
M
mamingshuai 已提交
800
        // Step 3: Three sub processes exit with the exitCode
W
wenjun 已提交
801 802
        exit(exitCode);
    } else {
M
mamingshuai 已提交
803 804 805 806 807 808
        // 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 已提交
809 810 811 812
        }
    }
}

M
mamingshuai 已提交
813
/*
W
wenjun 已提交
814
 * @tc.number     : SUB_SEC_AppSEC_PermissionMgmt_DAC_0080
M
mamingshuai 已提交
815
 * @tc.name       : Inheritance of process UID, GID and groups
W
wenjun 已提交
816 817
 * @tc.desc       : [C- SECURITY -0200]
 */
M
mamingshuai 已提交
818
HWTEST_F(DacTestSuite, DACTest0080, Function | MediumTest | Level2)
W
wenjun 已提交
819
{
M
mamingshuai 已提交
820 821 822 823 824 825 826 827
    int ret;
    uid_t ruid, euid, suid, newruid, neweuid, newsuid;
    gid_t rgid, egid, sgid, newrgid, newegid, newsgid, grouplist[SIZE255];
    // 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 已提交
828 829 830 831
    int groupsize = getgroups(0, grouplist);
    if (groupsize >= 0) {
        getgroups(groupsize, grouplist);
    } else {
M
mamingshuai 已提交
832
        EXPECT_GE(groupsize, 0) << "ErrInfo: Failed to get groups";
W
wenjun 已提交
833
    }
M
mamingshuai 已提交
834
    // Preset action: Obtain the group lists required for the testcase
W
wenjun 已提交
835 836 837 838 839 840 841
    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;
M
mamingshuai 已提交
842
    // Step 1: Factor combination test of UID, GID, and groups
W
wenjun 已提交
843 844 845 846 847 848 849
    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);
M
mamingshuai 已提交
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
    // 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 已提交
869

M
mamingshuai 已提交
870 871 872 873 874 875 876 877 878 879 880 881
/*
 * @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 已提交
882
    usleep(SLEEP_NUM);
M
mamingshuai 已提交
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
    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);
}
#endif