From 2b4610c75ec8fbb0ea9b77107dd77869833ef0fe Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Thu, 29 Dec 2016 18:12:33 -0500 Subject: [PATCH] vunshare: check socket() return --- vunshare.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/vunshare.c b/vunshare.c index 274eebc..2e7efbb 100644 --- a/vunshare.c +++ b/vunshare.c @@ -51,14 +51,19 @@ static void unshare_net(void) if (!vunshare(CLONE_NEWNET)) return; - int sock = socket(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0); + int sock; struct ifreq ifr; + sock = socket(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0); + if (sock < 0) + err(1, "socket(AF_LOCAL) failed"); + /* Equiv of `ip link set up lo`. Kernel will assign 127.0.0.1 for us. */ strcpy(ifr.ifr_name, "lo"); if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) err(1, "ioctl(SIOCGIFFLAGS) failed"); - strcpy(ifr.ifr_name, "lo"); + + /* The kernel preserves ifr.ifr_name for use. */ ifr.ifr_flags |= IFF_UP | IFF_RUNNING; if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) err(1, "ioctl(SIOCSIFFLAGS) failed"); -- 2.39.5