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 using namespace std;
32
OpendirCheck(void)33 static int OpendirCheck(void)
34 {
35 DIR *dir = opendir("/proc");
36 if (dir == nullptr) {
37 return EXIT_CODE_ERRNO_1;
38 }
39 closedir(dir);
40 return 0;
41 }
42
ChildFunc(void * arg)43 static int ChildFunc(void *arg)
44 {
45 int ret = 0;
46 ret = OpendirCheck();
47 if (ret == 0) {
48 return EXIT_CODE_ERRNO_1;
49 }
50
51 return 0;
52 }
53
TestFunc(void * arg)54 static int TestFunc(void *arg)
55 {
56 int ret = 0;
57 int fd;
58 char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE,
59 MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
60 if (stack == nullptr) {
61 return EXIT_CODE_ERRNO_1;
62 }
63 char *stackTop = stack + STACK_SIZE;
64
65 ret = OpendirCheck();
66 if (ret != 0) {
67 return EXIT_CODE_ERRNO_2;
68 }
69
70 ret = chroot("/system/etc");
71 if (ret != 0) {
72 return EXIT_CODE_ERRNO_3;
73 }
74
75 ret = OpendirCheck();
76 if (ret == 0) {
77 return EXIT_CODE_ERRNO_4;
78 }
79 fd = open("/PCID.sc", O_RDONLY);
80 if (fd == -1) {
81 return EXIT_CODE_ERRNO_5;
82 }
83 close(fd);
84 sleep(1);
85
86 auto pid = clone(ChildFunc, stackTop, SIGCHLD, arg);
87 if (pid == -1) {
88 return EXIT_CODE_ERRNO_6;
89 }
90 int status;
91 ret = waitpid(pid, &status, 0);
92 ret = WIFEXITED(status);
93 int exitCode = WEXITSTATUS(status);
94 if (exitCode != 0) {
95 return EXIT_CODE_ERRNO_7;
96 }
97
98 return 0;
99 }
100
ItContainerChroot002(void)101 void ItContainerChroot002(void)
102 {
103 int ret = 0;
104 char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE,
105 MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
106 ASSERT_TRUE(stack != nullptr);
107 char *stackTop = stack + STACK_SIZE;
108
109 int arg = CHILD_FUNC_ARG;
110 auto pid = clone(TestFunc, stackTop, SIGCHLD, &arg);
111 ASSERT_NE(pid, -1);
112
113 int status;
114 ret = waitpid(pid, &status, 0);
115 ret = WIFEXITED(status);
116 int exitCode = WEXITSTATUS(status);
117 ASSERT_EQ(exitCode, 0);
118 }
119