Introduce new wrappers to copy user-arrays
Subsystems (7)
v3 cover Introduce new wrappers to copy user-arrays
Hi!
David Airlie suggested that we could implement new wrappers around (v)memdup_user() for duplicating user arrays.
This small patch series first implements the two new wrapper functions memdup_array_user() and vmemdup_array_user(). They calculate the array-sizes safely, i.e., they return an error in case of an overflow.
It then implements the new wrappers in two components in kernel/ and two in the drm-subsystem.
In total, there are 18 files in the kernel that use (v)memdup_user() to duplicate arrays. My plan is to provide patches for the other 14 successively once this series has been merged.
Changes since v2:
- Remove the unlikely() from the overflow-check, since the latter already implements one (Dan Carpenter)
- Add the Reviewd-bys for the changes already reviewed in v2
Changes since v1:
- Insert new headers alphabetically ordered
- Remove empty lines in functions' docstrings
- Return -EOVERFLOW instead of -EINVAL from wrapper functions
P.
Philipp Stanner (5):
string.h: add array-wrappers for (v)memdup_user()
kernel: kexec: copy user-array safely
kernel: watch_queue: copy user-array safely drm_lease.c: copy user-array safely
drm: vmgfx_surface.c: copy user-array safely
drivers/gpu/drm/drm_lease.c | 4 +-- drivers/gpu/drm/vmwgfx/vmwgfx_surface.c | 4 +-- include/linux/string.h | 40 +++++++++++++++++++++++++ kernel/kexec.c | 2 +- kernel/watch_queue.c | 2 +- 5 files changed, 46 insertions(+), 6 deletions(-)
Signature · 1 line
2.41.0
PATCH v3 1/5 string.h: add array-wrappers for (v)memdup_user()
Currently, user array duplications are sometimes done without an overflow check. Sometimes the checks are done manually; sometimes the array size is calculated with array_size() and sometimes by calculating n * size directly in code.
Introduce wrappers for arrays for memdup_user() and vmemdup_user() to provide a standardized and safe way for duplicating user arrays.
This is both for new code as well as replacing usage of (v)memdup_user() in existing code that uses, e.g., n * size to calculate array sizes.
--- include/linux/string.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+)
include/linux/string.h +40 -0
@@ -5,7 +5,9 @@#include <linux/compiler.h> /* for inline */ #include <linux/types.h> /* for size_t */ #include <linux/stddef.h> /* for NULL */ +#include <linux/err.h> /* for ERR_PTR() */ #include <linux/errno.h> /* for E2BIG */ +#include <linux/overflow.h> /* for check_mul_overflow() */ #include <linux/stdarg.h> #include <uapi/linux/string.h>
@@ -14,6 +16,44 @@ extern void *memdup_user(const void __user *, size_t);extern void *vmemdup_user (const void __user *,size_t );extern void *memdup_user_nul (const void __user *,size_t ); +/** +* memdup_array_user - duplicate array from user space +* @src: source address in user space +* @n: number of array members to copy +* @size: size of one array member +* +* Return: an ERR_PTR() on failure. Result is physically +* contiguous, to be freed by kfree(). +*/ +static inline void *memdup_array_user (const void __user *src,size_t n,size_t size) +{ +size_t nbytes; + +if (check_mul_overflow (n, size, &nbytes)) +return ERR_PTR (-EOVERFLOW ); + +return memdup_user (src, nbytes); +} + +/** +* vmemdup_array_user - duplicate array from user space +* @src: source address in user space +* @n: number of array members to copy +* @size: size of one array member +* +* Return: an ERR_PTR() on failure. Result may be not +* physically contiguous. Use kvfree() to free. +*/ +static inline void *vmemdup_array_user (const void __user *src,size_t n,size_t size) +{ +size_t nbytes; + +if (check_mul_overflow (n, size, &nbytes)) +return ERR_PTR (-EOVERFLOW ); + +return vmemdup_user (src, nbytes); +} +/* * Include machine specific inline routines */
Signature · 1 line
2.41.0
PATCH v3 2/5 kernel: kexec: copy user-array safely
In reply to Philipp StannerCurrently, there is no overflow-check with memdup_user().
Use the new function memdup_array_user() instead of memdup_user() for duplicating the user-space array safely.
--- kernel/kexec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/kexec.c +1 -1
@@ -247,7 +247,7 @@ SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments, ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))return -EINVAL ; - ksegments =memdup_user (segments, nr_segments *sizeof (ksegments[0 ])); + ksegments =memdup_array_user (segments, nr_segments,sizeof (ksegments[0 ]));if (IS_ERR (ksegments))return PTR_ERR (ksegments);
Signature · 7 lines
2.41.0 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v3 3/5 kernel: watch_queue: copy user-array safely
In reply to Philipp StannerCurrently, there is no overflow-check with memdup_user().
Use the new function memdup_array_user() instead of memdup_user() for duplicating the user-space array safely.
--- kernel/watch_queue.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/watch_queue.c +1 -1
@@ -331,7 +331,7 @@ long watch_queue_set_filter(struct pipe_inode_info *pipe, filter.__reserved != 0)return -EINVAL ; - tf =memdup_user (_filter->filters, filter.nr_filters *sizeof (*tf)); + tf =memdup_array_user (_filter->filters, filter.nr_filters,sizeof (*tf));if (IS_ERR (tf))return PTR_ERR (tf);
Signature · 1 line
2.41.0
PATCH v3 4/5 drm_lease.c: copy user-array safely
In reply to Philipp StannerCurrently, there is no overflow-check with memdup_user().
Use the new function memdup_array_user() instead of memdup_user() for duplicating the user-space array safely.
--- drivers/gpu/drm/drm_lease.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_lease.c +2 -2
@@ -510,8 +510,8 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,/* Handle leased objects, if any */ idr_init (&leases);if (object_count !=0 ) { - object_ids =memdup_user (u64_to_user_ptr (cl->object_ids), -array_size (object_count,sizeof (__u32))); + object_ids =memdup_array_user (u64_to_user_ptr (cl->object_ids), + object_count,sizeof (__u32));if (IS_ERR (object_ids)) { ret =PTR_ERR (object_ids);idr_destroy (&leases);
Signature · 1 line
2.41.0
PATCH v3 5/5 drm: vmgfx_surface.c: copy user-array safely
In reply to Philipp StannerCurrently, there is no overflow-check with memdup_user().
Use the new function memdup_array_user() instead of memdup_user() for duplicating the user-space array safely.
--- drivers/gpu/drm/vmwgfx/vmwgfx_surface.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +2 -2
@@ -777,9 +777,9 @@ int vmw_surface_define_ioctl(struct drm_device *dev, void *data, sizeof(metadata->mip_levels)); metadata->num_sizes = num_sizes; metadata->sizes = -memdup_user ((struct drm_vmw_size __user *)(unsigned long ) +memdup_array_user ((struct drm_vmw_size __user *)(unsigned long ) req->size_addr, -sizeof (*metadata->sizes) * metadata->num_sizes); + metadata->num_sizes,sizeof (*metadata->sizes));if (IS_ERR (metadata->sizes)) { ret =PTR_ERR (metadata->sizes); goto out_no_sizes;
Signature · 1 line
2.41.0