KEYCTL_GET_SECURITY(2const)

πŸ’– KEYCTL_GET_SECURITY πŸ’–

okay bestie, sit down, hydrate, because we're about to read a kernel key's entire aura. this is a <pink>KEYCTL_GET_SECURITY</pink> page from the <purple>2const</purple> corner of the manual β€” yes, that's the section for the "just constants, no libc wrapper" girlies. and it's lowkey one of the cutest little syscalls you'll ever meet. πŸŽ€


✨ NAME ✨

KEYCTL_GET_SECURITY β€” manipulate the kernel's key management facility 😌✨

πŸ“š LIBRARY πŸ“š

Standard C library (libc, -lc). the classics. πŸ’…

🧚 SYNOPSIS 🧚

#include <linux/keyctl.h>  /* Definition of KEY* constants */
#include <sys/syscall.h>   /* Definition of SYS_* constants */
#include <unistd.h>

long syscall(size_t n;
             SYS_keyctl, KEYCTL_GET_SECURITY, key_serial_t key,
             char buf[_Nullable n], size_t n);

not me including <linux/keyctl.h> and <sys/syscall.h> like the dual-threat icon you are. 🌈


🌸 DESCRIPTION 🌸

KEYCTL_GET_SECURITY (since Linux 2.6.26)

okay so what does it DO. it gets the LSM (that's Linux Security Module) security label of the key you point at. it's giving "show me your papers bestie" but for keys. πŸͺͺ✨

  • the ID of the key whose security label you want lives in key.
  • the security label itself (terminated by a null byte, obviously, it's C, she's got a null terminator like every other string) gets dropped into the buffer that buf points at.
  • and the size of that buffer? you must hand it over in n. no size, no service. 🫢

here's the little plot twist: if buf is NULL, or the buffer size in n is too small, then the full size of the security label string β€” and yes, that includes the terminating null byte, don't forget her 😭 β€” gets returned as the function result, and nothing gets copied into the buffer. so this is your classic "probe first, allocate second, call again" move. iconique behaviour. πŸ‘‘

the caller must have view permission on the specified key. no peeking without permission, babe. πŸ™ˆ

the returned security label string gets rendered in whatever form is appropriate to the LSM currently in force. for example, with SELinux, it may look like:

unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

i know. she's long. she's specific. she's serving. πŸ“

and if no LSM is currently in force? then an empty string gets placed in the buffer. the silence. the void. the "nothing to report, bestie". πŸ’€


πŸ’« RETURN VALUE πŸ’«

  • On success β€” the size of the LSM security label string, including the terminating null byte, irrespective of the provided buffer size. so yeah, you get the real size even if your buffer was a whole clown 🀑
  • On error β€” -1 is returned, and errno is set to indicate the error. classic. πŸŽ€

πŸ¦‹ VERSIONS πŸ¦‹

a wrapper is provided in the libkeyutils library: keyctl_get_security(3). if you don't wanna rawdog the syscall, that's your girl. 🫢

⭐ STANDARDS ⭐

Linux. that's it, that's the standard. periodt. πŸ’…

πŸ‘‘ HISTORY πŸ‘‘

Linux 2.6.26. she's been around since 2.6.26 and she's still slaying. πŸ”₯


🌈 SEE ALSO 🌈


reminder bestie: buf as NULL or a too-small n means nothing gets copied β€” you just get the size back. don't go reading a buffer you never filled. that's not main character energy, that's undefined-ish behaviour energy. 😭

mwah, that's the whole page! this one's from Linux man-pages 6.20, dated 2026-02-08, and she's filed under KEYCTL_GET_SECURITY(2const). go forth and read those security labels, you gorgeous kernel gremlin. πŸŽ€βœ¨πŸ’–

πŸ“¦ 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: Linux-man-pages-copyleft
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms