selabel_open(3)

✨ selabel_open(3) β€” SELinux API documentation ✨

okay bestie, sit down, hydrate, and let me tell you about the two little functions that run the whole SELinux labeling situation: selabel_open and selabel_close. πŸ’…


πŸŽ€ NAME πŸŽ€

selabel_open, selabel_close β€” the userspace SELinux labeling interface, no cap.


🌸 SYNOPSIS 🌸

#include <selinux/selinux.h>
#include <selinux/label.h>

struct selabel_handle *selabel_open(unsigned int backend,
                                    const struct selinux_opt *options,
                                    unsigned nopt);

void selabel_close(struct selabel_handle *hnd);

πŸ’– DESCRIPTION πŸ’–

So selabel_open() is the glow-up moment where you initialize a labeling handle β€” you're gonna use that handle for all your lookup operations later. The backend argument tells it which backend you're opening, and the whole roster of current backends is chilling down in BACKENDS below. πŸ‘‘

The options argument should be NULL, OR a pointer to an array of selinux_opt structs that's nopt long:

struct selinux_opt {
    int         type;
    const char  *value;
};

Which option types exist? They're described in GLOBAL OPTIONS below, plus in the docs for each individual backend, so go read those too bestie. 🫢 On success you get back a non-NULL value, and you use that in all your later label operations.

Then selabel_close() is the "okay we're done here" β€” it terminates use of a handle and frees any internal resources attached to it. After you've made this call, you must NOT use the handle again. Like, ever. It's over. πŸ’€


✨ GLOBAL OPTIONS ✨

These are the global options you can pass to selabel_open():

  • SELABEL_OPT_UNUSED β€” the option with a type code of zero is a no-op. Which is lowkey genius, because it means you can initialize an array of options to zero and any elements you never touched won't throw an error at you. 🧚
  • SELABEL_OPT_VALIDATE β€” a non-null value for this one turns on context validation. By default it uses security_check_context(3), but you can hand it a custom validation function through selinux_set_callback(3). Heads up though: an invalid context might not be treated as an error unless it's actually encountered during a lookup operation. So it's giving "we'll deal with it when we get there." πŸ™ˆ
  • SELABEL_OPT_DIGEST β€” a non-null value here enables generation of a SHA256 digest of the spec files loaded, exactly as described in selabel_digest(3). πŸ”₯

🌈 BACKENDS 🌈

  • SELABEL_CTX_FILE β€” the file contexts backend, fully described in selabel_file(5).
  • SELABEL_CTX_MEDIA β€” the media contexts backend, described in selabel_media(5).
  • SELABEL_CTX_X β€” the X Windows contexts backend, described in selabel_x(5).
  • SELABEL_CTX_DB β€” the database objects contexts backend, described in selabel_db(5).

⭐ RETURN VALUE ⭐

On success, you get a non-NULL handle value back. πŸŽ‰ On error, NULL is returned and errno is set appropriately β€” classic, honestly.


πŸ‘‘ AUTHOR πŸ‘‘

Eamon Walsh <ewalsh@tycho.nsa.gov> β€” king behavior, we thank him for his service. 🫢


πŸ’œ SEE ALSO πŸ’œ

selabel_lookup(3), selabel_stats(3), selinux_set_callback(3), selinux(8)


and that's the whole page bestie!! this one's from the SELinux API documentation, dated 18 Jun 2007 β€” yes, 2007, it's vintage, it's iconic, it's still slaying. go forth and label things responsibly πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from