Skip to content

Commit 6309863

Browse files
edumazetkuba-moo
authored andcommitted
net: add copy_safe_from_sockptr() helper
copy_from_sockptr() helper is unsafe, unless callers did the prior check against user provided optlen. Too many callers get this wrong, lets add a helper to fix them and avoid future copy/paste bugs. Instead of : if (optlen < sizeof(opt)) { err = -EINVAL; break; } if (copy_from_sockptr(&opt, optval, sizeof(opt)) { err = -EFAULT; break; } Use : err = copy_safe_from_sockptr(&opt, sizeof(opt), optval, optlen); if (err) break; Signed-off-by: Eric Dumazet <edumazet@google.com> Link: https://lore.kernel.org/r/20240408082845.3957374-2-edumazet@google.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent cf1b720 commit 6309863

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

include/linux/sockptr.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,36 @@ static inline int copy_from_sockptr_offset(void *dst, sockptr_t src,
5050
return 0;
5151
}
5252

53+
/* Deprecated.
54+
* This is unsafe, unless caller checked user provided optlen.
55+
* Prefer copy_safe_from_sockptr() instead.
56+
*/
5357
static inline int copy_from_sockptr(void *dst, sockptr_t src, size_t size)
5458
{
5559
return copy_from_sockptr_offset(dst, src, 0, size);
5660
}
5761

62+
/**
63+
* copy_safe_from_sockptr: copy a struct from sockptr
64+
* @dst: Destination address, in kernel space. This buffer must be @ksize
65+
* bytes long.
66+
* @ksize: Size of @dst struct.
67+
* @optval: Source address. (in user or kernel space)
68+
* @optlen: Size of @optval data.
69+
*
70+
* Returns:
71+
* * -EINVAL: @optlen < @ksize
72+
* * -EFAULT: access to userspace failed.
73+
* * 0 : @ksize bytes were copied
74+
*/
75+
static inline int copy_safe_from_sockptr(void *dst, size_t ksize,
76+
sockptr_t optval, unsigned int optlen)
77+
{
78+
if (optlen < ksize)
79+
return -EINVAL;
80+
return copy_from_sockptr(dst, optval, ksize);
81+
}
82+
5883
static inline int copy_struct_from_sockptr(void *dst, size_t ksize,
5984
sockptr_t src, size_t usize)
6085
{

0 commit comments

Comments
 (0)