Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp2207267ybk; Mon, 11 May 2020 14:57:28 -0700 (PDT) X-Google-Smtp-Source: APiQypLv1yTScdBafTKSXlu9Ahy3SPQzELQPNYDVYTVayf/ys/LYT/BayL2ygY33YWLxdFzXg8gP X-Received: by 2002:a17:906:3791:: with SMTP id n17mr9321433ejc.262.1589234248102; Mon, 11 May 2020 14:57:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589234248; cv=none; d=google.com; s=arc-20160816; b=sG3+gzhoY0nbb/F110nFGV2eWptIcCJtpvwHLRJUcqIBKkpLe4N4ebOjf7xQsxoaae EXeX06JQvKspv49mj8CD7yiXNUUvpWeQBpFCBWmp3aPi261Oy2xkM9ajz4EQUF2ScHb9 hCVZq8MDfR/iKTW5bzOBVsOOLrB7xU5X1DoIYKGhwl/DrjetJlyX+NMejkXtW1RkWK1h zAjwvWAwPG+4xNThQTtLrOUhI23YMlLJvBWZVMpMmBOyJyJ0IxJLuXSxOVxnTqvXcrDU TVby6V/lKxumUw1Em1LJtHMeDyIzuKetSKTKLpQ7BIEy5MgWea9ZRYnntm3YR9DIt8n1 Xw4A== 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=N2k3VluFVmqb0Md48Vp4lnUitQu5LwJqg2jgZWuBL9c=; b=rmBiBLXwwM7FAJD3kLsPVSdGhMq2XfdKcgaZzkeCvtDHmaDLnyZdk6S3IqGY8kBuX/ jsROCxQu/yPNcoWBzZvj9gRoUyVloyzVA5yRMhQ9wWtaMAtYNp6+0wbHpw70oHAiV5iy 0xenNXPo1d2XOstYNl753UyIgLNE5G2Z4DvpuoxU+3mngcl6oC0h3ZArzQxjVaE7zBIl boSi/SgED17YOPG0V0bpZos4Rvc6hWy0C2F9ntih367I3rzAXBeqyWClwFfK9GHE6XGu WRT1q2+cYPVWjjZ97cTdAD0bpmQ22xvr+ZsfF4pc1L6t8eIdPIRcrK+qRO/6CAh8ZiSg hUIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=JEzt9vSq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id cw10si7119978edb.54.2020.05.11.14.57.04; Mon, 11 May 2020 14:57:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=JEzt9vSq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1727980AbgEKVzg (ORCPT + 99 others); Mon, 11 May 2020 17:55:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1727835AbgEKVzf (ORCPT ); Mon, 11 May 2020 17:55:35 -0400 Received: from mail-pf1-x442.google.com (mail-pf1-x442.google.com [IPv6:2607:f8b0:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 233E1C061A0E for ; Mon, 11 May 2020 14:55:30 -0700 (PDT) Received: by mail-pf1-x442.google.com with SMTP id w65so5343622pfc.12 for ; Mon, 11 May 2020 14:55:30 -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=N2k3VluFVmqb0Md48Vp4lnUitQu5LwJqg2jgZWuBL9c=; b=JEzt9vSq/OK6SQi7DpkzAtwK38cH/0tMbEx2UaeIBcMAouECDmTJVeSPnA0bBKE+Ck F8zI0p1T1rt+EGdoMyVb7LnCDsLEuCQzbdVJXI3sOwN3+ZYFFLYPmtHKe6xkbZVsHgYq hmdaAdjKvcW39mx29cCbIMwsMM/z1+9FimibY= 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=N2k3VluFVmqb0Md48Vp4lnUitQu5LwJqg2jgZWuBL9c=; b=P/HAikyLlJ1oCKrYq2DTQHn1+yilIW9IMwsneQiyd+56O4By3UclC2yBgZdbq/p6T3 tMqbcK+mFoWWMrYygVv/nrgbburTXKHbtmZpE/UB00Jl5FYi75N/c6UsogfSdT+HHOmv IjLewsRkkHS0ODn3NXmkuziTxaW34BHqXVgHPMivIEueRifzlVOew4sgFZP70c3Tp9Nm HfHjuUuVHAsGVXsV7jjkJElo2k7940/omwuJ88GuMqKiuF5D9lCakSSOeqEaUi665qb3 OLELKVb/q1s9kQPkFt+r6L0ZTybdlqFthpoZ34TUOZK0rZDO2nqzeBDfPsa2K2mSplFB VQrw== X-Gm-Message-State: AGi0PuY2msRY0OuTb3glvmciUXWZQazdA+wQTsv1fZaPN3jSV560/uzb R5gZbLcSh7uwhFh/9Z1KBZtT9g== X-Received: by 2002:a63:6d86:: with SMTP id i128mr9934914pgc.432.1589234129533; Mon, 11 May 2020 14:55:29 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id j35sm8731698pgl.74.2020.05.11.14.55.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 May 2020 14:55:28 -0700 (PDT) Date: Mon, 11 May 2020 14:55:27 -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: <202005111428.B094E3B76A@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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87sgg6v8we.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 Mon, May 11, 2020 at 09:33:21AM -0500, Eric W. Biederman wrote: > Linus Torvalds writes: > > > On Sat, May 9, 2020 at 9:30 PM Tetsuo Handa > > wrote: > >> > >> Wouldn't this change cause > >> > >> if (fd_binary > 0) > >> ksys_close(fd_binary); > >> bprm->interp_flags = 0; > >> bprm->interp_data = 0; > >> > >> not to be called when "Search for the interpreter" failed? > > > > Good catch. We seem to have some subtle magic wrt the fd_binary file > > descriptor, which depends on the recursive behavior. > > Yes. I Tetsuo I really appreciate you noticing this. This is exactly > the kind of behavior I am trying to flush out and keep from being > hidden. > > > I'm not seeing how to fix it cleanly with the "turn it into a loop". > > Basically, that binfmt_misc use-case isn't really a tail-call. > > I have reservations about installing a new file descriptor before > we process the close on exec logic and the related security modules > closing file descriptors that your new credentials no longer give > you access to logic. Hm, this does feel odd. In looking at this, it seems like this file never gets close-on-exec set, and doesn't have its flags changed from its original open: .open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC, only the UMH path through exec doesn't explicitly open a file by name from what I can see, so we'll only have these flags. > I haven't yet figured out how opening a file descriptor during exec > should fit into all of that. > > What I do see is that interp_data is just a parameter that is smuggled > into the call of search binary handler. And the next binary handler > needs to be binfmt_elf for it to make much sense, as only binfmt_elf > (and binfmt_elf_fdpic) deals with BINPRM_FLAGS_EXECFD. > > So I think what needs to happen is to rename bprm->interp_data to > bprm->execfd, remove BINPRM_FLAGS_EXECFD and make closing that file > descriptor free_bprm's responsiblity. Yeah, I would agree. As far as the close handling, I don't think there is a difference here: it interp_data was closed on the binfmt_misc.c error path, and in the new world it would be the exec error path -- both would be under the original credentials. > I hope such a change will make it easier to see all of the pieces that > are intereacting during exec. Right -- I'm not sure which piece should "consume" bprm->execfd though, which I think is what you're asking next... > I am still asking: is the installation of that file descriptor useful if > it is not exported passed to userspace as an AT_EXECFD note? > > I will dig in and see what I can come up with. 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? -- Kees Cook