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=-1.0 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS 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 51249CA90AF for ; Tue, 12 May 2020 19:25:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 25B88206DD for ; Tue, 12 May 2020 19:25:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="QrgzGmWQ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731036AbgELTZ1 (ORCPT ); Tue, 12 May 2020 15:25:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725554AbgELTZ0 (ORCPT ); Tue, 12 May 2020 15:25:26 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A025C061A0C for ; Tue, 12 May 2020 12:25:26 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id t40so9882964pjb.3 for ; Tue, 12 May 2020 12:25:26 -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=zuvKbkxXexDFgEr1pGnuiyt05zDdT11JeOxs67G6etM=; b=QrgzGmWQtElD5mjDu3W8wIpwV1hy6zO5D4IGwwOsKgvGqbaM7TGjkewco9AYTSGXlv dWsAVSyFtoNXZ/+C554O1raxb4PYKxPNEq4eBRLvewfg2sLpL6VawYdI7rYRn9aSZHkd 3QqRZuo0mp94NP4bIrrxWUWlYjP41OnHwnU2A= 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=zuvKbkxXexDFgEr1pGnuiyt05zDdT11JeOxs67G6etM=; b=gGuzvmHecHsD3f9mN4d7MKFktcEgtBQ9rCzywdsEY30UzyhBwV4ATvJqtjvXqqA7HI Kw1tAxQTGELPqjfDMq+hLJw2aUkot7lDWvLl6U3UbwtjZvjR30BmbqRzBGtrSp2BhOdT FekxKvluFDeQU7KqJaGYCCnJI226WUzkenRagD6L9AkGkfjVl3+5dUCABqLGtxgp4bsz qW7+flztj6M0fSB9gvO7xDhpge+5x05bsuUdovDk50RPg2qMSpbfpZB4Upq9CcPLpk27 YM2Myp8NiiJfI7NTkHLTEv17KUe/gz1u7OOIEZY+Km6GcaKinHjLFMg+QYiSuA1V23M6 0Vcw== X-Gm-Message-State: AGi0PuYW6XHIZig/9exJ1Qcui2Up3smajChhjuxZvU+skH9LMH/UnZSr aBqAz699+Z3R86lJCayBiTs6Bw== X-Google-Smtp-Source: APiQypLJuiWQRFv5kofh3l2xnXnOJoCBvEnuis0K48GEmJut0JZfFKUjUksikIwYvKq/qZ8mSMhu+g== X-Received: by 2002:a17:902:103:: with SMTP id 3mr21731839plb.325.1589311526054; Tue, 12 May 2020 12:25:26 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id 3sm12789369pfo.27.2020.05.12.12.25.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 May 2020 12:25:25 -0700 (PDT) Date: Tue, 12 May 2020 12:25:24 -0700 From: Kees Cook To: "Eric W. Biederman" Cc: Linus Torvalds , Tetsuo Handa , Linux Kernel Mailing List , Oleg Nesterov , Jann Horn , Greg Ungerer , Rob Landley , Bernd Edlinger , linux-fsdevel , Al Viro , Alexey Dobriyan , Andrew Morton , Casey Schaufler , LSM List , James Morris , "Serge E. Hallyn" , Andy Lutomirski Subject: Re: [PATCH 3/5] exec: Remove recursion from search_binary_handler Message-ID: <202005121218.ED0B728DA@keescook> References: <87h7wujhmz.fsf@x220.int.ebiederm.org> <87sgga6ze4.fsf@x220.int.ebiederm.org> <87v9l4zyla.fsf_-_@x220.int.ebiederm.org> <87eerszyim.fsf_-_@x220.int.ebiederm.org> <87sgg6v8we.fsf@x220.int.ebiederm.org> <202005111428.B094E3B76A@keescook> <874kslq9jm.fsf@x220.int.ebiederm.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <874kslq9jm.fsf@x220.int.ebiederm.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 12, 2020 at 01:42:53PM -0500, Eric W. Biederman wrote: > Kees Cook writes: > > Should binfmt_misc do the install, or can the consuming binfmt do it? > > i.e. when binfmt_elf sees bprm->execfd, does it perform the install > > instead? > > I am still thinking about this one, but here is where I am at. At a > practical level passing the file descriptor of the script to interpreter > seems like something we should encourage in the long term. It removes > races and it is cheaper because then the interpreter does not have to > turn around and open the script itself. Yeah, this does sounds pretty good, though I have concerns about doing it for a process that isn't expecting it. I've seen a lot of bad code make assumptions about initial fd numbers. :( > Strictly speaking binfmt_misc should not need to close the file > descriptor in binfmt_misc because we have already unshared the files > struct and reset_files_struct should handle restoring it. If I get what you mean, I agree. The error case is fine. > Calling fd_install in binfmt_misc still seems wrong, as that exposes > the new file descriptor to user space with the old creds. I haven't dug into the details here -- is there a real risk here? The old creds are what opened the file originally for the exec. Are you thinking about executable-but-not-readable files? > It is possible although unlikely for userspace to find the file > descriptor without consulting AT_EXECFD so just to be conservative I > think we should install the file descriptor in begin_new_exec even if > the next interpreter does not support AT_EXECFD. I think universally installing the fd needs to be a distinct patch -- it's going to have a lot of consequences, IMO. We can certainly deal with them, but I don't think it should be part of this clean-up series. > I am still working on how to handle recursive binfmts but I suspect it > is just a matter of having an array of struct files in struct > linux_binprm. If install is left if binfmt_misc, then the recursive problem goes away, yes? -- Kees Cook