Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp5704178rwd; Mon, 5 Jun 2023 07:29:15 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6ICN56SjP6q8c6zgZl1PcRkvMxf3PoiOR5pdQdSB9qjKSpFo99GfYzhULqRzNA0XW4TEIM X-Received: by 2002:a17:902:eccb:b0:1b0:5425:216f with SMTP id a11-20020a170902eccb00b001b05425216fmr3804706plh.34.1685975355359; Mon, 05 Jun 2023 07:29:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685975355; cv=none; d=google.com; s=arc-20160816; b=pW21u7bFXh9aw/jD1sXWLRg9L9lXvuWEKhNbiCGy7ov4XrFjeI/C3XIT1LFgLhdDoH 6kN00gEORm/dgZEYp3EUpa8u4SMqFDI+zfchL80YUF6eYvM7wFE1LMIGcVN4iv1Lhm6+ BM/ByTWCx4JHhG3vWG4tE16ZvJWj1sqlVIGx8esgG97FrldF6vQhqt5oxGCOq/oB3qD9 VO2i490MgCRjPFRzdYMKty9bIapy/ugxybPZsEk1Cu9QpZq4gE883fDGU62eGLSNPBTM LvfmJCFt2uPg4wW3/cKPmOTmeLmcJQMGiU/gQHRNYw0xFL+yhw0vNHLDo9ldEVGve3cT IadA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=TcBDBFDQ3L4FR22bsNuNzJ2kP4lWCdL/8ZDNuPTFy8s=; b=QUW0gEvqZBM7p8PXRx9MazX8mSH7CFD6d/jGcuXx2cfyunbwVQt2CmJN72Qw19QTRV D9kksny1wWwo+ULeP3SYfVw88LzWxE2uhXg0f3klqWfK+0LuODibxLyaNmRp149Xl+iC 2wEnLxwi1WUZnKj7cw4VH/ENqOFxjPqwHsGJ54NbeJqmF1n6RPYdJK0B/s5DmTlEPjsQ E8MLFdqoqXFsqRmXdZfHB3QnU5EGY+vazZr/PhHlF3NfNxyTN7ehfL4t3RKJoOjffxu4 uwhE9mtIpJn2mTYZOa1HoofehJRkemveNAAjq5Ik1CvznHOoE+GILtCNzt8AEuBB3F0b /dug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=TVwrJXBA; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jn15-20020a170903050f00b001ae014d8d03si5461966plb.432.2023.06.05.07.29.03; Mon, 05 Jun 2023 07:29:15 -0700 (PDT) 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=@kernel.org header.s=k20201202 header.b=TVwrJXBA; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231978AbjFEORa (ORCPT + 99 others); Mon, 5 Jun 2023 10:17:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234312AbjFEORR (ORCPT ); Mon, 5 Jun 2023 10:17:17 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B291AF9 for ; Mon, 5 Jun 2023 07:17:10 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4B8EC621D8 for ; Mon, 5 Jun 2023 14:17:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3E5E1C433D2; Mon, 5 Jun 2023 14:17:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685974627; bh=EjKVzHEQE6AFEn44fePw694F4A8RUgP5HVlndyzHc24=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=TVwrJXBAr1d8PjsQQMfP40s6ci3Jfo0A0cP0TJ1GtlDl1qQQSjUu3RoRGE8o7QnAY w5klcNEXW2zyHar/0nU/6h3syLB9bNHXQy8sbMhP21bVwE/ShWRVzbiBTyITQnWjJg 81E13a3eyb4VW4FrX7xdIdogzodmZ3uRbGwPLM6U1KhUll8oX8J0ebvSymOpj0YILX 9POtIPnBivwwbrlxKE0Y9j3JdQRcS+OR7+zGlIXoHu0XqnFjOOZcuEX2LGEzqTsgu2 x0ARjrCjOmwck6Qr5mxtYBcFsnixEtw6nCJhSEsEspAPh8NVXPhCGrFZymYR4SiU8x zuwvC73TgHV6w== Date: Mon, 5 Jun 2023 16:17:04 +0200 From: Frederic Weisbecker To: Thomas Gleixner Cc: LKML , Anna-Maria Behnsen , Peter Zijlstra , syzbot+5c54bd3eb218bb595aa9@syzkaller.appspotmail.com, Dmitry Vyukov , Sebastian Siewior , Michael Kerrisk Subject: Re: [patch v2 02/20] posix-timers: Ensure timer ID search-loop limit is valid Message-ID: References: <20230425181827.219128101@linutronix.de> <20230425183312.932345089@linutronix.de> <87zg6i2xn3.ffs@tglx> <87v8h62vwp.ffs@tglx> <877cth1xyd.ffs@tglx> <87bkhzdn6g.ffs@tglx> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <87bkhzdn6g.ffs@tglx> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 Le Thu, Jun 01, 2023 at 08:58:47PM +0200, Thomas Gleixner a ?crit : > posix_timer_add() tries to allocate a posix timer ID by starting from the > cached ID which was stored by the last successful allocation. > > This is done in a loop searching the ID space for a free slot one by > one. The loop has to terminate when the search wrapped around to the > starting point. > > But that's racy vs. establishing the starting point. That is read out > lockless, which leads to the following problem: > > CPU0 CPU1 > posix_timer_add() > start = sig->posix_timer_id; > lock(hash_lock); > ... posix_timer_add() > if (++sig->posix_timer_id < 0) > start = sig->posix_timer_id; > sig->posix_timer_id = 0; > > So CPU1 can observe a negative start value, i.e. -1, and the loop break > never happens because the condition can never be true: > > if (sig->posix_timer_id == start) > break; > > While this is unlikely to ever turn into an endless loop as the ID space is > huge (INT_MAX), the racy read of the start value caught the attention of > KCSAN and Dmitry unearthed that incorrectness. > > Rewrite it so that all id operations are under the hash lock. > > Reported-by: syzbot+5c54bd3eb218bb595aa9@syzkaller.appspotmail.com > Reported-by: Dmitry Vyukov > Signed-off-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker