iio: frequency: admv1013: fix NULL pointer dereference on str
Subsystems (3)
Antoniu Miclaus
maintainerPATCH v2 1/1 iio: frequency: admv1013: fix NULL pointer dereference on str
When device_property_read_string() fails, str is left uninitialized but the code falls through to strcmp(str, ...), dereferencing a garbage pointer. Replace manual read/strcmp with device_property_match_property_string() which reads the property as a single string value and matches it against an array of known valid strings, handling the missing property case internally.
---
Changes in v2:
- use device_property_match_property_string() instead of device_property_match_string() (Andy)
- add Reviewed-by tag from Nuno
drivers/iio/frequency/admv1013.c | 41 ++++++++++++++++---------------- 1 file changed, 20 insertions(+), 21 deletions(-)
drivers/iio/frequency/admv1013.c +20 -21
@@ -512,37 +512,36 @@ static void admv1013_powerdown(void *data)admv1013_spi_update_bits (data,ADMV1013_REG_ENABLE , enable_reg_msk, enable_reg); } +static const char *const admv1013_input_modes[] = {"iq" ,"if" }; + +static const char *const admv1013_quad_se_modes[] = {"diff" ,"se-pos" ,"se-neg" }; +static int admv1013_properties_parse (struct admv1013_state *st) {int ret; -const char *str;struct device *dev = &st->spi->dev; st->det_en =device_property_read_bool (dev,"adi,detector-enable" ); - ret =device_property_read_string (dev,"adi,input-mode" , &str); -if (ret) - st->input_mode =ADMV1013_IQ_MODE ; - -if (!strcmp (str,"iq" )) - st->input_mode =ADMV1013_IQ_MODE ; -else if (!strcmp (str,"if" )) - st->input_mode =ADMV1013_IF_MODE ; -else -return -EINVAL ; - - ret =device_property_read_string (dev,"adi,quad-se-mode" , &str); -if (ret) - st->quad_se_mode =ADMV1013_SE_MODE_DIFF ; + ret =device_property_match_property_string (dev,"adi,input-mode" , + admv1013_input_modes, +ARRAY_SIZE (admv1013_input_modes)); + st->input_mode = ret >=0 ? ret :ADMV1013_IQ_MODE ; -if (!strcmp (str,"diff" )) - st->quad_se_mode =ADMV1013_SE_MODE_DIFF ; -else if (!strcmp (str,"se-pos" )) + ret =device_property_match_property_string (dev,"adi,quad-se-mode" , + admv1013_quad_se_modes, +ARRAY_SIZE (admv1013_quad_se_modes)); +switch (ret) { +case 1 : st->quad_se_mode =ADMV1013_SE_MODE_POS ; -else if (!strcmp (str,"se-neg" )) +break ; +case 2 : st->quad_se_mode =ADMV1013_SE_MODE_NEG ; -else -return -EINVAL ; +break ; +default : + st->quad_se_mode =ADMV1013_SE_MODE_DIFF ; +break ; + } ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(admv1013_vcc_regs),
Signature · 1 line
2.43.0
Quoted 6 lines · Antoniu Miclaus
When
device_property_read_string()fails, str is left uninitialized but the code falls through tostrcmp(str, ...), dereferencing a garbage pointer. Replace manual read/strcmp withdevice_property_match_property_string()which reads the property as a single string value and matches it against an array of known valid strings, handling the missing property case internally.
...
Quoted 1 line
+static const char * const admv1013_input_modes[] = { "iq", "if" };
This array has to be indexed.
static const char * const admv1013_input_modes[] = {
[ADMV1013_IQ_MODE] = "iq",
[ADMV1013_IF_MODE] = "if",
};...
Quoted 1 line
+static const char * const admv1013_quad_se_modes[] = { "diff", "se-pos", "se-neg" };
Also this one.
Taking into account the indices are not sequential, this may require another enumerator.
Ideally you need to list all possible modes and choose only supported by assigning an empty string to unsupported ones.
I haven't checked datasheet to understand why only 6, 9, 12 are in use. Maybe they can be simply 1, 2, 3 with a formula like 3 + x * 3 ? Dunno.
...
drivers/iio/frequency/admv1013.c static void admv1013_powerdown(void *data) · line 540
Quoted 8 lines
drivers/iio/frequency/admv1013.c +5
-1+switch (ret) { +case 1 : st->quad_se_mode =ADMV1013_SE_MODE_POS ; - else if (!strcmp(str, "se-neg")) +break ; +case 2 : st->quad_se_mode =ADMV1013_SE_MODE_NEG ; +break ;
This (the default) should take
case 0:
as well
Quoted 4 lines
drivers/iio/frequency/admv1013.c +4
-0+default : + st->quad_se_mode =ADMV1013_SE_MODE_DIFF ; +break ; + }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Miclaus, Antoniu
maintainerQuoted 42 lines
-----Original Message-----
From: Andy Shevchenko <[email protected]>
Sent: Tuesday, March 3, 2026 1:34 PM
To: Miclaus, Antoniu <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Jonathan Cameron <[email protected]>; David Lechner <[email protected]>; Sa, Nuno <[email protected]>; Andy Shevchenko <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH v2] iio: frequency: admv1013: fix NULL pointer dereference on str[External]
Quoted 6 lines · Antoniu Miclaus
When
device_property_read_string()fails, str is left uninitialized but the code falls through tostrcmp(str, ...), dereferencing a garbage pointer. Replace manual read/strcmp withdevice_property_match_property_string()which reads the property as a single string value and matches it against an array of known valid strings, handling the missing property case internally....
Quoted 1 line
+static const char * const admv1013_input_modes[] = { "iq", "if" };
This array has to be indexed.
static const char * const admv1013_input_modes[] = { [ADMV1013_IQ_MODE] = "iq", [ADMV1013_IF_MODE] = "if", };...
Quoted 1 line
+static const char * const admv1013_quad_se_modes[] = { "diff", "se-pos",
"se-neg" };
Also this one.
Taking into account the indices are not sequential, this may require another enumerator.
Quoted 6 lines
Ideally you need to list all possible modes and choose only supported by assigning an empty string to unsupported ones.
I haven't checked datasheet to understand why only 6, 9, 12 are in use. Maybe they can be simply 1, 2, 3 with a formula like 3 + x * 3 ? Dunno.
static const char * const admv1013_input_modes[] = {
[ADMV1013_IQ_MODE] = "iq",
[ADMV1013_IF_MODE] = "if",
};static const char * const admv1013_quad_se_modes[] = {
[ADMV1013_QUAD_SE_DIFF] = "diff",
[ADMV1013_QUAD_SE_POS] = "se-pos",
[ADMV1013_QUAD_SE_NEG] = "se-neg",
};static const unsigned int admv1013_quad_se_regvals[] = {
[ADMV1013_QUAD_SE_DIFF] = ADMV1013_SE_MODE_DIFF,
[ADMV1013_QUAD_SE_POS] = ADMV1013_SE_MODE_POS,
[ADMV1013_QUAD_SE_NEG] = ADMV1013_SE_MODE_NEG,
};Does this make sense?
Quoted 26 lines
...
drivers/iio/frequency/admv1013.c static void admv1013_powerdown(void *data) · line 540
Quoted 8 lines
drivers/iio/frequency/admv1013.c +5
-1+switch (ret) { +case 1 : st->quad_se_mode =ADMV1013_SE_MODE_POS ; - else if (!strcmp(str, "se-neg")) +break ; +case 2 : st->quad_se_mode =ADMV1013_SE_MODE_NEG ; +break ;This (the default) should take
case 0:as well
drivers/iio/frequency/admv1013.c static void admv1013_powerdown(void *data) · line 544
Quoted 4 lines
drivers/iio/frequency/admv1013.c +4
-0+default : + st->quad_se_mode =ADMV1013_SE_MODE_DIFF ; +break ; + }--
With Best Regards,
Andy Shevchenko
Quoted 4 lines · Antoniu
Quoted 4 lines
-----Original Message-----
From: Andy Shevchenko <[email protected]>
Sent: Tuesday, March 3, 2026 1:34 PM
On Tue, Mar 03, 2026 at 11:52:28AM +0200, Antoniu Miclaus wrote:
...
Quoted 24 lines
Quoted 12 lines
Quoted 1 line
+static const char * const admv1013_quad_se_modes[] = { "diff", "se-pos",
"se-neg" };
Taking into account the indices are not sequential, this may require another enumerator.
Ideally you need to list all possible modes and choose only supported by assigning an empty string to unsupported ones.
I haven't checked datasheet to understand why only 6, 9, 12 are in use. Maybe they can be simply 1, 2, 3 with a formula like 3 + x * 3 ? Dunno.
static const char * const admv1013_input_modes[] = { [ADMV1013_IQ_MODE] = "iq", [ADMV1013_IF_MODE] = "if", };static const char * const admv1013_quad_se_modes[] = { [ADMV1013_QUAD_SE_DIFF] = "diff", [ADMV1013_QUAD_SE_POS] = "se-pos", [ADMV1013_QUAD_SE_NEG] = "se-neg", };
Quoted 7 lines
static const unsigned int admv1013_quad_se_regvals[] = { [ADMV1013_QUAD_SE_DIFF] = ADMV1013_SE_MODE_DIFF, [ADMV1013_QUAD_SE_POS] = ADMV1013_SE_MODE_POS, [ADMV1013_QUAD_SE_NEG] = ADMV1013_SE_MODE_NEG, };Does this make sense?
Yes, if there is an explanation why we have 6,9,12 to begin with. Can somebody study the available datasheets and come up with the explanations?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Miclaus, Antoniu
maintainerQuoted 53 lines
-----Original Message-----
From: Andy Shevchenko <[email protected]>
Sent: Tuesday, March 3, 2026 3:55 PM
To: Miclaus, Antoniu <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Jonathan Cameron <[email protected]>; David Lechner <[email protected]>; Sa, Nuno <[email protected]>; Andy Shevchenko <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH v2] iio: frequency: admv1013: fix NULL pointer dereference on str[External]
Quoted 4 lines · Antoniu
Quoted 4 lines
-----Original Message-----
From: Andy Shevchenko <[email protected]>
Sent: Tuesday, March 3, 2026 1:34 PM
On Tue, Mar 03, 2026 at 11:52:28AM +0200, Antoniu Miclaus wrote:...
Quoted 1 line
Quoted 1 line
Quoted 1 line
+static const char * const admv1013_quad_se_modes[] = { "diff", "se-
pos",
Quoted 29 lines
Quoted 11 lines
"se-neg" };
Taking into account the indices are not sequential, this may require another enumerator.
Ideally you need to list all possible modes and choose only supported by assigning an empty string to unsupported ones.
I haven't checked datasheet to understand why only 6, 9, 12 are in use. Maybe they can be simply 1, 2, 3 with a formula like 3 + x * 3 ? Dunno.
static const char * const admv1013_input_modes[] = { [ADMV1013_IQ_MODE] = "iq", [ADMV1013_IF_MODE] = "if", };static const char * const admv1013_quad_se_modes[] = { [ADMV1013_QUAD_SE_DIFF] = "diff", [ADMV1013_QUAD_SE_POS] = "se-pos", [ADMV1013_QUAD_SE_NEG] = "se-neg", };static const unsigned int admv1013_quad_se_regvals[] = { [ADMV1013_QUAD_SE_DIFF] = ADMV1013_SE_MODE_DIFF, [ADMV1013_QUAD_SE_POS] = ADMV1013_SE_MODE_POS, [ADMV1013_QUAD_SE_NEG] = ADMV1013_SE_MODE_NEG, };
Quoted 5 lines
Quoted 1 line
Does this make sense?
Yes, if there is an explanation why we have 6,9,12 to begin with. Can somebody
study the available datasheets and come up with the explanations?
There are the values accepted by the QUAD_SE_MODE bitfield.
https://www.analog.com/media/en/technical-documentation/data-sheets/admv1013.pdf
Page 37 of 39, Table 15.
Quoted 5 lines
--
With Best Regards,
Andy Shevchenko
Quoted 6 lines · Antoniu
Quoted 6 lines
From: Andy Shevchenko <[email protected]>
Sent: Tuesday, March 3, 2026 3:55 PMQuoted 3 lines · Antoniu
Quoted 3 lines
From: Andy Shevchenko <[email protected]>
Sent: Tuesday, March 3, 2026 1:34 PM
On Tue, Mar 03, 2026 at 11:52:28AM +0200, Antoniu Miclaus wrote:
...
Quoted 33 lines
Quoted 33 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
+static const char * const admv1013_quad_se_modes[] = { "diff", "se-
pos",
Quoted 30 lines
Quoted 11 lines
"se-neg" };
Taking into account the indices are not sequential, this may require another enumerator.
Ideally you need to list all possible modes and choose only supported by assigning an empty string to unsupported ones.
I haven't checked datasheet to understand why only 6, 9, 12 are in use. Maybe they can be simply 1, 2, 3 with a formula like 3 + x * 3 ? Dunno.
static const char * const admv1013_input_modes[] = { [ADMV1013_IQ_MODE] = "iq", [ADMV1013_IF_MODE] = "if", };static const char * const admv1013_quad_se_modes[] = { [ADMV1013_QUAD_SE_DIFF] = "diff", [ADMV1013_QUAD_SE_POS] = "se-pos", [ADMV1013_QUAD_SE_NEG] = "se-neg", };static const unsigned int admv1013_quad_se_regvals[] = { [ADMV1013_QUAD_SE_DIFF] = ADMV1013_SE_MODE_DIFF, [ADMV1013_QUAD_SE_POS] = ADMV1013_SE_MODE_POS, [ADMV1013_QUAD_SE_NEG] = ADMV1013_SE_MODE_NEG, };Does this make sense?
Quoted 8 lines
Quoted 3 lines
Yes, if there is an explanation why we have 6,9,12 to begin with. Can somebody
study the available datasheets and come up with the explanations?There are the values accepted by the
QUAD_SE_MODEbitfield.https://www.analog.com/media/en/technical-documentation/data-sheets/admv1013.pdf
Page 37 of 39, Table 15.
Thanks!
So, looking at the code and the datasheet I think we need to do the following.
Apply the rule that each HW related enum has to be defined with explicit value. But looking at the code, I think we need to do exactly the opposite, id est treat all enums as Linux driver ones. With this
1. Drop values from the given enum
enum {
ADMV1013_SE_MODE_POS,
ADMV1013_SE_MODE_NEG,
ADMV1013_SE_MODE_DIFF,
};2. Use it directly as indices for string arrays.
3. Use plain numbers in the mapping switch when assigning the values in the similar way like it's done for other registers in other functions, exempli gratia quad filters.
This will make consistent code among different bitfields used in the driver and makes easier to use string arrays without double enums, et cetera.
Signature · 2 lines
With Best Regards, Andy Shevchenko