Bluetooth: Fix more mgmt parameter checks
Subsystems (2)
cover Bluetooth: Fix more mgmt parameter checks
Hi,
This patch set is intended to be applied after Szymon's "Bluetooth: Fix pair device command reply if adapter is powered off" patch.
This set includes fixes to improve the validity checks on parameters we receive from user space in mgmt commands. Each of these fixes has had its own test case added to the user space mgmt-tester and has been verified to work.
Johan
PATCH 1/10 Bluetooth: Fix checking for correct mgmt_load_link_keys parameters
From: Johan Hedberg <[email protected]>
The debug_keys parameter is only allowed to have the values 0x00 and 0x01. Any other value should result in a proper command status with MGMT_STATUS_INVALID_PARAMS.
--- net/bluetooth/mgmt.c | 4 ++++ 1 file changed, 4 insertions(+)
net/bluetooth/mgmt.c +4 -0
@@ -1519,6 +1519,10 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data, MGMT_STATUS_INVALID_PARAMS); } +if (cp->debug_keys !=0x00 && cp->debug_keys !=0x01 ) +return cmd_status (sk, hdev->id,MGMT_OP_LOAD_LINK_KEYS , +MGMT_STATUS_INVALID_PARAMS ); +BT_DBG ("%s debug_keys %u key_count %u" , hdev->name, cp->debug_keys, key_count);
Signature · 1 line
1.7.10.4
PATCH 2/10 Bluetooth: Fix returning proper mgmt status for Load LTKs
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
Failures of mgmt commands should be indicated with valid mgmt status codes, and EINVAL is not one of them. Instead MGMT_STATUS_INVALID_PARAMS should be returned.
--- net/bluetooth/mgmt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/bluetooth/mgmt.c +1 -1
@@ -2716,7 +2716,7 @@ static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,BT_ERR ("load_keys: expected %u bytes, got %u bytes" , len, expected_len);return cmd_status (sk, hdev->id,MGMT_OP_LOAD_LONG_TERM_KEYS , -EINVAL ); +MGMT_STATUS_INVALID_PARAMS ); }BT_DBG ("%s key_count %u" , hdev->name, key_count);
Signature · 1 line
1.7.10.4
PATCH 3/10 Bluetooth: Fix checking for proper key->master value in Load LTKs
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
The allowed values for the key->master parameter in the Load LTKs command are 0x00 and 0x01. If there is a key in the list with some other value the command should fail with a proper invalid params response.
--- net/bluetooth/mgmt.c | 9 +++++++++ 1 file changed, 9 insertions(+)
net/bluetooth/mgmt.c +9 -0
@@ -2729,6 +2729,14 @@ static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,struct mgmt_ltk_info *key = &cp->keys[i];u8 type; +if (key->master !=0x00 && key->master !=0x01 ) { +hci_smp_ltks_clear (hdev); + err =cmd_status (sk, hdev->id, +MGMT_OP_LOAD_LONG_TERM_KEYS , +MGMT_STATUS_INVALID_PARAMS ); + goto unlock; + } +if (key->master) type =HCI_SMP_LTK ;else
@@ -2743,6 +2751,7 @@ static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev, err =cmd_complete (sk, hdev->id,MGMT_OP_LOAD_LONG_TERM_KEYS ,0 ,NULL ,0 ); +unlock:hci_dev_unlock (hdev);return err;
Signature · 1 line
1.7.10.4
PATCH 4/10 Bluetooth: Refactor valid LTK data testing into its own function
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
This patch refactors valid LTK data testing into its own function. This will help keep the code readable since there are several tests still missing that need to be done on the LTK data.
--- net/bluetooth/mgmt.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
net/bluetooth/mgmt.c +8 -1
@@ -2701,6 +2701,13 @@ done:return err; } +static bool ltk_is_valid (struct mgmt_ltk_info *key) +{ +if (key->master !=0x00 && key->master !=0x01 ) +return false; +return true; +} +static int load_long_term_keys (struct sock *sk,struct hci_dev *hdev,void *cp_data,u16 len) {
@@ -2729,7 +2736,7 @@ static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,struct mgmt_ltk_info *key = &cp->keys[i];u8 type; -if (key->master !=0x00 && key->master !=0x01 ) { +if (!ltk_is_valid (key)) {hci_smp_ltks_clear (hdev); err = cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
Signature · 1 line
1.7.10.4
PATCH 5/10 Bluetooth: Check for valid key->authenticated value for LTKs
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
This patch adds necessary checks for the two allowed values of the authenticated parameter of each Long Term Key, i.e. 0x00 and 0x01. If any other value is encountered the valid response is to return invalid params to user space.
--- net/bluetooth/mgmt.c | 2 ++ 1 file changed, 2 insertions(+)
net/bluetooth/mgmt.c +2 -0
@@ -2703,6 +2703,8 @@ done:static bool ltk_is_valid (struct mgmt_ltk_info *key) { +if (key->authenticated !=0x00 && key->authenticated !=0x01 ) +return false;if (key->master !=0x00 && key->master !=0x01 )return false;return true;
Signature · 1 line
1.7.10.4
PATCH 6/10 Bluetooth: Add helper functions for testing bdaddr types
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
This patch adds two helper functions to test for valid bdaddr type values. These will be particularely useful in the mgmt code to check that user space has passed valid values to the kernel.
--- include/net/bluetooth/bluetooth.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
include/net/bluetooth/bluetooth.h +23 -0
@@ -166,6 +166,29 @@ typedef struct {#define BDADDR_LE_PUBLIC 0x01#define BDADDR_LE_RANDOM 0x02 +static inline bool bdaddr_type_is_valid (__u8 type) +{ +switch (type) { +case BDADDR_BREDR : +case BDADDR_LE_PUBLIC : +case BDADDR_LE_RANDOM : +return true; + } + +return false; +} + +static inline bool bdaddr_type_is_le (__u8 type) +{ +switch (type) { +case BDADDR_LE_PUBLIC : +case BDADDR_LE_RANDOM : +return true; + } + +return false; +} +#define BDADDR_ANY (&(bdaddr_t) {{0, 0, 0, 0, 0, 0} })#define BDADDR_LOCAL (&(bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff} })
Signature · 1 line
1.7.10.4
PATCH 7/10 Bluetooth: Fix checking for valid address type values in mgmt commands
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
This patch adds checks for valid address type values passed to mgmt commands. If an invalid address type is encountered the code will return a proper invalid params response.
--- net/bluetooth/mgmt.c | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-)
net/bluetooth/mgmt.c +38 -5
@@ -1506,7 +1506,7 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data, {struct mgmt_cp_load_link_keys *cp = data;u16 key_count, expected_len; -int i; +int i, err; key_count =__le16_to_cpu (cp->key_count);
@@ -1540,15 +1540,24 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,for (i =0 ; i < key_count; i++) {struct mgmt_link_key_info *key = &cp->keys[i]; +if (key->addr.type !=BDADDR_BREDR ) { +clear_bit (HCI_DEBUG_KEYS , &hdev->dev_flags); +hci_link_keys_clear (hdev); + err =cmd_status (sk, hdev->id,MGMT_OP_LOAD_LINK_KEYS , +MGMT_STATUS_INVALID_PARAMS ); + goto unlock; + } +hci_add_link_key (hdev,NULL ,0 , &key->addr.bdaddr, key->val, key->type, key->pin_len); } -cmd_complete (sk, hdev->id,MGMT_OP_LOAD_LINK_KEYS ,0 ,NULL ,0 ); + err =cmd_complete (sk, hdev->id,MGMT_OP_LOAD_LINK_KEYS ,0 ,NULL ,0 ); +unlock:hci_dev_unlock (hdev); -return 0 ; +return err; } static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
@@ -1573,12 +1582,17 @@ static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,struct hci_conn *conn;int err; -hci_dev_lock (hdev); -memset (&rp,0 ,sizeof (rp));bacpy (&rp.addr.bdaddr, &cp->addr.bdaddr); rp.addr.type = cp->addr.type; +if (!bdaddr_type_is_valid (cp->addr.type)) +return cmd_complete (sk, hdev->id,MGMT_OP_UNPAIR_DEVICE , +MGMT_STATUS_INVALID_PARAMS , + &rp,sizeof (rp)); + +hci_dev_lock (hdev); +if (!hdev_is_powered (hdev)) { err =cmd_complete (sk, hdev->id,MGMT_OP_UNPAIR_DEVICE ,MGMT_STATUS_NOT_POWERED , &rp,sizeof (rp));
@@ -1643,6 +1657,10 @@ static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,BT_DBG ("" ); +if (!bdaddr_type_is_valid (cp->addr.type)) +return cmd_status (sk, hdev->id,MGMT_OP_DISCONNECT , +MGMT_STATUS_INVALID_PARAMS ); +hci_dev_lock (hdev);if (!test_bit (HCI_UP , &hdev->flags)) {
@@ -1947,6 +1965,11 @@ static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,bacpy (&rp.addr.bdaddr, &cp->addr.bdaddr); rp.addr.type = cp->addr.type; +if (!bdaddr_type_is_valid (cp->addr.type)) +return cmd_complete (sk, hdev->id,MGMT_OP_PAIR_DEVICE , +MGMT_STATUS_INVALID_PARAMS , + &rp,sizeof (rp)); +hci_dev_lock (hdev);if (!hdev_is_powered (hdev)) {
@@ -2564,6 +2587,10 @@ static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,BT_DBG ("%s" , hdev->name); +if (!bdaddr_type_is_valid (cp->addr.type)) +return cmd_status (sk, hdev->id,MGMT_OP_BLOCK_DEVICE , +MGMT_STATUS_INVALID_PARAMS ); +hci_dev_lock (hdev); err =hci_blacklist_add (hdev, &cp->addr.bdaddr, cp->addr.type);
@@ -2589,6 +2616,10 @@ static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,BT_DBG ("%s" , hdev->name); +if (!bdaddr_type_is_valid (cp->addr.type)) +return cmd_status (sk, hdev->id,MGMT_OP_UNBLOCK_DEVICE , +MGMT_STATUS_INVALID_PARAMS ); +hci_dev_lock (hdev); err =hci_blacklist_del (hdev, &cp->addr.bdaddr, cp->addr.type);
@@ -2707,6 +2738,8 @@ static bool ltk_is_valid(struct mgmt_ltk_info *key)return false;if (key->master !=0x00 && key->master !=0x01 )return false; +if (!bdaddr_type_is_le (key->addr.type)) +return false;return true; }
Signature · 1 line
1.7.10.4
PATCH 8/10 Bluetooth: Fix checking for valid disconnect parameters in unpair_device
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
The valid values for the Disconnect parameter in the Unpair Device command are 0x00 and 0x01. If any other value is encountered the command should fail with the appropriate invalid params response.
--- net/bluetooth/mgmt.c | 5 +++++ 1 file changed, 5 insertions(+)
net/bluetooth/mgmt.c +5 -0
@@ -1591,6 +1591,11 @@ static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,MGMT_STATUS_INVALID_PARAMS , &rp, sizeof(rp)); +if (cp->disconnect !=0x00 && cp->disconnect !=0x01 ) +return cmd_complete (sk, hdev->id,MGMT_OP_UNPAIR_DEVICE , +MGMT_STATUS_INVALID_PARAMS , + &rp,sizeof (rp)); +hci_dev_lock (hdev);if (!hdev_is_powered (hdev)) {
Signature · 1 line
1.7.10.4
PATCH 9/10 Bluetooth: Fix returning proper cmd_complete for mgmt_disconnect
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
The Disconnect Management command should return Command Complete instead of Command Status whenever possible so that user space can distinguish exactly which command failed in the case of multiple commands. This patch does the necessary changes in the disconnect command handler to return the right event to user space.
--- net/bluetooth/mgmt.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)
net/bluetooth/mgmt.c +14 -8
@@ -1655,6 +1655,7 @@ static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) {struct mgmt_cp_disconnect *cp = data; +struct mgmt_rp_disconnect rp;struct hci_cp_disconnect dc;struct pending_cmd *cmd;struct hci_conn *conn;
@@ -1662,21 +1663,26 @@ static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,BT_DBG ("" ); +memset (&rp,0 ,sizeof (rp)); +bacpy (&rp.addr.bdaddr, &cp->addr.bdaddr); + rp.addr.type = cp->addr.type; +if (!bdaddr_type_is_valid (cp->addr.type)) -return cmd_status (sk, hdev->id,MGMT_OP_DISCONNECT , -MGMT_STATUS_INVALID_PARAMS ); +return cmd_complete (sk, hdev->id,MGMT_OP_DISCONNECT , +MGMT_STATUS_INVALID_PARAMS , + &rp,sizeof (rp));hci_dev_lock (hdev);if (!test_bit (HCI_UP , &hdev->flags)) { - err =cmd_status (sk, hdev->id,MGMT_OP_DISCONNECT , -MGMT_STATUS_NOT_POWERED ); + err =cmd_complete (sk, hdev->id,MGMT_OP_DISCONNECT , +MGMT_STATUS_NOT_POWERED , &rp,sizeof (rp)); goto failed; }if (mgmt_pending_find (MGMT_OP_DISCONNECT , hdev)) { - err =cmd_status (sk, hdev->id,MGMT_OP_DISCONNECT , -MGMT_STATUS_BUSY ); + err =cmd_complete (sk, hdev->id,MGMT_OP_DISCONNECT , +MGMT_STATUS_BUSY , &rp,sizeof (rp)); goto failed; }
@@ -1687,8 +1693,8 @@ static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data, conn =hci_conn_hash_lookup_ba (hdev,LE_LINK , &cp->addr.bdaddr);if (!conn || conn->state ==BT_OPEN || conn->state ==BT_CLOSED ) { - err =cmd_status (sk, hdev->id,MGMT_OP_DISCONNECT , -MGMT_STATUS_NOT_CONNECTED ); + err =cmd_complete (sk, hdev->id,MGMT_OP_DISCONNECT , +MGMT_STATUS_NOT_CONNECTED , &rp,sizeof (rp)); goto failed; }
Signature · 1 line
1.7.10.4
PATCH 10/10 Bluetooth: Fix returning proper cmd_complete for mgmt_block/unblock
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
The Block/Unblock Device Management commands should return Command Complete instead of Command Status whenever possible so that user space can distinguish exactly which command failed in the case of multiple commands. This patch does the necessary changes in the command handler to return the right event to user space.
--- net/bluetooth/mgmt.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
net/bluetooth/mgmt.c +6 -4
@@ -2599,8 +2599,9 @@ static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,BT_DBG ("%s" , hdev->name);if (!bdaddr_type_is_valid (cp->addr.type)) -return cmd_status (sk, hdev->id,MGMT_OP_BLOCK_DEVICE , -MGMT_STATUS_INVALID_PARAMS ); +return cmd_complete (sk, hdev->id,MGMT_OP_BLOCK_DEVICE , +MGMT_STATUS_INVALID_PARAMS , + &cp->addr,sizeof (cp->addr));hci_dev_lock (hdev);
@@ -2628,8 +2629,9 @@ static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,BT_DBG ("%s" , hdev->name);if (!bdaddr_type_is_valid (cp->addr.type)) -return cmd_status (sk, hdev->id,MGMT_OP_UNBLOCK_DEVICE , -MGMT_STATUS_INVALID_PARAMS ); +return cmd_complete (sk, hdev->id,MGMT_OP_UNBLOCK_DEVICE , +MGMT_STATUS_INVALID_PARAMS , + &cp->addr,sizeof (cp->addr));hci_dev_lock (hdev);
Signature · 1 line
1.7.10.4
Marcel Holtmann
maintainerRe: [PATCH 07/10] Bluetooth: Fix checking for valid address type values in mgmt commands
In reply to Johan HedbergHi Johan,
Quoted 27 lines
This patch adds checks for valid address type values passed to mgmt commands. If an invalid address type is encountered the code will return a proper invalid params response.
Signed-off-by Johan Hedberg <[email protected]>--- net/bluetooth/mgmt.c | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-)net/bluetooth/mgmt.c +2
-1@@ -1506,7 +1506,7 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data, {struct mgmt_cp_load_link_keys *cp = data;u16 key_count, expected_len; -int i; +int i, err; key_count =__le16_to_cpu (cp->key_count);@@ -1540,15 +1540,24 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,for (i =0 ; i < key_count; i++) {struct mgmt_link_key_info *key = &cp->keys[i]; +if (key->addr.type !=BDADDR_BREDR ) {
net/bluetooth/mgmt.c static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data, · line 1553
Quoted 10 lines
net/bluetooth/mgmt.c +7
-0+clear_bit (HCI_DEBUG_KEYS , &hdev->dev_flags); +hci_link_keys_clear (hdev); + err =cmd_status (sk, hdev->id,MGMT_OP_LOAD_LINK_KEYS , +MGMT_STATUS_INVALID_PARAMS ); + goto unlock; + } +hci_add_link_key (hdev,NULL ,0 , &key->addr.bdaddr, key->val, key->type, key->pin_len); }
I am not a huge fan of doing it this way (same applies to LTKs). You are introducing a side effect here. An invalid command will clear the keys.
Please check the validity of the key parameters ahead of time and not in the middle of applying them. Your rollback is broken since it just clears all keys.
We could add /sys/kernel/debug/bluetooth/hci0/link_keys as a root-only entry to allow checking this mgmt-tester.
Regards
Marcel
Re: [PATCH 08/10] Bluetooth: Fix checking for valid disconnect parameters in unpair_device
In reply to Johan HedbergHi Johan,
Quoted 8 lines
The valid values for the Disconnect parameter in the Unpair Device command are
0x00and0x01. If any other value is encountered the command should fail with the appropriate invalid params response.Signed-off-by Johan Hedberg <[email protected]>--- net/bluetooth/mgmt.c | 5 +++++ 1 file changed, 5 insertions(+)
Regards
Marcel
Re: [PATCH 01/10] Bluetooth: Fix checking for correct mgmt_load_link_keys parameters
In reply to Johan HedbergHi Johan,
Quoted 8 lines
The
debug_keysparameter is only allowed to have the values0x00and0x01. Any other value should result in a proper command status withMGMT_STATUS_INVALID_PARAMS.Signed-off-by Johan Hedberg <[email protected]>--- net/bluetooth/mgmt.c | 4 ++++ 1 file changed, 4 insertions(+)
Regards
Marcel
Re: [PATCH 02/10] Bluetooth: Fix returning proper mgmt status for Load LTKs
In reply to Johan HedbergHi Johan,
Quoted 8 lines
Failures of mgmt commands should be indicated with valid mgmt status codes, and EINVAL is not one of them. Instead
MGMT_STATUS_INVALID_PARAMSshould be returned.Signed-off-by Johan Hedberg <[email protected]>--- net/bluetooth/mgmt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Regards
Marcel