Skip to content

Commit 17e4321

Browse files
committed
OPP: add index check to assert to avoid buffer overflow in _read_freq()
JIRA: https://issues.redhat.com/browse/RHEL-81432 CVE: CVE-2024-57998 commit d659bc6 Author: Neil Armstrong <[email protected]> Date: Tue Dec 3 09:12:59 2024 +0100 OPP: add index check to assert to avoid buffer overflow in _read_freq() Pass the freq index to the assert function to make sure we do not read a freq out of the opp->rates[] table when called from the indexed variants: dev_pm_opp_find_freq_exact_indexed() or dev_pm_opp_find_freq_ceil/floor_indexed(). Add a secondary parameter to the assert function, unused for assert_single_clk() then add assert_clk_index() which will check for the clock index when called from the _indexed() find functions. Fixes: 142e17c ("OPP: Introduce dev_pm_opp_find_freq_{ceil/floor}_indexed() APIs") Fixes: a589392 ("OPP: Add dev_pm_opp_find_freq_exact_indexed()") Signed-off-by: Neil Armstrong <[email protected]> Signed-off-by: Viresh Kumar <[email protected]> Signed-off-by: Jared Kangas <[email protected]>
1 parent 15d6248 commit 17e4321

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

drivers/opp/core.c

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,21 @@ struct opp_table *_find_opp_table(struct device *dev)
101101
* representation in the OPP table and manage the clock configuration themselves
102102
* in an platform specific way.
103103
*/
104-
static bool assert_single_clk(struct opp_table *opp_table)
104+
static bool assert_single_clk(struct opp_table *opp_table,
105+
unsigned int __always_unused index)
105106
{
106107
return !WARN_ON(opp_table->clk_count > 1);
107108
}
108109

110+
/*
111+
* Returns true if clock table is large enough to contain the clock index.
112+
*/
113+
static bool assert_clk_index(struct opp_table *opp_table,
114+
unsigned int index)
115+
{
116+
return opp_table->clk_count > index;
117+
}
118+
109119
/**
110120
* dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
111121
* @opp: opp for which voltage has to be returned for
@@ -499,12 +509,12 @@ static struct dev_pm_opp *_opp_table_find_key(struct opp_table *opp_table,
499509
unsigned long (*read)(struct dev_pm_opp *opp, int index),
500510
bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
501511
unsigned long opp_key, unsigned long key),
502-
bool (*assert)(struct opp_table *opp_table))
512+
bool (*assert)(struct opp_table *opp_table, unsigned int index))
503513
{
504514
struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
505515

506516
/* Assert that the requirement is met */
507-
if (assert && !assert(opp_table))
517+
if (assert && !assert(opp_table, index))
508518
return ERR_PTR(-EINVAL);
509519

