[PATCH v3 10/16] tests: check decoding of AF_INET diag request attribute

JingPiao Chen chenjingpiao at gmail.com
Mon Jun 19 01:15:34 UTC 2017


* tests/sock_diag_inet_req_attrs.c: New file.
* tests/gen_tests.in (sock_diag_inet_req_attrs): New entry.
* tests/pure_executables.list: Add sock_diag_inet_req_attrs.
* tests/.gitignore: Likewise.
---
 tests/.gitignore                 |   1 +
 tests/gen_tests.in               |   1 +
 tests/pure_executables.list      |   1 +
 tests/sock_diag_inet_req_attrs.c | 219 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 222 insertions(+)
 create mode 100644 tests/sock_diag_inet_req_attrs.c

diff --git a/tests/.gitignore b/tests/.gitignore
index 34d3909..28381e2 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -345,6 +345,7 @@ sigreturn
 sigsuspend
 sleep
 sock_diag_inet_attrs
+sock_diag_inet_req_attrs
 sock_diag_netlink_attrs
 sock_diag_packet_attrs
 sock_diag_unix_attrs
diff --git a/tests/gen_tests.in b/tests/gen_tests.in
index 5fc653c..9d0488c 100644
--- a/tests/gen_tests.in
+++ b/tests/gen_tests.in
@@ -296,6 +296,7 @@ sigprocmask	-a34
 sigreturn	-esignal='!USR1'
 sigsuspend	-a19 -esignal=none
 sock_diag_inet_attrs		+netlink_sock_diag.test
+sock_diag_inet_req_attrs	+netlink_sock_diag.test
 sock_diag_netlink_attrs		+netlink_sock_diag.test
 sock_diag_packet_attrs		+netlink_sock_diag.test
 sock_diag_unix_attrs		+netlink_sock_diag.test
diff --git a/tests/pure_executables.list b/tests/pure_executables.list
index df8937a..a2507d1 100755
--- a/tests/pure_executables.list
+++ b/tests/pure_executables.list
@@ -284,6 +284,7 @@ sigprocmask
 sigreturn
 sigsuspend
 sock_diag_inet_attrs
+sock_diag_inet_req_attrs
 sock_diag_netlink_attrs
 sock_diag_packet_attrs
 sock_diag_unix_attrs
