add_key(2)

add_key(2) ๐Ÿ’– the "make me a key bestie" syscall

okay so this is a page from the System Calls Manual (yes, section 2, the kernel-adjacent girlies) and it's all about add_key โ€” the glow-up that puts a brand new key into the kernel's key management facility โœจ


โœจ NAME โœจ

add_key โ€” add a key to the kernel's key management facility ๐Ÿ”‘


๐ŸŽ€ LIBRARY ๐ŸŽ€

Standard C library (libc, -lc) ๐Ÿซถ


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

#include <keyutils.h>

key_serial_t add_key(size_t size;
                     const char *type, const char *description,
                     const void payload[size], size_t size,
                     key_serial_t keyring);
๐Ÿšจ Note: There is no glibc wrapper for this system call; see NOTES. k thanks bye ๐Ÿ’…

๐ŸŒธ DESCRIPTION ๐ŸŒธ

So like, add_key() creates or updates a key of the given type and description, instantiates it with the payload of size size, attaches it to the nominated keyring, and returns the key's serial number. That's it, that's the whole personality ๐Ÿ’–

ngl the key may be rejected if the data you gave it is in the wrong format or is invalid in some other way. it's giving "vibe check failed" ๐Ÿ˜ญ

Here's the tea tho: if the destination keyring already contains a key that matches the specified type and description, then โ€” if the key type supports it โ€” that key will be updated rather than a new key being created. but if it doesn't support it? a whole new key (with a different ID) gets created and it will displace the link to the extant key from the keyring. main character energy, literally ๐Ÿ™ˆ

The destination keyring serial number may be that of a valid keyring for which the caller has write permission โœ๏ธ. Alternatively, it may be one of the following special keyring IDs:

  • KEY_SPEC_THREAD_KEYRING โ€” this specifies the caller's thread-specific keyring (thread-keyring(7)).
  • KEY_SPEC_PROCESS_KEYRING โ€” this specifies the caller's process-specific keyring (process-keyring(7)).
  • KEY_SPEC_SESSION_KEYRING โ€” this specifies the caller's session-specific keyring (session-keyring(7)).
  • KEY_SPEC_USER_KEYRING โ€” this specifies the caller's UID-specific keyring (user-keyring(7)).
  • KEY_SPEC_USER_SESSION_KEYRING โ€” this specifies the caller's UID-session keyring (user-session-keyring(7)).

๐Ÿ’œ Key types ๐Ÿ’œ

The key type is a string that specifies the key's type ๐Ÿท๏ธ. Internally, the kernel defines a number of key types that are available in the core key management code. Among the types that are available for user-space use and can be specified as the type argument to add_key() are the following:

  • "keyring" โ€” keyrings are special key types that may contain links to sequences of other keys of any type ๐Ÿช†. If this interface is used to create a keyring, then payload should be NULL and size should be zero.
  • "user" โ€” this is a general purpose key type whose payload may be read and updated by user-space applications. The key is kept entirely within kernel memory ๐Ÿง . The payload for keys of this type is a blob of arbitrary data of up to 32,767 bytes.
  • "logon" (since Linux 3.3) โ€” this key type is essentially the same as "user", but it does not permit the key to read. This is suitable for storing payloads that you do not want to be readable from user space ๐Ÿคซ.

This key type vets the description to ensure that it is qualified by a "service" prefix, by checking to ensure that the description contains a ':' that is preceded by other characters.

  • "big_key" (since Linux 3.13) โ€” this key type is similar to "user", but may hold a payload of up to 1 MiB ๐Ÿ˜ณ. If the key payload is large enough, then it may be stored encrypted in tmpfs (which can be swapped out) rather than kernel memory.

For further details on these key types, see keyrings(7).


๐Ÿ”ฅ RETURN VALUE ๐Ÿ”ฅ

On success, add_key() returns the serial number of the key it created or updated ๐Ÿ‘‘. On error, -1 is returned and errno is set to indicate the error ๐Ÿ’€.


๐Ÿ’… ERRORS ๐Ÿ’…

  • EACCES โ€” the keyring wasn't available for modification by the user.
  • EDQUOT โ€” the key quota for this user would be exceeded by creating this key or linking it to the keyring ๐Ÿงพ.
  • EFAULT โ€” one or more of type, description, and payload points outside process's accessible address space ๐Ÿ‘ป.
  • EINVAL โ€” the size of the string (including the terminating null byte) specified in type or description exceeded the limit (32 bytes and 4096 bytes respectively).
  • EINVAL โ€” the payload data was invalid.
  • EINVAL โ€” type was "logon" and the description was not qualified with a prefix string of the form "service:".
  • EKEYEXPIRED โ€” the keyring has expired โŒ›.
  • EKEYREVOKED โ€” the keyring has been revoked ๐Ÿšซ.
  • ENOKEY โ€” the keyring doesn't exist.
  • ENOMEM โ€” insufficient memory to create a key ๐Ÿ˜ต.
  • EPERM โ€” the type started with a period ('.'). Key types that begin with a period are reserved to the implementation.
  • EPERM โ€” type was "keyring" and the description started with a period ('.'). Keyrings with descriptions (names) that begin with a period are reserved to the implementation.

๐ŸŒˆ STANDARDS ๐ŸŒˆ

Linux ๐Ÿง.


โญ HISTORY โญ

Linux 2.6.10.


๐Ÿซถ NOTES ๐Ÿซถ

glibc does not provide a wrapper for this system call, bestie ๐Ÿ˜ญ. A wrapper is provided in the libkeyutils library. (The accompanying package provides the <keyutils.h> header file.) When employing the wrapper in that library, link with -lkeyutils ๐Ÿ”—.


๐Ÿ“ EXAMPLES ๐Ÿ“

The program below creates a key with the type, description, and payload specified in its command-line arguments, and links that key into the session keyring. The following shell session demonstrates the use of the program:

$ ./a.out user mykey "Some payload";
Key ID is 64a4dca
$ grep '64a4dca' /proc/keys;
064a4dca I--Q---    1 perm 3f010000  1000  1000 user    mykey: 12

๐Ÿงš Program source ๐Ÿงš

#include <keyutils.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(int argc, char *argv[])
{
    key_serial_t key;

    if (argc != 4) {
        fprintf(stderr, "Usage: %s type description payload\n",
                argv[0]);
        exit(EXIT_FAILURE);
    }

    key = add_key(argv[1], argv[2], argv[3], strlen(argv[3]),
                  KEY_SPEC_SESSION_KEYRING);
    if (key == -1) {
        perror("add_key");
        exit(EXIT_FAILURE);
    }

    printf("Key ID is %jx\n", (uintmax_t) key);

    exit(EXIT_SUCCESS);
}

๐Ÿ”‘ SEE ALSO ๐Ÿ”‘

keyctl(1), keyctl(2), request_key(2), keyctl(3), keyrings(7), keyutils(7), persistent-keyring(7), process-keyring(7), session-keyring(7), thread-keyring(7), user-keyring(7), user-session-keyring(7)

The kernel source files Documentation/security/keys/core.rst and Documentation/keys/request-key.rst ๐Ÿ“š.


okay that's the whole page!! ๐Ÿ’– this came from the Linux man-pages crew, version 6.20, dated 2026-02-08 โ€” and honestly? slay. go add some keys bestie, no cap ๐Ÿซถโœจ๐Ÿ”‘

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: GPL-2.0-or-later
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms