Commit ce20aba74c69084c1f87725b68a507735733c798

Authored by Dave Hansen
Committed by Greg Kroah-Hartman
1 parent 92296f7dda

x86/selftests/pkeys: Fork() to check for state being preserved

commit e1812933b17be7814f51b6c310c5d1ced7a9a5f5 upstream.

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
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Showing 1 changed file with 31 additions and 10 deletions Side-by-side Diff

tools/testing/selftests/x86/protection_keys.c
... ... @@ -1133,6 +1133,21 @@
1133 1133 pkey_assert(err);
1134 1134 }
1135 1135  
  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 +
1136 1151 /* Assumes that all pkeys other than 'pkey' are unallocated */
1137 1152 void test_pkey_alloc_exhaust(int *ptr, u16 pkey)
1138 1153 {
... ... @@ -1141,7 +1156,7 @@
1141 1156 int nr_allocated_pkeys = 0;
1142 1157 int i;
1143 1158  
1144   - for (i = 0; i < NR_PKEYS*2; i++) {
  1159 + for (i = 0; i < NR_PKEYS*3; i++) {
1145 1160 int new_pkey;
1146 1161 dprintf1("%s() alloc loop: %d\n", __func__, i);
1147 1162 new_pkey = alloc_pkey();
1148 1163  
1149 1164  
... ... @@ -1152,19 +1167,25 @@
1152 1167 if ((new_pkey == -1) && (errno == ENOSPC)) {
1153 1168 dprintf2("%s() failed to allocate pkey after %d tries\n",
1154 1169 __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;
1156 1178 }
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();
1159 1186 }
1160 1187  
1161 1188 dprintf3("%s()::%d\n", __func__, __LINE__);
1162   -
1163   - /*
1164   - * ensure it did not reach the end of the loop without
1165   - * failure:
1166   - */
1167   - pkey_assert(i < NR_PKEYS*2);
1168 1189  
1169 1190 /*
1170 1191 * There are 16 pkeys supported in hardware. Three are