Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1009868rwr; Fri, 5 May 2023 08:00:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6ZUqDuXST1lMIcoAufRaArSSmSYUyTohDp5v5tFVIz/PgN4v8KnvAze1KkwZZr/Qsjr7Et X-Received: by 2002:a17:90a:6545:b0:249:64d2:4122 with SMTP id f5-20020a17090a654500b0024964d24122mr1819693pjs.21.1683298851794; Fri, 05 May 2023 08:00:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683298851; cv=none; d=google.com; s=arc-20160816; b=djZBPj8OcZ0Z47AfLuTfs51sab2C1jljEmFvz013g73OFN6uC8eqAocUlTY7JxkX/k aUXeVea3sX/baQBRtwN80qmO1CnY5mhrPiTIMdD0WYhEYargzZcRyTwvi5KLH0EmRCLK DUKBRf+e/6234vWiEjMsj2ipeH/wloC3VvV4l/b55jBJEmXrorLNkhLlJWLMsbDn5Yc6 K4di1O7BCol30Nhnwk/ce57uLS11xYWWjDMhZkPNgFlmpycyIlImYg8xQW+VuVTE3f8q EBOyy18hO5zoa34vzeV2Hax4nkgXpIMRxtmGcYaO7vExF0fvCw2c+UXObEj7uz/5W+10 FaAw== 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-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=wLT+oky3UYiByAIQzF3Q0f0P4NgxDCcPY4lStdu5XHo=; b=oK2WAu3FrnwUjmiTukAZ3ues7FBykngL+yal/zCps9pmQ/S3K6LTjHduYz0ES17WOJ 0kzD7tLBgLnocKgyFg/DRPTP/H7K+aGoU8XVdbUHMmqF3GKOVk/p3NNiKUuomDdPenz7 sVj7ycV+a208cRufQIbmPkDcwc8MYnMpWglx7eHR24c8kzbslqqlURF5VCyYPe5oYtVk B9Py05P247OtmNSG3HRi07KYaH8O7wvrXAo+pEF+vMhwbXETzNZ9q4lM2nXXG+ucf81Z j9aXdPlJJOLaJQpvVsbNjDl/HouKw6qyPQ3bs6Wh1AQZIRyC1vs7k4N5ep/E31cyB7G8 pedA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=MXNgQ22h; 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 t17-20020a17090ad15100b002369d39671fsi18188797pjw.160.2023.05.05.08.00.34; Fri, 05 May 2023 08:00:51 -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=MXNgQ22h; 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 S232525AbjEEOu3 (ORCPT + 99 others); Fri, 5 May 2023 10:50:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232580AbjEEOu2 (ORCPT ); Fri, 5 May 2023 10:50:28 -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 A67E01635E for ; Fri, 5 May 2023 07:50:24 -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 E67966348E for ; Fri, 5 May 2023 14:50:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EFE52C433EF; Fri, 5 May 2023 14:50:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1683298223; bh=dIDBpG6dhKNh4O8MDr9mOrIop26afePz8FQfz1RjU0o=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=MXNgQ22hNz/F6O3abYmNgNbgZaMH+72ul5lFb55gK+6PYXV0rUeqj0n6kJdTuk+Z2 muHlMecERZdf2yrwiPk7jyurKiTDsgQINO6owAQfMGY8TA10KdkGlPgDDnnZm8zf+R 28CDvHlGyPSGJ93iwZHOz3vgtFD7Xl8Yn2/hYGpwLK+pbMcHFh/OWJQ0q3HWKYkfxB UOxWo3XWAO+T9UiuUlVT+y2SZ692wsS7iuFpjL+mB594/xteG6hGR8DYA1gyeQwWYz SKayLRaJO06x585+4P7DLqDlzjlxOfQmRk6yt5Qx66sjvTPCR4JzHkHbrSrUSHlzaz mzW/XjPNR+7zg== Date: Fri, 5 May 2023 16:50:20 +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 02/20] posix-timers: Ensure timer ID search-loop limit is valid Message-ID: References: <20230425181827.219128101@linutronix.de> <20230425183312.932345089@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230425183312.932345089@linutronix.de> X-Spam-Status: No, score=-4.6 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 On Tue, Apr 25, 2023 at 08:48:58PM +0200, Thomas Gleixner wrote: > 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 the start condition can never observe the negative value > and annotate the read and the write with READ_ONCE()/WRITE_ONCE(). > > Reported-by: syzbot+5c54bd3eb218bb595aa9@syzkaller.appspotmail.com > Reported-by: Dmitry Vyukov > Signed-off-by: Thomas Gleixner > --- > include/linux/sched/signal.h | 2 +- > kernel/time/posix-timers.c | 30 +++++++++++++++++------------- > 2 files changed, 18 insertions(+), 14 deletions(-) > > --- a/include/linux/sched/signal.h > +++ b/include/linux/sched/signal.h > @@ -135,7 +135,7 @@ struct signal_struct { > #ifdef CONFIG_POSIX_TIMERS > > /* POSIX.1b Interval Timers */ > - int posix_timer_id; > + unsigned int next_posix_timer_id; > struct list_head posix_timers; > > /* ITIMER_REAL timer for the process */ > --- a/kernel/time/posix-timers.c > +++ b/kernel/time/posix-timers.c > @@ -140,25 +140,29 @@ static struct k_itimer *posix_timer_by_i > static int posix_timer_add(struct k_itimer *timer) > { > struct signal_struct *sig = current->signal; > - int first_free_id = sig->posix_timer_id; > struct hlist_head *head; > - int ret = -ENOENT; > + unsigned int start, id; > > - do { > + /* Can be written by a different task concurrently in the loop below */ > + start = READ_ONCE(sig->next_posix_timer_id); > + > + for (id = ~start; start != id; id++) { > spin_lock(&hash_lock); > - head = &posix_timers_hashtable[hash(sig, sig->posix_timer_id)]; > - if (!__posix_timers_find(head, sig, sig->posix_timer_id)) { > + id = sig->next_posix_timer_id; > + > + /* Write the next ID back. Clamp it to the positive space */ > + WRITE_ONCE(sig->next_posix_timer_id, (id + 1) & INT_MAX); Isn't that looping forever? Thanks.