Skip to contentSkip to conversation

ASoC: Intel: Skylake: Replace 1-element array with flex-array

PATCH In review Kees Cook 3 participants 5 messages Feb 10, 2023 – Feb 13, 2023

Subsystems (3)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 ASoC: Intel: Skylake: Replace 1-element array with flex-array

The kernel is globally removing the ambiguous 0-length and 1-element arrays in favor of flexible arrays, so that we can gain both compile-time and run-time array bounds checking[1]. In this instance, struct skl_cpr_cfg contains struct skl_cpr_gtw_cfg, which defined "config_data" as a 1-element array.

Normally when switching from a 1-element array to a flex-array, any related size calculations must be adjusted too. However, it seems the original code was over-allocating space, since 1 extra u32 would be included by the sizeof():

                param_size = sizeof(struct skl_cpr_cfg);
                param_size += mconfig->formats_config[SKL_PARAM_INIT].caps_size;

But the copy uses caps_size bytes, and cap_size / 4 (i.e. sizeof(u32)) for the length tracking:

        memcpy(cpr_mconfig->gtw_cfg.config_data,
                        mconfig->formats_config[SKL_PARAM_INIT].caps,
                        mconfig->formats_config[SKL_PARAM_INIT].caps_size);

        cpr_mconfig->gtw_cfg.config_length =
                        (mconfig->formats_config[SKL_PARAM_INIT].caps_size) / 4;

Therefore, no size calculations need adjusting. Change the struct skl_cpr_gtw_cfg config_data member to be a true flexible array, which also fixes the over-allocation, and silences this memcpy run-time false positive:

memcpy: detected field-spanning write (size 100) of single field "cpr_mconfig->gtw_cfg.config_data" at sound/soc/intel/skylake/skl-messages.c:554 (size 4)

[1] For lots of details, see both:
https://docs.kernel.org/process/deprecated.html#zero-length-and-one-element-arrays
https://people.kernel.org/kees/bounded-flexible-arrays-in-c

Cc Cezary Rojewski <[email protected]>
Cc Pierre-Louis Bossart <[email protected]>
Cc Liam Girdwood <[email protected]>
Cc Peter Ujfalusi <[email protected]>
Cc Bard Liao <[email protected]>
Cc Ranjani Sridharan <[email protected]>
Cc Kai Vehmanen <[email protected]>
Cc Mark Brown <[email protected]>
Cc Jaroslav Kysela <[email protected]>
Cc Takashi Iwai <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc "Amadeusz Sławiński" <[email protected]>
---
 sound/soc/intel/skylake/skl-topology.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/intel/skylake/skl-topology.h +1 -1
