Skip to content

Commit e181293

Browse files
hansendcKAGA-KOKO
authored andcommitted
x86/selftests/pkeys: Fork() to check for state being preserved
There was a bug where the per-mm pkey state was not being preserved across fork() in the child. fork() is performed in the pkey selftests, but all of the pkey activity is performed in the parent. The child does not perform any actions sensitive to pkey state. To make the test more sensitive to these kinds of bugs, add a fork() where the parent exits, and execution continues in the child. To achieve this let the key exhaustion test not terminate at the first allocation failure and fork after 2*NR_PKEYS loops and continue in the child. Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: bp@alien8.de Cc: hpa@zytor.com Cc: peterz@infradead.org Cc: mpe@ellerman.id.au Cc: will.deacon@arm.com Cc: luto@kernel.org Cc: jroedel@suse.de Cc: stable@vger.kernel.org Cc: Borislav Petkov <bp@alien8.de> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Will Deacon <will.deacon@arm.com> Cc: Andy Lutomirski <luto@kernel.org> Cc: Joerg Roedel <jroedel@suse.de> Link: https://lkml.kernel.org/r/20190102215657.585704B7@viggo.jf.intel.com
1 parent a31e184 commit e181293

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

tools/testing/selftests/x86/protection_keys.c

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,21 @@ void test_pkey_syscalls_bad_args(int *ptr, u16 pkey)
11331133
pkey_assert(err);
11341134
}
11351135

1136+
void become_child(void)
1137+
{
1138+
pid_t forkret;
1139+
1140+
forkret = fork();
1141+
pkey_assert(forkret >= 0);
1142+
dprintf3("[%d] fork() ret: %d\n", getpid(), forkret);
1143+
1144+
if (!forkret) {
1145+
/* in the child */
1146+
return;
1147+
}
1148+
exit(0);
1149+
}
1150+
11361151
/* Assumes that all pkeys other than 'pkey' are unallocated */
11371152
void test_pkey_alloc_exhaust(int *ptr, u16 pkey)
11381153
{
@@ -1141,7 +1156,7 @@ void test_pkey_alloc_exhaust(int *ptr, u16 pkey)
11411156
int nr_allocated_pkeys = 0;
11421157
int i;
11431158

1144-
for (i = 0; i < NR_PKEYS*2; i++) {
1159+
for (i = 0; i < NR_PKEYS*3; i++) {
11451160
int new_pkey;
11461161
dprintf1("%s() alloc loop: %d\n", __func__, i);
11471162
new_pkey = alloc_pkey();
@@ -1152,20 +1167,26 @@ void test_pkey_alloc_exhaust(int *ptr, u16 pkey)
11521167
if ((new_pkey == -1) && (errno == ENOSPC)) {
11531168
dprintf2("%s() failed to allocate pkey after %d tries\n",
11541169
__func__, nr_allocated_pkeys);
1155-
break;
1170+
} else {
1171+
/*
1172+
* Ensure the number of successes never
1173+
* exceeds the number of keys supported
1174+
* in the hardware.
1175+
*/
1176+
pkey_assert(nr_allocated_pkeys < NR_PKEYS);
1177+
allocated_pkeys[nr_allocated_pkeys++] = new_pkey;
11561178
}
1157-
pkey_assert(nr_allocated_pkeys < NR_PKEYS);
1158-
allocated_pkeys[nr_allocated_pkeys++] = new_pkey;
1179+
1180+
/*
1181+
* Make sure that allocation state is properly
1182+
* preserved across fork().
1183+
*/
1184+
if (i == NR_PKEYS*2)
1185+
become_child();
11591186
}
11601187

11611188
dprintf3("%s()::%d\n", __func__, __LINE__);
11621189

1163-
/*
1164-
* ensure it did not reach the end of the loop without
1165-
* failure:
1166-
*/
1167-
pkey_assert(i < NR_PKEYS*2);
1168-
11691190
/*
11701191
* There are 16 pkeys supported in hardware. Three are
11711192
* allocated by the time we get here:

0 commit comments

Comments
 (0)