Skip to content

Commit

Permalink
test/openat2: test bad address handling
Browse files Browse the repository at this point in the history
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
axboe committed Aug 30, 2024
1 parent 4ee7438 commit e37b7bb
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions test/openat2.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,32 @@
#include "liburing.h"

static int test_openat2(struct io_uring *ring, const char *path, int dfd,
bool direct, int fixed_index)
bool direct, int fixed_index, int bad_how)
{
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
struct open_how how;
struct open_how __how, *how;
int ret;

if (bad_how)
how = (struct open_how *) (uintptr_t) 0x1234;
else
how = &__how;

sqe = io_uring_get_sqe(ring);
if (!sqe) {
fprintf(stderr, "get sqe failed\n");
return -1;
}
memset(&how, 0, sizeof(how));
how.flags = O_RDWR;
if (!bad_how) {
memset(how, 0, sizeof(*how));
how->flags = O_RDWR;
}

if (!direct)
io_uring_prep_openat2(sqe, dfd, path, &how);
io_uring_prep_openat2(sqe, dfd, path, how);
else
io_uring_prep_openat2_direct(sqe, dfd, path, &how, fixed_index);
io_uring_prep_openat2_direct(sqe, dfd, path, how, fixed_index);

ret = io_uring_submit(ring);
if (ret <= 0) {
Expand Down Expand Up @@ -78,7 +85,7 @@ static int test_open_fixed(const char *path, int dfd)
return -1;
}

ret = test_openat2(&ring, path, dfd, true, 0);
ret = test_openat2(&ring, path, dfd, true, 0, 0);
if (ret == -EINVAL) {
printf("fixed open isn't supported\n");
return 1;
Expand Down Expand Up @@ -135,7 +142,7 @@ static int test_open_fixed_fail(const char *path, int dfd)
return -1;
}

ret = test_openat2(&ring, path, dfd, true, 0);
ret = test_openat2(&ring, path, dfd, true, 0, 0);
if (ret != -ENXIO) {
fprintf(stderr, "install into not existing table, %i\n", ret);
return 1;
Expand All @@ -149,19 +156,19 @@ static int test_open_fixed_fail(const char *path, int dfd)
return -1;
}

ret = test_openat2(&ring, path, dfd, true, 1);
ret = test_openat2(&ring, path, dfd, true, 1, 0);
if (ret != -EINVAL) {
fprintf(stderr, "install out of bounds, %i\n", ret);
return -1;
}

ret = test_openat2(&ring, path, dfd, true, (1u << 16));
ret = test_openat2(&ring, path, dfd, true, (1u << 16), 0);
if (ret != -EINVAL) {
fprintf(stderr, "install out of bounds or u16 overflow, %i\n", ret);
return -1;
}

ret = test_openat2(&ring, path, dfd, true, (1u << 16) + 1);
ret = test_openat2(&ring, path, dfd, true, (1u << 16) + 1, 0);
if (ret != -EINVAL) {
fprintf(stderr, "install out of bounds or u16 overflow, %i\n", ret);
return -1;
Expand Down Expand Up @@ -196,7 +203,7 @@ static int test_direct_reinstall(const char *path, int dfd)
}

/* reinstall into the second slot */
ret = test_openat2(&ring, path, dfd, true, 1);
ret = test_openat2(&ring, path, dfd, true, 1, 0);
if (ret != 0) {
fprintf(stderr, "reinstall failed, %i\n", ret);
return -1;
Expand Down Expand Up @@ -264,7 +271,7 @@ int main(int argc, char *argv[])
if (do_unlink)
t_create_file(path_rel, 4096);

ret = test_openat2(&ring, path, -1, false, 0);
ret = test_openat2(&ring, path, -1, false, 0, 0);
if (ret < 0) {
if (ret == -EINVAL) {
fprintf(stdout, "openat2 not supported, skipping\n");
Expand All @@ -274,7 +281,7 @@ int main(int argc, char *argv[])
goto err;
}

ret = test_openat2(&ring, path_rel, AT_FDCWD, false, 0);
ret = test_openat2(&ring, path_rel, AT_FDCWD, false, 0, 0);
if (ret < 0) {
fprintf(stderr, "test_openat2 relative failed: %d\n", ret);
goto err;
Expand All @@ -299,6 +306,18 @@ int main(int argc, char *argv[])
goto err;
}

ret = test_openat2(&ring, (const char *) (uintptr_t) 0x1234, AT_FDCWD, false, 0, 0);
if (ret != -EFAULT) {
fprintf(stderr, "test_openat2 bad address failed: %d\n", ret);
goto err;
}

ret = test_openat2(&ring, path_rel, AT_FDCWD, false, 0, 1);
if (ret != -EFAULT) {
fprintf(stderr, "test_openat2 bad how failed: %d\n", ret);
goto err;
}

done:
unlink(path);
if (do_unlink)
Expand Down

0 comments on commit e37b7bb

Please sign in to comment.