@@ -964,6 +964,58 @@ __attribute__((noinline)) int read_ptr(int *ptr)
964
964
return * ptr ;
965
965
}
966
966
967
+ void test_pkey_alloc_free_attach_pkey0 (int * ptr , u16 pkey )
968
+ {
969
+ int i , err ;
970
+ int max_nr_pkey_allocs ;
971
+ int alloced_pkeys [NR_PKEYS ];
972
+ int nr_alloced = 0 ;
973
+ long size ;
974
+
975
+ pkey_assert (pkey_last_malloc_record );
976
+ size = pkey_last_malloc_record -> size ;
977
+ /*
978
+ * This is a bit of a hack. But mprotect() requires
979
+ * huge-page-aligned sizes when operating on hugetlbfs.
980
+ * So, make sure that we use something that's a multiple
981
+ * of a huge page when we can.
982
+ */
983
+ if (size >= HPAGE_SIZE )
984
+ size = HPAGE_SIZE ;
985
+
986
+ /* allocate every possible key and make sure key-0 never got allocated */
987
+ max_nr_pkey_allocs = NR_PKEYS ;
988
+ for (i = 0 ; i < max_nr_pkey_allocs ; i ++ ) {
989
+ int new_pkey = alloc_pkey ();
990
+ pkey_assert (new_pkey != 0 );
991
+
992
+ if (new_pkey < 0 )
993
+ break ;
994
+ alloced_pkeys [nr_alloced ++ ] = new_pkey ;
995
+ }
996
+ /* free all the allocated keys */
997
+ for (i = 0 ; i < nr_alloced ; i ++ ) {
998
+ int free_ret ;
999
+
1000
+ if (!alloced_pkeys [i ])
1001
+ continue ;
1002
+ free_ret = sys_pkey_free (alloced_pkeys [i ]);
1003
+ pkey_assert (!free_ret );
1004
+ }
1005
+
1006
+ /* attach key-0 in various modes */
1007
+ err = sys_mprotect_pkey (ptr , size , PROT_READ , 0 );
1008
+ pkey_assert (!err );
1009
+ err = sys_mprotect_pkey (ptr , size , PROT_WRITE , 0 );
1010
+ pkey_assert (!err );
1011
+ err = sys_mprotect_pkey (ptr , size , PROT_EXEC , 0 );
1012
+ pkey_assert (!err );
1013
+ err = sys_mprotect_pkey (ptr , size , PROT_READ |PROT_WRITE , 0 );
1014
+ pkey_assert (!err );
1015
+ err = sys_mprotect_pkey (ptr , size , PROT_READ |PROT_WRITE |PROT_EXEC , 0 );
1016
+ pkey_assert (!err );
1017
+ }
1018
+
967
1019
void test_read_of_write_disabled_region (int * ptr , u16 pkey )
968
1020
{
969
1021
int ptr_contents ;
@@ -1448,6 +1500,7 @@ void (*pkey_tests[])(int *ptr, u16 pkey) = {
1448
1500
test_pkey_syscalls_on_non_allocated_pkey ,
1449
1501
test_pkey_syscalls_bad_args ,
1450
1502
test_pkey_alloc_exhaust ,
1503
+ test_pkey_alloc_free_attach_pkey0 ,
1451
1504
};
1452
1505
1453
1506
void run_tests_once (void )
0 commit comments