From owner-svn-src-all@freebsd.org Thu Oct 15 18:03:14 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id BB521443FB4; Thu, 15 Oct 2020 18:03:14 +0000 (UTC) (envelope-from jrtc27@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4CBxwL4X0Pz3YSW; Thu, 15 Oct 2020 18:03:14 +0000 (UTC) (envelope-from jrtc27@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 mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 7EAF223927; Thu, 15 Oct 2020 18:03:14 +0000 (UTC) (envelope-from jrtc27@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 09FI3EPt026263; Thu, 15 Oct 2020 18:03:14 GMT (envelope-from jrtc27@FreeBSD.org) Received: (from jrtc27@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 09FI3E8I026262; Thu, 15 Oct 2020 18:03:14 GMT (envelope-from jrtc27@FreeBSD.org) Message-Id: <202010151803.09FI3E8I026262@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jrtc27 set sender to jrtc27@FreeBSD.org using -f From: Jessica Clarke Date: Thu, 15 Oct 2020 18:03:14 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r366736 - head/usr.sbin/kldxref X-SVN-Group: head X-SVN-Commit-Author: jrtc27 X-SVN-Commit-Paths: head/usr.sbin/kldxref X-SVN-Commit-Revision: 366736 X-SVN-Commit-Repository: base 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.33 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: Thu, 15 Oct 2020 18:03:14 -0000 Author: jrtc27 Date: Thu Oct 15 18:03:14 2020 New Revision: 366736 URL: https://svnweb.freebsd.org/changeset/base/366736 Log: kldxref: Avoid buffer overflows in parse_pnp_list We convert a string like "W32:vendor/device" into "I:vendor;I:device", where the output is longer than the input, but only allocate space equal to the length of the input, leading to a buffer overflow. Instead use open_memstream so we get a safe dynamically-grown buffer. Found by: CHERI Reviewed by: imp, jhb (mentor) Approved by: imp, jhb (mentor) Obtained from: CheriBSD Differential Revision: https://reviews.freebsd.org/D26637 Modified: head/usr.sbin/kldxref/kldxref.c Modified: head/usr.sbin/kldxref/kldxref.c ============================================================================== --- head/usr.sbin/kldxref/kldxref.c Thu Oct 15 17:44:17 2020 (r366735) +++ head/usr.sbin/kldxref/kldxref.c Thu Oct 15 18:03:14 2020 (r366736) @@ -235,14 +235,17 @@ parse_pnp_list(const char *desc, char **new_desc, pnp_ const char *walker, *ep; const char *colon, *semi; struct pnp_elt *elt; - char *nd; char type[8], key[32]; int off; + size_t new_desc_size; + FILE *fp; walker = desc; ep = desc + strlen(desc); off = 0; - nd = *new_desc = malloc(strlen(desc) + 1); + fp = open_memstream(new_desc, &new_desc_size); + if (fp == NULL) + err(1, "Could not open new memory stream"); if (verbose > 1) printf("Converting %s into a list\n", desc); while (walker < ep) { @@ -336,42 +339,44 @@ parse_pnp_list(const char *desc, char **new_desc, pnp_ off = elt->pe_offset + sizeof(void *); } if (elt->pe_kind & TYPE_PAIRED) { - char *word, *ctx; + char *word, *ctx, newtype; for (word = strtok_r(key, "/", &ctx); word; word = strtok_r(NULL, "/", &ctx)) { - sprintf(nd, "%c:%s;", elt->pe_kind & TYPE_FLAGGED ? 'J' : 'I', - word); - nd += strlen(nd); + newtype = elt->pe_kind & TYPE_FLAGGED ? 'J' : 'I'; + fprintf(fp, "%c:%s;", newtype, word); } - } else { + char newtype; + if (elt->pe_kind & TYPE_FLAGGED) - *nd++ = 'J'; + newtype = 'J'; else if (elt->pe_kind & TYPE_GE) - *nd++ = 'G'; + newtype = 'G'; else if (elt->pe_kind & TYPE_LE) - *nd++ = 'L'; + newtype = 'L'; else if (elt->pe_kind & TYPE_MASK) - *nd++ = 'M'; + newtype = 'M'; else if (elt->pe_kind & TYPE_INT) - *nd++ = 'I'; + newtype = 'I'; else if (elt->pe_kind == TYPE_D) - *nd++ = 'D'; + newtype = 'D'; else if (elt->pe_kind == TYPE_Z || elt->pe_kind == TYPE_E) - *nd++ = 'Z'; + newtype = 'Z'; else if (elt->pe_kind == TYPE_T) - *nd++ = 'T'; + newtype = 'T'; else errx(1, "Impossible type %x\n", elt->pe_kind); - *nd++ = ':'; - strcpy(nd, key); - nd += strlen(nd); - *nd++ = ';'; + fprintf(fp, "%c:%s;", newtype, key); } } - *nd++ = '\0'; + if (ferror(fp) != 0) { + fclose(fp); + errx(1, "Exhausted space converting description %s", desc); + } + if (fclose(fp) != 0) + errx(1, "Failed to close memory stream"); return (0); err: errx(1, "Parse error of description string %s", desc);