-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for unnecessary privilege escalation (#1743)
Resolves tiny-pilot/tinypilot-pro#1214 <s>Blocked by https://github.com/tiny-pilot/tinypilot/pull/1744</s> <s>Blocked by https://github.com/tiny-pilot/tinypilot/pull/1745</s> This PR adds a dev script that checks for possible cases of privilege escalation in tinypilot-writable scripts (i.e., `scripts/`). The script only does a superficial check that root privileges were at least considered by matching on: > This script doesn't require root privileges. Example output of `dev-scripts/check-privilege-guard`: ``` $ ./dev-scripts/check-privilege-guard These files are missing a guard against privilege escalation: scripts/is-ssh-enabled scripts/streaming-mode scripts/update-service scripts/upgrade Please add the following check (or similar) to the above scripts: if [[ "${EUID}" == 0 ]]; then >&2 echo "This script doesn't require root privileges." >&2 echo 'Please re-run as tinypilot:' >&2 echo " runuser tinypilot --command '$0 $*'" exit 1 fi ``` Notes 1. <s>These tinypilot-writable scripts legitimately require root privileges: * `scripts/install-bundle` * `script/upgrade` So they do risk being used for privilege escalation, but they are/should never be executed by privileged scripts on the device. I've also added a superficial check for this too.</s> 2. This PR also fixes the privilege escalation issues that `dev-scripts/check-privilege-guard` as picked up. As a reminder, the fix is a runtime error asking for reduced permissions which is something we'll only encounter when we physically test the device. So as a result, this PR also tries to avoid those runtime errors by running these identified scripts as `tinypilot` where needed: ``` runuser tinypilot --command '/opt/tinypilot/scripts/some-script' ``` <a data-ca-tag href="https://codeapprove.com/pr/tiny-pilot/tinypilot/1743"><img src="https://codeapprove.com/external/github-tag-allbg.png" alt="Review on CodeApprove" /></a>
- Loading branch information
1 parent
1916fb5
commit 7b36179
Showing
6 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/bin/bash | ||
# | ||
# Check that the TinyPilot scripts contain a guard against privilege escalation. | ||
# | ||
# This script enforces a pattern of ensuring that scripts which are writable by | ||
# the `tinypilot` user don't get executed with unnecessary root privileges. | ||
|
||
# Exit on first failing command. | ||
set -e | ||
|
||
# Exit on unset variable. | ||
set -u | ||
|
||
# Find TinyPilot scripts that don't guard against privilege escalation. | ||
MATCHES="$(grep \ | ||
--files-without-match \ | ||
--fixed-strings \ | ||
--regexp "This script doesn't require root privileges." \ | ||
scripts/*; true)" | ||
readonly MATCHES | ||
if [[ -n "${MATCHES}" ]]; then | ||
>&2 echo 'These files are missing a guard against privilege escalation:' | ||
>&2 echo "${MATCHES}" | ||
>&2 echo 'Please add the following check (or similar) to the above scripts:' | ||
>&2 cat <<'EOF' | ||
if [[ "${EUID}" == 0 ]]; then | ||
>&2 echo "This script doesn't require root privileges." | ||
>&2 echo 'Please re-run as tinypilot:' | ||
>&2 echo " runuser tinypilot --command '$0 $*'" | ||
exit 1 | ||
fi | ||
EOF | ||
exit 1 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters