Skip to content

Commit 381fdb7

Browse files
committed
randstruct: Fix gcc-plugin performance mode to stay in group
The performance mode of the gcc-plugin randstruct was shuffling struct members outside of the cache-line groups. Limit the range to the specified group indexes. Cc: linux-hardening@vger.kernel.org Cc: stable@vger.kernel.org Reported-by: Lukas Loidolt <e1634039@student.tuwien.ac.at> Closes: https://lore.kernel.org/all/f3ca77f0-e414-4065-83a5-ae4c4d25545d@student.tuwien.ac.at Fixes: 313dd1b ("gcc-plugins: Add the randstruct plugin") Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent c5225cd commit 381fdb7

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

scripts/gcc-plugins/randomize_layout_plugin.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,14 @@ static void partition_struct(tree *fields, unsigned long length, struct partitio
191191

192192
static void performance_shuffle(tree *newtree, unsigned long length, ranctx *prng_state)
193193
{
194-
unsigned long i, x;
194+
unsigned long i, x, index;
195195
struct partition_group size_group[length];
196196
unsigned long num_groups = 0;
197197
unsigned long randnum;
198198

199199
partition_struct(newtree, length, (struct partition_group *)&size_group, &num_groups);
200+
201+
/* FIXME: this group shuffle is currently a no-op. */
200202
for (i = num_groups - 1; i > 0; i--) {
201203
struct partition_group tmp;
202204
randnum = ranval(prng_state) % (i + 1);
@@ -206,11 +208,14 @@ static void performance_shuffle(tree *newtree, unsigned long length, ranctx *prn
206208
}
207209

208210
for (x = 0; x < num_groups; x++) {
209-
for (i = size_group[x].start + size_group[x].length - 1; i > size_group[x].start; i--) {
211+
for (index = size_group[x].length - 1; index > 0; index--) {
210212
tree tmp;
213+
214+
i = size_group[x].start + index;
211215
if (DECL_BIT_FIELD_TYPE(newtree[i]))
212216
continue;
213-
randnum = ranval(prng_state) % (i + 1);
217+
randnum = ranval(prng_state) % (index + 1);
218+
randnum += size_group[x].start;
214219
// we could handle this case differently if desired
215220
if (DECL_BIT_FIELD_TYPE(newtree[randnum]))
216221
continue;

0 commit comments

Comments
 (0)