Skip to content

Commit

Permalink
Support bash in privileged mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo98 committed Mar 25, 2024
1 parent 966f819 commit d7dcf79
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Library/Homebrew/brew.sh
Expand Up @@ -216,7 +216,7 @@ numeric() {
}

check-run-command-as-root() {
[[ "$(id -u)" == 0 ]] || return
[[ "$(id -u)" == 0 || "$(id -ur)" == 0 ]] || return

# Allow Azure Pipelines/GitHub Actions/Docker/Concourse/Kubernetes to do everything as root (as it's normal there)
[[ -f /.dockerenv ]] && return
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/global.rb
Expand Up @@ -106,8 +106,8 @@ def auditing?
end

def running_as_root?
@process_uid ||= Process.uid
@process_uid.zero?
@process_euid ||= Process.euid
@process_euid.zero?
end

def owner_uid
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/shims/shared/curl
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash -p

# Make our $HOMEBREW_CURL selection universal - including in formulae usage.

Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/shims/shared/git
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash -p

# This script because we support $HOMEBREW_GIT, $HOMEBREW_SVN, etc., Xcode-only and
# no Xcode/CLT configurations. Order is careful to be what the user would want.
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/utils/fork.rb
Expand Up @@ -42,6 +42,9 @@ def self.safe_fork
server.close
read.close
write.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)

Process::UID.change_privilege(Process.euid) if Process.euid != Process.uid

yield
rescue Exception => e # rubocop:disable Lint/RescueException
error_hash = JSON.parse e.to_json
Expand Down
11 changes: 8 additions & 3 deletions Library/Homebrew/utils/lock.sh
Expand Up @@ -44,12 +44,17 @@ _create_lock() {
[[ -x "${ruby}" ]] || ruby="$(type -P ruby)"
[[ -x "${python}" ]] || python="$(type -P python)"

if [[ -x "${ruby}" ]] && "${ruby}" -e "exit(RUBY_VERSION >= '1.8.7')"
# Use a bash subprocess to reset setuid, which Ruby can error if != euid.
if [[ -x "${ruby}" ]] && /bin/bash -c "\"${ruby}\" -e \"exit(RUBY_VERSION >= '1.8.7')\""
then
"${ruby}" -e "File.new(${lock_fd}).flock(File::LOCK_EX | File::LOCK_NB) || exit(1)"
/bin/bash <<-SCRIPT
"${ruby}" -e "File.new(${lock_fd}).flock(File::LOCK_EX | File::LOCK_NB) || exit(1)"
SCRIPT
elif [[ -x "${python}" ]]
then
"${python}" -c "import fcntl; fcntl.flock(${lock_fd}, fcntl.LOCK_EX | fcntl.LOCK_NB)"
/bin/bash <<-SCRIPT
"${python}" -c "import fcntl; fcntl.flock(${lock_fd}, fcntl.LOCK_EX | fcntl.LOCK_NB)"
SCRIPT
elif [[ -x "$(type -P flock)" ]]
then
flock -n "${lock_fd}"
Expand Down
4 changes: 2 additions & 2 deletions bin/brew
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash -p
set -u

# Fail fast with concise message when not using bash
Expand Down Expand Up @@ -245,4 +245,4 @@ then
fi
unset VAR ENV_VAR_NAMES

exec /usr/bin/env -i "${FILTERED_ENV[@]}" /bin/bash "${HOMEBREW_LIBRARY}/Homebrew/brew.sh" "$@"
exec /usr/bin/env -i "${FILTERED_ENV[@]}" /bin/bash -p "${HOMEBREW_LIBRARY}/Homebrew/brew.sh" "$@"

0 comments on commit d7dcf79

Please sign in to comment.