Skip to content

Commit a1cb45f

Browse files
authored
Merge pull request #16948 from Homebrew/privileged-bash
Support bash in privileged mode
2 parents 6eb589f + 636d883 commit a1cb45f

File tree

7 files changed

+15
-9
lines changed

7 files changed

+15
-9
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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ _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 /dev/stdin, otherwise Ruby can error if uid != euid.
48+
# Can't use "-" as that's also blocked:
49+
# https://github.com/ruby/ruby/blob/e51435177e88fc845528dff7cf2bc2b75dd36144/ruby.c#L2333-L2335
50+
if [[ -x "${ruby}" ]] && "${ruby}" /dev/stdin <<<"exit(RUBY_VERSION >= '1.8.7')"
4851
then
49-
"${ruby}" -e "File.new(${lock_fd}).flock(File::LOCK_EX | File::LOCK_NB) || exit(1)"
52+
"${ruby}" /dev/stdin <<<"File.new(${lock_fd}).flock(File::LOCK_EX | File::LOCK_NB) || exit(1)"
5053
elif [[ -x "${python}" ]]
5154
then
5255
"${python}" -c "import fcntl; fcntl.flock(${lock_fd}, fcntl.LOCK_EX | fcntl.LOCK_NB)"

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)