Skip to content

Commit c74fe39

Browse files
hansendcKAGA-KOKO
authored andcommitted
pkeys: Add details of system call use to Documentation/
This spells out all of the pkey-related system calls that we have and provides some example code fragments to demonstrate how we expect them to be used. Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Cc: linux-arch@vger.kernel.org Cc: Dave Hansen <dave@sr71.net> Cc: mgorman@techsingularity.net Cc: arnd@arndb.de Cc: linux-api@vger.kernel.org Cc: linux-mm@kvack.org Cc: luto@kernel.org Cc: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org Link: http://lkml.kernel.org/r/20160729163020.59350E33@viggo.jf.intel.com Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent a60f7b6 commit c74fe39

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

Documentation/x86/protection-keys.txt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,68 @@ even though there is theoretically space in the PAE PTEs. These
1818
permissions are enforced on data access only and have no effect on
1919
instruction fetches.
2020

21+
=========================== Syscalls ===========================
22+
23+
There are 2 system calls which directly interact with pkeys:
24+
25+
int pkey_alloc(unsigned long flags, unsigned long init_access_rights)
26+
int pkey_free(int pkey);
27+
int pkey_mprotect(unsigned long start, size_t len,
28+
unsigned long prot, int pkey);
29+
30+
Before a pkey can be used, it must first be allocated with
31+
pkey_alloc(). An application calls the WRPKRU instruction
32+
directly in order to change access permissions to memory covered
33+
with a key. In this example WRPKRU is wrapped by a C function
34+
called pkey_set().
35+
36+
int real_prot = PROT_READ|PROT_WRITE;
37+
pkey = pkey_alloc(0, PKEY_DENY_WRITE);
38+
ptr = mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
39+
ret = pkey_mprotect(ptr, PAGE_SIZE, real_prot, pkey);
40+
... application runs here
41+
42+
Now, if the application needs to update the data at 'ptr', it can
43+
gain access, do the update, then remove its write access:
44+
45+
pkey_set(pkey, 0); // clear PKEY_DENY_WRITE
46+
*ptr = foo; // assign something
47+
pkey_set(pkey, PKEY_DENY_WRITE); // set PKEY_DENY_WRITE again
48+
49+
Now when it frees the memory, it will also free the pkey since it
50+
is no longer in use:
51+
52+
munmap(ptr, PAGE_SIZE);
53+
pkey_free(pkey);
54+
55+
=========================== Behavior ===========================
56+
57+
The kernel attempts to make protection keys consistent with the
58+
behavior of a plain mprotect(). For instance if you do this:
59+
60+
mprotect(ptr, size, PROT_NONE);
61+
something(ptr);
62+
63+
you can expect the same effects with protection keys when doing this:
64+
65+
pkey = pkey_alloc(0, PKEY_DISABLE_WRITE | PKEY_DISABLE_READ);
66+
pkey_mprotect(ptr, size, PROT_READ|PROT_WRITE, pkey);
67+
something(ptr);
68+
69+
That should be true whether something() is a direct access to 'ptr'
70+
like:
71+
72+
*ptr = foo;
73+
74+
or when the kernel does the access on the application's behalf like
75+
with a read():
76+
77+
read(fd, ptr, 1);
78+
79+
The kernel will send a SIGSEGV in both cases, but si_code will be set
80+
to SEGV_PKERR when violating protection keys versus SEGV_ACCERR when
81+
the plain mprotect() permissions are violated.
82+
2183
=========================== Config Option ===========================
2284

2385
This config option adds approximately 1.5kb of text. and 50 bytes of

0 commit comments

Comments
 (0)