Skip to content

Commit 56c27f5

Browse files
keesUlrich Hecht
authored andcommitted
randstruct: gcc-plugin: Fix attribute addition
[ Upstream commit f39f18f3c3531aa802b58a20d39d96e82eb96c14 ] Based on changes in the 2021 public version of the randstruct out-of-tree GCC plugin[1], more carefully update the attributes on resulting decls, to avoid tripping checks in GCC 15's comptypes_check_enum_int() when it has been configured with "--enable-checking=misc": arch/arm64/kernel/kexec_image.c:132:14: internal compiler error: in comptypes_check_enum_int, at c/c-typeck.cc:1519 132 | const struct kexec_file_ops kexec_image_ops = { | ^~~~~~~~~~~~~~ internal_error(char const*, ...), at gcc/gcc/diagnostic-global-context.cc:517 fancy_abort(char const*, int, char const*), at gcc/gcc/diagnostic.cc:1803 comptypes_check_enum_int(tree_node*, tree_node*, bool*), at gcc/gcc/c/c-typeck.cc:1519 ... Link: https://archive.org/download/grsecurity/grsecurity-3.1-5.10.41-202105280954.patch.gz [1] Reported-by: Thiago Jung Bauermann <thiago.bauermann@linaro.org> Closes: KSPP/linux#367 Closes: https://lore.kernel.org/lkml/20250530000646.104457-1-thiago.bauermann@linaro.org/ Reported-by: Ingo Saitz <ingo@hannover.ccc.de> Closes: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1104745 Fixes: 313dd1b ("gcc-plugins: Add the randstruct plugin") Tested-by: Thiago Jung Bauermann <thiago.bauermann@linaro.org> Link: https://lore.kernel.org/r/20250530221824.work.623-kees@kernel.org Signed-off-by: Kees Cook <kees@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Ulrich Hecht <uli@kernel.org>
1 parent 65100f7 commit 56c27f5

2 files changed

Lines changed: 43 additions & 11 deletions

File tree

scripts/gcc-plugins/gcc-common.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,38 @@ static inline tree build_const_char_string(int len, const char *str)
182182
return cstr;
183183
}
184184

185+
static inline void __add_type_attr(tree type, const char *attr, tree args)
186+
{
187+
tree oldattr;
188+
189+
if (type == NULL_TREE)
190+
return;
191+
oldattr = lookup_attribute(attr, TYPE_ATTRIBUTES(type));
192+
if (oldattr != NULL_TREE) {
193+
gcc_assert(TREE_VALUE(oldattr) == args || TREE_VALUE(TREE_VALUE(oldattr)) == TREE_VALUE(args));
194+
return;
195+
}
196+
197+
TYPE_ATTRIBUTES(type) = copy_list(TYPE_ATTRIBUTES(type));
198+
TYPE_ATTRIBUTES(type) = tree_cons(get_identifier(attr), args, TYPE_ATTRIBUTES(type));
199+
}
200+
201+
static inline void add_type_attr(tree type, const char *attr, tree args)
202+
{
203+
tree main_variant = TYPE_MAIN_VARIANT(type);
204+
205+
__add_type_attr(TYPE_CANONICAL(type), attr, args);
206+
__add_type_attr(TYPE_CANONICAL(main_variant), attr, args);
207+
__add_type_attr(main_variant, attr, args);
208+
209+
for (type = TYPE_NEXT_VARIANT(main_variant); type; type = TYPE_NEXT_VARIANT(type)) {
210+
if (!lookup_attribute(attr, TYPE_ATTRIBUTES(type)))
211+
TYPE_ATTRIBUTES(type) = TYPE_ATTRIBUTES(main_variant);
212+
213+
__add_type_attr(TYPE_CANONICAL(type), attr, args);
214+
}
215+
}
216+
185217
#define PASS_INFO(NAME, REF, ID, POS) \
186218
struct register_pass_info NAME##_pass_info = { \
187219
.pass = make_##NAME##_pass(), \

scripts/gcc-plugins/randomize_layout_plugin.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ static tree handle_randomize_layout_attr(tree *node, tree name, tree args, int f
9595

9696
if (TYPE_P(*node)) {
9797
type = *node;
98+
} else if (TREE_CODE(*node) == FIELD_DECL) {
99+
*no_add_attrs = false;
100+
return NULL_TREE;
98101
} else {
99102
gcc_assert(TREE_CODE(*node) == TYPE_DECL);
100103
type = TREE_TYPE(*node);
@@ -381,15 +384,14 @@ static int relayout_struct(tree type)
381384
TREE_CHAIN(newtree[i]) = newtree[i+1];
382385
TREE_CHAIN(newtree[num_fields - 1]) = NULL_TREE;
383386

387+
add_type_attr(type, "randomize_performed", NULL_TREE);
388+
add_type_attr(type, "designated_init", NULL_TREE);
389+
if (has_flexarray)
390+
add_type_attr(type, "has_flexarray", NULL_TREE);
391+
384392
main_variant = TYPE_MAIN_VARIANT(type);
385-
for (variant = main_variant; variant; variant = TYPE_NEXT_VARIANT(variant)) {
393+
for (variant = main_variant; variant; variant = TYPE_NEXT_VARIANT(variant))
386394
TYPE_FIELDS(variant) = newtree[0];
387-
TYPE_ATTRIBUTES(variant) = copy_list(TYPE_ATTRIBUTES(variant));
388-
TYPE_ATTRIBUTES(variant) = tree_cons(get_identifier("randomize_performed"), NULL_TREE, TYPE_ATTRIBUTES(variant));
389-
TYPE_ATTRIBUTES(variant) = tree_cons(get_identifier("designated_init"), NULL_TREE, TYPE_ATTRIBUTES(variant));
390-
if (has_flexarray)
391-
TYPE_ATTRIBUTES(type) = tree_cons(get_identifier("has_flexarray"), NULL_TREE, TYPE_ATTRIBUTES(type));
392-
}
393395

394396
/*
395397
* force a re-layout of the main variant
@@ -457,10 +459,8 @@ static void randomize_type(tree type)
457459
if (lookup_attribute("randomize_layout", TYPE_ATTRIBUTES(TYPE_MAIN_VARIANT(type))) || is_pure_ops_struct(type))
458460
relayout_struct(type);
459461

460-
for (variant = TYPE_MAIN_VARIANT(type); variant; variant = TYPE_NEXT_VARIANT(variant)) {
461-
TYPE_ATTRIBUTES(type) = copy_list(TYPE_ATTRIBUTES(type));
462-
TYPE_ATTRIBUTES(type) = tree_cons(get_identifier("randomize_considered"), NULL_TREE, TYPE_ATTRIBUTES(type));
463-
}
462+
add_type_attr(type, "randomize_considered", NULL_TREE);
463+
464464
#ifdef __DEBUG_PLUGIN
465465
fprintf(stderr, "Marking randomize_considered on struct %s\n", ORIG_TYPE_NAME(type));
466466
#ifdef __DEBUG_VERBOSE

0 commit comments

Comments
 (0)