Skip to contentSkip to conversation

setfiles: Add -A option to disable SELINUX_RESTORECON_ADD_ASSOC

PATCH New Richard W.M. Jones 3 participants 7 messages Sep 1, 2025 – Sep 2, 2025 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 setfiles: Add -A option to disable SELINUX_RESTORECON_ADD_ASSOC

SELINUX_RESTORECON_ADD_ASSOC tracks conflicts between inodes that have differing contexts. However doing this involves building a large internal hashtable that stores the full path of every file examined by setfiles. For filesystems that have very large numbers of files or long pathnames, this uses a lot of memory, which makes SELinux relabelling in constrained memory environments infeasible.

This adds a new 'setfiles -A' option that disables this tracking.

For example, using setfiles to relabel a filesystem with 15 million files took 3.7GB of RAM. Using this option, the same filesystem can be relabelled in 121MB (albeit with no warnings or errors possible for conflicting labels, but for our use case we don't care about that.)

While this is quite niche, reducing the memory usage of setfiles allows us to do offline SELinux relabelling without worrying about the number of files in the filesystem.

(I also posted it as a pull request here:
https://github.com/SELinuxProject/selinux/pull/490)

#1 lore raw

PATCH 1/1 setfiles: Add -A option to disable SELINUX_RESTORECON_ADD_ASSOC

SELINUX_RESTORECON_ADD_ASSOC tracks conflicts between inodes that have differing contexts. However doing this involves building a large internal hashtable that stores the full path of every file examined by setfiles. For filesystems that have very large numbers of files or long pathnames, this uses a lot of memory, which makes SELinux relabelling in constrained memory environments infeasible.

This adds a new setfiles -A option that disables this tracking.

For example, using setfiles to relabel a filesystem with 15 million files took 3.7GB of RAM. Using this option, the same filesystem can be relabelled in 121MB (albeit with no warnings or errors possible for conflicting labels, but for our use case we don't care about that.)

---
 policycoreutils/setfiles/setfiles.8 | 4 ++++
 policycoreutils/setfiles/setfiles.c | 5 ++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
policycoreutils/setfiles/setfiles.8 +4 -0
index eabf0a1c..7c9c5d39 100644
@@ -23,6 +23,7 @@ setfiles \- set SELinux file security contexts.
 .RB [ \-I | \-D ]
 .RB [ \-T
 .IR nthreads ]
+.RB [ \-A ]
 .I spec_file
 .IR pathname \ ...
 
@@ -187,6 +188,9 @@ use up to
 threads.  Specify 0 to create as many threads as there are available
 CPU cores; 1 to use only a single thread (default); or any positive
 number to use the given number of threads (if possible).
+.TP
+.B \-A
+do not track conflicting inodes (saves memory)
 
 .SH "ARGUMENTS"
 .TP
policycoreutils/setfiles/setfiles.c +4 -1
index ad09f840..40f2e7fe 100644
@@ -147,7 +147,7 @@ int main(int argc, char **argv)
 	const char *base;
 	int errors = 0;
 	const char *ropts = "e:f:hiIDlmno:pqrsvFURW0xT:";
-	const char *sopts = "c:de:f:hiIDlmno:pqr:svCEFUR:W0T:";
+	const char *sopts = "c:de:f:hiIDlmno:pqr:svCEFUR:W0T:A";
 	const char *opts;
 	union selinux_callback cb;
 	long unsigned skipped_errors;
@@ -375,6 +375,9 @@ int main(int argc, char **argv)
 			if (*optarg == '\0' || *endptr != '\0')
 				usage(argv[0]);
 			break;
+		case 'A':
+			r_opts.add_assoc = 0;
+			break;
 		case 'h':
 		case '?':
 			usage(argv[0]);
Signature · 1 line
2.50.1
maintainer Acked-by on v1 1/1
#2 lore raw
Quoted 10 lines · Richard W.M. Jones

SELINUX_RESTORECON_ADD_ASSOC tracks conflicts between inodes that have differing contexts. However doing this involves building a large internal hashtable that stores the full path of every file examined by setfiles. For filesystems that have very large numbers of files or long pathnames, this uses a lot of memory, which makes SELinux relabelling in constrained memory environments infeasible.

This adds a new setfiles -A option that disables this tracking.

Quoted 7 lines

For example, using setfiles to relabel a filesystem with 15 million files took 3.7GB of RAM. Using this option, the same filesystem can be relabelled in 121MB (albeit with no warnings or errors possible for conflicting labels, but for our use case we don't care about that.)

I don't think we usually include downstream issue tracker links in our upstream commit messages, but no need to re-submit just for that.

Quoted 54 lines
---
 policycoreutils/setfiles/setfiles.8 | 4 ++++
 policycoreutils/setfiles/setfiles.c | 5 ++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
policycoreutils/setfiles/setfiles.8 +4 -0
index eabf0a1c..7c9c5d39 100644
@@ -23,6 +23,7 @@ setfiles \- set SELinux file security contexts.
 .RB [ \-I | \-D ]
 .RB [ \-T
 .IR nthreads ]
+.RB [ \-A ]
 .I spec_file
 .IR pathname \ ...
 
@@ -187,6 +188,9 @@ use up to
 threads.  Specify 0 to create as many threads as there are available
 CPU cores; 1 to use only a single thread (default); or any positive
 number to use the given number of threads (if possible).
+.TP
+.B \-A
+do not track conflicting inodes (saves memory)
 
 .SH "ARGUMENTS"
 .TP
policycoreutils/setfiles/setfiles.c +4 -2
index ad09f840..40f2e7fe 100644
@@ -147,7 +147,7 @@ int main(int argc, char **argv)
        const char *base;
        int errors = 0;
        const char *ropts = "e:f:hiIDlmno:pqrsvFURW0xT:";
-       const char *sopts = "c:de:f:hiIDlmno:pqr:svCEFUR:W0T:";
+       const char *sopts = "c:de:f:hiIDlmno:pqr:svCEFUR:W0T:A";
        const char *opts;
        union selinux_callback cb;
        long unsigned skipped_errors;
@@ -375,6 +375,9 @@ int main(int argc, char **argv)
                        if (*optarg == '\0' || *endptr != '\0')
                                usage(argv[0]);
                        break;
+               case 'A':
+                       r_opts.add_assoc = 0;
+                       break;
                case 'h':
                case '?':
                        usage(argv[0]);
--

2.50.1

#3 lore raw
Quoted 16 lines · Stephen Smalley
Quoted 15 lines · Richard W.M. Jones

SELINUX_RESTORECON_ADD_ASSOC tracks conflicts between inodes that have differing contexts. However doing this involves building a large internal hashtable that stores the full path of every file examined by setfiles. For filesystems that have very large numbers of files or long pathnames, this uses a lot of memory, which makes SELinux relabelling in constrained memory environments infeasible.

This adds a new setfiles -A option that disables this tracking.

For example, using setfiles to relabel a filesystem with 15 million files took 3.7GB of RAM. Using this option, the same filesystem can be relabelled in 121MB (albeit with no warnings or errors possible for conflicting labels, but for our use case we don't care about that.)

Quoted 5 lines
Quoted 2 lines

I don't think we usually include downstream issue tracker links in our upstream commit messages, but no need to re-submit just for that.

FTR in git log there are already about 60 references to bugzilla.redhat.com and issues.redhat.com is replacement of bugzilla.redhat.com for issues reported on RHEL or CentOS. There are also bugs.debian.org, bugs.gentoo.org, bugs.chromium.org references.

Until these issue trackers disappear, it could be a good source for another context or related discussions.

Quoted 56 lines
Quoted 54 lines
---
 policycoreutils/setfiles/setfiles.8 | 4 ++++
 policycoreutils/setfiles/setfiles.c | 5 ++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
policycoreutils/setfiles/setfiles.8 +4 -0
index eabf0a1c..7c9c5d39 100644
@@ -23,6 +23,7 @@ setfiles \- set SELinux file security contexts.
 .RB [ \-I | \-D ]
 .RB [ \-T
 .IR nthreads ]
+.RB [ \-A ]
 .I spec_file
 .IR pathname \ ...
 
@@ -187,6 +188,9 @@ use up to
 threads.  Specify 0 to create as many threads as there are available
 CPU cores; 1 to use only a single thread (default); or any positive
 number to use the given number of threads (if possible).
+.TP
+.B \-A
+do not track conflicting inodes (saves memory)
 
 .SH "ARGUMENTS"
 .TP
policycoreutils/setfiles/setfiles.c +4 -2
index ad09f840..40f2e7fe 100644
@@ -147,7 +147,7 @@ int main(int argc, char **argv)
        const char *base;
        int errors = 0;
        const char *ropts = "e:f:hiIDlmno:pqrsvFURW0xT:";
-       const char *sopts = "c:de:f:hiIDlmno:pqr:svCEFUR:W0T:";
+       const char *sopts = "c:de:f:hiIDlmno:pqr:svCEFUR:W0T:A";
        const char *opts;
        union selinux_callback cb;
        long unsigned skipped_errors;
@@ -375,6 +375,9 @@ int main(int argc, char **argv)
                        if (*optarg == '\0' || *endptr != '\0')
                                usage(argv[0]);
                        break;
+               case 'A':
+                       r_opts.add_assoc = 0;
+                       break;
                case 'h':
                case '?':
                        usage(argv[0]);
--

2.50.1

maintainer
#4 lore raw
Quoted 25 lines · Petr Lautrbach
Quoted 16 lines · Stephen Smalley
Quoted 15 lines · Richard W.M. Jones

SELINUX_RESTORECON_ADD_ASSOC tracks conflicts between inodes that have differing contexts. However doing this involves building a large internal hashtable that stores the full path of every file examined by setfiles. For filesystems that have very large numbers of files or long pathnames, this uses a lot of memory, which makes SELinux relabelling in constrained memory environments infeasible.

This adds a new setfiles -A option that disables this tracking.

For example, using setfiles to relabel a filesystem with 15 million files took 3.7GB of RAM. Using this option, the same filesystem can be relabelled in 121MB (albeit with no warnings or errors possible for conflicting labels, but for our use case we don't care about that.)

Quoted 5 lines
Quoted 2 lines

I don't think we usually include downstream issue tracker links in our upstream commit messages, but no need to re-submit just for that.

Quoted 8 lines

FTR in git log there are already about 60 references to bugzilla.redhat.com and issues.redhat.com is replacement of bugzilla.redhat.com for issues reported on RHEL or CentOS. There are also bugs.debian.org, bugs.gentoo.org, bugs.chromium.org references.

Until these issue trackers disappear, it could be a good source for another context or related discussions.

Ok, thanks for clarifying. I know that tends to be frowned upon in the kernel but wasn't sure what current userspace practice is. My only request then is that such issues be publicly accessible if they are going to be referenced upstream.
But I won't die on that hill if others disagree.

Quoted 58 lines
Quoted 57 lines
Quoted 54 lines
---
 policycoreutils/setfiles/setfiles.8 | 4 ++++
 policycoreutils/setfiles/setfiles.c | 5 ++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
policycoreutils/setfiles/setfiles.8 +4 -0
index eabf0a1c..7c9c5d39 100644
@@ -23,6 +23,7 @@ setfiles \- set SELinux file security contexts.
 .RB [ \-I | \-D ]
 .RB [ \-T
 .IR nthreads ]
+.RB [ \-A ]
 .I spec_file
 .IR pathname \ ...
 
@@ -187,6 +188,9 @@ use up to
 threads.  Specify 0 to create as many threads as there are available
 CPU cores; 1 to use only a single thread (default); or any positive
 number to use the given number of threads (if possible).
+.TP
+.B \-A
+do not track conflicting inodes (saves memory)
 
 .SH "ARGUMENTS"
 .TP
policycoreutils/setfiles/setfiles.c +4 -2
index ad09f840..40f2e7fe 100644
@@ -147,7 +147,7 @@ int main(int argc, char **argv)
        const char *base;
        int errors = 0;
        const char *ropts = "e:f:hiIDlmno:pqrsvFURW0xT:";
-       const char *sopts = "c:de:f:hiIDlmno:pqr:svCEFUR:W0T:";
+       const char *sopts = "c:de:f:hiIDlmno:pqr:svCEFUR:W0T:A";
        const char *opts;
        union selinux_callback cb;
        long unsigned skipped_errors;
@@ -375,6 +375,9 @@ int main(int argc, char **argv)
                        if (*optarg == '\0' || *endptr != '\0')
                                usage(argv[0]);
                        break;
+               case 'A':
+                       r_opts.add_assoc = 0;
+                       break;
                case 'h':
                case '?':
                        usage(argv[0]);
--

2.50.1

#5 lore raw
In reply to Petr Lautrbach

BTW we seem to have "forked" the reviews. I made an update to the pull request, as requested by a reviewer there:

https://github.com/SELinuxProject/selinux/pull/490

I can post a v2 on the list if required.

Rich.

Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW
maintainer
#6 lore raw
Quoted 8 lines · Richard W.M. Jones

BTW we seem to have "forked" the reviews. I made an update to the pull request, as requested by a reviewer there:

https://github.com/SELinuxProject/selinux/pull/490

I can post a v2 on the list if required.

Yes please. The actual SELinux maintainers only review and merge patches on the list.

Quoted 10 lines

Rich.

--
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW