123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634 |
- #include "../config-host.h"
- /* SPDX-License-Identifier: MIT */
- /*
- * Description: test restrictions
- *
- */
- #include <errno.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <poll.h>
- #include <sys/eventfd.h>
- #include "liburing.h"
- enum {
- TEST_OK,
- TEST_SKIPPED,
- TEST_FAILED
- };
- static int test_restrictions_sqe_op(void)
- {
- struct io_uring_restriction res[2];
- struct io_uring_sqe *sqe;
- struct io_uring_cqe *cqe;
- struct io_uring ring;
- int ret, pipe1[2];
- uint64_t ptr;
- struct iovec vec = {
- .iov_base = &ptr,
- .iov_len = sizeof(ptr)
- };
- if (pipe(pipe1) != 0) {
- perror("pipe");
- return TEST_FAILED;
- }
- ret = io_uring_queue_init(8, &ring, IORING_SETUP_R_DISABLED);
- if (ret) {
- if (ret == -EINVAL)
- return TEST_SKIPPED;
- fprintf(stderr, "ring setup failed: %d\n", ret);
- return TEST_FAILED;
- }
- res[0].opcode = IORING_RESTRICTION_SQE_OP;
- res[0].sqe_op = IORING_OP_WRITEV;
- res[1].opcode = IORING_RESTRICTION_SQE_OP;
- res[1].sqe_op = IORING_OP_WRITE;
- ret = io_uring_register_restrictions(&ring, res, 2);
- if (ret) {
- if (ret == -EINVAL)
- return TEST_SKIPPED;
- fprintf(stderr, "failed to register restrictions: %d\n", ret);
- return TEST_FAILED;
- }
- ret = io_uring_enable_rings(&ring);
- if (ret) {
- fprintf(stderr, "ring enabling failed: %d\n", ret);
- return TEST_FAILED;
- }
- sqe = io_uring_get_sqe(&ring);
- io_uring_prep_writev(sqe, pipe1[1], &vec, 1, 0);
- sqe->user_data = 1;
- sqe = io_uring_get_sqe(&ring);
- io_uring_prep_readv(sqe, pipe1[0], &vec, 1, 0);
- sqe->user_data = 2;
- ret = io_uring_submit(&ring);
- if (ret != 2) {
- fprintf(stderr, "submit: %d\n", ret);
- return TEST_FAILED;
- }
- for (int i = 0; i < 2; i++) {
- ret = io_uring_wait_cqe(&ring, &cqe);
- if (ret) {
- fprintf(stderr, "wait: %d\n", ret);
- return TEST_FAILED;
- }
- switch (cqe->user_data) {
- case 1: /* writev */
- if (cqe->res != sizeof(ptr)) {
- fprintf(stderr, "write res: %d\n", cqe->res);
- return TEST_FAILED;
- }
- break;
- case 2: /* readv should be denied */
- if (cqe->res != -EACCES) {
- fprintf(stderr, "read res: %d\n", cqe->res);
- return TEST_FAILED;
- }
- break;
- }
- io_uring_cqe_seen(&ring, cqe);
- }
- io_uring_queue_exit(&ring);
- return TEST_OK;
- }
- static int test_restrictions_register_op(void)
- {
- struct io_uring_restriction res[1];
- struct io_uring ring;
- int ret, pipe1[2];
- uint64_t ptr;
- struct iovec vec = {
- .iov_base = &ptr,
- .iov_len = sizeof(ptr)
- };
- if (pipe(pipe1) != 0) {
- perror("pipe");
- return TEST_FAILED;
- }
- ret = io_uring_queue_init(8, &ring, IORING_SETUP_R_DISABLED);
- if (ret) {
- fprintf(stderr, "ring setup failed: %d\n", ret);
- return TEST_FAILED;
- }
- res[0].opcode = IORING_RESTRICTION_REGISTER_OP;
- res[0].register_op = IORING_REGISTER_BUFFERS;
- ret = io_uring_register_restrictions(&ring, res, 1);
- if (ret) {
- if (ret == -EINVAL)
- return TEST_SKIPPED;
- fprintf(stderr, "failed to register restrictions: %d\n", ret);
- return TEST_FAILED;
- }
- ret = io_uring_enable_rings(&ring);
- if (ret) {
- fprintf(stderr, "ring enabling failed: %d\n", ret);
- return TEST_FAILED;
- }
- ret = io_uring_register_buffers(&ring, &vec, 1);
- if (ret) {
- fprintf(stderr, "io_uring_register_buffers failed: %d\n", ret);
- return TEST_FAILED;
- }
- ret = io_uring_register_files(&ring, pipe1, 2);
- if (ret != -EACCES) {
- fprintf(stderr, "io_uring_register_files ret: %d\n", ret);
- return TEST_FAILED;
- }
- io_uring_queue_exit(&ring);
- return TEST_OK;
- }
- static int test_restrictions_fixed_file(void)
- {
- struct io_uring_restriction res[4];
- struct io_uring_sqe *sqe;
- struct io_uring_cqe *cqe;
- struct io_uring ring;
- int ret, pipe1[2];
- uint64_t ptr;
- struct iovec vec = {
- .iov_base = &ptr,
- .iov_len = sizeof(ptr)
- };
- if (pipe(pipe1) != 0) {
- perror("pipe");
- return TEST_FAILED;
- }
- ret = io_uring_queue_init(8, &ring, IORING_SETUP_R_DISABLED);
- if (ret) {
- fprintf(stderr, "ring setup failed: %d\n", ret);
- return TEST_FAILED;
- }
- res[0].opcode = IORING_RESTRICTION_SQE_OP;
- res[0].sqe_op = IORING_OP_WRITEV;
- res[1].opcode = IORING_RESTRICTION_SQE_OP;
- res[1].sqe_op = IORING_OP_READV;
- res[2].opcode = IORING_RESTRICTION_SQE_FLAGS_REQUIRED;
- res[2].sqe_flags = IOSQE_FIXED_FILE;
- res[3].opcode = IORING_RESTRICTION_REGISTER_OP;
- res[3].register_op = IORING_REGISTER_FILES;
- ret = io_uring_register_restrictions(&ring, res, 4);
- if (ret) {
- if (ret == -EINVAL)
- return TEST_SKIPPED;
- fprintf(stderr, "failed to register restrictions: %d\n", ret);
- return TEST_FAILED;
- }
- ret = io_uring_enable_rings(&ring);
- if (ret) {
- fprintf(stderr, "ring enabling failed: %d\n", ret);
- return TEST_FAILED;
- }
- ret = io_uring_register_files(&ring, pipe1, 2);
- if (ret) {
- fprintf(stderr, "io_uring_register_files ret: %d\n", ret);
- return TEST_FAILED;
- }
- sqe = io_uring_get_sqe(&ring);
- io_uring_prep_writev(sqe, 1, &vec, 1, 0);
- io_uring_sqe_set_flags(sqe, IOSQE_FIXED_FILE);
- sqe->user_data = 1;
- sqe = io_uring_get_sqe(&ring);
- io_uring_prep_readv(sqe, 0, &vec, 1, 0);
- io_uring_sqe_set_flags(sqe, IOSQE_FIXED_FILE);
- sqe->user_data = 2;
- sqe = io_uring_get_sqe(&ring);
- io_uring_prep_writev(sqe, pipe1[1], &vec, 1, 0);
- sqe->user_data = 3;
- ret = io_uring_submit(&ring);
- if (ret != 3) {
- fprintf(stderr, "submit: %d\n", ret);
- return TEST_FAILED;
- }
- for (int i = 0; i < 3; i++) {
- ret = io_uring_wait_cqe(&ring, &cqe);
- if (ret) {
- fprintf(stderr, "wait: %d\n", ret);
- return TEST_FAILED;
- }
- switch (cqe->user_data) {
- case 1: /* writev */
- if (cqe->res != sizeof(ptr)) {
- fprintf(stderr, "write res: %d\n", cqe->res);
- return TEST_FAILED;
- }
- break;
- case 2: /* readv */
- if (cqe->res != sizeof(ptr)) {
- fprintf(stderr, "read res: %d\n", cqe->res);
- return TEST_FAILED;
- }
- break;
- case 3: /* writev without fixed_file should be denied */
- if (cqe->res != -EACCES) {
- fprintf(stderr, "write res: %d\n", cqe->res);
- return TEST_FAILED;
- }
- break;
- }
- io_uring_cqe_seen(&ring, cqe);
- }
- io_uring_queue_exit(&ring);
- return TEST_OK;
- }
- static int test_restrictions_flags(void)
- {
- struct io_uring_restriction res[3];
- struct io_uring_sqe *sqe;
- struct io_uring_cqe *cqe;
- struct io_uring ring;
- int ret, pipe1[2];
- uint64_t ptr;
- struct iovec vec = {
- .iov_base = &ptr,
- .iov_len = sizeof(ptr)
- };
- if (pipe(pipe1) != 0) {
- perror("pipe");
- return TEST_FAILED;
- }
- ret = io_uring_queue_init(8, &ring, IORING_SETUP_R_DISABLED);
- if (ret) {
- fprintf(stderr, "ring setup failed: %d\n", ret);
- return TEST_FAILED;
- }
- res[0].opcode = IORING_RESTRICTION_SQE_OP;
- res[0].sqe_op = IORING_OP_WRITEV;
- res[1].opcode = IORING_RESTRICTION_SQE_FLAGS_ALLOWED;
- res[1].sqe_flags = IOSQE_ASYNC | IOSQE_IO_LINK;
- res[2].opcode = IORING_RESTRICTION_SQE_FLAGS_REQUIRED;
- res[2].sqe_flags = IOSQE_FIXED_FILE;
- ret = io_uring_register_restrictions(&ring, res, 3);
- if (ret) {
- if (ret == -EINVAL)
- return TEST_SKIPPED;
- fprintf(stderr, "failed to register restrictions: %d\n", ret);
- return TEST_FAILED;
- }
- ret = io_uring_register_files(&ring, pipe1, 2);
- if (ret) {
- fprintf(stderr, "io_uring_register_files ret: %d\n", ret);
- return TEST_FAILED;
- }
- ret = io_uring_enable_rings(&ring);
- if (ret) {
- fprintf(stderr, "ring enabling failed: %d\n", ret);
- return TEST_FAILED;
- }
- sqe = io_uring_get_sqe(&ring);
- io_uring_prep_writev(sqe, 1, &vec, 1, 0);
- io_uring_sqe_set_flags(sqe, IOSQE_FIXED_FILE);
- sqe->user_data = 1;
- sqe = io_uring_get_sqe(&ring);
- io_uring_prep_writev(sqe, 1, &vec, 1, 0);
- io_uring_sqe_set_flags(sqe, IOSQE_FIXED_FILE | IOSQE_ASYNC);
- sqe->user_data = 2;
- sqe = io_uring_get_sqe(&ring);
- io_uring_prep_writev(sqe, 1, &vec, 1, 0);
- io_uring_sqe_set_flags(sqe, IOSQE_FIXED_FILE | IOSQE_IO_LINK);
- sqe->user_data = 3;
- ret = io_uring_submit(&ring);
- if (ret != 3) {
- fprintf(stderr, "submit: %d\n", ret);
- return TEST_FAILED;
- }
- sqe = io_uring_get_sqe(&ring);
- io_uring_prep_writev(sqe, 1, &vec, 1, 0);
- io_uring_sqe_set_flags(sqe, IOSQE_FIXED_FILE | IOSQE_IO_DRAIN);
- sqe->user_data = 4;
- ret = io_uring_submit(&ring);
- if (ret != 1) {
- fprintf(stderr, "submit: %d\n", ret);
- return TEST_FAILED;
- }
- sqe = io_uring_get_sqe(&ring);
- io_uring_prep_writev(sqe, pipe1[1], &vec, 1, 0);
- io_uring_sqe_set_flags(sqe, IOSQE_IO_DRAIN);
- sqe->user_data = 5;
- ret = io_uring_submit(&ring);
- if (ret != 1) {
- fprintf(stderr, "submit: %d\n", ret);
- return TEST_FAILED;
- }
- sqe = io_uring_get_sqe(&ring);
- io_uring_prep_writev(sqe, pipe1[1], &vec, 1, 0);
- io_uring_sqe_set_flags(sqe, IOSQE_ASYNC);
- sqe->user_data = 6;
- ret = io_uring_submit(&ring);
- if (ret != 1) {
- fprintf(stderr, "submit: %d\n", ret);
- return TEST_FAILED;
- }
- sqe = io_uring_get_sqe(&ring);
- io_uring_prep_writev(sqe, pipe1[1], &vec, 1, 0);
- sqe->user_data = 7;
- ret = io_uring_submit(&ring);
- if (ret != 1) {
- fprintf(stderr, "submit: %d\n", ret);
- return TEST_FAILED;
- }
- for (int i = 0; i < 7; i++) {
- ret = io_uring_wait_cqe(&ring, &cqe);
- if (ret) {
- fprintf(stderr, "wait: %d\n", ret);
- return TEST_FAILED;
- }
- switch (cqe->user_data) {
- case 1: /* writev - flags = IOSQE_FIXED_FILE */
- case 2: /* writev - flags = IOSQE_FIXED_FILE | IOSQE_ASYNC */
- case 3: /* writev - flags = IOSQE_FIXED_FILE | IOSQE_IO_LINK */
- if (cqe->res != sizeof(ptr)) {
- fprintf(stderr, "write res: %d user_data %" PRIu64 "\n",
- cqe->res, (uint64_t) cqe->user_data);
- return TEST_FAILED;
- }
- break;
- case 4: /* writev - flags = IOSQE_FIXED_FILE | IOSQE_IO_DRAIN */
- case 5: /* writev - flags = IOSQE_IO_DRAIN */
- case 6: /* writev - flags = IOSQE_ASYNC */
- case 7: /* writev - flags = 0 */
- if (cqe->res != -EACCES) {
- fprintf(stderr, "write res: %d user_data %" PRIu64 "\n",
- cqe->res, (uint64_t) cqe->user_data);
- return TEST_FAILED;
- }
- break;
- }
- io_uring_cqe_seen(&ring, cqe);
- }
- io_uring_queue_exit(&ring);
- return TEST_OK;
- }
- static int test_restrictions_empty(void)
- {
- struct io_uring_restriction res[0];
- struct io_uring_sqe *sqe;
- struct io_uring_cqe *cqe;
- struct io_uring ring;
- int ret, pipe1[2];
- uint64_t ptr;
- struct iovec vec = {
- .iov_base = &ptr,
- .iov_len = sizeof(ptr)
- };
- if (pipe(pipe1) != 0) {
- perror("pipe");
- return TEST_FAILED;
- }
- ret = io_uring_queue_init(8, &ring, IORING_SETUP_R_DISABLED);
- if (ret) {
- fprintf(stderr, "ring setup failed: %d\n", ret);
- return TEST_FAILED;
- }
- ret = io_uring_register_restrictions(&ring, res, 0);
- if (ret) {
- if (ret == -EINVAL)
- return TEST_SKIPPED;
- fprintf(stderr, "failed to register restrictions: %d\n", ret);
- return TEST_FAILED;
- }
- ret = io_uring_enable_rings(&ring);
- if (ret) {
- fprintf(stderr, "ring enabling failed: %d\n", ret);
- return TEST_FAILED;
- }
- ret = io_uring_register_buffers(&ring, &vec, 1);
- if (ret != -EACCES) {
- fprintf(stderr, "io_uring_register_buffers ret: %d\n", ret);
- return TEST_FAILED;
- }
- ret = io_uring_register_files(&ring, pipe1, 2);
- if (ret != -EACCES) {
- fprintf(stderr, "io_uring_register_files ret: %d\n", ret);
- return TEST_FAILED;
- }
- sqe = io_uring_get_sqe(&ring);
- io_uring_prep_writev(sqe, pipe1[1], &vec, 1, 0);
- ret = io_uring_submit(&ring);
- if (ret != 1) {
- fprintf(stderr, "submit: %d\n", ret);
- return TEST_FAILED;
- }
- ret = io_uring_wait_cqe(&ring, &cqe);
- if (ret) {
- fprintf(stderr, "wait: %d\n", ret);
- return TEST_FAILED;
- }
- if (cqe->res != -EACCES) {
- fprintf(stderr, "write res: %d\n", cqe->res);
- return TEST_FAILED;
- }
- io_uring_cqe_seen(&ring, cqe);
- io_uring_queue_exit(&ring);
- return TEST_OK;
- }
- static int test_restrictions_rings_not_disabled(void)
- {
- struct io_uring_restriction res[1];
- struct io_uring ring;
- int ret;
- ret = io_uring_queue_init(8, &ring, 0);
- if (ret) {
- fprintf(stderr, "ring setup failed: %d\n", ret);
- return TEST_FAILED;
- }
- res[0].opcode = IORING_RESTRICTION_SQE_OP;
- res[0].sqe_op = IORING_OP_WRITEV;
- ret = io_uring_register_restrictions(&ring, res, 1);
- if (ret != -EBADFD) {
- fprintf(stderr, "io_uring_register_restrictions ret: %d\n",
- ret);
- return TEST_FAILED;
- }
- io_uring_queue_exit(&ring);
- return TEST_OK;
- }
- static int test_restrictions_rings_disabled(void)
- {
- struct io_uring_sqe *sqe;
- struct io_uring ring;
- int ret;
- ret = io_uring_queue_init(8, &ring, IORING_SETUP_R_DISABLED);
- if (ret) {
- fprintf(stderr, "ring setup failed: %d\n", ret);
- return TEST_FAILED;
- }
- sqe = io_uring_get_sqe(&ring);
- io_uring_prep_nop(sqe);
- ret = io_uring_submit(&ring);
- if (ret != -EBADFD) {
- fprintf(stderr, "submit: %d\n", ret);
- return TEST_FAILED;
- }
- io_uring_queue_exit(&ring);
- return TEST_OK;
- }
- int main(int argc, char *argv[])
- {
- int ret;
- if (argc > 1)
- return 0;
- ret = test_restrictions_sqe_op();
- if (ret == TEST_SKIPPED) {
- printf("test_restrictions_sqe_op: skipped\n");
- return 0;
- } else if (ret == TEST_FAILED) {
- fprintf(stderr, "test_restrictions_sqe_op failed\n");
- return ret;
- }
- ret = test_restrictions_register_op();
- if (ret == TEST_SKIPPED) {
- printf("test_restrictions_register_op: skipped\n");
- } else if (ret == TEST_FAILED) {
- fprintf(stderr, "test_restrictions_register_op failed\n");
- return ret;
- }
- ret = test_restrictions_fixed_file();
- if (ret == TEST_SKIPPED) {
- printf("test_restrictions_fixed_file: skipped\n");
- } else if (ret == TEST_FAILED) {
- fprintf(stderr, "test_restrictions_fixed_file failed\n");
- return ret;
- }
- ret = test_restrictions_flags();
- if (ret == TEST_SKIPPED) {
- printf("test_restrictions_flags: skipped\n");
- } else if (ret == TEST_FAILED) {
- fprintf(stderr, "test_restrictions_flags failed\n");
- return ret;
- }
- ret = test_restrictions_empty();
- if (ret == TEST_SKIPPED) {
- printf("test_restrictions_empty: skipped\n");
- } else if (ret == TEST_FAILED) {
- fprintf(stderr, "test_restrictions_empty failed\n");
- return ret;
- }
- ret = test_restrictions_rings_not_disabled();
- if (ret == TEST_SKIPPED) {
- printf("test_restrictions_rings_not_disabled: skipped\n");
- } else if (ret == TEST_FAILED) {
- fprintf(stderr, "test_restrictions_rings_not_disabled failed\n");
- return ret;
- }
- ret = test_restrictions_rings_disabled();
- if (ret == TEST_SKIPPED) {
- printf("test_restrictions_rings_disabled: skipped\n");
- } else if (ret == TEST_FAILED) {
- fprintf(stderr, "test_restrictions_rings_disabled failed\n");
- return ret;
- }
- return 0;
- }
|