[PATCH 04/10] tests: check decoding of NETLINK_SOCK_DIAG AF_UNIX diag attributes

JingPiao Chen chenjingpiao at gmail.com
Wed Jun 21 02:42:17 UTC 2017


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

diff --git a/tests/.gitignore b/tests/.gitignore
index 04d75cd..d14ba53 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -212,6 +212,7 @@ newfstatat
 nlattr
 nlattr_inet_diag
 nlattr_netlink_diag
+nlattr_unix_diag
 nsyscalls
 old_mmap
 oldfstat
diff --git a/tests/gen_tests.in b/tests/gen_tests.in
index b5cad20..1b8969f 100644
--- a/tests/gen_tests.in
+++ b/tests/gen_tests.in
@@ -195,6 +195,7 @@ newfstatat	-a32 -v -P stat.sample -P /dev/full
 nlattr		+netlink_sock_diag.test
 nlattr_inet_diag	+netlink_sock_diag.test
 nlattr_netlink_diag	+netlink_sock_diag.test
+nlattr_unix_diag	+netlink_sock_diag.test
 old_mmap	-a11 -e trace=mmap
 oldfstat	-a18 -v -P stat.sample
 oldlstat	-a32 -v -P stat.sample -P /dev/full
diff --git a/tests/nlattr_unix_diag.c b/tests/nlattr_unix_diag.c
new file mode 100644
index 0000000..e42f654
--- /dev/null
+++ b/tests/nlattr_unix_diag.c
@@ -0,0 +1,347 @@
+/*
+ * 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 <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/sysmacros.h>
+#include <netinet/tcp.h>
+#include <linux/netlink.h>
+#include <linux/rtnetlink.h>
+#include <linux/sock_diag.h>
+#include <linux/unix_diag.h>
+
+#if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
+# define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
+#endif
+
+static void
+test_unix_diag_vfs(const int fd)
+{
+	struct nlmsghdr *nlh;
+	struct unix_diag_msg *msg;
+	struct nlattr *nla;
+	struct unix_diag_vfs *uv;
+	int nla_len;
+	unsigned int msg_len;
+	void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*msg)));
+	long rc;
+
+	/* len < sizeof(*uv) */
+	nla_len = NLA_HDRLEN + 2;
+	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+	nlh = nlh0 - nla_len;
+	SET_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_DUMP
+	);
+
+	msg = NLMSG_DATA(nlh);
+	*msg = (struct unix_diag_msg) {
+		.udiag_family = AF_UNIX,
+		.udiag_type = SOCK_STREAM,
+		.udiag_state = TCP_FIN_WAIT1,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = UNIX_DIAG_VFS
+	};
+	memcpy(RTA_DATA(nla), "12", 2);
+
+	rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {udiag_family=AF_UNIX"
+	       ", udiag_type=SOCK_STREAM, udiag_state=TCP_FIN_WAIT1"
+	       ", udiag_ino=0, udiag_cookie=[0, 0]}, {{nla_len=%u"
+	       ", nla_type=UNIX_DIAG_VFS}, \"12\"}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, nla->nla_len, msg_len, sprintrc(rc));
+
+	/* short read of unix_diag_vfs */
+	nla_len = NLA_HDRLEN + sizeof(*uv);
+	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+	nlh = nlh0 - (nla_len - 1);
+	SET_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_DUMP
+	);
+
+	msg = NLMSG_DATA(nlh);
+	*msg = (struct unix_diag_msg) {
+		.udiag_family = AF_UNIX,
+		.udiag_type = SOCK_STREAM,
+		.udiag_state = TCP_FIN_WAIT1,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = UNIX_DIAG_VFS
+	};
+
+	rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {udiag_family=AF_UNIX"
+	       ", udiag_type=SOCK_STREAM, udiag_state=TCP_FIN_WAIT1"
+	       ", udiag_ino=0, udiag_cookie=[0, 0]}, {{nla_len=%u"
+	       ", nla_type=UNIX_DIAG_VFS}, %p}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, nla->nla_len, RTA_DATA(nla),
+	       msg_len, sprintrc(rc));
+
+	/* unix_diag_vfs */
+	nla_len = NLA_HDRLEN + sizeof(*uv);
+	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+	nlh = nlh0 - nla_len;
+	SET_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_DUMP
+	);
+
+	msg = NLMSG_DATA(nlh);
+	*msg = (struct unix_diag_msg) {
+		.udiag_family = AF_UNIX,
+		.udiag_type = SOCK_STREAM,
+		.udiag_state = TCP_FIN_WAIT1,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = UNIX_DIAG_VFS
+	};
+	uv = RTA_DATA(nla);
+	*uv = (struct unix_diag_vfs) {
+		.udiag_vfs_dev = 0xabcddafa,
+		.udiag_vfs_ino = 0xbafabcda,
+	};
+
+	rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {udiag_family=AF_UNIX"
+	       ", udiag_type=SOCK_STREAM, udiag_state=TCP_FIN_WAIT1"
+	       ", udiag_ino=0, udiag_cookie=[0, 0]}, {{nla_len=%u"
+	       ", nla_type=UNIX_DIAG_VFS}, {udiag_vfs_dev=makedev(%u, %u)"
+	       ", udiag_vfs_ino=%u}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, nla->nla_len, major(0xabcddafa),
+	       minor(0xabcddafa), 0xbafabcda, msg_len, sprintrc(rc));
+}
+
+static void
+test_unix_diag_icons(const int fd)
+{
+	struct nlmsghdr *nlh;
+	struct unix_diag_msg *msg;
+	struct nlattr *nla;
+	uint32_t *inode;
+	int nla_len;
+	unsigned int msg_len;
+	void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*msg)));
+	long rc;
+
+	nla_len = NLA_HDRLEN + sizeof(*inode) * 2;
+	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+	nlh = nlh0 - nla_len;
+	SET_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_DUMP
+	);
+
+	msg = NLMSG_DATA(nlh);
+	*msg = (struct unix_diag_msg) {
+		.udiag_family = AF_UNIX,
+		.udiag_type = SOCK_STREAM,
+		.udiag_state = TCP_FIN_WAIT1,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = UNIX_DIAG_ICONS
+	};
+	inode = RTA_DATA(nla);
+	inode[0] = 0xadbcadbc;
+	inode[1] = 0xfabdcdac;
+
+	rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {udiag_family=AF_UNIX"
+	       ", udiag_type=SOCK_STREAM, udiag_state=TCP_FIN_WAIT1"
+	       ", udiag_ino=0, udiag_cookie=[0, 0]}, {{nla_len=%u"
+	       ", nla_type=UNIX_DIAG_ICONS}, [%u, %u]}}, %u"
+	       ", MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, nla->nla_len, 0xadbcadbc, 0xfabdcdac,
+	       msg_len, sprintrc(rc));
+}
+
+static void
+test_unix_diag_rqlen(const int fd)
+{
+	struct nlmsghdr *nlh;
+	struct unix_diag_msg *msg;
+	struct nlattr *nla;
+	struct unix_diag_rqlen *rql;
+	int nla_len;
+	unsigned int msg_len;
+	void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*msg)));
+	long rc;
+
+	/* len < sizeof(*rql) */
+	nla_len = NLA_HDRLEN + 2;
+	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+	nlh = nlh0 - nla_len;
+	SET_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_DUMP
+	);
+
+	msg = NLMSG_DATA(nlh);
+	*msg = (struct unix_diag_msg) {
+		.udiag_family = AF_UNIX,
+		.udiag_type = SOCK_STREAM,
+		.udiag_state = TCP_FIN_WAIT1,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = UNIX_DIAG_RQLEN
+	};
+	memcpy(RTA_DATA(nla), "12", 2);
+
+	rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {udiag_family=AF_UNIX"
+	       ", udiag_type=SOCK_STREAM, udiag_state=TCP_FIN_WAIT1"
+	       ", udiag_ino=0, udiag_cookie=[0, 0]}, {{nla_len=%u"
+	       ", nla_type=UNIX_DIAG_RQLEN}, \"12\"}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, nla->nla_len, msg_len, sprintrc(rc));
+
+	/* short read of unix_diag_rqlen */
+	nla_len = NLA_HDRLEN + sizeof(*rql);
+	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+	nlh = nlh0 - (nla_len - 1);
+	SET_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_DUMP
+	);
+
+	msg = NLMSG_DATA(nlh);
+	*msg = (struct unix_diag_msg) {
+		.udiag_family = AF_UNIX,
+		.udiag_type = SOCK_STREAM,
+		.udiag_state = TCP_FIN_WAIT1,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = UNIX_DIAG_RQLEN
+	};
+
+	rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {udiag_family=AF_UNIX"
+	       ", udiag_type=SOCK_STREAM, udiag_state=TCP_FIN_WAIT1"
+	       ", udiag_ino=0, udiag_cookie=[0, 0]}, {{nla_len=%u"
+	       ", nla_type=UNIX_DIAG_RQLEN}, %p}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, nla->nla_len, RTA_DATA(nla),
+	       msg_len, sprintrc(rc));
+
+	/* unix_diag_rqlen */
+	nla_len = NLA_HDRLEN + sizeof(*rql);
+	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+	nlh = nlh0 - nla_len;
+	SET_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_DUMP
+	);
+
+	msg = NLMSG_DATA(nlh);
+	*msg = (struct unix_diag_msg) {
+		.udiag_family = AF_UNIX,
+		.udiag_type = SOCK_STREAM,
+		.udiag_state = TCP_FIN_WAIT1,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = UNIX_DIAG_RQLEN
+	};
+	rql = RTA_DATA(nla);
+	*rql = (struct unix_diag_rqlen) {
+		.udiag_rqueue = 0xfabdcdad,
+		.udiag_wqueue = 0xbacdadcf
+	};
+
+	rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {udiag_family=AF_UNIX"
+	       ", udiag_type=SOCK_STREAM, udiag_state=TCP_FIN_WAIT1"
+	       ", udiag_ino=0, udiag_cookie=[0, 0]}, {{nla_len=%u"
+	       ", nla_type=UNIX_DIAG_RQLEN}, {udiag_rqueue=%u"
+	       ", udiag_wqueue=%u}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, nla->nla_len, 0xfabdcdad,
+	       0xbacdadcf, msg_len, sprintrc(rc));
+}
+
+int main(void)
+{
+	skip_if_unavailable("/proc/self/fd/");
+
+	int fd = create_nl_socket(NETLINK_SOCK_DIAG);
+
+	test_unix_diag_vfs(fd);
+	test_unix_diag_icons(fd);
+	test_unix_diag_rqlen(fd);
+
+	printf("+++ exited with 0 +++\n");
+
+	return 0;
+}
diff --git a/tests/pure_executables.list b/tests/pure_executables.list
index 92c8feb..b1efac2 100755
--- a/tests/pure_executables.list
+++ b/tests/pure_executables.list
@@ -177,6 +177,7 @@ newfstatat
 nlattr
 nlattr_inet_diag
 nlattr_netlink_diag
+nlattr_unix_diag
 old_mmap
 oldfstat
 oldlstat
-- 
2.7.4





More information about the Strace-devel mailing list