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 TEST_SET_OLD_ID = 1001;
35 const int TEST_SET_NEW_ID = 4;
36 static const int SLEEP_TIME = 3;
37
GetIdMapPath(int pid,const std::string & mapType)38 std::string GetIdMapPath(int pid, const std::string& mapType)
39 {
40 std::ostringstream buf;
41 buf << "/proc/" << pid << "/" << mapType;
42 return buf.str();
43 }
44
WriteIdMap(int pid)45 int WriteIdMap(int pid)
46 {
47 std::string uidMapPath = GetIdMapPath(pid, "uid_map");
48 std::string gidMapPath = GetIdMapPath(pid, "gid_map");
49
50 const char* idMapStr = "0 1000 1\n1 1001 1\n2 1002 1\n3 1003 1\n4 1004 1\n";
51 int strLen = strlen(idMapStr);
52 int uidMap = open(uidMapPath.c_str(), O_WRONLY);
53 if (uidMap == -1) {
54 return EXIT_CODE_ERRNO_1;
55 }
56
57 int ret = write(uidMap, idMapStr, strLen);
58 if (ret != strLen) {
59 close(uidMap);
60 return EXIT_CODE_ERRNO_2;
61 }
62 close(uidMap);
63
64 int gidMap = open(gidMapPath.c_str(), O_WRONLY);
65 if (gidMap == -1) {
66 close(gidMap);
67 return EXIT_CODE_ERRNO_3;
68 }
69
70 ret = write(gidMap, idMapStr, strLen);
71 if (ret != strLen) {
72 close(gidMap);
73 return EXIT_CODE_ERRNO_4;
74 }
75
76 close(gidMap);
77 return 0;
78 }
79
childFunc(void * arg)80 static int childFunc(void *arg)
81 {
82 (void)arg;
83 sleep(SLEEP_TIME);
84
85 int newUid = getuid();
86 if (newUid != 1) {
87 return EXIT_CODE_ERRNO_1;
88 }
89
90 int newGid = getgid();
91 if (newGid != 1) {
92 return EXIT_CODE_ERRNO_2;
93 }
94
95 int ret = setuid(TEST_SET_NEW_ID);
96 if (ret != 0) {
97 return EXIT_CODE_ERRNO_3;
98 }
99
100 ret = setgid(TEST_SET_NEW_ID);
101 if (ret != 0) {
102 return EXIT_CODE_ERRNO_4;
103 }
104
105 newUid = getuid();
106 if (newUid != TEST_SET_NEW_ID) {
107 return EXIT_CODE_ERRNO_5;
108 }
109
110 newGid = getgid();
111 if (newGid != TEST_SET_NEW_ID) {
112 return EXIT_CODE_ERRNO_6;
113 }
114
115 exit(EXIT_TRUE_CODE);
116 }
117
ItUserContainer002(void)118 void ItUserContainer002(void)
119 {
120 int ret = setuid(TEST_SET_OLD_ID);
121 ASSERT_EQ(ret, 0);
122
123 int oldUid = getuid();
124 ASSERT_EQ(oldUid, TEST_SET_OLD_ID);
125
126 ret = setgid(TEST_SET_OLD_ID);
127 ASSERT_EQ(ret, 0);
128
129 int oldGid = getgid();
130 ASSERT_EQ(oldGid, TEST_SET_OLD_ID);
131
132 int arg = CHILD_FUNC_ARG;
133 auto pid = CloneWrapper(childFunc, CLONE_NEWUSER, &arg);
134 ASSERT_NE(pid, -1);
135
136 ret = WriteIdMap(pid);
137 ASSERT_EQ(ret, 0);
138
139 int status;
140 ret = waitpid(pid, &status, 0);
141 ASSERT_EQ(ret, pid);
142
143 status = WEXITSTATUS(status);
144 ASSERT_EQ(status, EXIT_TRUE_CODE);
145
146 int oldUid1 = getuid();
147 ASSERT_EQ(oldUid1, TEST_SET_OLD_ID);
148
149 int oldGid1 = getgid();
150 ASSERT_EQ(oldGid1, TEST_SET_OLD_ID);
151
152 ASSERT_EQ(oldUid, oldUid1);
153 ASSERT_EQ(oldGid, oldGid1);
154 }
155