Skip to contentSkip to conversation

Bluetooth: hci_conn, hci_sync: Use __counted_by() in multiple structs and avoid -Wfamnae warnings

PATCH Applied Gustavo A. R. Silva 4 participants 5 messages Apr 26, 2024 – Apr 29, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Bluetooth: hci_conn, hci_sync: Use __counted_by() in multiple structs and avoid -Wfamnae warnings

Prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions).

Also, -Wflex-array-member-not-at-end is coming in GCC-14, and we are getting ready to enable it globally.

So, use the DEFINE_FLEX() helper for multiple on-stack definitions of a flexible structure where the size of the flexible-array member is known at compile-time, and refactor the rest of the code, accordingly.

Notice that, due to the use of __counted_by() in struct hci_cp_le_create_cis, the for loop in function hci_cs_le_create_cis() had to be modified. Once the index i, through which cp->cis[i] is accessed, falls in the interval [0, cp->num_cis), cp->num_cis cannot be decremented all the way down to zero while accessing cp->cis[]:

net/bluetooth/hci_event.c:4310:

4310    for (i = 0; cp->num_cis; cp->num_cis--, i++) {
                ...

4314 handle = __le16_to_cpu(cp->cis[i].cis_handle);

otherwise, only half (one iteration before cp->num_cis == i) or half plus one (one iteration before cp->num_cis < i) of the items in the array will be accessed before running into an out-of-bounds issue. So, in order to avoid this, set cp->num_cis to zero just after the for loop.

Also, make use of aux_num_cis variable to update cmd->num_cis after a list_for_each_entry_rcu() loop.

With these changes, fix the following warnings:
net/bluetooth/hci_sync.c:1239:56: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] net/bluetooth/hci_sync.c:1415:51: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] net/bluetooth/hci_sync.c:1731:51: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] net/bluetooth/hci_sync.c:6497:45: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

---
Changes in v2:
- Update cmd->num_cis after list_for_each_entry_rcu() loop.

v1:
- Link: https://lore.kernel.org/linux-hardening/ZiwqqZCa7PK9bzCX@neat/

 include/net/bluetooth/hci.h |  8 ++--
 net/bluetooth/hci_event.c   |  3 +-
 net/bluetooth/hci_sync.c    | 84 +++++++++++++++----------------------
 3 files changed, 40 insertions(+), 55 deletions(-)
include/net/bluetooth/hci.h +4 -4
index fe23e862921d..c4c6b8810701 100644
@@ -2026,7 +2026,7 @@ struct hci_cp_le_set_ext_adv_data {
 	__u8  operation;
 	__u8  frag_pref;
 	__u8  length;
-	__u8  data[];
+	__u8  data[] __counted_by(length);
 } __packed;
 
 #define HCI_OP_LE_SET_EXT_SCAN_RSP_DATA		0x2038
@@ -2035,7 +2035,7 @@ struct hci_cp_le_set_ext_scan_rsp_data {
 	__u8  operation;
 	__u8  frag_pref;
 	__u8  length;
-	__u8  data[];
+	__u8  data[] __counted_by(length);
 } __packed;
 
 #define HCI_OP_LE_SET_EXT_ADV_ENABLE		0x2039
@@ -2061,7 +2061,7 @@ struct hci_cp_le_set_per_adv_data {
 	__u8  handle;
 	__u8  operation;
 	__u8  length;
-	__u8  data[];
+	__u8  data[] __counted_by(length);
 } __packed;
 
 #define HCI_OP_LE_SET_PER_ADV_ENABLE		0x2040
@@ -2162,7 +2162,7 @@ struct hci_cis {
 
 struct hci_cp_le_create_cis {
 	__u8    num_cis;
-	struct hci_cis cis[];
+	struct hci_cis cis[] __counted_by(num_cis);
 } __packed;
 
 #define HCI_OP_LE_REMOVE_CIG			0x2065
net/bluetooth/hci_event.c +2 -1
index 9a38e155537e..9a7ca084302e 100644
@@ -4307,7 +4307,7 @@ static void hci_cs_le_create_cis(struct hci_dev *hdev, u8 status)
 	hci_dev_lock(hdev);
 
 	/* Remove connection if command failed */
-	for (i = 0; cp->num_cis; cp->num_cis--, i++) {
+	for (i = 0; i < cp->num_cis; i++) {
 		struct hci_conn *conn;
 		u16 handle;
 
@@ -4323,6 +4323,7 @@ static void hci_cs_le_create_cis(struct hci_dev *hdev, u8 status)
 			hci_conn_del(conn);
 		}
 	}
+	cp->num_cis = 0;
 
 	if (pending)
 		hci_le_create_cis_pending(hdev);
net/bluetooth/hci_sync.c +34 -50

188 lines not on this page · raw message

Signature · 1 line
2.34.1
#1 lore raw
Quoted 59 lines · Gustavo A. R. Silva

Prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions).

Also, -Wflex-array-member-not-at-end is coming in GCC-14, and we are getting ready to enable it globally.

So, use the DEFINE_FLEX() helper for multiple on-stack definitions of a flexible structure where the size of the flexible-array member is known at compile-time, and refactor the rest of the code, accordingly.

