Skip to contentSkip to conversation

net: can: remove custom hex_to_bin()

PATCH Applied Andy Shevchenko 7 participants 16 messages Jul 18, 2011 – Jul 19, 2011

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: can: remove custom hex_to_bin()

Cc Wolfgang Grandegger <[email protected]>
---
 drivers/net/can/slcan.c |   26 +++++---------------------
 1 files changed, 5 insertions(+), 21 deletions(-)
drivers/net/can/slcan.c +5 -21
index aa8ad73..65e54fd 100644
@@ -56,6 +56,7 @@
 #include <linux/sched.h>
 #include <linux/delay.h>
 #include <linux/init.h>
+#include <linux/kernel.h>
 #include <linux/can.h>
 
 static __initdata const char banner[] =
@@ -142,21 +143,6 @@ static struct net_device **slcan_devs;
   *			STANDARD SLCAN DECAPSULATION			 *
   ************************************************************************/
 
-static int asc2nibble(char c)
-{
-
-	if ((c >= '0') && (c <= '9'))
-		return c - '0';
-
-	if ((c >= 'A') && (c <= 'F'))
-		return c - 'A' + 10;
-
-	if ((c >= 'a') && (c <= 'f'))
-		return c - 'a' + 10;
-
-	return 16; /* error */
-}
-
 /* Send one completely decapsulated can_frame to the network layer */
 static void slc_bump(struct slcan *sl)
 {
@@ -195,18 +181,16 @@ static void slc_bump(struct slcan *sl)
 	*(u64 *) (&cf.data) = 0; /* clear payload */
 
 	for (i = 0, dlc_pos++; i < cf.can_dlc; i++) {
-
-		tmp = asc2nibble(sl->rbuff[dlc_pos++]);
-		if (tmp > 0x0F)
+		tmp = hex_to_bin(sl->rbuff[dlc_pos++]);
+		if (tmp < 0)
 			return;
 		cf.data[i] = (tmp << 4);
-		tmp = asc2nibble(sl->rbuff[dlc_pos++]);
-		if (tmp > 0x0F)
+		tmp = hex_to_bin(sl->rbuff[dlc_pos++]);
+		if (tmp < 0)
 			return;
 		cf.data[i] |= tmp;
 	}
 
-
 	skb = dev_alloc_skb(sizeof(struct can_frame));
 	if (!skb)
 		return;
Signature · 1 line
1.7.5.4
#1 lore raw
Quoted 5 lines · Andy Shevchenko
drivers/net/can/slcan.c +1 -3
 	for (i = 0, dlc_pos++; i < cf.can_dlc; i++) {
-
-		tmp = asc2nibble(sl->rbuff[dlc_pos++]);
-		if (tmp > 0x0F)
+		tmp = hex_to_bin(sl->rbuff[dlc_pos++]);

drivers/net/can/slcan.c static void slc_bump(struct slcan *sl) · line 192

Quoted 10 lines
drivers/net/can/slcan.c +3 -2
+		if (tmp < 0)
 			return;
 		cf.data[i] = (tmp << 4);
-		tmp = asc2nibble(sl->rbuff[dlc_pos++]);
-		if (tmp > 0x0F)
+		tmp = hex_to_bin(sl->rbuff[dlc_pos++]);
+		if (tmp < 0)
 			return;
 		cf.data[i] |= tmp;
 	}

What about changing

void hex2bin(u8 *dst, const char *src, size_t count)

to

bool hex2bin(u8 *dst, const char *src, size_t count)

in order to do error checks like

bool hex2bin_with_validation(u8 *dst, const char *src, size_t count)

{
	while (count--) {
		int c = hex_to_bin(*src++);
		int d;
		if (c < 0)
			return false;
		d = hex_to_bin(*src++)
		if (d < 0)
			return false;
		*dst++ = (c << 4) | d;
	}
	return true;
}

and use hex2bin() rather than hex_to_bin()?

#2 lore raw
Quoted 42 lines · Tetsuo Handa
Quoted 5 lines · Andy Shevchenko
drivers/net/can/slcan.c +1 -3
 	for (i = 0, dlc_pos++; i < cf.can_dlc; i++) {
-
-		tmp = asc2nibble(sl->rbuff[dlc_pos++]);
-		if (tmp > 0x0F)
+		tmp = hex_to_bin(sl->rbuff[dlc_pos++]);

drivers/net/can/slcan.c static void slc_bump(struct slcan *sl) · line 192

Quoted 10 lines
drivers/net/can/slcan.c +3 -2
+		if (tmp < 0)
 			return;
 		cf.data[i] = (tmp << 4);
-		tmp = asc2nibble(sl->rbuff[dlc_pos++]);
-		if (tmp > 0x0F)
+		tmp = hex_to_bin(sl->rbuff[dlc_pos++]);
+		if (tmp < 0)
 			return;
 		cf.data[i] |= tmp;
 	}

What about changing

void hex2bin(u8 *dst, const char *src, size_t count)

to

bool hex2bin(u8 *dst, const char *src, size_t count)

in order to do error checks like

bool hex2bin_with_validation(u8 *dst, const char *src, size_t count)

{
	while (count--) {
		int c = hex_to_bin(*src++);
		int d;
		if (c < 0)
			return false;
		d = hex_to_bin(*src++)
		if (d < 0)
			return false;
		*dst++ = (c << 4) | d;
	}
	return true;
}
Quoted 1 line

and use hex2bin() rather than hex_to_bin()?

Perhaps, good idea. Could you submit a patch?

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#3 lore raw

PATCH 1/1 Add error check to hex2bin().

Currently, security/keys/ is the only user of hex2bin(). Should I keep hex2bin() unmodified in case of bad input? If so, I'd like to make it as hex2bin_safe().
----------------------------------------
[PATCH] 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.

---

include/linux/kernel.h +1 -1
index 953352a..186e9fc 100644
@@ -374,7 +374,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 bool hex2bin(u8 *dst, const char *src, size_t count);
 
 /*
  * General tracing related utility functions - trace_printk(),
lib/hexdump.c +12 -4
index f5fe6ba..1524002 100644
@@ -38,14 +38,22 @@ 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 (c < 0)
+			return false;
+		d = hex_to_bin(*src++);
+		if (d < 0)
+			return false;
+		*dst++ = (c << 4) | d;
 	}
+	return true;
 }
 EXPORT_SYMBOL(hex2bin);
 

In message "Re: [PATCH] net: can: remove custom hex_to_bin()",

Quoted 49 lines · Andy Shevchenko
Quoted 42 lines · Tetsuo Handa
Quoted 5 lines · Andy Shevchenko
(quoted diff) +1 -3
 	for (i = 0, dlc_pos++; i < cf.can_dlc; i++) {
-
-		tmp = asc2nibble(sl->rbuff[dlc_pos++]);
-		if (tmp > 0x0F)
+		tmp = hex_to_bin(sl->rbuff[dlc_pos++]);

Review

Quoted 10 lines
(quoted diff) +3 -2
+		if (tmp < 0)
 			return;
 		cf.data[i] = (tmp << 4);
-		tmp = asc2nibble(sl->rbuff[dlc_pos++]);
-		if (tmp > 0x0F)
+		tmp = hex_to_bin(sl->rbuff[dlc_pos++]);
+		if (tmp < 0)
 			return;
 		cf.data[i] |= tmp;
 	}

What about changing

void hex2bin(u8 *dst, const char *src, size_t count)

to

bool hex2bin(u8 *dst, const char *src, size_t count)

in order to do error checks like

bool hex2bin_with_validation(u8 *dst, const char *src, size_t count)

{
	while (count--) {
		int c = hex_to_bin(*src++);
		int d;
		if (c < 0)
			return false;
		d = hex_to_bin(*src++)
		if (d < 0)
			return false;
		*dst++ = (c << 4) | d;
	}
	return true;
}
Quoted 1 line

and use hex2bin() rather than hex_to_bin()?

Perhaps, good idea. Could you submit a patch?

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#4 lore raw

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

Quoted 36 lines · Tetsuo Handa

Currently, security/keys/ is the only user of hex2bin(). Should I keep hex2bin() unmodified in case of bad input? If so, I'd like to make it as hex2bin_safe().

----------------------------------------
[PATCH] 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.

---

include/linux/kernel.h +1 -1
index 953352a..186e9fc 100644
@@ -374,7 +374,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 bool hex2bin(u8 *dst, const char *src, size_t count);
 
 /*
  * General tracing related utility functions - trace_printk(),
lib/hexdump.c +2 -0
index f5fe6ba..1524002 100644
@@ -38,14 +38,22 @@ 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.

lib/hexdump.c EXPORT_SYMBOL(hex_to_bin); · line 48

Quoted 10 lines
lib/hexdump.c +3 -4
  */
-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;

Missing blank line here.

lib/hexdump.c EXPORT_SYMBOL(hex_to_bin); · line 58

Quoted 10 lines
lib/hexdump.c +7 -0
+		if (c < 0)
+			return false;
+		d = hex_to_bin(*src++);
+		if (d < 0)
+			return false;
+		*dst++ = (c << 4) | d;
 	}
