[PATCH v4 09/10] tests: check decoding of NETLINK_SOCK_DIAG AF_INET attribute

Dmitry V. Levin ldv at altlinux.org
Sun Jun 25 23:56:59 UTC 2017


On Tue, Jun 20, 2017 at 05:23:54PM +0800, JingPiao Chen wrote:
> * tests/nlattr_inet_diag.c: New file.
> * tests/gen_tests.in (nlattr_inet_diag): New entry.
> * tests/pure_executables.list: Add nlattr_inet_diag.
> * tests/.gitignore: Likewise.

This tests decoding of inet_diag_msg attributes only, please reflect it in
commit message.  If you are going to add tests for other inet diag
attributes, maybe nlattr_inet_diag should be rather called
nlattr_inet_diag_msg.

> ---
>  tests/.gitignore            |   1 +
>  tests/gen_tests.in          |   1 +
>  tests/nlattr_inet_diag.c    | 727 ++++++++++++++++++++++++++++++++++++++++++++
>  tests/pure_executables.list |   1 +
>  4 files changed, 730 insertions(+)
>  create mode 100644 tests/nlattr_inet_diag.c
> 
> diff --git a/tests/.gitignore b/tests/.gitignore
> index fc6c82d..af7cad7 100644
> --- a/tests/.gitignore
> +++ b/tests/.gitignore
> @@ -210,6 +210,7 @@ netlink_unix_diag
>  netlink_xfrm
>  newfstatat
>  nlattr
> +nlattr_inet_diag
>  nsyscalls
>  old_mmap
>  oldfstat
> diff --git a/tests/gen_tests.in b/tests/gen_tests.in
> index 275d308..ad7d0b8 100644
> --- a/tests/gen_tests.in
> +++ b/tests/gen_tests.in
> @@ -193,6 +193,7 @@ netlink_selinux	+netlink_sock_diag.test
>  netlink_xfrm	+netlink_sock_diag.test
>  newfstatat	-a32 -v -P stat.sample -P /dev/full
>  nlattr		+netlink_sock_diag.test
> +nlattr_inet_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_inet_diag.c b/tests/nlattr_inet_diag.c
> new file mode 100644
> index 0000000..ef0000c
> --- /dev/null
> +++ b/tests/nlattr_inet_diag.c
> @@ -0,0 +1,727 @@
> +/*
> + * 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

Now you can include "netlink.h" and drop this fallback definition.

> +#define INIT_STRUCT(type, name, ...)			\
> +	do {						\
> +		type tmp = { __VA_ARGS__ };		\
> +		memcpy(name, &tmp, sizeof(tmp));	\
> +	} while (0)

You can use SET_STRUCT from tests.h

> +
> +static void
> +test_inet_diag_meminfo(const int fd)
> +{
> +	const char address[] = "12.34.56.78";
> +	struct nlmsghdr *nlh;
> +	struct inet_diag_msg *msg;
> +	struct nlattr *nla;
> +	struct inet_diag_meminfo *minfo;
> +	int nla_len;
> +	unsigned msg_len;
> +	void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*msg)));
> +	long rc;
> +
> +	/* len < sizeof(*minfo) */
> +	nla_len = NLA_HDRLEN + 2;
> +	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
> +	nlh = nlh0 - nla_len;
> +	INIT_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 inet_diag_msg) {
> +		.idiag_family = AF_INET,
> +		.idiag_state = TCP_LISTEN,
> +	};
> +
> +	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
> +	*nla = (struct nlattr) {
> +		.nla_len = nla_len,
> +		.nla_type = INET_DIAG_MEMINFO
> +	};
> +	memcpy(RTA_DATA(nla), "12", 2);
> +
> +	if (!inet_pton(AF_INET, address, msg->id.idiag_src))
> +		perror_msg_and_skip("sendto");
> +	if (!inet_pton(AF_INET, address, msg->id.idiag_dst))
> +		perror_msg_and_skip("sendto");

Why "sendto"?

Please use different addresses for different fields.

As this setting of inet_diag_msg structure is repeated so many times,
let's create a helper function.

Use memcpy or SET_STRUCT instead of direct initialization - beware
of unaligned access.

