Lee, Chun-Yi 30ab2a
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Lee, Chun-Yi 30ab2a
Date: Tue, 24 May 2022 13:02:45 -0700
Lee, Chun-Yi 30ab2a
Subject: Bluetooth: eir: Fix using strlen with hdev->{dev_name,short_name}
Lee, Chun-Yi 30ab2a
Patch-mainline: v6.0-rc1
Lee, Chun-Yi 30ab2a
Git-commit: dd7b8cdde098cf9f7c8de409b5b7bbb98f97be80
Lee, Chun-Yi 30ab2a
References: jsc#PED-1407
Lee, Chun-Yi 30ab2a
Lee, Chun-Yi 30ab2a
Both dev_name and short_name are not guaranteed to be NULL terminated so
Lee, Chun-Yi 30ab2a
this instead use strnlen and then attempt to determine if the resulting
Lee, Chun-Yi 30ab2a
string needs to be truncated or not.
Lee, Chun-Yi 30ab2a
Lee, Chun-Yi 30ab2a
Link: https://bugzilla.kernel.org/show_bug.cgi?id=216018
Lee, Chun-Yi 30ab2a
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Lee, Chun-Yi 30ab2a
Acked-by: Lee, Chun-Yi <jlee@suse.com>
Lee, Chun-Yi 30ab2a
---
Lee, Chun-Yi 30ab2a
 net/bluetooth/eir.c  |   41 ++++++++++++++++++++++++++---------------
Lee, Chun-Yi 30ab2a
 net/bluetooth/mgmt.c |    4 ++--
Lee, Chun-Yi 30ab2a
 2 files changed, 28 insertions(+), 17 deletions(-)
Lee, Chun-Yi 30ab2a
Lee, Chun-Yi 30ab2a
--- a/net/bluetooth/eir.c
Lee, Chun-Yi 30ab2a
+++ b/net/bluetooth/eir.c
Lee, Chun-Yi 30ab2a
@@ -13,6 +13,20 @@
Lee, Chun-Yi 30ab2a
 
Lee, Chun-Yi 30ab2a
 #define PNP_INFO_SVCLASS_ID		0x1200
Lee, Chun-Yi 30ab2a
 
Lee, Chun-Yi 30ab2a
+static u8 eir_append_name(u8 *eir, u16 eir_len, u8 type, u8 *data, u8 data_len)
Lee, Chun-Yi 30ab2a
+{
Lee, Chun-Yi 30ab2a
+	u8 name[HCI_MAX_SHORT_NAME_LENGTH + 1];
Lee, Chun-Yi 30ab2a
+
Lee, Chun-Yi 30ab2a
+	/* If data is already NULL terminated just pass it directly */
Lee, Chun-Yi 30ab2a
+	if (data[data_len - 1] == '\0')
Lee, Chun-Yi 30ab2a
+		return eir_append_data(eir, eir_len, type, data, data_len);
Lee, Chun-Yi 30ab2a
+
Lee, Chun-Yi 30ab2a
+	memcpy(name, data, HCI_MAX_SHORT_NAME_LENGTH);
Lee, Chun-Yi 30ab2a
+	name[HCI_MAX_SHORT_NAME_LENGTH] = '\0';
Lee, Chun-Yi 30ab2a
+
Lee, Chun-Yi 30ab2a
+	return eir_append_data(eir, eir_len, type, name, sizeof(name));
Lee, Chun-Yi 30ab2a
+}
Lee, Chun-Yi 30ab2a
+
Lee, Chun-Yi 30ab2a
 u8 eir_append_local_name(struct hci_dev *hdev, u8 *ptr, u8 ad_len)
