Skip to content

Commit a865b72

Browse files
andrewshaduraAleksei Vetrov
authored and
Sasha Levin
committed
Bluetooth: Fix type of len in rfcomm_sock_getsockopt{,_old}()
commit 5fe6caa upstream. Commit 9bf4e91 worked around an issue introduced after an innocuous optimisation change in LLVM main: > len is defined as an 'int' because it is assigned from > '__user int *optlen'. However, it is clamped against the result of > sizeof(), which has a type of 'size_t' ('unsigned long' for 64-bit > platforms). This is done with min_t() because min() requires compatible > types, which results in both len and the result of sizeof() being casted > to 'unsigned int', meaning len changes signs and the result of sizeof() > is truncated. From there, len is passed to copy_to_user(), which has a > third parameter type of 'unsigned long', so it is widened and changes > signs again. This excessive casting in combination with the KCSAN > instrumentation causes LLVM to fail to eliminate the __bad_copy_from() > call, failing the build. The same issue occurs in rfcomm in functions rfcomm_sock_getsockopt and rfcomm_sock_getsockopt_old. Change the type of len to size_t in both rfcomm_sock_getsockopt and rfcomm_sock_getsockopt_old and replace min_t() with min(). Cc: [email protected] Co-authored-by: Aleksei Vetrov <[email protected]> Improves: 9bf4e91 ("Bluetooth: Fix type of len in {l2cap,sco}_sock_getsockopt_old()") Link: ClangBuiltLinux/linux#2007 Link: llvm/llvm-project#85647 Signed-off-by: Andrej Shadura <[email protected]> Reviewed-by: Nathan Chancellor <[email protected]> Signed-off-by: Luiz Augusto von Dentz <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent fc3cfd2 commit a865b72

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

net/bluetooth/rfcomm/sock.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,8 @@ static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname, char __u
729729
struct sock *l2cap_sk;
730730
struct l2cap_conn *conn;
731731
struct rfcomm_conninfo cinfo;
732-
int len, err = 0;
732+
int err = 0;
733+
size_t len;
733734
u32 opt;
734735

735736
BT_DBG("sk %p", sk);
@@ -783,7 +784,7 @@ static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname, char __u
783784
cinfo.hci_handle = conn->hcon->handle;
784785
memcpy(cinfo.dev_class, conn->hcon->dev_class, 3);
785786

786-
len = min_t(unsigned int, len, sizeof(cinfo));
787+
len = min(len, sizeof(cinfo));
787788
if (copy_to_user(optval, (char *) &cinfo, len))
788789
err = -EFAULT;
789790

@@ -802,7 +803,8 @@ static int rfcomm_sock_getsockopt(struct socket *sock, int level, int optname, c
802803
{
803804
struct sock *sk = sock->sk;
804805
struct bt_security sec;
805-
int len, err = 0;
806+
int err = 0;
807+
size_t len;
806808

807809
BT_DBG("sk %p", sk);
808810

@@ -827,7 +829,7 @@ static int rfcomm_sock_getsockopt(struct socket *sock, int level, int optname, c
827829
sec.level = rfcomm_pi(sk)->sec_level;
828830
sec.key_size = 0;
829831

830-
len = min_t(unsigned int, len, sizeof(sec));
832+
len = min(len, sizeof(sec));
831833
if (copy_to_user(optval, (char *) &sec, len))
832834
err = -EFAULT;
833835

0 commit comments

Comments
 (0)