Received: by 2002:a05:6a10:8395:0:0:0:0 with SMTP id n21csp30017pxh; Tue, 9 Nov 2021 06:55:08 -0800 (PST) X-Google-Smtp-Source: ABdhPJwFAYbeomcHbyLtPWvG80EANbSmIa9DoCHtcQocMnOTrtn3W7VaK+HRA5DUElSRtbZYr5kd X-Received: by 2002:a17:906:af1a:: with SMTP id lx26mr10468366ejb.44.1636469707854; Tue, 09 Nov 2021 06:55:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1636469707; cv=none; d=google.com; s=arc-20160816; b=WVjRpMQj1dsO9aKMpC9Wg414Q8JE59REegEBzUi+ZFUcRZXFYktmugy53QFnxuw/YV ulYnWbPtRZ2kBbM+ZH3mX/9afZdmzDEoFkNel16LKyvKdjwIUZzfHbU4SNdEunmCx2Fx 5N1UZTr8unJ+3Oqg8Bc1aQzIYcSzcnCeJl023I775SrZ9ynuvV6Z7Vfb0b4zns4xT5OT wJDN9lQrrhEOzwZln4efiSBgd1WUrOgEU1BB31FxSObluRhBe3p5zBJ2NPM0KqI+CQ4S AnpUgcnzpv7mBTxGCrlvVuLFLmkhib9Fr+xpT3jm8FInPwvS1G94Lm2rbg9mqOhWF2Qm i8sg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=3CYQUrUcZzBOktjOhOc7ywBFO23bCaXu3jx+CJJnEwk=; b=DvZgGuD5M64xUp0UGJyK1QpmJlW1mn6rGgNMy0erTYtma4kx48STfK9QwCt8/QqR33 Nai4dGNwkKkynPVHgkUH1SKAX+s2jjACB5GMgyduy6rj6LOpPwfq+MLYtNwa6uNByq6y QlcP33hGcFqu7aB8qdXSE9bBC3EVWOIjVX0UKUjupJL6D63baXJYkeL/iER+XwUnkmBt 9EsLPbXakNUx+l+U+N7rH0cUC2HKKrMbofo6kr2yN49lgXntMoTFzjb9PFe/SlLTQTi6 fE0x6fuCGZ08+etxcqm1ldaSdvVErZqElBa5PLWNQZIHfqHVGtXfOVu9EQgP2a+ZEfkR eXmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=B19OhJJy; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h14si29959396eje.334.2021.11.09.06.54.42; Tue, 09 Nov 2021 06:55:07 -0800 (PST) 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=@google.com header.s=20210112 header.b=B19OhJJy; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243295AbhKIGwd (ORCPT + 99 others); Tue, 9 Nov 2021 01:52:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241000AbhKIGwb (ORCPT ); Tue, 9 Nov 2021 01:52:31 -0500 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E299C061764 for ; Mon, 8 Nov 2021 22:49:46 -0800 (PST) Received: by mail-pl1-x62c.google.com with SMTP id v20so19177114plo.7 for ; Mon, 08 Nov 2021 22:49:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=3CYQUrUcZzBOktjOhOc7ywBFO23bCaXu3jx+CJJnEwk=; b=B19OhJJy0VGivwYvFUuM+Q44BnrgO4mezKnX0wn5V3DrmRuTvd4+zwbT3Uy76ft+RN kADRoPnJDvdFBPgYgD8329wffk3q4ZAIAKqfH5FJaYPCvyVT9uzQViygUmYwMzbrzBvD M49I8gKXVMcFn0eGaC+dm6tkOBQXzfeElvT3+yjnLduXI76LSkeUwKdnTAH4wf/rG8Lq IkrUtCeKXMtbwx7PBUI2udUt0FGEjyjwk/5orT/eI4FPggLVKVccRny1deXGRQlFydSf ZmFud/old/iXxFF+EfpLTEJ0dYPvIfDAQheFlsOle5UWU0TfpIJVOvg01s8E3x7xSaOG 6ZPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=3CYQUrUcZzBOktjOhOc7ywBFO23bCaXu3jx+CJJnEwk=; b=iMKk26lyXW1yBv67GCKap7HdUoS6K5KUlXDUaFUbGQb9/AOWsHJXBfMWk+/MUWTci1 vR7AvrSGZkxDT704QDcd429Nr/izwWoF572nTBm6Prgj4tA4m5RWqpXJP6B8seUsZdtO bhUg57ZNRtole6HWakxfowja0QH2TclTIBme0vdQXW3WGlkRww2FUL11rmPm2YqnZbLH 465hDyZ8/IL2QnkrL2fiCMbUNVqrZK8VqVKQ0N/+T10gKY6/Hrm2jF3+oCirs+gbFrnS fS53Y9J3nmfgnzBm8KVjSvJXbUhHXjplGVLj68RY2wZZ90VOiskZNqPE/cLveL0ouVhp NCEw== X-Gm-Message-State: AOAM531ej+wWEwrPSEmxhmBTHQGp3WmgwHPjDWUP77DlK0LUHa1TElg4 lOTxgaxM/aHrmRJy2Xvsi9o89PgyyehT8RiBt5NbOg== X-Received: by 2002:a17:90a:ca81:: with SMTP id y1mr4569160pjt.231.1636440585497; Mon, 08 Nov 2021 22:49:45 -0800 (PST) MIME-Version: 1.0 References: <20211101083629.101241-1-tzungbi@google.com> In-Reply-To: <20211101083629.101241-1-tzungbi@google.com> From: Tzung-Bi Shih Date: Tue, 9 Nov 2021 14:49:34 +0800 Message-ID: Subject: Re: [PATCH v2] platform/chrome: cros_ec_debugfs: detach log reader wq from devm To: bleung@chromium.org, groeck@chromium.org, bleung@google.com, groeck@google.com Cc: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Benson and Guenter, On Mon, Nov 1, 2021 at 4:36 PM Tzung-Bi Shih wrote: > > Debugfs console_log uses devm memory (e.g. debug_info in > cros_ec_console_log_poll()). However, lifecycles of device and debugfs > are independent. An use-after-free issue is observed if userland > program operates the debugfs after the memory has been freed. > > The call trace: > do_raw_spin_lock > _raw_spin_lock_irqsave > remove_wait_queue > ep_unregister_pollwait > ep_remove > do_epoll_ctl > > A Python example to reproduce the issue: > ... import select > ... p = select.epoll() > ... f = open('/sys/kernel/debug/cros_scp/console_log') > ... p.register(f, select.POLLIN) > ... p.poll(1) > [(4, 1)] # 4=fd, 1=select.POLLIN > > [ shutdown cros_scp at the point ] > > ... p.poll(1) > [(4, 16)] # 4=fd, 16=select.POLLHUP > ... p.unregister(f) > > An use-after-free issue raises here. It called epoll_ctl with > EPOLL_CTL_DEL which in turn to use the workqueue in the devm (i.e. > log_wq). > > Detaches log reader's workqueue from devm to make sure it is persistent > even if the device has been removed. > > Signed-off-by: Tzung-Bi Shih > --- > As for 2 other related cases I could image: > > Case 1. userland program opens the debugfs after the device has been removed > > ENOENT. cros_ec_debugfs_remove() calls debugfs_remove_recursive(). > > Case 2. userland program is reading when the device is removing > > If the userland program is waiting in cros_ec_console_log_read(), the device > removal will wait for it. > > See the calling stack for the case: > wait_for_completion > __debugfs_file_removed > remove_one > simple_recursive_removal > debugfs_remove > cros_ec_debugfs_remove > platform_drv_remove > device_release_driver_internal > device_release_driver > bus_remove_device > device_del > platform_device_del > platform_device_unregister Would you mind to share your thoughts on the patch?