• 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         return -1;
66     }
67 
68     printf("shm_fd=%d,st.st_size=%lld\n", shmFd, st.st_size);
69     rc = ftruncate(shmFd, st.st_size);
70     if (rc == -1) {
71         perror("ftruncate");
72         return -1;
73     }
74 
75     p = mmap(nullptr, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, shmFd, 0);
76     if (p == MAP_FAILED) {
77         perror("mmap");
78         return -1;
79     }
80 
81     fd = open("/bin/shell", O_RDONLY, 0);
82     printf("fd=%d\n", fd);
83     if (fd == -1) {
84         perror("openls");
85         return -1;
86     }
87 
88     rc = read(fd, p, st.st_size);
89     if (rc == -1) {
90         perror("read");
91         return -1;
92     }
93     if (rc != st.st_size) {
94         fputs("Strange situation!\n", stderr);
95         return -1;
96     }
97 
98     munmap(p, st.st_size);
99     close(shmFd);
100 
101     shmFd = shm_open("wurstverschwendung", O_RDONLY, 0);
102     ret = fexecve(shmFd, g_args, environ);
103     perror("fexecve");
104     ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
105 
106     return LOS_OK;
107 }
108 
ItTestFexecve001(void)109 void ItTestFexecve001(void)
110 {
111     TEST_ADD_CASE(__FUNCTION__, TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
112 }
113