Skip to content

Commit 6cd5ab4

Browse files
fix(acfs): prevent wrapper sudo password prompts
1 parent e8312bd commit 6cd5ab4

4 files changed

Lines changed: 47 additions & 8 deletions

File tree

scripts/acfs-global

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,12 +1037,12 @@ main() {
10371037
fi
10381038
found_candidates=1
10391039
echo " - $candidate_user: $candidate_bin" >&2
1040-
echo " Try: sudo -u \"$candidate_user\" -H -- \"$0\" [args...]" >&2
1040+
echo " Try: sudo -n -u \"$candidate_user\" -H -- \"$0\" [args...]" >&2
10411041
done < <(getent_passwd_entry 2>/dev/null || true)
10421042

10431043
echo "" >&2
10441044
echo "If ACFS was installed for a different user, run this wrapper as that user, e.g.:" >&2
1045-
echo " sudo -u <user> -H -- \"$0\" [args...]" >&2
1045+
echo " sudo -n -u <user> -H -- \"$0\" [args...]" >&2
10461046
echo "" >&2
10471047
echo "You can also override state discovery:" >&2
10481048
echo " ACFS_STATE_FILE=/path/to/state.json \"$0\" [args...]" >&2
@@ -1102,6 +1102,7 @@ main() {
11021102
local env_bin=""
11031103
local runuser_bin=""
11041104
local sudo_bin=""
1105+
local true_bin=""
11051106

11061107
sanitized_state_file="$(sanitize_abs_nonroot_path "${ACFS_STATE_FILE:-}" 2>/dev/null || true)"
11071108
sanitized_system_state_file="$(sanitize_abs_nonroot_path "${ACFS_SYSTEM_STATE_FILE:-}" 2>/dev/null || true)"
@@ -1155,7 +1156,7 @@ main() {
11551156

11561157
sudo_bin="$(system_binary_path sudo 2>/dev/null || true)"
11571158
if [[ -n "$sudo_bin" ]]; then
1158-
exec "$sudo_bin" -u "$user" -H "${cmd_with_env[@]}"
1159+
exec "$sudo_bin" -n -u "$user" -H "${cmd_with_env[@]}"
11591160
fi
11601161

11611162
echo "Error: need runuser or sudo to run acfs as '$user'" >&2
@@ -1168,8 +1169,16 @@ main() {
11681169
exit 1
11691170
fi
11701171

1172+
true_bin="$(system_binary_path true 2>/dev/null || true)"
1173+
true_bin="${true_bin:-true}"
1174+
if ! "$sudo_bin" -n -u "$user" -H "$true_bin" 2>/dev/null; then
1175+
echo "Error: passwordless sudo is required to run acfs as '$user' from this account." >&2
1176+
echo "Log in as '$user' and rerun acfs, or run it from root/noninteractive sudo." >&2
1177+
exit 1
1178+
fi
1179+
11711180
echo "Note: Running acfs as '$user' (ACFS owner)" >&2
1172-
exec "$sudo_bin" -u "$user" -H "${cmd_with_env[@]}"
1181+
exec "$sudo_bin" -n -u "$user" -H "${cmd_with_env[@]}"
11731182
}
11741183

11751184
# If owner detection failed or we're already the target user, run directly

scripts/acfs-update

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,7 @@ exec_as_target_user() {
937937
local env_bin=""
938938
local runuser_bin=""
939939
local sudo_bin=""
940+
local true_bin=""
940941

941942
sanitized_state_file="$(sanitize_abs_nonroot_path "${ACFS_STATE_FILE:-}" 2>/dev/null || true)"
942943
sanitized_system_state_file="$(sanitize_abs_nonroot_path "${ACFS_SYSTEM_STATE_FILE:-}" 2>/dev/null || true)"
@@ -990,7 +991,7 @@ exec_as_target_user() {
990991

991992
sudo_bin="$(system_binary_path sudo 2>/dev/null || true)"
992993
if [[ -n "$sudo_bin" ]]; then
993-
exec "$sudo_bin" -u "$user" -H "${cmd_with_env[@]}"
994+
exec "$sudo_bin" -n -u "$user" -H "${cmd_with_env[@]}"
994995
fi
995996

996997
echo "Error: need runuser or sudo to run acfs-update as '$user'" >&2
@@ -1003,8 +1004,16 @@ exec_as_target_user() {
10031004
exit 1
10041005
fi
10051006

1007+
true_bin="$(system_binary_path true 2>/dev/null || true)"
1008+
true_bin="${true_bin:-true}"
1009+
if ! "$sudo_bin" -n -u "$user" -H "$true_bin" 2>/dev/null; then
1010+
echo "Error: passwordless sudo is required to run acfs-update as '$user' from this account." >&2
1011+
echo "Log in as '$user' and rerun acfs-update, or run it from root/noninteractive sudo." >&2
1012+
exit 1
1013+
fi
1014+
10061015
echo "Note: Running acfs-update as '$user' (ACFS owner)" >&2
1007-
exec "$sudo_bin" -u "$user" -H "${cmd_with_env[@]}"
1016+
exec "$sudo_bin" -n -u "$user" -H "${cmd_with_env[@]}"
10081017
}
10091018

10101019
main() {

scripts/generated/internal_checksums.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ declare -gA ACFS_INTERNAL_CHECKSUMS=(
2424
[scripts/lib/user.sh]="03fb18f299da1f0e27a87ddd0e0ce47027a6597e11fbf07ee3c8c53edae7d501"
2525
[scripts/lib/tools.sh]="e2d48e800888b759245e66cc9d86d097d83bf10f6d3bd2fcd17ec4bddfec94e0"
2626
[scripts/lib/export-config.sh]="6280123436223b5708f506b0a18b83c64891f8ee55af93c5c513f7fb202b97e3"
27-
[scripts/acfs-global]="593829bc77d2f2e65f012fa09446937ae4620e0d85e3d5534f499bc7f507b596"
28-
[scripts/acfs-update]="3ab9578caa95e7866a706c543bc8af9302ccf20538b716cca621e5c037ee24b0"
27+
[scripts/acfs-global]="1b7b9d64efa06fd2a31e373b905223373ae71d7e875a34b5a29d1a5e0001148e"
28+
[scripts/acfs-update]="a3001699d8967640291b414c7a29b48cdeecac0549ffa29e6242cb43297f2c0f"
2929
)
3030

3131
ACFS_INTERNAL_CHECKSUMS_COUNT=18

tests/unit/lib/test_update.bats

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4859,6 +4859,27 @@ EOF
48594859
done
48604860
}
48614861

4862+
@test "global wrappers use noninteractive sudo for cross-user re-exec" {
4863+
local update_wrapper="$PROJECT_ROOT/scripts/acfs-update"
4864+
local global_wrapper="$PROJECT_ROOT/scripts/acfs-global"
4865+
4866+
run grep -F 'exec "$sudo_bin" -n -u "$user" -H "${cmd_with_env[@]}"' "$update_wrapper"
4867+
assert_success
4868+
run grep -F 'if ! "$sudo_bin" -n -u "$user" -H "$true_bin" 2>/dev/null; then' "$update_wrapper"
4869+
assert_success
4870+
run grep -F 'passwordless sudo is required to run acfs-update as' "$update_wrapper"
4871+
assert_success
4872+
4873+
run grep -F 'exec "$sudo_bin" -n -u "$user" -H "${cmd_with_env[@]}"' "$global_wrapper"
4874+
assert_success
4875+
run grep -F 'if ! "$sudo_bin" -n -u "$user" -H "$true_bin" 2>/dev/null; then' "$global_wrapper"
4876+
assert_success
4877+
run grep -F 'passwordless sudo is required to run acfs as' "$global_wrapper"
4878+
assert_success
4879+
run grep -F 'sudo -n -u <user> -H -- \"$0\" [args...]' "$global_wrapper"
4880+
assert_success
4881+
}
4882+
48624883
@test "ACFS home resolvers honor explicit TARGET_HOME over stale system state" {
48634884
local current_home
48644885
local target_home

0 commit comments

Comments
 (0)