[PATCH 5/6] perf/sched: replace list_first_entry with list_first_entry_or_null
From: Wang Haoran
Date: Fri May 29 2026 - 02:57:21 EST
>From f66a328ea7a6832689b8d19f4643f31b8caf1e28 Mon Sep 17 00:00:00 2001
From: Wang Haoran <haoranwangsec@xxxxxxxxx>
Date: Thu, 28 May 2026 15:18:07 +0800
Subject: [PATCH 5/6] perf/sched: replace list_first_entry with
list_first_entry_or_null
list_first_entry() is unsafe when called on a potentially empty list:
it computes container_of() on the list head itself and returns a
garbage pointer rather than NULL, so any NULL check on the result is
dead code.
get_all_cpu_stats() and show_schedstat_data() call list_first_entry()
on lists that are populated from user-controlled perf.data content,
making them reachable via crafted input. Replace every such call with
list_first_entry_or_null() and add the corresponding NULL guards.
Fixes: <get_all_cpu_stats / show_schedstat_data>
Signed-off-by: Wang Haoran <haoranwangsec@xxxxxxxxx>
---
tools/perf/builtin-sched.c | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)
diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index ab4c9ffa4..55391f0b1 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -4170,7 +4170,7 @@ static void summarize_schedstat_domain(struct schedstat_domain *summary_domain,
*/
static int get_all_cpu_stats(struct list_head *head)
{
- struct schedstat_cpu *cptr = list_first_entry(head, struct schedstat_cpu, cpu_list);
+ struct schedstat_cpu *cptr = list_first_entry_or_null(head, struct schedstat_cpu, cpu_list);
struct schedstat_cpu *summary_head = NULL;
struct perf_record_schedstat_domain *ds;
struct perf_record_schedstat_cpu *cs;
@@ -4212,8 +4212,11 @@ static int get_all_cpu_stats(struct list_head *head)
cnt++;
summarize_schedstat_cpu(summary_head, cptr, cnt, is_last);
- tdptr = list_first_entry(&summary_head->domain_head, struct schedstat_domain,
- domain_list);
+ tdptr = list_first_entry_or_null(&summary_head->domain_head,
+ struct schedstat_domain,
+ domain_list);
+ if (!tdptr)
+ break;
list_for_each_entry(dptr, &cptr->domain_head, domain_list) {
summarize_schedstat_domain(tdptr, dptr, cnt, is_last);
@@ -4229,7 +4232,8 @@ static int show_schedstat_data(struct list_head *head1, struct cpu_domain_map **
struct list_head *head2, struct cpu_domain_map **cd_map2,
bool summary_only)
{
- struct schedstat_cpu *cptr1 = list_first_entry(head1, struct schedstat_cpu, cpu_list);
+ struct schedstat_cpu *cptr1 =
+ list_first_entry_or_null(head1, struct schedstat_cpu, cpu_list);
struct perf_record_schedstat_domain *ds1 = NULL, *ds2 = NULL;
struct perf_record_schedstat_cpu *cs1 = NULL, *cs2 = NULL;
struct schedstat_domain *dptr1 = NULL, *dptr2 = NULL;
@@ -4250,10 +4254,14 @@ static int show_schedstat_data(struct list_head *head1, struct cpu_domain_map **
printf("\n");
printf("%-65s: ", "Time elapsed (in jiffies)");
+ if (!cptr1)
+ return -EINVAL;
jiffies1 = cptr1->cpu_data->timestamp;
printf("%11llu", jiffies1);
if (head2) {
- cptr2 = list_first_entry(head2, struct schedstat_cpu, cpu_list);
+ cptr2 = list_first_entry_or_null(head2, struct schedstat_cpu, cpu_list);
+ if (!cptr2)
+ return -EINVAL;
jiffies2 = cptr2->cpu_data->timestamp;
printf(",%11llu", jiffies2);
}
--
2.53.0