net: add copy_safe_from_sockptr() helper
authorEric Dumazet <edumazet@google.com>
Mon, 8 Apr 2024 08:28:43 +0000 (08:28 +0000)
committerJakub Kicinski <kuba@kernel.org>
Wed, 10 Apr 2024 00:00:16 +0000 (17:00 -0700)
commit6309863b31dd80317cd7d6824820b44e254e2a9c
tree5f85d74ace141ee9e7685ed521ce8c6166b37c7f
parentcf1b7201df59fb936f40f4a807433fe3f2ce310a
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>
include/linux/sockptr.h