matchpathcon(3)
π matchpathcon(3) β SELinux API documentation π
okay bestie, welcome to matchpathcon(3), straight out of the SELinux API documentation, and honestly? she's giving legacy icon. we're gonna get you the default SELinux security context for a path, from the file contexts config, no cap.
β¨ NAME β¨
matchpathcon, matchpathcon_index β get the default SELinux security context for the specified path from the file contexts configuration π
πΈ SYNOPSIS πΈ
#include <selinux/selinux.h>
int matchpathcon_init(const char *path);
int matchpathcon_init_prefix(const char *path, const char *prefix);
int matchpathcon_fini(void);
int matchpathcon(const char *path, mode_t mode, char **con);
int matchpathcon_index(const char *name, mode_t mode, char **con);
π¦ DESCRIPTION π¦
so first things first, and I need you to hear me on this one:
π¨ this whole family of functions is deprecated. like, retired. for new code, please useselabel_open(3)with theSELABEL_CTX_FILEbackend instead ofmatchpathcon_init(), useselabel_close(3)instead ofmatchpathcon_fini(), and useselabel_lookup(3)instead ofmatchpathcon().
the rest of what I'm about to tell you is for the legacy interface, iykyk. she's still here, she's just vintage π
π the functions, one by one π
matchpathcon_init() loads the file contexts configuration specified by path into memory, so the matchpathcon() calls that come after it can use it. if path is NULL, then the active file contexts configuration gets loaded by default β that's literally the path returned by selinux_file_context_path(3). and unless the MATCHPATHCON_BASEONLY flag has been set via set_matchpathcon_flags(3), files with the same path prefix but a .homedirs and .local suffix are also looked up and loaded if they're present. those files are what provide dynamically generated entries for user home directories and for local customizations. cute, right? π
matchpathcon_init_prefix() is the same as matchpathcon_init() but only loads entries with regular expressions whose first pathname component is a prefix of prefix. like, pass "/dev" if you're only ever gonna call matchpathcon() with pathnames beginning with /dev. but β and this is the tea β this optimization is no longer necessary thanks to the use of file_contexts.bin files with precompiled regular expressions, so using this interface is deprecated. periodt. π«Ά
matchpathcon_fini() frees the memory allocated by a prior call to matchpathcon_init.() β¨ you can use it to free and reset the internal state between multiple matchpathcon_init() calls, or just to free memory when you're done using matchpathcon(). clean up after yourself, bestie, it's self-care π§
matchpathcon() matches the specified pathname against the file contexts configuration and sets the security context con to refer to the resulting context. quick note on the pathname: it gets transformed via realpath(3), excepting any final symbolic link component if S_IFLNK was specified as the mode. and then it matches mode against the configuration too.
a few things you need to know, no skips:
- the caller must free the returned security context
conusingfreecon(3)when finished using it. not optional. that's your memory and it's your job π modecan be0to disable mode matching, but it should be provided whenever possible, since it may affect the matching.- only the file format bits (i.e. the file type) of the
modeare used. - if
matchpathcon_init()has not already been called, then this function will call it upon its first invocation with aNULLpath, defaulting to the active file contexts configuration. she's self-sufficient like that π
matchpathcon_index() is the same as matchpathcon() but returns a specification index that can later be used in a matchpathcon_filespec_add(3) call. main character energy, honestly β
π RETURN VALUE π
returns zero on success or -1 otherwise. that's it, that's the vibe check.
π SEE ALSO π
selinux(8)set_matchpathcon_flags(3)set_matchpathcon_invalidcon(3)set_matchpathcon_printf(3)matchpathcon_filespec_add(3)matchpathcon_checkmatches(3)freecon(3)setfilecon(3)setfscreatecon(3)
and that's the whole page, bestie!! π this one was brought to you by stephen.smalley.work@gmail.com, dated 21 November 2009 β yes, she's an elder, and we respect her πβ¨πΈπ¦ go forth and set those contexts responsibly π«Άπ
π¦ 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
- centos-stream 9: libselinux-devel 3.6-5.el9 Β· Public Domain
- centos-stream 9: libselinux-devel 3.6-5.el9 Β· 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
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms