Bluetooth: SMP: add missing skb len check in smp_cmd_keypress_notify
Subsystems (3)
PATCH 1/1 Bluetooth: SMP: add missing skb len check in smp_cmd_keypress_notify
smp_cmd_keypress_notify() accesses the received payload as struct smp_cmd_keypress_notify without verifying that skb->len contains enough data.
smp_sig_channel() removes the opcode byte before dispatching to command handlers, so a SMP_CMD_KEYPRESS_NOTIFY packet without a payload leaves skb->len equal to zero on entry to the handler, causing a 1-byte out-of-bounds read from the heap.
Add a length check before accessing the payload and return SMP_INVALID_PARAMS when the packet is too short, matching the pattern used by other SMP command handlers.
--- net/bluetooth/smp.c | 3 +++ 1 file changed, 3 insertions(+)
net/bluetooth/smp.c +3 -0
@@ -2932,6 +2932,9 @@ static int smp_cmd_keypress_notify(struct l2cap_conn *conn, {struct smp_cmd_keypress_notify *kp = (void *) skb->data; +if (skb->len <sizeof (*kp)) +return SMP_INVALID_PARAMS ; +bt_dev_dbg (conn->hcon->hdev,"value 0x%02x" , kp->value);return 0 ;
Signature · 1 line
2.54.0
This is automated email and please do not reply to this email!
Dear submitter,
Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=1096089
---Test result---
Test Summary:
CheckPatch PASS 0.74 seconds GitLint PASS 0.35 seconds SubjectPrefix PASS 0.13 seconds BuildKernel PASS 28.81 seconds CheckAllWarning PASS 29.40 seconds CheckSparse PASS 28.55 seconds BuildKernel32 PASS 26.07 seconds TestRunnerSetup PASS 576.08 seconds TestRunner_smp-tester PASS 18.39 seconds IncrementalBuild PASS 25.74 seconds
https://github.com/bluez/bluetooth-next/pull/205
---
Regards,
Linux Bluetooth
Dear Muhammad,
Thank you for patch.
Am 17.05.26 um 16:54 schrieb Muhammad Bilal:
Quoted 25 lines
smp_cmd_keypress_notify()accesses the received payload as structsmp_cmd_keypress_notifywithout verifying thatskb->lencontains enough data.
smp_sig_channel()removes the opcode byte before dispatching to command handlers, so aSMP_CMD_KEYPRESS_NOTIFYpacket without a payload leavesskb->lenequal to zero on entry to the handler, causing a 1-byte out-of-bounds read from the heap.Add a length check before accessing the payload and return
SMP_INVALID_PARAMSwhen the packet is too short, matching the pattern used by other SMP command handlers.Fixes 1408bb6efb04 ("Bluetooth: Add dummy handler for LE SC keypress notification")Signed-off-by Muhammad Bilal <[email protected]>--- net/bluetooth/smp.c | 3 +++ 1 file changed, 3 insertions(+)net/bluetooth/smp.c +0
-0
net/bluetooth/smp.c static int smp_cmd_keypress_notify(struct l2cap_conn *conn, · line 2938
Quoted 7 lines
net/bluetooth/smp.c +3
-0@@ -2932,6 +2932,9 @@ static int smp_cmd_keypress_notify(struct l2cap_conn *conn, {struct smp_cmd_keypress_notify *kp = (void *) skb->data; +if (skb->len <sizeof (*kp)) +return SMP_INVALID_PARAMS ; +bt_dev_dbg (conn->hcon->hdev,"value 0x%02x" , kp->value);
Add the check after the debug log, so it can be inspected?
net/bluetooth/smp.c static int smp_cmd_keypress_notify(struct l2cap_conn *conn, · line 2940
Quoted 2 lines
net/bluetooth/smp.c +0
-0return 0 ;
Kind regards,
Paul
Hi Paul,
Thanks for the review.
Moving the check after bt_dev_dbg() would not be safe since the debug statement reads kp->value, which is exactly what the length check is guarding.
On a truncated SMP_CMD_KEYPRESS_NOTIFY packet, skb->len may be smaller than sizeof(*kp) when entering the handler, so evaluating kp->value in the debug log would already access out-of-bounds memory before the guard is reached.
Therefore the length check needs to remain before any access to kp->value.
Regards,
Muhammad Bilal
Dear Muhammad,
Am 17.05.26 um 20:08 schrieb Muhammad Bilal:
Quoted 1 line
Thanks for the review.
Thank you for your instant reply.
Quoted 10 lines
Moving the check after
bt_dev_dbg()would not be safe since the debug statement readskp->value, which is exactly what the length check is guarding.On a truncated
SMP_CMD_KEYPRESS_NOTIFYpacket,skb->lenmay be smaller thansizeof(*kp)when entering the handler, so evaluatingkp->valuein the debug log would already access out-of-bounds memory before the guard is reached.Therefore the length check needs to remain before any access to
kp->value.
Thank you for the explanation. Is there another to log the faulty value?
Kind regards,
Paul
Hi Paul,
There is no safe way to access kp->value in the truncated case, since the payload is not guaranteed to be present when skb->len < sizeof(*kp).
If diagnostic information is still useful, only metadata can be logged:
if (skb->len < sizeof(*kp)) {
bt_dev_dbg(conn->hcon->hdev,
"truncated keypress notify, len=%u",
skb->len);
return SMP_INVALID_PARAMS;
}This keeps visibility into malformed packets without touching unvalidated memory. Happy to send a v2 if that looks good to you.
Regards,
Muhammad Bilal
Dear Muhammad,
Am 17.05.26 um 21:03 schrieb Muhammad Bilal:
Quoted 2 lines
There is no safe way to access
kp->valuein the truncated case, since the payload is not guaranteed to be present whenskb->len<sizeof(*kp).
Indeed, you are right.
Quoted 11 lines
If diagnostic information is still useful, only metadata can be logged:
if (skb->len < sizeof(*kp)) { bt_dev_dbg(conn->hcon->hdev, "truncated keypress notify, len=%u", skb->len); return SMP_INVALID_PARAMS; }This keeps visibility into malformed packets without touching unvalidated memory. Happy to send a v2 if that looks good to you.
It looks good to me. Hopefully the maintainers agree.
Kind regards,
Paul
Luiz Augusto von Dentz
maintainerHi Muhammad,
Quoted 26 lines · Muhammad Bilal
smp_cmd_keypress_notify()accesses the received payload as structsmp_cmd_keypress_notifywithout verifying thatskb->lencontains enough data.
smp_sig_channel()removes the opcode byte before dispatching to command handlers, so aSMP_CMD_KEYPRESS_NOTIFYpacket without a payload leavesskb->lenequal to zero on entry to the handler, causing a 1-byte out-of-bounds read from the heap.Add a length check before accessing the payload and return
SMP_INVALID_PARAMSwhen the packet is too short, matching the pattern used by other SMP command handlers.Fixes 1408bb6efb04 ("Bluetooth: Add dummy handler for LE SC keypress notification")Signed-off-by Muhammad Bilal <[email protected]>--- net/bluetooth/smp.c | 3 +++ 1 file changed, 3 insertions(+)net/bluetooth/smp.c +0
-0
net/bluetooth/smp.c static int smp_cmd_keypress_notify(struct l2cap_conn *conn, · line 2933
Quoted 2 lines
net/bluetooth/smp.c +0
-0@@ -2932,6 +2932,9 @@ static int smp_cmd_keypress_notify(struct l2cap_conn *conn, {struct smp_cmd_keypress_notify *kp = (void *) skb->data;
Perhaps we should stop assigning it directly and instead just use skb_pull_data, which performs bounds checks on its own.
net/bluetooth/smp.c static int smp_cmd_keypress_notify(struct l2cap_conn *conn, · line 2936
Quoted 2 lines
net/bluetooth/smp.c +2
-0+if (skb->len <sizeof (*kp)) +return SMP_INVALID_PARAMS ;
I suggested we add a bt_dev_warn_ratelimit with something like "Too small packet: skb->len %u < %u" to make debugging easier.
Quoted 7 lines
net/bluetooth/smp.c +1
-1+bt_dev_dbg (conn->hcon->hdev,"value 0x%02x" , kp->value);return 0 ; --2.54.0
Signature · 1 line
Luiz Augusto von Dentz
PATCH v2 1/1 Bluetooth: SMP: add missing skb len check in smp_cmd_keypress_notify
In reply to Muhammad Bilalsmp_cmd_keypress_notify() accesses the received payload as struct smp_cmd_keypress_notify without verifying that skb->len contains enough data.
smp_sig_channel() removes the opcode byte before dispatching to command handlers, so a SMP_CMD_KEYPRESS_NOTIFY packet without a payload leaves skb->len equal to zero on entry to the handler, causing a 1-byte out-of-bounds read from the heap.
Use skb_pull_data() to safely consume the payload; it performs a bounds check internally and returns NULL when the packet is too short. Add a ratelimited warning in that path to aid debugging of malformed packets, matching the pattern used by hci_event.c.
--- net/bluetooth/smp.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
net/bluetooth/smp.c +9 -1
@@ -2930,7 +2930,15 @@ static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)static int smp_cmd_keypress_notify (struct l2cap_conn *conn,struct sk_buff *skb) { -struct smp_cmd_keypress_notify *kp = (void *) skb->data; +struct smp_cmd_keypress_notify *kp; + + kp =skb_pull_data (skb,sizeof (struct smp_cmd_keypress_notify )); +if (!kp) { +bt_dev_warn_ratelimited (conn->hcon->hdev, +"Too small packet: skb->len %u < %zu" , + skb->len,sizeof (struct smp_cmd_keypress_notify )); +return SMP_INVALID_PARAMS ; + }bt_dev_dbg (conn->hcon->hdev,"value 0x%02x" , kp->value);
Signature · 1 line
2.54.0
This is automated email and please do not reply to this email!
Dear submitter,
Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=1096915
---Test result---
Test Summary:
CheckPatch PASS 0.54 seconds GitLint PASS 0.20 seconds SubjectPrefix PASS 0.06 seconds BuildKernel PASS 26.48 seconds CheckAllWarning PASS 29.04 seconds CheckSparse PASS 27.45 seconds BuildKernel32 PASS 25.80 seconds TestRunnerSetup PASS 568.32 seconds TestRunner_smp-tester PASS 17.82 seconds IncrementalBuild PASS 26.03 seconds
https://github.com/bluez/bluetooth-next/pull/212
---
Regards,
Linux Bluetooth