RE: [PATCH v2] tipc: Don't using smp_processor_id() in preemptible code

From: Tuong Tong Lien
Date: Tue Jul 14 2020 - 23:53:52 EST




> -----Original Message-----
> From: Zhang, Qiang <Qiang.Zhang@xxxxxxxxxxxxx>
> Sent: Wednesday, July 15, 2020 9:13 AM
> To: Eric Dumazet <eric.dumazet@xxxxxxxxx>; jmaloy@xxxxxxxxxx; davem@xxxxxxxxxxxxx; kuba@xxxxxxxxxx; Tuong Tong Lien
> <tuong.t.lien@xxxxxxxxxxxxxx>; Xue, Ying <Ying.Xue@xxxxxxxxxxxxx>
> Cc: netdev@xxxxxxxxxxxxxxx; tipc-discussion@xxxxxxxxxxxxxxxxxxxxx; linux-kernel@xxxxxxxxxxxxxxx
> Subject: åå: [PATCH v2] tipc: Don't using smp_processor_id() in preemptible code
>
>
>
> ________________________________________
> åää: Eric Dumazet <eric.dumazet@xxxxxxxxx>
> åéæé: 2020å7æ14æ 22:15
> æää: Zhang, Qiang; jmaloy@xxxxxxxxxx; davem@xxxxxxxxxxxxx; kuba@xxxxxxxxxx; tuong.t.lien@xxxxxxxxxxxxxx;
> eric.dumazet@xxxxxxxxx; Xue, Ying
> æé: netdev@xxxxxxxxxxxxxxx; tipc-discussion@xxxxxxxxxxxxxxxxxxxxx; linux-kernel@xxxxxxxxxxxxxxx
> äé: Re: [PATCH v2] tipc: Don't using smp_processor_id() in preemptible code
>
>
>
> On 7/14/20 1:05 AM, qiang.zhang@xxxxxxxxxxxxx wrote:
> > From: Zhang Qiang <qiang.zhang@xxxxxxxxxxxxx>
> >
> > CPU: 0 PID: 6801 Comm: syz-executor201 Not tainted 5.8.0-rc4-syzkaller #0
> > Hardware name: Google Google Compute Engine/Google Compute Engine,
> > BIOS Google 01/01/2011
> >
> > Fixes: fc1b6d6de2208 ("tipc: introduce TIPC encryption & authentication")
> > Reported-by: syzbot+263f8c0d007dc09b2dda@xxxxxxxxxxxxxxxxxxxxxxxxx
> > Signed-off-by: Zhang Qiang <qiang.zhang@xxxxxxxxxxxxx>
> > ---
> > v1->v2:
> > add fixes tags.
> >
> > net/tipc/crypto.c | 3 ++-
> > 1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/net/tipc/crypto.c b/net/tipc/crypto.c
> > index 8c47ded2edb6..520af0afe1b3 100644
> > --- a/net/tipc/crypto.c
> > +++ b/net/tipc/crypto.c
> > @@ -399,9 +399,10 @@ static void tipc_aead_users_set(struct tipc_aead __rcu *aead, int val)
> > */
> > static struct crypto_aead *tipc_aead_tfm_next(struct tipc_aead *aead)
> > {
> > - struct tipc_tfm **tfm_entry = this_cpu_ptr(aead->tfm_entry);
> > + struct tipc_tfm **tfm_entry = get_cpu_ptr(aead->tfm_entry);
> >
> > *tfm_entry = list_next_entry(*tfm_entry, list);
> > + put_cpu_ptr(tfm_entry);
> > return (*tfm_entry)->tfm;
> > }
> >
> >
>
> > You have not explained why this was safe.
> >
> > This seems to hide a real bug.
> >
> > Presumably callers of this function should have disable preemption, and maybe > interrupts as well.
> >
> >Right after put_cpu_ptr(tfm_entry), this thread could migrate to another cpu, >and still access
> >data owned by the old cpu.
>
> Thanks for you suggest, I will check code again.
>

Actually, last week I sent a similar patch to tipc-discussion which covers the
case as well (there is also another place causing the same issue...). If you
don't mind, you can take a look at below (just copied/pasted).

BR/Tuong

-----Original Message-----
From: Tuong Tong Lien <tuong.t.lien@xxxxxxxxxxxxxx>
Sent: Friday, July 10, 2020 5:11 PM
To: jmaloy@xxxxxxxxxx; maloy@xxxxxxxxxxx; ying.xue@xxxxxxxxxxxxx; tipc-discussion@xxxxxxxxxxxxxxxxxxxxx
Cc: tipc-dek <tipc-dek@xxxxxxxxxxxxxx>
Subject: [PATCH RFC 1/5] tipc: fix using smp_processor_id() in preemptible

The 'this_cpu_ptr()' is used to obtain the AEAD key' TFM on the current
CPU for encryption, however the execution can be preemptible since it's
actually user-space context, so the 'using smp_processor_id() in
preemptible' has been observed.

We fix the issue by using the 'get/put_cpu_ptr()' API which consists of
a 'preempt_disable()' instead.

Signed-off-by: Tuong Lien <tuong.t.lien@xxxxxxxxxxxxxx>
---
net/tipc/crypto.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/net/tipc/crypto.c b/net/tipc/crypto.c
index c8c47fc72653..1827ce4fac5d 100644
--- a/net/tipc/crypto.c
+++ b/net/tipc/crypto.c
@@ -326,7 +326,8 @@ static void tipc_aead_free(struct rcu_head *rp)
if (aead->cloned) {
tipc_aead_put(aead->cloned);
} else {
- head = *this_cpu_ptr(aead->tfm_entry);
+ head = *get_cpu_ptr(aead->tfm_entry);
+ put_cpu_ptr(aead->tfm_entry);
list_for_each_entry_safe(tfm_entry, tmp, &head->list, list) {
crypto_free_aead(tfm_entry->tfm);
list_del(&tfm_entry->list);
@@ -399,10 +400,15 @@ static void tipc_aead_users_set(struct tipc_aead __rcu *aead, int val)
*/
static struct crypto_aead *tipc_aead_tfm_next(struct tipc_aead *aead)
{
- struct tipc_tfm **tfm_entry = this_cpu_ptr(aead->tfm_entry);
+ struct tipc_tfm **tfm_entry;
+ struct crypto_aead *tfm;

+ tfm_entry = get_cpu_ptr(aead->tfm_entry);
*tfm_entry = list_next_entry(*tfm_entry, list);
- return (*tfm_entry)->tfm;
+ tfm = (*tfm_entry)->tfm;
+ put_cpu_ptr(tfm_entry);
+
+ return tfm;
}

/**
--
2.13.7