]> granicus.if.org Git - strace/commitdiff
tests: check decoding of netlink unix_diag_msg attributes
authorJingPiao Chen <chenjingpiao@gmail.com>
Wed, 28 Jun 2017 01:40:07 +0000 (09:40 +0800)
committerDmitry V. Levin <ldv@altlinux.org>
Sat, 1 Jul 2017 00:05:39 +0000 (00:05 +0000)
* tests/nlattr_unix_diag_msg.c: New file.
* tests/gen_tests.in (nlattr_unix_diag_msg): New entry.
* tests/pure_executables.list: Add nlattr_unix_diag_msg.
* tests/.gitignore: Likewise.

tests/.gitignore
tests/gen_tests.in
tests/nlattr_unix_diag_msg.c [new file with mode: 0644]
tests/pure_executables.list

index 4042b180c332786f7312d82299f91ddd405e45f1..cca763fcbd1fc0612e1523f3d3afafeccdd9862c 100644 (file)
@@ -212,6 +212,7 @@ newfstatat
 nlattr
 nlattr_inet_diag_msg
 nlattr_netlink_diag_msg
+nlattr_unix_diag_msg
 nsyscalls
 old_mmap
 oldfstat
index 7170d6a156ea6e435a48a95878d97fd2327ae291..57fd5fd9c74b4fdcc680be7db84956d7a5d9db6c 100644 (file)
@@ -195,6 +195,7 @@ newfstatat  -a32 -v -P stat.sample -P /dev/full
 nlattr         +netlink_sock_diag.test
 nlattr_inet_diag_msg           +netlink_sock_diag.test
 nlattr_netlink_diag_msg                +netlink_sock_diag.test