+	return true;
 }
 EXPORT_SYMBOL(hex2bin);

We probably don't need to define a separate 'safe' function.

Instead of changing the existing code to short circuit out and return a value, does only adding the return value work? Something like:

        bool ret = true;
        int c, d;

        while (count--) {
                c = hex_to_bin(*src++);
                d = hex_to_bin(*src++);
                *dst++ = (c << 4) | d;

                if (c < 0 || d < 0)
                        ret = false;
        }
        return ret;

thanks,

Mimi

Quoted 56 lines
lib/hexdump.c +0 -0
 In message "Re: [PATCH] net: can: remove custom hex_to_bin()",
Quoted 43 lines · Andy Shevchenko
Quoted 42 lines · Tetsuo Handa
Quoted 5 lines · Andy Shevchenko
(quoted diff) +1 -3
 	for (i = 0, dlc_pos++; i < cf.can_dlc; i++) {
-
-		tmp = asc2nibble(sl->rbuff[dlc_pos++]);
-		if (tmp > 0x0F)
+		tmp = hex_to_bin(sl->rbuff[dlc_pos++]);

Review

Quoted 10 lines
(quoted diff) +3 -2
+		if (tmp < 0)
 			return;
 		cf.data[i] = (tmp << 4);
-		tmp = asc2nibble(sl->rbuff[dlc_pos++]);
-		if (tmp > 0x0F)
+		tmp = hex_to_bin(sl->rbuff[dlc_pos++]);
+		if (tmp < 0)
 			return;
 		cf.data[i] |= tmp;
 	}

