Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 31 Dec 2017 22:43:25 +0000 (UTC)
From:      Ian Lepore <ian@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r327453 - in head: stand/libsa sys/sys
Message-ID:  <201712312243.vBVMhPtS086151@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ian
Date: Sun Dec 31 22:43:24 2017
New Revision: 327453
URL: https://svnweb.freebsd.org/changeset/base/327453

Log:
  Add a validbcd() routine that uses the bcd2bin_data[] array and returns a
  bool indicating whether the input value represents a valid BCD byte.
  
  The existing bcd2bin() routine will KASSERT if asked to convert a bad value,
  but sometimes the kernel has to handle BCD data from untrusted sources, so
  this will provide a mechanism to validate data before attempting conversion.
  
  This would be have easier/cleaner if the bcd2bin_data[] array contained an
  out-of-range value (such as 0xff) in the infill locations that aren't valid,
  but it's a global symbol that might be referenced by out-of-tree code
  relying on the current scheme, so I'm leaving that alone.

Modified:
  head/stand/libsa/stand.h
  head/sys/sys/libkern.h

Modified: head/stand/libsa/stand.h
==============================================================================
--- head/stand/libsa/stand.h	Sun Dec 31 22:35:32 2017	(r327452)
+++ head/stand/libsa/stand.h	Sun Dec 31 22:43:24 2017	(r327453)
@@ -354,6 +354,7 @@ extern char const	hex2ascii_data[];
 #define	bcd2bin(bcd)	(bcd2bin_data[bcd])
 #define	bin2bcd(bin)	(bin2bcd_data[bin])
 #define	hex2ascii(hex)	(hex2ascii_data[hex])
+#define	validbcd(bcd)	(bcd == 0 || (bcd > 0 && bcd <= 0x99 && bcd2bin_data[bcd] != 0))
 
 /* min/max (undocumented) */
 static __inline int imax(int a, int b) { return (a > b ? a : b); }

Modified: head/sys/sys/libkern.h
==============================================================================
--- head/sys/sys/libkern.h	Sun Dec 31 22:35:32 2017	(r327452)
+++ head/sys/sys/libkern.h	Sun Dec 31 22:43:24 2017	(r327453)
@@ -82,6 +82,13 @@ hex2ascii(int hex)
 	return (hex2ascii_data[hex]);
 }
 
+static inline bool
+validbcd(int bcd)
+{
+
+	return (bcd == 0 || (bcd > 0 && bcd <= 0x99 && bcd2bin_data[bcd] != 0));
+}
+
 static __inline int imax(int a, int b) { return (a > b ? a : b); }
 static __inline int imin(int a, int b) { return (a < b ? a : b); }
 static __inline long lmax(long a, long b) { return (a > b ? a : b); }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201712312243.vBVMhPtS086151>