net/bluetooth: Fix bound check in event handling
Subsystems (4)
PATCH 1/1 net/bluetooth: Fix bound check in event handling
hci_inquiry_result_with_rssi_evt() can perform out of bound reads on skb->data as a bound check is missing.
---
Syzkaler reports:
https://syzkaller.appspot.com/bug?id=d708485af9edc3af35f3b4d554e827c6c8bf6b0f
https://syzkaller.appspot.com/bug?id=3acd1155d48a5acc5d76711568b04926945a6885
net/bluetooth/hci_event.c | 4 ++++ 1 file changed, 4 insertions(+)
net/bluetooth/hci_event.c +4 -0
@@ -3983,6 +3983,10 @@ static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,for (; num_rsp; num_rsp--, info++) {u32 flags; +if ((void *)(info +sizeof (info)) > + (void *)(skb->data + skb->len)) +break ; +bacpy (&data.bdaddr, &info->bdaddr); data.pscan_rep_mode = info->pscan_rep_mode; data.pscan_period_mode = info->pscan_period_mode;
Signature · 1 line
2.11.0
Marcel Holtmann
maintainerHi Tomas,
Quoted 19 lines
hci_inquiry_result_with_rssi_evt()can perform out of bound reads onskb->dataas a bound check is missing.Signed-off-by Tomas Bortoli <[email protected]>Reported-by [email protected]Reported-by [email protected]---
Syzkaler reports:
https://syzkaller.appspot.com/bug?id=d708485af9edc3af35f3b4d554e827c6c8bf6b0f
https://syzkaller.appspot.com/bug?id=3acd1155d48a5acc5d76711568b04926945a6885
net/bluetooth/hci_event.c| 4 ++++1 file changed, 4 insertions(+)net/bluetooth/hci_event.c +0
-0
net/bluetooth/hci_event.c static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, · line 3989
Quoted 7 lines
net/bluetooth/hci_event.c +4
-0@@ -3983,6 +3983,10 @@ static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,for (; num_rsp; num_rsp--, info++) {u32 flags; +if ((void *)(info +sizeof (info)) > + (void *)(skb->data + skb->len)) +break ; +
first of all, the loop exists twice here. If one is vulnerable, then the second is a well. And second, can we not just do this inside the for-condition check or a lot simpler than this void casting fun.
Regards
Marcel
Hi Marcel,
Quoted 24 lines · Marcel Holtmann
Hi Tomas,
Quoted 22 lines
hci_inquiry_result_with_rssi_evt()can perform out of bound reads onskb->dataas a bound check is missing.Signed-off-by Tomas Bortoli <[email protected]>Reported-by [email protected]Reported-by [email protected]---
Syzkaler reports:
https://syzkaller.appspot.com/bug?id=d708485af9edc3af35f3b4d554e827c6c8bf6b0f
https://syzkaller.appspot.com/bug?id=3acd1155d48a5acc5d76711568b04926945a6885
net/bluetooth/hci_event.c| 4 ++++1 file changed, 4 insertions(+)net/bluetooth/hci_event.c +0
-0@@ -3983,6 +3983,10 @@ static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,for (; num_rsp; num_rsp--, info++) {u32 flags;
Quoted 7 lines
net/bluetooth/hci_event.c static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, · line 3989
Quoted 4 lines
net/bluetooth/hci_event.c +4
-0+if ((void *)(info +sizeof (info)) > + (void *)(skb->data + skb->len)) +break ; +first of all, the loop exists twice here. If one is vulnerable, then the second is a well. And second, can we not just do this inside the for-condition check or a lot simpler than this void casting fun.
1. The other for loop is not vulnerable because of the if that wraps the loops. Unfortunately the condition only provides bound checking for the "else" branch.
2. Sure. I was just getting some compiler warning like "calculated value but unused" when putting the additional condition. By the way I am curious to see how to avoid this void casting.
Regards,
Tomas
Hi Tomas,
url: https://github.com/0day-ci/linux/commits/Tomas-Bortoli/net-bluetooth-Fix-bound-check-in-event-handling/20190301-213647 base: https://git.kernel.org/pub/scm/linux/kernel/git/bluetooth/bluetooth-next.git master
smatch warnings:net/bluetooth/hci_event.c:3986 hci_inquiry_result_with_rssi_evt() warn: potential pointer math issue ('info' is a 120 bit pointer)
# https://github.com/0day-ci/linux/commit/00305742c021794f147b348d45eb10ea26e5a514
git remote add linux-review https://github.com/0day-ci/linux
git remote update linux-review
git checkout 00305742c021794f147b348d45eb10ea26e5a514
vim +3986 net/bluetooth/hci_event.c
a9de9248 Marcel Holtmann 2007-10-20 3979 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
138d22ef Szymon Janc 2011-02-17 3980 struct inquiry_info_with_rssi_and_pscan_mode *info;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^138d22ef Szymon Janc 2011-02-17 3981 info = (void *) (skb->data + 1);
a9de9248 Marcel Holtmann 2007-10-20 3982
e17acd40 Johan Hedberg 2011-03-30 3983 for (; num_rsp; num_rsp--, info++) {
af58925c Marcel Holtmann 2014-07-01 3984 u32 flags;
af58925c Marcel Holtmann 2014-07-01 3985
00305742 Tomas Bortoli 2019-02-28 @3986 if ((void *)(info + sizeof(info)) >
^^^^^^^^^^^^^^^^^^^This is a pointer math bug. The options to fix it are:
if ((void *)info + sizeof(info) >
Or:
if ((void *)(info + 1) >
00305742 Tomas Bortoli 2019-02-28 3987 (void *)(skb->data + skb->len)) 00305742 Tomas Bortoli 2019-02-28 3988 break; 00305742 Tomas Bortoli 2019-02-28 3989 a9de9248 Marcel Holtmann 2007-10-20 3990 bacpy(&data.bdaddr, &info->bdaddr); a9de9248 Marcel Holtmann 2007-10-20 3991 data.pscan_rep_mode = info->pscan_rep_mode; a9de9248 Marcel Holtmann 2007-10-20 3992 data.pscan_period_mode = info->pscan_period_mode; a9de9248 Marcel Holtmann 2007-10-20 3993 data.pscan_mode = info->pscan_mode; a9de9248 Marcel Holtmann 2007-10-20 3994 memcpy(data.dev_class, info->dev_class, 3); a9de9248 Marcel Holtmann 2007-10-20 3995 data.clock_offset = info->clock_offset; a9de9248 Marcel Holtmann 2007-10-20 3996 data.rssi = info->rssi; 41a96212 Marcel Holtmann 2008-07-14 3997 data.ssp_mode = 0x00; 3175405b Johan Hedberg 2012-01-04 3998
regards,
dan carpenter
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Hi Dan,
Quoted 25 lines · Dan Carpenter
Hi Tomas,
url: https://github.com/0day-ci/linux/commits/Tomas-Bortoli/net-bluetooth-Fix-bound-check-in-event-handling/20190301-213647 base: https://git.kernel.org/pub/scm/linux/kernel/git/bluetooth/bluetooth-next.git mastersmatch warnings:
net/bluetooth/hci_event.c:3986hci_inquiry_result_with_rssi_evt()warn: potential pointer math issue ('info' is a 120 bit pointer)# https://github.com/0day-ci/linux/commit/00305742c021794f147b348d45eb10ea26e5a514
git remote add linux-review https://github.com/0day-ci/linux
git remote update linux-review
git checkout00305742c021794f147b348d45eb10ea26e5a514
vim +3986net/bluetooth/hci_event.ca9de9248 Marcel Holtmann 2007-10-20 3979 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) { 138d22ef Szymon Janc 2011-02-17 3980 struct inquiry_info_with_rssi_and_pscan_mode *info; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^138d22ef Szymon Janc 2011-02-17 3981 info = (void *) (skb->data + 1); a9de9248 Marcel Holtmann 2007-10-20 3982 e17acd40 Johan Hedberg 2011-03-30 3983 for (; num_rsp; num_rsp--, info++) { af58925c Marcel Holtmann 2014-07-01 3984 u32 flags; af58925c Marcel Holtmann 2014-07-01 3985 00305742 Tomas Bortoli 2019-02-28 @3986 if ((void *)(info + sizeof(info)) > ^^^^^^^^^^^^^^^^^^^
Quoted 3 lines
This is a pointer math bug. The options to fix it are:
if ((void *)info + sizeof(info) >
Yes, also sizeof(info) should be sizeof(*info)..
Quoted 18 lines
Or:
if ((void *)(info + 1) >00305742 Tomas Bortoli 2019-02-28 3987 (void *)(skb->data + skb->len)) 00305742 Tomas Bortoli 2019-02-28 3988 break; 00305742 Tomas Bortoli 2019-02-28 3989 a9de9248 Marcel Holtmann 2007-10-20 3990 bacpy(&data.bdaddr, &info->bdaddr); a9de9248 Marcel Holtmann 2007-10-20 3991 data.pscan_rep_mode = info->pscan_rep_mode; a9de9248 Marcel Holtmann 2007-10-20 3992 data.pscan_period_mode = info->pscan_period_mode; a9de9248 Marcel Holtmann 2007-10-20 3993 data.pscan_mode = info->pscan_mode; a9de9248 Marcel Holtmann 2007-10-20 3994 memcpy(data.dev_class, info->dev_class, 3); a9de9248 Marcel Holtmann 2007-10-20 3995 data.clock_offset = info->clock_offset; a9de9248 Marcel Holtmann 2007-10-20 3996 data.rssi = info->rssi; 41a96212 Marcel Holtmann 2008-07-14 3997 data.ssp_mode = 0x00; 3175405b Johan Hedberg 2012-01-04 3998
Quoted 7 lines
regards,
dan carpenter---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
regards,
Tomas
PATCH 1/1 net/bluetooth: Fix bound check in event handling
In reply to Tomas Bortolihci_inquiry_result_with_rssi_evt() can perform out of bound reads on skb->data as a bound check is missing.
---
v2:
- changed sizeof(info) into sizeof(*info), as it should be.
- simplified expression in if
net/bluetooth/hci_event.c | 4 ++++ 1 file changed, 4 insertions(+)
net/bluetooth/hci_event.c +4 -0
@@ -3978,11 +3978,15 @@ static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,if ((skb->len -1 ) / num_rsp !=sizeof (struct inquiry_info_with_rssi )) {struct inquiry_info_with_rssi_and_pscan_mode *info; +void *data_end = (void *)skb->data + skb->len; info = (void *) (skb->data +1 );for (; num_rsp; num_rsp--, info++) {u32 flags; +if ((void *)info +sizeof (*info) > data_end) +break ; +bacpy (&data.bdaddr, &info->bdaddr); data.pscan_rep_mode = info->pscan_rep_mode; data.pscan_period_mode = info->pscan_period_mode;
Signature · 1 line
2.11.0
[ This is an old warning. Sorry for missing it earlier. I would have
caught it when the code was merged as well so there was no real risk but it's just awkward. ]
Hi Tomas,
url: https://github.com/0day-ci/linux/commits/Tomas-Bortoli/net-bluetooth-Fix-bound-check-in-event-handling/20190301-213647 base: https://git.kernel.org/pub/scm/linux/kernel/git/bluetooth/bluetooth-next.git master
smatch warnings:net/bluetooth/hci_event.c:3986 hci_inquiry_result_with_rssi_evt() warn: potential pointer math issue ('info' is a 120 bit pointer)
# https://github.com/0day-ci/linux/commit/00305742c021794f147b348d45eb10ea26e5a514
git remote add linux-review https://github.com/0day-ci/linux
git remote update linux-review
git checkout 00305742c021794f147b348d45eb10ea26e5a514
vim +3986 net/bluetooth/hci_event.c
6039aa73 Gustavo Padovan 2012-05-23 3963 static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,
807deac2 Gustavo Padovan 2012-05-17 3964 struct sk_buff *skb)
a9de9248 Marcel Holtmann 2007-10-20 3965 {
a9de9248 Marcel Holtmann 2007-10-20 3966 struct inquiry_data data;
a9de9248 Marcel Holtmann 2007-10-20 3967 int num_rsp = *((__u8 *) skb->data);
a9de9248 Marcel Holtmann 2007-10-20 3968
a9de9248 Marcel Holtmann 2007-10-20 3969 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
a9de9248 Marcel Holtmann 2007-10-20 3970
a9de9248 Marcel Holtmann 2007-10-20 3971 if (!num_rsp)
a9de9248 Marcel Holtmann 2007-10-20 3972 return;
a9de9248 Marcel Holtmann 2007-10-20 3973
d7a5a11d Marcel Holtmann 2015-03-13 3974 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
1519cc17 Andre Guedes 2012-03-21 3975 return;
1519cc17 Andre Guedes 2012-03-21 3976
a9de9248 Marcel Holtmann 2007-10-20 3977 hci_dev_lock(hdev);
a9de9248 Marcel Holtmann 2007-10-20 3978
a9de9248 Marcel Holtmann 2007-10-20 3979 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
138d22ef Szymon Janc 2011-02-17 3980 struct inquiry_info_with_rssi_and_pscan_mode *info;
138d22ef Szymon Janc 2011-02-17 3981 info = (void *) (skb->data + 1);
a9de9248 Marcel Holtmann 2007-10-20 3982
e17acd40 Johan Hedberg 2011-03-30 3983 for (; num_rsp; num_rsp--, info++) {
af58925c Marcel Holtmann 2014-07-01 3984 u32 flags;
af58925c Marcel Holtmann 2014-07-01 3985
00305742 Tomas Bortoli 2019-02-28 @3986 if ((void *)(info + sizeof(info)) >
^^^^^^^^^^^^^^^^^^^^^^^^^^^^This should be (void *)info + sizeof(info). The code you have will break for valid uses because the pointer math error. I notice that this isn't merged into linux-next, but it does seem required. I am writing a similar fix for a different function.
Another way to write this would be:
if ((u8 *)(info + 1) > &skb->data[skb->len]) {00305742 Tomas Bortoli 2019-02-28 3987 (void *)(skb->data + skb->len))
00305742 Tomas Bortoli 2019-02-28 3988 break;
00305742 Tomas Bortoli 2019-02-28 3989
a9de9248 Marcel Holtmann 2007-10-20 3990 bacpy(&data.bdaddr, &info->bdaddr);
a9de9248 Marcel Holtmann 2007-10-20 3991 data.pscan_rep_mode = info->pscan_rep_mode;
a9de9248 Marcel Holtmann 2007-10-20 3992 data.pscan_period_mode = info->pscan_period_mode;
a9de9248 Marcel Holtmann 2007-10-20 3993 data.pscan_mode = info->pscan_mode;
a9de9248 Marcel Holtmann 2007-10-20 3994 memcpy(data.dev_class, info->dev_class, 3);
a9de9248 Marcel Holtmann 2007-10-20 3995 data.clock_offset = info->clock_offset;
a9de9248 Marcel Holtmann 2007-10-20 3996 data.rssi = info->rssi;
41a96212 Marcel Holtmann 2008-07-14 3997 data.ssp_mode = 0x00;
3175405b Johan Hedberg 2012-01-04 3998
af58925c Marcel Holtmann 2014-07-01 3999 flags = hci_inquiry_cache_update(hdev, &data, false);
af58925c Marcel Holtmann 2014-07-01 4000
48264f06 Johan Hedberg 2011-11-09 4001 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
e17acd40 Johan Hedberg 2011-03-30 4002 info->dev_class, info->rssi,
af58925c Marcel Holtmann 2014-07-01 4003 flags, NULL, 0, NULL, 0);
a9de9248 Marcel Holtmann 2007-10-20 4004 }
a9de9248 Marcel Holtmann 2007-10-20 4005 } else {---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Quoted 43 lines · Dan Carpenter
[ This is an old warning. Sorry for missing it earlier. I would have
caught it when the code was merged as well so there was no real risk but it's just awkward. ]Hi Tomas,
url: https://github.com/0day-ci/linux/commits/Tomas-Bortoli/net-bluetooth-Fix-bound-check-in-event-handling/20190301-213647 base: https://git.kernel.org/pub/scm/linux/kernel/git/bluetooth/bluetooth-next.git mastersmatch warnings:
net/bluetooth/hci_event.c:3986hci_inquiry_result_with_rssi_evt()warn: potential pointer math issue ('info' is a 120 bit pointer)# https://github.com/0day-ci/linux/commit/00305742c021794f147b348d45eb10ea26e5a514
git remote add linux-review https://github.com/0day-ci/linux
git remote update linux-review
git checkout00305742c021794f147b348d45eb10ea26e5a514
vim +3986net/bluetooth/hci_event.c6039aa73 Gustavo Padovan 2012-05-23 3963 static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, 807deac2 Gustavo Padovan 2012-05-17 3964 struct sk_buff *skb) a9de9248 Marcel Holtmann 2007-10-20 3965 { a9de9248 Marcel Holtmann 2007-10-20 3966 struct inquiry_data data; a9de9248 Marcel Holtmann 2007-10-20 3967 int num_rsp = *((__u8 *) skb->data); a9de9248 Marcel Holtmann 2007-10-20 3968 a9de9248 Marcel Holtmann 2007-10-20 3969 BT_DBG("%s num_rsp %d", hdev->name, num_rsp); a9de9248 Marcel Holtmann 2007-10-20 3970 a9de9248 Marcel Holtmann 2007-10-20 3971 if (!num_rsp) a9de9248 Marcel Holtmann 2007-10-20 3972 return; a9de9248 Marcel Holtmann 2007-10-20 3973 d7a5a11d Marcel Holtmann 2015-03-13 3974 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) 1519cc17 Andre Guedes 2012-03-21 3975 return; 1519cc17 Andre Guedes 2012-03-21 3976 a9de9248 Marcel Holtmann 2007-10-20 3977 hci_dev_lock(hdev); a9de9248 Marcel Holtmann 2007-10-20 3978 a9de9248 Marcel Holtmann 2007-10-20 3979 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) { 138d22ef Szymon Janc 2011-02-17 3980 struct inquiry_info_with_rssi_and_pscan_mode *info; 138d22ef Szymon Janc 2011-02-17 3981 info = (void *) (skb->data + 1); a9de9248 Marcel Holtmann 2007-10-20 3982 e17acd40 Johan Hedberg 2011-03-30 3983 for (; num_rsp; num_rsp--, info++) { af58925c Marcel Holtmann 2014-07-01 3984 u32 flags; af58925c Marcel Holtmann 2014-07-01 3985 00305742 Tomas Bortoli 2019-02-28 @3986 if ((void *)(info + sizeof(info)) > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 1 line
This should be (void *)info + sizeof(info). The code you have will
^^^^^^^^^^^^ sizeof(*info)
Sorry...
regards,
dan carpenter
Hi Dan,
Quoted 43 lines · Dan Carpenter
[ This is an old warning. Sorry for missing it earlier. I would have
caught it when the code was merged as well so there was no real risk but it's just awkward. ]Hi Tomas,
url: https://github.com/0day-ci/linux/commits/Tomas-Bortoli/net-bluetooth-Fix-bound-check-in-event-handling/20190301-213647 base: https://git.kernel.org/pub/scm/linux/kernel/git/bluetooth/bluetooth-next.git mastersmatch warnings:
net/bluetooth/hci_event.c:3986hci_inquiry_result_with_rssi_evt()warn: potential pointer math issue ('info' is a 120 bit pointer)# https://github.com/0day-ci/linux/commit/00305742c021794f147b348d45eb10ea26e5a514
git remote add linux-review https://github.com/0day-ci/linux
git remote update linux-review
git checkout00305742c021794f147b348d45eb10ea26e5a514
vim +3986net/bluetooth/hci_event.c6039aa73 Gustavo Padovan 2012-05-23 3963 static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, 807deac2 Gustavo Padovan 2012-05-17 3964 struct sk_buff *skb) a9de9248 Marcel Holtmann 2007-10-20 3965 { a9de9248 Marcel Holtmann 2007-10-20 3966 struct inquiry_data data; a9de9248 Marcel Holtmann 2007-10-20 3967 int num_rsp = *((__u8 *) skb->data); a9de9248 Marcel Holtmann 2007-10-20 3968 a9de9248 Marcel Holtmann 2007-10-20 3969 BT_DBG("%s num_rsp %d", hdev->name, num_rsp); a9de9248 Marcel Holtmann 2007-10-20 3970 a9de9248 Marcel Holtmann 2007-10-20 3971 if (!num_rsp) a9de9248 Marcel Holtmann 2007-10-20 3972 return; a9de9248 Marcel Holtmann 2007-10-20 3973 d7a5a11d Marcel Holtmann 2015-03-13 3974 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) 1519cc17 Andre Guedes 2012-03-21 3975 return; 1519cc17 Andre Guedes 2012-03-21 3976 a9de9248 Marcel Holtmann 2007-10-20 3977 hci_dev_lock(hdev); a9de9248 Marcel Holtmann 2007-10-20 3978 a9de9248 Marcel Holtmann 2007-10-20 3979 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) { 138d22ef Szymon Janc 2011-02-17 3980 struct inquiry_info_with_rssi_and_pscan_mode *info; 138d22ef Szymon Janc 2011-02-17 3981 info = (void *) (skb->data + 1); a9de9248 Marcel Holtmann 2007-10-20 3982 e17acd40 Johan Hedberg 2011-03-30 3983 for (; num_rsp; num_rsp--, info++) { af58925c Marcel Holtmann 2014-07-01 3984 u32 flags; af58925c Marcel Holtmann 2014-07-01 3985 00305742 Tomas Bortoli 2019-02-28 @3986 if ((void *)(info + sizeof(info)) > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 8 lines
This should be (void *)info + sizeof(info). The code you have will break for valid uses because the pointer math error. I notice that this isn't merged into linux-next, but it does seem required. I am writing a similar fix for a different function.
Another way to write this would be:
if ((u8 *)(info + 1) > &skb->data[skb->len]) {
Yeah it hasn't been accepted afaik. Why just + 1 ? Also, &skb->data[skb->len] is right after the last byte so the > should rather be a >=, I think.
your code looks better (as per pointer casting) but is logically different from what I proposed with v2:
https://lkml.org/lkml/2019/3/4/892
I think the bound check should validate that there is enough data from the info pointer to read an entire struct inquiry_info_with_rssi_and_pscan_mode.
Best regards,
Tomas