[PATCH v6 07/18] tests: check decoding of inet_diag_msg attributes
Dmitry V. Levin
ldv at altlinux.org
Tue Jun 27 17:34:58 UTC 2017
On Tue, Jun 27, 2017 at 06:10:09PM +0800, JingPiao Chen wrote:
[...]
> +static void
> +init_inet_diag_msg(struct nlmsghdr *nlh, unsigned int msg_len,
> + const char *address)
> +{
> + struct inet_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 inet_diag_msg, msg,
> + .idiag_family = AF_INET,
> + .idiag_state = TCP_LISTEN
> + );
> +
> + if (!inet_pton(AF_INET, address, msg->id.idiag_src))
> + perror_msg_and_skip("inet_pton");
> + if (!inet_pton(AF_INET, address, msg->id.idiag_dst))
> + perror_msg_and_skip("inet_pton");
> +}
This can be shortened to
if (!inet_pton(AF_INET, address, msg->id.idiag_src) ||
!inet_pton(AF_INET, address, msg->id.idiag_dst))
perror_msg_and_skip("inet_pton");
[...]
> +static void
> +test_inet_diag_skmeminfo(const int fd)
> +{
> + const int hdrlen = sizeof(struct inet_diag_msg);
> + const char address[] = "87.65.43.21";
> + struct nlmsghdr *nlh;
> + struct nlattr *nla;
> + uint32_t *mem;
> + 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_inet_diag_msg(nlh, msg_len, address);
> +
> + nla = NLMSG_ATTR(nlh, hdrlen);
> + SET_STRUCT(struct nlattr, nla,
> + .nla_len = nla_len,
> + .nla_type = INET_DIAG_SKMEMINFO
> + );
> + 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}, {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}, \"12\"}}"
> + ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
> + fd, msg_len, address, address, nla_len,
> + msg_len, sprintrc(rc));
> +
> + /* len = sizeof(uint32_t) * 2 - 1 */
> + nla_len = NLA_HDRLEN + sizeof(*mem) * 2 - 1;
> + msg_len = NLMSG_SPACE(hdrlen) + nla_len;
> + nlh = nlh0 - nla_len;
> + init_inet_diag_msg(nlh, msg_len, address);
> +
> + nla = NLMSG_ATTR(nlh, hdrlen);
> + SET_STRUCT(struct nlattr, nla,
> + .nla_len = nla_len,
> + .nla_type = INET_DIAG_SKMEMINFO
> + );
> + mem = RTA_DATA(nla);
> + mem[0] = 0xaffacbad;
This is unaligned access. I suggest to do the same trick with mem[] array
as you did with structures: create a static array and call memcpy.
> +
> + 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, MSG_DONTWAIT, NULL, 0) = %s\n",
> + fd, msg_len, address, address, nla_len,
> + mem[0], msg_len, sprintrc(rc));
> +
> + /* len = sizeof(uint32_t) * 2 */
> + nla_len = NLA_HDRLEN + sizeof(*mem) * 2;
> + msg_len = NLMSG_SPACE(hdrlen) + nla_len;
> + nlh = nlh0 - nla_len;
> + init_inet_diag_msg(nlh, msg_len, address);
> +
> + nla = NLMSG_ATTR(nlh, hdrlen);
> + SET_STRUCT(struct nlattr, nla,
> + .nla_len = nla_len,
> + .nla_type = INET_DIAG_SKMEMINFO
> + );
> + mem = RTA_DATA(nla);
> + mem[0] = 0xaffacbad;
> + mem[1] = 0xffadbcab;
There is no unaligned access here, but I suggest to use memcpy here as well.
--
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/20170627/b5442e20/attachment.bin>
More information about the Strace-devel
mailing list