Skip to content

Commit 9832e1b

Browse files
andrewshaduraAleksei Vetrov
authored andcommitted
Bluetooth: Fix type of len in rfcomm_sock_getsockopt{,_old}()
commit 5fe6caa62b07fd39cd6a28acc8f92ba2955e11a6 upstream. Commit 9bf4e919ccad 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: 9bf4e919ccad ("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 9f350a4 commit 9832e1b

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
@@ -736,7 +736,8 @@ static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname, char __u
736736
struct sock *l2cap_sk;
737737
struct l2cap_conn *conn;
738738
struct rfcomm_conninfo cinfo;
739-
int len, err = 0;
739+
int err = 0;
740+
size_t len;
740741
u32 opt;
741742

742743
BT_DBG("sk %p", sk);
@@ -790,7 +791,7 @@ static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname, char __u
790791
cinfo.hci_handle = conn->hcon->handle;
791792
memcpy(cinfo.dev_class, conn->hcon->dev_class, 3);
792793

793-
len = min_t(unsigned int, len, sizeof(cinfo));
794+
len = min(len, sizeof(cinfo));
794795
if (copy_to_user(optval, (char *) &cinfo, len))
795796
err = -EFAULT;
796797

@@ -809,7 +810,8 @@ static int rfcomm_sock_getsockopt(struct socket *sock, int level, int optname, c
809810
{
810811
struct sock *sk = sock->sk;
811812
struct bt_security sec;
812-
int len, err = 0;
813+
int err = 0;
814+
size_t len;
813815

814816
BT_DBG("sk %p", sk);
815817

@@ -834,7 +836,7 @@ static int rfcomm_sock_getsockopt(struct socket *sock, int level, int optname, c
834836
sec.level = rfcomm_pi(sk)->sec_level;
835837
sec.key_size = 0;
836838

837-
len = min_t(unsigned int, len, sizeof(sec));
839+
len = min(len, sizeof(sec));
838840
if (copy_to_user(optval, (char *) &sec, len))
839841
err = -EFAULT;
840842

0 commit comments

Comments
 (0)