1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2020 Linaro Ltd.
4 *
5 * Failure tests.
6 */
7
8 #include <unistd.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <sys/time.h>
12 #include <sys/types.h>
13 #include <sys/wait.h>
14 #include <fcntl.h>
15 #include "select_var.h"
16
17 static fd_set readfds_reg, writefds_reg, fds_closed;
18 static fd_set *preadfds_reg = &readfds_reg, *pwritefds_reg = &writefds_reg;
19 static fd_set *pfds_closed = &fds_closed, *nullfds = NULL, *faulty_fds;
20 static int fd_closed, fd[2];
21 static int negative_nfds = -1, maxfds;
22 static struct timeval timeout = {.tv_sec = 0, .tv_usec = 100000};
23
24 static struct timeval *valid_to = &timeout, *invalid_to;
25
26 static struct tcases {
27 char *name;
28 int *nfds;
29 fd_set **readfds;
30 fd_set **writefds;
31 fd_set **exceptfds;
32 struct timeval **timeout;
33 int exp_errno;
34 } tests[] = {
35 { "Negative nfds", &negative_nfds, &preadfds_reg, &pwritefds_reg, &nullfds, &valid_to, EINVAL },
36 { "Invalid readfds", &maxfds, &pfds_closed, &pwritefds_reg, &nullfds, &valid_to, EBADF },
37 { "Invalid writefds", &maxfds, &preadfds_reg, &pfds_closed, &nullfds, &valid_to, EBADF },
38 { "Invalid exceptfds", &maxfds, &preadfds_reg, &pwritefds_reg, &pfds_closed, &valid_to, EBADF },
39 { "Faulty readfds", &maxfds, &faulty_fds, &pwritefds_reg, &nullfds, &valid_to, EFAULT },
40 { "Faulty writefds", &maxfds, &preadfds_reg, &faulty_fds, &nullfds, &valid_to, EFAULT },
41 { "Faulty exceptfds", &maxfds, &preadfds_reg, &pwritefds_reg, &faulty_fds, &valid_to, EFAULT },
42 { "Faulty timeout", &maxfds, &preadfds_reg, &pwritefds_reg, &nullfds, &invalid_to, EFAULT },
43 };
44
verify_select(unsigned int n)45 static void verify_select(unsigned int n)
46 {
47 struct tcases *tc = &tests[n];
48
49 TEST(do_select_faulty_to(*tc->nfds, *tc->readfds, *tc->writefds,
50 *tc->exceptfds, *tc->timeout,
51 tc->timeout == &invalid_to));
52
53 if (TST_RET != -1) {
54 tst_res(TFAIL, "%s: select() passed unexpectedly with %ld",
55 tc->name, TST_RET);
56 return;
57 }
58
59 if (tc->exp_errno != TST_ERR) {
60 tst_res(TFAIL | TTERRNO, "%s: select()() should fail with %s",
61 tc->name, tst_strerrno(tc->exp_errno));
62 return;
63 }
64
65 tst_res(TPASS | TTERRNO, "%s: select() failed as expected", tc->name);
66
67 exit(0);
68 }
69
run(unsigned int n)70 static void run(unsigned int n)
71 {
72 int pid, status;
73
74 pid = SAFE_FORK();
75 if (!pid)
76 verify_select(n);
77
78 SAFE_WAITPID(pid, &status, 0);
79
80 if (WIFEXITED(status))
81 return;
82
83 if (tst_variant == GLIBC_SELECT_VARIANT &&
84 tests[n].timeout == &invalid_to &&
85 WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
86 tst_res(TPASS, "%s: select() killed by signal", tests[n].name);
87 return;
88 }
89
90 tst_res(TFAIL, "Child %s", tst_strstatus(status));
91 }
92
setup(void)93 static void setup(void)
94 {
95 void *faulty_address;
96
97 select_info();
98
99 /* Regular file */
100 fd_closed = SAFE_OPEN("tmpfile1", O_CREAT | O_RDWR, 0777);
101 FD_ZERO(&fds_closed);
102 FD_SET(fd_closed, &fds_closed);
103
104 SAFE_PIPE(fd);
105 FD_ZERO(&readfds_reg);
106 FD_ZERO(&writefds_reg);
107 FD_SET(fd[0], &readfds_reg);
108 FD_SET(fd[1], &writefds_reg);
109
110 SAFE_CLOSE(fd_closed);
111
112 maxfds = fd[1] + 1;
113 faulty_address = tst_get_bad_addr(NULL);
114 invalid_to = faulty_address;
115 faulty_fds = faulty_address;
116 }
117
118 static struct tst_test test = {
119 .test = run,
120 .tcnt = ARRAY_SIZE(tests),
121 .test_variants = TEST_VARIANTS,
122 .setup = setup,
123 .needs_tmpdir = 1,
124 .forks_child = 1,
125 };
126