Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp1070020ybh; Tue, 10 Mar 2020 14:02:08 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtHh44pW0NwtQjs8tWO5+GhPOtcsHFpVR4wTrL6sbQTPbAYaoq+sw4sLzagsjaDgzpMg580 X-Received: by 2002:aca:cdd1:: with SMTP id d200mr2497986oig.153.1583874128455; Tue, 10 Mar 2020 14:02:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583874128; cv=none; d=google.com; s=arc-20160816; b=hZNqN0ZipHnXr6HR06aiHUmQYxLDXUpZg9eNCDxKpAquawqZ4TLdPDEvyiAVSOa5v1 6uyI9eZH+oHWw/PWY7AmEl7K3b+GAlcIzf89f5goVcz6M+p9MBaqK9Cxd+d/YxO1kckU sG53B6zlzQlNBDlk4QMXT3u0OyF9FzZStjDu7pKwi/YXCjHzDKENSatPM2z/OyV02cUm IU/4wbN1p4vvFUw10Q8ZAK4kNUqk/RTM95N6NvOAbrbIcs4GKnDlalAKcSJueJbS43ig DFE+JhQxQX0Z0vzBkt15HfpYAErPTHr5o9mTjBZodtYVTW+WABQGUxLPU7QPYCCQ953X 7xEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=8rwWaGaAnCRv7abCrLVGP/12v6tm69sSb/N4biM+MpU=; b=mVDuYi6qpzRl5MvZkb0aryye6BNwkYVqY5ojmAbPOZy1NWS4Jyvg+Llpj0ZKv+tvFY YTu2JWwLlveuV041Tjoxyvd0dNI/HJhpPjz5wkmCgEDrVmW3od77NzYVwwHfXMJC2uoY egz+iMHc1UAeNgBalW8MIlLJf1hTmXo934m64oCesMcbmiZ6KFpH8avhLZgaIRvMg8NT xDxIkW8wDbIZro1aLw4WMh7zx10U+lObihuBSzxKiyig8HnPAY3gT2nAGc3v44dAuKWJ /VnYAzPgKkiFkO0fD+g/Q94MH+qWsR8tWV+Rd2iTouBZWi6jELnHywnJ7WISythkvKbl oGRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=E4fHeovR; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v22si8595482otq.109.2020.03.10.14.01.55; Tue, 10 Mar 2020 14:02:08 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=E4fHeovR; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727614AbgCJVAp (ORCPT + 99 others); Tue, 10 Mar 2020 17:00:45 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:33764 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727141AbgCJVAo (ORCPT ); Tue, 10 Mar 2020 17:00:44 -0400 Received: by mail-pf1-f195.google.com with SMTP id n7so48910pfn.0 for ; Tue, 10 Mar 2020 14:00:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=8rwWaGaAnCRv7abCrLVGP/12v6tm69sSb/N4biM+MpU=; b=E4fHeovRMCTEHPf2Y5E/1XX89oUv6PPRO7U0xbU19Ojqt9o6vTvNVOrSjmjVlksK0u WT3MVOTY1M10Pq6ui6+/FqibefM1Uq0luC+0aYMB0nLC3CZ3xdern/rFtpoTc70jV0mw 55hRTwk9a28dM92Eddl4f7pViHS37XCoNZMBo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=8rwWaGaAnCRv7abCrLVGP/12v6tm69sSb/N4biM+MpU=; b=mpFGAnXygUSI+qFHaRjpYkWoy3yl6jheKdh/UNK8jNsw5JU6h15tWozbAV3/11kAFB AI78mmZWHOJowTkPuKFcTiuLKABZCvQbWmJkabmKHxxiKbC+vNhNdz9yu5kaAEacU00T qcAMiu6eIJ98ZvDdezB1bNSgMbE0LrI7ki52SQRjmriHo4Hcb7Ll/RbD4S5lqGp3ipYp rZHrrSU9q7WA3zArxTI59YlG07CjMVRjlW3r+jf5VZThYdaqkyHitXxIZiCM6WRCWlus swigMtyIDsPc6rhMmFVYmhIYtmABsOtrMkJPsnCpDcpS0rgcL6/U/hixgl2toOckrJkg ZsMA== X-Gm-Message-State: ANhLgQ1U4b1Wm8Z+BDKq+qvdJseOiM73hl67F2gKHo9tNmdVc45OWXSY JmFD43+N79QVu73i7e5uEA9LpQ== X-Received: by 2002:a63:2542:: with SMTP id l63mr22436554pgl.312.1583874043288; Tue, 10 Mar 2020 14:00:43 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id c201sm3106135pfc.73.2020.03.10.14.00.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Mar 2020 14:00:41 -0700 (PDT) Date: Tue, 10 Mar 2020 14:00:40 -0700 From: Kees Cook To: Bernd Edlinger Cc: "Eric W. Biederman" , Christian Brauner , Jann Horn , Jonathan Corbet , Alexander Viro , Andrew Morton , Alexey Dobriyan , Thomas Gleixner , Oleg Nesterov , Frederic Weisbecker , Andrei Vagin , Ingo Molnar , "Peter Zijlstra (Intel)" , Yuyang Du , David Hildenbrand , Sebastian Andrzej Siewior , Anshuman Khandual , David Howells , James Morris , Greg Kroah-Hartman , Shakeel Butt , Jason Gunthorpe , Christian Kellner , Andrea Arcangeli , Aleksa Sarai , "Dmitry V. Levin" , "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "linux-fsdevel@vger.kernel.org" , "linux-mm@kvack.org" , "stable@vger.kernel.org" , "linux-api@vger.kernel.org" Subject: Re: [PATCH 1/4] exec: Fix a deadlock in ptrace Message-ID: <202003101359.8BEE26F322@keescook> References: <878sk94eay.fsf@x220.int.ebiederm.org> <87r1y12yc7.fsf@x220.int.ebiederm.org> <87k13t2xpd.fsf@x220.int.ebiederm.org> <87d09l2x5n.fsf@x220.int.ebiederm.org> <871rq12vxu.fsf@x220.int.ebiederm.org> <877dzt1fnf.fsf@x220.int.ebiederm.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 10, 2020 at 02:43:41PM +0100, Bernd Edlinger wrote: > This fixes a deadlock in the tracer when tracing a multi-threaded > application that calls execve while more than one thread are running. > > I observed that when running strace on the gcc test suite, it always > blocks after a while, when expect calls execve, because other threads > have to be terminated. They send ptrace events, but the strace is no > longer able to respond, since it is blocked in vm_access. > > The deadlock is always happening when strace needs to access the > tracees process mmap, while another thread in the tracee starts to > execve a child process, but that cannot continue until the > PTRACE_EVENT_EXIT is handled and the WIFEXITED event is received: > > strace D 0 30614 30584 0x00000000 > Call Trace: > __schedule+0x3ce/0x6e0 > schedule+0x5c/0xd0 > schedule_preempt_disabled+0x15/0x20 > __mutex_lock.isra.13+0x1ec/0x520 > __mutex_lock_killable_slowpath+0x13/0x20 > mutex_lock_killable+0x28/0x30 > mm_access+0x27/0xa0 > process_vm_rw_core.isra.3+0xff/0x550 > process_vm_rw+0xdd/0xf0 > __x64_sys_process_vm_readv+0x31/0x40 > do_syscall_64+0x64/0x220 > entry_SYSCALL_64_after_hwframe+0x44/0xa9 > > expect D 0 31933 30876 0x80004003 > Call Trace: > __schedule+0x3ce/0x6e0 > schedule+0x5c/0xd0 > flush_old_exec+0xc4/0x770 > load_elf_binary+0x35a/0x16c0 > search_binary_handler+0x97/0x1d0 > __do_execve_file.isra.40+0x5d4/0x8a0 > __x64_sys_execve+0x49/0x60 > do_syscall_64+0x64/0x220 > entry_SYSCALL_64_after_hwframe+0x44/0xa9 > > This changes mm_access to use the new exec_update_mutex > instead of cred_guard_mutex. > > This patch is based on the following patch by Eric W. Biederman: > "[PATCH 0/5] Infrastructure to allow fixing exec deadlocks" > Link: https://lore.kernel.org/lkml/87v9ne5y4y.fsf_-_@x220.int.ebiederm.org/ > > Signed-off-by: Bernd Edlinger Cool, yes, on top of the new infrastructure this looks correct to me -- the new mutex wraps mm changes and mm_access() is looking at *drum roll* the mm! :) Reviewed-by: Kees Cook -Kees > --- > kernel/fork.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/kernel/fork.c b/kernel/fork.c > index c12595a..5720ff3 100644 > --- a/kernel/fork.c > +++ b/kernel/fork.c > @@ -1224,7 +1224,7 @@ struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) > struct mm_struct *mm; > int err; > > - err = mutex_lock_killable(&task->signal->cred_guard_mutex); > + err = mutex_lock_killable(&task->signal->exec_update_mutex); > if (err) > return ERR_PTR(err); > > @@ -1234,7 +1234,7 @@ struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) > mmput(mm); > mm = ERR_PTR(-EACCES); > } > - mutex_unlock(&task->signal->cred_guard_mutex); > + mutex_unlock(&task->signal->exec_update_mutex); > > return mm; > } > -- > 1.9.1 -- Kees Cook