> +	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}, {idiag_family=AF_INET"
> +	       ", idiag_state=TCP_LISTEN, idiag_timer=0, idiag_retrans=0"
> +	       ", 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_expires=0"
> +	       ", idiag_rqueue=0, idiag_wqueue=0, idiag_uid=0"
> +	       ", idiag_inode=0}, {{nla_len=%u, nla_type=INET_DIAG_MEMINFO}"
> +	       ", \"12\"}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
> +	       fd, msg_len, address, address, nla->nla_len,
> +	       msg_len, sprintrc(rc));
> +
> +	/* short read of inet_diag_meminfo */
> +	nla_len = NLA_HDRLEN + sizeof(*minfo);
> +	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
> +	nlh = nlh0 - (nla_len - 1);
> +	INIT_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 inet_diag_msg) {
> +		.idiag_family = AF_INET,
> +		.idiag_state = TCP_LISTEN,
> +	};
> +
> +	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
> +	*nla = (struct nlattr) {
> +		.nla_len = nla_len,
> +		.nla_type = INET_DIAG_MEMINFO
> +	};

For example, here nlh, msg, and nla are intentionally unaligned.

[...]
> +static void
> +test_inet_diag_skmeminfo(const int fd)
> +{
> +	const char address[] = "12.34.56.78";
> +	struct nlmsghdr *nlh;
> +	struct inet_diag_msg *msg;
> +	struct nlattr *nla;
> +	uint32_t *mem;
> +	int nla_len;
> +	unsigned msg_len;
> +	void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*msg)));
> +	long rc;
> +
> +	nla_len = NLA_HDRLEN + sizeof(*mem) * 2;
> +	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
> +	nlh = nlh0 - nla_len;
> +	INIT_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 inet_diag_msg) {
> +		.idiag_family = AF_INET,
> +		.idiag_state = TCP_LISTEN,
> +	};
> +
> +	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
> +	*nla = (struct nlattr) {
> +		.nla_len = nla_len,
> +		.nla_type = INET_DIAG_SKMEMINFO
> +	};
> +	mem = RTA_DATA(nla);
> +	mem[0] = 0xaffacbad;
> +	mem[1] = 0xffadbcab;
> +
> +	if (!inet_pton(AF_INET, address, msg->id.idiag_src))
> +		perror_msg_and_skip("sendto");
> +	if (!inet_pton(AF_INET, address, msg->id.idiag_dst))
> +		perror_msg_and_skip("sendto");
> +
> +	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}, {idiag_family=AF_INET"
> +	       ", idiag_state=TCP_LISTEN, idiag_timer=0, idiag_retrans=0"
> +	       ", 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_expires=0"
> +	       ", idiag_rqueue=0, idiag_wqueue=0, idiag_uid=0"
> +	       ", idiag_inode=0}, {{nla_len=%u, nla_type=INET_DIAG_SKMEMINFO}"
> +	       ", [%u, %u]}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
> +	       fd, msg_len, address, address, nla->nla_len,
> +	       0xaffacbad, 0xffadbcab, msg_len, sprintrc(rc));
> +}

The case when the attribute is not a proper INET_DIAG_SKMEMINFO array
is not covered.

