From 02e21af657abff93319bffe1c7c0272245a6c0d1 Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Thu, 14 Dec 2017 13:48:12 +0100 Subject: tun.c: tun_addaddr: Fix segfault and wrong usage of tun_nlattr First of all, dstaddr can be NULL, avoid copying it in that case. Second, we want to copy the addr data, not the pointer. I tested it and the IP was not added (not shown in ip addr) until I copied the content instead of the address. Change-Id: I8da637b155f0e913cab6c5b0dde355c9f33375b5 --- lib/tun.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/tun.c b/lib/tun.c index d8e4b62..3081575 100644 --- a/lib/tun.c +++ b/lib/tun.c @@ -333,8 +333,9 @@ int tun_addaddr(struct tun_t *this, return -1; } - tun_nlattr(&req.n, sizeof(req), IFA_ADDRESS, addr, sizeof(addr)); - tun_nlattr(&req.n, sizeof(req), IFA_LOCAL, dstaddr, sizeof(dstaddr)); + tun_nlattr(&req.n, sizeof(req), IFA_ADDRESS, addr, sizeof(*addr)); + if (dstaddr) + tun_nlattr(&req.n, sizeof(req), IFA_LOCAL, dstaddr, sizeof(*dstaddr)); if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) { SYS_ERR(DTUN, LOGL_ERROR, errno, "socket() failed"); -- cgit v1.2.3