]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/add_key.2
Various pages: Normalize NOTES about nonexistent glibc wrappers
[thirdparty/man-pages.git] / man2 / add_key.2
1 .\" Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
2 .\" Written by David Howells (dhowells@redhat.com)
3 .\" and Copyright (C) 2016 Michael Kerrisk <mtk.man-pages@gmail.com>
4 .\"
5 .\" %%%LICENSE_START(GPLv2+_SW_ONEPARA)
6 .\" This program is free software; you can redistribute it and/or
7 .\" modify it under the terms of the GNU General Public License
8 .\" as published by the Free Software Foundation; either version
9 .\" 2 of the License, or (at your option) any later version.
10 .\" %%%LICENSE_END
11 .\"
12 .TH ADD_KEY 2 2020-11-01 Linux "Linux Key Management Calls"
13 .SH NAME
14 add_key \- add a key to the kernel's key management facility
15 .SH SYNOPSIS
16 .nf
17 .B #include <sys/types.h>
18 .B #include <keyutils.h>
19 .PP
20 .BI "key_serial_t add_key(const char *" type ", const char *" description ,
21 .BI " const void *" payload ", size_t " plen ,
22 .BI " key_serial_t " keyring ");"
23 .fi
24 .PP
25 .IR Note :
26 There is no glibc wrapper for this system call; see NOTES.
27 .SH DESCRIPTION
28 .BR add_key ()
29 creates or updates a key of the given
30 .I type
31 and
32 .IR description ,
33 instantiates it with the
34 .I payload
35 of length
36 .IR plen ,
37 attaches it to the nominated
38 .IR keyring ,
39 and returns the key's serial number.
40 .PP
41 The key may be rejected if the provided data is in the wrong format or
42 it is invalid in some other way.
43 .PP
44 If the destination
45 .I keyring
46 already contains a key that matches the specified
47 .IR type
48 and
49 .IR description ,
50 then, if the key type supports it,
51 .\" FIXME The aforementioned phrases begs the question:
52 .\" which key types support this?
53 that key will be updated rather than a new key being created;
54 if not, a new key (with a different ID) will be created
55 and it will displace the link to the extant key from the keyring.
56 .\" FIXME Perhaps elaborate the implications here? Namely, the new
57 .\" key will have a new ID, and if the old key was a keyring that
58 .\" is consequently unlinked, then keys that it was anchoring
59 .\" will have their reference count decreased by one (and may
60 .\" consequently be garbage collected). Is this all correct?
61 .PP
62 The destination
63 .I keyring
64 serial number may be that of a valid keyring for which the caller has
65 .I write
66 permission.
67 Alternatively, it may be one of the following special keyring IDs:
68 .\" FIXME . Perhaps have a separate page describing special keyring IDs?
69 .TP
70 .B KEY_SPEC_THREAD_KEYRING
71 This specifies the caller's thread-specific keyring
72 .RB ( thread-keyring (7)).
73 .TP
74 .B KEY_SPEC_PROCESS_KEYRING
75 This specifies the caller's process-specific keyring
76 .RB ( process-keyring (7)).
77 .TP
78 .B KEY_SPEC_SESSION_KEYRING
79 This specifies the caller's session-specific keyring
80 .RB ( session-keyring (7)).
81 .TP
82 .B KEY_SPEC_USER_KEYRING
83 This specifies the caller's UID-specific keyring
84 .RB ( user-keyring (7)).
85 .TP
86 .B KEY_SPEC_USER_SESSION_KEYRING
87 This specifies the caller's UID-session keyring
88 .RB ( user-session-keyring (7)).
89 .SS Key types
90 The key
91 .I type
92 is a string that specifies the key's type.
93 Internally, the kernel defines a number of key types that are
94 available in the core key management code.
95 Among the types that are available for user-space use
96 and can be specified as the
97 .I type
98 argument to
99 .BR add_key ()
100 are the following:
101 .TP
102 .I """keyring"""
103 Keyrings are special key types that may contain links to sequences of other
104 keys of any type.
105 If this interface is used to create a keyring, then
106 .I payload
107 should be NULL and
108 .I plen
109 should be zero.
110 .TP
111 .IR """user"""
112 This is a general purpose key type whose payload may be read and updated
113 by user-space applications.
114 The key is kept entirely within kernel memory.
115 The payload for keys of this type is a blob of arbitrary data
116 of up to 32,767 bytes.
117 .TP
118 .IR """logon""" " (since Linux 3.3)"
119 .\" commit 9f6ed2ca257fa8650b876377833e6f14e272848b
120 This key type is essentially the same as
121 .IR """user""" ,
122 but it does not permit the key to read.
123 This is suitable for storing payloads
124 that you do not want to be readable from user space.
125 .PP
126 This key type vets the
127 .I description
128 to ensure that it is qualified by a "service" prefix,
129 by checking to ensure that the
130 .I description
131 contains a ':' that is preceded by other characters.
132 .TP
133 .IR """big_key""" " (since Linux 3.13)"
134 .\" commit ab3c3587f8cda9083209a61dbe3a4407d3cada10
135 This key type is similar to
136 .IR """user""" ,
137 but may hold a payload of up to 1\ MiB.
138 If the key payload is large enough,
139 then it may be stored encrypted in tmpfs
140 (which can be swapped out) rather than kernel memory.
141 .PP
142 For further details on these key types, see
143 .BR keyrings (7).
144 .SH RETURN VALUE
145 On success,
146 .BR add_key ()
147 returns the serial number of the key it created or updated.
148 On error, \-1 is returned and
149 .I errno
150 is set to indicate the cause of the error.
151 .SH ERRORS
152 .TP
153 .B EACCES
154 The keyring wasn't available for modification by the user.
155 .TP
156 .B EDQUOT
157 The key quota for this user would be exceeded by creating this key or linking
158 it to the keyring.
159 .TP
160 .B EFAULT
161 One or more of
162 .IR type ,
163 .IR description ,
164 and
165 .I payload
166 points outside process's accessible address space.
167 .TP
168 .B EINVAL
169 The size of the string (including the terminating null byte) specified in
170 .I type
171 or
172 .I description
173 exceeded the limit (32 bytes and 4096 bytes respectively).
174 .TP
175 .B EINVAL
176 The payload data was invalid.
177 .TP
178 .B EINVAL
179 .IR type
180 was
181 .IR """logon"""
182 and the
183 .I description
184 was not qualified with a prefix string of the form
185 .IR """service:""" .
186 .TP
187 .B EKEYEXPIRED
188 The keyring has expired.
189 .TP
190 .B EKEYREVOKED
191 The keyring has been revoked.
192 .TP
193 .B ENOKEY
194 The keyring doesn't exist.
195 .TP
196 .B ENOMEM
197 Insufficient memory to create a key.
198 .TP
199 .B EPERM
200 The
201 .I type
202 started with a period (\(aq.\(aq).
203 Key types that begin with a period are reserved to the implementation.
204 .TP
205 .B EPERM
206 .I type
207 was
208 .I """keyring"""
209 and the
210 .I description
211 started with a period (\(aq.\(aq).
212 Keyrings with descriptions (names)
213 that begin with a period are reserved to the implementation.
214 .SH VERSIONS
215 This system call first appeared in Linux 2.6.10.
216 .SH CONFORMING TO
217 This system call is a nonstandard Linux extension.
218 .SH NOTES
219 Glibc does not provide a wrapper for this system call.
220 A wrapper is provided in the
221 .IR libkeyutils
222 package.
223 When employing the wrapper in that library, link with
224 .IR \-lkeyutils .
225 .SH EXAMPLES
226 The program below creates a key with the type, description, and payload
227 specified in its command-line arguments,
228 and links that key into the session keyring.
229 The following shell session demonstrates the use of the program:
230 .PP
231 .in +4n
232 .EX
233 $ \fB./a.out user mykey "Some payload"\fP
234 Key ID is 64a4dca
235 $ \fBgrep \(aq64a4dca\(aq /proc/keys\fP
236 064a4dca I--Q--- 1 perm 3f010000 1000 1000 user mykey: 12
237 .EE
238 .in
239 .SS Program source
240 \&
241 .EX
242 #include <sys/types.h>
243 #include <keyutils.h>
244 #include <stdint.h>
245 #include <stdio.h>
246 #include <stdlib.h>
247 #include <string.h>
248
249 int
250 main(int argc, char *argv[])
251 {
252 key_serial_t key;
253
254 if (argc != 4) {
255 fprintf(stderr, "Usage: %s type description payload\en",
256 argv[0]);
257 exit(EXIT_FAILURE);
258 }
259
260 key = add_key(argv[1], argv[2], argv[3], strlen(argv[3]),
261 KEY_SPEC_SESSION_KEYRING);
262 if (key == \-1) {
263 perror("add_key");
264 exit(EXIT_FAILURE);
265 }
266
267 printf("Key ID is %jx\en", (uintmax_t) key);
268
269 exit(EXIT_SUCCESS);
270 }
271 .EE
272 .SH SEE ALSO
273 .ad l
274 .nh
275 .BR keyctl (1),
276 .BR keyctl (2),
277 .BR request_key (2),
278 .BR keyctl (3),
279 .BR keyrings (7),
280 .BR keyutils (7),
281 .BR persistent\-keyring (7),
282 .BR process\-keyring (7),
283 .BR session\-keyring (7),
284 .BR thread\-keyring (7),
285 .BR user\-keyring (7),
286 .BR user\-session\-keyring (7)
287 .PP
288 The kernel source files
289 .IR Documentation/security/keys/core.rst
290 and
291 .IR Documentation/keys/request\-key.rst
292 (or, before Linux 4.13, in the files
293 .\" commit b68101a1e8f0263dbc7b8375d2a7c57c6216fb76
294 .IR Documentation/security/keys.txt
295 and
296 .\" commit 3db38ed76890565772fcca3279cc8d454ea6176b
297 .IR Documentation/security/keys\-request\-key.txt ).