Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp5008218rwj; Tue, 20 Dec 2022 18:57:34 -0800 (PST) X-Google-Smtp-Source: AA0mqf5zV+wG89+jNCuvE9S3/fwY941Iv6Eot8Ldyrk7fTUaMO2Q3HKmWzzVLurtCVH12h3jPrSe X-Received: by 2002:a17:907:2072:b0:7c1:5ee1:4c57 with SMTP id qp18-20020a170907207200b007c15ee14c57mr32271945ejb.8.1671591454558; Tue, 20 Dec 2022 18:57:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671591454; cv=none; d=google.com; s=arc-20160816; b=IMzv+6R1Hyidza7AW08gZQfVJ7++0BO+XoMMiSkenZ4ZlkhR25jj7+X1roGZBwv7gV dM7kHYGSnMrzZU3daRE5YYZUrcPFMGRH8o155WSurSTXOydWuJm+BRj2Hff410I4QpTf LKg83RZS/A1qYTZYXvdwunkOKYz5DUTcWJv1bI9/3Uk08YelT4OcnXYbjMtQ/4U4pMMB uxTLRBaeiLPDa/tgwKwpyFOmWa+daxQiOPnjP3IiRYwRbqQKtnSaFGHXLO3k2QV2s7fD EKO+cA6ZI26GRCYNFaNLB2s0tqs5pTO/dd3tIYb4HcUP5MRfpXu0bV0GAQRmLIvkb5GD Dc6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:from:dkim-signature; bh=OStSd32YplqutNgXvFahsrXJGNlcVFLY4LscMZH0nv0=; b=S+4NGUnSaDyycRBHh058i1XdoRaMDfEi34Dvy0blFzvlG/9iThaa7rwCrtEtdeQQUU xpSjHxKNxkq/2EBIHSAOWTKtormH7VFlpoM3dx15NqFB8HMR+8Xyfmy1U+N4x7/q5SoU roI6tENIQdLXGjIL+3R7r8fwIGTSe7rLNnqFKGuJyoKyT+JHrLTa9Y4sDC9HShgLwoyH 7JbCk6Pduf838YfQe+cRsa6tMfkK3wG0Wg317Uv/BQ/Vp5lIpyoGB4LLJjsrL/rwUTHN 6XPLw/8IKfLW4QDsLo5i8Ndt2VKUaliIAwf4AknrLF5N9l4WLnM5lTBKmAEN7ypoWhfJ 1Mew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=sN1cz4J6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gb30-20020a170907961e00b007bc5528a4d7si14824266ejc.47.2022.12.20.18.57.17; Tue, 20 Dec 2022 18:57:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=sN1cz4J6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234272AbiLUCn0 (ORCPT + 69 others); Tue, 20 Dec 2022 21:43:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234260AbiLUCnT (ORCPT ); Tue, 20 Dec 2022 21:43:19 -0500 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB1BC1D300 for ; Tue, 20 Dec 2022 18:42:52 -0800 (PST) Received: by mail-pj1-x102a.google.com with SMTP id q17-20020a17090aa01100b002194cba32e9so757491pjp.1 for ; Tue, 20 Dec 2022 18:42:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:in-reply-to:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=OStSd32YplqutNgXvFahsrXJGNlcVFLY4LscMZH0nv0=; b=sN1cz4J6eT7EFe157PmafvjLNvNPsz4rEiWWNtSqYpqGQvtGm8KN5CnOv7jf6YKehs p6OmOUv53NIQgxFBpS0urwKIseyuGyzk/SW1jONEO7T/y+p1gM04ntOt1iRhemzw6gWU 8iFxe74N3zHDbbhULKnsXdBY7WGHFAjTmEWqabZwbBmoC6V3XZXhE18Oop3JWJaGiuKh +4zfj5TZ88e2lQToLPpZ8Dd1L8DhIcj7xZ0/R+te0D1MzFhRyvYeGzHwstPKJWJDGTbH ilE46U3zMaBACcbFGDzGiQbjxBnYclFHhvF4g+Dp37AKjcyhrIyXi9dZtDAL2ScsyZoK PTMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OStSd32YplqutNgXvFahsrXJGNlcVFLY4LscMZH0nv0=; b=k1Nk3uQBR++EXPingyVVKd0IFl820RmbGJl0Nl4ylClQ6Regp52R/G0bVSd7XDUmE1 8lyfIjJvbXYFzGjqNMEht6aXzUM9yUd6dLrSq+BWQNcOrZmxdOk7H5r6VyOfj+VLM4Ek Gq3N1p5OCONMMKZJ/zdcYzHGGuzKHvO1phMYBSxIVXgYtd9EWKe5WIpejhmEHxgs6kpE iGgHS2PLJecOv5SVoJUkgBnDDgrYSK0WiU0Wqz6C87hZqSS5dzRGL1EbPGJMi5gMEzwn UxIzMK6yrEf/n7SSHPAY+K+PXRuC+NEftUesAinSKcfUIsGHrLUuwZPDdGNKeUpEiHxu 0AGQ== X-Gm-Message-State: AFqh2kpS+/mcO7v0iuMsP/Fj1TK01UgGg5+POQEv8aIenjzxeevIMnda 4Rv6R6Uzq+WCxDWsNL+aRoJj5Q== X-Received: by 2002:a05:6a20:d39a:b0:9d:efbf:6618 with SMTP id iq26-20020a056a20d39a00b0009defbf6618mr605811pzb.38.1671590572269; Tue, 20 Dec 2022 18:42:52 -0800 (PST) Received: from ?IPV6:2409:8a28:e63:2da0:b0e9:bb47:3cf6:6095? ([2409:8a28:e63:2da0:b0e9:bb47:3cf6:6095]) by smtp.gmail.com with ESMTPSA id h10-20020a62830a000000b00574ffc5976fsm9354886pfe.159.2022.12.20.18.42.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 20 Dec 2022 18:42:51 -0800 (PST) From: Chengming Zhou X-Google-Original-From: Chengming Zhou Message-ID: <3a5a4738-2868-8f2f-f8b2-a28c10fbe25b@linux.dev> Date: Wed, 21 Dec 2022 10:42:39 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.6.0 Subject: Re: [syzbot] KASAN: use-after-free Read in put_pmu_ctx Content-Language: en-US To: Peter Zijlstra , syzbot Cc: acme@kernel.org, alexander.shishkin@linux.intel.com, bpf@vger.kernel.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mark.rutland@arm.com, mingo@redhat.com, namhyung@kernel.org, netdev@vger.kernel.org, syzkaller-bugs@googlegroups.com References: <000000000000a20a2e05f029c577@google.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2022/12/19 22:40, Peter Zijlstra wrote: > On Mon, Dec 19, 2022 at 12:04:43AM -0800, syzbot wrote: >> Hello, >> >> syzbot found the following issue on: >> >> HEAD commit: 13e3c7793e2f Merge tag 'for-netdev' of https://git.kernel... >> git tree: bpf >> console+strace: https://syzkaller.appspot.com/x/log.txt?x=177df7e0480000 >> kernel config: https://syzkaller.appspot.com/x/.config?x=b0e91ad4b5f69c47 >> dashboard link: https://syzkaller.appspot.com/bug?extid=b8e8c01c8ade4fe6e48f >> compiler: gcc (Debian 10.2.1-6) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2 >> syz repro: https://syzkaller.appspot.com/x/repro.syz?x=15e87100480000 >> C reproducer: https://syzkaller.appspot.com/x/repro.c?x=16ceeb13880000 >> >> Downloadable assets: >> disk image: https://storage.googleapis.com/syzbot-assets/373a99daa295/disk-13e3c779.raw.xz >> vmlinux: https://storage.googleapis.com/syzbot-assets/7fa71ed0fe17/vmlinux-13e3c779.xz >> kernel image: https://storage.googleapis.com/syzbot-assets/2842ad5c698b/bzImage-13e3c779.xz >> >> IMPORTANT: if you fix the issue, please add the following tag to the commit: >> Reported-by: syzbot+b8e8c01c8ade4fe6e48f@syzkaller.appspotmail.com >> >> ================================================================== >> BUG: KASAN: use-after-free in __lock_acquire+0x3ee7/0x56d0 kernel/locking/lockdep.c:4925 >> Read of size 8 at addr ffff8880237d6018 by task syz-executor287/8300 >> >> CPU: 0 PID: 8300 Comm: syz-executor287 Not tainted 6.1.0-syzkaller-09661-g13e3c7793e2f #0 >> Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/26/2022 >> Call Trace: >> >> __dump_stack lib/dump_stack.c:88 [inline] >> dump_stack_lvl+0xd1/0x138 lib/dump_stack.c:106 >> print_address_description mm/kasan/report.c:284 [inline] >> print_report+0x15e/0x45d mm/kasan/report.c:395 >> kasan_report+0xbf/0x1f0 mm/kasan/report.c:495 >> __lock_acquire+0x3ee7/0x56d0 kernel/locking/lockdep.c:4925 >> lock_acquire kernel/locking/lockdep.c:5668 [inline] >> lock_acquire+0x1e3/0x630 kernel/locking/lockdep.c:5633 >> __raw_spin_lock_irqsave include/linux/spinlock_api_smp.h:110 [inline] >> _raw_spin_lock_irqsave+0x3d/0x60 kernel/locking/spinlock.c:162 >> put_pmu_ctx kernel/events/core.c:4913 [inline] >> put_pmu_ctx+0xad/0x390 kernel/events/core.c:4893 >> _free_event+0x3c5/0x13d0 kernel/events/core.c:5196 >> free_event+0x58/0xc0 kernel/events/core.c:5224 >> __do_sys_perf_event_open+0x66d/0x2980 kernel/events/core.c:12701 >> do_syscall_x64 arch/x86/entry/common.c:50 [inline] >> do_syscall_64+0x39/0xb0 arch/x86/entry/common.c:80 >> entry_SYSCALL_64_after_hwframe+0x63/0xcd > > Does this help? > > diff --git a/kernel/events/core.c b/kernel/events/core.c > index e47914ac8732..bbff551783e1 100644 > --- a/kernel/events/core.c > +++ b/kernel/events/core.c > @@ -12689,7 +12689,8 @@ SYSCALL_DEFINE5(perf_event_open, > return event_fd; > > err_context: > - /* event->pmu_ctx freed by free_event() */ > + put_pmu_ctx(event->pmu_ctx); > + event->pmu_ctx = NULL; /* _free_event() */ > err_locked: > mutex_unlock(&ctx->mutex); > perf_unpin_context(ctx); Tested-by: Chengming Zhou While reviewing the code, I found perf_event_create_kernel_counter() has the similar problem in the "err_pmu_ctx" error handling path: CPU0 CPU1 perf_event_create_kernel_counter() // inc ctx refcnt find_get_context(task, event) (1) // inc pmu_ctx refcnt pmu_ctx = find_get_pmu_context() event->pmu_ctx = pmu_ctx ... goto err_pmu_ctx: // dec pmu_ctx refcnt put_pmu_ctx(pmu_ctx) (2) mutex_unlock(&ctx->mutex) // dec ctx refcnt put_ctx(ctx) perf_event_exit_task_context() mutex_lock() mutex_unlock() // last refcnt put put_ctx() free_event(event) if (event->pmu_ctx) // True put_pmu_ctx() (3) // will access freed pmu_ctx or ctx if (event->ctx) // False put_ctx() (3) has UAF problem since the pmu_ctx maybe freed in (2), so also should have "event->pmu_ctx = NULL;" in (2).