All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] net: can: remove custom hex_to_bin()
@ 2011-07-18  8:26 Andy Shevchenko
  2011-07-18 11:41 ` Tetsuo Handa
  2011-07-18 14:02 ` [PATCH] net: can: remove custom hex_to_bin() Oliver Hartkopp
  0 siblings, 2 replies; 18+ messages in thread
From: Andy Shevchenko @ 2011-07-18  8:26 UTC (permalink / raw)
  To: netdev, linux-kernel; +Cc: Andy Shevchenko, Wolfgang Grandegger

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Wolfgang Grandegger <wg@grandegger.com>
---
 drivers/net/can/slcan.c |   26 +++++---------------------
 1 files changed, 5 insertions(+), 21 deletions(-)

diff --git a/drivers/net/can/slcan.c b/drivers/net/can/slcan.c
index aa8ad73..65e54fd 100644
--- a/drivers/net/can/slcan.c
+++ b/drivers/net/can/slcan.c
@@ -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;
-- 
1.7.5.4


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

* Re: [PATCH] net: can: remove custom hex_to_bin()
  2011-07-18  8:26 [PATCH] net: can: remove custom hex_to_bin() Andy Shevchenko
@ 2011-07-18 11:41 ` Tetsuo Handa
  2011-07-18 12:23   ` Andy Shevchenko
  2011-07-18 14:02 ` [PATCH] net: can: remove custom hex_to_bin() Oliver Hartkopp
  1 sibling, 1 reply; 18+ messages in thread
From: Tetsuo Handa @ 2011-07-18 11:41 UTC (permalink / raw)
  To: andriy.shevchenko; +Cc: netdev, linux-kernel

Andy Shevchenko wrote:
>  	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;
>  	}

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()?

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

* Re: [PATCH] net: can: remove custom hex_to_bin()
  2011-07-18 11:41 ` Tetsuo Handa
@ 2011-07-18 12:23   ` Andy Shevchenko
  2011-07-18 12:48     ` [PATCH] Add error check to hex2bin() Tetsuo Handa
  0 siblings, 1 reply; 18+ messages in thread
From: Andy Shevchenko @ 2011-07-18 12:23 UTC (permalink / raw)
  To: Tetsuo Handa; +Cc: netdev, linux-kernel

On Mon, 2011-07-18 at 20:41 +0900, Tetsuo Handa wrote: 
> Andy Shevchenko wrote:
> >  	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;
> >  	}
> 
> 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()?
Perhaps, good idea. Could you submit a patch?

-- 
Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Intel Finland Oy

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

* [PATCH] Add error check to hex2bin().
  2011-07-18 12:23   ` Andy Shevchenko
@ 2011-07-18 12:48     ` Tetsuo Handa
  2011-07-18 18:03       ` Mimi Zohar
  2011-07-18 20:49         ` Geert Uytterhoeven
  0 siblings, 2 replies; 18+ messages in thread
From: Tetsuo Handa @ 2011-07-18 12:48 UTC (permalink / raw)
  To: linux-security-module; +Cc: andriy.shevchenko, netdev, linux-kernel

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.

Signed-off-by: Tetsuo Handa <penguin-kernel@I=love.SAKURA.ne.jp>
---
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 953352a..186e9fc 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -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(),
diff --git a/lib/hexdump.c b/lib/hexdump.c
index f5fe6ba..1524002 100644
--- a/lib/hexdump.c
+++ b/lib/hexdump.c
@@ -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()",
Andy Shevchenko wrote:
> On Mon, 2011-07-18 at 20:41 +0900, Tetsuo Handa wrote: 
> > Andy Shevchenko wrote:
> > >  	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;
> > >  	}
> > 
> > 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()?
> Perhaps, good idea. Could you submit a patch?
> 
> -- 
> Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Intel Finland Oy

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