What about changing

void hex2bin(u8 *dst, const char *src, size_t count)

to

bool hex2bin(u8 *dst, const char *src, size_t count)

in order to do error checks like

bool hex2bin_with_validation(u8 *dst, const char *src, size_t count)

{
	while (count--) {
		int c = hex_to_bin(*src++);
		int d;
		if (c < 0)
			return false;
		d = hex_to_bin(*src++)
		if (d < 0)
			return false;
		*dst++ = (c << 4) | d;
	}
	return true;
}
Quoted 6 lines
Quoted 1 line

and use hex2bin() rather than hex_to_bin()?

Perhaps, good idea. Could you submit a patch?

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw

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

Quoted 61 lines · Mimi Zohar
Quoted 36 lines · Tetsuo Handa

Currently, security/keys/ is the only user of hex2bin(). Should I keep hex2bin() unmodified in case of bad input? If so, I'd like to make it as hex2bin_safe().

----------------------------------------
[PATCH] 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.

---

include/linux/kernel.h +1 -1
index 953352a..186e9fc 100644
@@ -374,7 +374,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 bool hex2bin(u8 *dst, const char *src, size_t count);
 
  /*
   * General tracing related utility functions - trace_printk(),
lib/hexdump.c +2 -0
index f5fe6ba..1524002 100644
@@ -38,14 +38,22 @@ 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.

lib/hexdump.c EXPORT_SYMBOL(hex_to_bin); · line 48

Quoted 10 lines
lib/hexdump.c +3 -4
   */
-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;

Missing blank line here.

