Module Name:    src
Committed By:   snj
Date:           Mon Feb 12 04:05:07 UTC 2018

Modified Files:
        src/sbin/gpt [netbsd-8]: backup.c biosboot.c gpt.c gpt.h restore.c
            show.c
        src/sys/dev/dkwedge [netbsd-8]: dkwedge_gpt.c

Log Message:
Pull up following revision(s) (requested by christos in ticket #545):
        sbin/gpt/backup.c: 1.17-1.18
        sbin/gpt/biosboot.c: 1.29-1.30
        sbin/gpt/gpt.c: 1.71-1.73
        sbin/gpt/gpt.h: 1.36
        sbin/gpt/restore.c: 1.17
        sbin/gpt/show.c: 1.40-1.41
        sys/dev/dkwedge/dkwedge_gpt.c: 1.19-1.20
PR/52522: Piotr Meyer: Don't NUL terminate the gpt label name.
--
- make sure that the utf16 string is padded with 0's where needed.
- since the utf16 string is not 0 terminated, pass the size of the string.
--
use __arraycount
--
PR/52522: ent_name is not necessarily 0 terminated, so check bounds.
--
use arraycount.


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.16.8.1 src/sbin/gpt/backup.c src/sbin/gpt/restore.c
cvs rdiff -u -r1.27.4.1 -r1.27.4.2 src/sbin/gpt/biosboot.c
cvs rdiff -u -r1.70 -r1.70.4.1 src/sbin/gpt/gpt.c
cvs rdiff -u -r1.35 -r1.35.4.1 src/sbin/gpt/gpt.h
cvs rdiff -u -r1.39 -r1.39.6.1 src/sbin/gpt/show.c
cvs rdiff -u -r1.18 -r1.18.6.1 src/sys/dev/dkwedge/dkwedge_gpt.c

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

Modified files:

Index: src/sbin/gpt/backup.c
diff -u src/sbin/gpt/backup.c:1.16 src/sbin/gpt/backup.c:1.16.8.1
--- src/sbin/gpt/backup.c:1.16	Thu Dec  3 21:40:32 2015
+++ src/sbin/gpt/backup.c	Mon Feb 12 04:05:07 2018
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: backup.c,v 1.16 2015/12/03 21:40:32 christos Exp $");
+__RCSID("$NetBSD: backup.c,v 1.16.8.1 2018/02/12 04:05:07 snj Exp $");
 #endif
 
 #include <sys/bootblock.h>
@@ -231,7 +231,8 @@ store_tbl(gpt_t gpt, const map_t m, prop
 		PROP_ERR(propnum);
 		rc = prop_dictionary_set(gpt_dict, "attributes", propnum);
 		PROP_ERR(rc);
-		utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
+		utf16_to_utf8(ent->ent_name, __arraycount(ent->ent_name),
+		    utfbuf, __arraycount(utfbuf));
 		if (utfbuf[0] != '\0') {
 			propstr = prop_string_create_cstring((char *)utfbuf);
 			PROP_ERR(propstr);
Index: src/sbin/gpt/restore.c
diff -u src/sbin/gpt/restore.c:1.16 src/sbin/gpt/restore.c:1.16.8.1
--- src/sbin/gpt/restore.c:1.16	Thu Dec  3 02:02:43 2015
+++ src/sbin/gpt/restore.c	Mon Feb 12 04:05:07 2018
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: restore.c,v 1.16 2015/12/03 02:02:43 christos Exp $");
+__RCSID("$NetBSD: restore.c,v 1.16.8.1 2018/02/12 04:05:07 snj Exp $");
 #endif
 
 #include <sys/types.h>
@@ -178,7 +178,7 @@ restore_ent(gpt_t gpt, prop_dictionary_t
 	if (propstr != NULL) {
 		s = prop_string_cstring_nocopy(propstr);
 		utf8_to_utf16((const uint8_t *)s, ent.ent_name,
-			__arraycount(ent.ent_name));
+		    __arraycount(ent.ent_name));
 	}
 	propnum = prop_dictionary_get(gpt_dict, "index");
 	PROP_ERR(propnum);

Index: src/sbin/gpt/biosboot.c
diff -u src/sbin/gpt/biosboot.c:1.27.4.1 src/sbin/gpt/biosboot.c:1.27.4.2
--- src/sbin/gpt/biosboot.c:1.27.4.1	Wed Jul  5 20:07:42 2017
+++ src/sbin/gpt/biosboot.c	Mon Feb 12 04:05:07 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: biosboot.c,v 1.27.4.1 2017/07/05 20:07:42 snj Exp $ */
+/*	$NetBSD: biosboot.c,v 1.27.4.2 2018/02/12 04:05:07 snj Exp $ */
 
 /*
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #ifdef __RCSID
-__RCSID("$NetBSD: biosboot.c,v 1.27.4.1 2017/07/05 20:07:42 snj Exp $");
+__RCSID("$NetBSD: biosboot.c,v 1.27.4.2 2018/02/12 04:05:07 snj Exp $");
 #endif
 
 #include <sys/stat.h>
@@ -219,7 +219,9 @@ biosboot(gpt_t gpt, daddr_t start, uint6
 			break;
 
 		if (label != NULL) {
-			utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
+			utf16_to_utf8(ent->ent_name,
+			    __arraycount(ent->ent_name), utfbuf,
+			    __arraycount(utfbuf));
 			if (strcmp((char *)label, (char *)utfbuf) == 0)
 				break;
 		}

Index: src/sbin/gpt/gpt.c
diff -u src/sbin/gpt/gpt.c:1.70 src/sbin/gpt/gpt.c:1.70.4.1
--- src/sbin/gpt/gpt.c:1.70	Thu Feb 16 03:32:17 2017
+++ src/sbin/gpt/gpt.c	Mon Feb 12 04:05:07 2018
@@ -35,7 +35,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/gpt.c,v 1.16 2006/07/07 02:44:23 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: gpt.c,v 1.70 2017/02/16 03:32:17 christos Exp $");
+__RCSID("$NetBSD: gpt.c,v 1.70.4.1 2018/02/12 04:05:07 snj Exp $");
 #endif
 
 #include <sys/param.h>
@@ -121,16 +121,22 @@ crc32(const void *buf, size_t size)
 	return crc ^ ~0U;
 }
 
+/*
+ * Produce a NUL-terminated utf-8 string from the non-NUL-terminated
+ * utf16 string.
+ */
 void
-utf16_to_utf8(const uint16_t *s16, uint8_t *s8, size_t s8len)
+utf16_to_utf8(const uint16_t *s16, size_t s16len, uint8_t *s8, size_t s8len)
 {
-	size_t s8idx, s16idx, s16len;
+	size_t s8idx, s16idx;
 	uint32_t utfchar;
 	unsigned int c;
 
-	s16len = 0;
-	while (s16[s16len++] != 0)
-		continue;
+	for (s16idx = 0; s16idx < s16len; s16idx++)
+		if (s16[s16idx] == 0)
+			break;
+
+	s16len = s16idx;
 	s8idx = s16idx = 0;
 	while (s16idx < s16len) {
 		utfchar = le16toh(s16[s16idx++]);
@@ -168,6 +174,10 @@ utf16_to_utf8(const uint16_t *s16, uint8
 	s8[s8idx] = 0;
 }
 
+/*
+ * Produce a non-NUL-terminated utf-16 string from the NUL-terminated
+ * utf8 string.
+ */
 void
 utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len)
 {
@@ -224,11 +234,13 @@ utf8_to_utf16(const uint8_t *s8, uint16_
 			} else
 				s16[s16idx++] = htole16((uint16_t)utfchar);
 			if (s16idx == s16len) {
-				s16[--s16idx] = 0;
 				return;
 			}
 		}
 	} while (c != 0);
+
+	while (s16idx < s16len)
+		s16[s16idx++] = 0;
 }
 
 void *
@@ -1028,7 +1040,9 @@ gpt_change_ent(gpt_t gpt, const struct g
 
 		ent = gpt_ent_primary(gpt, i);
 		if (find->label != NULL) {
-			utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
+			utf16_to_utf8(ent->ent_name,
+			    __arraycount(ent->ent_name),
+			    utfbuf, __arraycount(utfbuf));
 			if (strcmp((char *)find->label, (char *)utfbuf) == 0)
 				continue;
 		}

Index: src/sbin/gpt/gpt.h
diff -u src/sbin/gpt/gpt.h:1.35 src/sbin/gpt/gpt.h:1.35.4.1
--- src/sbin/gpt/gpt.h:1.35	Thu Feb 16 22:40:19 2017
+++ src/sbin/gpt/gpt.h	Mon Feb 12 04:05:07 2018
@@ -99,7 +99,7 @@ struct gpt_ent *gpt_ent_primary(gpt_t, u
 struct gpt_ent *gpt_ent_backup(gpt_t, unsigned int);
 int	gpt_usage(const char *, const struct gpt_cmd *);
 
-void 	utf16_to_utf8(const uint16_t *, uint8_t *, size_t);
+void 	utf16_to_utf8(const uint16_t *, size_t, uint8_t *, size_t);
 void	utf8_to_utf16(const uint8_t *, uint16_t *, size_t);
 
 #define GPT_FIND "ab:i:L:s:t:"

Index: src/sbin/gpt/show.c
diff -u src/sbin/gpt/show.c:1.39 src/sbin/gpt/show.c:1.39.6.1
--- src/sbin/gpt/show.c:1.39	Wed Oct  5 03:06:24 2016
+++ src/sbin/gpt/show.c	Mon Feb 12 04:05:07 2018
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: show.c,v 1.39 2016/10/05 03:06:24 kre Exp $");
+__RCSID("$NetBSD: show.c,v 1.39.6.1 2018/02/12 04:05:07 snj Exp $");
 #endif
 
 #include <sys/bootblock.h>
@@ -129,8 +129,9 @@ print_part_type(int map_type, int flags,
 		printf("GPT part ");
 		ent = map_data;
 		if (flags & SHOW_LABEL) {
-			utf16_to_utf8(ent->ent_name, utfbuf,
-			    sizeof(utfbuf));
+			utf16_to_utf8(ent->ent_name,
+			    __arraycount(ent->ent_name), utfbuf,
+			    __arraycount(utfbuf));
 			b = (char *)utfbuf;
 		} else if (flags & SHOW_GUID) {
 			gpt_uuid_snprintf( buf, sizeof(buf), "%d",
@@ -215,7 +216,8 @@ show_one(gpt_t gpt, unsigned int entry)
 	gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
 	printf("GUID: %s\n", s2);
 
-	utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
+	utf16_to_utf8(ent->ent_name, __arraycount(ent->ent_name), utfbuf,
+	    __arraycount(utfbuf));
 	printf("Label: %s\n", (char *)utfbuf);
 
 	printf("Attributes: ");
@@ -284,7 +286,9 @@ show_all(gpt_t gpt)
 #endif
 			putchar('\n');
 
-			utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
+			utf16_to_utf8(ent->ent_name,
+			    __arraycount(ent->ent_name), utfbuf,
+			    __arraycount(utfbuf));
 			printf(PFX "Label: %s\n", (char *)utfbuf);
 
 			printf(PFX "Attributes: ");

Index: src/sys/dev/dkwedge/dkwedge_gpt.c
diff -u src/sys/dev/dkwedge/dkwedge_gpt.c:1.18 src/sys/dev/dkwedge/dkwedge_gpt.c:1.18.6.1
--- src/sys/dev/dkwedge/dkwedge_gpt.c:1.18	Thu Jan 19 00:44:40 2017
+++ src/sys/dev/dkwedge/dkwedge_gpt.c	Mon Feb 12 04:05:07 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: dkwedge_gpt.c,v 1.18 2017/01/19 00:44:40 maya Exp $	*/
+/*	$NetBSD: dkwedge_gpt.c,v 1.18.6.1 2018/02/12 04:05:07 snj Exp $	*/
 
 /*-
  * Copyright (c) 2004 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: dkwedge_gpt.c,v 1.18 2017/01/19 00:44:40 maya Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dkwedge_gpt.c,v 1.18.6.1 2018/02/12 04:05:07 snj Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -252,7 +252,8 @@ dkwedge_discover_gpt(struct disk *pdk, s
 		else {
 			c = dkw.dkw_wname;
 			r = sizeof(dkw.dkw_wname) - 1;
-			for (j = 0; ent->ent_name[j] != 0x0000; j++) {
+			for (j = 0; j < __arraycount(ent->ent_name)
+			    && ent->ent_name[j] != 0x0000; j++) {
 				n = wput_utf8(c, r, le16toh(ent->ent_name[j]));
 				if (n == 0)
 					break;

Reply via email to