Notice that, due to the use of __counted_by() in struct hci_cp_le_create_cis, the for loop in function hci_cs_le_create_cis() had to be modified. Once the index i, through which cp->cis[i] is accessed, falls in the interval [0, cp->num_cis), cp->num_cis cannot be decremented all the way down to zero while accessing cp->cis[]:

net/bluetooth/hci_event.c:4310:

4310    for (i = 0; cp->num_cis; cp->num_cis--, i++) {
                ...

4314 handle = __le16_to_cpu(cp->cis[i].cis_handle);

otherwise, only half (one iteration before cp->num_cis == i) or half plus one (one iteration before cp->num_cis < i) of the items in the array will be accessed before running into an out-of-bounds issue. So, in order to avoid this, set cp->num_cis to zero just after the for loop.

Also, make use of aux_num_cis variable to update cmd->num_cis after a list_for_each_entry_rcu() loop.

With these changes, fix the following warnings:
net/bluetooth/hci_sync.c:1239:56: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] net/bluetooth/hci_sync.c:1415:51: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] net/bluetooth/hci_sync.c:1731:51: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] net/bluetooth/hci_sync.c:6497:45: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

---
Changes in v2:
- Update cmd->num_cis after list_for_each_entry_rcu() loop.

v1:
- Link: https://lore.kernel.org/linux-hardening/ZiwqqZCa7PK9bzCX@neat/

 include/net/bluetooth/hci.h |  8 ++--
 net/bluetooth/hci_event.c   |  3 +-
 net/bluetooth/hci_sync.c    | 84 +++++++++++++++----------------------
 3 files changed, 40 insertions(+), 55 deletions(-)
include/net/bluetooth/hci.h +0 -0
index fe23e862921d..c4c6b8810701 100644

include/net/bluetooth/hci.h struct hci_cp_le_set_ext_adv_data { · line 2030

Quoted 6 lines
include/net/bluetooth/hci.h +1 -1
@@ -2026,7 +2026,7 @@ struct hci_cp_le_set_ext_adv_data {
 	__u8  operation;
 	__u8  frag_pref;
 	__u8  length;
-	__u8  data[];
+	__u8  data[] __counted_by(length);
 } __packed;

I noticed some of the other structs here aren't flexible arrays, so it made me go take a look at these ones. I see that the only user of struct hci_cp_le_set_ext_adv_data uses a fixed-size array:

        struct {
                struct hci_cp_le_set_ext_adv_data cp;
                u8 data[HCI_MAX_EXT_AD_LENGTH];
        } pdu;

Let's just change this from a flex array to a fixed-size array?

include/net/bluetooth/hci.h struct hci_cp_le_set_ext_scan_rsp_data { · line 2039

Quoted 9 lines
include/net/bluetooth/hci.h +1 -1
 #define HCI_OP_LE_SET_EXT_SCAN_RSP_DATA		0x2038
@@ -2035,7 +2035,7 @@ struct hci_cp_le_set_ext_scan_rsp_data {
 	__u8  operation;
 	__u8  frag_pref;
 	__u8  length;
-	__u8  data[];
+	__u8  data[] __counted_by(length);
 } __packed;

Same for this one:

        struct {
                struct hci_cp_le_set_ext_scan_rsp_data cp;
                u8 data[HCI_MAX_EXT_AD_LENGTH];
        } pdu;

include/net/bluetooth/hci.h struct hci_cp_le_set_per_adv_data { · line 2065

Quoted 9 lines
include/net/bluetooth/hci.h +1 -1
 #define HCI_OP_LE_SET_EXT_ADV_ENABLE		0x2039
@@ -2061,7 +2061,7 @@ struct hci_cp_le_set_per_adv_data {
 	__u8  handle;
 	__u8  operation;
 	__u8  length;
-	__u8  data[];
+	__u8  data[] __counted_by(length);
 } __packed;

And this one. :P

        struct {
                struct hci_cp_le_set_per_adv_data cp;
                u8 data[HCI_MAX_PER_AD_LENGTH];
        } pdu;

include/net/bluetooth/hci.h struct hci_cis { · line 2166

Quoted 9 lines
include/net/bluetooth/hci.h +1 -1
 #define HCI_OP_LE_SET_PER_ADV_ENABLE		0x2040
@@ -2162,7 +2162,7 @@ struct hci_cis {
 
 struct hci_cp_le_create_cis {
 	__u8    num_cis;
-	struct hci_cis cis[];
+	struct hci_cis cis[] __counted_by(num_cis);
 } __packed;

This one isn't as obvious, so I'd say keep your changes for this one.

Quoted 16 lines
include/net/bluetooth/hci.h +0 -0
 #define HCI_OP_LE_REMOVE_CIG			0x2065
net/bluetooth/hci_event.c +1 -1
index 9a38e155537e..9a7ca084302e 100644
@@ -4307,7 +4307,7 @@ static void hci_cs_le_create_cis(struct hci_dev *hdev, u8 status)
 	hci_dev_lock(hdev);
 
 	/* Remove connection if command failed */
-	for (i = 0; cp->num_cis; cp->num_cis--, i++) {
+	for (i = 0; i < cp->num_cis; i++) {
 		struct hci_conn *conn;
 		u16 handle;
 

net/bluetooth/hci_event.c static void hci_cs_le_create_cis(struct hci_dev *hdev, u8 status) · line 4326

Quoted 4 lines
net/bluetooth/hci_event.c +1 -0
@@ -4323,6 +4323,7 @@ static void hci_cs_le_create_cis(struct hci_dev *hdev, u8 status)
 			hci_conn_del(conn);
 		}
 	}
+	cp->num_cis = 0;

Yeah, this loop never leaves early, and if it did, it processing the array forward, so having num_cis reduced during the loop iteration doesn't make sense. What you have looks right to me!

Quoted 12 lines
net/bluetooth/hci_event.c +0 -0
 	if (pending)
 		hci_le_create_cis_pending(hdev);
net/bluetooth/hci_sync.c +0 -1
index 9092b4d59545..6e15594d3565 100644
@@ -1235,31 +1235,27 @@ int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
 
 static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
 {
-	struct {

net/bluetooth/hci_sync.c int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) · line 1246

Quoted 10 lines
net/bluetooth/hci_sync.c +2 -4
-		struct hci_cp_le_set_ext_scan_rsp_data cp;
-		u8 data[HCI_MAX_EXT_AD_LENGTH];
-	} pdu;
+	DEFINE_FLEX(struct hci_cp_le_set_ext_scan_rsp_data, pdu, data, length,
+		    HCI_MAX_EXT_AD_LENGTH);
 	u8 len;
 	struct adv_info *adv = NULL;
 	int err;
 
-	memset(&pdu, 0, sizeof(pdu));

These become much easier, just:

	struct hci_cp_le_set_ext_scan_rsp_data cp = { };

etc...

Signature · 1 line
Kees Cook
#2 lore raw
Quoted 17 lines

include/net/bluetooth/hci.h struct hci_cp_le_set_ext_adv_data { · line 2030

Quoted 12 lines
include/net/bluetooth/hci.h +1 -1
index fe23e862921d..c4c6b8810701 100644
@@ -2026,7 +2026,7 @@ struct hci_cp_le_set_ext_adv_data {
  	__u8  operation;
  	__u8  frag_pref;
  	__u8  length;
-	__u8  data[];
+	__u8  data[] __counted_by(length);
  } __packed;

I noticed some of the other structs here aren't flexible arrays, so it made me go take a look at these ones. I see that the only user of struct hci_cp_le_set_ext_adv_data uses a fixed-size array:

Quoted 6 lines
         struct {
                 struct hci_cp_le_set_ext_adv_data cp;
                 u8 data[HCI_MAX_EXT_AD_LENGTH];
         } pdu;

Let's just change this from a flex array to a fixed-size array?

mmh... not sure about this. It would basically mean reverting this commit:

c9ed0a707730 ("Bluetooth: Fix Set Extended (Scan Response) Data")

--
Gustavo

Reviewed-by
#3 lore raw
Quoted 22 lines · Gustavo A. R. Silva
Quoted 22 lines

include/net/bluetooth/hci.h struct hci_cp_le_set_ext_adv_data { · line 2030

Quoted 12 lines
include/net/bluetooth/hci.h +1 -1
index fe23e862921d..c4c6b8810701 100644
@@ -2026,7 +2026,7 @@ struct hci_cp_le_set_ext_adv_data {
  	__u8  operation;
  	__u8  frag_pref;
  	__u8  length;
-	__u8  data[];
+	__u8  data[] __counted_by(length);
  } __packed;

I noticed some of the other structs here aren't flexible arrays, so it made me go take a look at these ones. I see that the only user of struct hci_cp_le_set_ext_adv_data uses a fixed-size array:

         struct {
                 struct hci_cp_le_set_ext_adv_data cp;
                 u8 data[HCI_MAX_EXT_AD_LENGTH];
         } pdu;
Quoted 5 lines
Quoted 1 line

Let's just change this from a flex array to a fixed-size array?

mmh... not sure about this. It would basically mean reverting this commit:

c9ed0a707730 ("Bluetooth: Fix Set Extended (Scan Response) Data")

That change doesn't seem to need to make them flex arrays, though -- there's no savings at all (the same amount is stack allocated).

Anyway, not a big deal, I guess. It's an improvement to be using __counted_by, so good! :)

Signature · 1 line
Kees Cook
#4 lore raw
In reply to Gustavo A. R. Silva
This patch was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:

Hello:

This patch was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:

Quoted 10 lines · On Fri, 26 Apr 2024 16:52:46 -0600 you wrote:

Prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions).

Also, -Wflex-array-member-not-at-end is coming in GCC-14, and we are getting ready to enable it globally.

[...]

Here is the summary with links:
- [v2,next] Bluetooth: hci_conn, hci_sync: Use __counted_by() in multiple structs and avoid -Wfamnae warnings

    https://git.kernel.org/bluetooth/bluetooth-next/c/4a4008a5b0e3

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html