Module Name:    src
Committed By:   bouyer
Date:           Mon Dec  9 19:31:18 UTC 2019

Modified Files:
        src/usr.sbin/sysinst [netbsd-9]: disklabel.c

Log Message:
Pull up following revision(s) (requested by martin in ticket #537):
        usr.sbin/sysinst/disklabel.c: revision 1.16
        usr.sbin/sysinst/disklabel.c: revision 1.17
Sanitize disk type and packname a bit more - when using existing disklabel
partitions we might run into trouble later when filing this label (unescaped)
in disktab format otherwise.
Relax restrictions on packnames, as disklabel(8) does not do full decoding
for the tag field.
Fix quoting of command args.


To generate a diff of this commit:
cvs rdiff -u -r1.10.2.5 -r1.10.2.6 src/usr.sbin/sysinst/disklabel.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/sysinst/disklabel.c
diff -u src/usr.sbin/sysinst/disklabel.c:1.10.2.5 src/usr.sbin/sysinst/disklabel.c:1.10.2.6
--- src/usr.sbin/sysinst/disklabel.c:1.10.2.5	Sun Nov 17 13:45:26 2019
+++ src/usr.sbin/sysinst/disklabel.c	Mon Dec  9 19:31:18 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: disklabel.c,v 1.10.2.5 2019/11/17 13:45:26 msaitoh Exp $	*/
+/*	$NetBSD: disklabel.c,v 1.10.2.6 2019/12/09 19:31:18 bouyer Exp $	*/
 
 /*
  * Copyright 2018 The NetBSD Foundation, Inc.
@@ -262,13 +262,32 @@ disklabel_parts_read(const char *disk, d
 	return &parts->dp;
 }
 
+/*
+ * Escape a string for usage as a tag name in a capfile(5),
+ * we really know there is enough space in the destination buffer...
+ */
+static void
+escape_capfile(char *dest, const char *src, size_t len)
+{
+	while (*src && len > 0) {
+		if (*src == ':')
+			*dest++ = ' ';
+		else
+			*dest++ = *src;
+		src++;
+		len--;
+	}
+	*dest = 0;
+}
+
 static bool
 disklabel_write_to_disk(struct disk_partitions *arg)
 {
 	struct disklabel_disk_partitions *parts =
 	    (struct disklabel_disk_partitions*)arg;
 	FILE *f;
-	char fname[PATH_MAX], packname[sizeof(parts->l.d_packname)+1];
+	char fname[PATH_MAX], packname[sizeof(parts->l.d_packname)+1],
+	    disktype[sizeof(parts->l.d_typename)+1];
 	int i, rv = 0;
 	const char *disk = parts->dp.disk, *s;
 	const struct partition *lp;
@@ -281,13 +300,10 @@ disklabel_write_to_disk(struct disk_part
 	assert(parts->l.d_ncylinders != 0);
 	assert(parts->l.d_secpercyl != 0);
 
-	sprintf(fname, "/tmp/disklabel.%u", getpid());
-	f = fopen(fname, "w");
-	if (f == NULL)
-		return false;
-
 	/* make sure we have a 0 terminated packname */
 	strlcpy(packname, parts->l.d_packname, sizeof packname);
+	if (packname[0] == 0)
+		strcpy(packname, "fictious");
 
 	/* fill typename with disk name prefix, if not already set */
 	if (strlen(parts->l.d_typename) == 0) {
@@ -298,18 +314,24 @@ disklabel_write_to_disk(struct disk_part
 			*d = *s;
 		}
 	}
-	parts->l.d_typename[sizeof(parts->l.d_typename)-1] = 0;
 
 	/* we need a valid disk type name, so enforce an arbitrary if
 	 * above did not yield a usable one */
 	if (strlen(parts->l.d_typename) == 0)
 		strncpy(parts->l.d_typename, "SCSI",
 		    sizeof(parts->l.d_typename));
+	escape_capfile(disktype, parts->l.d_typename,
+	    sizeof(parts->l.d_typename));
+
+	sprintf(fname, "/tmp/disklabel.%u", getpid());
+	f = fopen(fname, "w");
+	if (f == NULL)
+		return false;
 
 	lp = parts->l.d_partitions;
 	scripting_fprintf(NULL, "cat <<EOF >%s\n", fname);
 	scripting_fprintf(f, "%s|NetBSD installation generated:\\\n",
-	    parts->l.d_typename);
+	    disktype);
 	scripting_fprintf(f, "\t:nc#%d:nt#%d:ns#%d:\\\n",
 	    parts->l.d_ncylinders, parts->l.d_ntracks, parts->l.d_nsectors);
 	scripting_fprintf(f, "\t:sc#%d:su#%" PRIu32 ":\\\n",
@@ -351,8 +373,8 @@ disklabel_write_to_disk(struct disk_part
 	 */
 #ifdef DISKLABEL_CMD
 	/* disklabel the disk */
-	rv = run_program(RUN_DISPLAY, "%s -f %s %s %s %s",
-	    DISKLABEL_CMD, fname, disk, parts->l.d_typename, packname);
+	rv = run_program(RUN_DISPLAY, "%s -f %s %s '%s' '%s'",
+	    DISKLABEL_CMD, fname, disk, disktype, packname);
 #endif
 
 	unlink(fname);

Reply via email to