summaryrefslogtreecommitdiff
path: root/tests/netlink_sock_diag.c
diff options
context:
space:
mode:
authorEugene Syromyatnikov <evgsyr@gmail.com>2018-05-08 07:10:16 +0200
committerDmitry V. Levin <ldv@altlinux.org>2018-05-09 22:01:45 +0000
commit346e8002b00d6af4b53c2fd341d79906b3f8f235 (patch)
tree39fe826774bcd4db09173483dda3d6b8744dcaf2 /tests/netlink_sock_diag.c
parentafc49df0b09e46eb0bb25c657b5471f50e90815a (diff)
downloadstrace-346e8002b00d6af4b53c2fd341d79906b3f8f235.tar.gz
tests: introduce midtail_alloc and use it in netlink tests
netlink tests happen to access memory located before the tail_malloc'ed pointers, a practice that doesn't go well with the latest compilers because tail_malloc is marked with ATTRIBUTE_MALLOC. For example, glibc in -D_FORTIFY_SOURCE=2 mode and gcc 8 with -Warray-bounds enabled complain about negative offsets out of bounds. Fix this issue by introducing midtail_alloc. * tests/tests.h (midtail_alloc): New macro. * tests/netlink_crypto.c: Use it instead of tail_malloc for nlh0 allocation. * tests/netlink_netfilter.c: Likewise. * tests/netlink_protocol.c: Likewise. * tests/netlink_route.c: Likewise. * tests/netlink_selinux.c: Likewise. * tests/netlink_sock_diag.c: Likewise. * tests/nlattr_br_port_msg.c: Likewise. * tests/nlattr_crypto_user_alg.c: Likewise. * tests/nlattr_dcbmsg.c: Likewise. * tests/nlattr_fib_rule_hdr.c: Likewise. * tests/nlattr_ifaddrlblmsg.c: Likewise. * tests/nlattr_ifaddrmsg.c: Likewise. * tests/nlattr_ifinfomsg.c: Likewise. * tests/nlattr_ifla_brport.c: Likewise. * tests/nlattr_ifla_port.c: Likewise. * tests/nlattr_ifla_xdp.c: Likewise. * tests/nlattr_inet_diag_msg.c: Likewise. * tests/nlattr_inet_diag_req_compat.c: Likewise. * tests/nlattr_inet_diag_req_v2.c: Likewise. * tests/nlattr_mdba_mdb_entry.c: Likewise. * tests/nlattr_mdba_router_port.c: Likewise. * tests/nlattr_ndmsg.c: Likewise. * tests/nlattr_ndtmsg.c: Likewise. * tests/nlattr_netconfmsg.c: Likewise. * tests/nlattr_netlink_diag_msg.c: Likewise. * tests/nlattr_nlmsgerr.c: Likewise. * tests/nlattr_packet_diag_msg.c: Likewise. * tests/nlattr_rtgenmsg.c: Likewise. * tests/nlattr_rtmsg.c: Likewise. * tests/nlattr_smc_diag_msg.c: Likewise. * tests/nlattr_tc_stats.c: Likewise. * tests/nlattr_tca_stab.c: Likewise. * tests/nlattr_tcamsg.c: Likewise. * tests/nlattr_tcmsg.c: Likewise. * tests/nlattr_unix_diag_msg.c: Likewise. Co-Authored-by: Dmitry V. Levin <ldv@altlinux.org>
Diffstat (limited to 'tests/netlink_sock_diag.c')
-rw-r--r--tests/netlink_sock_diag.c36
1 files changed, 18 insertions, 18 deletions
diff --git a/tests/netlink_sock_diag.c b/tests/netlink_sock_diag.c
index c2ebf129c..c9e1232f9 100644
--- a/tests/netlink_sock_diag.c
+++ b/tests/netlink_sock_diag.c
@@ -127,10 +127,11 @@ test_nlmsg_flags(const int fd)
static void
test_odd_family_req(const int fd)
{
- void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
+ uint8_t family = 0;
+ char buf[sizeof(family) + 4];
+ void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(buf));
/* unspecified family only */
- uint8_t family = 0;
TEST_NETLINK(fd, nlh0,
SOCK_DIAG_BY_FAMILY,
NLM_F_REQUEST,
@@ -153,7 +154,6 @@ test_odd_family_req(const int fd)
printf("%p", NLMSG_DATA(TEST_NETLINK_nlh)));
/* unspecified family and string */
- char buf[sizeof(family) + 4];
family = 0;
memcpy(buf, &family, sizeof(family));
memcpy(buf + sizeof(family), "1234", 4);
@@ -177,10 +177,11 @@ test_odd_family_req(const int fd)
static void
test_odd_family_msg(const int fd)
{
- void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
+ uint8_t family = 0;
+ char buf[sizeof(family) + 4];
+ void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(buf));
/* unspecified family only */
- uint8_t family = 0;
TEST_NETLINK(fd, nlh0,
SOCK_DIAG_BY_FAMILY, NLM_F_DUMP,
sizeof(family), &family, sizeof(family),
@@ -200,7 +201,6 @@ test_odd_family_msg(const int fd)
printf("%p", NLMSG_DATA(TEST_NETLINK_nlh)));
/* unspecified family and string */
- char buf[sizeof(family) + 4];
family = 0;
memcpy(buf, &family, sizeof(family));
memcpy(buf + sizeof(family), "1234", 4);
@@ -222,7 +222,6 @@ test_odd_family_msg(const int fd)
static void
test_unix_diag_req(const int fd)
{
- void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
static const struct unix_diag_req req = {
.sdiag_family = AF_UNIX,
.sdiag_protocol = 253,
@@ -231,6 +230,7 @@ test_unix_diag_req(const int fd)
.udiag_show = UDIAG_SHOW_NAME,
.udiag_cookie = { 0xdeadbeef, 0xbadc0ded }
};
+ void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(req));
TEST_SOCK_DIAG(fd, nlh0, AF_UNIX,
SOCK_DIAG_BY_FAMILY, NLM_F_REQUEST, req,
printf("{sdiag_family=AF_UNIX"),
@@ -245,7 +245,6 @@ test_unix_diag_req(const int fd)
static void
test_unix_diag_msg(const int fd)
{
- void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
static const struct unix_diag_msg msg = {
.udiag_family = AF_UNIX,
.udiag_type = SOCK_STREAM,
@@ -253,6 +252,7 @@ test_unix_diag_msg(const int fd)
.udiag_ino = 0xfacefeed,
.udiag_cookie = { 0xdeadbeef, 0xbadc0ded }
};
+ void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));
TEST_SOCK_DIAG(fd, nlh0, AF_UNIX,
SOCK_DIAG_BY_FAMILY, NLM_F_DUMP, msg,
printf("{udiag_family=AF_UNIX"),
@@ -266,7 +266,6 @@ test_unix_diag_msg(const int fd)
static void
test_netlink_diag_req(const int fd)
{
- void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
struct netlink_diag_req req = {
.sdiag_family = AF_NETLINK,
.sdiag_protocol = NDIAG_PROTO_ALL,
@@ -274,6 +273,7 @@ test_netlink_diag_req(const int fd)
.ndiag_show = NDIAG_SHOW_MEMINFO,
.ndiag_cookie = { 0xdeadbeef, 0xbadc0ded }
};
+ void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(req));
TEST_SOCK_DIAG(fd, nlh0, AF_NETLINK,
SOCK_DIAG_BY_FAMILY, NLM_F_REQUEST, req,
printf("{sdiag_family=AF_NETLINK"),
@@ -298,7 +298,6 @@ test_netlink_diag_req(const int fd)
static void
test_netlink_diag_msg(const int fd)
{
- void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
static const struct netlink_diag_msg msg = {
.ndiag_family = AF_NETLINK,
.ndiag_type = SOCK_RAW,
@@ -310,6 +309,7 @@ test_netlink_diag_msg(const int fd)
.ndiag_ino = 0xdaeefacd,
.ndiag_cookie = { 0xbadc0ded, 0xdeadbeef }
};
+ void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));
TEST_SOCK_DIAG(fd, nlh0, AF_NETLINK,
SOCK_DIAG_BY_FAMILY, NLM_F_DUMP, msg,
printf("{ndiag_family=AF_NETLINK"),
@@ -327,7 +327,6 @@ test_netlink_diag_msg(const int fd)
static void
test_packet_diag_req(const int fd)
{
- void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
static const struct packet_diag_req req = {
.sdiag_family = AF_PACKET,
.sdiag_protocol = ETH_P_LOOP,
@@ -335,6 +334,7 @@ test_packet_diag_req(const int fd)
.pdiag_show = PACKET_SHOW_INFO,
.pdiag_cookie = { 0xdeadbeef, 0xbadc0ded }
};
+ void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(req));
TEST_SOCK_DIAG(fd, nlh0, AF_PACKET,
SOCK_DIAG_BY_FAMILY, NLM_F_REQUEST, req,
printf("{sdiag_family=AF_PACKET"),
@@ -348,7 +348,6 @@ test_packet_diag_req(const int fd)
static void
test_packet_diag_msg(const int fd)
{
- void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
static const struct packet_diag_msg msg = {
.pdiag_family = AF_PACKET,
.pdiag_type = SOCK_STREAM,
@@ -356,6 +355,7 @@ test_packet_diag_msg(const int fd)
.pdiag_ino = 0xfacefeed,
.pdiag_cookie = { 0xdeadbeef, 0xbadc0ded }
};
+ void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));
TEST_SOCK_DIAG(fd, nlh0, AF_PACKET,
SOCK_DIAG_BY_FAMILY, NLM_F_DUMP, msg,
printf("{pdiag_family=AF_PACKET"),
@@ -371,7 +371,6 @@ test_inet_diag_sockid(const int fd)
{
const char address[] = "12.34.56.78";
const char address6[] = "12:34:56:78:90:ab:cd:ef";
- void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
struct inet_diag_req_v2 req = {
.sdiag_family = AF_INET,
.idiag_ext = 1 << (INET_DIAG_CONG - 1),
@@ -384,6 +383,7 @@ test_inet_diag_sockid(const int fd)
.idiag_cookie = { 0xdeadbeef, 0xbadc0ded }
},
};
+ void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(req));
if (!inet_pton(AF_INET, address, &req.id.idiag_src) ||
!inet_pton(AF_INET, address, &req.id.idiag_dst))
@@ -435,7 +435,6 @@ static void
test_inet_diag_req(const int fd)
{
const char address[] = "12.34.56.78";
- void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
struct inet_diag_req req = {
.idiag_family = AF_INET,
.idiag_src_len = 0xde,
@@ -450,6 +449,7 @@ test_inet_diag_req(const int fd)
.idiag_states = 1 << TCP_LAST_ACK,
.idiag_dbs = 0xfacefeed,
};
+ void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(req));
if (!inet_pton(AF_INET, address, &req.id.idiag_src) ||
!inet_pton(AF_INET, address, &req.id.idiag_dst))
@@ -479,7 +479,6 @@ static void
test_inet_diag_req_v2(const int fd)
{
const char address[] = "87.65.43.21";
- void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
struct inet_diag_req_v2 req = {
.sdiag_family = AF_INET,
.idiag_ext = 1 << (INET_DIAG_CONG - 1),
@@ -492,6 +491,7 @@ test_inet_diag_req_v2(const int fd)
.idiag_cookie = { 0xdeadbeef, 0xbadc0ded }
},
};
+ void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(req));
if (!inet_pton(AF_INET, address, &req.id.idiag_src) ||
!inet_pton(AF_INET, address, &req.id.idiag_dst))
@@ -519,7 +519,6 @@ static void
test_inet_diag_msg(const int fd)
{
const char address[] = "11.22.33.44";
- void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
struct inet_diag_msg msg = {
.idiag_family = AF_INET,
.idiag_state = TCP_LISTEN,
@@ -537,6 +536,7 @@ test_inet_diag_msg(const int fd)
.idiag_uid = 0xdecefaeb,
.idiag_inode = 0xbadc0ded,
};
+ void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));
if (!inet_pton(AF_INET, address, &msg.id.idiag_src) ||
!inet_pton(AF_INET, address, &msg.id.idiag_dst))
@@ -570,7 +570,6 @@ static void
test_smc_diag_req(const int fd)
{
const char address[] = "43.21.56.78";
- void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
struct smc_diag_req req = {
.diag_family = AF_SMC,
.diag_ext = 1 << (SMC_DIAG_CONNINFO - 1),
@@ -581,6 +580,7 @@ test_smc_diag_req(const int fd)
.idiag_cookie = { 0xdeadbeef, 0xbadc0ded },
},
};
+ void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(req));
if (!inet_pton(AF_INET, address, &req.id.idiag_src) ||
!inet_pton(AF_INET, address, &req.id.idiag_dst))
@@ -606,7 +606,6 @@ static void
test_smc_diag_msg(const int fd)
{
const char address[] = "34.87.12.90";
- void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
struct smc_diag_msg msg = {
.diag_family = AF_SMC,
.diag_state = SMC_ACTIVE,
@@ -621,6 +620,7 @@ test_smc_diag_msg(const int fd)
.diag_uid = 0xadcdfafc,
.diag_inode = 0xbadc0ded,
};
+ void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));
if (!inet_pton(AF_INET, address, &msg.id.idiag_src) ||
!inet_pton(AF_INET, address, &msg.id.idiag_dst))