index 6db0fd7bad49..ad94f8020c27 100644
@@ -115,7 +115,7 @@ struct skl_cpr_gtw_cfg {
 	u32 dma_buffer_size;
 	u32 config_length;
 	/* not mandatory; required only for DMIC/I2S */
-	u32 config_data[1];
+	u32 config_data[];
 } __packed;
 
 struct skl_dma_control {
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 29 lines · Kees Cook

The kernel is globally removing the ambiguous 0-length and 1-element arrays in favor of flexible arrays, so that we can gain both compile-time and run-time array bounds checking[1]. In this instance, struct skl_cpr_cfg contains struct skl_cpr_gtw_cfg, which defined "config_data" as a 1-element array.

Normally when switching from a 1-element array to a flex-array, any related size calculations must be adjusted too. However, it seems the original code was over-allocating space, since 1 extra u32 would be included by the sizeof():

                param_size = sizeof(struct skl_cpr_cfg);
                param_size += mconfig->formats_config[SKL_PARAM_INIT].caps_size;

But the copy uses caps_size bytes, and cap_size / 4 (i.e. sizeof(u32)) for the length tracking:

        memcpy(cpr_mconfig->gtw_cfg.config_data,
                        mconfig->formats_config[SKL_PARAM_INIT].caps,
                        mconfig->formats_config[SKL_PARAM_INIT].caps_size);

        cpr_mconfig->gtw_cfg.config_length =
                        (mconfig->formats_config[SKL_PARAM_INIT].caps_size) / 4;

Therefore, no size calculations need adjusting. Change the struct skl_cpr_gtw_cfg config_data member to be a true flexible array, which also fixes the over-allocation, and silences this memcpy run-time false positive:

Quoted 5 lines

memcpy: detected field-spanning write (size 100) of single field "cpr_mconfig->gtw_cfg.config_data" at sound/soc/intel/skylake/skl-messages.c:554 (size 4)

[1] For lots of details, see both:
https://docs.kernel.org/process/deprecated.html#zero-length-and-one-element-arrays
https://people.kernel.org/kees/bounded-flexible-arrays-in-c

Quoted 34 lines
Cc Cezary Rojewski <[email protected]>
Cc Pierre-Louis Bossart <[email protected]>
Cc Liam Girdwood <[email protected]>
Cc Peter Ujfalusi <[email protected]>
Cc Bard Liao <[email protected]>
Cc Ranjani Sridharan <[email protected]>
Cc Kai Vehmanen <[email protected]>
Cc Mark Brown <[email protected]>
Cc Jaroslav Kysela <[email protected]>
Cc Takashi Iwai <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc "Amadeusz Sławiński" <[email protected]>
---
 sound/soc/intel/skylake/skl-topology.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/intel/skylake/skl-topology.h +1 -1
index 6db0fd7bad49..ad94f8020c27 100644
@@ -115,7 +115,7 @@ struct skl_cpr_gtw_cfg {
 	u32 dma_buffer_size;
 	u32 config_length;
 	/* not mandatory; required only for DMIC/I2S */
-	u32 config_data[1];
+	u32 config_data[];
 } __packed;
 
 struct skl_dma_control {
Signature · 1 line
Péter
#2 lore raw
In reply to Kees Cook
Quoted 60 lines · Kees Cook

The kernel is globally removing the ambiguous 0-length and 1-element arrays in favor of flexible arrays, so that we can gain both compile-time and run-time array bounds checking[1]. In this instance, struct skl_cpr_cfg contains struct skl_cpr_gtw_cfg, which defined "config_data" as a 1-element array.

Normally when switching from a 1-element array to a flex-array, any related size calculations must be adjusted too. However, it seems the original code was over-allocating space, since 1 extra u32 would be included by the sizeof():

                 param_size = sizeof(struct skl_cpr_cfg);
                 param_size += mconfig->formats_config[SKL_PARAM_INIT].caps_size;

But the copy uses caps_size bytes, and cap_size / 4 (i.e. sizeof(u32)) for the length tracking:

         memcpy(cpr_mconfig->gtw_cfg.config_data,
                         mconfig->formats_config[SKL_PARAM_INIT].caps,
                         mconfig->formats_config[SKL_PARAM_INIT].caps_size);

         cpr_mconfig->gtw_cfg.config_length =
                         (mconfig->formats_config[SKL_PARAM_INIT].caps_size) / 4;

Therefore, no size calculations need adjusting. Change the struct skl_cpr_gtw_cfg config_data member to be a true flexible array, which also fixes the over-allocation, and silences this memcpy run-time false positive:

memcpy: detected field-spanning write (size 100) of single field "cpr_mconfig->gtw_cfg.config_data" at sound/soc/intel/skylake/skl-messages.c:554 (size 4)

[1] For lots of details, see both:
https://docs.kernel.org/process/deprecated.html#zero-length-and-one-element-arrays
https://people.kernel.org/kees/bounded-flexible-arrays-in-c

Cc Cezary Rojewski <[email protected]>
Cc Pierre-Louis Bossart <[email protected]>
Cc Liam Girdwood <[email protected]>
Cc Peter Ujfalusi <[email protected]>
Cc Bard Liao <[email protected]>
Cc Ranjani Sridharan <[email protected]>
Cc Kai Vehmanen <[email protected]>
Cc Mark Brown <[email protected]>
Cc Jaroslav Kysela <[email protected]>
Cc Takashi Iwai <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc "Amadeusz Sławiński" <[email protected]>
---
  sound/soc/intel/skylake/skl-topology.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/intel/skylake/skl-topology.h +0 -0
index 6db0fd7bad49..ad94f8020c27 100644

sound/soc/intel/skylake/skl-topology.h struct skl_cpr_gtw_cfg { · line 121

Quoted 8 lines
sound/soc/intel/skylake/skl-topology.h +1 -1
@@ -115,7 +115,7 @@ struct skl_cpr_gtw_cfg {
  	u32 dma_buffer_size;
  	u32 config_length;
  	/* not mandatory; required only for DMIC/I2S */
-	u32 config_data[1];
+	u32 config_data[];
  } __packed;
 
  struct skl_dma_control {

This fails in our validation. Maybe we can use the union workaround, to leave the size as is?

Following seems to work in manual test:

sound/soc/intel/skylake/skl-topology.h +4 -1
index 6db0fd7bad49..ffbd2e60fede 100644
@@ -115,7 +115,10 @@ struct skl_cpr_gtw_cfg {
        u32 dma_buffer_size;
        u32 config_length;
        /* not mandatory; required only for DMIC/I2S */
-       u32 config_data[1];
+       union {
+               u32 x;
+               u32 config_data[0];
+       };
 } __packed;
 
 struct skl_dma_control {

I can also run it through validation to make sure if it is acceptable.

author
#3 lore raw
Quoted 56 lines · Amadeusz Sławiński
Quoted 55 lines · Kees Cook

The kernel is globally removing the ambiguous 0-length and 1-element arrays in favor of flexible arrays, so that we can gain both compile-time and run-time array bounds checking[1]. In this instance, struct skl_cpr_cfg contains struct skl_cpr_gtw_cfg, which defined "config_data" as a 1-element array.

Normally when switching from a 1-element array to a flex-array, any related size calculations must be adjusted too. However, it seems the original code was over-allocating space, since 1 extra u32 would be included by the sizeof():

                 param_size = sizeof(struct skl_cpr_cfg);
                 param_size += mconfig->formats_config[SKL_PARAM_INIT].caps_size;

But the copy uses caps_size bytes, and cap_size / 4 (i.e. sizeof(u32)) for the length tracking:

         memcpy(cpr_mconfig->gtw_cfg.config_data,
                         mconfig->formats_config[SKL_PARAM_INIT].caps,
                         mconfig->formats_config[SKL_PARAM_INIT].caps_size);

         cpr_mconfig->gtw_cfg.config_length =
                         (mconfig->formats_config[SKL_PARAM_INIT].caps_size) / 4;

Therefore, no size calculations need adjusting. Change the struct skl_cpr_gtw_cfg config_data member to be a true flexible array, which also fixes the over-allocation, and silences this memcpy run-time false positive:

memcpy: detected field-spanning write (size 100) of single field "cpr_mconfig->gtw_cfg.config_data" at sound/soc/intel/skylake/skl-messages.c:554 (size 4)

[1] For lots of details, see both:
https://docs.kernel.org/process/deprecated.html#zero-length-and-one-element-arrays
https://people.kernel.org/kees/bounded-flexible-arrays-in-c

Cc Cezary Rojewski <[email protected]>
Cc Pierre-Louis Bossart <[email protected]>
Cc Liam Girdwood <[email protected]>
Cc Peter Ujfalusi <[email protected]>
Cc Bard Liao <[email protected]>
Cc Ranjani Sridharan <[email protected]>
Cc Kai Vehmanen <[email protected]>
Cc Mark Brown <[email protected]>
Cc Jaroslav Kysela <[email protected]>
Cc Takashi Iwai <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc "Amadeusz Sławiński" <[email protected]>
---
  sound/soc/intel/skylake/skl-topology.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

sound/soc/intel/skylake/skl-topology.h struct skl_cpr_gtw_cfg { · line 120

Quoted 12 lines
sound/soc/intel/skylake/skl-topology.h +1 -1
index 6db0fd7bad49..ad94f8020c27 100644
@@ -115,7 +115,7 @@ struct skl_cpr_gtw_cfg {
  	u32 dma_buffer_size;
  	u32 config_length;
  	/* not mandatory; required only for DMIC/I2S */
-	u32 config_data[1];
+	u32 config_data[];
  } __packed;
  struct skl_dma_control {

This fails in our validation.

Ah, okay. Thanks for checking!

Quoted 10 lines

Maybe we can use the union workaround, to
leave the size as is?

Following seems to work in manual test:

sound/soc/intel/skylake/skl-topology.h +0 -0
index 6db0fd7bad49..ffbd2e60fede 100644

sound/soc/intel/skylake/skl-topology.h struct skl_cpr_gtw_cfg { · line 121

Quoted 8 lines
sound/soc/intel/skylake/skl-topology.h +4 -1
@@ -115,7 +115,10 @@ struct skl_cpr_gtw_cfg {
        u32 dma_buffer_size;
        u32 config_length;
        /* not mandatory; required only for DMIC/I2S */
-       u32 config_data[1];
+       union {
+               u32 x;
+               u32 config_data[0];
+       };

Yeah, that could work, though the last member would be:

	DECLARE_FLEX_ARRAY(u32, config_data);

otherwise the array is 0 length (rather than a proper flex array).

But before that, let me see if I can track down where the size is being used, in case we can avoid adding the padding.

Signature · 1 line
Kees Cook
#4 lore raw
Quoted 94 lines · Kees Cook
Quoted 56 lines · Amadeusz Sławiński
Quoted 55 lines · Kees Cook

The kernel is globally removing the ambiguous 0-length and 1-element arrays in favor of flexible arrays, so that we can gain both compile-time and run-time array bounds checking[1]. In this instance, struct skl_cpr_cfg contains struct skl_cpr_gtw_cfg, which defined "config_data" as a 1-element array.

Normally when switching from a 1-element array to a flex-array, any related size calculations must be adjusted too. However, it seems the original code was over-allocating space, since 1 extra u32 would be included by the sizeof():

                  param_size = sizeof(struct skl_cpr_cfg);
                  param_size += mconfig->formats_config[SKL_PARAM_INIT].caps_size;

But the copy uses caps_size bytes, and cap_size / 4 (i.e. sizeof(u32)) for the length tracking:

          memcpy(cpr_mconfig->gtw_cfg.config_data,
                          mconfig->formats_config[SKL_PARAM_INIT].caps,
                          mconfig->formats_config[SKL_PARAM_INIT].caps_size);

          cpr_mconfig->gtw_cfg.config_length =
                          (mconfig->formats_config[SKL_PARAM_INIT].caps_size) / 4;

Therefore, no size calculations need adjusting. Change the struct skl_cpr_gtw_cfg config_data member to be a true flexible array, which also fixes the over-allocation, and silences this memcpy run-time false positive:

    memcpy: detected field-spanning write (size 100) of single field "cpr_mconfig->gtw_cfg.config_data" at sound/soc/intel/skylake/skl-messages.c:554 (size 4)

[1] For lots of details, see both:
https://docs.kernel.org/process/deprecated.html#zero-length-and-one-element-arrays
https://people.kernel.org/kees/bounded-flexible-arrays-in-c

Cc Cezary Rojewski <[email protected]>
Cc Pierre-Louis Bossart <[email protected]>
Cc Liam Girdwood <[email protected]>
Cc Peter Ujfalusi <[email protected]>
Cc Bard Liao <[email protected]>
Cc Ranjani Sridharan <[email protected]>
Cc Kai Vehmanen <[email protected]>
Cc Mark Brown <[email protected]>
Cc Jaroslav Kysela <[email protected]>
Cc Takashi Iwai <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc "Amadeusz Sławiński" <[email protected]>
---
   sound/soc/intel/skylake/skl-topology.h | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

sound/soc/intel/skylake/skl-topology.h struct skl_cpr_gtw_cfg { · line 120

Quoted 12 lines
sound/soc/intel/skylake/skl-topology.h +1 -1
index 6db0fd7bad49..ad94f8020c27 100644
@@ -115,7 +115,7 @@ struct skl_cpr_gtw_cfg {
   	u32 dma_buffer_size;
   	u32 config_length;
   	/* not mandatory; required only for DMIC/I2S */
-	u32 config_data[1];
+	u32 config_data[];
   } __packed;
   struct skl_dma_control {

This fails in our validation.

Ah, okay. Thanks for checking!

Quoted 10 lines

Maybe we can use the union workaround, to
leave the size as is?

Following seems to work in manual test:

sound/soc/intel/skylake/skl-topology.h +0 -0
index 6db0fd7bad49..ffbd2e60fede 100644

sound/soc/intel/skylake/skl-topology.h struct skl_cpr_gtw_cfg { · line 121

Quoted 8 lines
sound/soc/intel/skylake/skl-topology.h +4 -1
@@ -115,7 +115,10 @@ struct skl_cpr_gtw_cfg {
         u32 dma_buffer_size;
         u32 config_length;
         /* not mandatory; required only for DMIC/I2S */
-       u32 config_data[1];
+       union {
+               u32 x;
+               u32 config_data[0];
+       };

Yeah, that could work, though the last member would be:

Quoted 6 lines
	DECLARE_FLEX_ARRAY(u32, config_data);

otherwise the array is 0 length (rather than a proper flex array).

But before that, let me see if I can track down where the size is being used, in case we can avoid adding the padding.

I did spend some more time on this, apparently struct was missing one field according to IPC protocol. I've send fix for this.