Skip to content

Commit d7dcf79

Browse files
committed
Support bash in privileged mode
1 parent 966f819 commit d7dcf79

File tree

7 files changed

+18
-10
lines changed

7 files changed

+18
-10
lines changed

Library/Homebrew/brew.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ numeric() {
216216
}
217217

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

221221
# Allow Azure Pipelines/GitHub Actions/Docker/Concourse/Kubernetes to do everything as root (as it's normal there)
222222
[[ -f /.dockerenv ]] && return

Library/Homebrew/global.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ def auditing?
106106
end
107107

108108
def running_as_root?
109-
@process_uid ||= Process.uid
110-
@process_uid.zero?
109+
@process_euid ||= Process.euid
110+
@process_euid.zero?
111111
end
112112

113113
def owner_uid

Library/Homebrew/shims/shared/curl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/bash -p
22

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

Library/Homebrew/shims/shared/git

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/bash -p
22

33
# This script because we support $HOMEBREW_GIT, $HOMEBREW_SVN, etc., Xcode-only and
44
# no Xcode/CLT configurations. Order is careful to be what the user would want.

Library/Homebrew/utils/fork.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ def self.safe_fork
4242
server.close
4343
read.close
4444
write.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
45+
46+
Process::UID.change_privilege(Process.euid) if Process.euid != Process.uid
47+
4548
yield
4649
rescue Exception => e # rubocop:disable Lint/RescueException
4750
error_hash = JSON.parse e.to_json

Library/Homebrew/utils/lock.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,17 @@ _create_lock() {
4444
[[ -x "${ruby}" ]] || ruby="$(type -P ruby)"
4545
[[ -x "${python}" ]] || python="$(type -P python)"
4646

47-
if [[ -x "${ruby}" ]] && "${ruby}" -e "exit(RUBY_VERSION >= '1.8.7')"
47+
# Use a bash subprocess to reset setuid, which Ruby can error if != euid.
48+
if [[ -x "${ruby}" ]] && /bin/bash -c "\"${ruby}\" -e \"exit(RUBY_VERSION >= '1.8.7')\""
4849
then
49-
"${ruby}" -e "File.new(${lock_fd}).flock(File::LOCK_EX | File::LOCK_NB) || exit(1)"
50+
/bin/bash <<-SCRIPT
51+
"${ruby}" -e "File.new(${lock_fd}).flock(File::LOCK_EX | File::LOCK_NB) || exit(1)"
52+
SCRIPT
5053
elif [[ -x "${python}" ]]
5154
then
52-
"${python}" -c "import fcntl; fcntl.flock(${lock_fd}, fcntl.LOCK_EX | fcntl.LOCK_NB)"
55+
/bin/bash <<-SCRIPT
56+
"${python}" -c "import fcntl; fcntl.flock(${lock_fd}, fcntl.LOCK_EX | fcntl.LOCK_NB)"
57+
SCRIPT
5358
elif [[ -x "$(type -P flock)" ]]
5459
then
5560
flock -n "${lock_fd}"

bin/brew

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/bash -p
22
set -u
33

44
# Fail fast with concise message when not using bash
@@ -245,4 +245,4 @@ then
245245
fi
246246
unset VAR ENV_VAR_NAMES
247247

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

0 commit comments

Comments
 (0)