Skip to content

Commit

Permalink
syscalls: select: Add failure tests
Browse files Browse the repository at this point in the history
This adds a variety of failure tests to select() syscalls. Another
helper is added in select_var.h to make sure we don't access a bad
address while reading/writing the timeout value.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
  • Loading branch information
vireshk authored and metan-ucw committed Oct 21, 2020
1 parent 62e322f commit fb5ddb9
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 15 deletions.
1 change: 1 addition & 0 deletions runtest/syscalls
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,7 @@ sched_getattr02 sched_getattr02

select01 select01
select02 select02
select03 select03

semctl01 semctl01
semctl02 semctl02
Expand Down
1 change: 1 addition & 0 deletions testcases/kernel/syscalls/select/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/select01
/select02
/select03
95 changes: 95 additions & 0 deletions testcases/kernel/syscalls/select/select03.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2020 Linaro Ltd.
*
* Failure tests.
*/

#include <unistd.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include "select_var.h"

static fd_set readfds_reg, writefds_reg, fds_closed;
static fd_set *preadfds_reg = &readfds_reg, *pwritefds_reg = &writefds_reg;
static fd_set *pfds_closed = &fds_closed, *nullfds = NULL, *faulty_fds;
static int fd_closed, fd[2];
static struct timeval timeout = {.tv_sec = 0, .tv_usec = 100000};

static struct timeval *valid_to = &timeout, *invalid_to;

static struct tcases {
char *name;
int nfds;
fd_set **readfds;
fd_set **writefds;
fd_set **exceptfds;
struct timeval **timeout;
int exp_errno;
} tests[] = {
{ "Negative nfds", -1, &preadfds_reg, &pwritefds_reg, &nullfds, &valid_to, EINVAL },
{ "Invalid readfds", 6, &pfds_closed, &pwritefds_reg, &nullfds, &valid_to, EBADF },
{ "Invalid writefds", 6, &preadfds_reg, &pfds_closed, &nullfds, &valid_to, EBADF },
{ "Invalid exceptfds", 6, &preadfds_reg, &pwritefds_reg, &pfds_closed, &valid_to, EBADF },
{ "Faulty readfds", 6, &faulty_fds, &pwritefds_reg, &nullfds, &valid_to, EFAULT },
{ "Faulty writefds", 6, &preadfds_reg, &faulty_fds, &nullfds, &valid_to, EFAULT },
{ "Faulty exceptfds", 6, &preadfds_reg, &pwritefds_reg, &faulty_fds, &valid_to, EFAULT },
{ "Faulty timeout", 6, &preadfds_reg, &pwritefds_reg, &nullfds, &invalid_to, EFAULT },
};

static void run(unsigned int n)
{
struct tcases *tc = &tests[n];

TEST(do_select_faulty_to(tc->nfds, *tc->readfds, *tc->writefds,
*tc->exceptfds, *tc->timeout,
tc->timeout == &invalid_to));

if (TST_RET != -1) {
tst_res(TFAIL, "%s: select() passed unexpectedly with %ld",
tc->name, TST_RET);
return;
}

if (tc->exp_errno != TST_ERR) {
tst_res(TFAIL | TTERRNO, "%s: select()() should fail with %s",
tc->name, tst_strerrno(tc->exp_errno));
return;
}

tst_res(TPASS | TTERRNO, "%s: select() failed as expected", tc->name);
}

static void setup(void)
{
void *faulty_address;

select_info();

/* Regular file */
fd_closed = SAFE_OPEN("tmpfile1", O_CREAT | O_RDWR, 0777);
FD_ZERO(&fds_closed);
FD_SET(fd_closed, &fds_closed);

SAFE_PIPE(fd);
FD_ZERO(&readfds_reg);
FD_ZERO(&writefds_reg);
FD_SET(fd[0], &readfds_reg);
FD_SET(fd[1], &writefds_reg);

SAFE_CLOSE(fd_closed);

faulty_address = tst_get_bad_addr(NULL);
invalid_to = faulty_address;
faulty_fds = faulty_address;
}

static struct tst_test test = {
.test = run,
.tcnt = ARRAY_SIZE(tests),
.test_variants = TEST_VARIANTS,
.setup = setup,
.needs_tmpdir = 1,
};
55 changes: 40 additions & 15 deletions testcases/kernel/syscalls/select/select_var.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ struct compat_sel_arg_struct {
long _tvp;
};

static int do_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
static int do_select_faulty_to(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout, int faulty_to)
{
switch (tst_variant) {
case 0:
Expand All @@ -39,25 +40,43 @@ static int do_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *except
}
case 2: {
int ret;
struct __kernel_old_timespec ts = {
.tv_sec = timeout->tv_sec,
.tv_nsec = timeout->tv_usec * 1000,
};
ret = tst_syscall(__NR_pselect6, nfds, readfds, writefds, exceptfds, &ts, NULL);
timeout->tv_sec = ts.tv_sec;
timeout->tv_usec = ts.tv_nsec / 1000;
struct __kernel_old_timespec _ts;
void *ts;

if (faulty_to) {
ts = timeout;
} else {
ts = &_ts;
_ts.tv_sec = timeout->tv_sec;
_ts.tv_nsec = timeout->tv_usec * 1000;
}

ret = tst_syscall(__NR_pselect6, nfds, readfds, writefds, exceptfds, ts, NULL);
if (!faulty_to) {
timeout->tv_sec = _ts.tv_sec;
timeout->tv_usec = _ts.tv_nsec / 1000;
}
return ret;
}
case 3: {
int ret = 0;
#if (__NR_pselect6_time64 != __LTP__NR_INVALID_SYSCALL)
struct __kernel_timespec ts = {
.tv_sec = timeout->tv_sec,
.tv_nsec = timeout->tv_usec * 1000,
};
ret = tst_syscall(__NR_pselect6_time64, nfds, readfds, writefds, exceptfds, &ts, NULL);
timeout->tv_sec = ts.tv_sec;
timeout->tv_usec = ts.tv_nsec / 1000;
struct __kernel_timespec _ts;
void *ts;

if (faulty_to) {
ts = timeout;
} else {
ts = &_ts;
_ts.tv_sec = timeout->tv_sec;
_ts.tv_nsec = timeout->tv_usec * 1000;
}

ret = tst_syscall(__NR_pselect6_time64, nfds, readfds, writefds, exceptfds, ts, NULL);
if (!faulty_to) {
timeout->tv_sec = _ts.tv_sec;
timeout->tv_usec = _ts.tv_nsec / 1000;
}
#else
tst_brk(TCONF, "__NR_pselect6 time64 variant not supported");
#endif
Expand All @@ -75,6 +94,12 @@ static int do_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *except
return -1;
}

static inline int do_select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout)
{
return do_select_faulty_to(nfds, readfds, writefds, exceptfds, timeout, 0);
}

static void select_info(void)
{
switch (tst_variant) {
Expand Down

0 comments on commit fb5ddb9

Please sign in to comment.