lib/hexdump.c EXPORT_SYMBOL(hex_to_bin); · line 58

Quoted 10 lines
lib/hexdump.c +7 -0
+             if (c < 0)
+                     return false;
+             d = hex_to_bin(*src++);
+             if (d < 0)
+                     return false;
+             *dst++ = (c << 4) | d;
       }
+     return true;
  }
  EXPORT_SYMBOL(hex2bin);
Quoted 1 line

We probably don't need to define a separate 'safe' function.

There is an opponent on any approach. Although, small and fast error route could be good.

Quoted 10 lines

Instead of changing the existing code to short circuit out and return a value, does only adding the return value work?  Something like:

       bool ret = true;
       int c, d;

       while (count--) {
               c = hex_to_bin(*src++);
               d = hex_to_bin(*src++);

Here is a performance issue, yeah. The user prefers to know about an error as soon as possible.

Quoted 4 lines

               *dst++ = (c << 4) | d;

               if (c < 0 || d < 0)
                       ret = false;

The ret value is redundant, and here you continue to fill the result array by something arbitrary (might be wrong data).

Quoted 71 lines

       }
       return ret;

thanks,

Mimi

Quoted 48 lines
lib/hexdump.c +0 -0
 In message "Re: [PATCH] net: can: remove custom hex_to_bin()",
Quoted 46 lines · Andy Shevchenko
Quoted 42 lines · Tetsuo Handa
Quoted 7 lines · Andy Shevchenko

        for (i = 0, dlc_pos++; i < cf.can_dlc; i++) { -
-               tmp = asc2nibble(sl->rbuff[dlc_pos++]);
-               if (tmp > 0x0F)
+               tmp = hex_to_bin(sl->rbuff[dlc_pos++]);
+               if (tmp < 0)
                        return;

Quoted 8 lines

                cf.data[i] = (tmp << 4);
-               tmp = asc2nibble(sl->rbuff[dlc_pos++]);
-               if (tmp > 0x0F)
+               tmp = hex_to_bin(sl->rbuff[dlc_pos++]);
+               if (tmp < 0)
                        return;
                cf.data[i] |= tmp;
        }

What about changing

  void hex2bin(u8 *dst, const char *src, size_t count)

to

  bool hex2bin(u8 *dst, const char *src, size_t count)

in order to do error checks like

bool hex2bin_with_validation(u8 *dst, const char *src, size_t count) {
  while (count--) {
          int c = hex_to_bin(*src++);
          int d;
          if (c < 0)
                  return false;
          d = hex_to_bin(*src++)
          if (d < 0)
                  return false;
          *dst++ = (c << 4) | d;
  }
  return true;
}

Quoted 1 line

and use hex2bin() rather than hex_to_bin()?

Perhaps, good idea. Could you submit a patch?

Quoted 8 lines
Quoted 3 lines

--
Andy Shevchenko <[email protected]> Intel Finland Oy

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

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

Quoted 62 lines · Andy Shevchenko
Quoted 61 lines · Mimi Zohar
Quoted 36 lines · Tetsuo Handa

Currently, security/keys/ is the only user of hex2bin(). Should I keep hex2bin() unmodified in case of bad input? If so, I'd like to make it as hex2bin_safe().

----------------------------------------
[PATCH] 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.

---

include/linux/kernel.h +1 -1
index 953352a..186e9fc 100644
@@ -374,7 +374,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 bool hex2bin(u8 *dst, const char *src, size_t count);
 
 /*
  * General tracing related utility functions - trace_printk(),
lib/hexdump.c +2 -0
index f5fe6ba..1524002 100644
@@ -38,14 +38,22 @@ 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.

lib/hexdump.c EXPORT_SYMBOL(hex_to_bin); · line 48

Quoted 10 lines
lib/hexdump.c +3 -4
  */
-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;

Missing blank line here.

Quoted 10 lines
lib/hexdump.c +7 -0
+             if (c < 0)
+                     return false;
+             d = hex_to_bin(*src++);
+             if (d < 0)
+                     return false;
+             *dst++ = (c << 4) | d;
      }
+     return true;
 }
 EXPORT_SYMBOL(hex2bin);
