Skip to content

Commit

Permalink
fault-injection: enhance failcmd to exit on non-hex address input
Browse files Browse the repository at this point in the history
The failcmd.sh script in the fault-injection toolkit does not currently
validate whether the provided address is in hexadecimal format.  This can
lead to silent failures if the address is sourced from places like
`/proc/kallsyms`, which omits the '0x' prefix, potentially causing users
to operate under incorrect assumptions.

Introduce a new function, `exit_if_not_hex`, which checks the format of
the provided address and exits with an error message if the address is not
a valid hexadecimal number.

This enhancement prevents users from running the command with improperly
formatted addresses, thus improving the robustness and usability of the
failcmd tool.

Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Breno Leitao <[email protected]>
Reviewed-by: Akinobu Mita <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
  • Loading branch information
leitao authored and akpm00 committed Sep 2, 2024
1 parent 588661f commit 11ee88a
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tools/testing/fault-injection/failcmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ ENVIRONMENT
EOF
}

exit_if_not_hex() {
local value="$1"
if ! [[ $value =~ ^0x[0-9a-fA-F]+$ ]]; then
echo "Error: The provided value '$value' is not a valid hexadecimal number." >&2
exit 1
fi
}

if [ $UID != 0 ]; then
echo must be run as root >&2
exit 1
Expand Down Expand Up @@ -160,18 +168,22 @@ while true; do
shift 2
;;
--require-start)
exit_if_not_hex "$2"
echo $2 > $FAULTATTR/require-start
shift 2
;;
--require-end)
exit_if_not_hex "$2"
echo $2 > $FAULTATTR/require-end
shift 2
;;
--reject-start)
exit_if_not_hex "$2"
echo $2 > $FAULTATTR/reject-start
shift 2
;;
--reject-end)
exit_if_not_hex "$2"
echo $2 > $FAULTATTR/reject-end
shift 2
;;
Expand Down

0 comments on commit 11ee88a

Please sign in to comment.