From dc16e68bc523583e5ac5abc218892576a4c402d1 Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Wed, 12 Jul 2017 21:08:03 +0000 Subject: [PATCH] netlink_sock_diag: enhance decoding of long meminfo arrays Print trailing dots instead of silent truncation if the array is too long. * netlink_sock_diag.c (decode_meminfo): Do not apply SK_MEMINFO_VARS limit to nmemb, specify element count to print_array instead. (print_meminfo): Check element count and terminate printing if the element count exceeds SK_MEMINFO_VARS. * tests/nlattr_inet_diag_msg.c (main): Check it. --- netlink_sock_diag.c | 13 +++++++++---- tests/nlattr_inet_diag_msg.c | 12 ++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/netlink_sock_diag.c b/netlink_sock_diag.c index f0cb500b..44ec34a1 100644 --- a/netlink_sock_diag.c +++ b/netlink_sock_diag.c @@ -120,6 +120,13 @@ print_meminfo(struct tcb *const tcp, const size_t elem_size, void *const opaque_data) { + unsigned int *const count = opaque_data; + + if ((*count)++ >= SK_MEMINFO_VARS) { + tprints("..."); + return false; + } + tprintf("%" PRIu32, *(uint32_t *) elem_buf); return true; @@ -137,11 +144,9 @@ decode_meminfo(struct tcb *const tcp, if (!nmemb) return false; - if (nmemb > SK_MEMINFO_VARS) - nmemb = SK_MEMINFO_VARS; - + unsigned int count = 0; print_array(tcp, addr, nmemb, &mem, sizeof(mem), - umoven_or_printaddr, print_meminfo, 0); + umoven_or_printaddr, print_meminfo, &count); return true; } diff --git a/tests/nlattr_inet_diag_msg.c b/tests/nlattr_inet_diag_msg.c index ca1eedd8..6a8ffa27 100644 --- a/tests/nlattr_inet_diag_msg.c +++ b/tests/nlattr_inet_diag_msg.c @@ -169,6 +169,18 @@ main(void) init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_SKMEMINFO, pattern, mem, print_uint); + static uint32_t bigmem[SK_MEMINFO_VARS + 1]; + memcpy(bigmem, pattern, sizeof(bigmem)); + + TEST_NLATTR(fd, nlh0, hdrlen, init_inet_diag_msg, print_inet_diag_msg, + INET_DIAG_SKMEMINFO, sizeof(bigmem), bigmem, sizeof(bigmem), + size_t i; + for (i = 0; i < SK_MEMINFO_VARS; ++i) { + printf(i ? ", " : "["); + print_uint(&bigmem[i]); + } + printf(", ...]")); + static const uint32_t mark = 0xabdfadca; TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_inet_diag_msg, print_inet_diag_msg, -- 2.40.0