Quoted 3 lines
Quoted 1 line

We probably don't need to define a separate 'safe' function.

There is an opponent on any approach. Although, small and fast error route could be good.

As nothing but trusted/encrypted keys is using hex2bin, it shouldn't be a problem. :-) I'll update trusted/encrypted keys to check the return code.

thanks,

Mimi

Quoted 11 lines
Quoted 9 lines

Instead of changing the existing code to short circuit out and return a value, does only adding the return value work? Something like:

       bool ret = true;
       int c, d;

       while (count--) {
               c = hex_to_bin(*src++);
               d = hex_to_bin(*src++);

Here is a performance issue, yeah. The user prefers to know about an error as soon as possible.

ok

Quoted 11 lines
Quoted 4 lines
               *dst++ = (c << 4) | d;

               if (c < 0 || d < 0)
                       ret = false;

The ret value is redundant, and here you continue to fill the result array by something arbitrary (might be wrong data).

Quoted 3 lines
       }
       return ret;
#7 lore raw

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

Quoted 6 lines · Mimi Zohar
Quoted 3 lines
Quoted 1 line

We probably don't need to define a separate 'safe' function.

There is an opponent on any  approach. Although, small and fast error route could be good.

As nothing but trusted/encrypted keys is using hex2bin, it shouldn't be a problem.  :-)

The key word "until now". But people will start to use anything which has public API, won't they?

Quoted 2 lines

 I'll update trusted/encrypted keys to check the return code.

Actually another question shall we add __must_check to the prototype or not?

Signature · 6 lines
With Best Regards,
Andy Shevchenko
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#8 lore raw

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

Quoted 10 lines · Andy Shevchenko
Quoted 6 lines · Mimi Zohar
Quoted 3 lines
Quoted 1 line

We probably don't need to define a separate 'safe' function.

There is an opponent on any approach. Although, small and fast error route could be good.

As nothing but trusted/encrypted keys is using hex2bin, it shouldn't be a problem. :-)

The key word "until now". But people will start to use anything which has public API, won't they?

Someone with more experience than me needs to responds.

Quoted 3 lines
Quoted 2 lines

I'll update trusted/encrypted keys to check the return
code.

Actually another question shall we add __must_check to the prototype or not?

Probably a good idea.

thanks,

Mimi

#9 lore raw

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

In reply to Tetsuo Handa
Quoted 30 lines · Tetsuo Handa

Currently, security/keys/ is the only user of hex2bin(). Should I keep hex2bin() unmodified in case of bad input? If so, I'd like to make it as hex2bin_safe().
----------------------------------------
[PATCH] 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.

---

