From c826e3137a2a09881631122d0670a10e8206fd77 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Thu, 13 Aug 2026 08:35:31 +0000 Subject: [PATCH] sh: fix command injection in bash Tab completion (compgen -W expansion) _module_comgen_words_and_files() fed untrusted text (module names read off disk, LOADEDMODULES, MODULEPATH) straight into `compgen -W`, which performs a full unquoted-word expansion on its wordlist -- including command substitution -- as a normal, documented part of its behavior. A module name, loaded-module entry, or MODULEPATH component containing e.g. `$(...)` therefore ran arbitrary shell code the moment a user pressed Tab. Fix: never hand candidate text to `compgen -W`. The new _module_comgen_words() splits the candidate list with `read -r -d ''` (pure IFS word-splitting, no expansion of any kind) and does the prefix match itself; _module_comgen_words_and_files() now layers the nospace-for-directory-entries behavior on top of it. Every call site that previously built a compgen -W wordlist from LOADEDMODULES, MODULEPATH, or a stash/save collection name now goes through one of these two functions instead. Cherry-picked and adapted from the fix already applied to the main branch, adjusted to this branch's completion script layout. Verified with a live bash session: an injected LOADEDMODULES entry is now listed as an inert candidate string, with no code execution. Assisted-by: Claude:claude-sonnet-5 Signed-off-by: Xavier Delaruelle --- init/bash_completion.in | 51 ++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/init/bash_completion.in b/init/bash_completion.in index 9cef0eac..94dd7c49 100644 --- a/init/bash_completion.in +++ b/init/bash_completion.in @@ -2,17 +2,30 @@ # # Bash commandline completion # +_module_comgen_words() { + local -a words + local val + # split candidate list on IFS without triggering shell expansion (command + # substitution, arithmetic, ...): candidate words may come from module + # names read off disk, which must never be treated as executable code + IFS=$' \t\n' read -r -d '' -a words <<<"$1" + for val in "${words[@]}"; do + case "$val" in + "$2"*) COMPREPLY[${#COMPREPLY[@]}]="$val" ;; + esac + done +} + _module_comgen_words_and_files() { - local k=0 - local setnospace=1 + local start=${#COMPREPLY[@]} i + _module_comgen_words "$1" "$2" # do not append space to word completed if it is a directory (ends with /) - for val in $(compgen -W "$1" -- "$2"); do - if [ $setnospace -eq 1 ] && [ "${val: -1:1}" = '/' ]; then + for ((i = start; i < ${#COMPREPLY[@]}; i++)); do + if [ "${COMPREPLY[i]: -1:1}" = '/' ]; then # Bash >=4.0 is required for compopt type compopt &>/dev/null && compopt -o nospace - setnospace=0 + break fi - COMPREPLY[k++]="$val" done } @@ -64,7 +77,7 @@ _module_long_arg_list() { _module_comgen_words_and_files "$(_module_not_yet_loaded "$cur")" "$cur" break;; rm|del|remove|unload|switch|swap) - COMPREPLY=( $(IFS=: compgen -W "${LOADEDMODULES}" -- "$cur") ) + _module_comgen_words "${LOADEDMODULES//:/ }" "$cur" break;; esac done @@ -85,14 +98,14 @@ _module() { list|savelist) COMPREPLY=( $(compgen -W "@comp_list_opts@" -- "$cur") );; stashlist) COMPREPLY=( $(compgen -W "@comp_stashlist_opts@" -- "$cur") );; stashpop|stashshow|stashrm) - COMPREPLY=( $(compgen -W "$(_module_stashlist)" -- "$cur") );; + _module_comgen_words "$(_module_stashlist)" "$cur";; clear) COMPREPLY=( $(compgen -W "@comp_clear_opts@" -- "$cur") );; restore|save|saveshow|describe|saverm|disable|is-saved) - COMPREPLY=( $(compgen -W "$(_module_savelist)" -- "$cur") );; + _module_comgen_words "$(_module_savelist)" "$cur";; rm|del|remove|unload) - COMPREPLY=( $(compgen -W "@comp_unload_opts@ ${LOADEDMODULES//:/ }" -- "$cur") );; - switch|swap) COMPREPLY=( $(compgen -W "@comp_load_opts@ ${LOADEDMODULES//:/ }" -- "$cur") );; - unuse|is-used) COMPREPLY=( $(IFS=: compgen -W "${MODULEPATH}" -- "$cur") );; + _module_comgen_words "@comp_unload_opts@ ${LOADEDMODULES//:/ }" "$cur";; + switch|swap) _module_comgen_words "@comp_load_opts@ ${LOADEDMODULES//:/ }" "$cur";; + unuse|is-used) _module_comgen_words "${MODULEPATH//:/ }" "$cur";; use|-a|--append|cachebuild) ;; # let readline handle the completion display|help|show|test|path|paths|is-loaded|info-loaded) _module_comgen_words_and_files "@comp_mfile_opts@ $(_module_avail "$cur")" "$cur";; @@ -148,14 +161,14 @@ if type -t ml >/dev/null; then list|savelist) COMPREPLY=( $(compgen -W "@comp_list_opts@" -- "$cur") );; stashlist) COMPREPLY=( $(compgen -W "@comp_stashlist_opts@" -- "$cur") );; stashpop|stashshow|stashrm) - COMPREPLY=( $(compgen -W "$(_module_stashlist)" -- "$cur") );; + _module_comgen_words "$(_module_stashlist)" "$cur";; clear) COMPREPLY=( $(compgen -W "@comp_clear_opts@" -- "$cur") );; restore|save|saveshow|describe|saverm|disable|is-saved) - COMPREPLY=( $(compgen -W "$(_module_savelist)" -- "$cur") );; + _module_comgen_words "$(_module_savelist)" "$cur";; rm|del|remove|unload) - COMPREPLY=( $(compgen -W "@comp_unload_opts@ ${LOADEDMODULES//:/ }" -- "$cur") );; - switch|swap) COMPREPLY=( $(compgen -W "@comp_load_opts@ ${LOADEDMODULES//:/ }" -- "$cur") );; - unuse|is-used) COMPREPLY=( $(IFS=: compgen -W "${MODULEPATH}" -- "$cur") );; + _module_comgen_words "@comp_unload_opts@ ${LOADEDMODULES//:/ }" "$cur";; + switch|swap) _module_comgen_words "@comp_load_opts@ ${LOADEDMODULES//:/ }" "$cur";; + unuse|is-used) _module_comgen_words "${MODULEPATH//:/ }" "$cur";; use|-a|--append|cachebuild) ;; # let readline handle the completion display|help|show|test|path|paths|is-loaded|info-loaded) _module_comgen_words_and_files "@comp_mfile_opts@ $(_module_avail "$cur")" "$cur";; @@ -192,14 +205,14 @@ if type -t ml >/dev/null; then for i in ${LOADEDMODULES//:/ }; do loaded_modules+="-${i} " done - COMPREPLY=( "${COMPREPLY[@]}" $(compgen -W "@comp_load_opts@ $loaded_modules" -- "$cur") );; + _module_comgen_words "@comp_load_opts@ $loaded_modules" "$cur";; *) _module_comgen_words_and_files "@comp_load_opts@ $(_module_not_yet_loaded "$cur")" "$cur" COMPREPLY=( "${COMPREPLY[@]}" $(compgen -W "@comp_opts@ @comp_cmds@" -- "$cur") ) loaded_modules="" for i in ${LOADEDMODULES//:/ }; do loaded_modules+="-${i} " done - COMPREPLY=( "${COMPREPLY[@]}" $(compgen -W "@comp_load_opts@ $loaded_modules" -- "$cur") );; + _module_comgen_words "@comp_load_opts@ $loaded_modules" "$cur";; esac fi;; esac -- 2.55.0