selabel_media(5)
π selabel_media β the media contexts backend, explained bestie π
okay so this is the manual page for selabel_media(5), which lives in the SELinux API documentation family. it's the whole vibe of the userspace SELinux labeling interface and configuration file format for the media contexts backend. yes that's a mouthful, we'll break it down π
β¨ NAME β¨
selabel_media β the userspace SELinux labeling interface and configuration file format for the media contexts backend π¦
β¨ SYNOPSIS β¨
#include <selinux/label.h>
int selabel_lookup(struct selabel_handle *hnd,
char **context,
const char *device_name, int unused);
int selabel_lookup_raw(struct selabel_handle *hnd,
char **context,
const char *device_name, int unused);
two functions, same shape, one does a little extra magic. stay with me π
β¨ DESCRIPTION β¨
so the media contexts backend is literally the thing that maps from media device names β think "cdrom" or "floppy" β into security contexts πΈ it's what you use to find the right context for establishing context mounts on those devices. slay.
the context it hands back? you gotta free it with freecon(3), no leaving that lying around bestie π«Ά
selabel_lookup(3) is where you go to read up on the function, plus its return codes and error codes. it's all there.
the integer lookup argument is currently unused and you should set it to zero. periodt.
any messages that selabel_lookup(3) generates get sent to stderr by default β but you can change that with selinux_set_callback(3). iconic.
selabel_lookup_raw(3) behaves identically to selabel_lookup(3), except it does not perform context translation. that's the whole difference, fr fr.
and the FILES section (scroll down π) has all the tea on the configuration files used to determine the media context.
β¨ OPTIONS β¨
on top of the global options that selabel_open(3) already describes, this backend also recognizes the following:
SELABEL_OPT_PATHβ a non-null value for this option specifies a path to a file that will be opened in lieu of the standard media contexts file. so basically: "not that file, this file instead" π
β¨ FILES β¨
which media context file gets used to retrieve a default context depends on the SELABEL_OPT_PATH parameter you passed to selabel_open(3). here's the breakdown:
- if it's
NULL, then theSELABEL_OPT_PATHvalue defaults to the active policy media contexts location β the one returned byselinux_media_context_path(3)π¦ - otherwise, the actual
SELABEL_OPT_PATHvalue you specified is what gets used. you said it, we use it.
the default media contexts file is:
/etc/selinux/{SELINUXTYPE}/contexts/files/media
where {SELINUXTYPE} is the entry from the selinux configuration file config β see selinux_config(5) if you need it π
β¨ FILE FORMAT β¨
every single line inside the media file looks like this:
device_name context
and here's what each piece means:
device_nameβ the media identifier (e.g.cdrom,floppy,diskandusb).contextβ the context to be used for labeling the device.
example, because examples eat every time π:
# contexts/files/media
cdrom system_u:object_r:removable_device_t
floppy system_u:object_r:removable_device_t
disk system_u:object_r:fixed_disk_device_t
β¨ NOTES β¨
if you want contexts to be validated, then the global optionSELABEL_OPT_VALIDATEmust be set before callingselabel_open(3). if it's not set, then it is possible for an invalid context to be returned π so like. set it. please.
β¨ SEE ALSO β¨
selinux(8), selabel_open(3), selabel_lookup(3), selabel_stats(3), selabel_close(3), selinux_set_callback(3), selinux_media_context_path(3), freecon(3), selinux_config(5)
and that's the whole page, bestie β go forth and label your media devices correctly πβ¨
this was brought to you by the Security Enhanced Linux girlies, dated 29 Nov 2011, which lowkey means this page is older than most of us and still serving main character energy π no cap. mwah π
π¦ where this page comes from
- alpine 3.24: libselinux-doc 3.10-r1 Β· Public-Domain
- centos-stream 10: libselinux-utils 3.11-3.el10 Β· LicenseRef-Fedora-Public-Domain
- centos-stream 9: libselinux-utils 3.6-5.el9 Β· Public Domain
- debian bookworm: selinux-utils 3.4-1+b6
- debian trixie: selinux-utils 3.8.1-1 Β· LicenseRef-public-domain
- fedora 43: libselinux-utils 3.9-5.fc43 Β· LicenseRef-Fedora-Public-Domain
- fedora 44: libselinux-utils 3.10-1.fc44 Β· LicenseRef-Fedora-Public-Domain
- fedora 44: libselinux-utils 3.11-2.fc44 Β· LicenseRef-Fedora-Public-Domain
- nixos 26.05: libselinux 3.10
- nixpkgs unstable: libselinux 3.11
- opensuse-leap 16.0: selinux-tools 3.8.1-160000.4.1 Β· SUSE-Public-Domain
- opensuse-leap 16.0: selinux-tools 3.8.1-160000.4.1 Β· SUSE-Public-Domain
- opensuse-leap 16.0: selinux-tools 3.8.1-160000.4.1 Β· SUSE-Public-Domain
- opensuse-leap 16.0: selinux-tools 3.8.1-160000.4.1 Β· SUSE-Public-Domain
- ubuntu jammy: selinux-utils 3.3-1build2
- ubuntu noble: selinux-utils 3.5-2ubuntu2 Β· LicenseRef-public-domain
- ubuntu noble: selinux-utils 3.5-2ubuntu2.1 Β· LicenseRef-public-domain
- ubuntu questing: selinux-utils 3.8.1-1build1
- ubuntu resolute: selinux-utils 3.9-4build1 Β· LicenseRef-public-domain
- ubuntu stonking: selinux-utils 3.11-2.1 Β· LicenseRef-public-domain
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms