Skip to contentSkip to conversation

RDMA/uverbs: check for allocation failure in uapi_add_elm()

PATCH Applied Dan Carpenter 2 participants 2 messages May 30, 2019

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

PATCH 1/1 RDMA/uverbs: check for allocation failure in uapi_add_elm()

If the kzalloc() fails then we should return ERR_PTR(-ENOMEM). In the current code it's possible that the kzalloc() fails and the radix_tree_insert() inserts the NULL pointer successfully and we return the NULL "elm" pointer to the caller. That results in a NULL pointer dereference.

Fixes 9ed3e5f44772 ("IB/uverbs: Build the specs into a radix tree at runtime")
---
 drivers/infiniband/core/uverbs_uapi.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/infiniband/core/uverbs_uapi.c +2 -0
index 7a987acf0c0b..ccc4be0a6566 100644
@@ -22,6 +22,8 @@ static void *uapi_add_elm(struct uverbs_api *uapi, u32 key, size_t alloc_size)
 		return ERR_PTR(-EOVERFLOW);
 
 	elm = kzalloc(alloc_size, GFP_KERNEL);
+	if (!elm)
+		return ERR_PTR(-ENOMEM);
 	rc = radix_tree_insert(&uapi->radix, key, elm);
 	if (rc) {
 		kfree(elm);
Signature · 1 line
2.20.1
#1 lore raw
Quoted 11 lines · Dan Carpenter

If the kzalloc() fails then we should return ERR_PTR(-ENOMEM). In the current code it's possible that the kzalloc() fails and the radix_tree_insert() inserts the NULL pointer successfully and we return the NULL "elm" pointer to the caller. That results in a NULL pointer dereference.

Fixes 9ed3e5f44772 ("IB/uverbs: Build the specs into a radix tree at runtime")
---
 drivers/infiniband/core/uverbs_uapi.c | 2 ++
 1 file changed, 2 insertions(+)

Applied to for-next, thanks

Jason