public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] perf ftrace: Fix undefined behavior in cmp_profile_data()
@ 2024-12-09 13:42 Kuan-Wei Chiu
  2024-12-09 17:02 ` Namhyung Kim
  2024-12-11 17:23 ` Namhyung Kim
  0 siblings, 2 replies; 5+ messages in thread
From: Kuan-Wei Chiu @ 2024-12-09 13:42 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung
  Cc: mark.rutland, alexander.shishkin, jolsa, irogers, kan.liang,
	adrian.hunter, jserv, chuang, linux-perf-users, linux-kernel,
	Kuan-Wei Chiu, stable

The comparison function cmp_profile_data() violates the C standard's
requirements for qsort() comparison functions, which mandate symmetry
and transitivity:

* Symmetry: If x < y, then y > x.
* Transitivity: If x < y and y < z, then x < z.

When v1 and v2 are equal, the function incorrectly returns 1, breaking
symmetry and transitivity. This causes undefined behavior, which can
lead to memory corruption in certain versions of glibc [1].

Fix the issue by returning 0 when v1 and v2 are equal, ensuring
compliance with the C standard and preventing undefined behavior.

Link: https://www.qualys.com/2024/01/30/qsort.txt [1]
Fixes: 0f223813edd0 ("perf ftrace: Add 'profile' command")
Fixes: 74ae366c37b7 ("perf ftrace profile: Add -s/--sort option")
Cc: stable@vger.kernel.org
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
 tools/perf/builtin-ftrace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
index 272d3c70810e..a56cf8b0a7d4 100644
--- a/tools/perf/builtin-ftrace.c
+++ b/tools/perf/builtin-ftrace.c
@@ -1151,8 +1151,9 @@ static int cmp_profile_data(const void *a, const void *b)
 
 	if (v1 > v2)
 		return -1;
-	else
+	if (v1 < v2)
 		return 1;
+	return 0;
 }
 
 static void print_profile_result(struct perf_ftrace *ftrace)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] perf ftrace: Fix undefined behavior in cmp_profile_data()
  2024-12-09 13:42 [PATCH] perf ftrace: Fix undefined behavior in cmp_profile_data() Kuan-Wei Chiu
@ 2024-12-09 17:02 ` Namhyung Kim
  2024-12-09 20:26   ` Arnaldo Carvalho de Melo
  2024-12-11 17:23 ` Namhyung Kim
  1 sibling, 1 reply; 5+ messages in thread
From: Namhyung Kim @ 2024-12-09 17:02 UTC (permalink / raw)
  To: Kuan-Wei Chiu
  Cc: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	irogers, kan.liang, adrian.hunter, jserv, chuang,
	linux-perf-users, linux-kernel, stable

Hello,

On Mon, Dec 9, 2024 at 5:42 AM Kuan-Wei Chiu <visitorckw@gmail.com> wrote:
>
> The comparison function cmp_profile_data() violates the C standard's
> requirements for qsort() comparison functions, which mandate symmetry
> and transitivity:
>
> * Symmetry: If x < y, then y > x.
> * Transitivity: If x < y and y < z, then x < z.
>
> When v1 and v2 are equal, the function incorrectly returns 1, breaking
> symmetry and transitivity. This causes undefined behavior, which can
> lead to memory corruption in certain versions of glibc [1].
>
> Fix the issue by returning 0 when v1 and v2 are equal, ensuring
> compliance with the C standard and preventing undefined behavior.
>
> Link: https://www.qualys.com/2024/01/30/qsort.txt [1]
> Fixes: 0f223813edd0 ("perf ftrace: Add 'profile' command")
> Fixes: 74ae366c37b7 ("perf ftrace profile: Add -s/--sort option")
> Cc: stable@vger.kernel.org
> Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>

Reviewed-by: Namhyung Kim <namhyung@kernel.org>

Thanks for the fix.
Namhyung

> ---
>  tools/perf/builtin-ftrace.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
> index 272d3c70810e..a56cf8b0a7d4 100644
> --- a/tools/perf/builtin-ftrace.c
> +++ b/tools/perf/builtin-ftrace.c
> @@ -1151,8 +1151,9 @@ static int cmp_profile_data(const void *a, const void *b)
>
>         if (v1 > v2)
>                 return -1;
> -       else
> +       if (v1 < v2)
>                 return 1;
> +       return 0;
>  }
>
>  static void print_profile_result(struct perf_ftrace *ftrace)
> --
> 2.34.1
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] perf ftrace: Fix undefined behavior in cmp_profile_data()
  2024-12-09 17:02 ` Namhyung Kim
@ 2024-12-09 20:26   ` Arnaldo Carvalho de Melo
  2024-12-09 21:48     ` Namhyung Kim
  0 siblings, 1 reply; 5+ messages in thread
From: Arnaldo Carvalho de Melo @ 2024-12-09 20:26 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Kuan-Wei Chiu, peterz, mingo, mark.rutland, alexander.shishkin,
	jolsa, irogers, kan.liang, adrian.hunter, jserv, chuang,
	linux-perf-users, linux-kernel, stable

