Debian Codegraph @

debian/patches/fix-command-injection-in-bash-Tab-completion-comp-v5.6.x.patch

raw
1 From c826e3137a2a09881631122d0670a10e8206fd77 Mon Sep 17 00:00:00 2001
2 From: Xavier Delaruelle <[email protected]>
3 Date: Thu, 13 Aug 2026 08:35:31 +0000
4 Subject: [PATCH] sh: fix command injection in bash Tab completion (compgen -W
5 expansion)
6
7 _module_comgen_words_and_files() fed untrusted text (module names read
8 off disk, LOADEDMODULES, MODULEPATH) straight into `compgen -W`, which
9 performs a full unquoted-word expansion on its wordlist -- including
10 command substitution -- as a normal, documented part of its behavior.
11 A module name, loaded-module entry, or MODULEPATH component containing
12 e.g. `$(...)` therefore ran arbitrary shell code the moment a user
13 pressed Tab.
14
15 Fix: never hand candidate text to `compgen -W`. The new
16 _module_comgen_words() splits the candidate list with `read -r -d ''`
17 (pure IFS word-splitting, no expansion of any kind) and does the
18 prefix match itself; _module_comgen_words_and_files() now layers the
19 nospace-for-directory-entries behavior on top of it. Every call site
20 that previously built a compgen -W wordlist from LOADEDMODULES,
21 MODULEPATH, or a stash/save collection name now goes through one of
22 these two functions instead.
23
24 Cherry-picked and adapted from the fix already applied to the main
25 branch, adjusted to this branch's completion script layout.
26
27 Verified with a live bash session: an injected LOADEDMODULES entry is
28 now listed as an inert candidate string, with no code execution.
29
30 Assisted-by: Claude:claude-sonnet-5
31 Signed-off-by: Xavier Delaruelle <[email protected]>
32 ---
33 init/bash_completion.in | 51 ++++++++++++++++++++++++++---------------
34 1 file changed, 32 insertions(+), 19 deletions(-)
35
36 diff --git a/init/bash_completion.in b/init/bash_completion.in
37 index 9cef0eac..94dd7c49 100644
38 --- a/init/bash_completion.in
39 +++ b/init/bash_completion.in
40 @@ -2,17 +2,30 @@
41 #
42 # Bash commandline completion
43 #
44 +_module_comgen_words() {
45 + local -a words
46 + local val
47 + # split candidate list on IFS without triggering shell expansion (command
48 + # substitution, arithmetic, ...): candidate words may come from module
49 + # names read off disk, which must never be treated as executable code
50 + IFS=$' \t\n' read -r -d '' -a words <<<"$1"
51 + for val in "${words[@]}"; do
52 + case "$val" in
53 + "$2"*) COMPREPLY[${#COMPREPLY[@]}]="$val" ;;
54 + esac
55 + done
56 +}
57 +
58 _module_comgen_words_and_files() {
59 - local k=0
60 - local setnospace=1
61 + local start=${#COMPREPLY[@]} i
62 + _module_comgen_words "$1" "$2"
63 # do not append space to word completed if it is a directory (ends with /)
64 - for val in $(compgen -W "$1" -- "$2"); do
65 - if [ $setnospace -eq 1 ] && [ "${val: -1:1}" = '/' ]; then
66 + for ((i = start; i < ${#COMPREPLY[@]}; i++)); do
67 + if [ "${COMPREPLY[i]: -1:1}" = '/' ]; then
68 # Bash >=4.0 is required for compopt
69 type compopt &>/dev/null && compopt -o nospace
70 - setnospace=0
71 + break
72 fi
73 - COMPREPLY[k++]="$val"
74 done
75 }
76
77 @@ -64,7 +77,7 @@ _module_long_arg_list() {
78 _module_comgen_words_and_files "$(_module_not_yet_loaded "$cur")" "$cur"
79 break;;
80 rm|del|remove|unload|switch|swap)
81 - COMPREPLY=( $(IFS=: compgen -W "${LOADEDMODULES}" -- "$cur") )
82 + _module_comgen_words "${LOADEDMODULES//:/ }" "$cur"
83 break;;
84 esac
85 done
86 @@ -85,14 +98,14 @@ _module() {
87 list|savelist) COMPREPLY=( $(compgen -W "@comp_list_opts@" -- "$cur") );;
88 stashlist) COMPREPLY=( $(compgen -W "@comp_stashlist_opts@" -- "$cur") );;
89 stashpop|stashshow|stashrm)
90 - COMPREPLY=( $(compgen -W "$(_module_stashlist)" -- "$cur") );;
91 + _module_comgen_words "$(_module_stashlist)" "$cur";;
92 clear) COMPREPLY=( $(compgen -W "@comp_clear_opts@" -- "$cur") );;
93 restore|save|saveshow|describe|saverm|disable|is-saved)
94 - COMPREPLY=( $(compgen -W "$(_module_savelist)" -- "$cur") );;
95 + _module_comgen_words "$(_module_savelist)" "$cur";;
96 rm|del|remove|unload)
97 - COMPREPLY=( $(compgen -W "@comp_unload_opts@ ${LOADEDMODULES//:/ }" -- "$cur") );;
98 - switch|swap) COMPREPLY=( $(compgen -W "@comp_load_opts@ ${LOADEDMODULES//:/ }" -- "$cur") );;
99 - unuse|is-used) COMPREPLY=( $(IFS=: compgen -W "${MODULEPATH}" -- "$cur") );;
100 + _module_comgen_words "@comp_unload_opts@ ${LOADEDMODULES//:/ }" "$cur";;
101 + switch|swap) _module_comgen_words "@comp_load_opts@ ${LOADEDMODULES//:/ }" "$cur";;
102 + unuse|is-used) _module_comgen_words "${MODULEPATH//:/ }" "$cur";;
103 use|-a|--append|cachebuild) ;; # let readline handle the completion
104 display|help|show|test|path|paths|is-loaded|info-loaded)
105 _module_comgen_words_and_files "@comp_mfile_opts@ $(_module_avail "$cur")" "$cur";;
106 @@ -148,14 +161,14 @@ if type -t ml >/dev/null; then
107 list|savelist) COMPREPLY=( $(compgen -W "@comp_list_opts@" -- "$cur") );;
108 stashlist) COMPREPLY=( $(compgen -W "@comp_stashlist_opts@" -- "$cur") );;
109 stashpop|stashshow|stashrm)
110 - COMPREPLY=( $(compgen -W "$(_module_stashlist)" -- "$cur") );;
111 + _module_comgen_words "$(_module_stashlist)" "$cur";;
112 clear) COMPREPLY=( $(compgen -W "@comp_clear_opts@" -- "$cur") );;
113 restore|save|saveshow|describe|saverm|disable|is-saved)
114 - COMPREPLY=( $(compgen -W "$(_module_savelist)" -- "$cur") );;
115 + _module_comgen_words "$(_module_savelist)" "$cur";;
116 rm|del|remove|unload)
117 - COMPREPLY=( $(compgen -W "@comp_unload_opts@ ${LOADEDMODULES//:/ }" -- "$cur") );;
118 - switch|swap) COMPREPLY=( $(compgen -W "@comp_load_opts@ ${LOADEDMODULES//:/ }" -- "$cur") );;
119 - unuse|is-used) COMPREPLY=( $(IFS=: compgen -W "${MODULEPATH}" -- "$cur") );;
120 + _module_comgen_words "@comp_unload_opts@ ${LOADEDMODULES//:/ }" "$cur";;
121 + switch|swap) _module_comgen_words "@comp_load_opts@ ${LOADEDMODULES//:/ }" "$cur";;
122 + unuse|is-used) _module_comgen_words "${MODULEPATH//:/ }" "$cur";;
123 use|-a|--append|cachebuild) ;; # let readline handle the completion
124 display|help|show|test|path|paths|is-loaded|info-loaded)
125 _module_comgen_words_and_files "@comp_mfile_opts@ $(_module_avail "$cur")" "$cur";;
126 @@ -192,14 +205,14 @@ if type -t ml >/dev/null; then
127 for i in ${LOADEDMODULES//:/ }; do
128 loaded_modules+="-${i} "
129 done
130 - COMPREPLY=( "${COMPREPLY[@]}" $(compgen -W "@comp_load_opts@ $loaded_modules" -- "$cur") );;
131 + _module_comgen_words "@comp_load_opts@ $loaded_modules" "$cur";;
132 *) _module_comgen_words_and_files "@comp_load_opts@ $(_module_not_yet_loaded "$cur")" "$cur"
133 COMPREPLY=( "${COMPREPLY[@]}" $(compgen -W "@comp_opts@ @comp_cmds@" -- "$cur") )
134 loaded_modules=""
135 for i in ${LOADEDMODULES//:/ }; do
136 loaded_modules+="-${i} "
137 done
138 - COMPREPLY=( "${COMPREPLY[@]}" $(compgen -W "@comp_load_opts@ $loaded_modules" -- "$cur") );;
139 + _module_comgen_words "@comp_load_opts@ $loaded_modules" "$cur";;
140 esac
141 fi;;
142 esac
143 --
144 2.55.0
145