selinux_set_mapping(3)
π selinux_set_mapping(3) π
okay bestie, so this one lives in the SELinux API documentation section of the manual, and it's literally the page about dynamic object class and permission mapping. it's giving "make your own numbering system" energy, and honestly? kinda iconic.
β¨ NAME β¨
selinux_set_mapping β establish dynamic object class and permission mapping π¦
πΈ SYNOPSIS πΈ
#include <selinux/selinux.h>
struct security_class_mapping {
const char *name;
const char *perms[];
};
int selinux_set_mapping(const struct security_class_mapping *map);
π DESCRIPTION π
so selinux_set_mapping() does the thing where it establishes a mapping from your ordering of object classes and permissions β to the actual numbers that the currently loaded system policy is using. like, you pick the order you want, and this function translates it into policy reality. fr fr.
if you're using this function, there's a whole extra step you need to know about: apps should also set a SELINUX_CB_POLICYLOAD callback via selinux_set_callback(3). why? because that callback needs to call this function again whenever the policy reloads, so the mapping gets re-created in case the class or permission values changed in the new policy. policy reloads are messy like that. π
and now for the plot twist: it's generally preferred to instead use selinux_check_access(3) rather than avc_has_perm(3) or security_compute_av(3), and to justβ¦ not use this function at all. no cap. that's straight from the page. π
once the mapping is established, all libselinux functions that operate on class and permission values will take the user-provided numbers. and those numbers are determined like this:
the map argument is an array of security_class_mapping structures, and it must be terminated by a structure whose name field is NULL. that's the little "we're done here" sentinel, bestie. except for that last structure, the name field should point to the string name of an object class, and the matching perms field should point to an array of permission bit names terminated by a NULL string. (yes, another NULL terminator. they love a NULL terminator here. π)
the object classes named in the mapping, and the bit indexes of each set of permission bits named in the mapping, are numbered in order starting from 1. those numbers are the values you should be passing to subsequent libselinux calls. periodt.
π RETURN VALUE π
0β returned on success β¨-1β returned on error, anderrnogets set appropriately
π₯ ERRORS π₯
EINVALβ one of the class or permission names you requested in the mapping is not present in the loaded policy. (rip, that name doesn't exist in this policy π)ENOMEMβ an attempt to allocate memory failed π
π EXAMPLE π
struct security_class_mapping map[] = {
{ "file", { "create", "unlink", "read", "write", NULL } },
{ "socket", { "bind", NULL } },
{ "process", { "signal", NULL } },
{ NULL }
};
if (selinux_set_mapping(map) < 0)
exit(1);
so in this example, after the call has succeeded: classes file, socket, and process will be identified by 1, 2 and 3 respectively. and the permissions create, unlink, read, and write (for the file class) will be identified by 1, 2, 4, and 8 respectively. yes those are powers of two, she's doing bit math, she ate. π
β οΈ classes and permissions that are not listed in the mapping cannot be used. like, at all. so don't forget anybody important. π«Ά
π¦ AUTHOR π¦
Originally Eamon Walsh. Updated by Stephen Smalley <stephen.smalley.work@gmail.com> π
β SEE ALSO β
k bye bestie, go map those classes πβ¨π β this lil page is from the SELinux API documentation, dated 12 Jun 2008, and it's the selinux_set_mapping(3) page itself. stay slay, stay selinux. π§π
π¦ where this page comes from
- alpine 3.24: libselinux-doc 3.10-r1 Β· Public-Domain
- centos-stream 10: libselinux-devel 3.11-3.el10 Β· LicenseRef-Fedora-Public-Domain
- debian trixie: libselinux1-dev 3.8.1-1 Β· LicenseRef-public-domain
- fedora 43: libselinux-devel 3.9-5.fc43 Β· LicenseRef-Fedora-Public-Domain
- fedora 43: libselinux-devel 3.9-5.fc43 Β· LicenseRef-Fedora-Public-Domain
- fedora 44: libselinux-devel 3.10-1.fc44 Β· LicenseRef-Fedora-Public-Domain
- fedora 44: libselinux-devel 3.10-1.fc44 Β· LicenseRef-Fedora-Public-Domain
- fedora 44: libselinux-devel 3.11-2.fc44 Β· LicenseRef-Fedora-Public-Domain
- fedora 44: libselinux-devel 3.11-2.fc44 Β· LicenseRef-Fedora-Public-Domain
- nixos 26.05: libselinux 3.10
- nixpkgs unstable: libselinux 3.11
- opensuse-leap 16.0: libselinux-devel 3.8.1-160000.4.1 Β· SUSE-Public-Domain
- opensuse-leap 16.0: libselinux-devel 3.8.1-160000.4.1 Β· SUSE-Public-Domain
- opensuse-leap 16.0: libselinux-devel 3.8.1-160000.4.1 Β· SUSE-Public-Domain
- opensuse-leap 16.0: libselinux-devel 3.8.1-160000.4.1 Β· SUSE-Public-Domain
- ubuntu questing: libselinux1-dev 3.8.1-1build1
- ubuntu resolute: libselinux-dev 3.9-4build1 Β· LicenseRef-public-domain
- ubuntu stonking: libselinux-dev 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