KEYCTL_READ(3)

πŸ“¦
version 1.6.3 Β· 2 versions
  • 1.6.3 Β· alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, centos-stream 10, centos-stream 9, debian bookworm, debian trixie, fedora 43, fedora 44, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking (latest)
  • 1.6.1 Β· ubuntu jammy
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, centos-stream 10, centos-stream 9, debian bookworm, debian trixie, fedora 43, fedora 44, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– KEYCTL_READ β€” bestie, let's read a key πŸ’–

okay so this is a page from the Linux Key Management Calls section of the manual, section 3, and it's called KEYCTL_READ(3). it's giving "how do i actually look inside a key" energy and honestly? iconic. let's get into it 🫢


✨ NAME ✨

keyctl_read β€” read a key. that's it, that's the whole vibe πŸ’…


🌸 SYNOPSIS 🌸

#include <keyutils.h>

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

long keyctl_read_alloc(key_serial_t key, void **_buffer);

two functions, one dream. one reads into your buffer, one goes and gets a buffer for you like the main character it is πŸ’–


πŸ¦‹ DESCRIPTION πŸ¦‹

so keyctl_read() reads the payload of a key β€” if the key type supports it. if it doesn't, that's a whole other situation (see ERRORS, bestie, it's down there πŸ‘‡).

the caller must have read permission on a key to be able to read it. no permission, no payload. periodt.

buffer and buflen specify the buffer into which the payload data will be placed. now listen β€” if the buffer is too small, then the full size of the payload will be returned, and the contents of the buffer may be overwritten in some undefined way. undefined. way. no cap. don't be sloppy with your buffer sizes, i'm begging πŸ™ˆ

keyctl_read_alloc() is similar to keyctl_read() except that it allocates a buffer big enough to hold the payload data and places the data in it. if successful, a pointer to the buffer is placed in *_buffer. and bestie β€” the caller must free the buffer. yes YOU. it's your buffer now. you free it. no one else is coming to do it for you 😭

keyctl_read_alloc() adds a NUL character after the data it retrieves, though this is not counted in the size value it returns. sneaky lil extra byte, love that for us ✨


πŸ‘‘ READING KEYRINGS πŸ‘‘

this call can be used to list the contents of a keyring. the data is presented to the user as an array of key_serial_t values, each of which corresponds to a key to which the keyring holds a link. it's like a little guest list for the keyring's party πŸŽ€

the size of the keyring will be sizeof(key_serial_t) multiplied by the number of keys. and the size of key_serial_t is invariant across different word sizes, though the byte-ordering is as appropriate for the kernel. so like, consistent size, kernel-appropriate ordering. iykyk 🌈


πŸ’œ RETURN VALUE πŸ’œ

on success, keyctl_read() returns the amount of data placed into the buffer. and if the buffer was too small, then the size of buffer required will be returned, and the contents of the buffer may have been overwritten in some undefined way. again. undefined. way. we talked about this πŸ™ˆ

on success, keyctl_read_alloc() returns the amount of data in the buffer.

on error, both functions set errno to an appropriate code and return the value -1. classic πŸ’€


πŸ”₯ ERRORS πŸ”₯

vibe check on the ways this can go wrong:

  • ENOKEY β€” the key specified is invalid. like, that key doesn't exist bestie, who is she πŸ’€
  • EKEYEXPIRED β€” the key specified has expired. it had its moment, it's gone now πŸ₯€
  • EKEYREVOKED β€” the key specified had been revoked. someone took away its access, harsh but fair βœ‹
  • EACCES β€” the key exists, but is not readable by the calling process. it's there! you just can't look at it. boundaries, honestly πŸ’…
  • EOPNOTSUPP β€” the key type does not support reading of the payload data. this key type simply doesn't do that, and that's its right 🧚

πŸŽ€ 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, she'll ghost you otherwise 🫢


⭐ SEE ALSO ⭐

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

go read those too, they're all part of the same friendship group πŸ’–


and that's the whole page, bestie!! this little gem is from the Linux man page collection and it's dated 21 Feb 2014. stay hydrated, free your buffers, and slay πŸ’…βœ¨πŸŒΈ

πŸ“¦ where this page comes from