Skip to content

Commit 101545f

Browse files
committed
Bluetooth: Fix potential bad memory access with sysfs files
When creating a high number of Bluetooth sockets (L2CAP, SCO and RFCOMM) it is possible to scribble repeatedly on arbitrary pages of memory. Ensure that the content of these sysfs files is always less than one page. Even if this means truncating. The files in question are scheduled to be moved over to debugfs in the future anyway. Based on initial patches from Neil Brown and Linus Torvalds Reported-by: Neil Brown <neilb@suse.de> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
1 parent af98441 commit 101545f

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

‎net/bluetooth/l2cap.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3944,16 +3944,24 @@ static ssize_t l2cap_sysfs_show(struct class *dev,
39443944
struct sock *sk;
39453945
struct hlist_node *node;
39463946
char *str = buf;
3947+
int size = PAGE_SIZE;
39473948

39483949
read_lock_bh(&l2cap_sk_list.lock);
39493950

39503951
sk_for_each(sk, node, &l2cap_sk_list.head) {
39513952
struct l2cap_pinfo *pi = l2cap_pi(sk);
3953+
int len;
39523954

3953-
str += sprintf(str, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d\n",
3955+
len = snprintf(str, size, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d\n",
39543956
batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst),
39553957
sk->sk_state, __le16_to_cpu(pi->psm), pi->scid,
39563958
pi->dcid, pi->imtu, pi->omtu, pi->sec_level);
3959+
3960+
size -= len;
3961+
if (size <= 0)
3962+
break;
3963+
3964+
str += len;
39573965
}
39583966

39593967
read_unlock_bh(&l2cap_sk_list.lock);

‎net/bluetooth/rfcomm/core.c‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2105,6 +2105,7 @@ static ssize_t rfcomm_dlc_sysfs_show(struct class *dev,
21052105
struct rfcomm_session *s;
21062106
struct list_head *pp, *p;
21072107
char *str = buf;
2108+
int size = PAGE_SIZE;
21082109

21092110
rfcomm_lock();
21102111

@@ -2113,11 +2114,21 @@ static ssize_t rfcomm_dlc_sysfs_show(struct class *dev,
21132114
list_for_each(pp, &s->dlcs) {
21142115
struct sock *sk = s->sock->sk;
21152116
struct rfcomm_dlc *d = list_entry(pp, struct rfcomm_dlc, list);
2117+
int len;
21162118

2117-
str += sprintf(str, "%s %s %ld %d %d %d %d\n",
2119+
len = snprintf(str, size, "%s %s %ld %d %d %d %d\n",
21182120
batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst),
21192121
d->state, d->dlci, d->mtu, d->rx_credits, d->tx_credits);
2122+
2123+
size -= len;
2124+
if (size <= 0)
2125+
break;
2126+
2127+
str += len;
21202128
}
2129+
2130+
if (size <= 0)
2131+
break;
21212132
}
21222133

21232134
rfcomm_unlock();

‎net/bluetooth/rfcomm/sock.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,13 +1068,22 @@ static ssize_t rfcomm_sock_sysfs_show(struct class *dev,
10681068
struct sock *sk;
10691069
struct hlist_node *node;
10701070
char *str = buf;
1071+
int size = PAGE_SIZE;
10711072

10721073
read_lock_bh(&rfcomm_sk_list.lock);
10731074

10741075
sk_for_each(sk, node, &rfcomm_sk_list.head) {
1075-
str += sprintf(str, "%s %s %d %d\n",
1076+
int len;
1077+
1078+
len = snprintf(str, size, "%s %s %d %d\n",
10761079
batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst),
10771080
sk->sk_state, rfcomm_pi(sk)->channel);
1081+
1082+
size -= len;
1083+
if (size <= 0)
1084+
break;
1085+
1086+
str += len;
10781087
}
10791088

10801089
read_unlock_bh(&rfcomm_sk_list.lock);

‎net/bluetooth/sco.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,13 +960,22 @@ static ssize_t sco_sysfs_show(struct class *dev,
960960
struct sock *sk;
961961
struct hlist_node *node;
962962
char *str = buf;
963+
int size = PAGE_SIZE;
963964

964965
read_lock_bh(&sco_sk_list.lock);
965966

966967
sk_for_each(sk, node, &sco_sk_list.head) {
967-
str += sprintf(str, "%s %s %d\n",
968+
int len;
969+
970+
len = snprintf(str, size, "%s %s %d\n",
968971
batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst),
969972
sk->sk_state);
973+
974+
size -= len;
975+
if (size <= 0)
976+
break;
977+
978+
str += len;
970979
}
971980

972981
read_unlock_bh(&sco_sk_list.lock);

0 commit comments

Comments
 (0)