1pkey_alloc(2)                 System Calls Manual                pkey_alloc(2)
2
3
4

NAME

6       pkey_alloc, pkey_free - allocate or free a protection key
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #define _GNU_SOURCE             /* See feature_test_macros(7) */
13       #include <sys/mman.h>
14
15       int pkey_alloc(unsigned int flags, unsigned int access_rights);
16       int pkey_free(int pkey);
17

DESCRIPTION

19       pkey_alloc()  allocates  a  protection  key  (pkey) and allows it to be
20       passed to pkey_mprotect(2).
21
22       The pkey_alloc() flags is reserved for future use  and  currently  must
23       always be specified as 0.
24
25       The  pkey_alloc()  access_rights argument may contain zero or more dis‐
26       able operations:
27
28       PKEY_DISABLE_ACCESS
29              Disable all data access to memory covered by the  returned  pro‐
30              tection key.
31
32       PKEY_DISABLE_WRITE
33              Disable  write  access to memory covered by the returned protec‐
34              tion key.
35
36       pkey_free() frees a protection key and makes it available for later al‐
37       locations.   After a protection key has been freed, it may no longer be
38       used in any protection-key-related operations.
39
40       An application should not call pkey_free() on any protection key  which
41       has  been assigned to an address range by pkey_mprotect(2) and which is
42       still in use.  The behavior in this case is undefined and may result in
43       an error.
44

RETURN VALUE

46       On  success,  pkey_alloc() returns a positive protection key value.  On
47       success, pkey_free() returns zero.  On error, -1 is returned, and errno
48       is set to indicate the error.
49

ERRORS

51       EINVAL pkey, flags, or access_rights is invalid.
52
53       ENOSPC (pkey_alloc())  All  protection  keys  available for the current
54              process have been allocated.  The number of  keys  available  is
55              architecture-specific and implementation-specific and may be re‐
56              duced by kernel-internal use of certain keys.   There  are  cur‐
57              rently 15 keys available to user programs on x86.
58
59              This  error  will also be returned if the processor or operating
60              system does not support protection  keys.   Applications  should
61              always  be  prepared to handle this error, since factors outside
62              of the application's control can reduce the number of  available
63              pkeys.
64

STANDARDS

66       Linux.
67

HISTORY

69       Linux 4.9, glibc 2.27.
70

NOTES

72       pkey_alloc()  is  always  safe to call regardless of whether or not the
73       operating system supports protection keys.  It can be used in  lieu  of
74       any  other  mechanism  for  detecting pkey support and will simply fail
75       with the error ENOSPC if the operating system has no pkey support.
76
77       The kernel guarantees that the contents of the hardware rights register
78       (PKRU)  will be preserved only for allocated protection keys.  Any time
79       a key is unallocated (either before the first call returning  that  key
80       from pkey_alloc() or after it is freed via pkey_free()), the kernel may
81       make arbitrary changes to the parts of the  rights  register  affecting
82       access to that key.
83

EXAMPLES

85       See pkeys(7).
86

SEE ALSO

88       pkey_mprotect(2), pkeys(7)
89
90
91
92Linux man-pages 6.04              2023-03-30                     pkey_alloc(2)
Impressum