From 46a6ada904f318c9c6a3b894815418727570985a Mon Sep 17 00:00:00 2001 From: Jan Nidzwetzki Date: Thu, 16 Feb 2023 15:38:58 +0100 Subject: [PATCH] Added coccinelle rule to find strlcpy on NameData NameData is a fixed-size type of 64 bytes. Using strlcpy to copy data into a NameData struct can cause problems because any data that follows the initial null-terminated string will also be part of the data. --- coccinelle/namedata.cocci | 24 ++++++++++++++++++++++++ scripts/coccinelle.sh | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 coccinelle/namedata.cocci diff --git a/coccinelle/namedata.cocci b/coccinelle/namedata.cocci new file mode 100644 index 00000000000..cc309f2644e --- /dev/null +++ b/coccinelle/namedata.cocci @@ -0,0 +1,24 @@ +// NameData is a fixed-size type of 64 bytes. Using strlcpy to copy data into a +// NameData struct can cause problems because any data that follows the initial +// null-terminated string will also be part of the data. + +@rule_var_decl_struct@ +symbol NAMEDATALEN; +identifier I1, I2; +@@ +struct I1 +{ + ... +- char I2[NAMEDATALEN]; ++ /* You are declaring a char of length NAMEDATALEN, please consider using NameData instead. */ ++ NameData I2; + ... +} + +@rule_namedata_strlcpy@ +expression E1, E2; +symbol NAMEDATALEN; +@@ +- strlcpy(E1, E2, NAMEDATALEN); ++ /* You are using strlcpy with NAMEDATALEN, please consider using NameData and namestrcpy instead. */ ++ namestrcpy(E1, E2); diff --git a/scripts/coccinelle.sh b/scripts/coccinelle.sh index ad2c20d1c80..72ef5a4dbc2 100755 --- a/scripts/coccinelle.sh +++ b/scripts/coccinelle.sh @@ -7,7 +7,7 @@ FAILED=false true > coccinelle.diff for f in "${SCRIPT_DIR}"/../coccinelle/*.cocci; do - find "${SCRIPT_DIR}"/.. -name '*.c' -exec spatch --very-quiet -sp_file "$f" {} + | tee -a coccinelle.diff + spatch --very-quiet --include-headers --sp-file "$f" --dir "${SCRIPT_DIR}"/.. | tee -a coccinelle.diff rc=$? if [ $rc -ne 0 ]; then FAILED=true