Skip to content

Commit c69f271

Browse files
keesalexdeucher
authored andcommitted
drm/radeon: Avoid power table parsing memory leaks
Avoid leaving a hanging pre-allocated clock_info if last mode is invalid, and avoid heap corruption if no valid modes are found. Bug: https://bugzilla.kernel.org/show_bug.cgi?id=211537 Fixes: 6991b8f ("drm/radeon/kms: fix segfault in pm rework") Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Alex Deucher <[email protected]>
1 parent 5bbf219 commit c69f271

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

drivers/gpu/drm/radeon/radeon_atombios.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,11 +2120,14 @@ static int radeon_atombios_parse_power_table_1_3(struct radeon_device *rdev)
21202120
return state_index;
21212121
/* last mode is usually default, array is low to high */
21222122
for (i = 0; i < num_modes; i++) {
2123-
rdev->pm.power_state[state_index].clock_info =
2124-
kcalloc(1, sizeof(struct radeon_pm_clock_info),
2125-
GFP_KERNEL);
2123+
/* avoid memory leaks from invalid modes or unknown frev. */
2124+
if (!rdev->pm.power_state[state_index].clock_info) {
2125+
rdev->pm.power_state[state_index].clock_info =
2126+
kzalloc(sizeof(struct radeon_pm_clock_info),
2127+
GFP_KERNEL);
2128+
}
21262129
if (!rdev->pm.power_state[state_index].clock_info)
2127-
return state_index;
2130+
goto out;
21282131
rdev->pm.power_state[state_index].num_clock_modes = 1;
21292132
rdev->pm.power_state[state_index].clock_info[0].voltage.type = VOLTAGE_NONE;
21302133
switch (frev) {
@@ -2243,8 +2246,15 @@ static int radeon_atombios_parse_power_table_1_3(struct radeon_device *rdev)
22432246
break;
22442247
}
22452248
}
2249+
out:
2250+
/* free any unused clock_info allocation. */
2251+
if (state_index && state_index < num_modes) {
2252+
kfree(rdev->pm.power_state[state_index].clock_info);
2253+
rdev->pm.power_state[state_index].clock_info = NULL;
2254+
}
2255+
22462256
/* last mode is usually default */
2247-
if (rdev->pm.default_power_state_index == -1) {
2257+
if (state_index && rdev->pm.default_power_state_index == -1) {
22482258
rdev->pm.power_state[state_index - 1].type =
22492259
POWER_STATE_TYPE_DEFAULT;
22502260
rdev->pm.default_power_state_index = state_index - 1;

0 commit comments

Comments
 (0)