* Re: [PATCH] net: can: remove custom hex_to_bin()
  2011-07-18  8:26 [PATCH] net: can: remove custom hex_to_bin() Andy Shevchenko
  2011-07-18 11:41 ` Tetsuo Handa
@ 2011-07-18 14:02 ` Oliver Hartkopp
  2011-07-18 18:33   ` David Miller
  1 sibling, 1 reply; 18+ messages in thread
From: Oliver Hartkopp @ 2011-07-18 14:02 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: netdev, linux-kernel, Wolfgang Grandegger

On 18.07.2011 10:26, Andy Shevchenko wrote:
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Cc: Wolfgang Grandegger <wg@grandegger.com>

Acked-by: Oliver Hartkopp <socketcan@hartkopp.net>

> ---
>  drivers/net/can/slcan.c |   26 +++++---------------------
>  1 files changed, 5 insertions(+), 21 deletions(-)
> 
> diff --git a/drivers/net/can/slcan.c b/drivers/net/can/slcan.c
> index aa8ad73..65e54fd 100644
> --- a/drivers/net/can/slcan.c
> +++ b/drivers/net/can/slcan.c
> @@ -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;


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

* Re: [PATCH] Add error check to hex2bin().
  2011-07-18 12:48     ` [PATCH] Add error check to hex2bin() Tetsuo Handa
@ 2011-07-18 18:03       ` Mimi Zohar
  2011-07-18 18:57         ` Andy Shevchenko
  2011-07-18 20:49         ` Geert Uytterhoeven
  1 sibling, 1 reply; 18+ messages in thread
From: Mimi Zohar @ 2011-07-18 18:03 UTC (permalink / raw)
  To: Tetsuo Handa
  Cc: linux-security-module, andriy.shevchenko, netdev, linux-kernel

On Mon, 2011-07-18 at 21:48 +0900, Tetsuo Handa wrote:
> 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.
> 
> Signed-off-by: Tetsuo Handa <penguin-kernel@I=love.SAKURA.ne.jp>
> ---
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> index 953352a..186e9fc 100644
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -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(),
> diff --git a/lib/hexdump.c b/lib/hexdump.c
> index f5fe6ba..1524002 100644
> --- a/lib/hexdump.c
> +++ b/lib/hexdump.c
> @@ -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;

Missing blank line here.

> +		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

> In message "Re: [PATCH] net: can: remove custom hex_to_bin()",
> Andy Shevchenko wrote:
> > On Mon, 2011-07-18 at 20:41 +0900, Tetsuo Handa wrote: 
> > > Andy Shevchenko wrote:
> > > >  	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;
> > > >  	}
> > > 
> > > 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()?
> > Perhaps, good idea. Could you submit a patch?
> > 
> > -- 
> > Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> > Intel Finland Oy
> --
> To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 



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

* Re: [PATCH] net: can: remove custom hex_to_bin()
  2011-07-18 14:02 ` [PATCH] net: can: remove custom hex_to_bin() Oliver Hartkopp
@ 2011-07-18 18:33   ` David Miller
  0 siblings, 0 replies; 18+ messages in thread
From: David Miller @ 2011-07-18 18:33 UTC (permalink / raw)
  To: socketcan; +Cc: andriy.shevchenko, netdev, linux-kernel, wg

From: Oliver Hartkopp <socketcan@hartkopp.net>
Date: Mon, 18 Jul 2011 16:02:49 +0200

> On 18.07.2011 10:26, Andy Shevchenko wrote:
>> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
>> Cc: Wolfgang Grandegger <wg@grandegger.com>
> 
> Acked-by: Oliver Hartkopp <socketcan@hartkopp.net>

Applied.

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

* Re: [PATCH] Add error check to hex2bin().
  2011-07-18 18:03       ` Mimi Zohar
@ 2011-07-18 18:57         ` Andy Shevchenko
  2011-07-18 19:20           ` Mimi Zohar
  0 siblings, 1 reply; 18+ messages in thread
From: Andy Shevchenko @ 2011-07-18 18:57 UTC (permalink / raw)
  To: Mimi Zohar
  Cc: Tetsuo Handa, linux-security-module, andriy.shevchenko, netdev,
	linux-kernel

On Mon, Jul 18, 2011 at 9:03 PM, Mimi Zohar <zohar@linux.vnet.ibm.com> wrote:
> On Mon, 2011-07-18 at 21:48 +0900, Tetsuo Handa wrote:
>> 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.
>>
>> Signed-off-by: Tetsuo Handa <penguin-kernel@I=love.SAKURA.ne.jp>
>> ---
>> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
>> index 953352a..186e9fc 100644
>> --- a/include/linux/kernel.h
>> +++ b/include/linux/kernel.h
>> @@ -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(),
>> diff --git a/lib/hexdump.c b/lib/hexdump.c
>> index f5fe6ba..1524002 100644
>> --- a/lib/hexdump.c
>> +++ b/lib/hexdump.c
>> @@ -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;
>
> Missing blank line here.
>
>> +             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.
There is an opponent on any  approach. Although, small and fast error
route could be good.

>
> 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.

>                *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).

>        }
>        return ret;
>
> thanks,
>
> Mimi
>
>> In message "Re: [PATCH] net: can: remove custom hex_to_bin()",
>> Andy Shevchenko wrote:
>> > On Mon, 2011-07-18 at 20:41 +0900, Tetsuo Handa wrote:
>> > > Andy Shevchenko wrote:
>> > > >         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;
>> > > >         }
>> > >
>> > > 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()?
>> > Perhaps, good idea. Could you submit a patch?
>> >
>> > --
>> > Andy Shevchenko <andriy.shevchenko@linux.intel.com>
>> > Intel Finland Oy
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
>> the body of a message to majordomo@vger.kernel.org
>> 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 majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/
>



-- 
With Best Regards,
Andy Shevchenko

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

* Re: [PATCH] Add error check to hex2bin().
  2011-07-18 18:57         ` Andy Shevchenko
@ 2011-07-18 19:20           ` Mimi Zohar
  2011-07-18 19:44               ` Andy Shevchenko
  0 siblings, 1 reply; 18+ messages in thread
From: Mimi Zohar @ 2011-07-18 19:20 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Tetsuo Handa, linux-security-module, andriy.shevchenko, netdev,
	linux-kernel

On Mon, 2011-07-18 at 21:57 +0300, Andy Shevchenko wrote:
> On Mon, Jul 18, 2011 at 9:03 PM, Mimi Zohar <zohar@linux.vnet.ibm.com> wrote:
> > On Mon, 2011-07-18 at 21:48 +0900, Tetsuo Handa wrote:
> >> 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.
> >>
> >> Signed-off-by: Tetsuo Handa <penguin-kernel@I=love.SAKURA.ne.jp>
> >> ---
> >> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> >> index 953352a..186e9fc 100644
> >> --- a/include/linux/kernel.h
> >> +++ b/include/linux/kernel.h
> >> @@ -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(),
> >> diff --git a/lib/hexdump.c b/lib/hexdump.c
> >> index f5fe6ba..1524002 100644
> >> --- a/lib/hexdump.c
> >> +++ b/lib/hexdump.c
> >> @@ -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;
> >
> > Missing blank line here.
> >
> >> +             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.
> 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

> > 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

> >                *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).

> 
> >        }
> >        return ret;
> >



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

* Re: [PATCH] Add error check to hex2bin().
  2011-07-18 19:20           ` Mimi Zohar
@ 2011-07-18 19:44               ` Andy Shevchenko
  0 siblings, 0 replies; 18+ messages in thread
From: Andy Shevchenko @ 2011-07-18 19:44 UTC (permalink / raw)
  To: Mimi Zohar
  Cc: Tetsuo Handa, linux-security-module, andriy.shevchenko, netdev,
	linux-kernel

On Mon, Jul 18, 2011 at 10:20 PM, Mimi Zohar <zohar@linux.vnet.ibm.com> wrote:

>> > 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?

>  I'll update trusted/encrypted keys to check the return
> code.
Actually another question shall we add __must_check to the prototype or not?

-- 
With Best Regards,
Andy Shevchenko

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

* Re: [PATCH] Add error check to hex2bin().
@ 2011-07-18 19:44               ` Andy Shevchenko
  0 siblings, 0 replies; 18+ messages in thread