Lee, Chun-Yi 30ab2a
 {
Lee, Chun-Yi 30ab2a
 	size_t short_len;
Lee, Chun-Yi 30ab2a
@@ -23,29 +37,26 @@ u8 eir_append_local_name(struct hci_dev
Lee, Chun-Yi 30ab2a
 		return ad_len;
Lee, Chun-Yi 30ab2a
 
Lee, Chun-Yi 30ab2a
 	/* use complete name if present and fits */
Lee, Chun-Yi 30ab2a
-	complete_len = strlen(hdev->dev_name);
Lee, Chun-Yi 30ab2a
+	complete_len = strnlen(hdev->dev_name, sizeof(hdev->dev_name));
Lee, Chun-Yi 30ab2a
 	if (complete_len && complete_len <= HCI_MAX_SHORT_NAME_LENGTH)
Lee, Chun-Yi 30ab2a
-		return eir_append_data(ptr, ad_len, EIR_NAME_COMPLETE,
Lee, Chun-Yi 30ab2a
+		return eir_append_name(ptr, ad_len, EIR_NAME_COMPLETE,
Lee, Chun-Yi 30ab2a
 				       hdev->dev_name, complete_len + 1);
Lee, Chun-Yi 30ab2a
 
Lee, Chun-Yi 30ab2a
 	/* use short name if present */
Lee, Chun-Yi 30ab2a
-	short_len = strlen(hdev->short_name);
Lee, Chun-Yi 30ab2a
+	short_len = strnlen(hdev->short_name, sizeof(hdev->short_name));
Lee, Chun-Yi 30ab2a
 	if (short_len)
Lee, Chun-Yi 30ab2a
-		return eir_append_data(ptr, ad_len, EIR_NAME_SHORT,
Lee, Chun-Yi 30ab2a
-				       hdev->short_name, short_len + 1);
Lee, Chun-Yi 30ab2a
+		return eir_append_name(ptr, ad_len, EIR_NAME_SHORT,
Lee, Chun-Yi 30ab2a
+				       hdev->short_name,
Lee, Chun-Yi 30ab2a
+				       short_len == HCI_MAX_SHORT_NAME_LENGTH ?
Lee, Chun-Yi 30ab2a
+				       short_len : short_len + 1);
Lee, Chun-Yi 30ab2a
 
Lee, Chun-Yi 30ab2a
 	/* use shortened full name if present, we already know that name
Lee, Chun-Yi 30ab2a
 	 * is longer then HCI_MAX_SHORT_NAME_LENGTH
Lee, Chun-Yi 30ab2a
 	 */
Lee, Chun-Yi 30ab2a
-	if (complete_len) {
Lee, Chun-Yi 30ab2a
-		u8 name[HCI_MAX_SHORT_NAME_LENGTH + 1];
Lee, Chun-Yi 30ab2a
-
Lee, Chun-Yi 30ab2a
-		memcpy(name, hdev->dev_name, HCI_MAX_SHORT_NAME_LENGTH);
Lee, Chun-Yi 30ab2a
-		name[HCI_MAX_SHORT_NAME_LENGTH] = '\0';
Lee, Chun-Yi 30ab2a
-
Lee, Chun-Yi 30ab2a
-		return eir_append_data(ptr, ad_len, EIR_NAME_SHORT, name,
Lee, Chun-Yi 30ab2a
-				       sizeof(name));
Lee, Chun-Yi 30ab2a
-	}
Lee, Chun-Yi 30ab2a
+	if (complete_len)
Lee, Chun-Yi 30ab2a
+		return eir_append_name(ptr, ad_len, EIR_NAME_SHORT,
Lee, Chun-Yi 30ab2a
+				       hdev->dev_name,
Lee, Chun-Yi 30ab2a
+				       HCI_MAX_SHORT_NAME_LENGTH);
Lee, Chun-Yi 30ab2a
 
Lee, Chun-Yi 30ab2a
 	return ad_len;
Lee, Chun-Yi 30ab2a
 }
Lee, Chun-Yi 30ab2a
@@ -181,7 +192,7 @@ void eir_create(struct hci_dev *hdev, u8
Lee, Chun-Yi 30ab2a
 	u8 *ptr = data;
Lee, Chun-Yi 30ab2a
 	size_t name_len;
Lee, Chun-Yi 30ab2a
 
Lee, Chun-Yi 30ab2a
-	name_len = strlen(hdev->dev_name);
Lee, Chun-Yi 30ab2a
+	name_len = strnlen(hdev->dev_name, sizeof(hdev->dev_name));
Lee, Chun-Yi 30ab2a
 
Lee, Chun-Yi 30ab2a
 	if (name_len > 0) {
Lee, Chun-Yi 30ab2a
 		/* EIR Data type */
Lee, Chun-Yi 30ab2a
--- a/net/bluetooth/mgmt.c
Lee, Chun-Yi 30ab2a
+++ b/net/bluetooth/mgmt.c
Lee, Chun-Yi 30ab2a
@@ -1082,11 +1082,11 @@ static u16 append_eir_data_to_buf(struct
Lee, Chun-Yi 30ab2a
 		eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE,
Lee, Chun-Yi 30ab2a
 					  hdev->appearance);
Lee, Chun-Yi 30ab2a
 
Lee, Chun-Yi 30ab2a
-	name_len = strlen(hdev->dev_name);
Lee, Chun-Yi 30ab2a
+	name_len = strnlen(hdev->dev_name, sizeof(hdev->dev_name));
Lee, Chun-Yi 30ab2a
 	eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE,
Lee, Chun-Yi 30ab2a
 				  hdev->dev_name, name_len);
Lee, Chun-Yi 30ab2a
 
Lee, Chun-Yi 30ab2a
-	name_len = strlen(hdev->short_name);
Lee, Chun-Yi 30ab2a
+	name_len = strnlen(hdev->short_name, sizeof(hdev->short_name));
Lee, Chun-Yi 30ab2a
 	eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT,
Lee, Chun-Yi 30ab2a
 				  hdev->short_name, name_len);
Lee, Chun-Yi 30ab2a