Skip to contentSkip to conversation

wifi: ath10k: Asking for some light on this, please :)

RFC Gustavo A. R. Silva 3 participants 8 messages Oct 24, 2023 – Dec 12, 2023 Qualcomm Atheros ATH10K wireless driver

lore
Share & tools
lore
#0 lore raw

Hi all,

While working on tranforming one-element array peer_chan_list in struct wmi_tdls_peer_capabilities into a flex-array member

7187 struct wmi_tdls_peer_capabilities {
...
7199 struct wmi_channel peer_chan_list[1];
7200 } __packed;

the following line caught my attention:

./drivers/net/wireless/ath/ath10k/wmi.c:
8920 memset(skb->data, 0, sizeof(*cmd));

Notice that before the flex-array transformation, we are zeroing 128 bytes in skb->data because sizeof(*cmd) == 128, see below:

$ pahole -C wmi_10_4_tdls_peer_update_cmd drivers/net/wireless/ath/ath10k/wmi.o
struct wmi_10_4_tdls_peer_update_cmd {
	__le32                     vdev_id;              /*     0     4 */
	struct wmi_mac_addr        peer_macaddr;         /*     4     8 */
	__le32                     peer_state;           /*    12     4 */
	__le32                     reserved[4];          /*    16    16 */
	struct wmi_tdls_peer_capabilities peer_capab;    /*    32    96 */
	/* size: 128, cachelines: 2, members: 5 */
};

So, after the flex-array transformation (and the necessary adjustments to a few other lines of code) we would be zeroing 104 bytes in skb->data because sizeof(*cmd) == 104, see below:

$ pahole -C wmi_10_4_tdls_peer_update_cmd drivers/net/wireless/ath/ath10k/wmi.o
struct wmi_10_4_tdls_peer_update_cmd {
	__le32                     vdev_id;              /*     0     4 */
	struct wmi_mac_addr        peer_macaddr;         /*     4     8 */
	__le32                     peer_state;           /*    12     4 */
	__le32                     reserved[4];          /*    16    16 */
	struct wmi_tdls_peer_capabilities peer_capab;    /*    32    72 */
	/* size: 104, cachelines: 2, members: 5 */
	/* last cacheline: 40 bytes */
};

This difference arises because the size of the element type for the peer_chan_list array, which is sizeof(struct wmi_channel) == 24

$ pahole -C wmi_channel drivers/net/wireless/ath/ath10k/wmi.o
struct wmi_channel {
	__le32                     mhz;                  /*     0     4 */
	__le32                     band_center_freq1;    /*     4     4 */
	__le32                     band_center_freq2;    /*     8     4 */

[..]

                                               /*    20     4 */

	/* size: 24, cachelines: 1, members: 6 */
	/* last cacheline: 24 bytes */
};

is included in sizeof(*cmd) before the transformation.

So, my question is: do we really need to zero out those extra 24 bytes in skb->data? or is it rather a bug in the original code?

Thanks!
--
Gustavo

#1 lore raw
Quoted 15 lines · Gustavo A. R. Silva

Hi all,

While working on tranforming one-element array peer_chan_list in struct wmi_tdls_peer_capabilities into a flex-array member

7187 struct wmi_tdls_peer_capabilities {
...
7199 struct wmi_channel peer_chan_list[1];
7200 } __packed;

the following line caught my attention:

./drivers/net/wireless/ath/ath10k/wmi.c:
8920 memset(skb->data, 0, sizeof(*cmd));

Quoted 7 lines

Notice that before the flex-array transformation, we are zeroing 128 bytes in skb->data because sizeof(*cmd) == 128, see below:

So, my question is: do we really need to zero out those extra 24 bytes in skb->data? or is it rather a bug in the original code?

If we look a step further, I _think_ even that memset is unnecessary?

struct sk_buff *ath10k_wmi_alloc_skb(struct ath10k *ar, u32 len)

{
        struct sk_buff *skb;
        u32 round_len = roundup(len, 4);

        skb = ath10k_htc_alloc_skb(ar, WMI_SKB_HEADROOM + round_len);
        if (!skb)
                return NULL;

        skb_reserve(skb, WMI_SKB_HEADROOM);
        if (!IS_ALIGNED((unsigned long)skb->data, 4))
                ath10k_warn(ar, "Unaligned WMI skb\n");

        skb_put(skb, round_len);
        memset(skb->data, 0, round_len);

        return skb;
}

So shouldn't the outgoing skb be exactly the same?

Anyway, just looking at the code out of curiosity, I don't actually know anything about this driver :)

johannes

