From owner-svn-src-all@freebsd.org Wed Sep 21 13:02:44 2016 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 3BDA5BE1B62; Wed, 21 Sep 2016 13:02:44 +0000 (UTC) (envelope-from ed@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 002BFB23; Wed, 21 Sep 2016 13:02:43 +0000 (UTC) (envelope-from ed@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u8LD2h7w058197; Wed, 21 Sep 2016 13:02:43 GMT (envelope-from ed@FreeBSD.org) Received: (from ed@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u8LD2hBS058196; Wed, 21 Sep 2016 13:02:43 GMT (envelope-from ed@FreeBSD.org) Message-Id: <201609211302.u8LD2hBS058196@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: ed set sender to ed@FreeBSD.org using -f From: Ed Schouten Date: Wed, 21 Sep 2016 13:02:43 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r306099 - head/usr.sbin/bhyve X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 21 Sep 2016 13:02:44 -0000 Author: ed Date: Wed Sep 21 13:02:43 2016 New Revision: 306099 URL: https://svnweb.freebsd.org/changeset/base/306099 Log: Fix misuse of the basename() and dirname() functions. These functions are allowed to overwrite their input. Pull a copy of the input parameter and call dirname() and basename() on that instead. Do ensure that we reload the pathname value between calls. Modified: head/usr.sbin/bhyve/pci_virtio_console.c Modified: head/usr.sbin/bhyve/pci_virtio_console.c ============================================================================== --- head/usr.sbin/bhyve/pci_virtio_console.c Wed Sep 21 11:59:52 2016 (r306098) +++ head/usr.sbin/bhyve/pci_virtio_console.c Wed Sep 21 13:02:43 2016 (r306099) @@ -264,6 +264,7 @@ pci_vtcon_sock_add(struct pci_vtcon_soft { struct pci_vtcon_sock *sock; struct sockaddr_un sun; + char *pathcopy; int s = -1, fd = -1, error = 0; sock = calloc(1, sizeof(struct pci_vtcon_sock)); @@ -278,15 +279,24 @@ pci_vtcon_sock_add(struct pci_vtcon_soft goto out; } - fd = open(dirname(path), O_RDONLY | O_DIRECTORY); + pathcopy = strdup(path); + if (pathcopy == NULL) { + error = -1; + goto out; + } + + fd = open(dirname(pathcopy), O_RDONLY | O_DIRECTORY); if (fd < 0) { + free(pathcopy); error = -1; goto out; } sun.sun_family = AF_UNIX; sun.sun_len = sizeof(struct sockaddr_un); - strncpy(sun.sun_path, basename((char *)path), sizeof(sun.sun_path)); + strcpy(pathcopy, path); + strncpy(sun.sun_path, basename(pathcopy), sizeof(sun.sun_path)); + free(pathcopy); if (bindat(fd, s, (struct sockaddr *)&sun, sun.sun_len) < 0) { error = -1;