[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] Add error check to hex2bin().



James Morris wrote:
> On Mon, 8 Aug 2011, Tetsuo Handa wrote:
> > OK, updated in V3. No other changes since V2.
> 
> What about these uses ?
> 
> drivers/target/target_core_cdb.c
> drivers/target/target_core_fabric_lib.c
> 
Oh, new user in Linux 3.1.

Nicholas, I'm planning to add error check to hex2bin().

----------------------------------------
[PATCH (v3)] Add error check to hex2bin().

Since converting 2 hexadecimal letters into a byte with error checks is
commonly used, we can replace multiple hex_to_bin() calls with single hex2bin()
call by changing hex2bin() to do error checks.

--- security-testing-2.6.orig/include/linux/kernel.h
+++ security-testing-2.6/include/linux/kernel.h
@@ -382,7 +382,7 @@ static inline char *pack_hex_byte(char *
 }
 
 extern int hex_to_bin(char ch);
-extern void hex2bin(u8 *dst, const char *src, size_t count);
+extern bool __must_check hex2bin(u8 *dst, const char *src, size_t count);
 
 /*
  * General tracing related utility functions - trace_printk(),
--- security-testing-2.6.orig/lib/hexdump.c
+++ security-testing-2.6/lib/hexdump.c
@@ -38,14 +38,23 @@ EXPORT_SYMBOL(hex_to_bin);
  * @dst: binary result
  * @src: ascii hexadecimal string
  * @count: result length
+ *
+ * Returns true on success, false in case of bad input.
  */
-void hex2bin(u8 *dst, const char *src, size_t count)
+bool hex2bin(u8 *dst, const char *src, size_t count)
 {
 	while (count--) {
-		*dst = hex_to_bin(*src++) << 4;
-		*dst += hex_to_bin(*src++);
-		dst++;
+		int c = hex_to_bin(*src++);
+		int d;
+
+		if (unlikely(c < 0))
+			return false;
+		d = hex_to_bin(*src++);
+		if (unlikely(d < 0))
+			return false;
+		*dst++ = (c << 4) | d;
 	}
+	return true;
 }
 EXPORT_SYMBOL(hex2bin);
 
----------------------------------------

I was assuming that, by adding error check, hex2bin() will leave the buffer
uninitialized in case of bad input because hex2bin() user in Linux 3.0 can
return errors. Now you are a new user of hex2bin() but your code cannot return
errors. Nicholas, how do you want to handle bad input?
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]

Powered by Linux