selinux_restorecon(3)
π selinux_restorecon β restore file(s) default SELinux security contexts π
okay bestie, gather round, because this is the selinux_restorecon(3) page from the SELinux API documentation and it's literally the glow-up your file labels have been waiting for β¨
πΈ SYNOPSIS πΈ
#include <selinux/restorecon.h>
int selinux_restorecon(const char *pathname,
unsigned int restorecon_flags);
int selinux_restorecon_parallel(const char *pathname,
unsigned int restorecon_flags,
size_t nthreads);
π¦ DESCRIPTION π¦
selinux_restorecon() restores file default security contexts on filesystems that support extended attributes (see xattr(7)), and it does it based on these two icons π:
pathnamecontaining a directory or file to be relabeled. If this is a directory and therestorecon_flagsSELINUX_RESTORECON_RECURSEhas been set (for descending through directories), thenselinux_restorecon()will write an SHA256 digest of specfile entries calculated byselabel_get_digests_all_partial_matches(3)to an extended attribute ofsecurity.sehashonce the relabeling has been completed successfully (see the NOTES section for details). These digests will be checked shouldselinux_restorecon()be rerun with therestorecon_flagsSELINUX_RESTORECON_RECURSEflag set. If any of the specfile entries had been updated, the digest will also be updated. However if the digest is the same, no relabeling checks will take place. Therestorecon_flagsthat can be used to manage the usage of the SHA256 digest are:SELINUX_RESTORECON_SKIP_DIGESTSELINUX_RESTORECON_IGNORE_DIGEST
restorecon_flagscontains the labeling option/rules as follows:SELINUX_RESTORECON_SKIP_DIGESTβ Do not check or update any extended attributesecurity.sehashentries. no cap, we're leaving those attrs alone πSELINUX_RESTORECON_IGNORE_DIGESTβ force the checking of labels even if the stored SHA256 digest matches the specfile entries SHA256 digest. The specfile entries digest will be written to thesecurity.sehashextended attribute once relabeling has been completed successfully provided theSELINUX_RESTORECON_NOCHANGEflag has not been set, and no errors have been skipped during the file tree walk due to theSELINUX_RESTORECON_COUNT_ERRORSflag.SELINUX_RESTORECON_NOCHANGEβ don't change any file labels (passive check) or update the digest in thesecurity.sehashextended attribute. it's giving "look but don't touch" πSELINUX_RESTORECON_SET_SPECFILE_CTXβ If set, reset the files label to match the default specfile context. If not set only reset the files "type" component of the context to match the default specfile context.SELINUX_RESTORECON_RECURSEβ change file and directory labels recursively (descend directories) and if successful write an SHA256 digest of the specfile entries to an extended attribute as described in the NOTES section.SELINUX_RESTORECON_VERBOSEβ log file label changes. Note that ifSELINUX_RESTORECON_VERBOSEandSELINUX_RESTORECON_PROGRESSflags are set, thenSELINUX_RESTORECON_PROGRESSwill take precedence.SELINUX_RESTORECON_PROGRESSβ show progress by outputting the number of files in 1k blocks processed to stdout. If theSELINUX_RESTORECON_MASS_RELABELflag is also set then the approximate percentage complete will be shown.SELINUX_RESTORECON_MASS_RELABELβ generally set when relabeling the entire OS, that will then show the approximate percentage complete. TheSELINUX_RESTORECON_PROGRESSflag must also be set.SELINUX_RESTORECON_REALPATHβ convert passed-inpathnameto the canonical pathname usingrealpath(3).SELINUX_RESTORECON_XDEVβ prevent descending into directories that have a different device number than thepathnameentry from which the descent began.SELINUX_RESTORECON_ADD_ASSOCβ attempt to add an association between an inode and a specification. If there is already an association for the inode and it conflicts with the specification, then use the last matching specification.SELINUX_RESTORECON_ABORT_ON_ERRORβ abort on errors during the file tree walk.SELINUX_RESTORECON_SYSLOG_CHANGESβ log any label changes tosyslog(3).SELINUX_RESTORECON_LOG_MATCHESβ log what specfile context matched each file.SELINUX_RESTORECON_IGNORE_NOENTRYβ ignore files that do not exist. if it's not there, it's not our problem π«ΆSELINUX_RESTORECON_IGNORE_MOUNTSβ do not read/proc/mountsto obtain a list of non-seclabel mounts to be excluded from relabeling checks. SettingSELINUX_RESTORECON_IGNORE_MOUNTSis useful where there is a non-seclabel fs mounted with a seclabel fs mounted on a directory below this.SELINUX_RESTORECON_CONFLICT_ERRORβ to treat conflicting specifications, such as where two hardlinks for the same inode have different contexts, as errors.SELINUX_RESTORECON_COUNT_ERRORSβ Count, but otherwise ignore, errors during the file tree walk. Only makes a difference if theSELINUX_RESTORECON_ABORT_ON_ERRORflag is clear. Callselinux_restorecon_get_skipped_errors(3)for fetching the ignored (skipped) error count afterselinux_restorecon(3)orselinux_restorecon_parallel(3)completes with success. In case any errors were skipped during the file tree walk, the specfile entries SHA256 digest will not have been written to thesecurity.sehashextended attribute.SELINUX_RESTORECON_SET_USER_ROLEβ Set the user and role component of the security context too.SELINUX_RESTORECON_COUNT_RELABELEDβ Count the number of relabeled files or the number that would be relabeled ifSELINUX_RESTORECON_NOCHANGEwas not set.SELINUX_RESTORECON_SKIP_MULTILINKβ Skip any non-directory files that have more than one hard link. Use this option when relabeling untrusted directories that might contain hard links to files owned by others. π© lowkey a safety girlie
The behavior regarding the checking and updating of the SHA256 digest described above is the default behavior. It is possible to change this by first calling selabel_open(3) and not enabling the SELABEL_OPT_DIGEST option, then calling selinux_restorecon_set_sehandle(3) to set the handle to be used by selinux_restorecon(3).
If the pathname is a directory path, then it is possible to set directories to be excluded from the path by calling selinux_restorecon_set_exclude_list(3) with a NULL terminated list before calling selinux_restorecon(3).
By default selinux_restorecon(3) reads /proc/mounts to obtain a list of non-seclabel mounts to be excluded from relabeling checks unless the SELINUX_RESTORECON_IGNORE_MOUNTS flag has been set.
selinux_restorecon_parallel() is similar to selinux_restorecon(3), but accepts another parameter that allows to run relabeling over multiple threads π§΅β¨:
nthreadsspecifies the number of threads to use during relabeling. When set to1, the behavior is the same as callingselinux_restorecon(3). When set to0, the function will try to use as many threads as there are online CPU cores. When set to any other number, the function will try to use the given number of threads.
Note that to use the parallel relabeling capability, the calling process must be linked with the libpthread library (either at compile time or dynamically at run time). Otherwise the function will print a warning and fall back to the single threaded mode. bestie. link the library. i'm begging π
β RETURN VALUE β
On success, zero is returned. On error, -1 is returned and errno is set appropriately. that's the whole vibe check, 0 good, -1 not good π
π NOTES π
- To improve performance when relabeling file systems recursively (e.g. the
restorecon_flagsSELINUX_RESTORECON_RECURSEflag is set)selinux_restorecon()will write a calculated SHA256 digest of the specfile entries returned byselabel_get_digests_all_partial_matches(3)to an extended attribute namedsecurity.sehashfor each directory in thepathnamepath. - To check the extended attribute entry use
getfattr(1), for example:
getfattr -e hex -n security.sehash /
- Should any of the specfile entries have changed, then when
selinux_restorecon()is run again with theSELINUX_RESTORECON_RECURSEflag set, new SHA256 digests will be calculated and all files automatically relabeled depending on the settings of theSELINUX_RESTORECON_SET_SPECFILE_CTXflag (providedSELINUX_RESTORECON_NOCHANGEis not set). /sysand in-memory filesystems do not support thesecurity.sehashextended attribute and are automatically excluded from any relabeling checks. they said "not invited to the party" π- By default
stderris used to log output messages and errors. This may be changed by callingselinux_set_callback(3)with theSELINUX_CB_LOGtype option.
π SEE ALSO π
selabel_get_digests_all_partial_matches(3)selinux_restorecon_set_sehandle(3)selinux_restorecon_default_handle(3)selinux_restorecon_get_skipped_errors(3)selinux_restorecon_set_exclude_list(3)selinux_restorecon_set_alt_rootpath(3)selinux_restorecon_xattr(3)selinux_set_callback(3)
and that's the whole page, bestie π«Ά from the Security Enhanced Linux girlies, dated 20 Oct 2015, dropping selinux_restorecon(3) β go forth and relabel responsibly β¨ππ
π¦ where this page comes from
- centos-stream 10: libselinux-devel 3.11-3.el10 Β· 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
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms