Skip to contentSkip to conversation

selinux_restorecon: Do not exit on directory cycles

PATCH In review Johannes Segitz 2 participants 5 messages Jul 28 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 selinux_restorecon: Do not exit on directory cycles

All other failures are handled gracefully. Directory cycles can e.g. happen on BTRFS filesystems with subvolumes. Skip them and continue instead of exiting

---
 libselinux/src/selinux_restorecon.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
libselinux/src/selinux_restorecon.c +3 -5
index 30f1b836..4233ba65 100644
@@ -1393,15 +1393,13 @@ static void *selinux_restorecon_thread(void *arg)
 		if (is_dir) {
 			if (descend && walk_is_cycle(state, ent_sb.st_dev,
 						     ent_sb.st_ino)) {
-				selinux_log(SELINUX_ERROR,
+				selinux_log(SELINUX_WARNING,
 					    "Directory cycle on %s.\n",
 					    ent_path);
 				close(ent_fd);
 				close(rd_fd);
-				errno = ELOOP;
-				state->error = -1;
-				state->abort = true;
-				goto finish;
+				prune_pathbuf(state);
+				continue;
 			}
 
 			if (state->sfsb.f_type == SYSFS_MAGIC &&
Signature · 1 line
2.55.0
#1 lore raw

PATCH v4 2/2 man: Document directory cycle handling of selinux_restorecon

Skipping directory cycles instead of aborting the file tree walk is a user visible behavior change: restorecon -R over a tree containing e.g. a bind mount of an ancestor directory now logs a warning, skips the cycle and completes successfully, instead of failing with ELOOP.

Document the new behavior for the flag SELINUX_RESTORECON_RECURSE, including that a cycle is not an error and therefore is neither affected by SELINUX_RESTORECON_ABORT_ON_ERROR nor counted by SELINUX_RESTORECON_COUNT_ERRORS, and note the previous behavior in the selinux_restorecon(3), restorecon(8) and setfiles(8) man pages.

---
 libselinux/man/man3/selinux_restorecon.3 | 28 ++++++++++++++++++++++++
 policycoreutils/setfiles/restorecon.8    | 20 +++++++++++++++++
 policycoreutils/setfiles/setfiles.8      | 14 ++++++++++++
 3 files changed, 62 insertions(+)
libselinux/man/man3/selinux_restorecon.3 +28 -0
index 9bcd3d42..bafa86a2 100644
@@ -99,6 +99,21 @@ and if successful write an SHA1 digest of the specfile entries to an
 extended attribute as described in the
 .B NOTES
 section.
+.RS
+Note that directory cycles encountered while descending, for example a bind
+mount of an ancestor directory or nested BTRFS subvolumes, are not followed.
+A warning is logged, the offending directory is not descended into and the
+file tree walk continues with the remaining entries. Such cycles are not
+treated as errors, therefore they neither abort the file tree walk when
+.B SELINUX_RESTORECON_ABORT_ON_ERROR
+is set, nor are they included in the count returned by
+.BR selinux_restorecon_get_skipped_errors (3)
+when
+.B SELINUX_RESTORECON_COUNT_ERRORS
+is set. See the
+.B NOTES
+section for details of the previous behavior.
+.RE
 .sp
 .B SELINUX_RESTORECON_VERBOSE
 log file label changes.
@@ -302,6 +317,19 @@ with the
 .B SELINUX_CB_LOG
 .I type
 option.
+.IP "6." 4
+Up to and including libselinux 3.11 a directory cycle was fatal:
+.BR selinux_restorecon ()
+logged an error, stopped the file tree walk and returned \-1 with
+.I errno
+set to
+.BR ELOOP ,
+regardless of the
+.B SELINUX_RESTORECON_ABORT_ON_ERROR
+and
+.B SELINUX_RESTORECON_COUNT_ERRORS
+flags. Since then the cycle is skipped as described above, so a file tree
+walk over a directory tree containing a cycle can complete successfully.
 .
 .SH "SEE ALSO"
 .BR selabel_get_digests_all_partial_matches (3),
policycoreutils/setfiles/restorecon.8 +20 -0
index 982701c2..443f29ab 100644
@@ -145,6 +145,9 @@ options are mutually exclusive.
 .TP
 .B \-R, \-r
 change files and directories file labels recursively (descend directories).
+Directory cycles are skipped, see the
+.B NOTES
+section for details.
 .br
 .TP
 .B \-v
@@ -240,6 +243,23 @@ and provided the
 .B \-n
 option is NOT set and recursive mode is set, files will be relabeled as
 required with the digests then being updated provided there are no errors.
+.IP "4." 4
+When descending directories (i.e. the
+.B \-R
+or
+.B \-r
+option is set)
+.B restorecon
+detects directory cycles, for example a bind mount of an ancestor directory or
+nested BTRFS subvolumes. A cycle is reported as a warning and the offending
+directory is not descended into, the remaining entries are still relabeled.
+A directory cycle is not counted as a relabeling error, so it does not affect
+the exit status and does not stop the file tree walk. Up to and including
+version 3.11 a directory cycle was instead treated as a fatal
+.B ELOOP
+error, terminating
+.B restorecon
+with a non-zero exit status.
 
 .SH EXAMPLE
 .nf
policycoreutils/setfiles/setfiles.8 +14 -0
index d55b42db..17cabe37 100644
@@ -307,6 +307,20 @@ and provided the
 .B \-n
 option is NOT set, files will be relabeled as required with the digests then
 being updated provided there are no errors.
+.IP "4." 4
+.B setfiles
+detects directory cycles while descending directories, for example a bind
+mount of an ancestor directory or nested BTRFS subvolumes. A cycle is reported
+as a warning and the offending directory is not descended into, the remaining
+entries are still relabeled. A directory cycle is not counted as a relabeling
+error, so it does not affect the exit status and does not stop the file tree
+walk. Up to and including version 3.11 a directory cycle was instead treated
+as a fatal
+.B ELOOP
+error, terminating
+.B setfiles
+with exit status
+.BR 255 .
 
 .SH EXAMPLE
 .nf
Signature · 1 line
2.55.0
maintainer Acked-by on v4 2/2
#2 lore raw

Re: [PATCH v4 2/2] man: Document directory cycle handling of selinux_restorecon

Quoted 6 lines · Johannes Segitz

Skipping directory cycles instead of aborting the file tree walk is a user visible behavior change: restorecon -R over a tree containing e.g. a bind mount of an ancestor directory now logs a warning, skips the cycle and completes successfully, instead of failing with ELOOP.

Quoted 7 lines

Document the new behavior for the flag SELINUX_RESTORECON_RECURSE, including that a cycle is not an error and therefore is neither affected by SELINUX_RESTORECON_ABORT_ON_ERROR nor counted by SELINUX_RESTORECON_COUNT_ERRORS, and note the previous behavior in the selinux_restorecon(3), restorecon(8) and setfiles(8) man pages.

Thank you for doing this!

Quoted 119 lines
---
 libselinux/man/man3/selinux_restorecon.3 | 28 ++++++++++++++++++++++++
 policycoreutils/setfiles/restorecon.8    | 20 +++++++++++++++++
 policycoreutils/setfiles/setfiles.8      | 14 ++++++++++++
 3 files changed, 62 insertions(+)
libselinux/man/man3/selinux_restorecon.3 +28 -0
index 9bcd3d42..bafa86a2 100644
@@ -99,6 +99,21 @@ and if successful write an SHA1 digest of the specfile entries to an
 extended attribute as described in the
 .B NOTES
 section.
+.RS
+Note that directory cycles encountered while descending, for example a bind
+mount of an ancestor directory or nested BTRFS subvolumes, are not followed.
+A warning is logged, the offending directory is not descended into and the
+file tree walk continues with the remaining entries. Such cycles are not
+treated as errors, therefore they neither abort the file tree walk when
+.B SELINUX_RESTORECON_ABORT_ON_ERROR
+is set, nor are they included in the count returned by
+.BR selinux_restorecon_get_skipped_errors (3)
+when
+.B SELINUX_RESTORECON_COUNT_ERRORS
+is set. See the
+.B NOTES
+section for details of the previous behavior.
+.RE
 .sp
 .B SELINUX_RESTORECON_VERBOSE
 log file label changes.
@@ -302,6 +317,19 @@ with the
 .B SELINUX_CB_LOG
 .I type
 option.
+.IP "6." 4
+Up to and including libselinux 3.11 a directory cycle was fatal:
+.BR selinux_restorecon ()
+logged an error, stopped the file tree walk and returned \-1 with
+.I errno
+set to
+.BR ELOOP ,
+regardless of the
+.B SELINUX_RESTORECON_ABORT_ON_ERROR
+and
+.B SELINUX_RESTORECON_COUNT_ERRORS
+flags. Since then the cycle is skipped as described above, so a file tree
+walk over a directory tree containing a cycle can complete successfully.
 .
 .SH "SEE ALSO"
 .BR selabel_get_digests_all_partial_matches (3),
policycoreutils/setfiles/restorecon.8 +20 -0
index 982701c2..443f29ab 100644
@@ -145,6 +145,9 @@ options are mutually exclusive.
 .TP
 .B \-R, \-r
 change files and directories file labels recursively (descend directories).
+Directory cycles are skipped, see the
+.B NOTES
+section for details.
 .br
 .TP
 .B \-v
@@ -240,6 +243,23 @@ and provided the
 .B \-n
 option is NOT set and recursive mode is set, files will be relabeled as
 required with the digests then being updated provided there are no errors.
+.IP "4." 4
+When descending directories (i.e. the
+.B \-R
+or
+.B \-r
+option is set)
+.B restorecon
+detects directory cycles, for example a bind mount of an ancestor directory or
+nested BTRFS subvolumes. A cycle is reported as a warning and the offending
+directory is not descended into, the remaining entries are still relabeled.
+A directory cycle is not counted as a relabeling error, so it does not affect
+the exit status and does not stop the file tree walk. Up to and including
+version 3.11 a directory cycle was instead treated as a fatal
+.B ELOOP
+error, terminating
+.B restorecon
+with a non-zero exit status.
 
 .SH EXAMPLE
 .nf
policycoreutils/setfiles/setfiles.8 +14 -1
index d55b42db..17cabe37 100644
@@ -307,6 +307,20 @@ and provided the
 .B \-n
 option is NOT set, files will be relabeled as required with the digests then
 being updated provided there are no errors.
+.IP "4." 4
+.B setfiles
+detects directory cycles while descending directories, for example a bind
+mount of an ancestor directory or nested BTRFS subvolumes. A cycle is reported
+as a warning and the offending directory is not descended into, the remaining
+entries are still relabeled. A directory cycle is not counted as a relabeling
+error, so it does not affect the exit status and does not stop the file tree
+walk. Up to and including version 3.11 a directory cycle was instead treated
+as a fatal
+.B ELOOP
+error, terminating
+.B setfiles
+with exit status
+.BR 255 .
 
 .SH EXAMPLE
 .nf
--

2.55.0

maintainer
#3 lore raw

Re: [PATCH v4 2/2] man: Document directory cycle handling of selinux_restorecon

Quoted 14 lines · Stephen Smalley
Quoted 12 lines · Johannes Segitz

Skipping directory cycles instead of aborting the file tree walk is a user visible behavior change: restorecon -R over a tree containing e.g. a bind mount of an ancestor directory now logs a warning, skips the cycle and completes successfully, instead of failing with ELOOP.

Document the new behavior for the flag SELINUX_RESTORECON_RECURSE, including that a cycle is not an error and therefore is neither affected by SELINUX_RESTORECON_ABORT_ON_ERROR nor counted by SELINUX_RESTORECON_COUNT_ERRORS, and note the previous behavior in the selinux_restorecon(3), restorecon(8) and setfiles(8) man pages.

Quoted 5 lines
Quoted 1 line

Thank you for doing this!

This series is merged.

maintainer Acked-by on v4 1/2
#4 lore raw
In reply to Johannes Segitz
Quoted 6 lines · Johannes Segitz

All other failures are handled gracefully. Directory cycles can e.g. happen on BTRFS filesystems with subvolumes. Skip them and continue instead of exiting

Quoted 31 lines
---
 libselinux/src/selinux_restorecon.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
libselinux/src/selinux_restorecon.c +3 -6
index 30f1b836..4233ba65 100644
@@ -1393,15 +1393,13 @@ static void *selinux_restorecon_thread(void *arg)
                if (is_dir) {
                        if (descend && walk_is_cycle(state, ent_sb.st_dev,
                                                     ent_sb.st_ino)) {
-                               selinux_log(SELINUX_ERROR,
+                               selinux_log(SELINUX_WARNING,
                                            "Directory cycle on %s.\n",
                                            ent_path);
                                close(ent_fd);
                                close(rd_fd);
-                               errno = ELOOP;
-                               state->error = -1;
-                               state->abort = true;
-                               goto finish;
+                               prune_pathbuf(state);
+                               continue;
                        }
 
                        if (state->sfsb.f_type == SYSFS_MAGIC &&
--

2.55.0