key_alloc(9)
π key_alloc(9) β from the Kernel API Manual, bestie
okay so picture this: you're deep in ./security/keys land and you need a brand new key. that's literally this function. welcome π«Ά
β¨ NAME β¨
key_alloc β allocate a key of the specified type. that's it, that's the whole gig π
πΈ SYNOPSIS πΈ
struct key * key_alloc (struct key_type *type , const char *desc ,
kuid_t uid , kgid_t gid , const struct cred *cred , key_perm_t perm ,
unsigned long flags , struct key_restriction *restrict_link );
π ARGUMENTS π
typeβ the type of key you're allocating. gotta pick one, bestie.descβ the key description, which is the thing that lets the key be searched out later. iykyk, naming matters.uidβ the owner of the new key.gidβ the group ID that's used for the new key's group permissions.credβ the credentials that specify the UID namespace.permβ the permissions mask of the new key.flagsβ flags that specify quota properties.restrict_linkβ optional link restriction for new keyrings. optional, so like... no pressure, but also read the description π
π¦ DESCRIPTION π¦
So you call this and it allocates a key of the specified type with all the attributes you handed over. The key comes back to you in an uninstantiated state β which means the caller (that's you, hun) needs to instantiate the key before returning. don't just walk away with a half-done key, that's not the vibe π
The restrict_link structure β if it's not NULL β will be freed when the keyring is destroyed, so it must be dynamically allocated. like, actually dynamically allocated. no stack tricks, no cap. π
The user's key count quota gets updated to reflect the creation of the key, and the user's key data quota has the default for the key type reserved. The instantiation function should amend this as necessary. If there isn't enough quota available, you'll get -EDQUOT back. rude but fair.
The LSM security modules can prevent a key from being created, and in that case you get -EACCES back. it's giving bouncer-at-the-club energy π«
Returns a pointer to the new key if it worked, and an error code otherwise. slay.
β οΈ real talk: note that the caller needs to ensure the key type isn't uninstantiated. Internally this can be done by lockingkey_types_sem. Externally, it can be done by either never unregistering the key type, or by making surekey_alloccalls don't race with module unloading. that last one is a whole vibe check you do not want to fail, fr fr. π₯
π SEE ALSO π
Kernel file ./security/keys/key.c β and also these iconic siblings: key_payload_reserve(9), key_instantiate_and_link(9), key_reject_and_link(9), key_put(9), key_create_or_update(9), key_create(9), key_update(9), key_revoke(9), key_invalidate(9), generic_key_instantiate(9), register_key_type(9), unregister_key_type(9). go read them, they ate π
and that's the whole page, bestie! π this came out of ./security/keys, dated October 2026, and yes it's key_alloc(9) β the main character of this particular manpage. mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms