Skip to content

Support bash in privileged mode #16948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Library/Homebrew/brew.sh
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
7 changes: 5 additions & 2 deletions Library/Homebrew/utils/lock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ _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 /dev/stdin, otherwise Ruby can error if uid != euid.
# Can't use "-" as that's also blocked:
# https://github.com/ruby/ruby/blob/e51435177e88fc845528dff7cf2bc2b75dd36144/ruby.c#L2333-L2335
if [[ -x "${ruby}" ]] && "${ruby}" /dev/stdin <<<"exit(RUBY_VERSION >= '1.8.7')"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if [[ -x "${ruby}" ]] && "${ruby}" /dev/stdin <<<"exit(RUBY_VERSION >= '1.8.7')"
if [[ -x "${ruby}" ]] && "${ruby}" - <<<"exit(RUBY_VERSION >= '1.8.7')"

This is equivalent, no? Though I suppose /dev/stdin is more explicit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've adjusted the comment to mention that

then
"${ruby}" -e "File.new(${lock_fd}).flock(File::LOCK_EX | File::LOCK_NB) || exit(1)"
"${ruby}" /dev/stdin <<<"File.new(${lock_fd}).flock(File::LOCK_EX | File::LOCK_NB) || exit(1)"
elif [[ -x "${python}" ]]
then
"${python}" -c "import fcntl; fcntl.flock(${lock_fd}, fcntl.LOCK_EX | fcntl.LOCK_NB)"
Expand Down
4 changes: 2 additions & 2 deletions bin/brew
Original file line number Diff line number Diff line change
@@ -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" "$@"