1 /* Test that failing system calls do set errno to the correct value.
4 Copyright (C) 2017-2024 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <https://www.gnu.org/licenses/>. */
21 #include <array_length.h>
32 #include <sys/epoll.h>
33 #include <sys/eventfd.h>
35 #include <sys/fsuid.h>
36 #include <sys/inotify.h>
39 #include <sys/quota.h>
40 #include <sys/resource.h>
41 #include <sys/select.h>
42 #include <sys/sendfile.h>
45 #include <sys/types.h>
47 #include <libc-diag.h>
49 /* This is not an exhaustive test: only system calls that can be
50 persuaded to fail with a consistent error code and no side effects
51 are included. Usually these are failures due to invalid arguments,
52 with errno code EBADF or EINVAL. The order of argument checks is
53 unspecified, so we must take care to provide arguments that only
54 allow _one_ failure mode.
56 Note that all system calls that can fail with EFAULT are permitted
57 to deliver a SIGSEGV signal instead, so we avoid supplying invalid
58 pointers in general, and we do not attempt to test system calls
59 that can only fail with EFAULT (e.g. gettimeofday, gethostname).
61 Also note that root-only system calls (e.g. acct, reboot) may, when
62 the test is run as an unprivileged user, fail due to insufficient
63 privileges before bothering to do argument checks, so those are not
66 Also, system calls that take enum or a set of flags as argument is
67 not tested if POSIX doesn't specify exact binary values for all
68 flags, and so any value passed to flags may become valid.
70 Some tests assume "/bin/sh" names a file that exists and is not a
73 /* Evaluates to the arguments in a list initializer which can be used
74 as a single macro argument. */
75 #define LIST(...) { __VA_ARGS__ }
77 /* This macro is necessary to forward the output of LIST as a macro
79 #define LIST_FORWARD(...) __VA_ARGS__
81 /* Return true if CODE is contained in the array [CODES, CODES +
84 check_error_in_list (int code
, int *codes
, size_t count
)
86 for (size_t i
= 0; i
< count
; ++i
)
92 #define test_wrp_rv(rtype, prtype, experr_list, syscall, ...) \
95 int experr[] = experr_list; \
96 rtype ret = syscall (__VA_ARGS__); \
99 if ((ret == (rtype) -1) \
100 && check_error_in_list (err, experr, array_length (experr))) \
105 if (ret != (rtype) -1) \
106 printf ("FAIL: " #syscall ": didn't fail as expected" \
107 " (return "prtype")\n", ret); \
108 else if (err == 0xdead) \
109 puts ("FAIL: " #syscall ": didn't update errno"); \
111 printf ("FAIL: " #syscall \
112 ": errno is: %d (%s) expected one of %s\n", \
113 err, strerror (err), #experr_list); \
118 #define test_wrp(experr, syscall, ...) \
119 test_wrp_rv(int, "%d", LIST (experr), syscall, __VA_ARGS__)
121 #define test_wrp2(experr, syscall, ...) \
122 test_wrp_rv(int, "%d", LIST_FORWARD (experr), syscall, __VA_ARGS__)
125 invalid_sigprocmask_how (void)
128 const int how
[] = { SIG_BLOCK
, SIG_UNBLOCK
, SIG_SETMASK
};
129 for (int i
= 0; i
< array_length (how
); i
++)
143 struct epoll_event epoll_event
;
144 struct pollfd pollfd
;
145 struct sched_param sch_param
;
149 unsigned char vec
[16];
150 ss
.ss_flags
= ~SS_DISABLE
;
156 fails
|= test_wrp (EINVAL
, epoll_create
, -1);
157 fails
|= test_wrp (EINVAL
, epoll_create1
, EPOLL_CLOEXEC
+ 1);
158 fails
|= test_wrp (EBADF
, epoll_ctl
, -1, EPOLL_CTL_ADD
, 0, &epoll_event
);
159 fails
|= test_wrp (EBADF
, epoll_wait
, -1, &epoll_event
, 1, 1);
160 fails
|= test_wrp (EBADF
, fdatasync
, -1);
161 fails
|= test_wrp (EBADF
, flock
, -1, LOCK_SH
);
162 fails
|= test_wrp (ESRCH
, getpgid
, -1);
163 /* Linux v3.8 (676a0675c) removed the test to check at least one valid
164 bit in flags (to return EINVAL). It was later added back in v3.9
166 fails
|= test_wrp2 (LIST (EINVAL
, EBADF
), inotify_add_watch
, -1, "/", 0);
167 fails
|= test_wrp (EINVAL
, mincore
, (void *) -1, 0, vec
);
168 /* mlock fails if the result of the addition addr+len was less than addr
169 (which indicates final address overflow), however on 32 bits binaries
170 running on 64 bits kernels, internal syscall address check won't result
171 in an invalid address and thus syscalls fails later in vma
173 fails
|= test_wrp2 (LIST (EINVAL
, ENOMEM
), mlock
, (void *) -1, 1);
174 fails
|= test_wrp (EINVAL
, nanosleep
, &ts
, &ts
);
176 DIAG_PUSH_NEEDS_COMMENT
;
178 #if __GNUC_PREREQ (9, 0)
179 /* Suppress valid GCC warning:
180 'poll' specified size 18446744073709551608 exceeds maximum object size
182 DIAG_IGNORE_NEEDS_COMMENT (9, "-Wstringop-overflow=");
184 fails
|= test_wrp (EINVAL
, poll
, &pollfd
, -1, 0);
185 DIAG_POP_NEEDS_COMMENT
;
187 /* quotactl returns ENOSYS for kernels not configured with
188 CONFIG_QUOTA, and may return EPERM if called within certain types
189 of containers. Linux 5.4 added additional argument validation
190 and can return EINVAL. */
191 fails
|= test_wrp2 (LIST (ENODEV
, ENOSYS
, EPERM
, EINVAL
),
192 quotactl
, Q_GETINFO
, NULL
, -1, (caddr_t
) &dqblk
);
193 fails
|= test_wrp (EINVAL
, sched_getparam
, -1, &sch_param
);
194 fails
|= test_wrp (EINVAL
, sched_getscheduler
, -1);
195 fails
|= test_wrp (EINVAL
, sched_get_priority_max
, -1);
196 fails
|= test_wrp (EINVAL
, sched_get_priority_min
, -1);
197 fails
|= test_wrp (EINVAL
, sched_rr_get_interval
, -1, &ts
);
198 fails
|= test_wrp (EINVAL
, sched_setparam
, -1, &sch_param
);
199 fails
|= test_wrp (EINVAL
, sched_setscheduler
, -1, 0, &sch_param
);
200 fails
|= test_wrp (EINVAL
, select
, -1, &rs
, &ws
, &es
, &tv
);
201 fails
|= test_wrp (EBADF
, sendfile
, -1, -1, &off
, 0);
202 fails
|= test_wrp (EINVAL
, sigaltstack
, &ss
, NULL
);
203 fails
|= test_wrp (ECHILD
, wait4
, -1, &status
, 0, NULL
);
204 /* Austin Group issue #1132 states EINVAL should be returned for invalid
205 how argument iff the new set mask is non-null. And Linux follows the
206 standard on this regard. */
207 fails
|= test_wrp (EINVAL
, sigprocmask
, invalid_sigprocmask_how (), &sigs
,
213 #include "support/test-driver.c"