From 9aa2dcffeec8a4694d162eb412c8df50ff1bd1c8 Mon Sep 17 00:00:00 2001 From: closer-claudio Date: Tue, 17 Nov 2026 01:17:00 -0800 Subject: [PATCH] ssh-agent-install no longer replaces an rc with less than it held strip_lines wrote its copy of the rc to .tmp, behind `|| true`, then wrote that copy over the rc. Two losses came of it. `|| true` was there for grep's exit 1 (no line left to keep) and passed over every other failure as well: in a home that takes no new file the copy was never made, and the redirection of the second command had emptied the rc before cat failed on the missing copy. An uninstall, or an install replacing an earlier install's export, left a writable rc at 0 bytes there. And an rc with a NUL byte in it (a key binding) is binary to grep, which prints one line, "Binary file matches", and exits 0: that line became the rc. The copy is now made with mktemp under TMPDIR, by grep -a; a grep status above 1 stops before the rc is touched; and a failed write through leaves the copy and names it, since writing through (which keeps a linked rc a link) cannot be atomic. test-binary.sh 6d holds each clause: a home of mode 0555 with a writable rc that carries the old export beside the user's lines, one of them quoting the export and one with a NUL byte, compared byte for byte after uninstall and after install; an rc holding the export alone; a grep that fails on the copy; a cat that fails on the write; and TMPDIR empty after every run that succeeds. 6b and 6c now also look for the mark line, which their pattern did not match. Twelve single-clause breakages of the installer each fail a row. Fixes https://github.com/forkcloser/openssh/issues/135 Co-Authored-By: Claude Fable 5.2 Signed-off-by: closer-claudio --- agent/ssh-agent-install | 23 +++++++++-- test-binary.sh | 84 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 102 insertions(+), 5 deletions(-) diff --git a/agent/ssh-agent-install b/agent/ssh-agent-install index 163c4fa..7f1afa6 100755 --- a/agent/ssh-agent-install +++ b/agent/ssh-agent-install @@ -44,10 +44,27 @@ done # strip_lines : the lines the install writes, and only those, taken # out; written through, so an rc that is a symlink into a dotfiles tree -# stays one. +# stays one. The copy is made outside the home, which may take no new file, +# and the rc is touched only once the copy is whole: grep exits 1 when no +# line is left to keep, and anything above that is a failure. -a, or a +# grep that takes the rc for binary (a NUL byte in a key binding) prints +# one line about it, exits 0, and that line becomes the rc. Writing through +# empties the rc first, so a write that fails leaves the copy and says +# where it is. strip_lines() { - grep -vxF -e "${rc_mark}" -e "${rc_line}" -e "${old_line}" "$1" > "$1.tmp" || true - cat "$1.tmp" > "$1" && rm -f "$1.tmp" + local copy status=0 + copy="$(mktemp "${TMPDIR:-/tmp}/ssh-agent-install.XXXXXX")" + grep -avxF -e "${rc_mark}" -e "${rc_line}" -e "${old_line}" -- "$1" > "${copy}" || status=$? + if [ "${status}" -gt 1 ]; then + rm -f "${copy}" + echo "ssh-agent-install: could not copy $1, left as it is" >&2 + return 1 + fi + if ! cat "${copy}" > "$1"; then + echo "ssh-agent-install: writing $1 failed; what it held, without the agent line, is in ${copy}: move it somewhere that is kept" >&2 + return 1 + fi + rm -f "${copy}" } # can_write : whether its lines can be put in or taken out. Not an rc diff --git a/test-binary.sh b/test-binary.sh index 3c3782a..e0cbc90 100755 --- a/test-binary.sh +++ b/test-binary.sh @@ -263,7 +263,7 @@ for kind in plain profile login; do [ "${made}" = "${own}" ] || [ ! -e "${made}" ] || { echo "ssh-agent-install (${kind}) created ${made##*/}" >&2; exit 1; } done HOME="${rc_home}" PATH="${work}/stub:/usr/bin:/bin" "${prefix}/bin/ssh-agent-install" uninstall > /dev/null - if grep -q 'org.forkcloser' "${written[@]}"; then + if grep -q -e 'org.forkcloser' -e 'forkcloser/openssh' "${written[@]}"; then echo "ssh-agent-install uninstall (${kind}) left its line in an rc" >&2 exit 1 fi @@ -324,7 +324,7 @@ for kind in readonly linked; do esac cksum "${target}" | cmp -s - "${work}/locked.sum" || { echo "ssh-agent-install uninstall (${kind}) changed an rc it cannot write" >&2; exit 1; } [ ! -e "${locked}.tmp" ] || { echo "ssh-agent-install uninstall (${kind}) left ${locked##*/}.tmp behind" >&2; exit 1; } - ! grep -q 'org.forkcloser' "${rc_home}/.bashrc" || { echo "ssh-agent-install uninstall (${kind}) left its line in an rc it can write" >&2; exit 1; } + ! grep -q -e 'org.forkcloser' -e 'forkcloser/openssh' "${rc_home}/.bashrc" || { echo "ssh-agent-install uninstall (${kind}) left its line in an rc it can write" >&2; exit 1; } done # And the line an rc may be left with that is not harmless: an earlier # install's export, in a file that has since been locked. Uninstall says it @@ -345,6 +345,86 @@ case "${said}" in *) ;; esac +# 6d. What the installer does to an rc it rewrites, an earlier install's +# export in it beside the user's own lines: the export goes, by uninstall +# and by an install that replaces it, and every other byte stays, a line +# that only quotes the export and a line with a NUL byte in it (a key +# binding) among them. In a home that takes no new file, too: the copy the +# installer works from is made under TMPDIR, not beside the rc, where +# failing to make it once emptied the file; and it is gone after the run. +rc_tmp="${work}/rc-tmp" +mkdir "${rc_tmp}" +printf '# mine\n# was: %s\nbindkey "^X\0" x\nalias kept=1\n' "${old_export}" > "${work}/rc.stripped" +for action in uninstall install; do + rc_home="${work}/home-closed-${action}" + mkdir -p "${rc_home}/.ssh" "${rc_home}/Library/LaunchAgents" "${rc_home}/Library/Logs/org.forkcloser.ssh_agent" + printf '# mine\n%s\n# was: %s\nbindkey "^X\0" x\nalias kept=1\n' "${old_export}" "${old_export}" > "${rc_home}/.zshrc" + chmod 0555 "${rc_home}" + status=0 + if [ "${action}" = uninstall ]; then + HOME="${rc_home}" TMPDIR="${rc_tmp}" PATH="${work}/stub:/usr/bin:/bin" "${prefix}/bin/ssh-agent-install" uninstall > /dev/null 2> "${work}/install.err" || status=$? + cp "${work}/rc.stripped" "${work}/rc.expected" + else + HOME="${rc_home}" TMPDIR="${rc_tmp}" PATH="${work}/stub:/usr/bin:/bin" "${prefix}/bin/ssh-agent-install" > /dev/null 2> "${work}/install.err" || status=$? + { cat "${work}/rc.stripped"; printf '\n# forkcloser/openssh: the agent the LaunchAgent runs\n%s\n' "${by_hand}"; } > "${work}/rc.expected" + fi + # Open again whatever happened: the trap removes the tree. + chmod 0755 "${rc_home}" + [ "${status}" -eq 0 ] || { cat "${work}/install.err" >&2; echo "ssh-agent-install ${action} stopped in a home that takes no new file" >&2; exit 1; } + cmp -s "${rc_home}/.zshrc" "${work}/rc.expected" || { echo "ssh-agent-install ${action} did not leave an rc it rewrites as it was, less the old export" >&2; exit 1; } + [ -z "$(ls -A "${rc_tmp}")" ] || { echo "ssh-agent-install ${action} left its copy of an rc behind" >&2; exit 1; } +done +# An rc that holds the export and nothing else: no line is left to keep, +# which is not a failure. +for action in uninstall install; do + rc_home="${work}/home-alone-${action}" + mkdir -p "${rc_home}/.ssh" + printf '%s\n' "${old_export}" > "${rc_home}/.zshrc" + if [ "${action}" = uninstall ]; then + HOME="${rc_home}" TMPDIR="${rc_tmp}" PATH="${work}/stub:/usr/bin:/bin" "${prefix}/bin/ssh-agent-install" uninstall > /dev/null || + { echo "ssh-agent-install uninstall stopped on an rc that holds the old export alone" >&2; exit 1; } + [ ! -s "${rc_home}/.zshrc" ] || { echo "ssh-agent-install uninstall left something in an rc that held the old export alone" >&2; exit 1; } + else + HOME="${rc_home}" TMPDIR="${rc_tmp}" PATH="${work}/stub:/usr/bin:/bin" "${prefix}/bin/ssh-agent-install" > /dev/null || + { echo "ssh-agent-install stopped on an rc that holds the old export alone" >&2; exit 1; } + grep -qxF -- "${by_hand}" "${rc_home}/.zshrc" || { echo "ssh-agent-install did not put its line in an rc that held the old export alone" >&2; exit 1; } + ! grep -qxF -- "${old_export}" "${rc_home}/.zshrc" || { echo "ssh-agent-install left the old export in an rc that held it alone" >&2; exit 1; } + fi +done +# The copy cannot be made (a grep that fails as on a full disk; the calls +# that only look go to the real one): the installer stops, and the rc is +# as it was. +mkdir "${work}/stub-grep" "${work}/stub-cat" +# shellcheck disable=SC2016 # the stub's own arguments, for the stub to expand +printf '#!/bin/sh\ncase "$1" in -*v*) exit 2 ;; *) exec /usr/bin/grep "$@" ;; esac\n' > "${work}/stub-grep/grep" +printf '#!/bin/sh\nexit 1\n' > "${work}/stub-cat/cat" +chmod 0755 "${work}/stub-grep/grep" "${work}/stub-cat/cat" +rc_home="${work}/home-nocopy" +mkdir -p "${rc_home}/.ssh" +printf '# mine\n%s\nalias kept=1\n' "${old_export}" > "${rc_home}/.zshrc" +cksum "${rc_home}/.zshrc" > "${work}/rc.sum" +if HOME="${rc_home}" TMPDIR="${rc_tmp}" PATH="${work}/stub-grep:${work}/stub:/usr/bin:/bin" "${prefix}/bin/ssh-agent-install" uninstall > /dev/null 2>&1; then + echo "ssh-agent-install uninstall went on without a copy of the rc it rewrites" >&2 + exit 1 +fi +cksum "${rc_home}/.zshrc" | cmp -s - "${work}/rc.sum" || { echo "ssh-agent-install uninstall changed an rc it could not copy" >&2; exit 1; } +[ -z "$(ls -A "${rc_tmp}")" ] || { echo "ssh-agent-install uninstall left a part of a copy behind" >&2; exit 1; } +# The copy is whole and writing it over the rc fails (a cat that fails; the +# shell has emptied the rc by then, as a full disk leaves it): the +# installer stops, and names the copy, which holds what the rc did. +rc_home="${work}/home-nowrite" +mkdir -p "${rc_home}/.ssh" +printf '# mine\n%s\nalias kept=1\n' "${old_export}" > "${rc_home}/.zshrc" +if HOME="${rc_home}" TMPDIR="${rc_tmp}" PATH="${work}/stub-cat:${work}/stub:/usr/bin:/bin" "${prefix}/bin/ssh-agent-install" uninstall > /dev/null 2> "${work}/install.err"; then + echo "ssh-agent-install uninstall passed over a failed write of an rc" >&2 + exit 1 +fi +copy="$(find "${rc_tmp}" -type f)" +[ -n "${copy}" ] || { echo "ssh-agent-install uninstall kept no copy of an rc it failed to write" >&2; exit 1; } +printf '# mine\nalias kept=1\n' | cmp -s - "${copy}" || { echo "ssh-agent-install uninstall kept no whole copy of an rc it failed to write" >&2; exit 1; } +grep -qF -- "${copy}" "${work}/install.err" || { cat "${work}/install.err" >&2; echo "ssh-agent-install uninstall did not name the copy of the rc it failed to write" >&2; exit 1; } +rm -f "${copy}" + # 7. The server, as this user, on an ephemeral port: the shipped sshd_config # is read from the package (no -f: the relocated default), the config test # passes with a fresh host key, and a login with a key from a throwaway