Received: by 2002:a05:7412:8d09:b0:fa:4c10:6cad with SMTP id bj9csp514436rdb; Tue, 16 Jan 2024 07:23:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IHVNF1MKDgkWeg56QKD3iOYtH4s8B38lLFo/xXuU6q2R2seD9bQnrPIQu1yPrOqh7UAzNZD X-Received: by 2002:aa7:d589:0:b0:559:b25d:22b6 with SMTP id r9-20020aa7d589000000b00559b25d22b6mr248548edq.43.1705418636351; Tue, 16 Jan 2024 07:23:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705418636; cv=none; d=google.com; s=arc-20160816; b=tNFWNMERvyA+bUlD0Oh2VAPqBqyfm1PHzI8AJmRsQnTLtejUHtd5RJuSSledWpizQO LZ0x+uG6VehtR7amGROz93ltkc1U8rH2Dv9EIY76sWQgMuOUOyeA5F112mCJO9p6+qMO 4wbB5iycoZ+FYyAFXCCjzydck+sOCbF67NRLoXXbmbPw/X7YxwvbzYOca6KdV6Gxx4rV bBHA5h61TMchCI7kaHcV1fSf7JjL+PKgUJhyH4bQ5BrPP9YHlcEirzfaq4jL3r/dfl/R luUj3I4uUOKWyDocjWiYma238uNtVJOx14HaMBfp3y9IUChGYqcky3+j2gP5/T+2goAj JPmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:in-reply-to:content-disposition:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:references :message-id:subject:cc:to:from:date:dkim-signature; bh=95o70husdyrbHX9ZG97EzcVPleiqbXjcOXKaS79W3CE=; fh=WbiPWVJdPw8Cr4myMGvuemWCU0jnwfwiSx8atqjaKmE=; b=R8ZVBpF/uqwwaL95A3wVEICedEaEvbOfPoGZ9v1mKHHtKefUoOoX4Q5CJPn/jnJquR YVsfoj+MctRRDXIg9ylW1t9cFRLsZLaE0rmJHCDTIkfsiaf+aK/4AfB0x7+LrXDi30w0 5w0VrmQMtWL8w5fOKofZjvnhJu+IscWqjF/2zIqV2brcLBioCbYkIrficW3jO4jTYEY0 N3BzKG0R2cK/iSs0F6tIEVjJdXfGKNHSVZVtM125K3glutiGKdrkOKZH1w0dBhjbVMr1 rQzqhci8VT7Xs5ksaW1qZh5H/wodBZug5m1Ymbc+Pq+q5x+gaM/hKewtQGMa26vMQ9y3 5NIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dGxcMImc; spf=pass (google.com: domain of linux-kernel+bounces-27512-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-27512-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id i26-20020a0564020f1a00b00558d40f35acsi4208574eda.639.2024.01.16.07.23.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Jan 2024 07:23:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-27512-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dGxcMImc; spf=pass (google.com: domain of linux-kernel+bounces-27512-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-27512-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id D64181F2338D for ; Tue, 16 Jan 2024 15:23:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 32EC81C28C; Tue, 16 Jan 2024 15:23:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="dGxcMImc" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CE00F1BF43 for ; Tue, 16 Jan 2024 15:23:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705418622; 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=95o70husdyrbHX9ZG97EzcVPleiqbXjcOXKaS79W3CE=; b=dGxcMImcUSpQe/Bc32/ghp1h3btPsgM27AO8cx5TLyxjgT0m8Deu9P+AlnIEwMBCNwa4I5 YE/kBQawmxMQiaftk+nfghLFK08KPkZihKvsJOHY8Z5A08OogTtjBE+I2mORb9ZtYLvfSP avBPu7IDGGcO4ejspla7kuZs5qPu2Wk= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-313-k8Dnsa1DNG-rwjU6HiPbdA-1; Tue, 16 Jan 2024 10:23:37 -0500 X-MC-Unique: k8Dnsa1DNG-rwjU6HiPbdA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B08983C29A72; Tue, 16 Jan 2024 15:23:32 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.224.96]) by smtp.corp.redhat.com (Postfix) with SMTP id 0014F2026D6F; Tue, 16 Jan 2024 15:23:23 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Tue, 16 Jan 2024 16:22:20 +0100 (CET) Date: Tue, 16 Jan 2024 16:22:11 +0100 From: Oleg Nesterov To: Bernd Edlinger Cc: Alexander Viro , Alexey Dobriyan , Kees Cook , Andy Lutomirski , Will Drewry , Christian Brauner , Andrew Morton , Michal Hocko , Serge Hallyn , James Morris , Randy Dunlap , Suren Baghdasaryan , Yafang Shao , Helge Deller , "Eric W. Biederman" , Adrian Reber , Thomas Gleixner , Jens Axboe , Alexei Starovoitov , "linux-fsdevel@vger.kernel.org" , "linux-kernel@vger.kernel.org" , linux-kselftest@vger.kernel.org, linux-mm@kvack.org, tiozhang , Luis Chamberlain , "Paulo Alcantara (SUSE)" , Sergey Senozhatsky , Frederic Weisbecker , YueHaibing , Paul Moore , Aleksa Sarai , Stefan Roesch , Chao Yu , xu xin , Jeff Layton , Jan Kara , David Hildenbrand , Dave Chinner , Shuah Khan , Zheng Yejian , Elena Reshetova , David Windsor , Mateusz Guzik , Ard Biesheuvel , "Joel Fernandes (Google)" , "Matthew Wilcox (Oracle)" , Hans Liljestrand Subject: Re: [PATCH v14] exec: Fix dead-lock in de_thread with ptrace_attach Message-ID: <20240116152210.GA12342@redhat.com> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 I'll try to recall this problem and actually read the patch tommorrow... Hmm. but it doesn't apply to Linus's tree, you need to rebase it. In particular, please note the recent commit 5431fdd2c181dd2eac2 ("ptrace: Convert ptrace_attach() to use lock guards") On 01/15, Bernd Edlinger wrote: > > The problem happens when a tracer tries to ptrace_attach > to a multi-threaded process, that does an execve in one of > the threads at the same time, without doing that in a forked > sub-process. That means: There is a race condition, when one > or more of the threads are already ptraced, but the thread > that invoked the execve is not yet traced. Now in this > case the execve locks the cred_guard_mutex and waits for > de_thread to complete. But that waits for the traced > sibling threads to exit, and those have to wait for the > tracer to receive the exit signal, but the tracer cannot > call wait right now, because it is waiting for the ptrace > call to complete, and this never does not happen. > The traced process and the tracer are now in a deadlock > situation, and can only be killed by a fatal signal. This looks very confusing to me. And even misleading. So IIRC the problem is "simple". de_thread() sleeps with cred_guard_mutex waiting for other threads to exit and pass release_task/__exit_signal. If one of the sub-threads is traced, debugger should do ptrace_detach() or wait() to release this tracee, the killed tracee won't autoreap. Now. If debugger tries to take the same cred_guard_mutex before detach/wait we have a deadlock. This is not specific to ptrace_attach(), proc_pid_attr_write() takes this lock too. Right? Or are there other issues? > -static int de_thread(struct task_struct *tsk) > +static int de_thread(struct task_struct *tsk, struct linux_binprm *bprm) > { > struct signal_struct *sig = tsk->signal; > struct sighand_struct *oldsighand = tsk->sighand; > spinlock_t *lock = &oldsighand->siglock; > + struct task_struct *t = tsk; > + bool unsafe_execve_in_progress = false; > > if (thread_group_empty(tsk)) > goto no_thread_group; > @@ -1066,6 +1068,19 @@ static int de_thread(struct task_struct *tsk) > if (!thread_group_leader(tsk)) > sig->notify_count--; > > + while_each_thread(tsk, t) { for_other_threads() > + if (unlikely(t->ptrace) > + && (t != tsk->group_leader || !t->exit_state)) > + unsafe_execve_in_progress = true; The !t->exit_state is not right... This sub-thread can already be a zombie with ->exit_state != 0 but see above, it won't be reaped until the debugger does wait(). > + if (unlikely(unsafe_execve_in_progress)) { > + spin_unlock_irq(lock); > + sig->exec_bprm = bprm; > + mutex_unlock(&sig->cred_guard_mutex); > + spin_lock_irq(lock); I don't understand why do we need to unlock and lock siglock here... But my main question is why do we need the unsafe_execve_in_progress boolean. If this patch is correct and de_thread() can drop and re-acquire cread_guard_mutex when one of the threads is traced, then why can't we do this unconditionally ? Oleg.