1 /*
2  * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef KEYCTL_H__
19 #define KEYCTL_H__
20 
21 #include "config.h"
22 
23 #if defined(HAVE_KEYUTILS_H) && defined(HAVE_LIBKEYUTILS)
24 # include <keyutils.h>
25 #else
26 # ifdef HAVE_LINUX_KEYCTL_H
27 #  include <linux/keyctl.h>
28 # endif /* HAVE_LINUX_KEYCTL_H */
29 
30 # include <stdarg.h>
31 # include <stdint.h>
32 # include "lapi/syscalls.h"
33 typedef int32_t key_serial_t;
34 
add_key(const char * type,const char * description,const void * payload,size_t plen,key_serial_t ringid)35 static inline key_serial_t add_key(const char *type,
36 				   const char *description,
37 				   const void *payload,
38 				   size_t plen,
39 				   key_serial_t ringid)
40 {
41 	return tst_syscall(__NR_add_key,
42 		type, description, payload, plen, ringid);
43 }
44 
request_key(const char * type,const char * description,const char * callout_info,key_serial_t destringid)45 static inline key_serial_t request_key(const char *type,
46 				       const char *description,
47 				       const char *callout_info,
48 				       key_serial_t destringid)
49 {
50 	return tst_syscall(__NR_request_key,
51 		type, description, callout_info, destringid);
52 }
53 
keyctl(int cmd,...)54 static inline long keyctl(int cmd, ...)
55 {
56 	va_list va;
57 	unsigned long arg2, arg3, arg4, arg5;
58 
59 	va_start(va, cmd);
60 	arg2 = va_arg(va, unsigned long);
61 	arg3 = va_arg(va, unsigned long);
62 	arg4 = va_arg(va, unsigned long);
63 	arg5 = va_arg(va, unsigned long);
64 	va_end(va);
65 
66 	return tst_syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5);
67 }
68 
keyctl_join_session_keyring(const char * name)69 static inline key_serial_t keyctl_join_session_keyring(const char *name) {
70 	return keyctl(KEYCTL_JOIN_SESSION_KEYRING, name);
71 }
72 
73 #endif /* defined(HAVE_KEYUTILS_H) && defined(HAVE_LIBKEYUTILS) */
74 
75 /* special process keyring shortcut IDs */
76 #ifndef KEY_SPEC_THREAD_KEYRING
77 # define KEY_SPEC_THREAD_KEYRING -1
78 #endif
79 
80 #ifndef KEY_SPEC_PROCESS_KEYRING
81 # define KEY_SPEC_PROCESS_KEYRING -2
82 #endif
83 
84 #ifndef KEY_SPEC_SESSION_KEYRING
85 # define KEY_SPEC_SESSION_KEYRING -3
86 #endif
87 
88 #ifndef KEY_SPEC_USER_KEYRING
89 # define KEY_SPEC_USER_KEYRING -4
90 #endif
91 
92 
93 #ifndef KEY_SPEC_USER_SESSION_KEYRING
94 # define KEY_SPEC_USER_SESSION_KEYRING -5
95 #endif
96 
97 /* request-key default keyrings */
98 #ifndef KEY_REQKEY_DEFL_THREAD_KEYRING
99 # define KEY_REQKEY_DEFL_THREAD_KEYRING 1
100 #endif
101 
102 #ifndef KEY_REQKEY_DEFL_SESSION_KEYRING
103 # define KEY_REQKEY_DEFL_SESSION_KEYRING 3
104 #endif
105 
106 #ifndef KEY_REQKEY_DEFL_DEFAULT
107 # define KEY_REQKEY_DEFL_DEFAULT	0
108 #endif
109 
110 /* keyctl commands */
111 #ifndef KEYCTL_GET_KEYRING_ID
112 # define KEYCTL_GET_KEYRING_ID 0
113 #endif
114 
115 #ifndef KEYCTL_JOIN_SESSION_KEYRING
116 # define KEYCTL_JOIN_SESSION_KEYRING 1
117 #endif
118 
119 #ifndef KEYCTL_UPDATE
120 # define KEYCTL_UPDATE 2
121 #endif
122 
123 #ifndef KEYCTL_REVOKE
124 # define KEYCTL_REVOKE 3
125 #endif
126 
127 #ifndef KEYCTL_SETPERM
128 # define KEYCTL_SETPERM 5
129 #endif
130 
131 #ifndef KEYCTL_CLEAR
132 # define KEYCTL_CLEAR 7
133 #endif
134 
135 #ifndef KEYCTL_UNLINK
136 # define KEYCTL_UNLINK 9
137 #endif
138 
139 #ifndef KEYCTL_READ
140 # define KEYCTL_READ 11
141 #endif
142 
143 #ifndef KEYCTL_SET_REQKEY_KEYRING
144 # define KEYCTL_SET_REQKEY_KEYRING 14
145 #endif
146 
147 #ifndef KEYCTL_SET_TIMEOUT
148 # define KEYCTL_SET_TIMEOUT 15
149 #endif
150 
151 #ifndef KEYCTL_INVALIDATE
152 # define KEYCTL_INVALIDATE 21
153 #endif
154 
155 /* key permissions */
156 #ifndef KEY_POS_VIEW
157 # define KEY_POS_VIEW    0x01000000
158 # define KEY_POS_READ    0x02000000
159 # define KEY_POS_WRITE   0x04000000
160 # define KEY_POS_SEARCH  0x08000000
161 # define KEY_POS_LINK    0x10000000
162 # define KEY_POS_SETATTR 0x20000000
163 # define KEY_POS_ALL     0x3f000000
164 
165 # define KEY_USR_VIEW    0x00010000
166 # define KEY_USR_READ    0x00020000
167 # define KEY_USR_WRITE   0x00040000
168 # define KEY_USR_SEARCH  0x00080000
169 # define KEY_USR_LINK    0x00100000
170 # define KEY_USR_SETATTR 0x00200000
171 # define KEY_USR_ALL     0x003f0000
172 
173 # define KEY_GRP_VIEW    0x00000100
174 # define KEY_GRP_READ    0x00000200
175 # define KEY_GRP_WRITE   0x00000400
176 # define KEY_GRP_SEARCH  0x00000800
177 # define KEY_GRP_LINK    0x00001000
178 # define KEY_GRP_SETATTR 0x00002000
179 # define KEY_GRP_ALL     0x00003f00
180 
181 # define KEY_OTH_VIEW    0x00000001
182 # define KEY_OTH_READ    0x00000002
183 # define KEY_OTH_WRITE   0x00000004
184 # define KEY_OTH_SEARCH  0x00000008
185 # define KEY_OTH_LINK    0x00000010
186 # define KEY_OTH_SETATTR 0x00000020
187 # define KEY_OTH_ALL     0x0000003f
188 #endif /* !KEY_POS_VIEW */
189 
190 #endif	/* KEYCTL_H__ */
191