• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  * conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  * of conditions and the following disclaimer in the documentation and/or other materials
13  * provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  * to endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 #define __USE_GUN
32 #include "it_test_exc.h"
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <sys/mman.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 
40 static char *g_args[] = {
41     "hic et nunc",
42     "-l",
43     "/dev/shm",
44     nullptr
45 };
46 
TestCase(void)47 static int TestCase(void)
48 {
49     struct stat st = {0};
50     void *p = nullptr;
51     int fd = 0;
52     int shmFd = 0;
53     int rc = 0;
54     int ret = 0;
55 
56     shmFd = shm_open("wurstverschwendung", O_RDWR | O_CREAT, 0777);
57     if (shmFd == -1) {
58         perror("shm_open");
59         return -1;
60     }
61 
62     rc = stat("/bin/shell", &st);
63     if (rc == -1) {
64         perror("stat");
65         close(shmFd);
66         return -1;
67     }
68 
69     printf("shm_fd=%d,st.st_size=%lld\n", shmFd, st.st_size);
70     rc = ftruncate(shmFd, st.st_size);
71     if (rc == -1) {
72         perror("ftruncate");
73         close(shmFd);
74         return -1;
75     }
76 
77     p = mmap(nullptr, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, shmFd, 0);
78     if (p == MAP_FAILED) {
79         perror("mmap");
80         close(shmFd);
81         return -1;
82     }
83 
84     fd = open("/bin/shell", O_RDONLY, 0);
85     printf("fd=%d\n", fd);
86     if (fd == -1) {
87         perror("openls");
88         munmap(p, st.st_size);
89         close(shmFd);
90         return -1;
91     }
92 
93     rc = read(fd, p, st.st_size);
94     if (rc == -1) {
95         perror("read");
96         munmap(p, st.st_size);
97         close(shmFd);
98         return -1;
99     }
100     if (rc != st.st_size) {
101         fputs("Strange situation!\n", stderr);
102         munmap(p, st.st_size);
103         close(shmFd);
104         return -1;
105     }
106 
107     munmap(p, st.st_size);
108     close(shmFd);
109 
110     shmFd = shm_open("wurstverschwendung", O_RDONLY, 0);
111     ret = fexecve(shmFd, g_args, environ);
112     perror("fexecve");
113     ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
114 
115     return LOS_OK;
116 }
117 
ItTestFexecve001(void)118 void ItTestFexecve001(void)
119 {
120     TEST_ADD_CASE(__FUNCTION__, TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
121 }
122