KEYCTL_SEARCH(3)
π KEYCTL_SEARCH β ok so you wanna find a key in a keyring bestie
welcome to the Linux Key Management Calls chapter of the manual, specifically the keyctl_search(3) entry π this is a <keyutils.h> situation and honestly? it's giving treasure hunt but make it kernel. let's go β¨
πΈ NAME πΈ
keyctl_search β search a keyring for a key π no cap
β¨ SYNOPSIS β¨
#include <keyutils.h>
long keyctl_search(key_serial_t keyring, const char *type,
const char *description, key_serial_t destination);
look at that signature. four args. gorgeous. iconic. we love to see it π
π¦ DESCRIPTION π¦
okay so keyctl_search() recursively searches the keyring for a key of the specified type and description. like it goes digging. it does not stop at the top level. it's thorough. it's committed. main character energy fr fr.
If found, the key will be attached to the destination keyring (if given), and its serial number will be returned. π«Ά
now here's the permission tea β The source keyring must grant search permission to the caller, and for a key to be found, it must also grant search permission to the caller. Child keyrings will be only be recursively searched if they grant search permission to the caller as well. so basically everyone in the chain has to say yes or the whole thing falls apart π
If the destination keyring is zero, no attempt will be made to forge a link to the key, and just the serial number will be returned. (zero = no linking, just vibes and a number)
If the destination keyring is given, then the link may only be formed if the found key grants the caller link permission and the destination keyring grants the caller write permission. two yeses minimum, bestie π€
If the search is successful, and if the destination keyring already contains a link to a key that matches the specified type and description, then that link will be replaced by a link to the found key. out with the old, in with the new, periodt β¨
The source keyring and destination keyring serial numbers may be those of valid keyrings to which the caller has appropriate permission, or they may be special keyring IDs:
KEY_SPEC_THREAD_KEYRINGβ This specifies the caller's thread-specific keyring. π§΅KEY_SPEC_PROCESS_KEYRINGβ This specifies the caller's process-specific keyring. πKEY_SPEC_SESSION_KEYRINGβ This specifies the caller's session-specific keyring. πKEY_SPEC_USER_KEYRINGβ This specifies the caller's UID-specific keyring. πKEY_SPEC_USER_SESSION_KEYRINGβ This specifies the caller's UID-session keyring. π
iykyk these special IDs are how you point at the keyrings the kernel already made for you without knowing their serial numbers π
β RETURN VALUE β
On success keyctl_search() returns the serial number of the key it found. π you got your number, bestie!
On error, the value -1 will be returned and errno will have been set to an appropriate error. π not the -1β¦
π₯ ERRORS π₯
ENOKEYβ One of the keyrings doesn't exist, no key was found by the search, or the only key found by the search was a negative key. (a negative key is a "no" stored as a key, so likeβ¦ you found a no. sad.)ENOTDIRβ One of the keyrings is a valid key that isn't a keyring. π« not a directory, not a keyring, it's just some guyEKEYEXPIREDβ One of the keyrings has expired, or the only key found was expired. βEKEYREVOKEDβ One of the keyrings has been revoked, or the only key found was revoked. π«ENOMEMβ Insufficient memory to expand the destination keyring. π the kernel ran out of room, it happens to the best of usEDQUOTβ The key quota for this user would be exceeded by creating a link to the found key in the destination keyring. πEACCESβ The source keyring didn't grant search permission, the destination keyring didn't grant write permission or the found key didn't grant link permission to the caller. π permission denied bestie, go ask nicely
π LINKING π
Although this is a Linux system call, it is not present in libc but can be found rather in libkeyutils. When linking, -lkeyutils should be specified to the linker.
so when you're compiling, don't forget the flag or you'll be staring at undefined reference errors like ποΈπποΈ
-lkeyutils
π SEE ALSO π
keyctl(1), add_key(2), keyctl(2), request_key(2), keyctl(3), keyrings(7), keyutils(7)
the whole friend group fr fr π«Ά
and that's the page babes π this entry lives in the Linux manual, dated 4 May 2006 β yeah she's a classic, an OG, and she still slays. go search some keyrings, you got this πβ¨π
π¦ where this page comes from
- alpine 3.22: keyutils-doc 1.6.3-r4 Β· GPL-2.0-or-later AND LGPL-2.0-or-later
- alpine 3.23: keyutils-doc 1.6.3-r4 Β· GPL-2.0-or-later AND LGPL-2.0-or-later
- alpine 3.24: keyutils-doc 1.6.3-r4 Β· GPL-2.0-or-later AND LGPL-2.0-or-later
- arch rolling: keyutils 1.6.3-4 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- centos-stream 10: keyutils-libs-devel 1.6.3-5.el10 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- centos-stream 9: keyutils-libs-devel 1.6.3-1.el9 Β· GPLv2+ and LGPLv2+
- centos-stream 9: keyutils-libs-devel 1.6.3-1.el9 Β· GPLv2+ and LGPLv2+
- debian bookworm: libkeyutils-dev 1.6.3-2 Β· GPL-2.0-or-later
- debian trixie: libkeyutils-dev 1.6.3-6 Β· GPL-2.0-or-later
- fedora 43: keyutils-libs-devel 1.6.3-6.fc43 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- fedora 43: keyutils-libs-devel 1.6.3-6.fc43 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- fedora 44: keyutils-libs-devel 1.6.3-7.fc44 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- fedora 44: keyutils-libs-devel 1.6.3-7.fc44 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- nixpkgs unstable: keyutils 1.6.3
- opensuse-leap 16.0: keyutils 1.6.3-160000.3.2 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- opensuse-leap 16.0: keyutils 1.6.3-160000.3.2 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- opensuse-leap 16.0: keyutils 1.6.3-160000.3.2 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- opensuse-leap 16.0: keyutils 1.6.3-160000.3.2 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- ubuntu jammy: libkeyutils-dev 1.6.1-2ubuntu3 Β· GPL-2.0-or-later
- ubuntu noble: libkeyutils-dev 1.6.3-3build1 Β· GPL-2.0-or-later
- ubuntu questing: libkeyutils-dev 1.6.3-6ubuntu2
- ubuntu resolute: libkeyutils-dev 1.6.3-6ubuntu3 Β· GPL-2.0-or-later
- ubuntu stonking: libkeyutils-dev 1.6.3-6ubuntu3 Β· GPL-2.0-or-later
- Copyright (C) 2006 Red Hat, Inc. All Rights Reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms