Skip to contentSkip to conversation

ErrHandling:Make IS_ERR_VALUE_U32 as generic API to avoid IS_ERR_VALUE abuses.

PATCH In review Arvind Yadav 2 participants 2 messages Jul 9, 2016 – Sep 3, 2016 Broadcom specific amba driver (bcma)

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ErrHandling:Make IS_ERR_VALUE_U32 as generic API to avoid IS_ERR_VALUE abuses.

IS_ERR_VALUE() assumes that its parameter is an unsigned long. It can not be used to check if an 'unsigned int' reflects an error. As they pass an 'unsigned int' into a function that takes an 'unsigned long' argument. This happens to work because the type is sign-extended on 64-bit architectures before it gets converted into an unsigned type.

However, anything that passes an 'unsigned short' or 'unsigned int' argument into IS_ERR_VALUE() is guaranteed to be broken, as are 8-bit integers and types that are wider than 'unsigned long'.

It would be nice to any users that are not passing 'unsigned int' arguments.

---
 drivers/bcma/scan.c | 1 -
 include/linux/err.h | 2 ++
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/bcma/scan.c +0 -1
index 4a2d1b2..3bc77eb 100644
@@ -272,7 +272,6 @@ static struct bcma_device *bcma_find_core_reverse(struct bcma_bus *bus, u16 core
 	return NULL;
 }
 
-#define IS_ERR_VALUE_U32(x) ((x) >= (u32)-MAX_ERRNO)
 
 static int bcma_get_next_core(struct bcma_bus *bus, u32 __iomem **eromptr,
 			      struct bcma_device_id *match, int core_num,
include/linux/err.h +2 -0
index 1e35588..e05a63d 100644
@@ -20,6 +20,8 @@
 
 #define IS_ERR_VALUE(x) unlikely((unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO)
 
+#define IS_ERR_VALUE_U32(x) unlikely((unsigned int)(x) >= (unsigned int)-MAX_ERRNO)
+
 static inline void * __must_check ERR_PTR(long error)
 {
 	return (void *) error;
Signature · 1 line
1.9.1
#1 lore raw
Quoted 7 lines · Arvind Yadav

IS_ERR_VALUE() assumes that its parameter is an unsigned long. It can not be used to check if an 'unsigned int' reflects an error. As they pass an 'unsigned int' into a function that takes an 'unsigned long' argument. This happens to work because the type is sign-extended on 64-bit architectures before it gets converted into an unsigned type.

Quoted 8 lines

However, anything that passes an 'unsigned short' or 'unsigned int' argument into IS_ERR_VALUE() is guaranteed to be broken, as are 8-bit integers and types that are wider than 'unsigned long'.

It would be nice to any users that are not passing 'unsigned int' arguments.

This touches include/linux/err.h and I'm not very enthusiastic to change anything in include directory without wider support. I recommend first to just fix bcma. And separately you can try to improve linux/err.h via some more approariate tree, not via wireless trees.

Signature · 2 lines
Sent by pwcli
https://patchwork.kernel.org/patch/9222139/