From nobody Tue Dec 2 02:58:22 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0F27C339B43 for ; Mon, 17 Nov 2025 18:54:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763405659; cv=none; b=F1Z/0NftzrBmDWj8DfqRgB1ckK7eJL9bYYEg9iuH/teivNJOHhFE6p4hNPjMLrRCD+07sU0jiRJkk5FxszlZ+ms06GQ+Nhj8Pry4R7XZkJuKHjI1+0NNdQuuab74laTi3G3OE+Dn443VEYH0aueWMF6bDqXSXBkjO4TBPD31B5w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763405659; c=relaxed/simple; bh=YWrSV4nY4LocEp7mCnwqF5xXNyBYnRwLmTPE4sU0cPE=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KxbVswITi2yTNmWoBm38DhEiCFCOpK2xXOsqwfCne4taCnUcz1vc3IRsoAuRIHmf3TQIFP1TxvioeT+2zfBWBcdl90qoLM9+kAjq4J+a/ffYK8yBmMOAQuLwfVW9LDhGnN0iD+PZ7yf+qrM3bYwpfLpvZRIoB93knYLcQLaeiCk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=HccpLtMB; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="HccpLtMB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763405655; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bFNQ6BM0IW516zJK11WM40Y2bQSeuES9ESqJWTtS0po=; b=HccpLtMBcfvysZ2MyG1Yw80NwpD/ZoeqzSo0e2DyI5vftyaOySuxmVa8/DQrSxxuplW/rC kL0t+Q7h3jUY0DrTRaGv2cENYtuLknroef/EhMyxyTyiKG3V7wMmNPoCh3kvIeJgW/Cg3D Atqn6NOPuO8fr7crpGC29QaiEW+fXC8= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-328-4Ygaa0CeP7-nYKrFBbDUbQ-1; Mon, 17 Nov 2025 13:54:12 -0500 X-MC-Unique: 4Ygaa0CeP7-nYKrFBbDUbQ-1 X-Mimecast-MFC-AGG-ID: 4Ygaa0CeP7-nYKrFBbDUbQ_1763405651 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8356C1801308; Mon, 17 Nov 2025 18:54:11 +0000 (UTC) Received: from wcosta-thinkpadt14gen4.rmtbr.csb (unknown [10.22.81.153]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C3536180049F; Mon, 17 Nov 2025 18:54:05 +0000 (UTC) From: Wander Lairson Costa To: Steven Rostedt , Wander Lairson Costa , Tomas Glozar , Ivan Pravdin , Crystal Wood , John Kacur , Costa Shulyupin , Tiezhu Yang , linux-trace-kernel@vger.kernel.org (open list:Real-time Linux Analysis (RTLA) tools), linux-kernel@vger.kernel.org (open list), bpf@vger.kernel.org (open list:BPF [MISC]:Keyword:(?:\b|_)bpf(?:\b|_)) Subject: [rtla 01/13] rtla: Check for memory allocation failures Date: Mon, 17 Nov 2025 15:41:08 -0300 Message-ID: <20251117184409.42831-2-wander@redhat.com> In-Reply-To: <20251117184409.42831-1-wander@redhat.com> References: <20251117184409.42831-1-wander@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Type: text/plain; charset="utf-8" The actions_init() and actions_new() functions did not check the return value of calloc() and realloc() respectively. In a low memory situation, this could lead to a NULL pointer dereference. Add checks for the return value of memory allocation functions and return an error in case of failure. Update the callers to handle the error properly. Signed-off-by: Wander Lairson Costa Reviewed-by: Masami Hiramatsu (Google) --- tools/tracing/rtla/src/actions.c | 26 +++++++++++++++++++++++--- tools/tracing/rtla/src/actions.h | 2 +- tools/tracing/rtla/src/timerlat_hist.c | 7 +++++-- tools/tracing/rtla/src/timerlat_top.c | 7 +++++-- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/tools/tracing/rtla/src/actions.c b/tools/tracing/rtla/src/acti= ons.c index 8945aee58d511..01648a1425c10 100644 --- a/tools/tracing/rtla/src/actions.c +++ b/tools/tracing/rtla/src/actions.c @@ -11,11 +11,13 @@ /* * actions_init - initialize struct actions */ -void +int actions_init(struct actions *self) { self->size =3D action_default_size; self->list =3D calloc(self->size, sizeof(struct action)); + if (!self->list) + return -1; self->len =3D 0; self->continue_flag =3D false; =20 @@ -23,6 +25,7 @@ actions_init(struct actions *self) =20 /* This has to be set by the user */ self->trace_output_inst =3D NULL; + return 0; } =20 /* @@ -50,8 +53,13 @@ static struct action * actions_new(struct actions *self) { if (self->len >=3D self->size) { - self->size *=3D 2; - self->list =3D realloc(self->list, self->size * sizeof(struct action)); + const size_t new_size =3D self->size * 2; + void *p =3D reallocarray(self->list, new_size, sizeof(struct action)); + + if (!p) + return NULL; + self->list =3D p; + self->size =3D new_size; } =20 return &self->list[self->len++]; @@ -65,6 +73,9 @@ actions_add_trace_output(struct actions *self, const char= *trace_output) { struct action *action =3D actions_new(self); =20 + if (!action) + return -1; + self->present[ACTION_TRACE_OUTPUT] =3D true; action->type =3D ACTION_TRACE_OUTPUT; action->trace_output =3D calloc(strlen(trace_output) + 1, sizeof(char)); @@ -83,6 +94,9 @@ actions_add_signal(struct actions *self, int signal, int = pid) { struct action *action =3D actions_new(self); =20 + if (!action) + return -1; + self->present[ACTION_SIGNAL] =3D true; action->type =3D ACTION_SIGNAL; action->signal =3D signal; @@ -99,6 +113,9 @@ actions_add_shell(struct actions *self, const char *comm= and) { struct action *action =3D actions_new(self); =20 + if (!action) + return -1; + self->present[ACTION_SHELL] =3D true; action->type =3D ACTION_SHELL; action->command =3D calloc(strlen(command) + 1, sizeof(char)); @@ -117,6 +134,9 @@ actions_add_continue(struct actions *self) { struct action *action =3D actions_new(self); =20 + if (!action) + return -1; + self->present[ACTION_CONTINUE] =3D true; action->type =3D ACTION_CONTINUE; =20 diff --git a/tools/tracing/rtla/src/actions.h b/tools/tracing/rtla/src/acti= ons.h index a4f9b570775b5..439bcc58ac93a 100644 --- a/tools/tracing/rtla/src/actions.h +++ b/tools/tracing/rtla/src/actions.h @@ -42,7 +42,7 @@ struct actions { struct tracefs_instance *trace_output_inst; }; =20 -void actions_init(struct actions *self); +int actions_init(struct actions *self); void actions_destroy(struct actions *self); int actions_add_trace_output(struct actions *self, const char *trace_outpu= t); int actions_add_signal(struct actions *self, int signal, int pid); diff --git a/tools/tracing/rtla/src/timerlat_hist.c b/tools/tracing/rtla/sr= c/timerlat_hist.c index 606c1688057b2..09a3da3f58630 100644 --- a/tools/tracing/rtla/src/timerlat_hist.c +++ b/tools/tracing/rtla/src/timerlat_hist.c @@ -798,8 +798,11 @@ static struct common_params if (!params) exit(1); =20 - actions_init(¶ms->common.threshold_actions); - actions_init(¶ms->common.end_actions); + if (actions_init(¶ms->common.threshold_actions) || + actions_init(¶ms->common.end_actions)) { + err_msg("Error initializing actions"); + exit(EXIT_FAILURE); + } =20 /* disabled by default */ params->dma_latency =3D -1; diff --git a/tools/tracing/rtla/src/timerlat_top.c b/tools/tracing/rtla/src= /timerlat_top.c index fc479a0dcb597..7679820e72db5 100644 --- a/tools/tracing/rtla/src/timerlat_top.c +++ b/tools/tracing/rtla/src/timerlat_top.c @@ -556,8 +556,11 @@ static struct common_params if (!params) exit(1); =20 - actions_init(¶ms->common.threshold_actions); - actions_init(¶ms->common.end_actions); + if (actions_init(¶ms->common.threshold_actions) || + actions_init(¶ms->common.end_actions)) { + err_msg("Error initializing actions"); + exit(EXIT_FAILURE); + } =20 /* disabled by default */ params->dma_latency =3D -1; --=20 2.51.1