From: Andy Shevchenko @ 2011-07-18 19:44 UTC (permalink / raw)
  To: Mimi Zohar
  Cc: Tetsuo Handa, linux-security-module, andriy.shevchenko, netdev,
	linux-kernel

On Mon, Jul 18, 2011 at 10:20 PM, Mimi Zohar <zohar@linux.vnet.ibm.com> wrote:

>> > 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?

>  I'll update trusted/encrypted keys to check the return
> code.
Actually another question shall we add __must_check to the prototype or not?

-- 
With Best Regards,
Andy Shevchenko
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [PATCH] Add error check to hex2bin().
  2011-07-18 12:48     ` [PATCH] Add error check to hex2bin() Tetsuo Handa
@ 2011-07-18 20:49         ` Geert Uytterhoeven
  2011-07-18 20:49         ` Geert Uytterhoeven
  1 sibling, 0 replies; 18+ messages in thread
From: Geert Uytterhoeven @ 2011-07-18 20:49 UTC (permalink / raw)
  To: Tetsuo Handa
  Cc: linux-security-module, andriy.shevchenko, netdev, linux-kernel

On Mon, Jul 18, 2011 at 14:48, Tetsuo Handa
<penguin-kernel@i-love.sakura.ne.jp> wrote:
> 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.
>
> Signed-off-by: Tetsuo Handa <penguin-kernel@I=love.SAKURA.ne.jp>
> ---
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> index 953352a..186e9fc 100644
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -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(),
> diff --git a/lib/hexdump.c b/lib/hexdump.c
> index f5fe6ba..1524002 100644
> --- a/lib/hexdump.c
> +++ b/lib/hexdump.c
> @@ -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 -- geert@linux-m68k.org

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

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

* Re: [PATCH] Add error check to hex2bin().
@ 2011-07-18 20:49         ` Geert Uytterhoeven
  0 siblings, 0 replies; 18+ messages in thread
From: Geert Uytterhoeven @ 2011-07-18 20:49 UTC (permalink / raw)
  To: Tetsuo Handa
  Cc: linux-security-module, andriy.shevchenko, netdev, linux-kernel

On Mon, Jul 18, 2011 at 14:48, Tetsuo Handa
<penguin-kernel@i-love.sakura.ne.jp> wrote:
> 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.
>
> Signed-off-by: Tetsuo Handa <penguin-kernel@I=love.SAKURA.ne.jp>
> ---
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> index 953352a..186e9fc 100644
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -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(),
> diff --git a/lib/hexdump.c b/lib/hexdump.c
> index f5fe6ba..1524002 100644
> --- a/lib/hexdump.c
> +++ b/lib/hexdump.c
> @@ -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 -- geert@linux-m68k.org

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 majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [PATCH] Add error check to hex2bin().
  2011-07-18 20:49         ` Geert Uytterhoeven
  (?)
@ 2011-07-18 21:18         ` Andy Shevchenko
  2011-07-18 21:59           ` Mimi Zohar
                             ` (2 more replies)
  -1 siblings, 3 replies; 18+ messages in thread
From: Andy Shevchenko @ 2011-07-18 21:18 UTC (permalink / raw)
  To: Geert Uytterhoeven, Mimi Zohar
  Cc: Tetsuo Handa, linux-security-module, andriy.shevchenko, netdev,
	linux-kernel

On Mon, Jul 18, 2011 at 11:49 PM, Geert Uytterhoeven
<geert@linux-m68k.org> wrote:
> 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.

-- 
With Best Regards,
Andy Shevchenko

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

* Re: [PATCH] Add error check to hex2bin().
  2011-07-18 19:44               ` Andy Shevchenko
  (?)
@ 2011-07-18 21:43               ` Mimi Zohar
  -1 siblings, 0 replies; 18+ messages in thread
From: Mimi Zohar @ 2011-07-18 21:43 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Tetsuo Handa, linux-security-module, andriy.shevchenko, netdev,
	linux-kernel

