Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp4846630rwb; Sun, 13 Nov 2022 15:25:15 -0800 (PST) X-Google-Smtp-Source: AA0mqf7zit3qo0Px1mCnwEqVPo8wtfft3ozvhRuAFR9C1f6CUr6ZkTZGFDq9EXL98nXhESA93SXP X-Received: by 2002:a17:906:fb92:b0:78d:42b8:2dc2 with SMTP id lr18-20020a170906fb9200b0078d42b82dc2mr8406397ejb.608.1668381915398; Sun, 13 Nov 2022 15:25:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668381915; cv=none; d=google.com; s=arc-20160816; b=yhzFGcjcyuDCjWE8W3W1Ad2EEOkUkpTJW0id9tU2EHFe+L0fRqPJZFvgZWZYb77EGO KtkVBW5/0qf+2gnRsL8xGq2GNC3X1fd/rXiziR6tMy0nLgSRiyPgLat25bnTjZkNEpFQ tqy7LBpOUiZPGc0bo+DJCtGf0pcqAerrRK+qNtyd8gpW0+qoLl7prmvp3JfHTR9uYbCo 2P9R2ethCbXROykqLTgec6jCPKKEqadb10vnES41XRpgSVr+KvttZbpRFhjcLM2XnPs4 CO6CDA/FGtWLGe6rWyf4KgJhqr2uC7nEEdTOce+WGjsC7xXATTJY8YjghGCdSrcmENAi jTDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:dkim-signature:dkim-signature:from; bh=GElb0Q0Spb4WXlQ7HhaogZlg9YgU+muEVwXfBjt/Fn0=; b=e1R6jrOnSJxfnxWykBb4lUv/FFbhuH3hri2obhWzUl1xaSWrBHqPUXW+aV5UanDs8V lLkP58Wcxmybj964IWPpzn0A2eVwL1n9wg2WVr0dSMAAmhouBe6W9hCVSOT57ZqmbIdp xHGUkLBigg6yZMLUz2cDiophimkTQvoSdPFq43gkwFNDm8uONJKtNk2bJOiJ2epsqf8v jtfQxZH/z3k7/pS97WUQCm1wXkvjqGQlvpv7mt65BJHzmRaFmahm+L22M2XukV28CzA1 LpxaYFNDmDXrGG0zzLNC4PLYu64mUx45YOh3KHja7+wtl2lHyAQbKb/VNGBup6CknOny zuOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=DsozGNCk; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=ZWzgVCeh; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e7-20020a50fb87000000b0046272b487cesi6807924edq.331.2022.11.13.15.24.53; Sun, 13 Nov 2022 15:25:15 -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=@linutronix.de header.s=2020 header.b=DsozGNCk; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=ZWzgVCeh; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235377AbiKMXSc (ORCPT + 90 others); Sun, 13 Nov 2022 18:18:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233793AbiKMXS2 (ORCPT ); Sun, 13 Nov 2022 18:18:28 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EECA5DD6 for ; Sun, 13 Nov 2022 15:18:24 -0800 (PST) From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1668381501; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=GElb0Q0Spb4WXlQ7HhaogZlg9YgU+muEVwXfBjt/Fn0=; b=DsozGNCkHgOuv5fD8ayb+j1qcJiOR9HY1MZFEQNQr4iof8vFTt7L6MXtYqWWOPqTDbWV52 UscAjtSYDxsUTsGQDoCT4gWIKzrixvgEFE2D5vg/bHzkucp0/C4Gmq7sLKBx+OaN42XT56 R2bTd2y3SBJhx6MJ/ifnjqqP5r8Fsh+YnC/mDWjw0wZCViQXwr5LhSR6Xy00YgQNnMCD4E RNBJExgTz3+6zUKRyuS4pue2BRFAqyxIZzJgmbz4hMxspb1RLHCGZlpvCAYRXHo6frcVjd 5uzR6Ab1kSTSmjoc56ksVO+ZPbmEo+8MJUCBev1lCes01YCsX8NYqB+Bf6NRhw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1668381501; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=GElb0Q0Spb4WXlQ7HhaogZlg9YgU+muEVwXfBjt/Fn0=; b=ZWzgVCehYsrjL8a85j1mJrGJr53ogyusZqqEjwZL9imdmPwSAWqkMYb2B654SQ6SF90Lna R0pxBOEYEKJMm6Bw== To: Steven Rostedt , linux-kernel@vger.kernel.org Cc: Linus Torvalds , Stephen Boyd , Guenter Roeck , Anna-Maria Gleixner , Andrew Morton , Julia Lawall Subject: Re: [PATCH v6 4/6] timers: Add timer_shutdown_sync() to be called before freeing timers In-Reply-To: <20221110064147.343514404@goodmis.org> References: <20221110064101.429013735@goodmis.org> <20221110064147.343514404@goodmis.org> Date: Mon, 14 Nov 2022 00:18:21 +0100 Message-ID: <875yfitpdu.ffs@tglx> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS autolearn=ham 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 Thu, Nov 10 2022 at 01:41, Steven Rostedt wrote: > +static inline int timer_shutdown_sync(struct timer_list *timer) > +{ > + return __del_timer_sync(timer, true); > +} > +static int __try_to_del_timer_sync(struct timer_list *timer, bool free) > { > struct timer_base *base; > unsigned long flags; > @@ -1285,11 +1281,25 @@ int try_to_del_timer_sync(struct timer_list *timer) > > if (base->running_timer != timer) > ret = detach_if_pending(timer, base, true); > + if (free) > + timer->function = NULL; Same problem as in the timer_shutdown() case just more subtle: CPU0 CPU1 lock_timer(timer); base->running_timer = timer; fn = timer->function; unlock_timer(timer); fn(timer) { __try_to_del_timer_sync(timer, free=true) lock_timer(timer); if (base->running_timer != timer) // Not taken if (free) mod_timer(timer); if (WARN_ON_ONCE(!timer->function)) return; // not taken timer->function = NULL; unlock_timer(timer); lock_timer(timer); enqueue_timer(timer); unlock_timer(timer); } //timer expires lock_timer(timer); fn = timer->function; unlock_timer(timer); fn(timer); <--- NULL pointer dereference You surely have spent a massive amount of analysis on this! Can you please explain how you came up with the brilliant idea of asking Linus to pull this post -rc4 without a review from the timer maintainers or anyone else who understands concurrency? If we really want to make this work, then this needs at least a sanity check of timer->function in the mod/add*_timer() path _after_ locking the timer. Though I'm not convinced that this would really be cutting it simply because the circular dependencies of timer scheduling work and work arming timer is as demonstrated above not as trivial as you might think. In the worst case the concurrent code path might still end up in a UAF as far as I can tell. But what's worse is that you try to create the illusion that timer_shutdown_sync() is actually preventing people from shooting themself into their feet. As implemented right now it's just a bandaid which makes it less likely, but does neither prevent any of the hard to debug shutdown issues nor the resulting holes in peoples feets. Thanks, tglx