From: Mimi Zohar <zohar@linux.vnet.ibm.com>
Date: Tue, 20 Sep 2011 11:23:49 -0400
Subject: lib: add error checking to hex2bin
Git-commit: b78049831ffed65f0b4e61f69df14f3ab17922cb
Patch-mainline: v3.2-rc1
References: bsc#1107829 CVE-2018-14633

hex2bin converts a hexadecimal string to its binary representation.
The original version of hex2bin did not do any error checking.  This
patch adds error checking and returns the result.

Changelog v1:
- removed unpack_hex_byte()
- changed return code from boolean to int

Changelog:
- use the new unpack_hex_byte()
- add __must_check compiler option (Andy Shevchenko's suggestion)
- change function API to return error checking result
  (based on Tetsuo Handa's initial patch)

Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
Acked-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Acked-by: Lee Duncan <lduncan@suse.com>

Updated for SLE cve-linux-3.0 to leave the old function and
name the new function starting with an underscore.
---
 include/linux/kernel.h |    1 +
 lib/hexdump.c          |   23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+)

--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -413,6 +413,7 @@ static inline char * __deprecated pack_h
 
 extern int hex_to_bin(char ch);
 extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
+extern int __must_check _hex2bin(u8 *dst, const char *src, size_t count);
 
 /*
  * General tracing related utility functions - trace_printk(),
--- a/lib/hexdump.c
+++ b/lib/hexdump.c
@@ -57,6 +57,29 @@ int hex2bin(u8 *dst, const char *src, si
 EXPORT_SYMBOL(hex2bin);
 
 /**
+ * _hex2bin - convert an ascii hexadecimal string to its binary representation
+ * @dst: binary result
+ * @src: ascii hexadecimal string
+ * @count: result length
+ *
+ * Return 0 on success, -1 in case of bad input.
+ */
+int _hex2bin(u8 *dst, const char *src, size_t count)
+{
+	while (count--) {
+		int hi = hex_to_bin(*src++);
+		int lo = hex_to_bin(*src++);
+
+		if ((hi < 0) || (lo < 0))
+			return -1;
+
+		*dst++ = (hi << 4) | lo;
+	}
+	return 0;
+}
+EXPORT_SYMBOL(_hex2bin);
+
+/**
  * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
  * @buf: data blob to dump
  * @len: number of bytes in the @buf
