Skip to content

Commit 2e8039c

Browse files
mikijoyjgunthorpe
authored andcommitted
IB/core: uverbs copy to struct or zero helper
Add a helper to zero fill fields before copying data to UVERBS_ATTR_STRUCT. As UVERBS_ATTR_STRUCT can be used as an extensible struct, we want to make sure that if the user supplies us with a struct that has new fields that we are not aware of, we return them zeroed to the user. This helper should be used when using UVERBS_ATTR_STRUCT for an extendable data structure and there is a need to make sure that extended members of the struct, that the kernel doesn't handle, are returned zeroed to the user. This is needed due to the fact that UVERBS_ATTR_STRUCT allows non-zero values for members after 'last' member. Signed-off-by: Michael Guralnik <[email protected]> Reviewed-by: Majd Dibbiny <[email protected]> Signed-off-by: Leon Romanovsky <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent f55c3ec commit 2e8039c

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

drivers/infiniband/core/uverbs_ioctl.c

+11
Original file line numberDiff line numberDiff line change
@@ -751,3 +751,14 @@ int _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
751751
return 0;
752752
}
753753
EXPORT_SYMBOL(_uverbs_get_const);
754+
755+
int uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
756+
size_t idx, const void *from, size_t size)
757+
{
758+
const struct uverbs_attr *attr = uverbs_attr_get(bundle, idx);
759+
760+
if (clear_user(u64_to_user_ptr(attr->ptr_attr.data),
761+
attr->ptr_attr.len))
762+
return -EFAULT;
763+
return uverbs_copy_to(bundle, idx, from, size);
764+
}

include/rdma/uverbs_ioctl.h

+8
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,8 @@ static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
871871
int _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
872872
size_t idx, s64 lower_bound, u64 upper_bound,
873873
s64 *def_val);
874+
int uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
875+
size_t idx, const void *from, size_t size);
874876
#else
875877
static inline int
876878
uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
@@ -906,6 +908,12 @@ _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
906908
{
907909
return -EINVAL;
908910
}
911+
static inline int
912+
uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
913+
size_t idx, const void *from, size_t size)
914+
{
915+
return -EINVAL;
916+
}
909917
#endif
910918

911919
#define uverbs_get_const(_to, _attrs_bundle, _idx) \

0 commit comments

Comments
 (0)