cXQsZi5U4Cw ygw6QRf//TcBKoYchSR/oRvV6BTJoy/q+PtdDIa/Y68F2b/w0nHQVXBz+xzwXuQ1LV4tJ7 Ij3gYQ0qmhtnUK6gziq21JbUZFHjxs6rtHLuaaU/Ctv0bLrRrZOfpP6zTaDASig5SdlOtS P1ssgPY42ZnRg24cZ8e8NvLXsj4ozQ1EGNx3X5/UfidXHMKQ0JKkz+EPRP77i1LVWt3Ly0 1JUW1eSOxZni0hanYyDI+mzc80ELAm7pOrv07Lmtnkak/AJ4oMRAvit8UUjMbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1766370233; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LI2RXYSoULI9RYbw8Q1DXQW1VTl1uHUvdn/LsU0jhRs=; b=ok9GBG1aiAAKO4M5PkIhv6s7Eypm/2jAIkzIktFMQP08qXp7ttLbpXcww+WMQ6LBeCAAVF B3TDHmKUvCGu8pVjpmHzLEKdmAYXhGKVqrAdeV9VkdMTrWpyozN3RGpgQs5ydKgzQj5+/h vMebcrzQaF6wGW346AP59ADVC8j1GuIVAWDsk/1L5lStH2awPPRHagP8k4qsJei0FtRuf+ wnO43anvfgauAUB1d4BgfQA+5v5T05TaMiDuiljdlFHw02EZOvvPOEi4Bm1J0EUCq069+z PXTJiGwNFu19uBdo24dEqym1fnZby9+/lwLFqftqZh6T4OTGI7VjPPqIqbwUxQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1766370233; a=rsa-sha256; cv=none; b=hJgkNUSS4T/NcUUv/f7qc39EMGHbfI1E4DHaIkWYDmuSkWOCipMPkwnJUY06/NSFUjTDzY p9XSBDARV77ok/tweBOcv1VnRzjfScqISOGfL0QpiUjb9nFTy/JaS0BpRrsVEVr+2fWoyz CcOBLyjGQYIID8wZHvQ4bqkGv9xCMeJe4Q5dWhxua7ptRbXXYU/M+oEHEvTap+eeoTUz/T xxVyjorEp1mrngVOLAIRgclfrFjf0i4ACj9Vum8xFnhRyW3b0ZGjaoWlPKFMRYA4ftlXeL NqKvrogi0EcXSORCBebkuy67roJVkXfYENEwQwjiJkcjphhyLs490/PVYmUOyA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4dZMQJ6npXz16Dw for ; Mon, 22 Dec 2025 02:23:52 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 37437 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Mon, 22 Dec 2025 02:23:52 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org Cc: Guy Harris From: Gleb Smirnoff Subject: git: 88b04633c29e - main - libpcap: fix resource leaks and set error messages List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: glebius X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 88b04633c29e3a8fd2ac88febd1aedf409ce6925 Auto-Submitted: auto-generated Date: Mon, 22 Dec 2025 02:23:52 +0000 Message-Id: <6948abb8.37437.77874608@gitrepo.freebsd.org> The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=88b04633c29e3a8fd2ac88febd1aedf409ce6925 commit 88b04633c29e3a8fd2ac88febd1aedf409ce6925 Author: Guy Harris AuthorDate: 2025-12-22 01:26:32 +0000 Commit: Gleb Smirnoff CommitDate: 2025-12-22 02:23:28 +0000 libpcap: fix resource leaks and set error messages In finddevs_bpf() close the BPF device descriptor and free the device list buffer before returning. For ioctl() and malloc() errors, use pcapint_fmt_errmsg_for_errno() to generate an error message. Fix some comments. (cherry picked from commit 6870404f35da32d63a0a8789edb010842dd6b208) --- contrib/libpcap/pcap-bpf.c | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/contrib/libpcap/pcap-bpf.c b/contrib/libpcap/pcap-bpf.c index 83a363eee0bd..9ca8df156219 100644 --- a/contrib/libpcap/pcap-bpf.c +++ b/contrib/libpcap/pcap-bpf.c @@ -45,7 +45,7 @@ #if defined(__FreeBSD__) && __FreeBSD_version < 1600006 /* - * Add support for capturing on FreeBSD usbusN interfaces. + * Add support for creating FreeBSD usbusN interfaces as necessary. */ static const char usbus_prefix[] = "usbus"; #define USBUS_PREFIX_LEN (sizeof(usbus_prefix) - 1) @@ -2039,8 +2039,8 @@ pcap_activate_bpf(pcap_t *p) #if defined(__FreeBSD__) && __FreeBSD_version < 1600006 /* - * If this is legacy FreeBSD, and the device name begins with "usbus", - * try to create the interface if it's not available. + * If this is FreeBSD 15 or earlier, and the device name begins + * with "usbus", try to create the interface if it's not available. */ if (strncmp(p->opt.device, usbus_prefix, USBUS_PREFIX_LEN) == 0) { /* @@ -2879,18 +2879,48 @@ finddevs_bpf(pcap_if_list_t *devlistp, char *errbuf) const char *name; int fd; - if ((fd = bpf_open(errbuf)) < 0) + if ((fd = bpf_open(errbuf)) < 0) { + /* + * XXX - this just means we won't have permission to + * open any BPF devices, an thus we don't have + * permission to capture on network interfaces. + */ return (-1); + } memset(&bi, 0, sizeof(bi)); - if (ioctl(fd, BIOCGETIFLIST, (caddr_t)&bi) != 0 || bi.bi_size == 0) + if (ioctl(fd, BIOCGETIFLIST, (caddr_t)&bi) != 0) { + pcapint_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, "BIOCGETIFLIST to get buffer size"); + close(fd); return (-1); + } + if (bi.bi_size == 0) { + /* + * There are no devices attached to BPF. + * This means that, in practice, whatever network + * interfaces we found don't support packet capture + * or injection. + */ + close(fd); + return (0); + } - if ((bi.bi_ubuf = malloc(bi.bi_size)) == NULL) + if ((bi.bi_ubuf = malloc(bi.bi_size)) == NULL) { + pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, errno, + "malloc"); + close(fd); return (-1); + } - if (ioctl(fd, BIOCGETIFLIST, (caddr_t)&bi) != 0) + if (ioctl(fd, BIOCGETIFLIST, (caddr_t)&bi) != 0) { + pcapint_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, "BIOCGETIFLIST to attached devics"); + free(bi.bi_ubuf); + close(fd); return (-1); + } + close(fd); for (name = bi.bi_ubuf; bi.bi_count > 0; bi.bi_count--, name += strlen(name) + 1)