Skip to content

Commit e39120a

Browse files
committed
KVM: selftests: Fix dynamic generation of configuration names
When we dynamically generate a name for a configuration in get-reg-list we use strcat() to append to a buffer allocated using malloc() but we never initialise that buffer. Since malloc() offers no guarantees regarding the contents of the memory it returns this can lead to us corrupting, and likely overflowing, the buffer: vregs: PASS vregs+pmu: PASS sve: PASS sve+pmu: PASS vregs+pauth_address+pauth_generic: PASS X?vr+gspauth_addre+spauth_generi+pmu: PASS The bug is that strcat() should have been strcpy(), and that replacement would be enough to fix it, but there are other things in the function that leave something to be desired. In particular, an (incorrectly) empty config would cause an out of bounds access to c->name[-1]. Since the strcpy() call relies on c->name[0..len-1] being initialized, enforce that invariant throughout the function. Fixes: 2f9ace5 ("KVM: arm64: selftests: get-reg-list: Introduce vcpu configs") Reviewed-by: Andrew Jones <[email protected]> Co-developed-by: Mark Brown <[email protected]> Signed-off-by: Mark Brown <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 4cdf351 commit e39120a

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

tools/testing/selftests/kvm/get-reg-list.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@ static const char *config_name(struct vcpu_reg_list *c)
7171
for_each_sublist(c, s) {
7272
if (!strcmp(s->name, "base"))
7373
continue;
74-
strcat(c->name + len, s->name);
75-
len += strlen(s->name) + 1;
76-
c->name[len - 1] = '+';
74+
if (len)
75+
c->name[len++] = '+';
76+
strcpy(c->name + len, s->name);
77+
len += strlen(s->name);
7778
}
78-
c->name[len - 1] = '\0';
79+
c->name[len] = '\0';
7980

8081
return c->name;
8182
}

0 commit comments

Comments
 (0)