From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 08130C18E5B for ; Tue, 10 Mar 2020 21:00:04 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AB4BD22522 for ; Tue, 10 Mar 2020 21:00:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AB4BD22522 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=xmission.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3186B6B0006; Tue, 10 Mar 2020 17:00:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2C8A26B0007; Tue, 10 Mar 2020 17:00:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 16ACD6B0008; Tue, 10 Mar 2020 17:00:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0075.hostedemail.com [216.40.44.75]) by kanga.kvack.org (Postfix) with ESMTP id F0B156B0006 for ; Tue, 10 Mar 2020 17:00:02 -0400 (EDT) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id B0BC49418 for ; Tue, 10 Mar 2020 21:00:02 +0000 (UTC) X-FDA: 76580669844.23.bite09_43e4668a636 X-HE-Tag: bite09_43e4668a636 X-Filterd-Recvd-Size: 9335 Received: from out02.mta.xmission.com (out02.mta.xmission.com [166.70.13.232]) by imf22.hostedemail.com (Postfix) with ESMTP for ; Tue, 10 Mar 2020 21:00:01 +0000 (UTC) Received: from in01.mta.xmission.com ([166.70.13.51]) by out02.mta.xmission.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jBlym-00067f-DA; Tue, 10 Mar 2020 14:59:56 -0600 Received: from ip68-227-160-95.om.om.cox.net ([68.227.160.95] helo=x220.xmission.com) by in01.mta.xmission.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.87) (envelope-from ) id 1jBlyk-0006E3-MQ; Tue, 10 Mar 2020 14:59:55 -0600 From: ebiederm@xmission.com (Eric W. Biederman) To: Jann Horn Cc: Christian Brauner , Bernd Edlinger , Kees Cook , 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" , Arnd Bergmann , Sargun Dhillon References: <87r1y8dqqz.fsf@x220.int.ebiederm.org> <87tv32cxmf.fsf_-_@x220.int.ebiederm.org> <87v9ne5y4y.fsf_-_@x220.int.ebiederm.org> <87eeu25y14.fsf_-_@x220.int.ebiederm.org> <20200309195909.h2lv5uawce5wgryx@wittgenstein> <877dztz415.fsf@x220.int.ebiederm.org> <20200309201729.yk5sd26v4bz4gtou@wittgenstein> <87k13txnig.fsf@x220.int.ebiederm.org> <20200310085540.pztaty2mj62xt2nm@wittgenstein> <87wo7svy96.fsf_-_@x220.int.ebiederm.org> <87k13sui1p.fsf@x220.int.ebiederm.org> Date: Tue, 10 Mar 2020 15:57:35 -0500 In-Reply-To: (Jann Horn's message of "Tue, 10 Mar 2020 21:10:59 +0100") Message-ID: <87lfo8rkqo.fsf@x220.int.ebiederm.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-XM-SPF: eid=1jBlyk-0006E3-MQ;;;mid=<87lfo8rkqo.fsf@x220.int.ebiederm.org>;;;hst=in01.mta.xmission.com;;;ip=68.227.160.95;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX1+legm72Gnu4YbUlNo0F2EELZNKSd0AFy8= X-SA-Exim-Connect-IP: 68.227.160.95 X-SA-Exim-Mail-From: ebiederm@xmission.com Subject: Re: [PATCH] pidfd: Stop taking cred_guard_mutex X-SA-Exim-Version: 4.2.1 (built Thu, 05 May 2016 13:38:54 -0600) X-SA-Exim-Scanned: Yes (on in01.mta.xmission.com) X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Jann Horn writes: > On Tue, Mar 10, 2020 at 9:00 PM Jann Horn wrote: >> On Tue, Mar 10, 2020 at 8:29 PM Eric W. Biederman wrote: >> > Jann Horn writes: >> > > On Tue, Mar 10, 2020 at 7:54 PM Eric W. Biederman wrote: >> > >> During exec some file descriptors are closed and the files struct is >> > >> unshared. But all of that can happen at other times and it has the >> > >> same protections during exec as at ordinary times. So stop taking the >> > >> cred_guard_mutex as it is useless. >> > >> >> > >> Furthermore he cred_guard_mutex is a bad idea because it is deadlock >> > >> prone, as it is held in serveral while waiting possibly indefinitely >> > >> for userspace to do something. > [...] >> > > If you make this change, then if this races with execution of a setuid >> > > program that afterwards e.g. opens a unix domain socket, an attacker >> > > will be able to steal that socket and inject messages into >> > > communication with things like DBus. procfs currently has the same >> > > race, and that still needs to be fixed, but at least procfs doesn't >> > > let you open things like sockets because they don't have a working >> > > ->open handler, and it enforces the normal permission check for >> > > opening files. >> > >> > It isn't only exec that can change credentials. Do we need a lock for >> > changing credentials? > [...] >> > If we need a lock around credential change let's design and build that. >> > Having a mismatch between what a lock is designed to do, and what >> > people use it for can only result in other bugs as people get confused. >> >> Hmm... what benefits do we get from making it a separate lock? I guess >> it would allow us to make it a per-task lock instead of a >> signal_struct-wide one? That might be helpful... > > But actually, isn't the core purpose of the cred_guard_mutex to guard > against concurrent credential changes anyway? That's what almost > everyone uses it for, and it's in the name... Having been through all of the users nope. Maybe someone tried to repurpose for that. I haven't traced through when it went the it was renamed from cred_exec_mutex to cred_guard_mutex. The original purpose was to make make exec and ptrace deadlock. But it was seen as being there to allow safely calculating the new credentials before the point of now return. Because if a process is ptraced or not affects the new credential calculations. Unfortunately offering that guarantee fundamentally leads to deadlock. So ptrace_attach and seccomp use the cred_guard_mutex to guarantee a deadlock. The common use is to take cred_guard_mutex to guard the window when credentials and process details are out of sync in exec. But there is at least do_io_accounting that seems to have the same justification for holding __pidfd_fget. With effort I suspect we can replace exec_change_mutex with task_lock. When we are guaranteed to be single threaded placing exec_change_mutex in signal_struct doesn't really help us (except maybe in some races?). The deep problem is no one really understands cred_guard_mutex so it is a mess. Code with poorly defined semantics is always wrong somewhere for someone. Which is part of why I am attacking this and having the conversations to make certain I understand what is going on. I see your point about commit_creds making a process undumpable. So in practice it really is only exec that changes creds in a way that ptrace_may_access will allow the process to be inspected. So I guess for now the practical non-regressing course is to change everything to my exec_change_mutex, removing the deadlock. Then we figure out how to cleanly deal with the races inspecting a process with changing credentials has. Eric