Skip to content

Commit f39f18f

Browse files
committed
randstruct: gcc-plugin: Fix attribute addition
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 <[email protected]> Closes: KSPP#367 Closes: https://lore.kernel.org/lkml/[email protected]/ Reported-by: Ingo Saitz <[email protected]> Closes: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1104745 Fixes: 313dd1b ("gcc-plugins: Add the randstruct plugin") Tested-by: Thiago Jung Bauermann <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 5c78e79 commit f39f18f

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

scripts/gcc-plugins/gcc-common.h

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

126+
static inline void __add_type_attr(tree type, const char *attr, tree args)
127+
{
128+
tree oldattr;
129+
130+
if (type == NULL_TREE)
131+
return;
132+
oldattr = lookup_attribute(attr, TYPE_ATTRIBUTES(type));
133+
if (oldattr != NULL_TREE) {
134+
gcc_assert(TREE_VALUE(oldattr) == args || TREE_VALUE(TREE_VALUE(oldattr)) == TREE_VALUE(args));
135+
return;
136+
}
137+
138+
TYPE_ATTRIBUTES(type) = copy_list(TYPE_ATTRIBUTES(type));
139+
TYPE_ATTRIBUTES(type) = tree_cons(get_identifier(attr), args, TYPE_ATTRIBUTES(type));
140+
}
141+
142+
static inline void add_type_attr(tree type, const char *attr, tree args)
143+
{
144+
tree main_variant = TYPE_MAIN_VARIANT(type);
145+
146+
__add_type_attr(TYPE_CANONICAL(type), attr, args);
147+
__add_type_attr(TYPE_CANONICAL(main_variant), attr, args);
148+
__add_type_attr(main_variant, attr, args);
149+
150+
for (type = TYPE_NEXT_VARIANT(main_variant); type; type = TYPE_NEXT_VARIANT(type)) {
151+
if (!lookup_attribute(attr, TYPE_ATTRIBUTES(type)))
152+
TYPE_ATTRIBUTES(type) = TYPE_ATTRIBUTES(main_variant);
153+
154+
__add_type_attr(TYPE_CANONICAL(type), attr, args);
155+
}
156+
}
157+
126158
#define PASS_INFO(NAME, REF, ID, POS) \
127159
struct register_pass_info NAME##_pass_info = { \
128160
.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
@@ -73,6 +73,9 @@ static tree handle_randomize_layout_attr(tree *node, tree name, tree args, int f
7373

7474
if (TYPE_P(*node)) {
7575
type = *node;
76+
} else if (TREE_CODE(*node) == FIELD_DECL) {
77+
*no_add_attrs = false;
78+
return NULL_TREE;
7679
} else {
7780
gcc_assert(TREE_CODE(*node) == TYPE_DECL);
7881
type = TREE_TYPE(*node);
@@ -348,15 +351,14 @@ static int relayout_struct(tree type)
348351
TREE_CHAIN(newtree[i]) = newtree[i+1];
349352
TREE_CHAIN(newtree[num_fields - 1]) = NULL_TREE;
350353

354+
add_type_attr(type, "randomize_performed", NULL_TREE);
355+
add_type_attr(type, "designated_init", NULL_TREE);
356+
if (has_flexarray)
357+
add_type_attr(type, "has_flexarray", NULL_TREE);
358+
351359
main_variant = TYPE_MAIN_VARIANT(type);
352-
for (variant = main_variant; variant; variant = TYPE_NEXT_VARIANT(variant)) {
360+
for (variant = main_variant; variant; variant = TYPE_NEXT_VARIANT(variant))
353361
TYPE_FIELDS(variant) = newtree[0];
354-
TYPE_ATTRIBUTES(variant) = copy_list(TYPE_ATTRIBUTES(variant));
355-
TYPE_ATTRIBUTES(variant) = tree_cons(get_identifier("randomize_performed"), NULL_TREE, TYPE_ATTRIBUTES(variant));
356-
TYPE_ATTRIBUTES(variant) = tree_cons(get_identifier("designated_init"), NULL_TREE, TYPE_ATTRIBUTES(variant));
357-
if (has_flexarray)
358-
TYPE_ATTRIBUTES(type) = tree_cons(get_identifier("has_flexarray"), NULL_TREE, TYPE_ATTRIBUTES(type));
359-
}
360362

361363
/*
362364
* force a re-layout of the main variant
@@ -424,10 +426,8 @@ static void randomize_type(tree type)
424426
if (lookup_attribute("randomize_layout", TYPE_ATTRIBUTES(TYPE_MAIN_VARIANT(type))) || is_pure_ops_struct(type))
425427
relayout_struct(type);
426428

427-
for (variant = TYPE_MAIN_VARIANT(type); variant; variant = TYPE_NEXT_VARIANT(variant)) {
428-
TYPE_ATTRIBUTES(type) = copy_list(TYPE_ATTRIBUTES(type));
429-
TYPE_ATTRIBUTES(type) = tree_cons(get_identifier("randomize_considered"), NULL_TREE, TYPE_ATTRIBUTES(type));
430-
}
429+
add_type_attr(type, "randomize_considered", NULL_TREE);
430+
431431
#ifdef __DEBUG_PLUGIN
432432
fprintf(stderr, "Marking randomize_considered on struct %s\n", ORIG_TYPE_NAME(type));
433433
#ifdef __DEBUG_VERBOSE

0 commit comments

Comments
 (0)