diff --git a/tests/sock_diag_inet_req_attrs.c b/tests/sock_diag_inet_req_attrs.c
new file mode 100644
index 0000000..2f67b29
--- /dev/null
+++ b/tests/sock_diag_inet_req_attrs.c
@@ -0,0 +1,219 @@
+/*
+ * Copyright (c) 2017 JingPiao Chen <chenjingpiao at gmail.com>
+ * Copyright (c) 2017 The strace developers.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "tests.h"
+#include <stdio.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/tcp.h>
+#include <arpa/inet.h>
+#include <linux/inet_diag.h>
+#include <linux/netlink.h>
+#include <linux/rtnetlink.h>
+#include <linux/sock_diag.h>
+
+#if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
+# define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
+#endif
+
+static void
+test_inet_diag_req_none_compat(const int fd)
+{
+	const char address[] = "12.34.56.78";
+	struct nlmsghdr *nlh;
+	struct inet_diag_req *req;
+	struct nlattr *nla;
+	int nla_len;
+	void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*req)));
+	long rc;
+
+	/* struct nlattr */
+	nla_len = NLA_HDRLEN;
+	nlh = nlh0 - nla_len;
+	*nlh = (struct nlmsghdr) {
+		.nlmsg_len = NLMSG_SPACE(sizeof(*req)) + nla_len,
+		.nlmsg_type = TCPDIAG_GETSOCK,
+		.nlmsg_flags = NLM_F_REQUEST
+	};
+
+	req = NLMSG_DATA(nlh);
+	*req = (struct inet_diag_req) {
+		.idiag_family = AF_INET,
+		.idiag_ext = 1 << (INET_DIAG_TOS - 1),
+		.idiag_states = 1 << TCP_LAST_ACK
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = INET_DIAG_REQ_NONE
+	};
+
+	if (!inet_pton(AF_INET, address, req->id.idiag_src))
+		perror_msg_and_skip("sendto");
+	if (!inet_pton(AF_INET, address, req->id.idiag_dst))
+		perror_msg_and_skip("sendto");
+
+	rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=TCPDIAG_GETSOCK"
+	       ", flags=NLM_F_REQUEST, seq=0, pid=0}, {idiag_family=AF_INET"
+	       ", idiag_src_len=0, idiag_dst_len=0"
+	       ", idiag_ext=1<<(INET_DIAG_TOS-1), id={idiag_sport=htons(0)"
+	       ", idiag_dport=htons(0), inet_pton(AF_INET, \"%s\", &idiag_src)"
+	       ", inet_pton(AF_INET, \"%s\", &idiag_dst), idiag_if=0"
+	       ", idiag_cookie=[0, 0]}, idiag_states=1<<TCP_LAST_ACK"
+	       ", idiag_dbs=0}, {nla_len=%u, nla_type=INET_DIAG_REQ_NONE}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, nlh->nlmsg_len, address, address, nla->nla_len,
+	       nlh->nlmsg_len, sprintrc(rc));
+
+	/* struct nlattr and some data */
+	nla_len = NLA_HDRLEN + 4;
+	memmove(nlh0 - nla_len, nlh, NLMSG_SPACE(sizeof(*req)));
+	nlh = nlh0 - nla_len;
+	nlh->nlmsg_len = NLMSG_SPACE(sizeof(*req)) + nla_len;
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = INET_DIAG_REQ_NONE
+	};
+	memcpy(RTA_DATA(nla), "1234", 4);
+
+	rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=TCPDIAG_GETSOCK"
+	       ", flags=NLM_F_REQUEST, seq=0, pid=0}, {idiag_family=AF_INET"
+	       ", idiag_src_len=0, idiag_dst_len=0"
+	       ", idiag_ext=1<<(INET_DIAG_TOS-1), id={idiag_sport=htons(0)"
+	       ", idiag_dport=htons(0), inet_pton(AF_INET, \"%s\", &idiag_src)"
+	       ", inet_pton(AF_INET, \"%s\", &idiag_dst), idiag_if=0"
+	       ", idiag_cookie=[0, 0]}, idiag_states=1<<TCP_LAST_ACK"
+	       ", idiag_dbs=0}, {{nla_len=%u, nla_type=INET_DIAG_REQ_NONE}"
+	       ", \"1234\"}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, nlh->nlmsg_len, address, address, nla->nla_len,
+	       nlh->nlmsg_len, sprintrc(rc));
+}
+
+static void
+test_inet_diag_req_none(const int fd)
+{
+	const char address[] = "12.34.56.78";
+	struct nlmsghdr *nlh;
+	struct inet_diag_req_v2 *req;
+	struct nlattr *nla;
+	int nla_len;
+	void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*req)));
+	long rc;
+
+	/* struct nlattr */
+	nla_len = NLA_HDRLEN;
+	nlh = nlh0 - nla_len;
+	*nlh = (struct nlmsghdr) {
+		.nlmsg_len = NLMSG_SPACE(sizeof(*req)) + nla_len,
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_REQUEST
+	};
+
+	req = NLMSG_DATA(nlh);
+	*req = (struct inet_diag_req_v2) {
+		.sdiag_family = AF_INET,
+		.idiag_ext = 1 << (INET_DIAG_CONG - 1),
+		.sdiag_protocol = IPPROTO_TCP,
+		.idiag_states = 1 << TCP_CLOSE
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = INET_DIAG_REQ_NONE
+	};
+
+	if (!inet_pton(AF_INET, address, req->id.idiag_src))
+		perror_msg_and_skip("sendto");
+	if (!inet_pton(AF_INET, address, req->id.idiag_dst))
+		perror_msg_and_skip("sendto");
+
+	rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+	       ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+	       ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+	       ", id={idiag_sport=htons(0), idiag_dport=htons(0)"
+	       ", inet_pton(AF_INET, \"%s\", &idiag_src)"
+	       ", inet_pton(AF_INET, \"%s\", &idiag_dst)"
+	       ", idiag_if=0, idiag_cookie=[0, 0]}}, {nla_len=%u"
+	       ", nla_type=INET_DIAG_REQ_NONE}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, nlh->nlmsg_len, address, address, nla->nla_len,
+	       nlh->nlmsg_len, sprintrc(rc));
+
+	/* struct nlattr and some data */
+	nla_len = NLA_HDRLEN + 4;
+	memmove(nlh0 - nla_len, nlh, NLMSG_SPACE(sizeof(*req)));
+	nlh = nlh0 - nla_len;
+	nlh->nlmsg_len = NLMSG_SPACE(sizeof(*req)) + nla_len;
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = INET_DIAG_REQ_NONE
+	};
+	memcpy(RTA_DATA(nla), "1234", 4);
+
+	rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+	       ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+	       ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+	       ", id={idiag_sport=htons(0), idiag_dport=htons(0)"
+	       ", inet_pton(AF_INET, \"%s\", &idiag_src)"
+	       ", inet_pton(AF_INET, \"%s\", &idiag_dst)"
+	       ", idiag_if=0, idiag_cookie=[0, 0]}}, {{nla_len=%u"
+	       ", nla_type=INET_DIAG_REQ_NONE}, \"1234\"}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, nlh->nlmsg_len, address, address, nla->nla_len,
+	       nlh->nlmsg_len, sprintrc(rc));
+}
+
+int main(void)
+{
+	skip_if_unavailable("/proc/self/fd/");
+
+	int fd = create_nl_socket(NETLINK_SOCK_DIAG);
+
+	test_inet_diag_req_none_compat(fd);
+	test_inet_diag_req_none(fd);
+
+	printf("+++ exited with 0 +++\n");
+
+	return 0;
+}
-- 
2.7.4





More information about the Strace-devel mailing list