Skip to content

Conversation

@zengyijing
Copy link
Contributor

@zengyijing zengyijing commented Dec 8, 2025

fill_attr_in() unconditionally calls memcpy() when len <= sizeof(u64), even if len is zero. In commit d9af497 ("verbs: Add ibv_cmd_alloc/free commands for DMA handle"), the call

fill_attr_in_enum(cmdb, UVERBS_ATTR_ALLOC_DMAH_TPH_MEM_TYPE,
		  attr->tph_mem_type, NULL, 0);

started passing a NULL data pointer together with len == 0, which leads to memcpy() being invoked with a NULL source address. While nothing is actually copied, some compilers and sanitizers treat this as undefined behavior and emit errors.

Avoid this by skipping memcpy() when len is zero. Zero-length attributes have no payload, so this does not change behavior.

fill_attr_in() was originally introduced in commit c344635 ("verbs: Add basic infrastructure support for the kabi ioctl").

Fixes: d9af497 ("verbs: Add ibv_cmd_alloc/free commands for DMA handle")
Signed-off-by: Yijing Zeng [email protected]

@zengyijing zengyijing force-pushed the fix-fill-attr-zero-len branch from 807eb70 to 65a5630 Compare December 8, 2025 05:05
@rleon
Copy link
Member

rleon commented Dec 16, 2025

Actually, using NULL as a pointer is declared as undefined behavior in C99 spec, section 7.1.4.

What are the compilers and static analyze tools which complain?

@zengyijing
Copy link
Contributor Author

@rleon UndefinedBehaviorSanitizer (UBSan) complains about invalid-null-argument during run time

cmd_ioctl.h:284:23: runtime error: null pointer passed as argument 2, which is declared to never be null

@rleon
Copy link
Member

rleon commented Dec 17, 2025

@rleon UndefinedBehaviorSanitizer (UBSan) complains about invalid-null-argument during run time

cmd_ioctl.h:284:23: runtime error: null pointer passed as argument 2, which is declared to never be null

And this more makes sense and align with C99 specification. You should update your commit and description to check for NULL pointer and not for zero length.

@zengyijing zengyijing force-pushed the fix-fill-attr-zero-len branch from 65a5630 to 99a724f Compare January 20, 2026 01:09
@zengyijing zengyijing changed the title libibverbs: Skip zero-length memcpy in fill_attr_in() libibverbs: Avoid memcpy from NULL in fill_attr_in() Jan 20, 2026
@zengyijing
Copy link
Contributor Author

@rleon the code & commit msg are updated

fill_attr_in() unconditionally calls memcpy() when len <= sizeof(u64),
regardless of whether the data pointer is NULL. In commit d9af497
("verbs: Add ibv_cmd_alloc/free commands for DMA handle"), the call

	fill_attr_in_enum(cmdb, UVERBS_ATTR_ALLOC_DMAH_TPH_MEM_TYPE,
			  attr->tph_mem_type, NULL, 0);

started passing a NULL data pointer together with len == 0, which leads
to memcpy() being invoked with a NULL source address. While nothing is
actually copied, some compilers and sanitizers treat this as undefined
behavior and emit errors.

Fix this by only inlining small attributes when len <= sizeof(u64) and
the data pointer is non-NULL. In all other cases, including zero-length
attributes with a NULL data pointer, the pointer is stored via
ioctl_ptr_to_u64() instead. This preserves the existing behavior for
valid callers while avoiding memcpy() from NULL.

fill_attr_in() was originally introduced in commit c344635 ("verbs:
Add basic infrastructure support for the kabi ioctl").

Fixes: d9af497 ("verbs: Add ibv_cmd_alloc/free commands for DMA handle")
Signed-off-by: Yijing Zeng <[email protected]>
@zengyijing zengyijing force-pushed the fix-fill-attr-zero-len branch from 99a724f to 1de4f92 Compare January 22, 2026 05:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants