1 /*
2 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 #include "It_container_test.h"
31 #include "sys/utsname.h"
32
33 const int EXIT_TRUE_CODE = 255;
34 const int MAX_PID_RANGE = 100000;
35 const int TEST_SET_OLD_ID = 1001;
36 const int TEST_SET_NEW_ID = 4;
37
38 std::string GetIdMapPath(int pid, const std::string& mapType);
39 int WriteIdMap(int pid);
40
TestMap(int oldUid,int oldGid)41 static int TestMap(int oldUid, int oldGid)
42 {
43 pid_t pid = getpid();
44 int ret = WriteIdMap(pid);
45 if (ret != 0) {
46 return EXIT_CODE_ERRNO_6;
47 }
48
49 int newUid = getuid();
50 if (newUid != 1) {
51 return EXIT_CODE_ERRNO_7;
52 }
53
54 int newGid = getgid();
55 if (newGid != 1) {
56 return EXIT_CODE_ERRNO_8;
57 }
58
59 ret = setuid(TEST_SET_NEW_ID);
60 if (ret != 0) {
61 return EXIT_CODE_ERRNO_9;
62 }
63
64 ret = setgid(TEST_SET_NEW_ID);
65 if (ret != 0) {
66 return EXIT_CODE_ERRNO_10;
67 }
68
69 newUid = getuid();
70 if (newUid != TEST_SET_NEW_ID) {
71 return EXIT_CODE_ERRNO_11;
72 }
73
74 newGid = getgid();
75 if (newGid != TEST_SET_NEW_ID) {
76 exit(1);
77 }
78
79 if (oldUid == newUid) {
80 return EXIT_CODE_ERRNO_12;
81 }
82
83 if (oldGid == newGid) {
84 return EXIT_CODE_ERRNO_13;
85 }
86 return 0;
87 }
88
childFunc(void * arg)89 static int childFunc(void *arg)
90 {
91 (void)arg;
92 int ret;
93 int oldUid;
94 int oldGid;
95
96 ret = setuid(TEST_SET_OLD_ID);
97 if (ret != 0) {
98 return EXIT_CODE_ERRNO_1;
99 }
100
101 oldUid = getuid();
102 if (oldUid != TEST_SET_OLD_ID) {
103 return EXIT_CODE_ERRNO_2;
104 }
105
106 ret = setgid(TEST_SET_OLD_ID);
107 if (ret != 0) {
108 return EXIT_CODE_ERRNO_3;
109 }
110
111 oldGid = getgid();
112 if (oldGid != TEST_SET_OLD_ID) {
113 return EXIT_CODE_ERRNO_4;
114 }
115
116 ret = unshare(CLONE_NEWUSER);
117 if (ret == -1) {
118 return EXIT_CODE_ERRNO_5;
119 }
120
121 ret = TestMap(oldUid, oldGid);
122 if (ret != 0) {
123 return ret;
124 }
125
126 exit(EXIT_TRUE_CODE);
127 }
128
ItUserContainer003(void)129 void ItUserContainer003(void)
130 {
131 int ret;
132 int status = 0;
133 int arg = CHILD_FUNC_ARG;
134 auto pid = CloneWrapper(childFunc, SIGCHLD, &arg);
135 ASSERT_NE(pid, -1);
136
137 ret = waitpid(pid, &status, 0);
138 ASSERT_EQ(ret, pid);
139
140 status = WEXITSTATUS(status);
141 ASSERT_EQ(status, EXIT_TRUE_CODE);
142 }
143