2017-03-08 16:18:08 +00:00
|
|
|
/* Test that failing system calls do set errno to the correct value.
|
2017-03-13 13:20:05 +00:00
|
|
|
Linux sycalls version.
|
2017-03-08 16:18:08 +00:00
|
|
|
|
2021-01-02 19:32:25 +00:00
|
|
|
Copyright (C) 2017-2021 Free Software Foundation, Inc.
|
2017-03-08 16:18:08 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2017-03-08 16:18:08 +00:00
|
|
|
|
2017-11-02 12:55:51 +00:00
|
|
|
#include <array_length.h>
|
2017-03-08 16:18:08 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <mqueue.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <signal.h>
|
2017-11-02 12:55:51 +00:00
|
|
|
#include <stdbool.h>
|
2017-03-08 16:18:08 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <sys/eventfd.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/fsuid.h>
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <sys/quota.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/sendfile.h>
|
|
|
|
#include <sys/swap.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2021-05-06 16:56:25 +00:00
|
|
|
#include <libc-diag.h>
|
2017-03-08 16:18:08 +00:00
|
|
|
|
|
|
|
/* This is not an exhaustive test: only system calls that can be
|
|
|
|
persuaded to fail with a consistent error code and no side effects
|
|
|
|
are included. Usually these are failures due to invalid arguments,
|
|
|
|
with errno code EBADF or EINVAL. The order of argument checks is
|
|
|
|
unspecified, so we must take care to provide arguments that only
|
|
|
|
allow _one_ failure mode.
|
|
|
|
|
|
|
|
Note that all system calls that can fail with EFAULT are permitted
|
|
|
|
to deliver a SIGSEGV signal instead, so we avoid supplying invalid
|
|
|
|
pointers in general, and we do not attempt to test system calls
|
|
|
|
that can only fail with EFAULT (e.g. gettimeofday, gethostname).
|
|
|
|
|
|
|
|
Also note that root-only system calls (e.g. acct, reboot) may, when
|
|
|
|
the test is run as an unprivileged user, fail due to insufficient
|
|
|
|
privileges before bothering to do argument checks, so those are not
|
|
|
|
tested either.
|
|
|
|
|
|
|
|
Also, system calls that take enum or a set of flags as argument is
|
|
|
|
not tested if POSIX doesn't specify exact binary values for all
|
|
|
|
flags, and so any value passed to flags may become valid.
|
|
|
|
|
|
|
|
Some tests assume "/bin/sh" names a file that exists and is not a
|
|
|
|
directory. */
|
|
|
|
|
2017-11-02 12:55:51 +00:00
|
|
|
/* Evalutes to the arguments in a list initializer which can be used
|
|
|
|
as a single macro argument. */
|
|
|
|
#define LIST(...) { __VA_ARGS__ }
|
|
|
|
|
|
|
|
/* This macro is necessary to forward the output of LIST as a macro
|
|
|
|
argument. */
|
|
|
|
#define LIST_FORWARD(...) __VA_ARGS__
|
|
|
|
|
|
|
|
/* Return true if CODE is contained in the array [CODES, CODES +
|
|
|
|
COUNT]. */
|
|
|
|
static bool
|
|
|
|
check_error_in_list (int code, int *codes, size_t count)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < count; ++i)
|
|
|
|
if (codes[i] == code)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define test_wrp_rv(rtype, prtype, experr_list, syscall, ...) \
|
2017-03-08 16:18:08 +00:00
|
|
|
(__extension__ ({ \
|
|
|
|
errno = 0xdead; \
|
2017-11-02 12:55:51 +00:00
|
|
|
int experr[] = experr_list; \
|
2017-03-08 16:18:08 +00:00
|
|
|
rtype ret = syscall (__VA_ARGS__); \
|
|
|
|
int err = errno; \
|
|
|
|
int fail; \
|
2017-11-02 12:55:51 +00:00
|
|
|
if ((ret == (rtype) -1) \
|
|
|
|
&& check_error_in_list (err, experr, array_length (experr))) \
|
2017-03-08 16:18:08 +00:00
|
|
|
fail = 0; \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
fail = 1; \
|
|
|
|
if (ret != (rtype) -1) \
|
|
|
|
printf ("FAIL: " #syscall ": didn't fail as expected" \
|
2017-03-13 13:20:05 +00:00
|
|
|
" (return "prtype")\n", ret); \
|
2017-03-08 16:18:08 +00:00
|
|
|
else if (err == 0xdead) \
|
2017-03-13 13:20:05 +00:00
|
|
|
puts ("FAIL: " #syscall ": didn't update errno"); \
|
2017-11-02 12:55:51 +00:00
|
|
|
else \
|
2017-03-08 16:18:08 +00:00
|
|
|
printf ("FAIL: " #syscall \
|
2017-11-02 12:55:51 +00:00
|
|
|
": errno is: %d (%s) expected one of %s\n", \
|
|
|
|
err, strerror (err), #experr_list); \
|
2017-03-08 16:18:08 +00:00
|
|
|
} \
|
|
|
|
fail; \
|
|
|
|
}))
|
|
|
|
|
|
|
|
#define test_wrp(experr, syscall, ...) \
|
2017-11-02 12:55:51 +00:00
|
|
|
test_wrp_rv(int, "%d", LIST (experr), syscall, __VA_ARGS__)
|
2017-03-08 16:18:08 +00:00
|
|
|
|
2017-11-02 12:55:51 +00:00
|
|
|
#define test_wrp2(experr, syscall, ...) \
|
|
|
|
test_wrp_rv(int, "%d", LIST_FORWARD (experr), syscall, __VA_ARGS__)
|
2017-03-13 13:20:05 +00:00
|
|
|
|
2020-03-11 18:21:59 +00:00
|
|
|
static int
|
|
|
|
invalid_sigprocmask_how (void)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
const int how[] = { SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK };
|
|
|
|
for (int i = 0; i < array_length (how); i++)
|
|
|
|
if (how[i] == n)
|
|
|
|
n++;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2017-03-08 16:18:08 +00:00
|
|
|
static int
|
|
|
|
do_test (void)
|
|
|
|
{
|
|
|
|
fd_set rs, ws, es;
|
|
|
|
int status;
|
|
|
|
off_t off;
|
|
|
|
stack_t ss;
|
|
|
|
struct dqblk dqblk;
|
|
|
|
struct epoll_event epoll_event;
|
|
|
|
struct pollfd pollfd;
|
|
|
|
struct sched_param sch_param;
|
|
|
|
struct timespec ts;
|
|
|
|
struct timeval tv;
|
2020-03-11 18:21:59 +00:00
|
|
|
sigset_t sigs;
|
2017-03-08 16:18:08 +00:00
|
|
|
unsigned char vec[16];
|
|
|
|
ss.ss_flags = ~SS_DISABLE;
|
|
|
|
ts.tv_sec = -1;
|
|
|
|
|
2020-03-11 18:21:59 +00:00
|
|
|
sigemptyset (&sigs);
|
|
|
|
|
2017-03-08 16:18:08 +00:00
|
|
|
int fails = 0;
|
|
|
|
fails |= test_wrp (EINVAL, epoll_create, -1);
|
|
|
|
fails |= test_wrp (EINVAL, epoll_create1, EPOLL_CLOEXEC + 1);
|
|
|
|
fails |= test_wrp (EBADF, epoll_ctl, -1, EPOLL_CTL_ADD, 0, &epoll_event);
|
|
|
|
fails |= test_wrp (EBADF, epoll_wait, -1, &epoll_event, 1, 1);
|
|
|
|
fails |= test_wrp (EBADF, fdatasync, -1);
|
|
|
|
fails |= test_wrp (EBADF, flock, -1, LOCK_SH);
|
|
|
|
fails |= test_wrp (ESRCH, getpgid, -1);
|
2017-03-28 19:44:19 +00:00
|
|
|
/* Linux v3.8 (676a0675c) removed the test to check at least one valid
|
|
|
|
bit in flags (to return EINVAL). It was later added back in v3.9
|
|
|
|
(04df32fa1). */
|
2017-11-02 12:55:51 +00:00
|
|
|
fails |= test_wrp2 (LIST (EINVAL, EBADF), inotify_add_watch, -1, "/", 0);
|
2017-03-08 16:18:08 +00:00
|
|
|
fails |= test_wrp (EINVAL, mincore, (void *) -1, 0, vec);
|
2017-03-13 13:20:05 +00:00
|
|
|
/* mlock fails if the result of the addition addr+len was less than addr
|
|
|
|
(which indicates final address overflow), however on 32 bits binaries
|
|
|
|
running on 64 bits kernels, internal syscall address check won't result
|
|
|
|
in an invalid address and thus syscalls fails later in vma
|
|
|
|
allocation. */
|
2017-11-02 12:55:51 +00:00
|
|
|
fails |= test_wrp2 (LIST (EINVAL, ENOMEM), mlock, (void *) -1, 1);
|
2017-03-08 16:18:08 +00:00
|
|
|
fails |= test_wrp (EINVAL, nanosleep, &ts, &ts);
|
2021-05-06 16:56:25 +00:00
|
|
|
|
2021-05-06 19:38:44 +00:00
|
|
|
DIAG_PUSH_NEEDS_COMMENT;
|
2021-05-06 16:56:25 +00:00
|
|
|
|
|
|
|
#if __GNUC_PREREQ (9, 0)
|
|
|
|
/* Suppress valid GCC warning:
|
|
|
|
'poll' specified size 18446744073709551608 exceeds maximum object size
|
|
|
|
*/
|
|
|
|
DIAG_IGNORE_NEEDS_COMMENT (9, "-Wstringop-overflow=");
|
|
|
|
#endif
|
2017-03-08 16:18:08 +00:00
|
|
|
fails |= test_wrp (EINVAL, poll, &pollfd, -1, 0);
|
2021-05-06 16:56:25 +00:00
|
|
|
DIAG_POP_NEEDS_COMMENT;
|
|
|
|
|
2017-11-02 12:55:51 +00:00
|
|
|
/* quotactl returns ENOSYS for kernels not configured with
|
|
|
|
CONFIG_QUOTA, and may return EPERM if called within certain types
|
2019-12-05 16:29:42 +00:00
|
|
|
of containers. Linux 5.4 added additional argument validation
|
|
|
|
and can return EINVAL. */
|
|
|
|
fails |= test_wrp2 (LIST (ENODEV, ENOSYS, EPERM, EINVAL),
|
2017-11-02 12:55:51 +00:00
|
|
|
quotactl, Q_GETINFO, NULL, -1, (caddr_t) &dqblk);
|
2017-03-08 16:18:08 +00:00
|
|
|
fails |= test_wrp (EINVAL, sched_getparam, -1, &sch_param);
|
|
|
|
fails |= test_wrp (EINVAL, sched_getscheduler, -1);
|
|
|
|
fails |= test_wrp (EINVAL, sched_get_priority_max, -1);
|
|
|
|
fails |= test_wrp (EINVAL, sched_get_priority_min, -1);
|
|
|
|
fails |= test_wrp (EINVAL, sched_rr_get_interval, -1, &ts);
|
|
|
|
fails |= test_wrp (EINVAL, sched_setparam, -1, &sch_param);
|
|
|
|
fails |= test_wrp (EINVAL, sched_setscheduler, -1, 0, &sch_param);
|
|
|
|
fails |= test_wrp (EINVAL, select, -1, &rs, &ws, &es, &tv);
|
|
|
|
fails |= test_wrp (EBADF, sendfile, -1, -1, &off, 0);
|
|
|
|
fails |= test_wrp (EINVAL, sigaltstack, &ss, NULL);
|
|
|
|
fails |= test_wrp (ECHILD, wait4, -1, &status, 0, NULL);
|
2020-03-11 18:21:59 +00:00
|
|
|
/* Austin Group issue #1132 states EINVAL should be returned for invalid
|
|
|
|
how argument iff the new set mask is non-null. And Linux follows the
|
|
|
|
standard on this regard. */
|
|
|
|
fails |= test_wrp (EINVAL, sigprocmask, invalid_sigprocmask_how (), &sigs,
|
|
|
|
NULL);
|
2017-03-08 16:18:08 +00:00
|
|
|
|
|
|
|
return fails;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "support/test-driver.c"
|