+nlattr_unix_diag_msg           +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_msg.c b/tests/nlattr_unix_diag_msg.c
new file mode 100644 (file)
index 0000000..80b0b7d
--- /dev/null
@@ -0,0 +1,328 @@
+/*
+ * Copyright (c) 2017 JingPiao Chen <chenjingpiao@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 "netlink.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+#include <sys/socket.h>
+#include <netinet/tcp.h>
+#include <linux/rtnetlink.h>
+#include <linux/sock_diag.h>
+#include <linux/unix_diag.h>
+
+static void
+init_unix_diag_msg(struct nlmsghdr *nlh, unsigned int msg_len)
+{
+       struct unix_diag_msg *msg;
+
+       SET_STRUCT(struct nlmsghdr, nlh,
+               .nlmsg_len = msg_len,
+               .nlmsg_type = SOCK_DIAG_BY_FAMILY,
+               .nlmsg_flags = NLM_F_DUMP
+       );
+
+       msg = NLMSG_DATA(nlh);
+       SET_STRUCT(struct unix_diag_msg, msg,
+               .udiag_family = AF_UNIX,
+               .udiag_type = SOCK_STREAM,
+               .udiag_state = TCP_FIN_WAIT1
+       );
+}
+
+static void
+test_unix_diag_vfs(const int fd)
+{
+       const int hdrlen = sizeof(struct unix_diag_msg);
+       struct nlmsghdr *nlh;
+       struct nlattr *nla;
+       unsigned int nla_len;
+       unsigned int msg_len;
+       void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
+       long rc;
+
+       /* len < sizeof(struct unix_diag_vfs) */
+       nla_len = NLA_HDRLEN + 2;
+       msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+       nlh = nlh0 - nla_len;
+       init_unix_diag_msg(nlh, msg_len);
+
+       nla = NLMSG_ATTR(nlh, hdrlen);
+       SET_STRUCT(struct nlattr, nla,
+               .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_len, msg_len, sprintrc(rc));
+
+       /* short read of unix_diag_vfs */
+       nla_len = NLA_HDRLEN + sizeof(struct unix_diag_vfs);
+       msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+       nlh = nlh0 - (nla_len - 1);
+       init_unix_diag_msg(nlh, msg_len);
+
+       nla = NLMSG_ATTR(nlh, hdrlen);
+       SET_STRUCT(struct nlattr, nla,
+               .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_len, RTA_DATA(nla), msg_len, sprintrc(rc));
+
+       /* unix_diag_vfs */
+       nla_len = NLA_HDRLEN + sizeof(struct unix_diag_vfs);
+       msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+       nlh = nlh0 - nla_len;
+       init_unix_diag_msg(nlh, msg_len);
+
+       nla = NLMSG_ATTR(nlh, hdrlen);
+       SET_STRUCT(struct nlattr, nla,
+               .nla_len = nla_len,
+               .nla_type = UNIX_DIAG_VFS
+       );
+       static const struct unix_diag_vfs uv = {
+               .udiag_vfs_dev = 0xabcddafa,
+               .udiag_vfs_ino = 0xbafabcda
+       };
+       memcpy(RTA_DATA(nla), &uv, sizeof(uv));
+
+       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_len, major(uv.udiag_vfs_dev),
+              minor(uv.udiag_vfs_dev), uv.udiag_vfs_ino,
+              msg_len, sprintrc(rc));
+}
+
+static void
+test_unix_diag_icons(const int fd)
+{
+       const int hdrlen = sizeof(struct unix_diag_msg);
+       struct nlmsghdr *nlh;
+       struct nlattr *nla;
+       unsigned int nla_len;
+       unsigned int msg_len;
+       void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
+       long rc;
+
+       /* len < sizeof(uint32_t) */
+       nla_len = NLA_HDRLEN + 2;
+       msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+       nlh = nlh0 - nla_len;
+       init_unix_diag_msg(nlh, msg_len);
+
+       nla = NLMSG_ATTR(nlh, hdrlen);
+       SET_STRUCT(struct nlattr, nla,
+               .nla_len = nla_len,
+               .nla_type = UNIX_DIAG_ICONS
+       );
+       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_ICONS}, \"12\"}}, %u"
+              ", MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, msg_len, nla_len, msg_len, sprintrc(rc));
+
+       /* len == sizeof(uint32_t) * 2 - 1 */
+       nla_len = NLA_HDRLEN + sizeof(uint32_t) * 2 - 1;
+       msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+       nlh = nlh0 - nla_len;
+       init_unix_diag_msg(nlh, msg_len);
+
+       nla = NLMSG_ATTR(nlh, hdrlen);
+       SET_STRUCT(struct nlattr, nla,
+               .nla_len = nla_len,
+               .nla_type = UNIX_DIAG_ICONS
+       );
+       static const uint32_t inode[] = { 0xadbcadbc, 0xfabdcdac };
+       memcpy(RTA_DATA(nla), inode, sizeof(inode[0]));
+
+       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"
+              ", MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, msg_len, nla_len, inode[0], msg_len, sprintrc(rc));
+
+       /* len == sizeof(uint32_t) * 2 */
+       nla_len = NLA_HDRLEN + sizeof(uint32_t) * 2;
+       msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+       nlh = nlh0 - nla_len;
+       init_unix_diag_msg(nlh, msg_len);
+
+       nla = NLMSG_ATTR(nlh, hdrlen);
+       SET_STRUCT(struct nlattr, nla,
+               .nla_len = nla_len,
+               .nla_type = UNIX_DIAG_ICONS
+       );
+       memcpy(RTA_DATA(nla), inode, sizeof(inode));
+
+       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_len,
+              inode[0], inode[1], msg_len, sprintrc(rc));
+}
+
+static void
+test_unix_diag_rqlen(const int fd)
+{
+       const int hdrlen = sizeof(struct unix_diag_msg);
+       struct nlmsghdr *nlh;
+       struct nlattr *nla;
+       unsigned int nla_len;
+       unsigned int msg_len;
+       void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
+       long rc;
+
+       /* len < sizeof(struct unix_diag_rqlen) */
+       nla_len = NLA_HDRLEN + 2;
+       msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+       nlh = nlh0 - nla_len;
+       init_unix_diag_msg(nlh, msg_len);
+
+       nla = NLMSG_ATTR(nlh, hdrlen);
+       SET_STRUCT(struct nlattr, nla,
+               .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_len, msg_len, sprintrc(rc));
+
+       /* short read of unix_diag_rqlen */
+       nla_len = NLA_HDRLEN + sizeof(struct unix_diag_rqlen);
+       msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+       nlh = nlh0 - (nla_len - 1);
+       init_unix_diag_msg(nlh, msg_len);
+
+       nla = NLMSG_ATTR(nlh, hdrlen);
+       SET_STRUCT(struct nlattr, nla,
+               .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_len, RTA_DATA(nla),
+              msg_len, sprintrc(rc));
+
+       /* unix_diag_rqlen */
+       nla_len = NLA_HDRLEN + sizeof(struct unix_diag_rqlen);
+       msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+       nlh = nlh0 - nla_len;
+       init_unix_diag_msg(nlh, msg_len);
+
+       nla = NLMSG_ATTR(nlh, hdrlen);
+       SET_STRUCT(struct nlattr, nla,
+               .nla_len = nla_len,
+               .nla_type = UNIX_DIAG_RQLEN
+       );
+       static const struct unix_diag_rqlen rql = {
+               .udiag_rqueue = 0xfabdcdad,
+               .udiag_wqueue = 0xbacdadcf
+       };
+       memcpy(RTA_DATA(nla), &rql, sizeof(rql));
+
+       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_len, rql.udiag_rqueue,
+              rql.udiag_wqueue, 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;
+}
index 5efdc55d7a6a343eab8c2932efe7aaf6e58ebe70..033208e604e8b049b3e7241a9feacce3ab15746c 100755 (executable)
@@ -177,6 +177,7 @@ newfstatat
 nlattr
 nlattr_inet_diag_msg
 nlattr_netlink_diag_msg
+nlattr_unix_diag_msg
 old_mmap
 oldfstat
 oldlstat