coccinelle: semantic patch to check for potential struct_size calls
authorJacob Keller <jacob.e.keller@intel.com>
Mon, 27 Feb 2023 20:24:28 +0000 (12:24 -0800)
committerKees Cook <keescook@chromium.org>
Thu, 29 Feb 2024 21:38:01 +0000 (13:38 -0800)
commite7549481255167dcdab355c539562c7ace17e111
treeabc01d0800b8e65bc2bccad2be0aeda1981535ca
parent1d02f252339e2eaf3ba35b9dc77e7a1a9aa7414c
coccinelle: semantic patch to check for potential struct_size calls

include/linux/overflow.h includes helper macros intended for calculating
sizes of allocations. These macros prevent accidental overflow by
saturating at SIZE_MAX.

In general when calculating such sizes use of the macros is preferred. Add
a semantic patch which can detect code patterns which can be replaced by
struct_size.

Note that I set the confidence to medium because this patch doesn't make an
attempt to ensure that the relevant array is actually a flexible array. The
struct_size macro does specifically require a flexible array. In many cases
the detected code could be refactored to a flexible array, but this is not
always possible (such as if there are multiple over-allocations).

Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
Link: https://lore.kernel.org/r/20230227202428.3657443-1-jacob.e.keller@intel.com
Signed-off-by: Kees Cook <keescook@chromium.org>
scripts/coccinelle/misc/struct_size.cocci [new file with mode: 0644]