diff mbox series

[net,1/3] net: add copy_safe_from_sockptr() helper

Message ID 20240408082845.3957374-2-edumazet@google.com (mailing list archive)
State Accepted
Commit 6309863b31dd80317cd7d6824820b44e254e2a9c
Delegated to: Netdev Maintainers
Headers show
Series net: start to replace copy_from_sockptr() | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 7519 this patch: 7519
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers fail 3 maintainers not CCed: noureddine@arista.com fruggeri@arista.com 0x7f454c46@gmail.com
netdev/build_clang success Errors and warnings before: 1234 this patch: 1234
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 7902 this patch: 7902
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 36 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc fail Errors and warnings before: 0 this patch: 1
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-04-10--00-00 (tests: 958)

Commit Message

Eric Dumazet April 8, 2024, 8:28 a.m. UTC
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>
---
 include/linux/sockptr.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

Comments

Jakub Kicinski April 10, 2024, 12:03 a.m. UTC | #1
On Mon,  8 Apr 2024 08:28:43 +0000 Eric Dumazet wrote:
> + * Returns
> + *  * -EINVAL: @optlen < @ksize
> + *  * -EFAULT: access to userspace failed.
> + *  * 0 : @ksize bytes were copied

We enabled the "kdoc returns" validation, because people were nit
picking on the list. Apparently there needs to be a colon at the end:

          here
          v
 * Returns:
 *  * -EINVAL: @optlen < @ksize

Fixed when applying.
diff mbox series

Patch

diff --git a/include/linux/sockptr.h b/include/linux/sockptr.h
index 307961b41541a620023ad40d3178d47b94768126..b272d6866c87d837e340ce9e78e8fb3423efbddf 100644
--- a/include/linux/sockptr.h
+++ b/include/linux/sockptr.h
@@ -50,11 +50,36 @@  static inline int copy_from_sockptr_offset(void *dst, sockptr_t src,
 	return 0;
 }
 
+/* Deprecated.
+ * This is unsafe, unless caller checked user provided optlen.
+ * Prefer copy_safe_from_sockptr() instead.
+ */
 static inline int copy_from_sockptr(void *dst, sockptr_t src, size_t size)
 {
 	return copy_from_sockptr_offset(dst, src, 0, size);
 }
 
+/**
+ * copy_safe_from_sockptr: copy a struct from sockptr
+ * @dst:   Destination address, in kernel space. This buffer must be @ksize
+ *         bytes long.
+ * @ksize: Size of @dst struct.
+ * @optval: Source address. (in user or kernel space)
+ * @optlen: Size of @optval data.
+ *
+ * Returns
+ *  * -EINVAL: @optlen < @ksize
+ *  * -EFAULT: access to userspace failed.
+ *  * 0 : @ksize bytes were copied
+ */
+static inline int copy_safe_from_sockptr(void *dst, size_t ksize,
+					 sockptr_t optval, unsigned int optlen)
+{
+	if (optlen < ksize)
+		return -EINVAL;
+	return copy_from_sockptr(dst, optval, ksize);
+}
+
 static inline int copy_struct_from_sockptr(void *dst, size_t ksize,
 		sockptr_t src, size_t usize)
 {