> +
> +static void
> +test_inet_diag_dctcpinfo(const int fd)
> +{
> +	const char address[] = "12.34.56.78";
> +	struct nlmsghdr *nlh;
> +	struct inet_diag_msg *msg;
> +	struct nlattr *nla;
> +	struct tcp_dctcp_info *dctcp;
> +	int nla_len;
> +	unsigned msg_len;
> +	void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*msg)));
> +	long rc;
> +
> +	/* len < sizeof(*dctcp) */
> +	nla_len = NLA_HDRLEN + 2;
> +	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
> +	nlh = nlh0 - nla_len;
> +	INIT_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 inet_diag_msg) {
> +		.idiag_family = AF_INET,
> +		.idiag_state = TCP_LISTEN,
> +	};
> +
> +	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
> +	*nla = (struct nlattr) {
> +		.nla_len = nla_len,
> +		.nla_type = INET_DIAG_DCTCPINFO
> +	};
> +	memcpy(RTA_DATA(nla), "12", 2);
> +
> +	if (!inet_pton(AF_INET, address, msg->id.idiag_src))
> +		perror_msg_and_skip("sendto");
> +	if (!inet_pton(AF_INET, address, msg->id.idiag_dst))
> +		perror_msg_and_skip("sendto");
> +
> +	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}, {idiag_family=AF_INET"
> +	       ", idiag_state=TCP_LISTEN, idiag_timer=0, idiag_retrans=0"
> +	       ", 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_expires=0"
> +	       ", idiag_rqueue=0, idiag_wqueue=0, idiag_uid=0"
> +	       ", idiag_inode=0}, {{nla_len=%u, nla_type=INET_DIAG_DCTCPINFO}"
> +	       ", \"12\"}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
> +	       fd, msg_len, address, address, nla->nla_len,
> +	       msg_len, sprintrc(rc));
> +
> +	/* short read of tcp_dctcp_info */
> +	nla_len = NLA_HDRLEN + sizeof(*dctcp);
> +	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
> +	nlh = nlh0 - (nla_len - 1);
> +	INIT_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 inet_diag_msg) {
> +		.idiag_family = AF_INET,
> +		.idiag_state = TCP_LISTEN,
> +	};
> +
> +	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
> +	*nla = (struct nlattr) {
> +		.nla_len = nla_len,
> +		.nla_type = INET_DIAG_DCTCPINFO
> +	};
> +
> +	if (!inet_pton(AF_INET, address, msg->id.idiag_src))
> +		perror_msg_and_skip("sendto");
> +	if (!inet_pton(AF_INET, address, msg->id.idiag_dst))
> +		perror_msg_and_skip("sendto");
> +
> +	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}, {idiag_family=AF_INET"
> +	       ", idiag_state=TCP_LISTEN, idiag_timer=0, idiag_retrans=0"
> +	       ", 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_expires=0"
> +	       ", idiag_rqueue=0, idiag_wqueue=0, idiag_uid=0"
> +	       ", idiag_inode=0}, {{nla_len=%u, nla_type=INET_DIAG_DCTCPINFO}"
> +	       ", %p}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
> +	       fd, msg_len, address, address, nla->nla_len,
> +	       RTA_DATA(nla), msg_len, sprintrc(rc));
> +
> +	/* tcp_dctcp_info */
> +	nla_len = NLA_HDRLEN + sizeof(*dctcp);
> +	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
> +	nlh = nlh0 - nla_len;
> +	INIT_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 inet_diag_msg) {
> +		.idiag_family = AF_INET,
> +		.idiag_state = TCP_LISTEN,
> +	};
> +
> +	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
> +	*nla = (struct nlattr) {
> +		.nla_len = nla_len,
> +		.nla_type = INET_DIAG_DCTCPINFO
> +	};
> +	dctcp = RTA_DATA(nla);
> +	*dctcp = (struct tcp_dctcp_info) {
> +		.dctcp_enabled = 0xfdac,
> +		.dctcp_ce_state = 0xfadc,
> +		.dctcp_alpha = 0xbdabcada,
> +		.dctcp_ab_ecn = 0xbadbfafb,
> +		.dctcp_ab_tot = 0xfdacdadf
> +	};

If you had a source copy of struct tcp_dctcp_info,
you'd be able to do a memcpy here, and ...

> +
> +	if (!inet_pton(AF_INET, address, msg->id.idiag_src))
> +		perror_msg_and_skip("sendto");
> +	if (!inet_pton(AF_INET, address, msg->id.idiag_dst))
> +		perror_msg_and_skip("sendto");
> +
> +	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}, {idiag_family=AF_INET"
> +	       ", idiag_state=TCP_LISTEN, idiag_timer=0, idiag_retrans=0"
> +	       ", 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_expires=0"
> +	       ", idiag_rqueue=0, idiag_wqueue=0, idiag_uid=0"
> +	       ", idiag_inode=0}, {{nla_len=%u, nla_type=INET_DIAG_DCTCPINFO}"
> +	       ", {dctcp_enabled=%u, dctcp_ce_state=%u"
> +	       ", dctcp_alpha=%u, dctcp_ab_ecn=%u, dctcp_ab_tot=%u}}}"
> +	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
> +	       fd, msg_len, address, address, nla->nla_len,
> +	       0xfdac, 0xfadc, 0xbdabcada, 0xbadbfafb, 0xfdacdadf,
> +	       msg_len, sprintrc(rc));
> +}

... you'd be able to do use it here instead of repeating all these constants.


-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20170626/c503c119/attachment.bin>


More information about the Strace-devel mailing list