include/linux/kernel.h +1 -1
index 953352a..186e9fc 100644
@@ -374,7 +374,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 bool hex2bin(u8 *dst, const char *src, size_t count);
 
  /*
  * General tracing related utility functions - trace_printk(),
lib/hexdump.c +0 -0
index f5fe6ba..1524002 100644

lib/hexdump.c EXPORT_SYMBOL(hex_to_bin); · line 42

Quoted 5 lines
lib/hexdump.c +2 -0
@@ -38,14 +38,22 @@ 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.

What about making it return the number of unprocessed bytes left instead? Then the caller knows where the problem lies. And zero would mean success.

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#10 lore raw

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

Quoted 2 lines · Geert Uytterhoeven

What about making it return the number of unprocessed bytes left instead? Then the caller knows where the problem lies. And zero would mean success.

If I remember correctly it used to be src as return value in some version of that patch. I don't know the details of that interim solution. My current opinion is to return boolean and make an additional parameter to return src value. However, it could make this simple function fat.
P.S. Take into account that the user of it is only one so far, I would like to hear a Mimi's opinion.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#11 lore raw

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

Quoted 11 lines · Andy Shevchenko
Quoted 2 lines · Geert Uytterhoeven

What about making it return the number of unprocessed bytes left instead? Then the caller knows where the problem lies. And zero would mean success.

If I remember correctly it used to be src as return value in some version of that patch. I don't know the details of that interim solution. My current opinion is to return boolean and make an additional parameter to return src value. However, it could make this simple function fat.
P.S. Take into account that the user of it is only one so far, I would like to hear a Mimi's opinion.

Trusted/encrypted keys are not in a critical code path. They're used for loading/storing key blobs from userspace. Once you change the API, short circuiting out and adding an error return, from a trusted/encrypted key perspective, it doesn't make a difference.

thanks,

Mimi

#12 lore raw

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

In reply to Andy Shevchenko

(sorry for re-posting, but this doesn't seem to have been sent.)

Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Geert Uytterhoeven

What about making it return the number of unprocessed bytes left instead? Then the caller knows where the problem lies. And zero would mean success.

Quoted 8 lines

If I remember correctly it used to be src as return value in some version of that patch. I don't know the details of that interim solution. My current opinion is to return boolean and make an additional parameter to return src value. However, it could make this simple function fat.
P.S. Take into account that the user of it is only one so far, I would like to hear a Mimi's opinion.

Trusted/encrypted keys are not in a critical code path. They're used for loading/storing key blobs from userspace. Once you change the API, short circuiting out and adding an error return, from a trusted/encrypted key perspective, it doesn't make a difference.

thanks,

Mimi

#13 lore raw

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

In reply to Andy Shevchenko

(sorry for re-posting, but this doesn't seem to have made it to the lists.)

Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Geert Uytterhoeven

What about making it return the number of unprocessed bytes left instead? Then the caller knows where the problem lies. And zero would mean success.

Quoted 8 lines

If I remember correctly it used to be src as return value in some version of that patch. I don't know the details of that interim solution. My current opinion is to return boolean and make an additional parameter to return src value. However, it could make this simple function fat.
P.S. Take into account that the user of it is only one so far, I would like to hear a Mimi's opinion.

Trusted/encrypted keys are not in a critical code path. They're used for loading/storing key blobs from userspace. From a trusted/encrypted key perspective, it doesn't make much of a difference.

thanks,

Mimi

Acked-by on v1 1/1
#14 lore raw
In reply to Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
Cc Wolfgang Grandegger <[email protected]>
Quoted 61 lines
---
 drivers/net/can/slcan.c |   26 +++++---------------------
 1 files changed, 5 insertions(+), 21 deletions(-)
drivers/net/can/slcan.c +5 -21
index aa8ad73..65e54fd 100644
@@ -56,6 +56,7 @@
 #include <linux/sched.h>
 #include <linux/delay.h>
 #include <linux/init.h>
+#include <linux/kernel.h>
 #include <linux/can.h>
 
 static __initdata const char banner[] =
@@ -142,21 +143,6 @@ static struct net_device **slcan_devs;
   *			STANDARD SLCAN DECAPSULATION			 *
   ************************************************************************/
 
-static int asc2nibble(char c)
-{
-
-	if ((c >= '0') && (c <= '9'))
-		return c - '0';
-
-	if ((c >= 'A') && (c <= 'F'))
-		return c - 'A' + 10;
-
-	if ((c >= 'a') && (c <= 'f'))
-		return c - 'a' + 10;
-
-	return 16; /* error */
-}
-
 /* Send one completely decapsulated can_frame to the network layer */
 static void slc_bump(struct slcan *sl)
 {
@@ -195,18 +181,16 @@ static void slc_bump(struct slcan *sl)
 	*(u64 *) (&cf.data) = 0; /* clear payload */
 
 	for (i = 0, dlc_pos++; i < cf.can_dlc; i++) {
-
-		tmp = asc2nibble(sl->rbuff[dlc_pos++]);
-		if (tmp > 0x0F)
+		tmp = hex_to_bin(sl->rbuff[dlc_pos++]);
+		if (tmp < 0)
 			return;
 		cf.data[i] = (tmp << 4);
-		tmp = asc2nibble(sl->rbuff[dlc_pos++]);
-		if (tmp > 0x0F)
+		tmp = hex_to_bin(sl->rbuff[dlc_pos++]);
+		if (tmp < 0)
 			return;
 		cf.data[i] |= tmp;
 	}
 
-
 	skb = dev_alloc_skb(sizeof(struct can_frame));
 	if (!skb)
 		return;