From a7c124c2d0fc594be56f7b6cfe44464dc5ba2412 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera Date: Sat, 9 Aug 2025 03:15:53 +0800 Subject: [PATCH] brew.sh: enforce `HOMEBREW_FORCE_BREW_WRAPPER` more strictly `HOMEBREW_FORCE_BREW_WRAPPER` can be used as a security/compliance feature, but allowing it to be disabled by setting `HOMEBREW_NO_FORCE_BREW_WRAPPER` leaves a pretty large hole in it that allows it to be sidestepped. Let's fix that by actually checking the path of the process that called `brew`, and the verify that that path matches the configured value of `HOMEBREW_NO_FORCE_BREW_WRAPPER`. --- Library/Homebrew/brew.sh | 45 +++++++++++++++--------------- Library/Homebrew/dev-cmd/tests.rb | 1 - Library/Homebrew/env_config.rb | 9 +----- Library/Homebrew/utils/pid_path.rb | 23 +++++++++++++++ bin/brew | 14 +++++----- 5 files changed, 53 insertions(+), 39 deletions(-) create mode 100755 Library/Homebrew/utils/pid_path.rb diff --git a/Library/Homebrew/brew.sh b/Library/Homebrew/brew.sh index 56f2a5d58a..76f760af9e 100644 --- a/Library/Homebrew/brew.sh +++ b/Library/Homebrew/brew.sh @@ -190,35 +190,32 @@ esac # Include some helper functions. source "${HOMEBREW_LIBRARY}/Homebrew/utils/helpers.sh" -# Require HOMEBREW_BREW_WRAPPER to be set if HOMEBREW_FORCE_BREW_WRAPPER is set -# (and HOMEBREW_NO_FORCE_BREW_WRAPPER is not set) for all non-trivial commands -# (i.e. not defined above this line e.g. formulae or --cellar). -if [[ -z "${HOMEBREW_NO_FORCE_BREW_WRAPPER:-}" && -n "${HOMEBREW_FORCE_BREW_WRAPPER:-}" ]] +# If HOMEBREW_FORCE_BREW_WRAPPER is set, verify that the path to our parent +# process is the same as the value of HOMEBREW_FORCE_BREW_WRAPPER for all +# non-trivial commands (i.e. not defined above this line e.g. formulae or --cellar). +if [[ -n "${HOMEBREW_FORCE_BREW_WRAPPER:-}" ]] then - HOMEBREW_FORCE_BREW_WRAPPER_WITHOUT_BREW="${HOMEBREW_FORCE_BREW_WRAPPER%/brew}" - if [[ -z "${HOMEBREW_BREW_WRAPPER:-}" ]] + if [[ -n "${HOMEBREW_MACOS:-}" ]] then + source "${HOMEBREW_LIBRARY}/Homebrew/utils/ruby.sh" + setup-ruby-path + HOMEBREW_BREW_CALLER="$("${HOMEBREW_RUBY_PATH}" "${HOMEBREW_LIBRARY}/Homebrew/utils/pid_path.rb" "${PPID}")" + else + HOMEBREW_BREW_CALLER="$(readlink -f "/proc/${PPID}/exe")" + fi + + if [[ "${HOMEBREW_BREW_CALLER:-}" != "${HOMEBREW_FORCE_BREW_WRAPPER}" ]] + then + HOMEBREW_FORCE_BREW_WRAPPER_WITHOUT_BREW="${HOMEBREW_FORCE_BREW_WRAPPER%/brew}" + + # HOMEBREW_ORIGINAL_BREW_FILE set by bin/brew + # shellcheck disable=SC2154 odie <