mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/3] lib: add error checking to hex2bin
@ 2011-09-20 13:15 Mimi Zohar
  2011-09-20 13:15 ` [PATCH 2/3] trusted-keys: check hex2bin result Mimi Zohar
  2011-09-20 13:15 ` [PATCH 3/3] encrypted-keys: " Mimi Zohar
  0 siblings, 2 replies; 5+ messages in thread
From: Mimi Zohar @ 2011-09-20 13:15 UTC (permalink / raw)
  To: linux-security-module
  Cc: Mimi Zohar, linux-kernel, Andrew Morton, Andy Shevchenko,
	Tetsuo Handa, James Morris, David Safford

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:
- add __must_check compiler option (Andy Shevchenko's suggestion)
- change function API to return error checking result

Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
---
 include/linux/kernel.h |    2 +-
 lib/hexdump.c          |   15 +++++++++++----
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 46ac9a5..8eefcf7 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -382,7 +382,7 @@ static inline char *pack_hex_byte(char *buf, u8 byte)
 }
 
 extern int hex_to_bin(char ch);
-extern void 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(),
diff --git a/lib/hexdump.c b/lib/hexdump.c
index f5fe6ba..51d5ae2 100644
--- a/lib/hexdump.c
+++ b/lib/hexdump.c
@@ -38,14 +38,21 @@ EXPORT_SYMBOL(hex_to_bin);
  * @dst: binary result
  * @src: ascii hexadecimal string
  * @count: result length
+ *
+ * Return 0 on success, -1 in case of bad input.
  */
-void hex2bin(u8 *dst, const char *src, size_t count)
+int hex2bin(u8 *dst, const char *src, size_t count)
 {
 	while (count--) {
-		*dst = hex_to_bin(*src++) << 4;
-		*dst += hex_to_bin(*src++);
-		dst++;
+		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);
 
-- 
1.7.3.4


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-09-20 14:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-20 13:15 [PATCH 1/3] lib: add error checking to hex2bin Mimi Zohar
2011-09-20 13:15 ` [PATCH 2/3] trusted-keys: check hex2bin result Mimi Zohar
2011-09-20 13:57   ` Andy Shevchenko
2011-09-20 14:12     ` Mimi Zohar
2011-09-20 13:15 ` [PATCH 3/3] encrypted-keys: " Mimi Zohar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®