On Mon, 2011-07-18 at 22:44 +0300, Andy Shevchenko wrote:
> On Mon, Jul 18, 2011 at 10:20 PM, Mimi Zohar <zohar@linux.vnet.ibm.com> wrote:
> 
> >> > 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.

> >  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



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

* Re: [PATCH] Add error check to hex2bin().
  2011-07-18 21:18         ` Andy Shevchenko
@ 2011-07-18 21:59           ` Mimi Zohar
  2011-07-19  1:00           ` Mimi Zohar
  2011-07-19 10:52           ` Mimi Zohar
  2 siblings, 0 replies; 18+ messages in thread
From: Mimi Zohar @ 2011-07-18 21:59 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Geert Uytterhoeven, Tetsuo Handa, linux-security-module,
	andriy.shevchenko, netdev, linux-kernel

On Tue, 2011-07-19 at 00:18 +0300, Andy Shevchenko wrote:
> On Mon, Jul 18, 2011 at 11:49 PM, Geert Uytterhoeven
> <geert@linux-m68k.org> wrote:
> > 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


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

* Re: [PATCH] Add error check to hex2bin().
  2011-07-18 21:18         ` Andy Shevchenko
  2011-07-18 21:59           ` Mimi Zohar
@ 2011-07-19  1:00           ` Mimi Zohar
  2011-07-19 10:52           ` Mimi Zohar
  2 siblings, 0 replies; 18+ messages in thread
From: Mimi Zohar @ 2011-07-19  1:00 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Geert Uytterhoeven, Tetsuo Handa, linux-security-module,
	andriy.shevchenko, netdev, linux-kernel

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

On Tue, 2011-07-19 at 00:18 +0300, Andy Shevchenko wrote:
> On Mon, Jul 18, 2011 at 11:49 PM, Geert Uytterhoeven
> <geert@linux-m68k.org> wrote:
> > 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


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

* Re: [PATCH] Add error check to hex2bin().
  2011-07-18 21:18         ` Andy Shevchenko
  2011-07-18 21:59           ` Mimi Zohar
  2011-07-19  1:00           ` Mimi Zohar
@ 2011-07-19 10:52           ` Mimi Zohar
  2 siblings, 0 replies; 18+ messages in thread
From: Mimi Zohar @ 2011-07-19 10:52 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Geert Uytterhoeven, Tetsuo Handa, linux-security-module,
	andriy.shevchenko, netdev, linux-kernel

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

On Tue, 2011-07-19 at 00:18 +0300, Andy Shevchenko wrote:
> On Mon, Jul 18, 2011 at 11:49 PM, Geert Uytterhoeven
> <geert@linux-m68k.org> wrote:
> > 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.  From a trusted/encrypted key
perspective, it doesn't make much of a difference.

thanks,

Mimi


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

end of thread, other threads:[~2011-07-19 14:34 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-18  8:26 [PATCH] net: can: remove custom hex_to_bin() Andy Shevchenko
2011-07-18 11:41 ` Tetsuo Handa
2011-07-18 12:23   ` Andy Shevchenko
2011-07-18 12:48     ` [PATCH] Add error check to hex2bin() Tetsuo Handa
2011-07-18 18:03       ` Mimi Zohar
2011-07-18 18:57         ` Andy Shevchenko
2011-07-18 19:20           ` Mimi Zohar
2011-07-18 19:44             ` Andy Shevchenko
2011-07-18 19:44               ` Andy Shevchenko
2011-07-18 21:43               ` Mimi Zohar
2011-07-18 20:49       ` Geert Uytterhoeven
2011-07-18 20:49         ` Geert Uytterhoeven
2011-07-18 21:18         ` Andy Shevchenko
2011-07-18 21:59           ` Mimi Zohar
2011-07-19  1:00           ` Mimi Zohar
2011-07-19 10:52           ` Mimi Zohar
2011-07-18 14:02 ` [PATCH] net: can: remove custom hex_to_bin() Oliver Hartkopp
2011-07-18 18:33   ` David Miller

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.