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