Skip to content

Commit 7e699d2

Browse files
Arseniy Krasnovdavem330
Arseniy Krasnov
authored andcommitted
test/vsock: copy to user failure test
This adds SOCK_STREAM and SOCK_SEQPACKET tests for invalid buffer case. It tries to read data to NULL buffer (data already presents in socket's queue), then uses valid buffer. For SOCK_STREAM second read must return data, because skbuff is not dropped, but for SOCK_SEQPACKET skbuff will be dropped by kernel, and 'recv()' will return EAGAIN. Signed-off-by: Arseniy Krasnov <[email protected]> Reviewed-by: Stefano Garzarella <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 8daaf39 commit 7e699d2

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

tools/testing/vsock/vsock_test.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,114 @@ static void test_stream_poll_rcvlowat_client(const struct test_opts *opts)
860860
close(fd);
861861
}
862862

863+
#define INV_BUF_TEST_DATA_LEN 512
864+
865+
static void test_inv_buf_client(const struct test_opts *opts, bool stream)
866+
{
867+
unsigned char data[INV_BUF_TEST_DATA_LEN] = {0};
868+
ssize_t ret;
869+
int fd;
870+
871+
if (stream)
872+
fd = vsock_stream_connect(opts->peer_cid, 1234);
873+
else
874+
fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
875+
876+
if (fd < 0) {
877+
perror("connect");
878+
exit(EXIT_FAILURE);
879+
}
880+
881+
control_expectln("SENDDONE");
882+
883+
/* Use invalid buffer here. */
884+
ret = recv(fd, NULL, sizeof(data), 0);
885+
if (ret != -1) {
886+
fprintf(stderr, "expected recv(2) failure, got %zi\n", ret);
887+
exit(EXIT_FAILURE);
888+
}
889+
890+
if (errno != ENOMEM) {
891+
fprintf(stderr, "unexpected recv(2) errno %d\n", errno);
892+
exit(EXIT_FAILURE);
893+
}
894+
895+
ret = recv(fd, data, sizeof(data), MSG_DONTWAIT);
896+
897+
if (stream) {
898+
/* For SOCK_STREAM we must continue reading. */
899+
if (ret != sizeof(data)) {
900+
fprintf(stderr, "expected recv(2) success, got %zi\n", ret);
901+
exit(EXIT_FAILURE);
902+
}
903+
/* Don't check errno in case of success. */
904+
} else {
905+
/* For SOCK_SEQPACKET socket's queue must be empty. */
906+
if (ret != -1) {
907+
fprintf(stderr, "expected recv(2) failure, got %zi\n", ret);
908+
exit(EXIT_FAILURE);
909+
}
910+
911+
if (errno != EAGAIN) {
912+
fprintf(stderr, "unexpected recv(2) errno %d\n", errno);
913+
exit(EXIT_FAILURE);
914+
}
915+
}
916+
917+
control_writeln("DONE");
918+
919+
close(fd);
920+
}
921+
922+
static void test_inv_buf_server(const struct test_opts *opts, bool stream)
923+
{
924+
unsigned char data[INV_BUF_TEST_DATA_LEN] = {0};
925+
ssize_t res;
926+
int fd;
927+
928+
if (stream)
929+
fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
930+
else
931+
fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
932+
933+
if (fd < 0) {
934+
perror("accept");
935+
exit(EXIT_FAILURE);
936+
}
937+
938+
res = send(fd, data, sizeof(data), 0);
939+
if (res != sizeof(data)) {
940+
fprintf(stderr, "unexpected send(2) result %zi\n", res);
941+
exit(EXIT_FAILURE);
942+
}
943+
944+
control_writeln("SENDDONE");
945+
946+
control_expectln("DONE");
947+
948+
close(fd);
949+
}
950+
951+
static void test_stream_inv_buf_client(const struct test_opts *opts)
952+
{
953+
test_inv_buf_client(opts, true);
954+
}
955+
956+
static void test_stream_inv_buf_server(const struct test_opts *opts)
957+
{
958+
test_inv_buf_server(opts, true);
959+
}
960+
961+
static void test_seqpacket_inv_buf_client(const struct test_opts *opts)
962+
{
963+
test_inv_buf_client(opts, false);
964+
}
965+
966+
static void test_seqpacket_inv_buf_server(const struct test_opts *opts)
967+
{
968+
test_inv_buf_server(opts, false);
969+
}
970+
863971
static struct test_case test_cases[] = {
864972
{
865973
.name = "SOCK_STREAM connection reset",
@@ -920,6 +1028,16 @@ static struct test_case test_cases[] = {
9201028
.run_client = test_seqpacket_bigmsg_client,
9211029
.run_server = test_seqpacket_bigmsg_server,
9221030
},
1031+
{
1032+
.name = "SOCK_STREAM test invalid buffer",
1033+
.run_client = test_stream_inv_buf_client,
1034+
.run_server = test_stream_inv_buf_server,
1035+
},
1036+
{
1037+
.name = "SOCK_SEQPACKET test invalid buffer",
1038+
.run_client = test_seqpacket_inv_buf_client,
1039+
.run_server = test_seqpacket_inv_buf_server,
1040+
},
9231041
{},
9241042
};
9251043

0 commit comments

Comments
 (0)