On Mon, Dec 09, 2024 at 09:02:24AM -0800, Namhyung Kim wrote:
> Hello,
> 
> On Mon, Dec 9, 2024 at 5:42 AM Kuan-Wei Chiu <visitorckw@gmail.com> wrote:
> >
> > The comparison function cmp_profile_data() violates the C standard's
> > requirements for qsort() comparison functions, which mandate symmetry
> > and transitivity:
> >
> > * Symmetry: If x < y, then y > x.
> > * Transitivity: If x < y and y < z, then x < z.
> >
> > When v1 and v2 are equal, the function incorrectly returns 1, breaking
> > symmetry and transitivity. This causes undefined behavior, which can
> > lead to memory corruption in certain versions of glibc [1].
> >
> > Fix the issue by returning 0 when v1 and v2 are equal, ensuring
> > compliance with the C standard and preventing undefined behavior.
> >
> > Link: https://www.qualys.com/2024/01/30/qsort.txt [1]
> > Fixes: 0f223813edd0 ("perf ftrace: Add 'profile' command")
> > Fixes: 74ae366c37b7 ("perf ftrace profile: Add -s/--sort option")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
> 
> Reviewed-by: Namhyung Kim <namhyung@kernel.org>

I'm assuming you'll pick this for perf-tools, ok?

Reviewed-by: Arnaldo Carvalho de Melo <acme@redhat.com>

- Arnaldo

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] perf ftrace: Fix undefined behavior in cmp_profile_data()
  2024-12-09 20:26   ` Arnaldo Carvalho de Melo
@ 2024-12-09 21:48     ` Namhyung Kim
  0 siblings, 0 replies; 5+ messages in thread
From: Namhyung Kim @ 2024-12-09 21:48 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Kuan-Wei Chiu, peterz, mingo, mark.rutland, alexander.shishkin,
	jolsa, irogers, kan.liang, adrian.hunter, jserv, chuang,
	linux-perf-users, linux-kernel, stable

On Mon, Dec 9, 2024 at 12:26 PM Arnaldo Carvalho de Melo
<acme@kernel.org> wrote:
>
> On Mon, Dec 09, 2024 at 09:02:24AM -0800, Namhyung Kim wrote:
> > Hello,
> >
> > On Mon, Dec 9, 2024 at 5:42 AM Kuan-Wei Chiu <visitorckw@gmail.com> wrote:
> > >
> > > The comparison function cmp_profile_data() violates the C standard's
> > > requirements for qsort() comparison functions, which mandate symmetry
> > > and transitivity:
> > >
> > > * Symmetry: If x < y, then y > x.
> > > * Transitivity: If x < y and y < z, then x < z.
> > >
> > > When v1 and v2 are equal, the function incorrectly returns 1, breaking
> > > symmetry and transitivity. This causes undefined behavior, which can
> > > lead to memory corruption in certain versions of glibc [1].
> > >
> > > Fix the issue by returning 0 when v1 and v2 are equal, ensuring
> > > compliance with the C standard and preventing undefined behavior.
> > >
> > > Link: https://www.qualys.com/2024/01/30/qsort.txt [1]
> > > Fixes: 0f223813edd0 ("perf ftrace: Add 'profile' command")
> > > Fixes: 74ae366c37b7 ("perf ftrace profile: Add -s/--sort option")
> > > Cc: stable@vger.kernel.org
> > > Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
> >
> > Reviewed-by: Namhyung Kim <namhyung@kernel.org>
>
> I'm assuming you'll pick this for perf-tools, ok?
>
> Reviewed-by: Arnaldo Carvalho de Melo <acme@redhat.com>

Yep, sure.

Thanks,
Namhyung

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] perf ftrace: Fix undefined behavior in cmp_profile_data()
  2024-12-09 13:42 [PATCH] perf ftrace: Fix undefined behavior in cmp_profile_data() Kuan-Wei Chiu
  2024-12-09 17:02 ` Namhyung Kim
@ 2024-12-11 17:23 ` Namhyung Kim
  1 sibling, 0 replies; 5+ messages in thread
From: Namhyung Kim @ 2024-12-11 17:23 UTC (permalink / raw)
  To: peterz, mingo, acme, Kuan-Wei Chiu
  Cc: mark.rutland, alexander.shishkin, jolsa, irogers, kan.liang,
	adrian.hunter, jserv, chuang, linux-perf-users, linux-kernel,
	stable

On Mon, 09 Dec 2024 21:42:26 +0800, Kuan-Wei Chiu wrote:

> The comparison function cmp_profile_data() violates the C standard's
> requirements for qsort() comparison functions, which mandate symmetry
> and transitivity:
> 
> * Symmetry: If x < y, then y > x.
> * Transitivity: If x < y and y < z, then x < z.
> 
> [...]

Applied to perf-tools, thanks!

Best regards,
Namhyung


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-12-11 17:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-09 13:42 [PATCH] perf ftrace: Fix undefined behavior in cmp_profile_data() Kuan-Wei Chiu
2024-12-09 17:02 ` Namhyung Kim
2024-12-09 20:26   ` Arnaldo Carvalho de Melo
2024-12-09 21:48     ` Namhyung Kim
2024-12-11 17:23 ` Namhyung Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox