Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp1548254ybf; Sun, 1 Mar 2020 12:02:30 -0800 (PST) X-Google-Smtp-Source: APXvYqwmGEk/L+4RayNMxlc2AjlVAQ2IEMEAuag2++LqF/GKZae/cCoITyBYP+RtCCpKnL83e2QW X-Received: by 2002:a05:6830:4a7:: with SMTP id l7mr10219087otd.372.1583092950507; Sun, 01 Mar 2020 12:02:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583092950; cv=none; d=google.com; s=arc-20160816; b=Skrf94CZPe2X65vK8ESDSEGAhysJNjeGQILjgz/G6/gpR1odcNqS18qCxLaWxUyeu1 +G8jF8Mtiyv/Ahc1jDsZd1CYzW0ODaWNi4pv7vSgDx5rAkXR5W9q1zVEbQg5s8j0mjWO T+tbr6xlTe2O/DrzAOqK6ZMRwnCwNOHsUCLCQ7w5wVI6noMe4/VLN9o3TBCU8g0hz/6h 6YBkvJJShfjV3O4duysPqIX4g8TesEl4giVvXvH+kv9RLoDJScGU3oCN+dX+zjgSK0R2 IthhtLdNhQfZjqD+hp1+a70MC8F7xXSPivZ1wb1uBm4T9D0fxF9hAOblXo+Oij172Nf6 YTwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=eSJCogFW+5WEfqSVoqYuW5WPipQ09ya4rXn8XpVI9lk=; b=A2k+A298cA09oIn856zRGkVsa2+hL+G34Wq9LAWi2etWtylP3NA8KTf/upoX+pFaAm 4E82MbHVBNnAsZ/wo78a4l106D+g7ksOZgiy8Rl5iwU4n2Mm7S+DRaaS0naQYg+uxoPi iyzqGfcumu2ZBnqhnWDYIRqM7hUusHHP+Z8coCj/TzrMHVxt/UWYs+lCQ8lKbMmPYbdP FB7/pATraCB6Da1CzTQZGjmfXYmH8dIv4IBeGT/XlTjzXoiKoQDuE03t6sfoTfpLK4q/ ccKpa4XcDkBxyAi+wh4yq8ssoTvKrc4Bvu5RhoaQrofPYoYs9eCTpwShLyakdBkxMjBf EPTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=geEqLlC1; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q26si5070369oij.38.2020.03.01.12.02.17; Sun, 01 Mar 2020 12:02:30 -0800 (PST) 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=@google.com header.s=20161025 header.b=geEqLlC1; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726525AbgCAUAw (ORCPT + 99 others); Sun, 1 Mar 2020 15:00:52 -0500 Received: from mail-ot1-f66.google.com ([209.85.210.66]:41152 "EHLO mail-ot1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726050AbgCAUAv (ORCPT ); Sun, 1 Mar 2020 15:00:51 -0500 Received: by mail-ot1-f66.google.com with SMTP id v19so7623840ote.8 for ; Sun, 01 Mar 2020 12:00:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=eSJCogFW+5WEfqSVoqYuW5WPipQ09ya4rXn8XpVI9lk=; b=geEqLlC1SOE3TSZPMu4eY2C7kEQ3iAEu8+MjDZth1yJJEz3aYIl8RZzNGqXKo+fdBK L3piqmGBZ+R6u2jb71FG+otXTb5dexaFNTIMqfdFtBwyUv8J83uWWiWpJL700olDe1KU jdQ95KSTEviW1p++P8rmN10buW8MUjs1Ce5bxgvibleP8FVy2pIqdkf+iVbKv0OxIfep R3lnoXwspaouzFJKvOL2TU0Ri+Z9X4sJEOZJahs/nnjs5Uj7mCy6y8dFFjwiRQtUui/3 PYeYJCJjLDd5jpik81hu7laW8Arud8MEejgRzp/E94E5OSXDgeoA9nO6m2RJMQ1ob9wA F/aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=eSJCogFW+5WEfqSVoqYuW5WPipQ09ya4rXn8XpVI9lk=; b=NLi3voGjoyiXI6dA8S3tFA/hPRy+6g35mcP9mmn/FJJ7MYVwBS2cAWfeesmMcX9xME hV04nnaSD040XvDr1/QJQwxvwtL1MJUucgJUU2lE1Bs+7nIAVOCfoo7MJ9IBUSJmVkeS bz6WJ9zS7WNlShPAxMYfL7gTV+QnAVP9AI8jBgYCu6bOBUEhlCb5aw8/6Go4AmGJTFe/ 5qDCD9ybqlTLtCnf4bfbEhT/SM3qtCaJ0fKqegFh6Di8SGemBBMjryHCaUDPX8aQze80 pJRpKbm0dlClYBz+GdL1F6JXM1PATFZCKcIM6a6LIr/4GvXOkz15G+LKul3BvkmTmba4 d26Q== X-Gm-Message-State: APjAAAUsS2rU+bEP0H4e0EXAZNkTbpQS73pMMRQqFT7Rg00wYJsHnfRl cZ8DxPUeWz9GFcYV1AKkwcP7WarNN/28UKOlsC63QQ== X-Received: by 2002:a9d:5e8b:: with SMTP id f11mr10976903otl.110.1583092849244; Sun, 01 Mar 2020 12:00:49 -0800 (PST) MIME-Version: 1.0 References: <20200301185244.zkofjus6xtgkx4s3@wittgenstein> In-Reply-To: <20200301185244.zkofjus6xtgkx4s3@wittgenstein> From: Jann Horn Date: Sun, 1 Mar 2020 21:00:22 +0100 Message-ID: Subject: Re: [PATCH] exec: Fix a deadlock in ptrace To: Christian Brauner Cc: Bernd Edlinger , Jonathan Corbet , Alexander Viro , Andrew Morton , Alexey Dobriyan , "Eric W. Biederman" , 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 , Kees Cook , 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" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Mar 1, 2020 at 7:52 PM Christian Brauner wrote: > On Sun, Mar 01, 2020 at 07:21:03PM +0100, Jann Horn wrote: > > On Sun, Mar 1, 2020 at 12:27 PM Bernd Edlinger > > wrote: > > > The proposed solution is to have a second mutex that is > > > used in mm_access, so it is allowed to continue while the > > > dying threads are not yet terminated. > > > > Just for context: When I proposed something similar back in 2016, > > https://lore.kernel.org/linux-fsdevel/20161102181806.GB1112@redhat.com/ > > was the resulting discussion thread. At least back then, I looked > > through the various existing users of cred_guard_mutex, and the only > > places that couldn't be converted to the new second mutex were > > PTRACE_ATTACH and SECCOMP_FILTER_FLAG_TSYNC. > > > > > > The ideal solution would IMO be something like this: Decide what the > > new task's credentials should be *before* reaching de_thread(), > > install them into a second cred* on the task (together with the new > > dumpability), drop the cred_guard_mutex, and let ptrace_may_access() > > check against both. After that, some further restructuring might even > > Hm, so essentially a private ptrace_access_cred member in task_struct? And a second dumpability field, because that changes together with the creds during execve. (Btw, currently the dumpability is in the mm_struct, but that's kinda wrong. The mm_struct is removed from a task on exit while access checks can still be performed against it, and currently ptrace_may_access() just lets the access go through in that case, which weakens the protection offered by PR_SET_DUMPABLE when used for security purposes. I think it ought to be moved over into the task_struct.) > That would presumably also involve altering various LSM hooks to look at > ptrace_access_cred. When I tried to implement this in the past, I changed the LSM hook to take the target task's cred* as an argument, and then called the LSM hook twice from ptrace_may_access(). IIRC having the target task's creds as an argument works for almost all the LSMs, with the exception of Yama, which doesn't really care about the target task's creds, so you have to pass in both the task_struct* and the cred*.