Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3279563pxk; Mon, 28 Sep 2020 13:04:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwZ8jpYHS4UbzRo7okS1K3NUcVwjk4/LIgBuhHs60JdZd8D/bUFYz0W3LFndXVbZA8ecd/4 X-Received: by 2002:aa7:da16:: with SMTP id r22mr3813125eds.132.1601323480462; Mon, 28 Sep 2020 13:04:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601323480; cv=none; d=google.com; s=arc-20160816; b=LUfrJFWql8czpaxhgfFDlMoeISQPPY9pO9SXoUFG2VZujiSCbKdEiiUT3D82FjjQad Tp/0rw9/F/IfXInLAoU7DgxMohM6ONkyIlgQZsUrL4Cd517A1oCitA+/zpyA9OA5ASTN T/tgG6wTMPmDboQNDxfdFDzbrwEkpxqyMez2auvlog/rzGsyoKQMJthvUIMLg2UC79jq oAE+vA/4Stw7p/4/b9rng42muJwoWgXzIHPbw/wJXeIk61TR6yxCeLWqAB2/PeeMnWEP 1Ex322lkjOqfJiNEDsTg/pXc0419cnpOwWto+sMdUXbkG2moQH5opkN0wcM7Jn2puZ/v MnrQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=FVHTFluSha0AQSzv2ABK4QAsCYCIc/yOgt1qRmrDx08=; b=OfQIsx5dnhKgAU4lk8jol/PV5BcZ9yzT8zetDxIbc19AuAlUv7JB2Al+6VvuuFIr4k VjGQyi7e72EbjUrBauI+qc4fhbcY9ILqzIYgQEHFXw3Va1Q2ageFgDJweLStB4SNFaIY IM/lXAAtMRmy+DygazvIvKnMy02/MFzQHRTQ5+WEdfxRD3CWDSe0fsYyAiIhPoh7NtvZ /j1saoLEqqSAEkT9HcklxdlwyHKnF5oiCnHbVaz9jAm9OTT5rSdN0zOGxlg+veS4iZmv NrROAAVV/Fqk0xaRnGbvpxONmVOHUQYWTMecIZQnTS6UKNwILe4+Cdr+QObHzQlJJ758 CzcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=nyw7bxf2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b6si1275724edu.343.2020.09.28.13.04.18; Mon, 28 Sep 2020 13:04:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=nyw7bxf2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726655AbgI1UDM (ORCPT + 99 others); Mon, 28 Sep 2020 16:03:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726389AbgI1UDM (ORCPT ); Mon, 28 Sep 2020 16:03:12 -0400 Received: from mail-vs1-xe41.google.com (mail-vs1-xe41.google.com [IPv6:2607:f8b0:4864:20::e41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61558C061755 for ; Mon, 28 Sep 2020 13:03:12 -0700 (PDT) Received: by mail-vs1-xe41.google.com with SMTP id a16so1535083vsp.12 for ; Mon, 28 Sep 2020 13:03:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=FVHTFluSha0AQSzv2ABK4QAsCYCIc/yOgt1qRmrDx08=; b=nyw7bxf2RbT5J8k9zhdN9+g+3Iv4qFFqgw1efDnrOIi8mC91T0Is7QJTXwOXidhk44 g6nVa6SH2apB5kOnIFmUu6OmOsaE/f4gIuyG4JrmIsVPwYRRSSWSErb1mXfBuAuRckN7 du9gmAmAk8lVUZnkt+vPAv6M5TF4DGVU6zCY97Ofiyj7HqQ+fnnnHkA0LUDW9dG/LbY5 t4TI7UksGTKtkxxJc3IyDXohgVRZt7TuqrsEO/chh9VkhfVoDNShWbf93XuB6N/wgLYD eYtLLvkZeVxrIo6zvj/LcvsF9sAxTWUDAqbOhIcK2IHBQFjggBBXq8OfVwWjN8sN0pnD yUwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=FVHTFluSha0AQSzv2ABK4QAsCYCIc/yOgt1qRmrDx08=; b=d0whvz2538dfe1/EGUhRawvcKvMAjCEYLfo3SRVxJdPgwLIGHpwfFlXwyw2NZpJOLm Smv4Gql+alUo7aqEQDOORt5E0F9KH6zil+P29PuCHRwI10HF23PT24sR0BZPhgp37Woy zkm9ms66WOKsiC7viKPm8m1YfCy1chs3oKNjjkiZlEXgr9Ejj7GHq+F0NqDQfQO3Hz2+ Tk9NtPsPSKmWUfaum6cdjw/N6UvlyMwrgkLD7hLW4DogiQiI+IehHPBdA3IwloIlpk9c UPKCHQIzRk7MDOpHFDULHffLwgniXu0WIW7SOPiGr6/ggCfQiatbUpguHxsKDEf2FE1t NhAQ== X-Gm-Message-State: AOAM532HdBFUEZcgWnO2cJcFY5mF/v/FSJlGRxAitASe1ClGULkPSNh1 AxuHOJZzycYu1xEjLOUE3YCiRaE0i9ExBUEm1Vs7ZA== X-Received: by 2002:a67:80d2:: with SMTP id b201mr1027352vsd.12.1601323391431; Mon, 28 Sep 2020 13:03:11 -0700 (PDT) MIME-Version: 1.0 References: <20200820091537.490965042@linuxfoundation.org> <20200820091539.592290034@linuxfoundation.org> In-Reply-To: <20200820091539.592290034@linuxfoundation.org> From: Naresh Kamboju Date: Tue, 29 Sep 2020 01:32:59 +0530 Message-ID: Subject: Re: [PATCH 4.19 38/92] kprobes: Fix NULL pointer dereference at kprobe_ftrace_handler To: Greg Kroah-Hartman Cc: open list , linux- stable , Masami Hiramatsu , Muchun Song , Chengming Zhou , "Steven Rostedt (VMware)" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 20 Aug 2020 at 15:23, Greg Kroah-Hartman wrote: > > From: Muchun Song > > commit 0cb2f1372baa60af8456388a574af6133edd7d80 upstream. > > We found a case of kernel panic on our server. The stack trace is as > follows(omit some irrelevant information): > > BUG: kernel NULL pointer dereference, address: 0000000000000080 > RIP: 0010:kprobe_ftrace_handler+0x5e/0xe0 > RSP: 0018:ffffb512c6550998 EFLAGS: 00010282 > RAX: 0000000000000000 RBX: ffff8e9d16eea018 RCX: 0000000000000000 > RDX: ffffffffbe1179c0 RSI: ffffffffc0535564 RDI: ffffffffc0534ec0 > RBP: ffffffffc0534ec1 R08: ffff8e9d1bbb0f00 R09: 0000000000000004 > R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000000 > R13: ffff8e9d1f797060 R14: 000000000000bacc R15: ffff8e9ce13eca00 > CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > CR2: 0000000000000080 CR3: 00000008453d0005 CR4: 00000000003606e0 > DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 > DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 > Call Trace: > > ftrace_ops_assist_func+0x56/0xe0 > ftrace_call+0x5/0x34 > tcpa_statistic_send+0x5/0x130 [ttcp_engine] > > The tcpa_statistic_send is the function being kprobed. After analysis, > the root cause is that the fourth parameter regs of kprobe_ftrace_handler > is NULL. Why regs is NULL? We use the crash tool to analyze the kdump. > > crash> dis tcpa_statistic_send -r > : callq 0xffffffffbd8018c0 > > The tcpa_statistic_send calls ftrace_caller instead of ftrace_regs_caller= . > So it is reasonable that the fourth parameter regs of kprobe_ftrace_handl= er > is NULL. In theory, we should call the ftrace_regs_caller instead of the > ftrace_caller. After in-depth analysis, we found a reproducible path. > > Writing a simple kernel module which starts a periodic timer. The > timer's handler is named 'kprobe_test_timer_handler'. The module > name is kprobe_test.ko. > > 1) insmod kprobe_test.ko > 2) bpftrace -e 'kretprobe:kprobe_test_timer_handler {}' > 3) echo 0 > /proc/sys/kernel/ftrace_enabled > 4) rmmod kprobe_test > 5) stop step 2) kprobe > 6) insmod kprobe_test.ko > 7) bpftrace -e 'kretprobe:kprobe_test_timer_handler {}' > > We mark the kprobe as GONE but not disarm the kprobe in the step 4). > The step 5) also do not disarm the kprobe when unregister kprobe. So > we do not remove the ip from the filter. In this case, when the module > loads again in the step 6), we will replace the code to ftrace_caller > via the ftrace_module_enable(). When we register kprobe again, we will > not replace ftrace_caller to ftrace_regs_caller because the ftrace is > disabled in the step 3). So the step 7) will trigger kernel panic. Fix > this problem by disarming the kprobe when the module is going away. > > Link: https://lkml.kernel.org/r/20200728064536.24405-1-songmuchun@bytedan= ce.com > > Cc: stable@vger.kernel.org > Fixes: ae6aa16fdc16 ("kprobes: introduce ftrace based optimization") > Acked-by: Masami Hiramatsu > Signed-off-by: Muchun Song > Co-developed-by: Chengming Zhou > Signed-off-by: Chengming Zhou > Signed-off-by: Steven Rostedt (VMware) > Signed-off-by: Greg Kroah-Hartman stable rc branch 4.19 build warning on arm64. ../kernel/kprobes.c: In function =E2=80=98kill_kprobe=E2=80=99: ../kernel/kprobes.c:1070:33: warning: statement with no effect [-Wunused-va= lue] 1070 | #define disarm_kprobe_ftrace(p) (-ENODEV) | ^ ../kernel/kprobes.c:2090:3: note: in expansion of macro =E2=80=98disarm_kpr= obe_ftrace=E2=80=99 2090 | disarm_kprobe_ftrace(p); | ^~~~~~~~~~~~~~~~~~~~ > > --- > kernel/kprobes.c | 7 +++++++ > 1 file changed, 7 insertions(+) > > --- a/kernel/kprobes.c > +++ b/kernel/kprobes.c > @@ -2077,6 +2077,13 @@ static void kill_kprobe(struct kprobe *p > * the original probed function (which will be freed soon) any mo= re. > */ > arch_remove_kprobe(p); > + > + /* > + * The module is going away. We should disarm the kprobe which > + * is using ftrace. > + */ > + if (kprobe_ftrace(p)) > + disarm_kprobe_ftrace(p); > } > > /* Disable one kprobe */ > --=20 Linaro LKFT https://lkft.linaro.org