Convert AF_NETLINK's getsockopt implementation to use the new
getsockopt_iter callback with sockopt_t.

Key changes:
- Replace (char __user *optval, int __user *optlen) with sockopt_t *opt
- Use opt->optlen for buffer length (input) and returned size (output)
- Use copy_to_iter() instead of put_user()/copy_to_user()
- For NETLINK_LIST_MEMBERSHIPS: walk the groups bitmap and emit each
  u32 sequentially via copy_to_iter(), then set opt->optlen to the
  total size required (ALIGN(BITS_TO_BYTES(ngroups), sizeof(u32))).
  The wrapper writes opt->optlen back to userspace even on partial
  failure, preserving the existing API that lets userspace discover
  the needed allocation size.

Signed-off-by: Breno Leitao <[email protected]>
---
 net/netlink/af_netlink.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
index 2aeb0680807d6..db3be485b4804 100644
--- a/net/netlink/af_netlink.c
+++ b/net/netlink/af_netlink.c
@@ -39,6 +39,7 @@
 #include <linux/fs.h>
 #include <linux/slab.h>
 #include <linux/uaccess.h>
+#include <linux/uio.h>
 #include <linux/skbuff.h>
 #include <linux/netdevice.h>
 #include <linux/rtnetlink.h>
@@ -1716,18 +1717,18 @@ static int netlink_setsockopt(struct socket *sock, int 
level, int optname,
 }
 
 static int netlink_getsockopt(struct socket *sock, int level, int optname,
-                             char __user *optval, int __user *optlen)
+                             sockopt_t *opt)
 {
        struct sock *sk = sock->sk;
        struct netlink_sock *nlk = nlk_sk(sk);
        unsigned int flag;
        int len, val;
+       u32 group;
 
        if (level != SOL_NETLINK)
                return -ENOPROTOOPT;
 
-       if (get_user(len, optlen))
-               return -EFAULT;
+       len = opt->optlen;
        if (len < 0)
                return -EINVAL;
 
@@ -1751,14 +1752,14 @@ static int netlink_getsockopt(struct socket *sock, int 
level, int optname,
 
                        idx = pos / sizeof(unsigned long);
                        shift = (pos % sizeof(unsigned long)) * 8;
-                       if (put_user((u32)(nlk->groups[idx] >> shift),
-                                    (u32 __user *)(optval + pos))) {
+                       group = (u32)(nlk->groups[idx] >> shift);
+                       if (copy_to_iter(&group, sizeof(u32),
+                                        &opt->iter_out) != sizeof(u32)) {
                                err = -EFAULT;
                                break;
                        }
                }
-               if (put_user(ALIGN(BITS_TO_BYTES(nlk->ngroups), sizeof(u32)), 
optlen))
-                       err = -EFAULT;
+               opt->optlen = ALIGN(BITS_TO_BYTES(nlk->ngroups), sizeof(u32));
                netlink_unlock_table();
                return err;
        }
@@ -1784,8 +1785,8 @@ static int netlink_getsockopt(struct socket *sock, int 
level, int optname,
        len = sizeof(int);
        val = test_bit(flag, &nlk->flags);
 
-       if (put_user(len, optlen) ||
-           copy_to_user(optval, &val, len))
+       opt->optlen = len;
+       if (copy_to_iter(&val, len, &opt->iter_out) != len)
                return -EFAULT;
 
        return 0;
@@ -2813,7 +2814,7 @@ static const struct proto_ops netlink_ops = {
        .listen =       sock_no_listen,
        .shutdown =     sock_no_shutdown,
        .setsockopt =   netlink_setsockopt,
-       .getsockopt =   netlink_getsockopt,
+       .getsockopt_iter = netlink_getsockopt,
        .sendmsg =      netlink_sendmsg,
        .recvmsg =      netlink_recvmsg,
        .mmap =         sock_no_mmap,

-- 
2.52.0


Reply via email to