From patchwork Fri Mar 11 10:55:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arseniy Krasnov X-Patchwork-Id: 12777825 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5804BC433F5 for ; Fri, 11 Mar 2022 10:56:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348108AbiCKK5S (ORCPT ); Fri, 11 Mar 2022 05:57:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347547AbiCKK5R (ORCPT ); Fri, 11 Mar 2022 05:57:17 -0500 Received: from mail.sberdevices.ru (mail.sberdevices.ru [45.89.227.171]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 787381BBF40; Fri, 11 Mar 2022 02:56:13 -0800 (PST) Received: from s-lin-edge02.sberdevices.ru (localhost [127.0.0.1]) by mail.sberdevices.ru (Postfix) with ESMTP id E843F5FD03; Fri, 11 Mar 2022 13:56:10 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sberdevices.ru; s=mail; t=1646996170; bh=DD/056Xz/HdtVJttGh/0J3YuBHetrOfFSU0k/r+/LWU=; h=From:To:Subject:Date:Message-ID:Content-Type:MIME-Version; b=oygVYYnfmxInViijSb+SiWbY6FDcVbeACrqVXJ5/lj7pzqrJUhON6MRdmOrX5a2BX DeKfafmEVkco3JdxvWnNvbqxJtig5Rd1MZNcP/SjDjAYFgaX0HO2TZhQCjuA04iR0z WcVHnmNFBl8tPFPy5kue7LGFxeI1Njgm+/9ZoHGvriXc1JE55spbw6+lWpOntj5+eC nehQQqH2XgxIcm9CQfl75vAPOG0p2MzNqYQeRxvHzvPCXhSdiqq+UfiMhAjGpg1TsC J8jnPCH3ZoooJMC2N8R4O9MqNrJ6HsgP464NpkFZ/16dn6tdkXqLwvrdLQWVMPPjGt uEafdS1fWAxxA== Received: from S-MS-EXCH01.sberdevices.ru (S-MS-EXCH01.sberdevices.ru [172.16.1.4]) by mail.sberdevices.ru (Postfix) with ESMTP; Fri, 11 Mar 2022 13:56:10 +0300 (MSK) From: Krasnov Arseniy Vladimirovich To: Stefano Garzarella CC: Krasnov Arseniy , Rokosov Dmitry Dmitrievich , "kvm@vger.kernel.org" , "virtualization@lists.linux-foundation.org" , "netdev@vger.kernel.org" , "linux-kernel@vger.kernel.org" , Krasnov Arseniy Vladimirovich Subject: [RFC PATCH v1 2/3] af_vsock: SOCK_SEQPACKET receive timeout test Thread-Topic: [RFC PATCH v1 2/3] af_vsock: SOCK_SEQPACKET receive timeout test Thread-Index: AQHYNTaJU61+cjcYFEaSzJHWUZnmpA== Date: Fri, 11 Mar 2022 10:55:42 +0000 Message-ID: <6981b132-4121-62d8-7172-dca28ad1e498@sberdevices.ru> In-Reply-To: <1bb5ce91-da53-7de9-49ba-f49f76f45512@sberdevices.ru> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [172.16.1.12] Content-ID: MIME-Version: 1.0 X-KSMG-Rule-ID: 4 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiPhishing: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 1.1.2.30, bases: 2022/03/11 07:23:00 #18938550 X-KSMG-AntiVirus-Status: Clean, skipped Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-State: RFC Test for receive timeout check: connection is established, receiver sets timeout, but sender does nothing. Receiver's 'read()' call must return EAGAIN. Signed-off-by: Arseniy Krasnov --- tools/testing/vsock/vsock_test.c | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_test.c index 2a3638c0a008..aa2de27d0f77 100644 --- a/tools/testing/vsock/vsock_test.c +++ b/tools/testing/vsock/vsock_test.c @@ -391,6 +391,50 @@ static void test_seqpacket_msg_trunc_server(const struct test_opts *opts) close(fd); } +static void test_seqpacket_timeout_client(const struct test_opts *opts) +{ + int fd; + struct timeval tv; + char dummy; + + fd = vsock_seqpacket_connect(opts->peer_cid, 1234); + if (fd < 0) { + perror("connect"); + exit(EXIT_FAILURE); + } + + tv.tv_sec = 1; + tv.tv_usec = 0; + + if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv)) == -1) { + perror("setsockopt 'SO_RCVTIMEO'"); + exit(EXIT_FAILURE); + } + + if ((read(fd, &dummy, sizeof(dummy)) != -1) || + (errno != EAGAIN)) { + perror("EAGAIN expected"); + exit(EXIT_FAILURE); + } + + control_writeln("WAITDONE"); + close(fd); +} + +static void test_seqpacket_timeout_server(const struct test_opts *opts) +{ + int fd; + + fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL); + if (fd < 0) { + perror("accept"); + exit(EXIT_FAILURE); + } + + control_expectln("WAITDONE"); + close(fd); +} + static struct test_case test_cases[] = { { .name = "SOCK_STREAM connection reset", @@ -431,6 +475,11 @@ static struct test_case test_cases[] = { .run_client = test_seqpacket_msg_trunc_client, .run_server = test_seqpacket_msg_trunc_server, }, + { + .name = "SOCK_SEQPACKET timeout", + .run_client = test_seqpacket_timeout_client, + .run_server = test_seqpacket_timeout_server, + }, {}, }; From patchwork Fri Mar 11 10:58:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arseniy Krasnov X-Patchwork-Id: 12777826 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B296AC433EF for ; Fri, 11 Mar 2022 10:58:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348121AbiCKK74 (ORCPT ); Fri, 11 Mar 2022 05:59:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37758 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346129AbiCKK7y (ORCPT ); Fri, 11 Mar 2022 05:59:54 -0500 Received: from mail.sberdevices.ru (mail.sberdevices.ru [45.89.227.171]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5A9813C39F; Fri, 11 Mar 2022 02:58:50 -0800 (PST) Received: from s-lin-edge02.sberdevices.ru (localhost [127.0.0.1]) by mail.sberdevices.ru (Postfix) with ESMTP id 2683C5FD03; Fri, 11 Mar 2022 13:58:48 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sberdevices.ru; s=mail; t=1646996328; bh=WyQa/Gu+vWVCPA15DPgEI2x5TX1alJGD8E5F+CE4TPE=; h=From:To:Subject:Date:Message-ID:Content-Type:MIME-Version; b=LWFMqCacZCmAYwL9jnJ1qoHMCKSQ2Ov1UF0/5/3U3sVUr08UEsdiUUta3GCIHmdh6 +C11Q+nms9NSIO2Jd10gXbphwuFn8NFJH1RhDcL8uw9+cWdyvndMyY02s8ie5xw7u7 /q5vKWeHzsv/qGcALZ0Kr0aeN1Cuq2qpKUbCSVWjFhaoJyhIQmLTlBzHw63LaRK1q4 9+MZfpVCKNlTbcwDZH2x6+4MdD1Rxs1t4zHNTrLe+61qPXXuIcOt6oOOi8XVDwiyg/ aSkPGfN1lYeXxawti09Y59wKlo5FLK7oxb4ZcFE0CFPMP1a9nDztahrG+/G5gm+kmo r+ijCfwu/zamA== Received: from S-MS-EXCH01.sberdevices.ru (S-MS-EXCH01.sberdevices.ru [172.16.1.4]) by mail.sberdevices.ru (Postfix) with ESMTP; Fri, 11 Mar 2022 13:58:48 +0300 (MSK) From: Krasnov Arseniy Vladimirovich To: Stefano Garzarella CC: Krasnov Arseniy , Rokosov Dmitry Dmitrievich , Krasnov Arseniy Vladimirovich , "kvm@vger.kernel.org" , "virtualization@lists.linux-foundation.org" , "netdev@vger.kernel.org" , "linux-kernel@vger.kernel.org" Subject: [RFC PATCH v1 3/3] af_vsock: SOCK_SEQPACKET broken buffer test Thread-Topic: [RFC PATCH v1 3/3] af_vsock: SOCK_SEQPACKET broken buffer test Thread-Index: AQHYNTbvjWQnm44js0u5mxZaYZpaaQ== Date: Fri, 11 Mar 2022 10:58:32 +0000 Message-ID: In-Reply-To: <1bb5ce91-da53-7de9-49ba-f49f76f45512@sberdevices.ru> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [172.16.1.12] Content-ID: <067E1A7C5BB85447BE521D93922C4087@sberdevices.ru> MIME-Version: 1.0 X-KSMG-Rule-ID: 4 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiPhishing: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 1.1.2.30, bases: 2022/03/11 07:23:00 #18938550 X-KSMG-AntiVirus-Status: Clean, skipped Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-State: RFC Add test where sender sends two message, each with own data pattern. Reader tries to read first to broken buffer: it has three pages size, but middle page is unmapped. Then, reader tries to read second message to valid buffer. Test checks, that uncopied part of first message was dropped and thus not copied as part of second message. Signed-off-by: Arseniy Krasnov --- tools/testing/vsock/vsock_test.c | 121 +++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_test.c index aa2de27d0f77..686af712b4ad 100644 --- a/tools/testing/vsock/vsock_test.c +++ b/tools/testing/vsock/vsock_test.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "timeout.h" #include "control.h" @@ -435,6 +436,121 @@ static void test_seqpacket_timeout_server(const struct test_opts *opts) close(fd); } +#define BUF_PATTERN_1 'a' +#define BUF_PATTERN_2 'b' + +static void test_seqpacket_invalid_rec_buffer_client(const struct test_opts *opts) +{ + int fd; + unsigned char *buf1; + unsigned char *buf2; + int buf_size = getpagesize() * 3; + + fd = vsock_seqpacket_connect(opts->peer_cid, 1234); + if (fd < 0) { + perror("connect"); + exit(EXIT_FAILURE); + } + + buf1 = malloc(buf_size); + if (buf1 == NULL) { + perror("'malloc()' for 'buf1'"); + exit(EXIT_FAILURE); + } + + buf2 = malloc(buf_size); + if (buf2 == NULL) { + perror("'malloc()' for 'buf2'"); + exit(EXIT_FAILURE); + } + + memset(buf1, BUF_PATTERN_1, buf_size); + memset(buf2, BUF_PATTERN_2, buf_size); + + if (send(fd, buf1, buf_size, 0) != buf_size) { + perror("send failed"); + exit(EXIT_FAILURE); + } + + if (send(fd, buf2, buf_size, 0) != buf_size) { + perror("send failed"); + exit(EXIT_FAILURE); + } + + close(fd); +} + +static void test_seqpacket_invalid_rec_buffer_server(const struct test_opts *opts) +{ + int fd; + unsigned char *broken_buf; + unsigned char *valid_buf; + int page_size = getpagesize(); + int buf_size = page_size * 3; + ssize_t res; + int prot = PROT_READ | PROT_WRITE; + int flags = MAP_PRIVATE | MAP_ANONYMOUS; + int i; + + fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL); + if (fd < 0) { + perror("accept"); + exit(EXIT_FAILURE); + } + + /* Setup first buffer. */ + broken_buf = mmap(NULL, buf_size, prot, flags, -1, 0); + if (broken_buf == MAP_FAILED) { + perror("mmap for 'broken_buf'"); + exit(EXIT_FAILURE); + } + + /* Unmap "hole" in buffer. */ + if (munmap(broken_buf + page_size, page_size)) { + perror("'broken_buf' setup"); + exit(EXIT_FAILURE); + } + + valid_buf = mmap(NULL, buf_size, prot, flags, -1, 0); + if (valid_buf == MAP_FAILED) { + perror("mmap for 'valid_buf'"); + exit(EXIT_FAILURE); + } + + /* Try to fill buffer with unmapped middle. */ + res = read(fd, broken_buf, buf_size); + if (res != -1) { + perror("invalid read result of 'broken_buf'"); + exit(EXIT_FAILURE); + } + + if (errno != ENOMEM) { + perror("invalid errno of 'broken_buf'"); + exit(EXIT_FAILURE); + } + + /* Try to fill valid buffer. */ + res = read(fd, valid_buf, buf_size); + if (res != buf_size) { + perror("invalid read result of 'valid_buf'"); + exit(EXIT_FAILURE); + } + + for (i = 0; i < buf_size; i++) { + if (valid_buf[i] != BUF_PATTERN_2) { + perror("invalid pattern for valid buf"); + exit(EXIT_FAILURE); + } + } + + + /* Unmap buffers. */ + munmap(broken_buf, page_size); + munmap(broken_buf + page_size * 2, page_size); + munmap(valid_buf, buf_size); + close(fd); +} + static struct test_case test_cases[] = { { .name = "SOCK_STREAM connection reset", @@ -480,6 +596,11 @@ static struct test_case test_cases[] = { .run_client = test_seqpacket_timeout_client, .run_server = test_seqpacket_timeout_server, }, + { + .name = "SOCK_SEQPACKET invalid receive buffer", + .run_client = test_seqpacket_invalid_rec_buffer_client, + .run_server = test_seqpacket_invalid_rec_buffer_server, + }, {}, };