Skip to contentSkip to conversation

xenpm: sanitize allocations in show_cpufreq_para_by_cpuid()

PATCH In review Jan Beulich 3 participants 5 messages Mar 25, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xenpm: sanitize allocations in show_cpufreq_para_by_cpuid()

malloc(), when passed zero size, may return NULL (the behavior is implementation defined). Extend the ->gov_num check to the other two allocations as well. Don't chance then actually using a NULL in print_cpufreq_para().

Fixes 75e06d089d48 ("xenpm: add cpu frequency control interface, through which user can")
tools/misc/xenpm.c +7 -4
@@ -840,8 +840,9 @@ static void print_cpufreq_para(int cpuid
     }
     else
     {
-        printf("scaling_avail_gov    : %s\n",
-               p_cpufreq->scaling_available_governors);
+        if ( p_cpufreq->gov_num )
+            printf("scaling_avail_gov    : %s\n",
+                   p_cpufreq->scaling_available_governors);
 
         printf("current_governor     : %s\n", p_cpufreq->u.s.scaling_governor);
         if ( !strncmp(p_cpufreq->u.s.scaling_governor,
@@ -907,7 +908,8 @@ static int show_cpufreq_para_by_cpuid(xc
         p_cpufreq->scaling_available_frequencies = NULL;
         p_cpufreq->scaling_available_governors = NULL;
 
-        if (!(p_cpufreq->affected_cpus =
+        if (p_cpufreq->cpu_num &&
+            !(p_cpufreq->affected_cpus =
               malloc(p_cpufreq->cpu_num * sizeof(uint32_t))))
         {
             fprintf(stderr,
@@ -916,7 +918,8 @@ static int show_cpufreq_para_by_cpuid(xc
             ret = -ENOMEM;
             goto out;
         }
-        if (!(p_cpufreq->scaling_available_frequencies =
+        if (p_cpufreq->freq_num &&
+            !(p_cpufreq->scaling_available_frequencies =
               malloc(p_cpufreq->freq_num * sizeof(uint32_t))))
         {
             fprintf(stderr,
#1 lore raw
Quoted 2 lines · Jan Beulich

malloc(), when passed zero size, may return NULL (the behavior is implementation defined).

More importantly, this is the Musl behaviour, so is how ~most of Gitlab CI behaves.

Quoted 2 lines

Extend the ->gov_num check to the other two
allocations as well.

I'm not sure what you mean by this.  Only one of these hunks has a ->gov_num check.

Quoted 5 lines

Don't chance then actually using a NULL in
print_cpufreq_para().

Fixes 75e06d089d48 ("xenpm: add cpu frequency control interface, through which user can")

The code change looks ok, so Acked-by: Andrew Cooper <[email protected]> but I'd prefer a clarified commit message.

#2 lore raw
Quoted 4 lines · Andrew Cooper
Quoted 2 lines · Jan Beulich

malloc(), when passed zero size, may return NULL (the behavior is implementation defined).

Quoted 8 lines

More importantly, this is the Musl behaviour, so is how ~most of Gitlab CI behaves.

Quoted 2 lines

Extend the ->gov_num check to the other two
allocations as well.

I'm not sure what you mean by this.  Only one of these hunks has a ->gov_num check.

Not sure I see a better way of wording this: What I mean to say is that the ->gov_num check that there is already is being replicated (with "gov" replaced accordingly) to the other two places where similar allocations happen. Maybe simply s/Extend/Mirror" would already help?

Quoted 8 lines
Quoted 5 lines

Don't chance then actually using a NULL in
print_cpufreq_para().

Fixes 75e06d089d48 ("xenpm: add cpu frequency control interface, through which user can")

The code change looks ok, so Acked-by: Andrew Cooper <[email protected]> but I'd prefer a clarified commit message.

Thanks.

Jan

#3 lore raw
Quoted 11 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper
Quoted 2 lines · Jan Beulich

malloc(), when passed zero size, may return NULL (the behavior is implementation defined).

More importantly, this is the Musl behaviour, so is how ~most of Gitlab CI behaves.

Quoted 2 lines

Extend the ->gov_num check to the other two
allocations as well.

Quoted 7 lines
Quoted 2 lines

I'm not sure what you mean by this.  Only one of these hunks has a ->gov_num check.

Not sure I see a better way of wording this: What I mean to say is that the ->gov_num check that there is already is being replicated (with "gov" replaced accordingly) to the other two places where similar allocations happen. Maybe simply s/Extend/Mirror" would already help?

Oh, and to clarify: The ->gov_num check mentioned isn't the one visible in one of the hunks. It's entirely outside of patch context. It was put there (for [now] questionable reasons by the HWP work).

Jan

Reviewed-by
#4 lore raw
In reply to Jan Beulich
Quoted 11 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper
Quoted 2 lines · Jan Beulich

malloc(), when passed zero size, may return NULL (the behavior is implementation defined).

More importantly, this is the Musl behaviour, so is how ~most of Gitlab CI behaves.

Quoted 2 lines

Extend the ->gov_num check to the other two
allocations as well.

Quoted 7 lines
Quoted 2 lines

I'm not sure what you mean by this.  Only one of these hunks has a ->gov_num check.

Not sure I see a better way of wording this: What I mean to say is that the ->gov_num check that there is already is being replicated (with "gov" replaced accordingly) to the other two places where similar allocations happen. Maybe simply s/Extend/Mirror" would already help?

Yes, "Mirror" is better.

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

Don't chance then actually using a NULL in
print_cpufreq_para().

Fixes 75e06d089d48 ("xenpm: add cpu frequency control interface, through which user can")

The code change looks ok, so Acked-by: Andrew Cooper <[email protected]> but I'd prefer a clarified commit message.

Thanks,
Jason