git: 6d7145a2b053 - stable/12 - [PowerPC] Allow traversal of oversize OF properties.

Brandon Bergren bdragon at FreeBSD.org
Mon Mar 8 19:07:37 UTC 2021


The branch stable/12 has been updated by bdragon:

URL: https://cgit.FreeBSD.org/src/commit/?id=6d7145a2b053e65aaa7acc40a7573fc0aa76984a

commit 6d7145a2b053e65aaa7acc40a7573fc0aa76984a
Author:     Brandon Bergren <bdragon at FreeBSD.org>
AuthorDate: 2020-11-13 16:49:41 +0000
Commit:     Brandon Bergren <bdragon at FreeBSD.org>
CommitDate: 2021-03-08 19:02:52 +0000

    [PowerPC] Allow traversal of oversize OF properties.
    
    In standards such as LoPAPR, property names in excess of the usual 31
    characters exist.
    
    This breaks property traversal.
    
    While in IEEE 1275-1994, nextprop is defined explicitly to work with a
    32-byte region of memory, using a larger buffer should be fine. There is
    actually no way to pass a buffer length to the nextprop call in the OF
    client interface, so SLOF actually just blindly overflows the buffer.
    
    So we have to defensively make the buffer larger, to avoid memory
    corruption when reading out long properties on live OF systems.
    
    Note also that on real-mode OF, things are pretty tight because we are
    allocating against a static bounce buffer in low memory, so we can't just
    use a huge buffer to work around this without it being wasteful of our
    limited amount of 32-bit physical memory.
    
    This allows a patched ofwdump to operate properly on SLOF (i.e. pseries)
    systems, as well as any other PowerPC systems with overlength properties.
    
    Reviewed by:    jhibbits
    Sponsored by:   Tag1 Consulting, Inc.
    Differential Revision:  https://reviews.freebsd.org/D26669
    
    (cherry picked from commit 26869ad14c70306313405029229a1e2fd94510cd)
---
 sys/dev/ofw/openfirmio.c   | 15 +++++++++++++--
 sys/dev/ofw/openfirmio.h   | 14 ++++++++++++++
 usr.sbin/ofwdump/ofwdump.c |  2 +-
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/sys/dev/ofw/openfirmio.c b/sys/dev/ofw/openfirmio.c
index a6ee962b5c8f..ccd284e8bff7 100644
--- a/sys/dev/ofw/openfirmio.c
+++ b/sys/dev/ofw/openfirmio.c
@@ -115,7 +115,7 @@ openfirm_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
 	phandle_t node;
 	int len, ok, error;
 	char *name, *value;
-	char newname[32];
+	char newname[OFIOCSUGGPROPNAMELEN];
 
 	if ((flags & FREAD) == 0)
 		return (EBADF);
@@ -223,8 +223,19 @@ openfirm_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
 			break;
 		}
 		len = strlen(newname) + 1;
-		if (len > of->of_buflen)
+		if (len > of->of_buflen) {
+			/*
+			 * Passed buffer was insufficient.
+			 *
+			 * Instead of returning an error here, truncate the
+			 * property name to fit the buffer.
+			 *
+			 * This allows us to retain compatibility with old
+			 * tools which always pass a 32 character buffer.
+			 */
 			len = of->of_buflen;
+			newname[len - 1] = '\0';
+		}
 		else
 			of->of_buflen = len;
 		error = copyout(newname, of->of_buf, len);
diff --git a/sys/dev/ofw/openfirmio.h b/sys/dev/ofw/openfirmio.h
index 7ba7b907e892..e892c50c672a 100644
--- a/sys/dev/ofw/openfirmio.h
+++ b/sys/dev/ofw/openfirmio.h
@@ -76,4 +76,18 @@ struct ofiocdesc {
 /* Maximum accepted value length (maximum of nvramrc property). */
 #define	OFIOCMAXVALUE	8192
 
+/*
+ * While IEEE 1275-1994 states in 3.2.2.1.1 that property names are 1-31
+ * printable characters, in practice, this limit has been ignored.
+ * Noncompliant properties have been codified in standards such as LoPAPR.
+ *
+ * This is a suggested buffer length that should be large enough to hold
+ * any property name currently seen in device trees, without being overly
+ * wasteful of memory.
+ *
+ * If a future version of the Devicetree specification updates the property
+ * names length requirement, this value will be updated to match.
+ */
+#define	OFIOCSUGGPROPNAMELEN	64
+
 #endif /* _DEV_OFW_OPENFIRMIO_H_ */
diff --git a/usr.sbin/ofwdump/ofwdump.c b/usr.sbin/ofwdump/ofwdump.c
index 9a356f48d01b..6bca1ac839ab 100644
--- a/usr.sbin/ofwdump/ofwdump.c
+++ b/usr.sbin/ofwdump/ofwdump.c
@@ -144,7 +144,7 @@ static void
 ofw_dump_properties(int fd, phandle_t n, int level, int raw, int str)
 {
 	int nlen;
-	char prop[32];
+	char prop[OFIOCSUGGPROPNAMELEN];
 
 	for (nlen = ofw_firstprop(fd, n, prop, sizeof(prop)); nlen != 0;
 	     nlen = ofw_nextprop(fd, n, prop, prop, sizeof(prop)))


More information about the dev-commits-src-all mailing list