Received: by 2002:a05:7412:8d1c:b0:fa:4c10:6cad with SMTP id bj28csp512307rdb; Wed, 17 Jan 2024 08:39:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IETPvdKMDjdJIgrCwQ17jKUtmN0WzwVV2hcRLadHyKnkMUGs3/hW7r/yZ4FiRWRu8XsZ3LR X-Received: by 2002:a17:906:4756:b0:a26:85ed:7269 with SMTP id j22-20020a170906475600b00a2685ed7269mr2310593ejs.196.1705509594265; Wed, 17 Jan 2024 08:39:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705509594; cv=pass; d=google.com; s=arc-20160816; b=yDOF+91Pgi0QqtArQ5Uqa0hTKzdvLLPhux7H6FNt/Mycqi+PLWDMCHrff0sM9j+xkz gSt6lRipzKlsiBlSNrDZDYQLCyTOMevmBAb0of6pbnFg/yVnk8yuB1hG4A64dSvF2aCZ oDzkANj8k3kk9mronuCkYSYX3TJh20minBjkTepolxm8h0FitGTwwwKtIMUcrpaMHJ9p nsFclBKU7kZHx98nt38d4Pno9ppQueSOf5M6JaGKPdnC3kOcddPWFKKuNzW7a5XylvKF veEnUXge/6LVWLjZcgyZzQgPa7UtR4KR6O85pm+XMoAbDVNZlTtCUTu+SSu3T/T9r8/b ZsWw== ARC-Message-Signature: i=2; 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=9MEKaPLuC4dWjyeAAjRSNgqo7o077s9TgRJfiIfemoI=; fh=WbiPWVJdPw8Cr4myMGvuemWCU0jnwfwiSx8atqjaKmE=; b=A6yR07As6r7CYLACy4CD4ecZTyjtZNjH//1iX315tOL7InWlvfpmEErVl4Hsw0GK7m ZvxjNvQhdtCB06wfKDzU+NNSLz4VdYyKwGx+7wRS6NOJoFRi2gki5y4+iwSauy+2f5y1 HucXVonAT3eRTgKTaK/mhjTpyQnzFgxo0Ggt/btE+9hf1DR6Byy24pmKCMiI+y2qj7Md MK8GqrnDy0+Mcn0AoXc18ZW7OTirPiNdLmNXDvVyutb/3a1UusEQEVxO1iK37ddGK2FA UnBfW+B04I3My/sd5K9Zt9TTA597GbLVCSIRAXxQAeETvHlmpDGyEZAm5m2wUwhrwzta HRLw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=EqqdPV2S; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-29233-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-29233-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. [147.75.80.249]) by mx.google.com with ESMTPS id mf11-20020a170906cb8b00b00a2ea01bb4d4si1237580ejb.537.2024.01.17.08.39.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jan 2024 08:39:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-29233-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=EqqdPV2S; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-29233-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-29233-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 CF9BA1F2B7A9 for ; Wed, 17 Jan 2024 16:39:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4F828224E7; Wed, 17 Jan 2024 16:39:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="EqqdPV2S" 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 E1311224D3 for ; Wed, 17 Jan 2024 16:39:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705509584; cv=none; b=GKZhUHZD8l6IeXSwNf7eSjtXD2udkK5SoJIwA8PGTBRL9cin56bKKgdIRXTLcceUJJxGHJt9rMWL4unkMbPVoRltg/X4EIgLuhhGiVXHajgyyT6ZXFbhsZZN+GKmYRD98gi26NpdsiSQNfPCiz1YE7E89DEvLplHyGbHUvOZLak= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705509584; c=relaxed/simple; bh=M3t4lykNqYhmCjQO4zKq31KhD0/95qJoM+feR8YYTMs=; h=DKIM-Signature:Received:X-MC-Unique:Received:Received:Received: Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To:User-Agent: X-Scanned-By; b=BvuFCEWezHYB73SFAE4TjKJ1PgWI2T1xX7gEDlRehIyw2hz3ciwpWsT3Qy85bEfh8DSeKf4BQOKeFG61EXA+biHM/nvYVt0ze+gpiX/HUVBpRCLuelNHlNIgM3pSRWIMbtUHVCrIN4vrGONxKrl5zr14KHh1mrdWjx5Y1EsZYgs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=EqqdPV2S; arc=none smtp.client-ip=170.10.129.124 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=1705509582; 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=9MEKaPLuC4dWjyeAAjRSNgqo7o077s9TgRJfiIfemoI=; b=EqqdPV2SILocvrCJOjjGvYFBEYon/AWGInUeQD1r2FD4592EF7RdGYgkGQ5pd2V5QTPzu0 /TmfVmORiJCIlezG1UnS/8h9PeByj2niLp6XWety4Gfhe4TCtF8i3jZYad9nNG751j+m+o 1Q4g0DzhskdvxAFe3RaNTK6903yhXs4= 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-389-a8vvGCybN5WY-ilt55kkNA-1; Wed, 17 Jan 2024 11:39:36 -0500 X-MC-Unique: a8vvGCybN5WY-ilt55kkNA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (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 921CE1C29EC6; Wed, 17 Jan 2024 16:39:31 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.224.121]) by smtp.corp.redhat.com (Postfix) with SMTP id 8BF4E3C25; Wed, 17 Jan 2024 16:39:22 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Wed, 17 Jan 2024 17:38:18 +0100 (CET) Date: Wed, 17 Jan 2024 17:38:09 +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: <20240117163739.GA32526@redhat.com> References: <20240116152210.GA12342@redhat.com> 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.1 On 01/17, 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. > > > > Yes. but the tracer has to do its job, and that is ptrace_attach the > remaining treads, it does not know that it would avoid a dead-lock > when it calls wait(), instead of ptrace_attach. It does not know > that the tracee has just called execve in one of the not yet traced > threads. Hmm. I don't understand you. I agree we have a problem which should be fixed. Just the changelog looks confusing to me, imo it doesn't explain the race/problem clearly. > > 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? > > > > No, proc_pid_attr_write has no problem if it waits for cred_guard_mutex, > because it is only called from one of the sibling threads, OK, thanks, I was wrong. I forgot about "A task may only write its own attributes". So yes, ptrace_attach() is the only source of problematic mutex_lock() today. There were more in the past. > >> + 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(). > > > > I dont think so. > de_thread() handles the group_leader different than normal threads. I don't follow... I didn't say that t is a group leader. I said it can be a zombie sub-thread with ->exit_state != 0. > That means normal threads have to wait for being released from the zombie > state by the tracer: > sig->notify_count > 0, and de_thread is woken up by __exit_signal That is what I said before. Debugger should release a zombie sub-thread, it won't do __exit_signal() on its own. > >> + 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... > > That is just a precaution because I did want to release the > mutexes exactly in the reverse order as they were acquired. To me this adds the unnecessary complication. > > 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 ? > > > > I just wanted to keep the impact of the change as small as possible, But the unsafe_execve_in_progress logic increases the impact and complicates the patch. I think the fix should be as simple as possible. (to be honest, right now I don't think this is a right approach). > including > possible performance degradation due to double checking of credentials. Not sure I understand, but you can add the performance improvements later. Not to mention that this should be justified, and the for_other_threads() loop added by this patch into de_thread() is not nice performance-wise. Oleg.