KEYCTL_GET_SECURITY(3)

✨ KEYCTL_GET_SECURITY(3) ✨

okay bestie, welcome to the Linux Key Management Calls corner of the man pages πŸ’– this one is KEYCTL_GET_SECURITY and she's here to spill the tea on a key's security context. no cap.


πŸ’… NAME

keyctl_get_security β€” pull up a key's security context, fr fr


πŸŽ€ SYNOPSIS

#include <keyutils.h>

long keyctl_get_security(key_serial_t key, char *buffer,
size_t buflen);

long keyctl_get_security_alloc(key_serial_t key, char **_buffer);

πŸ¦‹ DESCRIPTION

so keyctl_get_security() retrieves the security context of a key, and she gives it to you as a NUL-terminated string ✨ it's rendered in whatever form is appropriate to the LSM that's currently in force β€” like, if you're running SELinux it may look like this:

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

and honestly? iconic. no notes.

heads up though β€” the caller must have view permission on a key to be able to get its security context. no view permission, no security context. that's the vibe check and you did not pass it 😭

okay so buffer and buflen specify the buffer into which the string will be placed 🫢 but if that buffer is too small, the full size of the string gets returned anyway, and no copy will take place β€” so basically she tells you how much room she needs and then just stands there πŸ’€

then we have keyctl_get_security_alloc(), which is similar to keyctl_get_security() except that it allocates a buffer big enough to hold the string and copies the string into it 🌸 she's the main character who handles the logistics for you. if successful, a pointer to the buffer is placed in *_buffer. and bestie, listen β€” the caller must free the buffer. that's your responsibility, not hers πŸ™ˆ


πŸ‘‘ RETURN VALUE

on success, keyctl_get_security() returns the amount of data placed into the buffer ✨ if the buffer was too small, then the size of buffer required will be returned, but no data will be transferred. on error, the value -1 will be returned and errno will have been set to an appropriate error πŸ’€

on success, keyctl_get_security_alloc() returns the amount of data in the buffer, less the NUL terminator (she's not counting that little guy). on error, the value -1 will be returned and errno will have been set to an appropriate error 😭


🌈 ERRORS

  • ENOKEY β€” the key specified is invalid. not a real key, not invited to the function 🚫
  • EKEYEXPIRED β€” the key specified has expired. she had her moment, it's over now ⏰
  • EKEYREVOKED β€” the key specified had been revoked. the key said "we're done here" πŸ’”
  • EACCES β€” the key exists, but is not viewable by the calling process. she's real, you just can't look at her πŸ™ˆ

πŸ”— LINKING

this is a library function that can be found in libkeyutils 🧚 when linking, -lkeyutils should be specified to the linker. don't forget it or she will simply not show up, periodt.


πŸ’œ SEE ALSO

keyctl(1), add_key(2), keyctl(2), request_key(2), keyctl(3), keyrings(7), keyutils(7)


and that's the whole page, bestie πŸ’– this one's from the Linux crew, dated 26 Feb 2010 β€” old but she ate, and honestly? still serving. go get your security contexts, iykyk πŸŽ€βœ¨

πŸ“¦ where this page comes from