Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp2279483rwn; Fri, 16 Sep 2022 08:05:13 -0700 (PDT) X-Google-Smtp-Source: AMsMyM41cgbOQglfXRCPLSs49SQ8ZXa+CRTAJmTnAunseVMxDj/GXLJ2DHrvm/8Ajuk5oz+LTfA/ X-Received: by 2002:a65:5b8d:0:b0:434:e318:80f with SMTP id i13-20020a655b8d000000b00434e318080fmr4990859pgr.97.1663340713227; Fri, 16 Sep 2022 08:05:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663340713; cv=none; d=google.com; s=arc-20160816; b=yMN9o5H9jLg1ziI7I6u0ZIm7d1i11G91XWF3C4uZOQ7+3VUsxZmif/+i7Csmla+YEQ IUVpWh9CtASfKZNC+VesAeuPabOUJGABMW+/6T2QCVetnBtXvwSP636qloIp4kkhCuao PMORlKAJTHn72pLXRprHV6jUfCC1eY5TDJwqNk+ibT8zrFEtkadU+lvlWrhJE7glhNmx T58MjRaF0PqBVBTKEFrEohIBfpQ4PzfZgiPJ9TZ9ZM7DoQtYPuOZZfnJGHTQDGOslzC2 E4H7GBS1jKz7NPyVop0DSMU/s4JJimag/W7KIYgbib0pMqlF1MsaxYkAtM4kkmGQRoH9 8Rwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=cKDTNYSujMHhHC8oskKxTsGs14KRpTgwA0hXaRQXqJE=; b=X6DBN5foj1j9mxyzfYCxwqV8apVPSIZb/fRevQUjusYNmPnQY6akvmnMMhINeVViUc Hvcw5d627KqHlBMOS4+ClgQSMwLO4pq6iKTn53q08QlegrSenRyerHinF845XrlKBzQT z13ivA6IdGT50kWTqUW2sp3FgV3I0TOyh1LTGWTc7qb+YctcUwaeHDG2fBhMCrCvgT6f RYyD6sdgvbTpOwLgKIIcQVrdJzfnQIJz029QaPKy/5g2qWPE1MKEw/Sd2CTC16b2eTEH zfkuqCoH9NQ33xTBFAMzD1HYqC+nbNnazeiSDaOBZ09BWhJvxdy4EC4mpkEqqLyft79/ Ngvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=LHdnib7N; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a31-20020a631a1f000000b0042b52f42418si21937299pga.227.2022.09.16.08.04.57; Fri, 16 Sep 2022 08:05:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=LHdnib7N; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S230238AbiIPOin (ORCPT + 99 others); Fri, 16 Sep 2022 10:38:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229658AbiIPOik (ORCPT ); Fri, 16 Sep 2022 10:38:40 -0400 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F62CB3B30 for ; Fri, 16 Sep 2022 07:38:40 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id r23so11965559pgr.6 for ; Fri, 16 Sep 2022 07:38:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=cKDTNYSujMHhHC8oskKxTsGs14KRpTgwA0hXaRQXqJE=; b=LHdnib7NohNt/B5VsHS71bRWCyRwWHlluIwZWIqC++4bLbYNnIejD0i+Vmu2TXtlsD 9EuBpBms8xebBJVcdKpNFvqK+AUgmJ8GriGB8g8/+s+JFFGbWp44RBMzhdfoguAAvaM9 Iab+b9A4XMNstm4V+iyB4SyEu89dq6ySAc6pk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=cKDTNYSujMHhHC8oskKxTsGs14KRpTgwA0hXaRQXqJE=; b=OIBYl0UigNEqefyxUEPHlsYKr7XVdtj1CTT009llFnzIcKSipgB1EEcD18/EvnNT9R 66F67oLDZJpTm1/6Q6U2FrUc67FmLJFGmvS7Q5eFD1oTAnT/6sEFXxTY8j9CE9FPQnoM HDYFSNhoHB4aIyS+MIJ57mLzZ3Pi9DXFmVnjnNSMWOWdXsr65Jm/asm+6K+s/2RcJpCs o6sRLSvGicG0Sl/t1mDx7iml/t6XP/zCXKv3gJLC08K5VyGHk6ltrh5eMcwzIXc+47iR K+5Pu3pcEAJQogSA6Kfx1Poa6tkJ+ilnFW6ImBDF1hwQ08JFSRv74mgYRvTenrccl8tp sLyA== X-Gm-Message-State: ACrzQf1Y0ZN80wDO2G9xc6BlbU+Cs0WH/nnatF3avjG8rUrDJtpRIL35 goQf7cmJY9buoSz5zLOW8rzk2Q== X-Received: by 2002:a05:6a00:1823:b0:544:b4db:50c with SMTP id y35-20020a056a00182300b00544b4db050cmr5039980pfa.33.1663339119577; Fri, 16 Sep 2022 07:38:39 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id b15-20020a170903228f00b001784a45511asm8184760plh.79.2022.09.16.07.38.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Sep 2022 07:38:38 -0700 (PDT) Date: Fri, 16 Sep 2022 07:38:37 -0700 From: Kees Cook To: Josh Triplett Cc: Eric Biederman , Alexander Viro , linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] fs/exec.c: Add fast path for ENOENT on PATH search before allocating mm Message-ID: <202209160727.5FC78B735@keescook> References: <5c7333ea4bec2fad1b47a8fa2db7c31e4ffc4f14.1663334978.git.josh@joshtriplett.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5c7333ea4bec2fad1b47a8fa2db7c31e4ffc4f14.1663334978.git.josh@joshtriplett.org> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Sep 16, 2022 at 02:41:30PM +0100, Josh Triplett wrote: > Currently, execve allocates an mm and parses argv and envp before > checking if the path exists. However, the common case of a $PATH search > may have several failed calls to exec before a single success. Do a > filename lookup for the purposes of returning ENOENT before doing more > expensive operations. At first I didn't understand how you were seeing this, since I'm so used to watching shell scripts under tracing, which correctly use stat(): $ strace bash -c foo stat("/home/keescook/bin/foo", 0x7ffe1f9ddea0) = -1 ENOENT (No such file or directory) stat("/usr/local/sbin/foo", 0x7ffe1f9ddea0) = -1 ENOENT (No such file or directory) stat("/usr/local/bin/foo", 0x7ffe1f9ddea0) = -1 ENOENT (No such file or directory) stat("/usr/sbin/foo", 0x7ffe1f9ddea0) = -1 ENOENT (No such file or directory) stat("/usr/bin/foo", 0x7ffe1f9ddea0) = -1 ENOENT (No such file or directory) stat("/sbin/foo", 0x7ffe1f9ddea0) = -1 ENOENT (No such file or directory) stat("/bin/foo", 0x7ffe1f9ddea0) = -1 ENOENT (No such file or directory) But I see, yes, glibc tries to actually call execve(), which, as you say, is extremely heavy: $ strace ./execvpe ... execve("/home/kees/bin/foo", ["./execvpe"], 0x7ffc542bff38 /* 33 vars */) = -1 ENOENT (No such file or directory) execve("/usr/local/sbin/foo", ["./execvpe"], 0x7ffc542bff38 /* 33 vars */) = -1 ENOENT (No such file or directory) execve("/usr/local/bin/foo", ["./execvpe"], 0x7ffc542bff38 /* 33 vars */) = -1 ENOENT (No such file or directory) execve("/usr/sbin/foo", ["./execvpe"], 0x7ffc542bff38 /* 33 vars */) = -1 ENOENT (No such file or directory) execve("/usr/bin/foo", ["./execvpe"], 0x7ffc542bff38 /* 33 vars */) = -1 ENOENT (No such file or directory) execve("/sbin/foo", ["./execvpe"], 0x7ffc542bff38 /* 33 vars */) = -1 ENOENT (No such file or directory) execve("/bin/foo", ["./execvpe"], 0x7ffc542bff38 /* 33 vars */) = -1 ENOENT (No such file or directory) This really seems much more like a glibc bug. The shell does it correctly... -Kees -- Kees Cook