Convert HCI socket'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 *sopt
- Use sopt->optlen for buffer length (input)
- Use copy_to_iter() instead of put_user()/copy_to_user()
- Add linux/uio.h for copy_to_iter()

The sockopt_t parameter is named sopt rather than opt to avoid
collision with the existing local int opt used by HCI_DATA_DIR and
HCI_TIME_STAMP.

Signed-off-by: Breno Leitao <[email protected]>
---
 net/bluetooth/hci_sock.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/net/bluetooth/hci_sock.c b/net/bluetooth/hci_sock.c
index 1823c06ba8940..61fec674a2078 100644
--- a/net/bluetooth/hci_sock.c
+++ b/net/bluetooth/hci_sock.c
@@ -27,6 +27,7 @@
 #include <linux/export.h>
 #include <linux/utsname.h>
 #include <linux/sched.h>
+#include <linux/uio.h>
 #include <linux/unaligned.h>
 
 #include <net/bluetooth/bluetooth.h>
@@ -2063,7 +2064,7 @@ static int hci_sock_setsockopt(struct socket *sock, int 
level, int optname,
 }
 
 static int hci_sock_getsockopt_old(struct socket *sock, int level, int optname,
-                                  char __user *optval, int __user *optlen)
+                                  sockopt_t *sopt)
 {
        struct hci_ufilter uf;
        struct sock *sk = sock->sk;
@@ -2071,8 +2072,7 @@ static int hci_sock_getsockopt_old(struct socket *sock, 
int level, int optname,
 
        BT_DBG("sk %p, opt %d", sk, optname);
 
-       if (get_user(len, optlen))
-               return -EFAULT;
+       len = sopt->optlen;
 
        lock_sock(sk);
 
@@ -2088,7 +2088,8 @@ static int hci_sock_getsockopt_old(struct socket *sock, 
int level, int optname,
                else
                        opt = 0;
 
-               if (put_user(opt, (int __user *)optval))
+               if (copy_to_iter(&opt, sizeof(opt), &sopt->iter_out) !=
+                   sizeof(opt))
                        err = -EFAULT;
                break;
 
@@ -2098,7 +2099,8 @@ static int hci_sock_getsockopt_old(struct socket *sock, 
int level, int optname,
                else
                        opt = 0;
 
-               if (put_user(opt, (int __user *)optval))
+               if (copy_to_iter(&opt, sizeof(opt), &sopt->iter_out) !=
+                   sizeof(opt))
                        err = -EFAULT;
                break;
 
@@ -2114,7 +2116,7 @@ static int hci_sock_getsockopt_old(struct socket *sock, 
int level, int optname,
                }
 
                len = min_t(unsigned int, len, sizeof(uf));
-               if (copy_to_user(optval, &uf, len))
+               if (copy_to_iter(&uf, len, &sopt->iter_out) != len)
                        err = -EFAULT;
                break;
 
@@ -2129,16 +2131,16 @@ static int hci_sock_getsockopt_old(struct socket *sock, 
int level, int optname,
 }
 
 static int hci_sock_getsockopt(struct socket *sock, int level, int optname,
-                              char __user *optval, int __user *optlen)
+                              sockopt_t *sopt)
 {
        struct sock *sk = sock->sk;
        int err = 0;
+       u16 mtu;
 
        BT_DBG("sk %p, opt %d", sk, optname);
 
        if (level == SOL_HCI)
-               return hci_sock_getsockopt_old(sock, level, optname, optval,
-                                              optlen);
+               return hci_sock_getsockopt_old(sock, level, optname, sopt);
 
        if (level != SOL_BLUETOOTH)
                return -ENOPROTOOPT;
@@ -2148,7 +2150,9 @@ static int hci_sock_getsockopt(struct socket *sock, int 
level, int optname,
        switch (optname) {
        case BT_SNDMTU:
        case BT_RCVMTU:
-               if (put_user(hci_pi(sk)->mtu, (u16 __user *)optval))
+               mtu = hci_pi(sk)->mtu;
+               if (copy_to_iter(&mtu, sizeof(mtu), &sopt->iter_out) !=
+                   sizeof(mtu))
                        err = -EFAULT;
                break;
 
@@ -2185,7 +2189,7 @@ static const struct proto_ops hci_sock_ops = {
        .listen         = sock_no_listen,
        .shutdown       = sock_no_shutdown,
        .setsockopt     = hci_sock_setsockopt,
-       .getsockopt     = hci_sock_getsockopt,
+       .getsockopt_iter = hci_sock_getsockopt,
        .connect        = sock_no_connect,
        .socketpair     = sock_no_socketpair,
        .accept         = sock_no_accept,

-- 
2.53.0-Meta


Reply via email to