Skip to contentSkip to conversation

x86/pmstat: Check size of PMSTAT_get_pxstat buffers

PATCH Superseded Ross Lagerwall 2 participants 4 messages Apr 17, 2025 – May 12, 2025

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/pmstat: Check size of PMSTAT_get_pxstat buffers

Check that the total number of states passed in and hence the size of buffers is sufficient to avoid writing more than the caller has allocated.

The interface is not explicit about whether getpx.total is expected to be set by the caller in this case but since it is always set in libxenctrl it seems reasonable to check it.

Fixes c06a7db0c547 ("X86 and IA64: Update cpufreq statistic logic for supporting both x86 and ia64")
---
 xen/drivers/acpi/pmstat.c | 8 ++++++++
 1 file changed, 8 insertions(+)
xen/drivers/acpi/pmstat.c +8 -0
index c51b9ca358c2..c388b1e9a17a 100644
@@ -104,6 +104,14 @@ int do_get_pm_info(struct xen_sysctl_get_pmstat *op)
         cpufreq_residency_update(op->cpuid, pxpt->u.cur);
 
         ct = pmpt->perf.state_count;
+
+        if ( op->u.getpx.total < ct )
+        {
+            spin_unlock(cpufreq_statistic_lock);
+            ret = -ENOSPC;
+            break;
+        }
+
         if ( copy_to_guest(op->u.getpx.trans_pt, pxpt->u.trans_pt, ct*ct) )
         {
             spin_unlock(cpufreq_statistic_lock);
Signature · 1 line
2.49.0
#1 lore raw

xen/drivers/acpi/pmstat.c int do_get_pm_info(struct xen_sysctl_get_pmstat *op) · line 113

Quoted 13 lines · Ross Lagerwall
xen/drivers/acpi/pmstat.c +7 -0
@@ -104,6 +104,14 @@ int do_get_pm_info(struct xen_sysctl_get_pmstat *op)
         cpufreq_residency_update(op->cpuid, pxpt->u.cur);
 
         ct = pmpt->perf.state_count;
+
+        if ( op->u.getpx.total < ct )
+        {
+            spin_unlock(cpufreq_statistic_lock);
+            ret = -ENOSPC;
+            break;
+        }

Simply producing an error is not an option imo. See pmstat_get_cx_stat()'s behavior. Imo the calculation of ct wants to become

        ct = min(pmpt->perf.state_count, op->u.getpx.total);

yet then the copying of the 2-dim array of data becomes more complicated when ct < pmpt->perf.state_count. An option may be to document that this array is copied only when the buffer is large enough.

Furthermore I think it would be a good idea to also amend the public header with IN/OUT annotations for the fields which are input and output (also for the Cx part, ideally).

And then - doesn't xc_pm_get_pxstat() have a similar issue?

Jan

#2 lore raw
Quoted 19 lines · Jan Beulich

xen/drivers/acpi/pmstat.c int do_get_pm_info(struct xen_sysctl_get_pmstat *op) · line 113

Quoted 13 lines · Ross Lagerwall
xen/drivers/acpi/pmstat.c +7 -0
@@ -104,6 +104,14 @@ int do_get_pm_info(struct xen_sysctl_get_pmstat *op)
         cpufreq_residency_update(op->cpuid, pxpt->u.cur);
 
         ct = pmpt->perf.state_count;
+
+        if ( op->u.getpx.total < ct )
+        {
+            spin_unlock(cpufreq_statistic_lock);
+            ret = -ENOSPC;
+            break;
+        }

Simply producing an error is not an option imo. See pmstat_get_cx_stat()'s behavior. Imo the calculation of ct wants to become

Quoted 5 lines
        ct = min(pmpt->perf.state_count, op->u.getpx.total);

yet then the copying of the 2-dim array of data becomes more complicated when ct < pmpt->perf.state_count. An option may be to document that this array is copied only when the buffer is large enough.

Another option would be for the caller to explicitly pass in both array sizes and Xen can copy as much as fits since in either case there would need to be a way for Xen to return how much it has (separately) copied for both arrays. Does that make sense?

Quoted 4 lines

Furthermore I think it would be a good idea to also amend the public header with IN/OUT annotations for the fields which are input and output (also for the Cx part, ideally).

Sure.

Quoted 3 lines

And then - doesn't xc_pm_get_pxstat() have a similar issue?

Indeed, I can send a patch for that.

Ross

#3 lore raw
Quoted 22 lines · Ross Lagerwall
Quoted 21 lines · Jan Beulich

xen/drivers/acpi/pmstat.c int do_get_pm_info(struct xen_sysctl_get_pmstat *op) · line 113

Quoted 13 lines · Ross Lagerwall
xen/drivers/acpi/pmstat.c +7 -0
@@ -104,6 +104,14 @@ int do_get_pm_info(struct xen_sysctl_get_pmstat *op)
         cpufreq_residency_update(op->cpuid, pxpt->u.cur);
 
         ct = pmpt->perf.state_count;
+
+        if ( op->u.getpx.total < ct )
+        {
+            spin_unlock(cpufreq_statistic_lock);
+            ret = -ENOSPC;
+            break;
+        }

Simply producing an error is not an option imo. See pmstat_get_cx_stat()'s behavior. Imo the calculation of ct wants to become

        ct = min(pmpt->perf.state_count, op->u.getpx.total);
Quoted 8 lines
Quoted 3 lines

yet then the copying of the 2-dim array of data becomes more complicated when ct < pmpt->perf.state_count. An option may be to document that this array is copied only when the buffer is large enough.

Another option would be for the caller to explicitly pass in both array sizes and Xen can copy as much as fits since in either case there would need to be a way for Xen to return how much it has (separately) copied for both arrays. Does that make sense?

May be an option, but would require adjustments to the interface itself.

Jan