510520
mutex_lock(&opp_table->lock);
@@ -532,7 +542,7 @@ _find_key(struct device *dev, unsigned long *key, int index, bool available,
532542
unsigned long (*read)(struct dev_pm_opp *opp, int index),
533543
bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
534544
unsigned long opp_key, unsigned long key),
535-
bool (*assert)(struct opp_table *opp_table))
545+
bool (*assert)(struct opp_table *opp_table, unsigned int index))
536546
{
537547
struct opp_table *opp_table;
538548
struct dev_pm_opp *opp;
@@ -555,7 +565,7 @@ _find_key(struct device *dev, unsigned long *key, int index, bool available,
555565
static struct dev_pm_opp *_find_key_exact(struct device *dev,
556566
unsigned long key, int index, bool available,
557567
unsigned long (*read)(struct dev_pm_opp *opp, int index),
558-
bool (*assert)(struct opp_table *opp_table))
568+
bool (*assert)(struct opp_table *opp_table, unsigned int index))
559569
{
560570
/*
561571
* The value of key will be updated here, but will be ignored as the
@@ -568,7 +578,7 @@ static struct dev_pm_opp *_find_key_exact(struct device *dev,
568578
static struct dev_pm_opp *_opp_table_find_key_ceil(struct opp_table *opp_table,
569579
unsigned long *key, int index, bool available,
570580
unsigned long (*read)(struct dev_pm_opp *opp, int index),
571-
bool (*assert)(struct opp_table *opp_table))
581+
bool (*assert)(struct opp_table *opp_table, unsigned int index))
572582
{
573583
return _opp_table_find_key(opp_table, key, index, available, read,
574584
_compare_ceil, assert);
@@ -577,7 +587,7 @@ static struct dev_pm_opp *_opp_table_find_key_ceil(struct opp_table *opp_table,
577587
static struct dev_pm_opp *_find_key_ceil(struct device *dev, unsigned long *key,
578588
int index, bool available,
579589
unsigned long (*read)(struct dev_pm_opp *opp, int index),
580-
bool (*assert)(struct opp_table *opp_table))
590+
bool (*assert)(struct opp_table *opp_table, unsigned int index))
581591
{
582592
return _find_key(dev, key, index, available, read, _compare_ceil,
583593
assert);
@@ -586,7 +596,7 @@ static struct dev_pm_opp *_find_key_ceil(struct device *dev, unsigned long *key,
586596
static struct dev_pm_opp *_find_key_floor(struct device *dev,
587597
unsigned long *key, int index, bool available,
588598
unsigned long (*read)(struct dev_pm_opp *opp, int index),
589-
bool (*assert)(struct opp_table *opp_table))
599+
bool (*assert)(struct opp_table *opp_table, unsigned int index))
590600
{
591601
return _find_key(dev, key, index, available, read, _compare_floor,
592602
assert);
@@ -647,7 +657,8 @@ struct dev_pm_opp *
647657
dev_pm_opp_find_freq_exact_indexed(struct device *dev, unsigned long freq,
648658
u32 index, bool available)
649659
{
650-
return _find_key_exact(dev, freq, index, available, _read_freq, NULL);
660+
return _find_key_exact(dev, freq, index, available, _read_freq,
661+
assert_clk_index);
651662
}
652663
EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact_indexed);
653664

@@ -707,7 +718,8 @@ struct dev_pm_opp *
707718
dev_pm_opp_find_freq_ceil_indexed(struct device *dev, unsigned long *freq,
708719
u32 index)
709720
{
710-
return _find_key_ceil(dev, freq, index, true, _read_freq, NULL);
721+
return _find_key_ceil(dev, freq, index, true, _read_freq,
722+
assert_clk_index);
711723
}
712724
EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_indexed);
713725

@@ -760,7 +772,7 @@ struct dev_pm_opp *
760772
dev_pm_opp_find_freq_floor_indexed(struct device *dev, unsigned long *freq,
761773
u32 index)
762774
{
763-
return _find_key_floor(dev, freq, index, true, _read_freq, NULL);
775+
return _find_key_floor(dev, freq, index, true, _read_freq, assert_clk_index);
764776
}
765777
EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor_indexed);
766778

@@ -1703,7 +1715,7 @@ void dev_pm_opp_remove(struct device *dev, unsigned long freq)
17031715
if (IS_ERR(opp_table))
17041716
return;
17051717

1706-
if (!assert_single_clk(opp_table))
1718+
if (!assert_single_clk(opp_table, 0))
17071719
goto put_table;
17081720

17091721
mutex_lock(&opp_table->lock);
@@ -2055,7 +2067,7 @@ int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
20552067
unsigned long tol, u_volt = data->u_volt;
20562068
int ret;
20572069

2058-
if (!assert_single_clk(opp_table))
2070+
if (!assert_single_clk(opp_table, 0))
20592071
return -EINVAL;
20602072

20612073
new_opp = _opp_allocate(opp_table);
@@ -2922,7 +2934,7 @@ static int _opp_set_availability(struct device *dev, unsigned long freq,
29222934
return r;
29232935
}
29242936

2925-
if (!assert_single_clk(opp_table)) {
2937+
if (!assert_single_clk(opp_table, 0)) {
29262938
r = -EINVAL;
29272939
goto put_table;
29282940
}
@@ -2998,7 +3010,7 @@ int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
29983010
return r;
29993011
}
30003012

3001-
if (!assert_single_clk(opp_table)) {
3013+
if (!assert_single_clk(opp_table, 0)) {
30023014
r = -EINVAL;
30033015
goto put_table;
30043016
}

0 commit comments

Comments
 (0)