nlattr: fix printing of unrecognized attribute data

Do not print more than "nlattr.nla_len - NLA_HDRLEN" bytes
of unrecognized attribute data.

* nlattr.c (decode_nlattr_with_data): Specify "nla_len - NLA_HDRLEN"
instead of "len - NLA_HDRLEN" as the size of unrecognized attribute
data.
* tests/nlattr.c (test_nlattr): Check it.
This commit is contained in:
Дмитрий Левин 2017-08-16 02:24:29 +00:00
parent c7946d380f
commit 239418b7f8
2 changed files with 15 additions and 9 deletions

View File

@ -88,7 +88,7 @@ decode_nlattr_with_data(struct tcb *const tcp,
nla_len - NLA_HDRLEN,
opaque_data))
printstr_ex(tcp, addr + NLA_HDRLEN,
len - NLA_HDRLEN, QUOTE_FORCE_HEX);
nla_len - NLA_HDRLEN, QUOTE_FORCE_HEX);
tprints("}");
}
}

View File

@ -210,18 +210,22 @@ test_nlattr(const int fd)
", nla_type=UNIX_DIAG_NAME}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
fd, msg_len, nla->nla_len, msg_len, sprintrc(rc));
/* abbreviated output */
/* unrecognized attribute data, abbreviated output */
#define ABBREV_LEN (DEFAULT_STRLEN + 1)
msg_len = NLA_HDRLEN * ABBREV_LEN + NLMSG_SPACE(sizeof(msg->udm));
msg = tail_memdup(&c_msg, msg_len);
memcpy(&msg->nlh.nlmsg_len, &msg_len, sizeof(msg_len));
msg_len = NLMSG_SPACE(sizeof(msg->udm)) + NLA_HDRLEN * ABBREV_LEN * 2;
msg = tail_alloc(msg_len);
memcpy(msg, &c_msg, sizeof(c_msg));
msg->nlh.nlmsg_len = msg_len;
unsigned int i;
nla = NLMSG_ATTR(msg, sizeof(msg->udm));
for (i = 0; i < ABBREV_LEN; ++i)
nla[i] = (struct nlattr) {
.nla_len = NLA_HDRLEN,
for (i = 0; i < ABBREV_LEN; ++i) {
nla[i * 2] = (struct nlattr) {
.nla_len = NLA_HDRLEN * 2 - 1,
.nla_type = UNIX_DIAG_SHUTDOWN + 1 + i
};
fill_memory_ex(&nla[i * 2 + 1], NLA_HDRLEN,
'0' + i, '~' - '0' - i);
}
rc = sendto(fd, msg, msg_len, MSG_DONTWAIT, NULL, 0);
printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
@ -233,8 +237,10 @@ test_nlattr(const int fd)
for (i = 0; i < DEFAULT_STRLEN; ++i) {
if (i)
printf(", ");
printf("{nla_len=%u, nla_type=%#x /* UNIX_DIAG_??? */}",
printf("{{nla_len=%u, nla_type=%#x /* UNIX_DIAG_??? */}, ",
nla->nla_len, UNIX_DIAG_SHUTDOWN + 1 + i);
print_quoted_hex(&nla[i * 2 + 1], NLA_HDRLEN - 1);
printf("}");
}
printf(", ...]}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
msg_len, sprintrc(rc));