• 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 
16 #include <fcntl.h>
17 #include <limits.h>
18 #include <unistd.h>
19 #include <signal.h>
20 #include <string.h>
21 #include <sys/wait.h>
22 #include "fortify_test.h"
23 #include "test.h"
24 
25 /**
26  * @tc.name     : getcwd
27  * @tc.desc     : Exception scenarios for getcwd
28  * @tc.level    : Level 2
29  */
unistd_dynamic_chk_001(void)30 static void unistd_dynamic_chk_001(void)
31 {
32     struct sigaction sigabrt = {
33         .sa_handler = SignalHandler,
34     };
35 
36     sigaction(SIGABRT, &sigabrt, NULL);
37     const int bufferSize = 1;
38     char buf[bufferSize];
39     size_t n = atoi("2");
40     int status;
41     int pid = fork();
42     switch (pid) {
43         case -1:
44             t_error("fork failed: %d\n", __LINE__);
45             break;
46         case 0:
47             getcwd(buf, n);
48             exit(0);
49         default:
50             waitpid(pid, &status, WUNTRACED);
51             TEST(WIFEXITED(status) == 0);
52             TEST(WIFSTOPPED(status) == 1);
53             TEST(WSTOPSIG(status) == SIGSTOP);
54             kill(pid, SIGCONT);
55             break;
56     }
57     return;
58 }
59 
60 /**
61  * @tc.name     : pread
62  * @tc.desc     : Exception scenarios for pread
63  * @tc.level    : Level 2
64  */
unistd_dynamic_chk_002(void)65 static void unistd_dynamic_chk_002(void)
66 {
67     struct sigaction sigabrt = {
68         .sa_handler = SignalHandler,
69     };
70     sigaction(SIGABRT, &sigabrt, NULL);
71 
72     const int err = -1;
73     int fd = open("/proc/version", O_RDWR | O_CREAT, 0777);
74     TEST(fd != err);
75 
76     int status;
77     const int bufferSize = 2;
78     char buf[bufferSize];
79     size_t n = atoi("4");
80     int pid = fork();
81     switch (pid) {
82         case -1:
83             t_error("fork failed: %d\n", __LINE__);
84             break;
85         case 0:
86             pread(fd, buf, n, 0);
87             exit(0);
88         default:
89             waitpid(pid, &status, WUNTRACED);
90             TEST(WIFEXITED(status) == 0);
91             TEST(WIFSTOPPED(status) == 1);
92             TEST(WSTOPSIG(status) == SIGSTOP);
93             kill(pid, SIGCONT);
94             break;
95     }
96     close(fd);
97     return;
98 }
99 
100 /**
101  * @tc.name     : pread
102  * @tc.desc     : Exception scenarios for pread
103  * @tc.level    : Level 2
104  */
unistd_dynamic_chk_003(void)105 static void unistd_dynamic_chk_003(void)
106 {
107     struct sigaction sigabrt = {
108         .sa_handler = SignalHandler,
109     };
110     sigaction(SIGABRT, &sigabrt, NULL);
111 
112     const int err = -1;
113     int fd = open("/proc/version", O_RDWR | O_CREAT, 0777);
114     TEST(fd != err);
115 
116     int status;
117     const int bufferSize = 2;
118     char buf[bufferSize];
119     size_t n = atoi("4") + SSIZE_MAX;
120     int pid = fork();
121     switch (pid) {
122         case -1:
123             t_error("fork failed: %d\n", __LINE__);
124             break;
125         case 0:
126             pread(fd, buf, n, 0);
127             exit(0);
128         default:
129             waitpid(pid, &status, WUNTRACED);
130             TEST(WIFEXITED(status) == 0);
131             TEST(WIFSTOPPED(status) == 1);
132             TEST(WSTOPSIG(status) == SIGSTOP);
133             kill(pid, SIGCONT);
134             break;
135     }
136     close(fd);
137     return;
138 }
139 
140 
141 /**
142  * @tc.name     : pread64
143  * @tc.desc     : Exception scenarios for pread64
144  * @tc.level    : Level 2
145  */
unistd_dynamic_chk_004(void)146 static void unistd_dynamic_chk_004(void)
147 {
148     struct sigaction sigabrt = {
149         .sa_handler = SignalHandler,
150     };
151     sigaction(SIGABRT, &sigabrt, NULL);
152 
153     const int err = -1;
154     int fd = open("/proc/version", O_RDWR | O_CREAT, 0777);
155     TEST(fd != err);
156     int status;
157 
158     const int bufferSize = 2;
159     char buf[bufferSize];
160     size_t n = atoi("4") + SSIZE_MAX;;
161     int pid = fork();
162     switch (pid) {
163         case -1:
164             t_error("fork failed: %d\n", __LINE__);
165             break;
166         case 0:
167             pread64(fd, buf, n, 0);
168             exit(0);
169         default:
170             waitpid(pid, &status, WUNTRACED);
171             TEST(WIFEXITED(status) == 0);
172             TEST(WIFSTOPPED(status) == 1);
173             TEST(WSTOPSIG(status) == SIGSTOP);
174             kill(pid, SIGCONT);
175             break;
176     }
177     close(fd);
178     return;
179 }
180 
181 /**
182  * @tc.name     : pread64
183  * @tc.desc     : Exception scenarios for pread64
184  * @tc.level    : Level 2
185  */
unistd_dynamic_chk_005(void)186 static void unistd_dynamic_chk_005(void)
187 {
188     struct sigaction sigabrt = {
189         .sa_handler = SignalHandler,
190     };
191     sigaction(SIGABRT, &sigabrt, NULL);
192 
193     const int err = -1;
194     int fd = open("/proc/version", O_RDWR | O_CREAT, 0777);
195     TEST(fd != err);
196     int status;
197 
198     const int bufferSize = 2;
199     char buf[bufferSize];
200     size_t n = atoi("4");
201     int pid = fork();
202     switch (pid) {
203         case -1:
204             t_error("fork failed: %d\n", __LINE__);
205             break;
206         case 0:
207             pread64(fd, buf, n, 0);
208             exit(0);
209         default:
210             waitpid(pid, &status, WUNTRACED);
211             TEST(WIFEXITED(status) == 0);
212             TEST(WIFSTOPPED(status) == 1);
213             TEST(WSTOPSIG(status) == SIGSTOP);
214             kill(pid, SIGCONT);
215             break;
216     }
217     close(fd);
218     return;
219 }
220 
221 /**
222  * @tc.name     : pwrite64
223  * @tc.desc     : Exception scenarios for pwrite64
224  * @tc.level    : Level 2
225  */
unistd_dynamic_chk_006(void)226 static void unistd_dynamic_chk_006(void)
227 {
228     char fileName[] = "./unistd_dynamic_chk_003.txt";
229     int fd = open(fileName, O_RDWR | O_CREAT, 0777);
230     TEST(fd != -1);
231 
232     struct sigaction sigabrt = {
233         .sa_handler = SignalHandler,
234     };
235     sigaction(SIGABRT, &sigabrt, NULL);
236 
237     char buf[] = "hello!";
238     size_t n = strlen(buf) + 2;
239     int status;
240     int pid = fork();
241     switch (pid) {
242         case -1:
243             t_error("fork failed: %d\n", __LINE__);
244             break;
245         case 0:
246             pwrite64(fd, buf, n, 0);
247             exit(0);
248         default:
249             waitpid(pid, &status, WUNTRACED);
250             TEST(WIFEXITED(status) == 0);
251             TEST(WIFSTOPPED(status) == 1);
252             TEST(WSTOPSIG(status) == SIGSTOP);
253             kill(pid, SIGCONT);
254             break;
255     }
256     close(fd);
257     return;
258 }
259 
260 /**
261  * @tc.name     : pwrite64
262  * @tc.desc     : Exception scenarios for pwrite64
263  * @tc.level    : Level 2
264  */
unistd_dynamic_chk_007(void)265 static void unistd_dynamic_chk_007(void)
266 {
267     char fileName[] = "./unistd_dynamic_chk_003.txt";
268     int fd = open(fileName, O_RDWR | O_CREAT, 0777);
269     TEST(fd != -1);
270 
271     struct sigaction sigabrt = {
272         .sa_handler = SignalHandler,
273     };
274     sigaction(SIGABRT, &sigabrt, NULL);
275 
276     char buf[] = "hello!";
277     size_t n = strlen(buf) + SSIZE_MAX;
278     int status;
279     int pid = fork();
280     switch (pid) {
281         case -1:
282             t_error("fork failed: %d\n", __LINE__);
283             break;
284         case 0:
285             pwrite64(fd, buf, n, 0);
286             exit(0);
287         default:
288             waitpid(pid, &status, WUNTRACED);
289             TEST(WIFEXITED(status) == 0);
290             TEST(WIFSTOPPED(status) == 1);
291             TEST(WSTOPSIG(status) == SIGSTOP);
292             kill(pid, SIGCONT);
293             break;
294     }
295     close(fd);
296     return;
297 }
298 
299 
300 /**
301  * @tc.name     : pwrite
302  * @tc.desc     : Exception scenarios for pwrite
303  * @tc.level    : Level 2
304  */
unistd_dynamic_chk_008(void)305 static void unistd_dynamic_chk_008(void)
306 {
307     const int err = -1;
308     char fileName[] = "./unistd_dynamic_chk_003.txt";
309     int fd = open(fileName, O_RDWR | O_CREAT, 0777);
310     TEST(fd != err);
311 
312     struct sigaction sigabrt = {
313         .sa_handler = SignalHandler,
314     };
315     sigaction(SIGABRT, &sigabrt, NULL);
316 
317     char buf[] = "hello!";
318     size_t n = strlen(buf) + 2;
319     int status;
320     int pid = fork();
321     switch (pid) {
322         case -1:
323             t_error("fork failed: %d\n", __LINE__);
324             break;
325         case 0:
326             pwrite(fd, buf, n, 0);
327             exit(0);
328         default:
329             waitpid(pid, &status, WUNTRACED);
330             TEST(WIFEXITED(status) == 0);
331             TEST(WIFSTOPPED(status) == 1);
332             TEST(WSTOPSIG(status) == SIGSTOP);
333             kill(pid, SIGCONT);
334             break;
335     }
336     close(fd);
337     return;
338 }
339 
340 /**
341  * @tc.name     : pwrite
342  * @tc.desc     : Exception scenarios for pwrite
343  * @tc.level    : Level 2
344  */
unistd_dynamic_chk_009(void)345 static void unistd_dynamic_chk_009(void)
346 {
347     const int err = -1;
348     char fileName[] = "./unistd_dynamic_chk_003.txt";
349     int fd = open(fileName, O_RDWR | O_CREAT, 0777);
350     TEST(fd != err);
351 
352     struct sigaction sigabrt = {
353         .sa_handler = SignalHandler,
354     };
355     sigaction(SIGABRT, &sigabrt, NULL);
356 
357     char buf[] = "hello!";
358     size_t n = strlen(buf) + SSIZE_MAX;
359     int status;
360     int pid = fork();
361     switch (pid) {
362         case -1:
363             t_error("fork failed: %d\n", __LINE__);
364             break;
365         case 0:
366             pwrite(fd, buf, n, 0);
367             exit(0);
368         default:
369             waitpid(pid, &status, WUNTRACED);
370             TEST(WIFEXITED(status) == 0);
371             TEST(WIFSTOPPED(status) == 1);
372             TEST(WSTOPSIG(status) == SIGSTOP);
373             kill(pid, SIGCONT);
374             break;
375     }
376     close(fd);
377     return;
378 }
379 
380 /**
381  * @tc.name     : read
382  * @tc.desc     : Exception scenarios for read
383  * @tc.level    : Level 2
384  */
unistd_dynamic_chk_010(void)385 static void unistd_dynamic_chk_010(void)
386 {
387     struct sigaction sigabrt = {
388         .sa_handler = SignalHandler,
389     };
390     sigaction(SIGABRT, &sigabrt, NULL);
391 
392     int fd = open("/proc/version", O_RDWR | O_CREAT, 0777);
393     int status;
394 
395     const int bufferSize = 2;
396     char buf[bufferSize];
397     size_t n = atoi("4");
398     int pid = fork();
399     switch (pid) {
400         case -1:
401             t_error("fork failed: %d\n", __LINE__);
402             break;
403         case 0:
404             read(fd, buf, n);
405             exit(0);
406         default:
407             waitpid(pid, &status, WUNTRACED);
408             TEST(WIFEXITED(status) == 0);
409             TEST(WIFSTOPPED(status) == 1);
410             TEST(WSTOPSIG(status) == SIGSTOP);
411             kill(pid, SIGCONT);
412             break;
413     }
414     close(fd);
415     return;
416 }
417 
418 /**
419  * @tc.name     : read
420  * @tc.desc     : Exception scenarios for read
421  * @tc.level    : Level 2
422  */
unistd_dynamic_chk_011(void)423 static void unistd_dynamic_chk_011(void)
424 {
425     struct sigaction sigabrt = {
426         .sa_handler = SignalHandler,
427     };
428     sigaction(SIGABRT, &sigabrt, NULL);
429 
430     int fd = open("/proc/version", O_RDWR | O_CREAT, 0777);
431     int status;
432 
433     const int bufferSize = 2;
434     char buf[bufferSize];
435     size_t n = atoi("4") + SSIZE_MAX;
436     int pid = fork();
437     switch (pid) {
438         case -1:
439             t_error("fork failed: %d\n", __LINE__);
440             break;
441         case 0:
442             read(fd, buf, n);
443             exit(0);
444         default:
445             waitpid(pid, &status, WUNTRACED);
446             TEST(WIFEXITED(status) == 0);
447             TEST(WIFSTOPPED(status) == 1);
448             TEST(WSTOPSIG(status) == SIGSTOP);
449             kill(pid, SIGCONT);
450             break;
451     }
452     close(fd);
453     return;
454 }
455 
456 /**
457  * @tc.name     : write
458  * @tc.desc     : Exception scenarios for write
459  * @tc.level    : Level 2
460  */
unistd_dynamic_chk_012(void)461 static void unistd_dynamic_chk_012(void)
462 {
463     struct sigaction sigabrt = {
464         .sa_handler = SignalHandler,
465     };
466     sigaction(SIGABRT, &sigabrt, NULL);
467 
468     const int err = -1;
469     char fileName[] = "./unistd_dynamic_chk_005.txt";
470     int fd = open(fileName, O_RDWR | O_CREAT, 0777);
471     TEST(fd != err);
472 
473     char buf[] = "hello!";
474     size_t n = strlen(buf) + 2;
475     int status;
476     int pid = fork();
477     switch (pid) {
478         case -1:
479             t_error("fork failed: %d\n", __LINE__);
480             break;
481         case 0:
482             write(fd, buf, n);
483             exit(0);
484         default:
485             waitpid(pid, &status, WUNTRACED);
486             TEST(WIFEXITED(status) == 0);
487             TEST(WIFSTOPPED(status) == 1);
488             TEST(WSTOPSIG(status) == SIGSTOP);
489             kill(pid, SIGCONT);
490             break;
491     }
492     close(fd);
493     return;
494 }
495 
496 /**
497  * @tc.name     : write
498  * @tc.desc     : Exception scenarios for write
499  * @tc.level    : Level 2
500  */
unistd_dynamic_chk_013(void)501 static void unistd_dynamic_chk_013(void)
502 {
503     struct sigaction sigabrt = {
504         .sa_handler = SignalHandler,
505     };
506     sigaction(SIGABRT, &sigabrt, NULL);
507 
508     const int err = -1;
509     char fileName[] = "./unistd_dynamic_chk_005.txt";
510     int fd = open(fileName, O_RDWR | O_CREAT, 0777);
511     TEST(fd != err);
512 
513     char buf[] = "hello!";
514     size_t n = strlen(buf) + SSIZE_MAX;
515     int status;
516     int pid = fork();
517     switch (pid) {
518         case -1:
519             t_error("fork failed: %d\n", __LINE__);
520             break;
521         case 0:
522             write(fd, buf, n);
523             exit(0);
524         default:
525             waitpid(pid, &status, WUNTRACED);
526             TEST(WIFEXITED(status) == 0);
527             TEST(WIFSTOPPED(status) == 1);
528             TEST(WSTOPSIG(status) == SIGSTOP);
529             kill(pid, SIGCONT);
530             break;
531     }
532     close(fd);
533     return;
534 }
535 
536 /**
537  * @tc.name     : readlink
538  * @tc.desc     : Exception scenarios for readlink
539  * @tc.level    : Level 2
540  */
unistd_dynamic_chk_014(void)541 static void unistd_dynamic_chk_014(void)
542 {
543     struct sigaction sigabrt = {
544         .sa_handler = SignalHandler,
545     };
546     sigaction(SIGABRT, &sigabrt, NULL);
547 
548     char path[] = "/dev/null";
549 
550     const int bufferSize = 1;
551     char buf[bufferSize];
552     size_t n = atoi("2");
553     int status;
554     int pid = fork();
555     switch (pid) {
556         case -1:
557             t_error("fork failed: %d\n", __LINE__);
558             break;
559         case 0:
560             readlink(path, buf, n);
561             exit(0);
562         default:
563             waitpid(pid, &status, WUNTRACED);
564             TEST(WIFEXITED(status) == 0);
565             TEST(WIFSTOPPED(status) == 1);
566             TEST(WSTOPSIG(status) == SIGSTOP);
567             kill(pid, SIGCONT);
568             break;
569     }
570     return;
571 }
572 
573 /**
574  * @tc.name     : readlink
575  * @tc.desc     : Exception scenarios for readlink
576  * @tc.level    : Level 2
577  */
unistd_dynamic_chk_015(void)578 static void unistd_dynamic_chk_015(void)
579 {
580     struct sigaction sigabrt = {
581         .sa_handler = SignalHandler,
582     };
583     sigaction(SIGABRT, &sigabrt, NULL);
584 
585     char path[] = "/dev/null";
586 
587     const int bufferSize = 1;
588     char buf[bufferSize];
589     size_t n = atoi("2") + SSIZE_MAX;
590     int status;
591     int pid = fork();
592     switch (pid) {
593         case -1:
594             t_error("fork failed: %d\n", __LINE__);
595             break;
596         case 0:
597             readlink(path, buf, n);
598             exit(0);
599         default:
600             waitpid(pid, &status, WUNTRACED);
601             TEST(WIFEXITED(status) == 0);
602             TEST(WIFSTOPPED(status) == 1);
603             TEST(WSTOPSIG(status) == SIGSTOP);
604             kill(pid, SIGCONT);
605             break;
606     }
607     return;
608 }
609 /**
610  * @tc.name     : readlinkat
611  * @tc.desc     : Exception scenarios for readlinkat
612  * @tc.level    : Level 2
613  */
unistd_dynamic_chk_016(void)614 static void unistd_dynamic_chk_016(void)
615 {
616     struct sigaction sigabrt = {
617         .sa_handler = SignalHandler,
618     };
619     sigaction(SIGABRT, &sigabrt, NULL);
620 
621     char path[] = "/dev/null";
622 
623     const int bufferSize = 1;
624     char buf[bufferSize];
625 
626     size_t n = atoi("2");
627     int status;
628     int pid = fork();
629     switch (pid) {
630         case -1:
631             t_error("fork failed: %d\n", __LINE__);
632             break;
633         case 0:
634             readlinkat(AT_FDCWD, path, buf, n);
635             exit(0);
636         default:
637             waitpid(pid, &status, WUNTRACED);
638             TEST(WIFEXITED(status) == 0);
639             TEST(WIFSTOPPED(status) == 1);
640             TEST(WSTOPSIG(status) == SIGSTOP);
641             kill(pid, SIGCONT);
642             break;
643     }
644     return;
645 }
646 
647 /**
648  * @tc.name     : readlinkat
649  * @tc.desc     : Exception scenarios for readlinkat
650  * @tc.level    : Level 2
651  */
unistd_dynamic_chk_017(void)652 static void unistd_dynamic_chk_017(void)
653 {
654     struct sigaction sigabrt = {
655         .sa_handler = SignalHandler,
656     };
657     sigaction(SIGABRT, &sigabrt, NULL);
658 
659     char path[] = "/dev/null";
660 
661     const int bufferSize = 1;
662     char buf[bufferSize];
663 
664     size_t n = atoi("1") + SSIZE_MAX;
665     int status;
666     int pid = fork();
667     switch (pid) {
668         case -1:
669             t_error("fork failed: %d\n", __LINE__);
670             break;
671         case 0:
672             readlinkat(AT_FDCWD, path, buf, n);
673             exit(0);
674         default:
675             waitpid(pid, &status, WUNTRACED);
676             TEST(WIFEXITED(status) == 0);
677             TEST(WIFSTOPPED(status) == 1);
678             TEST(WSTOPSIG(status) == SIGSTOP);
679             kill(pid, SIGCONT);
680             break;
681     }
682     return;
683 }
main(int argc,char * argv[])684 int main(int argc, char *argv[])
685 {
686     unistd_dynamic_chk_001();
687     unistd_dynamic_chk_002();
688     unistd_dynamic_chk_003();
689     unistd_dynamic_chk_004();
690     unistd_dynamic_chk_005();
691     unistd_dynamic_chk_006();
692     unistd_dynamic_chk_007();
693     unistd_dynamic_chk_008();
694     unistd_dynamic_chk_009();
695     unistd_dynamic_chk_010();
696     unistd_dynamic_chk_011();
697     unistd_dynamic_chk_012();
698     unistd_dynamic_chk_013();
699     unistd_dynamic_chk_014();
700     unistd_dynamic_chk_015();
701     unistd_dynamic_chk_016();
702     unistd_dynamic_chk_017();
703     return t_status;
704 }