Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp4369408imw; Tue, 19 Jul 2022 05:29:53 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vdzM7jfTYxiEI6HNJwT88rvrmeGs+emTYqC9873HPXmPNJfi8rfPGHAV3wlS/th+6Ul+C5 X-Received: by 2002:a05:6402:d0a:b0:437:f9a1:8493 with SMTP id eb10-20020a0564020d0a00b00437f9a18493mr43652463edb.226.1658233793628; Tue, 19 Jul 2022 05:29:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658233793; cv=none; d=google.com; s=arc-20160816; b=GFKOtgcohinA7kN2a4zbHjp6UCN2ZhvAwi5yxFR7WOe1strnLyyERtS58XzH6ErAis QZ0Z622CjpAcY1LYdlKqQnXfE7n3uI9VOm9UaS6ipKQJyPcKOWgnq+6udf7Cx2JGEuEt Ix+hlhVmozUCd/VnRKJ1WwFbbhG+seufIWF0hII7ubKTu80yWDowvT0wN3fZ1Jv1sHwV tX8KhxEvOosVTR8OV7x5nh+6B1cJgBAstXhUfl491+LaSusQ9d3Z26GdW8A1kfZgozTJ iZ4xe8q80QgEZtDDQYNnbFC347Inu6rDclU4ozgM36Imtl3V+A3VWxT9f+maq5/kKICU fG1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=GvHVt5WP6Pj1dNtZxbhe2JWC/ZUu4pNpMdZGRGJuCjo=; b=EbpenD60vi0mBF1Rvxu49BKQSvRtFhp8h8Fl+l/3YxUVvjxxVOrcp7u2GaOK2bgiiw 6YFO2TCiheISRqfRD4kAqOs64tskVLYDsFj4wsy5G5n3oZShe7eSGV0JLL5DdUe667p8 NHPUMbo2KkcWVeyj/UZcW+j9CTQmcqsuuSGews2hMyomERZxKTPxohey+KJlO2STcKWc Eu+XRmTIblEOVcVCEnqPWQBh2S1VUOWPzv29Hr4OGduP/o1hcgem0v7b8ejeiqSiKn4i MkTpW9vNgc3YyQlYL0lnH50GvEyJfTZN576RTqrAye35S8T/w2TGsexjfrfTFBwe6lpb 5f+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WSbRSQfs; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 6-20020a508746000000b0043adeeba898si17820093edv.283.2022.07.19.05.29.28; Tue, 19 Jul 2022 05:29:53 -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=@linuxfoundation.org header.s=korg header.b=WSbRSQfs; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239352AbiGSMR5 (ORCPT + 99 others); Tue, 19 Jul 2022 08:17:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237545AbiGSMPT (ORCPT ); Tue, 19 Jul 2022 08:15:19 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C02BB54656; Tue, 19 Jul 2022 05:06:01 -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 ams.source.kernel.org (Postfix) with ESMTPS id 49011B81B32; Tue, 19 Jul 2022 12:05:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 94DAFC341C6; Tue, 19 Jul 2022 12:05:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1658232334; bh=TaXABxc8N8pbG30PP50/ekto0HoZyuiPfVaoSoQDmSQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WSbRSQfsri5XPaYT26TGpoy0n9vFLPTyyquGKVeO7QUeYoEGt816baF+XzIvcRzVX YIajbC0aitgOWCxpDqNS4ONJGJcDt5FzAMOrWOM6HteTP8fpZ+dz7MLTu37eg6K6Ug mbJXif+OtDhO8jTeDXV7LEN5EklRNZk6fdlWr/nw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, chris@accessvector.net, Oleg Nesterov , Linus Torvalds Subject: [PATCH 5.10 008/112] fix race between exit_itimers() and /proc/pid/timers Date: Tue, 19 Jul 2022 13:53:01 +0200 Message-Id: <20220719114626.799742622@linuxfoundation.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220719114626.156073229@linuxfoundation.org> References: <20220719114626.156073229@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 From: Oleg Nesterov commit d5b36a4dbd06c5e8e36ca8ccc552f679069e2946 upstream. As Chris explains, the comment above exit_itimers() is not correct, we can race with proc_timers_seq_ops. Change exit_itimers() to clear signal->posix_timers with ->siglock held. Cc: Reported-by: chris@accessvector.net Signed-off-by: Oleg Nesterov Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- fs/exec.c | 2 +- include/linux/sched/task.h | 2 +- kernel/exit.c | 2 +- kernel/time/posix-timers.c | 19 ++++++++++++++----- 4 files changed, 17 insertions(+), 8 deletions(-) --- a/fs/exec.c +++ b/fs/exec.c @@ -1286,7 +1286,7 @@ int begin_new_exec(struct linux_binprm * bprm->mm = NULL; #ifdef CONFIG_POSIX_TIMERS - exit_itimers(me->signal); + exit_itimers(me); flush_itimer_signals(); #endif --- a/include/linux/sched/task.h +++ b/include/linux/sched/task.h @@ -82,7 +82,7 @@ static inline void exit_thread(struct ta extern void do_group_exit(int); extern void exit_files(struct task_struct *); -extern void exit_itimers(struct signal_struct *); +extern void exit_itimers(struct task_struct *); extern pid_t kernel_clone(struct kernel_clone_args *kargs); struct task_struct *fork_idle(int); --- a/kernel/exit.c +++ b/kernel/exit.c @@ -782,7 +782,7 @@ void __noreturn do_exit(long code) #ifdef CONFIG_POSIX_TIMERS hrtimer_cancel(&tsk->signal->real_timer); - exit_itimers(tsk->signal); + exit_itimers(tsk); #endif if (tsk->mm) setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm); --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -1051,15 +1051,24 @@ retry_delete: } /* - * This is called by do_exit or de_thread, only when there are no more - * references to the shared signal_struct. + * This is called by do_exit or de_thread, only when nobody else can + * modify the signal->posix_timers list. Yet we need sighand->siglock + * to prevent the race with /proc/pid/timers. */ -void exit_itimers(struct signal_struct *sig) +void exit_itimers(struct task_struct *tsk) { + struct list_head timers; struct k_itimer *tmr; - while (!list_empty(&sig->posix_timers)) { - tmr = list_entry(sig->posix_timers.next, struct k_itimer, list); + if (list_empty(&tsk->signal->posix_timers)) + return; + + spin_lock_irq(&tsk->sighand->siglock); + list_replace_init(&tsk->signal->posix_timers, &timers); + spin_unlock_irq(&tsk->sighand->siglock); + + while (!list_empty(&timers)) { + tmr = list_first_entry(&timers, struct k_itimer, list); itimer_delete(tmr); } }