#2 lore raw
Quoted 20 lines · Johannes Berg
Quoted 17 lines · Gustavo A. R. Silva

Hi all,

While working on tranforming one-element array peer_chan_list in struct wmi_tdls_peer_capabilities into a flex-array member

7187 struct wmi_tdls_peer_capabilities {
...
7199 struct wmi_channel peer_chan_list[1];
7200 } __packed;

the following line caught my attention:

./drivers/net/wireless/ath/ath10k/wmi.c:
8920 memset(skb->data, 0, sizeof(*cmd));

Notice that before the flex-array transformation, we are zeroing 128 bytes in skb->data because sizeof(*cmd) == 128, see below:

Quoted 5 lines
Quoted 3 lines

So, my question is: do we really need to zero out those extra 24 bytes in skb->data? or is it rather a bug in the original code?

If we look a step further, I _think_ even that memset is unnecessary?

It seems we run into the same issue in the function below, even in the case this memset() is unnecessary (which it seems it's not):

	8920         memset(skb->data, 0, sizeof(*cmd));

Notice that if cap->peer_chan_len == 0 or cap->peer_chan_len == 1, in the original code, we have len == sizeof(*cmd) == 128:

drivers/net/wireless/ath/ath10k/wmi.c:
8911 /* tdls peer update cmd has place holder for one channel*/
8912 chan_len = cap->peer_chan_len ? (cap->peer_chan_len - 1) : 0;
8913
8914 len = sizeof(*cmd) + chan_len * sizeof(*chan);
8915
8916 skb = ath10k_wmi_alloc_skb(ar, len);

Quoted 16 lines

struct sk_buff *ath10k_wmi_alloc_skb(struct ath10k *ar, u32 len)

{
         struct sk_buff *skb;
         u32 round_len = roundup(len, 4);

         skb = ath10k_htc_alloc_skb(ar, WMI_SKB_HEADROOM + round_len);
         if (!skb)
                 return NULL;

         skb_reserve(skb, WMI_SKB_HEADROOM);
         if (!IS_ALIGNED((unsigned long)skb->data, 4))
                 ath10k_warn(ar, "Unaligned WMI skb\n");

         skb_put(skb, round_len);

so round_len == roundup(len, 4) == 128 at the moment of this memset() call:

Quoted 1 line
         memset(skb->data, 0, round_len);

which take us back to the same problem, this time in the memset() above, because after the flex-array transformation we would have:

drivers/net/wireless/ath/ath10k/wmi.c +2 -5
@@ -8905,13 +8905,10 @@ ath10k_wmi_10_4_gen_tdls_peer_update(struct ath10k *ar,
        struct wmi_channel *chan;
        struct sk_buff *skb;
        u32 peer_qos;
-       int len, chan_len;
+       size_t len;
        int i;
 
-       /* tdls peer update cmd has place holder for one channel*/
-       chan_len = cap->peer_chan_len ? (cap->peer_chan_len - 1) : 0;
-
-       len = sizeof(*cmd) + chan_len * sizeof(*chan);
+       len = struct_size(cmd, peer_capab.peer_chan_list, cap->peer_chan_len);
 
        skb = ath10k_wmi_alloc_skb(ar, len);
        if (!skb)

which makes round_len == roundup(len, 4) == struct_size(cmd,...,...) == 104 when cap->peer_chan_len == 0

Quoted 1 line

So shouldn't the outgoing skb be exactly the same?

It seems it's not.

Quoted 5 lines

Anyway, just looking at the code out of curiosity, I don't actually know anything about this driver :)

johannes

--
Gustavo

#3 lore raw
Quoted 8 lines · Gustavo A. R. Silva

It seems we run into the same issue in the function below, even in the case this memset() is unnecessary (which it seems it's not):

	8920         memset(skb->data, 0, sizeof(*cmd));

Notice that if cap->peer_chan_len == 0 or cap->peer_chan_len == 1, in the original code, we have len == sizeof(*cmd) == 128:

Right.

Quoted 11 lines
drivers/net/wireless/ath/ath10k/wmi.c +1 -4
-       /* tdls peer update cmd has place holder for one channel*/
-       chan_len = cap->peer_chan_len ? (cap->peer_chan_len - 1) : 0;
-
-       len = sizeof(*cmd) + chan_len * sizeof(*chan);
+       len = struct_size(cmd, peer_capab.peer_chan_list, cap->peer_chan_len);
 
         skb = ath10k_wmi_alloc_skb(ar, len);
         if (!skb)

which makes round_len == roundup(len, 4) == struct_size(cmd,...,...) == 104 when cap->peer_chan_len == 0

And yeah, that's really the issue, it only matters for ==0. For a moment there I thought that doesn't even make sense, but it looks like it never even becomes non-zero.

No idea then, sorry. You'd hope firmware doesn't care about the actual message size if the inner data says "0 entries", but who knows? And how many firmware versions are there? :)

So I guess you'd want to stay compatible, even if it means having a

	chan_len = min(cap->peer_chan_len, 1);

for the struct_size()?

johannes

#4 lore raw
Quoted 32 lines · Johannes Berg
Quoted 8 lines · Gustavo A. R. Silva

It seems we run into the same issue in the function below, even in the case this memset() is unnecessary (which it seems it's not):

	8920         memset(skb->data, 0, sizeof(*cmd));

Notice that if cap->peer_chan_len == 0 or cap->peer_chan_len == 1, in the original code, we have len == sizeof(*cmd) == 128:

Right.

Quoted 11 lines
drivers/net/wireless/ath/ath10k/wmi.c +1 -4
-       /* tdls peer update cmd has place holder for one channel*/
-       chan_len = cap->peer_chan_len ? (cap->peer_chan_len - 1) : 0;
-
-       len = sizeof(*cmd) + chan_len * sizeof(*chan);
+       len = struct_size(cmd, peer_capab.peer_chan_list, cap->peer_chan_len);
 
          skb = ath10k_wmi_alloc_skb(ar, len);
          if (!skb)

which makes round_len == roundup(len, 4) == struct_size(cmd,...,...) == 104 when cap->peer_chan_len == 0

And yeah, that's really the issue, it only matters for ==0. For a moment there I thought that doesn't even make sense, but it looks like it never even becomes non-zero.

No idea then, sorry. You'd hope firmware doesn't care about the actual message size if the inner data says "0 entries", but who knows? And how many firmware versions are there? :)

Quoted 5 lines

So I guess you'd want to stay compatible, even if it means having a

	chan_len = min(cap->peer_chan_len, 1);

for the struct_size()?

Yeah, that's an alternative.

I'll wait for the maintainers to chime in and see if they have a different opinion.

Thanks
--
Gustavo

#5 lore raw
Quoted 38 lines · Gustavo A. R. Silva
Quoted 35 lines · Johannes Berg
Quoted 8 lines · Gustavo A. R. Silva

It seems we run into the same issue in the function below, even in the case this memset() is unnecessary (which it seems it's not):

    8920         memset(skb->data, 0, sizeof(*cmd));

Notice that if cap->peer_chan_len == 0 or cap->peer_chan_len == 1, in the original code, we have len == sizeof(*cmd) == 128:

Right.

Quoted 7 lines

-       /* tdls peer update cmd has place holder for one channel*/ -       chan_len = cap->peer_chan_len ? (cap->peer_chan_len - 1) : 0; -
-       len = sizeof(*cmd) + chan_len * sizeof(*chan);
+       len = struct_size(cmd, peer_capab.peer_chan_list, cap->peer_chan_len);

Quoted 5 lines

          skb = ath10k_wmi_alloc_skb(ar, len);
          if (!skb)

which makes round_len == roundup(len, 4) == struct_size(cmd,...,...) == 104 when cap->peer_chan_len == 0

And yeah, that's really the issue, it only matters for ==0. For a moment there I thought that doesn't even make sense, but it looks like it never even becomes non-zero.

No idea then, sorry. You'd hope firmware doesn't care about the actual message size if the inner data says "0 entries", but who knows? And how many firmware versions are there? :)

So I guess you'd want to stay compatible, even if it means having a

Quoted 8 lines
Quoted 3 lines

    chan_len = min(cap->peer_chan_len, 1);

for the struct_size()?

Yeah, that's an alternative.

I'll wait for the maintainers to chime in and see if they have a different opinion.

I'm seeing clarification from the development team.

/jeff

#6 lore raw
Quoted 44 lines · Jeff Johnson
Quoted 43 lines · Gustavo A. R. Silva
Quoted 39 lines · Johannes Berg
Quoted 8 lines · Gustavo A. R. Silva

It seems we run into the same issue in the function below, even in the case this memset() is unnecessary (which it seems it's not):

    8920         memset(skb->data, 0, sizeof(*cmd));

Notice that if cap->peer_chan_len == 0 or cap->peer_chan_len == 1, in the original code, we have len == sizeof(*cmd) == 128:

Right.

Quoted 7 lines

-       /* tdls peer update cmd has place holder for one channel*/ -       chan_len = cap->peer_chan_len ? (cap->peer_chan_len - 1) : 0; -
-       len = sizeof(*cmd) + chan_len * sizeof(*chan);
+       len = struct_size(cmd, peer_capab.peer_chan_list, cap->peer_chan_len);

Quoted 6 lines

          skb = ath10k_wmi_alloc_skb(ar, len);
          if (!skb)

which makes round_len == roundup(len, 4) == struct_size(cmd,...,...) == 104
when cap->peer_chan_len == 0

And yeah, that's really the issue, it only matters for ==0. For a moment there I thought that doesn't even make sense, but it looks like it never even becomes non-zero.

No idea then, sorry. You'd hope firmware doesn't care about the actual message size if the inner data says "0 entries", but who knows? And how many firmware versions are there? :)

So I guess you'd want to stay compatible, even if it means having a

    chan_len = min(cap->peer_chan_len, 1);

for the struct_size()?

Quoted 9 lines
Quoted 4 lines

Yeah, that's an alternative.

I'll wait for the maintainers to chime in and see if they have a different opinion.

I'm seeing clarification from the development team.

/jeff

I was not able to get a response from the firmware team.

I have gone ahead and created a series of patches to fix the remaining flexible array issues in ath10k including the one discussed here. I should be able to post those sometime this week.

/jeff

#7 lore raw
In reply to Gustavo A. R. Silva

[+CC Manikanta Pubbisetty ]

As Johannes[1] pointed out, this memset() is probably unnecessary:

./drivers/net/wireless/ath/ath10k/wmi.c:
8920 memset(skb->data, 0, sizeof(*cmd));

However, the same exact issue[2] is present at the line below inside function ath10k_wmi_alloc_skb():

drivers/net/wireless/ath/ath10k/wmi.c:
1799 memset(skb->data, 0, round_len);

Thanks
--
Gustavo

[1] https://lore.kernel.org/linux-hardening/[email protected]/
[2] https://lore.kernel.org/linux-hardening/[email protected]/

Quoted 71 lines · Gustavo A. R. Silva

Hi all,

While working on tranforming one-element array peer_chan_list in struct wmi_tdls_peer_capabilities into a flex-array member

7187 struct wmi_tdls_peer_capabilities {
...
7199         struct wmi_channel peer_chan_list[1]; 7200 } __packed;

the following line caught my attention:

./drivers/net/wireless/ath/ath10k/wmi.c:
8920         memset(skb->data, 0, sizeof(*cmd));

Notice that before the flex-array transformation, we are zeroing 128 bytes in skb->data because sizeof(*cmd) == 128, see below:

$ pahole -C wmi_10_4_tdls_peer_update_cmd drivers/net/wireless/ath/ath10k/wmi.o
struct wmi_10_4_tdls_peer_update_cmd {
     __le32                     vdev_id;              /*     0     4 */
     struct wmi_mac_addr        peer_macaddr;         /*     4     8 */
     __le32                     peer_state;           /*    12     4 */
     __le32                     reserved[4];          /*    16    16 */
     struct wmi_tdls_peer_capabilities peer_capab;    /*    32    96 */

    /* size: 128, cachelines: 2, members: 5 */
};

So, after the flex-array transformation (and the necessary adjustments to a few other lines of code) we would be zeroing 104 bytes in skb->data because sizeof(*cmd) == 104, see below:

$ pahole -C wmi_10_4_tdls_peer_update_cmd drivers/net/wireless/ath/ath10k/wmi.o
struct wmi_10_4_tdls_peer_update_cmd {
     __le32                     vdev_id;              /*     0     4 */
     struct wmi_mac_addr        peer_macaddr;         /*     4     8 */
     __le32                     peer_state;           /*    12     4 */
     __le32                     reserved[4];          /*    16    16 */
     struct wmi_tdls_peer_capabilities peer_capab;    /*    32    72 */

    /* size: 104, cachelines: 2, members: 5 */
    /* last cacheline: 40 bytes */
};

This difference arises because the size of the element type for the peer_chan_list array, which is sizeof(struct wmi_channel) == 24

$ pahole -C wmi_channel drivers/net/wireless/ath/ath10k/wmi.o
struct wmi_channel {
     __le32                     mhz;                  /*     0     4 */
     __le32                     band_center_freq1;    /*     4     4 */
     __le32                     band_center_freq2;    /*     8     4 */

[..]
                                               /*    20     4 */

    /* size: 24, cachelines: 1, members: 6 */
    /* last cacheline: 24 bytes */
};

is included in sizeof(*cmd) before the transformation.

So, my question is: do we really need to zero out those extra 24 bytes in skb->data? or is it rather a bug in the original code?

Thanks!

Signature · 1 line
Gustavo