• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <signal.h>
18 #include <string.h>
19 #include <sys/wait.h>
20 #include "fortify_test.h"
21 #include "test.h"
22 
23 #define FILE_MODE_ALL (0777)
24 
25 /**
26  * @tc.name     : open_0010
27  * @tc.desc     : test open normal condition
28  * @tc.level    : Level 0
29  */
open_0010(void)30 static void open_0010(void)
31 {
32     int fd = open("/proc/version", O_RDWR | O_CREAT, FILE_MODE_ALL);
33     TEST(fd != -1);
34     close(fd);
35 
36     return;
37 }
38 
39 /**
40  * @tc.name     : open_0020
41  * @tc.desc     : test open O_CREAT without mode
42  * @tc.level    : Level 2
43  */
open_0020(void)44 static void open_0020(void)
45 {
46     struct sigaction sigabrt = {
47         .sa_handler = SignalHandler,
48     };
49     sigaction(SIGABRT, &sigabrt, NULL);
50 
51     int flags = O_CREAT;
52     int status;
53     int pid = fork();
54     switch (pid) {
55         case -1:
56             t_error("fork failed: %s\n", strerror(errno));
57             break;
58         case 0:
59             open("/proc/version", flags);
60             exit(0);
61         default:
62             waitpid(pid, &status, WUNTRACED);
63             TEST(WIFEXITED(status) == 0);
64             TEST(WIFSTOPPED(status) == 1);
65             TEST(WSTOPSIG(status) == SIGSTOP);
66             kill(pid, SIGCONT);
67             break;
68     }
69 
70     return;
71 }
72 
73 /**
74  * @tc.name     : open_0030
75  * @tc.desc     : test open O_TMPFILE without mode
76  * @tc.level    : Level 2
77  */
open_0030(void)78 static void open_0030(void)
79 {
80     struct sigaction sigabrt = {
81         .sa_handler = SignalHandler,
82     };
83     sigaction(SIGABRT, &sigabrt, NULL);
84 
85     int flags = O_TMPFILE;
86     int status;
87     int pid = fork();
88     switch (pid) {
89         case -1:
90             t_error("fork failed: %s\n", strerror(errno));
91             break;
92         case 0:
93             open("/proc/version", flags);
94             exit(0);
95         default:
96             waitpid(pid, &status, WUNTRACED);
97             TEST(WIFEXITED(status) == 0);
98             TEST(WIFSTOPPED(status) == 1);
99             TEST(WSTOPSIG(status) == SIGSTOP);
100             kill(pid, SIGCONT);
101             break;
102     }
103 
104     return;
105 }
106 
107 
108 /**
109  * @tc.name     : openat_0010
110  * @tc.desc     : test openat normal condition
111  * @tc.level    : Level 0
112  */
openat_0010(void)113 static void openat_0010(void)
114 {
115     int fd = openat(AT_FDCWD, "/proc/version", O_RDWR | O_CREAT, FILE_MODE_ALL);
116     TEST(fd != -1);
117     close(fd);
118 
119     return;
120 }
121 
122 /**
123  * @tc.name     : openat_0020
124  * @tc.desc     : test openat O_CREAT without mode
125  * @tc.level    : Level 2
126  */
openat_0020(void)127 static void openat_0020(void)
128 {
129     struct sigaction sigabrt = {
130         .sa_handler = SignalHandler,
131     };
132     sigaction(SIGABRT, &sigabrt, NULL);
133 
134     int flags = O_CREAT;
135     int status;
136     int pid = fork();
137     switch (pid) {
138         case -1:
139             t_error("fork failed: %s\n", strerror(errno));
140             break;
141         case 0:
142             openat(AT_FDCWD, "/proc/version", flags);
143             exit(0);
144         default:
145             waitpid(pid, &status, WUNTRACED);
146             TEST(WIFEXITED(status) == 0);
147             TEST(WIFSTOPPED(status) == 1);
148             TEST(WSTOPSIG(status) == SIGSTOP);
149             kill(pid, SIGCONT);
150             break;
151     }
152 
153     return;
154 }
155 
156 /**
157  * @tc.name     : openat_0030
158  * @tc.desc     : test openat O_TMPFILE without mode
159  * @tc.level    : Level 2
160  */
openat_0030(void)161 static void openat_0030(void)
162 {
163     struct sigaction sigabrt = {
164         .sa_handler = SignalHandler,
165     };
166     sigaction(SIGABRT, &sigabrt, NULL);
167 
168     int flags = O_TMPFILE;
169     int status;
170     int pid = fork();
171     switch (pid) {
172         case -1:
173             t_error("fork failed: %s\n", strerror(errno));
174             break;
175         case 0:
176             openat(AT_FDCWD, "/proc/version", flags);
177             exit(0);
178         default:
179             waitpid(pid, &status, WUNTRACED);
180             TEST(WIFEXITED(status) == 0);
181             TEST(WIFSTOPPED(status) == 1);
182             TEST(WSTOPSIG(status) == SIGSTOP);
183             kill(pid, SIGCONT);
184             break;
185     }
186 
187     return;
188 }
189 
190 #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
191 /**
192  * @tc.name     : open64_0010
193  * @tc.desc     : test open64 normal condition
194  * @tc.level    : Level 0
195  */
open64_0010(void)196 static void open64_0010(void)
197 {
198     int fd = open64("/proc/version", O_RDWR | O_CREAT, FILE_MODE_ALL);
199     TEST(fd != -1);
200     close(fd);
201 
202     return;
203 }
204 
205 /**
206  * @tc.name     : open64_0020
207  * @tc.desc     : test open64 O_CREAT without mode
208  * @tc.level    : Level 2
209  */
open64_0020(void)210 static void open64_0020(void)
211 {
212     struct sigaction sigabrt = {
213         .sa_handler = SignalHandler,
214     };
215     sigaction(SIGABRT, &sigabrt, NULL);
216 
217     int flags = O_CREAT;
218     int status;
219     int pid = fork();
220     switch (pid) {
221         case -1:
222             t_error("fork failed: %s\n", strerror(errno));
223             break;
224         case 0:
225             open64("/proc/version", flags);
226             exit(0);
227         default:
228             waitpid(pid, &status, WUNTRACED);
229             TEST(WIFEXITED(status) == 0);
230             TEST(WIFSTOPPED(status) == 1);
231             TEST(WSTOPSIG(status) == SIGSTOP);
232             kill(pid, SIGCONT);
233             break;
234     }
235 
236     return;
237 }
238 
239 /**
240  * @tc.name     : open64_0030
241  * @tc.desc     : test open64 O_TMPFILE without mode
242  * @tc.level    : Level 2
243  */
open64_0030(void)244 static void open64_0030(void)
245 {
246     struct sigaction sigabrt = {
247         .sa_handler = SignalHandler,
248     };
249     sigaction(SIGABRT, &sigabrt, NULL);
250 
251     int flags = O_TMPFILE;
252     int status;
253     int pid = fork();
254     switch (pid) {
255         case -1:
256             t_error("fork failed: %s\n", strerror(errno));
257             break;
258         case 0:
259             open64("/proc/version", flags);
260             exit(0);
261         default:
262             waitpid(pid, &status, WUNTRACED);
263             TEST(WIFEXITED(status) == 0);
264             TEST(WIFSTOPPED(status) == 1);
265             TEST(WSTOPSIG(status) == SIGSTOP);
266             kill(pid, SIGCONT);
267             break;
268     }
269 
270     return;
271 }
272 
273 
274 /**
275  * @tc.name     : openat64_0010
276  * @tc.desc     : test openat64 normal condition
277  * @tc.level    : Level 0
278  */
openat64_0010(void)279 static void openat64_0010(void)
280 {
281     int fd = openat64(AT_FDCWD, "/proc/version", O_RDWR | O_CREAT, FILE_MODE_ALL);
282     TEST(fd != -1);
283     close(fd);
284 
285     return;
286 }
287 
288 /**
289  * @tc.name     : openat64_0020
290  * @tc.desc     : test openat64 O_CREAT without mode
291  * @tc.level    : Level 2
292  */
openat64_0020(void)293 static void openat64_0020(void)
294 {
295     struct sigaction sigabrt = {
296         .sa_handler = SignalHandler,
297     };
298     sigaction(SIGABRT, &sigabrt, NULL);
299 
300     int flags = O_CREAT;
301     int status;
302     int pid = fork();
303     switch (pid) {
304         case -1:
305             t_error("fork failed: %s\n", strerror(errno));
306             break;
307         case 0:
308             openat64(AT_FDCWD, "/proc/version", flags);
309             exit(0);
310         default:
311             waitpid(pid, &status, WUNTRACED);
312             TEST(WIFEXITED(status) == 0);
313             TEST(WIFSTOPPED(status) == 1);
314             TEST(WSTOPSIG(status) == SIGSTOP);
315             kill(pid, SIGCONT);
316             break;
317     }
318 
319     return;
320 }
321 
322 /**
323  * @tc.name     : openat64_0030
324  * @tc.desc     : test openat64 O_TMPFILE without mode
325  * @tc.level    : Level 2
326  */
openat64_0030(void)327 static void openat64_0030(void)
328 {
329     struct sigaction sigabrt = {
330         .sa_handler = SignalHandler,
331     };
332     sigaction(SIGABRT, &sigabrt, NULL);
333 
334     int flags = O_TMPFILE;
335     int status;
336     int pid = fork();
337     switch (pid) {
338         case -1:
339             t_error("fork failed: %s\n", strerror(errno));
340             break;
341         case 0:
342             openat64(AT_FDCWD, "/proc/version", flags);
343             exit(0);
344         default:
345             waitpid(pid, &status, WUNTRACED);
346             TEST(WIFEXITED(status) == 0);
347             TEST(WIFSTOPPED(status) == 1);
348             TEST(WSTOPSIG(status) == SIGSTOP);
349             kill(pid, SIGCONT);
350             break;
351     }
352 
353     return;
354 }
355 #endif
356 
main(int argc,char * argv[])357 int main(int argc, char *argv[]) {
358     open_0010();
359     open_0020();
360     open_0030();
361     openat_0010();
362     openat_0020();
363     openat_0030();
364 #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
365     open64_0010();
366     open64_0020();
367     open64_0030();
368     openat64_0010();
369     openat64_0020();
370     openat64_0030();
371 #endif
372     return t_status;
373 }