gcc-plugins: randstruct: Only warn about true flexible arrays
authorKees Cook <keescook@chromium.org>
Sat, 4 Nov 2023 20:43:37 +0000 (13:43 -0700)
committerKees Cook <keescook@chromium.org>
Wed, 8 Nov 2023 22:18:57 +0000 (14:18 -0800)
The randstruct GCC plugin tried to discover "fake" flexible arrays
to issue warnings about them in randomized structs. In the future
LSM overhead reduction series, it would be legal to have a randomized
struct with a 1-element array, and this should _not_ be treated as a
flexible array, especially since commit df8fc4e934c1 ("kbuild: Enable
-fstrict-flex-arrays=3"). Disable the 0-sized and 1-element array
discovery logic in the plugin, but keep the "true" flexible array check.

Cc: KP Singh <kpsingh@kernel.org>
Cc: linux-hardening@vger.kernel.org
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202311021532.iBwuZUZ0-lkp@intel.com/
Fixes: df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")
Reviewed-by: Bill Wendling <morbo@google.com>
Acked-by: "Gustavo A. R. Silva" <gustavoars@kernel.org>
Link: https://lore.kernel.org/r/20231104204334.work.160-kees@kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
scripts/gcc-plugins/randomize_layout_plugin.c

index 366395cab490dec0cd80da7623727122438e78ab..910bd21d08f48db58d36db2a1222c2cca33782f4 100644 (file)
@@ -278,8 +278,6 @@ static bool is_flexible_array(const_tree field)
 {
        const_tree fieldtype;
        const_tree typesize;
-       const_tree elemtype;
-       const_tree elemsize;
 
        fieldtype = TREE_TYPE(field);
        typesize = TYPE_SIZE(fieldtype);
@@ -287,20 +285,12 @@ static bool is_flexible_array(const_tree field)
        if (TREE_CODE(fieldtype) != ARRAY_TYPE)
                return false;
 
-       elemtype = TREE_TYPE(fieldtype);
-       elemsize = TYPE_SIZE(elemtype);
-
        /* size of type is represented in bits */
 
        if (typesize == NULL_TREE && TYPE_DOMAIN(fieldtype) != NULL_TREE &&
            TYPE_MAX_VALUE(TYPE_DOMAIN(fieldtype)) == NULL_TREE)
                return true;
 
-       if (typesize != NULL_TREE &&
-           (TREE_CONSTANT(typesize) && (!tree_to_uhwi(typesize) ||
-            tree_to_uhwi(typesize) == tree_to_uhwi(elemsize))))
-               return true;
-
        return false;
 }