LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Kalesh Singh <kaleshsingh@google.com>
To: Sami Tolvanen <samitolvanen@google.com>
Cc: Suren Baghdasaryan <surenb@google.com>,
	Hridya Valsaraju <hridya@google.com>,
	namhyung@kernel.org, Nick Desaulniers <ndesaulniers@google.com>,
	Android Kernel Team <kernel-team@android.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ingo Molnar <mingo@redhat.com>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] tracing/cfi: Fix cmp_entries_* functions signature mismatch
Date: Mon, 27 Sep 2021 11:15:01 -0700	[thread overview]
Message-ID: <CAC_TJve2aS_tHfmMCsayPFrAPaMid5DU3NK82KXC3dB9vhPdpw@mail.gmail.com> (raw)
In-Reply-To: <CABCJKucTiHDitevf1U97eCV1KdxAyui+fWvD1i7c8piVMnmjoQ@mail.gmail.com>

On Fri, Sep 24, 2021 at 8:20 AM Sami Tolvanen <samitolvanen@google.com> wrote:
>
> Hi Kalesh,
>
> On Thu, Sep 23, 2021 at 10:09 AM Kalesh Singh <kaleshsingh@google.com> wrote:
> >
> > If CONFIG_CFI_CLANG=y, attempting to read an event histogram will cause
> > the kernel to panic due to failed CFI check.
> >
> >     1. echo 'hist:keys=common_pid' >> events/sched/sched_switch/trigger
> >     2. cat >> events/sched/sched_switch/hist
> >     3. kernel panices on attempting to read hist
> >
> > This happens because the sort() function expects a generic
> > int (*)(const void *, const void *) pointer for the compare function.
> > To prevent this CFI failure, change tracing map cmp_entries_* function
> > signatures to match this.
> >
> > Signed-off-by: Kalesh Singh <kaleshsingh@google.com>
> > ---
> >  kernel/trace/tracing_map.c | 40 ++++++++++++++++++++++----------------
> >  1 file changed, 23 insertions(+), 17 deletions(-)
> >
> > diff --git a/kernel/trace/tracing_map.c b/kernel/trace/tracing_map.c
> > index d6bddb157ef2..a8c80ebbf9da 100644
> > --- a/kernel/trace/tracing_map.c
> > +++ b/kernel/trace/tracing_map.c
> > @@ -834,19 +834,21 @@ int tracing_map_init(struct tracing_map *map)
> >         return err;
> >  }
> >
> > -static int cmp_entries_dup(const struct tracing_map_sort_entry **a,
> > -                          const struct tracing_map_sort_entry **b)
> > +static int cmp_entries_dup(const void *__a, const void *__b)
> >  {
> >         int ret = 0;
> > +       const struct tracing_map_sort_entry *a
> > +               = *(const struct tracing_map_sort_entry **)__a;
> > +       const struct tracing_map_sort_entry *b
> > +               = *(const struct tracing_map_sort_entry **)__b;
> >
> > -       if (memcmp((*a)->key, (*b)->key, (*a)->elt->map->key_size))
> > +       if (memcmp(a->key, b->key, a->elt->map->key_size))
> >                 ret = 1;
> >
> >         return ret;
> >  }
> >
> > -static int cmp_entries_sum(const struct tracing_map_sort_entry **a,
> > -                          const struct tracing_map_sort_entry **b)
> > +static int cmp_entries_sum(const void *__a, const void *__b)
> >  {
> >         const struct tracing_map_elt *elt_a, *elt_b;
> >         struct tracing_map_sort_key *sort_key;
> > @@ -854,9 +856,13 @@ static int cmp_entries_sum(const struct tracing_map_sort_entry **a,
> >         tracing_map_cmp_fn_t cmp_fn;
> >         void *val_a, *val_b;
> >         int ret = 0;
> > +       const struct tracing_map_sort_entry *a
> > +               = *(const struct tracing_map_sort_entry **)__a;
> > +       const struct tracing_map_sort_entry *b
> > +               = *(const struct tracing_map_sort_entry **)__b;
> >
> > -       elt_a = (*a)->elt;
> > -       elt_b = (*b)->elt;
> > +       elt_a = a->elt;
> > +       elt_b = b->elt;
> >
> >         sort_key = &elt_a->map->sort_key;
> >
> > @@ -873,8 +879,7 @@ static int cmp_entries_sum(const struct tracing_map_sort_entry **a,
> >         return ret;
> >  }
> >
> > -static int cmp_entries_key(const struct tracing_map_sort_entry **a,
> > -                          const struct tracing_map_sort_entry **b)
> > +static int cmp_entries_key(const void *__a, const void *__b)
> >  {
> >         const struct tracing_map_elt *elt_a, *elt_b;
> >         struct tracing_map_sort_key *sort_key;
> > @@ -882,9 +887,13 @@ static int cmp_entries_key(const struct tracing_map_sort_entry **a,
> >         tracing_map_cmp_fn_t cmp_fn;
> >         void *val_a, *val_b;
> >         int ret = 0;
> > +       const struct tracing_map_sort_entry *a
> > +               = *(const struct tracing_map_sort_entry **)__a;
> > +       const struct tracing_map_sort_entry *b
> > +               = *(const struct tracing_map_sort_entry **)__b;
> >
> > -       elt_a = (*a)->elt;
> > -       elt_b = (*b)->elt;
> > +       elt_a = a->elt;
> > +       elt_b = b->elt;
> >
> >         sort_key = &elt_a->map->sort_key;
> >
> > @@ -989,10 +998,8 @@ static void sort_secondary(struct tracing_map *map,
> >                            struct tracing_map_sort_key *primary_key,
> >                            struct tracing_map_sort_key *secondary_key)
> >  {
> > -       int (*primary_fn)(const struct tracing_map_sort_entry **,
> > -                         const struct tracing_map_sort_entry **);
> > -       int (*secondary_fn)(const struct tracing_map_sort_entry **,
> > -                           const struct tracing_map_sort_entry **);
> > +       int (*primary_fn)(const void *, const void *);
> > +       int (*secondary_fn)(const void *, const void *);
> >         unsigned i, start = 0, n_sub = 1;
> >
> >         if (is_key(map, primary_key->field_idx))
> > @@ -1061,8 +1068,7 @@ int tracing_map_sort_entries(struct tracing_map *map,
> >                              unsigned int n_sort_keys,
> >                              struct tracing_map_sort_entry ***sort_entries)
> >  {
> > -       int (*cmp_entries_fn)(const struct tracing_map_sort_entry **,
> > -                             const struct tracing_map_sort_entry **);
> > +       int (*cmp_entries_fn)(const void *, const void *);
> >         struct tracing_map_sort_entry *sort_entry, **entries;
> >         int i, n_entries, ret;
> >
> >
>
> Thanks for the patch! This looks correct to me and fixes the function
> type mismatch that trips CFI.
>
> Reviewed-by: Sami Tolvanen <samitolvanen@google.com>

Thanks for the review Sami.

Steve, will this get picked up for your tree?

>
> Sami

  reply	other threads:[~2021-09-27 18:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-23 17:09 Kalesh Singh
2021-09-24 15:20 ` Sami Tolvanen
2021-09-27 18:15   ` Kalesh Singh [this message]
2021-09-27 21:56     ` Steven Rostedt
2021-10-13 22:08 ` Steven Rostedt
2021-10-14  1:39   ` Kalesh Singh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAC_TJve2aS_tHfmMCsayPFrAPaMid5DU3NK82KXC3dB9vhPdpw@mail.gmail.com \
    --to=kaleshsingh@google.com \
    --cc=hridya@google.com \
    --cc=kernel-team@android.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=rostedt@goodmis.org \
    --cc=samitolvanen@google.com \
    --cc=surenb@google.com \
    --subject='Re: [PATCH] tracing/cfi: Fix cmp_entries_* functions signature mismatch' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).