Skip to contentSkip to conversation

power: fix use-after-free in pstate code

PATCH Applied Anatoly Burakov 4 participants 7 messages Apr 7, 2021 – Apr 15, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 power: fix use-after-free in pstate code

Previous fix has addressed the incorrect handling of base_frequency file, but has added a use-after-free error due to the fact that all further code paths will lead to an fclose() call at the end, so the additional fclose() call right after processing the file was unnecessary.

Coverity issue: 369901

Fixes 8a5febaac4f7 ("power: fix P-state base frequency handling")
---
 lib/librte_power/power_pstate_cpufreq.c | 1 -
 1 file changed, 1 deletion(-)
lib/librte_power/power_pstate_cpufreq.c +0 -1
index 1cb0e4d917..ec745153d3 100644
@@ -220,7 +220,6 @@ power_init_for_setting_freq(struct pstate_power_info *pi)
 
 		base_ratio = strtoul(buf_base, NULL, POWER_CONVERT_TO_DECIMAL)
 				/ BUS_FREQ;
-		fclose(f_base);
 	}
 
 	/* Add MSR read to detect turbo status */
Signature · 1 line
2.25.1
Acked-by on v1 1/1
#1 lore raw

Hi Anatoly,

Quoted 20 lines · Anatoly Burakov

Previous fix has addressed the incorrect handling of base_frequency file, but has added a use-after-free error due to the fact that all further code paths will lead to an fclose() call at the end, so the additional fclose() call right after processing the file was unnecessary.

Coverity issue: 369901

Fixes 8a5febaac4f7 ("power: fix P-state base frequency handling")
---
  lib/librte_power/power_pstate_cpufreq.c | 1 -
  1 file changed, 1 deletion(-)
lib/librte_power/power_pstate_cpufreq.c +0 -0
index 1cb0e4d917..ec745153d3 100644

lib/librte_power/power_pstate_cpufreq.c power_init_for_setting_freq(struct pstate_power_info *pi) · line 225

Quoted 7 lines
lib/librte_power/power_pstate_cpufreq.c +0 -1
@@ -220,7 +220,6 @@ power_init_for_setting_freq(struct pstate_power_info *pi)
 
  		base_ratio = strtoul(buf_base, NULL, POWER_CONVERT_TO_DECIMAL)
  				/ BUS_FREQ;
-		fclose(f_base);
  	}
 
  	/* Add MSR read to detect turbo status */

Yes, removing the fclose will do it. Either that or add an "f_base = NULL" after the fclose, but the fclose removal is fine.

#2 lore raw

07/04/2021 18:10, David Hunt:

Quoted 11 lines

Hi Anatoly,

Quoted 8 lines · Anatoly Burakov

Previous fix has addressed the incorrect handling of base_frequency file, but has added a use-after-free error due to the fact that all further code paths will lead to an fclose() call at the end, so the additional fclose() call right after processing the file was unnecessary.

Coverity issue: 369901

Quoted 8 lines
Quoted 3 lines
Fixes 8a5febaac4f7 ("power: fix P-state base frequency handling")

Yes, removing the fclose will do it. Either that or add an "f_base = NULL" after the fclose, but the fclose removal is fine.

Applied, thanks

Reviewed-by on v1 1/1
#3 lore raw
In reply to Anatoly Burakov
Quoted 30 lines · Anatoly Burakov

Previous fix has addressed the incorrect handling of base_frequency file, but has added a use-after-free error due to the fact that all further code paths will lead to an fclose() call at the end, so the additional fclose() call right after processing the file was unnecessary.

Coverity issue: 369901

Fixes 8a5febaac4f7 ("power: fix P-state base frequency handling")
---
 lib/librte_power/power_pstate_cpufreq.c | 1 -
 1 file changed, 1 deletion(-)
lib/librte_power/power_pstate_cpufreq.c +0 -1
index 1cb0e4d917..ec745153d3 100644
@@ -220,7 +220,6 @@ power_init_for_setting_freq(struct pstate_power_info *pi)
 
 		base_ratio = strtoul(buf_base, NULL, POWER_CONVERT_TO_DECIMAL)
 				/ BUS_FREQ;
-		fclose(f_base);
 	}
 
 	/* Add MSR read to detect turbo status */
Signature · 1 line
2.25.1
#4 lore raw
In reply to Anatoly Burakov
Quoted 6 lines · Anatoly Burakov

Previous fix has addressed the incorrect handling of base_frequency file, but has added a use-after-free error due to the fact that all further code paths will lead to an fclose() call at the end, so the additional fclose() call right after processing the file was unnecessary.

Quoted 6 lines

Coverity issue: 369901

Fixes 8a5febaac4f7 ("power: fix P-state base frequency handling")

---

Actually, self-nack, because this:

	snprintf(fullpath_min, sizeof(fullpath_min), POWER_SYSFILE_MIN_FREQ,
			pi->lcore_id);
	f_min = fopen(fullpath_min, "rw+");
	FOPEN_OR_ERR_RET(f_min, -1);

	snprintf(fullpath_max, sizeof(fullpath_max), POWER_SYSFILE_MAX_FREQ,
			pi->lcore_id);
	f_max = fopen(fullpath_max, "rw+");
	if (f_max == NULL)
		fclose(f_min);
	FOPEN_OR_ERR_RET(f_max, -1);

comes after, and will leak the f_base descriptor. Closing it and setting it to NULL seems like a better solution.

Signature · 2 lines
Thanks,
Anatoly
#5 lore raw
Quoted 21 lines · Anatoly
Quoted 6 lines · Anatoly Burakov

Previous fix has addressed the incorrect handling of base_frequency file, but has added a use-after-free error due to the fact that all further code paths will lead to an fclose() call at the end, so the additional fclose() call right after processing the file was unnecessary.

Quoted 6 lines

Coverity issue: 369901

Fixes 8a5febaac4f7 ("power: fix P-state base frequency handling")

---

Actually, self-nack, because this:

    snprintf(fullpath_min, sizeof(fullpath_min), POWER_SYSFILE_MIN_FREQ, pi->lcore_id);
    f_min = fopen(fullpath_min, "rw+"); FOPEN_OR_ERR_RET(f_min, -1);

Quoted 9 lines

    snprintf(fullpath_max, sizeof(fullpath_max), POWER_SYSFILE_MAX_FREQ, pi->lcore_id);
    f_max = fopen(fullpath_max, "rw+");
    if (f_max == NULL)
        fclose(f_min);
    FOPEN_OR_ERR_RET(f_max, -1);

comes after, and will leak the f_base descriptor. Closing it and setting it to NULL seems like a better solution.

Actually no, scratch that, it doesn't :) that's before. So, patch should be OK.

Signature · 2 lines
Thanks,
Anatoly
#6 lore raw
Quoted 29 lines · Anatoly
Quoted 28 lines · Anatoly
Quoted 6 lines · Anatoly Burakov

Previous fix has addressed the incorrect handling of base_frequency file, but has added a use-after-free error due to the fact that all further code paths will lead to an fclose() call at the end, so the additional fclose() call right after processing the file was unnecessary.

Quoted 6 lines

Coverity issue: 369901

Fixes 8a5febaac4f7 ("power: fix P-state base frequency handling")

---

Actually, self-nack, because this:

    snprintf(fullpath_min, sizeof(fullpath_min), POWER_SYSFILE_MIN_FREQ, pi->lcore_id);
    f_min = fopen(fullpath_min, "rw+"); FOPEN_OR_ERR_RET(f_min, -1);

    snprintf(fullpath_max, sizeof(fullpath_max), POWER_SYSFILE_MAX_FREQ, pi->lcore_id);
    f_max = fopen(fullpath_max, "rw+");
    if (f_max == NULL)
        fclose(f_min);
    FOPEN_OR_ERR_RET(f_max, -1);

Quoted 6 lines
Quoted 3 lines

comes after, and will leak the f_base descriptor. Closing it and setting it to NULL seems like a better solution.

Actually no, scratch that, it doesn't :) that's before. So, patch should be OK.

A Cup Coffee will help ;-)

Quoted 4 lines
Signature · 3 lines
-- 
Thanks,
Anatoly