• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
4  */
5 
6 #ifndef LAPI_KEYCTL_H__
7 #define LAPI_KEYCTL_H__
8 
9 #include "config.h"
10 
11 #if defined(HAVE_KEYUTILS_H) && defined(HAVE_LIBKEYUTILS)
12 # include <keyutils.h>
13 #else
14 # ifdef HAVE_LINUX_KEYCTL_H
15 #  include <linux/keyctl.h>
16 # endif /* HAVE_LINUX_KEYCTL_H */
17 
18 # include <stdarg.h>
19 # include <stdint.h>
20 # include "lapi/syscalls.h"
21 typedef int32_t key_serial_t;
22 
add_key(const char * type,const char * description,const void * payload,size_t plen,key_serial_t ringid)23 static inline key_serial_t add_key(const char *type,
24 				   const char *description,
25 				   const void *payload,
26 				   size_t plen,
27 				   key_serial_t ringid)
28 {
29 	return tst_syscall(__NR_add_key,
30 		type, description, payload, plen, ringid);
31 }
32 
request_key(const char * type,const char * description,const char * callout_info,key_serial_t destringid)33 static inline key_serial_t request_key(const char *type,
34 				       const char *description,
35 				       const char *callout_info,
36 				       key_serial_t destringid)
37 {
38 	return tst_syscall(__NR_request_key,
39 		type, description, callout_info, destringid);
40 }
41 
keyctl(int cmd,...)42 static inline long keyctl(int cmd, ...)
43 {
44 	va_list va;
45 	unsigned long arg2, arg3, arg4, arg5;
46 
47 	va_start(va, cmd);
48 	arg2 = va_arg(va, unsigned long);
49 	arg3 = va_arg(va, unsigned long);
50 	arg4 = va_arg(va, unsigned long);
51 	arg5 = va_arg(va, unsigned long);
52 	va_end(va);
53 
54 	return tst_syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5);
55 }
56 
keyctl_join_session_keyring(const char * name)57 static inline key_serial_t keyctl_join_session_keyring(const char *name) {
58 	return keyctl(KEYCTL_JOIN_SESSION_KEYRING, name);
59 }
60 
61 #endif /* defined(HAVE_KEYUTILS_H) && defined(HAVE_LIBKEYUTILS) */
62 
63 /* special process keyring shortcut IDs */
64 #ifndef KEY_SPEC_THREAD_KEYRING
65 # define KEY_SPEC_THREAD_KEYRING -1
66 #endif
67 
68 #ifndef KEY_SPEC_PROCESS_KEYRING
69 # define KEY_SPEC_PROCESS_KEYRING -2
70 #endif
71 
72 #ifndef KEY_SPEC_SESSION_KEYRING
73 # define KEY_SPEC_SESSION_KEYRING -3
74 #endif
75 
76 #ifndef KEY_SPEC_USER_KEYRING
77 # define KEY_SPEC_USER_KEYRING -4
78 #endif
79 
80 
81 #ifndef KEY_SPEC_USER_SESSION_KEYRING
82 # define KEY_SPEC_USER_SESSION_KEYRING -5
83 #endif
84 
85 /* request-key default keyrings */
86 #ifndef KEY_REQKEY_DEFL_THREAD_KEYRING
87 # define KEY_REQKEY_DEFL_THREAD_KEYRING 1
88 #endif
89 
90 #ifndef KEY_REQKEY_DEFL_SESSION_KEYRING
91 # define KEY_REQKEY_DEFL_SESSION_KEYRING 3
92 #endif
93 
94 #ifndef KEY_REQKEY_DEFL_DEFAULT
95 # define KEY_REQKEY_DEFL_DEFAULT	0
96 #endif
97 
98 /* keyctl commands */
99 #ifndef KEYCTL_GET_KEYRING_ID
100 # define KEYCTL_GET_KEYRING_ID 0
101 #endif
102 
103 #ifndef KEYCTL_JOIN_SESSION_KEYRING
104 # define KEYCTL_JOIN_SESSION_KEYRING 1
105 #endif
106 
107 #ifndef KEYCTL_UPDATE
108 # define KEYCTL_UPDATE 2
109 #endif
110 
111 #ifndef KEYCTL_REVOKE
112 # define KEYCTL_REVOKE 3
113 #endif
114 
115 #ifndef KEYCTL_SETPERM
116 # define KEYCTL_SETPERM 5
117 #endif
118 
119 #ifndef KEYCTL_CLEAR
120 # define KEYCTL_CLEAR 7
121 #endif
122 
123 #ifndef KEYCTL_UNLINK
124 # define KEYCTL_UNLINK 9
125 #endif
126 
127 #ifndef KEYCTL_READ
128 # define KEYCTL_READ 11
129 #endif
130 
131 #ifndef KEYCTL_SET_REQKEY_KEYRING
132 # define KEYCTL_SET_REQKEY_KEYRING 14
133 #endif
134 
135 #ifndef KEYCTL_SET_TIMEOUT
136 # define KEYCTL_SET_TIMEOUT 15
137 #endif
138 
139 #ifndef KEYCTL_INVALIDATE
140 # define KEYCTL_INVALIDATE 21
141 #endif
142 
143 /* key permissions */
144 #ifndef KEY_POS_VIEW
145 # define KEY_POS_VIEW    0x01000000
146 # define KEY_POS_READ    0x02000000
147 # define KEY_POS_WRITE   0x04000000
148 # define KEY_POS_SEARCH  0x08000000
149 # define KEY_POS_LINK    0x10000000
150 # define KEY_POS_SETATTR 0x20000000
151 # define KEY_POS_ALL     0x3f000000
152 
153 # define KEY_USR_VIEW    0x00010000
154 # define KEY_USR_READ    0x00020000
155 # define KEY_USR_WRITE   0x00040000
156 # define KEY_USR_SEARCH  0x00080000
157 # define KEY_USR_LINK    0x00100000
158 # define KEY_USR_SETATTR 0x00200000
159 # define KEY_USR_ALL     0x003f0000
160 
161 # define KEY_GRP_VIEW    0x00000100
162 # define KEY_GRP_READ    0x00000200
163 # define KEY_GRP_WRITE   0x00000400
164 # define KEY_GRP_SEARCH  0x00000800
165 # define KEY_GRP_LINK    0x00001000
166 # define KEY_GRP_SETATTR 0x00002000
167 # define KEY_GRP_ALL     0x00003f00
168 
169 # define KEY_OTH_VIEW    0x00000001
170 # define KEY_OTH_READ    0x00000002
171 # define KEY_OTH_WRITE   0x00000004
172 # define KEY_OTH_SEARCH  0x00000008
173 # define KEY_OTH_LINK    0x00000010
174 # define KEY_OTH_SETATTR 0x00000020
175 # define KEY_OTH_ALL     0x0000003f
176 #endif /* !KEY_POS_VIEW */
177 
178 #endif	/* LAPI_KEYCTL_H__ */
179