From 2d5fe36980f2bb5666e85d1a433ea7e38d78092a Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Fri, 12 Jul 2019 18:50:46 +0000 Subject: [PATCH] usr.sbin/bhyve: close backend file descriptor during tap init error Coverity CID: 1402953 Reviewed by: scottl, markj, aleksandr.fedorov -at- itglobal.com Approved by: vmaffione, jhb Differential Revision: https://reviews.freebsd.org/D20913 --- usr.sbin/bhyve/net_backends.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/usr.sbin/bhyve/net_backends.c b/usr.sbin/bhyve/net_backends.c index a34b110eda3..88afaca4b1c 100644 --- a/usr.sbin/bhyve/net_backends.c +++ b/usr.sbin/bhyve/net_backends.c @@ -175,7 +175,6 @@ tap_init(struct net_backend *be, const char *devname, { struct tap_priv *priv = (struct tap_priv *)be->opaque; char tbuf[80]; - int fd; int opt = 1; #ifndef WITHOUT_CAPSICUM cap_rights_t rights; @@ -189,8 +188,8 @@ tap_init(struct net_backend *be, const char *devname, strcpy(tbuf, "/dev/"); strlcat(tbuf, devname, sizeof(tbuf)); - fd = open(tbuf, O_RDWR); - if (fd == -1) { + be->fd = open(tbuf, O_RDWR); + if (be->fd == -1) { WPRINTF(("open of tap device %s failed\n", tbuf)); goto error; } @@ -199,25 +198,23 @@ tap_init(struct net_backend *be, const char *devname, * Set non-blocking and register for read * notifications with the event loop */ - if (ioctl(fd, FIONBIO, &opt) < 0) { + if (ioctl(be->fd, FIONBIO, &opt) < 0) { WPRINTF(("tap device O_NONBLOCK failed\n")); goto error; } #ifndef WITHOUT_CAPSICUM cap_rights_init(&rights, CAP_EVENT, CAP_READ, CAP_WRITE); - if (caph_rights_limit(fd, &rights) == -1) + if (caph_rights_limit(be->fd, &rights) == -1) errx(EX_OSERR, "Unable to apply rights for sandbox"); #endif - priv->mevp = mevent_add(fd, EVF_READ, cb, param); + priv->mevp = mevent_add(be->fd, EVF_READ, cb, param); if (priv->mevp == NULL) { WPRINTF(("Could not register event\n")); goto error; } - be->fd = fd; - return (0); error: