Skip to contentSkip to conversation

IB/mlx5: Fix potential NULL dereference in query_device

PATCH In review Prathamesh Deshpande 2 participants 4 messages Mar 31

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

PATCH 1/1 IB/mlx5: Fix potential NULL dereference in query_device

Smatch reported an inconsistent NULL check for 'uhw' in mlx5_ib_query_device(). While 'uhw_outlen' is checked at the end of the function before calling ib_copy_to_udata(), 'uhw' is explicitly checked for NULL earlier in the same function.

If a caller provides a non-zero 'uhw_outlen' but a NULL 'uhw' pointer, ib_copy_to_udata() will attempt to dereference 'uhw', leading to a kernel panic. Fix this by checking the 'uhw' pointer directly.

Fixes 4835709176e8 ("RDMA/mlx5: Don't fake udata for kernel path")
---
 drivers/infiniband/hw/mlx5/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/infiniband/hw/mlx5/main.c +1 -1
index 635002e684a5..471dc8df4a52 100644
@@ -1353,7 +1353,7 @@ static int mlx5_ib_query_device(struct ib_device *ibdev,
 			fill_esw_mgr_reg_c0(mdev, &resp);
 	}
 
-	if (uhw_outlen) {
+	if (uhw) {
 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
 
 		if (err)
Signature · 1 line
2.43.0
#1 lore raw
Quoted 7 lines · Prathamesh Deshpande

Smatch reported an inconsistent NULL check for 'uhw' in mlx5_ib_query_device(). While 'uhw_outlen' is checked at the end of the function before calling ib_copy_to_udata(), 'uhw' is explicitly checked for NULL earlier in the same function.

If a caller provides a non-zero 'uhw_outlen' but a NULL 'uhw' pointer, ib_copy_to_udata() will attempt to dereference 'uhw',

How is it possible?

#2 lore raw
Quoted 10 lines · Leon Romanovsky
Quoted 7 lines · Prathamesh Deshpande

Smatch reported an inconsistent NULL check for 'uhw' in mlx5_ib_query_device(). While 'uhw_outlen' is checked at the end of the function before calling ib_copy_to_udata(), 'uhw' is explicitly checked for NULL earlier in the same function.

If a caller provides a non-zero 'uhw_outlen' but a NULL 'uhw' pointer, ib_copy_to_udata() will attempt to dereference 'uhw',

How is it possible?

Hi Leon,

You are right that in the current uverbs paths, 'uhw_outlen' and 'uhw' should stay in sync.

However, Smatch flags this as an inconsistency because 'uhw' is explicitly checked for NULL earlier in this same function (at line 968). If the code assumes 'uhw' could be NULL there, it is safer and more consistent to check the pointer directly before passing it to ib_copy_to_udata() at line 1357.

This prevents any future refactoring or unconventional kernel-space callers from accidentally triggering a NULL dereference.

What do you think?

Thanks,
Prathamesh

#3 lore raw
Quoted 17 lines · Prathamesh Deshpande
Quoted 10 lines · Leon Romanovsky
Quoted 7 lines · Prathamesh Deshpande

Smatch reported an inconsistent NULL check for 'uhw' in mlx5_ib_query_device(). While 'uhw_outlen' is checked at the end of the function before calling ib_copy_to_udata(), 'uhw' is explicitly checked for NULL earlier in the same function.

If a caller provides a non-zero 'uhw_outlen' but a NULL 'uhw' pointer, ib_copy_to_udata() will attempt to dereference 'uhw',

How is it possible?

Hi Leon,

You are right that in the current uverbs paths, 'uhw_outlen' and 'uhw' should stay in sync.

Quoted 8 lines

However, Smatch flags this as an inconsistency because 'uhw' is explicitly checked for NULL earlier in this same function (at line 968). If the code assumes 'uhw' could be NULL there, it is safer and more consistent to check the pointer directly before passing it to ib_copy_to_udata() at line 1357.

This prevents any future refactoring or unconventional kernel-space callers from accidentally triggering a NULL dereference.

Kernel-space callers don't use uverbs path. It is solely for the user-space access.

Thanks

Quoted 5 lines

What do you think?

Thanks,
Prathamesh