From nobody Tue Jan 02 20:52:38 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4T4Q5z1kK4z56J6k; Tue, 2 Jan 2024 20:52:39 +0000 (UTC) (envelope-from git@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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4T4Q5y61qNz4XhB; Tue, 2 Jan 2024 20:52:38 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1704228758; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=gyBajBEQaQFXqrENpVBmw0rvGSheIwa20fOPVUj+bA4=; b=EczZuKjybh6w5ShNPCB1yIRzYJFNX8WP4z2a+htXHOuQiBW4s+H/3tc9YMlfyIFF7zQ6QR OL69ZcHVwuznnJVJoykSg+6nlTPuRnPQcMiKrg7NweAZIDJ0SSY5auOFCpjBWsPeaKtdKP QNRHq9VyLqsxVlaCXmMW6zBjG7UVYmECt6Nur6yl6MRfFY4o/Q+VipiMzK1A2d/ajFRF0s EsZ/fxF6UEoouKujZarPF4xim0jx7QvTjFlFMkIMjh0rNpT18lVFlwOULoTGyaowxN6Md2 SvNsY8bWGOrNopjscnn9nEVzFhsQhSpz0YRNA0LPOGU8KDaKgmQkUYmEiUiE9Q== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1704228758; a=rsa-sha256; cv=none; b=ccoe2vyUenEdvU700icwWEwr4wWbzcfcMKMtJtE57IdvXbVYjhQjXUdBTJBRUq6F9lfVxv 7ucg1Tno/6TyttVWoGl01rsuiLNjJyOgcfup7VVbaQp8UGq/07lnz4aKLE4jZ9O4uRFyJ6 ApxRe2BGnEH/VhLFz7jXPq/TRe6pMSRpSHOi/DWZLT2/jM+r3hVSTlDbHVqQkFSgmu7MbO DuaC4FRSnMsAjH/gxHGoIktruwYCW+MctC6qz1eXMsUYodutmqYPLS6W3frXTfXNKtGbbo /NQswQDp0QHUPhdKiUwA+B0FCcYyy0BX9TaIC4Q7ofQTdV/usdv7HaD8K2RTyg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1704228758; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=gyBajBEQaQFXqrENpVBmw0rvGSheIwa20fOPVUj+bA4=; b=hiU5RcIgcllhiPGMgB9CferqV/z57+Ga8mBS1A0PMM8k/M9e7lz2UyiIuuZJ3+YhLC7ZDb Au+7kEqdCipO08Li6VtA2GcnVdSQcW05epb9CzvsI9vqZvjN3FAiBEdCTIOUSlkyrJCNEC 2QOdKwXi8+BEE3dXKj+/+5ECGJ8dmH5kjj3CUCVMO7OZNrWlAyv+AHLHclumA+o1udStD/ cTZARoNR75IMXA3EHzC5ny4qf3GuQQyHWsmymOC9528uhybQCVudlU+9u3a3GVM/e2qhrA rKpD6Mc9HgxQx6a7/RKvopFZLVRy2SfMxhZU3ccufWI6RWFBBQU5YeYeN7o1Iw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4T4Q5y56xBz1FXf; Tue, 2 Jan 2024 20:52:38 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 402KqcZH036985; Tue, 2 Jan 2024 20:52:38 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 402Kqcah036982; Tue, 2 Jan 2024 20:52:38 GMT (envelope-from git) Date: Tue, 2 Jan 2024 20:52:38 GMT Message-Id: <202401022052.402Kqcah036982@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: John Baldwin Subject: git: 89806d373aaf - stable/14 - bsdinstall partedit: Avoid potential buffer overflow in newfs_command 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: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 89806d373aafe4e7a107c118132b34c784d8a816 Auto-Submitted: auto-generated The branch stable/14 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=89806d373aafe4e7a107c118132b34c784d8a816 commit 89806d373aafe4e7a107c118132b34c784d8a816 Author: John Baldwin AuthorDate: 2023-10-16 23:25:03 +0000 Commit: John Baldwin CommitDate: 2024-01-02 19:20:37 +0000 bsdinstall partedit: Avoid potential buffer overflow in newfs_command Allocate the buffer holding the newfs command string dynamically (building the string via open_memstream) rather than storing the command into a caller-supplied buffer of unknown length. Reviewed by: emaste Differential Revision: https://reviews.freebsd.org/D42237 (cherry picked from commit ae2fc74fe76ca8b89c5ef0081ef3f4008f83de41) --- usr.sbin/bsdinstall/partedit/gpart_ops.c | 68 +++++++++++++++++++------------- 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/usr.sbin/bsdinstall/partedit/gpart_ops.c b/usr.sbin/bsdinstall/partedit/gpart_ops.c index 8b75d2d6d859..65f53f31545f 100644 --- a/usr.sbin/bsdinstall/partedit/gpart_ops.c +++ b/usr.sbin/bsdinstall/partedit/gpart_ops.c @@ -86,12 +86,16 @@ scheme_supports_labels(const char *scheme) return (0); } -static void -newfs_command(const char *fstype, char *command, int use_default) +static char * +newfs_command(const char *fstype, int use_default) { struct bsddialog_conf conf; + FILE *fp; + char *buf; + size_t len; bsddialog_initconf(&conf); + fp = open_memstream(&buf, &len); if (strcmp(fstype, "freebsd-ufs") == 0) { int i; @@ -115,21 +119,21 @@ newfs_command(const char *fstype, char *command, int use_default) choice = bsddialog_checklist(&conf, "", 0, 0, 0, nitems(items), items, NULL); if (choice == BSDDIALOG_CANCEL) - return; + goto out; } - strcpy(command, "newfs "); + fputs("newfs ", fp); for (i = 0; i < (int)nitems(items); i++) { if (items[i].on == false) continue; if (strcmp(items[i].name, "UFS1") == 0) - strcat(command, "-O1 "); + fputs("-O1 ", fp); else if (strcmp(items[i].name, "SU") == 0) - strcat(command, "-U "); + fputs("-U ", fp); else if (strcmp(items[i].name, "SUJ") == 0) - strcat(command, "-j "); + fputs("-j ", fp); else if (strcmp(items[i].name, "TRIM") == 0) - strcat(command, "-t "); + fputs("-t ", fp); } } else if (strcmp(fstype, "freebsd-zfs") == 0) { int i; @@ -153,30 +157,31 @@ newfs_command(const char *fstype, char *command, int use_default) choice = bsddialog_checklist(&conf, "", 0, 0, 0, nitems(items), items, NULL); if (choice == BSDDIALOG_CANCEL) - return; + goto out; } - strcpy(command, "zpool create -f -m none "); + fputs("zpool create -f -m none ", fp); if (getenv("BSDINSTALL_TMPBOOT") != NULL) { char zfsboot_path[MAXPATHLEN]; + snprintf(zfsboot_path, sizeof(zfsboot_path), "%s/zfs", getenv("BSDINSTALL_TMPBOOT")); mkdir(zfsboot_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); - sprintf(command, "%s -o cachefile=%s/zpool.cache ", - command, zfsboot_path); + fprintf(fp, " -o cachefile=%s/zpool.cache ", + zfsboot_path); } for (i = 0; i < (int)nitems(items); i++) { if (items[i].on == false) continue; if (strcmp(items[i].name, "fletcher4") == 0) - strcat(command, "-O checksum=fletcher4 "); + fputs("-O checksum=fletcher4 ", fp); else if (strcmp(items[i].name, "fletcher2") == 0) - strcat(command, "-O checksum=fletcher2 "); + fputs("-O checksum=fletcher2 ", fp); else if (strcmp(items[i].name, "sha256") == 0) - strcat(command, "-O checksum=sha256 "); + fputs("-O checksum=sha256 ", fp); else if (strcmp(items[i].name, "atime") == 0) - strcat(command, "-O atime=off "); + fputs("-O atime=off ", fp); } } else if (strcmp(fstype, "fat32") == 0 || strcmp(fstype, "efi") == 0 || strcmp(fstype, "ms-basic-data") == 0) { @@ -196,19 +201,19 @@ newfs_command(const char *fstype, char *command, int use_default) choice = bsddialog_radiolist(&conf, "", 0, 0, 0, nitems(items), items, NULL); if (choice == BSDDIALOG_CANCEL) - return; + goto out; } - strcpy(command, "newfs_msdos "); + fputs("newfs_msdos ", fp); for (i = 0; i < (int)nitems(items); i++) { if (items[i].on == false) continue; if (strcmp(items[i].name, "FAT32") == 0) - strcat(command, "-F 32 -c 1"); + fputs("-F 32 -c 1", fp); else if (strcmp(items[i].name, "FAT16") == 0) - strcat(command, "-F 16 "); + fputs("-F 16 ", fp); else if (strcmp(items[i].name, "FAT12") == 0) - strcat(command, "-F 12 "); + fputs("-F 12 ", fp); } } else { if (!use_default) { @@ -216,8 +221,11 @@ newfs_command(const char *fstype, char *command, int use_default) bsddialog_msgbox(&conf, "No configurable options exist " "for this filesystem.", 0, 0); } - command[0] = '\0'; } + +out: + fclose(fp); + return (buf); } const char * @@ -539,7 +547,7 @@ gpart_edit(struct gprovider *pp) const char *errstr, *oldtype, *scheme; struct partition_metadata *md; char sizestr[32]; - char newfs[255]; + char *newfs; intmax_t idx; int hadlabel, choice, nitems; unsigned i; @@ -686,10 +694,11 @@ editpart: } gctl_free(r); - newfs_command(items[0].value, newfs, 1); + newfs = newfs_command(items[0].value, 1); set_default_part_metadata(pp->lg_name, scheme, items[0].value, items[2].value, (strcmp(oldtype, items[0].value) != 0) ? newfs : NULL); + free(newfs); endedit: if (strcmp(oldtype, items[0].value) != 0 && cp != NULL) @@ -1021,7 +1030,7 @@ gpart_create(struct gprovider *pp, const char *default_type, struct ggeom *geom; const char *errstr, *scheme; char sizestr[32], startstr[32], output[64], *newpartname; - char newfs[255], options_fstype[64]; + char *newfs, options_fstype[64]; intmax_t maxsize, size, sector, firstfree, stripe; uint64_t bytes; int nitems, choice, junk; @@ -1122,7 +1131,7 @@ gpart_create(struct gprovider *pp, const char *default_type, /* Default options */ strncpy(options_fstype, items[0].init, sizeof(options_fstype)); - newfs_command(options_fstype, newfs, 1); + newfs = newfs_command(options_fstype, 1); init_allocated = false; addpartform: @@ -1141,9 +1150,10 @@ addpartform: case BSDDIALOG_CANCEL: return; case BSDDIALOG_EXTRA: /* Options */ + free(newfs); strncpy(options_fstype, items[0].value, sizeof(options_fstype)); - newfs_command(options_fstype, newfs, 0); + newfs = newfs_command(options_fstype, 0); for (i = 0; i < nitems(items); i++) { if (init_allocated) free((char*)items[i].init); @@ -1165,8 +1175,9 @@ addpartform: * their choices in favor of the new filesystem's defaults. */ if (strcmp(options_fstype, items[0].value) != 0) { + free(newfs); strncpy(options_fstype, items[0].value, sizeof(options_fstype)); - newfs_command(options_fstype, newfs, 1); + newfs = newfs_command(options_fstype, 1); } size = maxsize; @@ -1318,6 +1329,7 @@ addpartform: else set_default_part_metadata(newpartname, scheme, items[0].value, items[2].value, newfs); + free(newfs); for (i = 0; i < nitems(items); i++) { if (items[i].value != NULL) {