Skip to contentSkip to conversation

selinux_restorecon: Do not exit on directory cycles

PATCH Superseded Johannes Segitz 2 participants 6 messages Jul 23 – Jul 24 SELinux security module

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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 | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
libselinux/src/selinux_restorecon.c +5 -6
index 30f1b836..fdefc92c 100644
@@ -1393,15 +1393,14 @@ 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;
+                               if (rd_fd >= 0)
+                                       close(rd_fd);
+                               prune_pathbuf(state);
+                               continue;
                        }
 
                        if (state->sfsb.f_type == SYSFS_MAGIC &&

--
2.55.0

maintainer
#1 lore raw
Quoted 4 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

This is a user-visible behavior change so we should note it; restorecon -R on a tree with a bind mount of an ancestor will now complete with rc 0 and only a WARNING rather than aborting with ELOOP.

Missing Signed-off-by line and the patch still doesn't apply for me (did you send it using git send-email and against current selinux/main?) but also see comment below on the code.

Quoted 14 lines
---
 libselinux/src/selinux_restorecon.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
libselinux/src/selinux_restorecon.c +1 -1
index 30f1b836..fdefc92c 100644
@@ -1393,15 +1393,14 @@ 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,

libselinux/src/selinux_restorecon.c static void *selinux_restorecon_thread(void *arg) · line 1401

Quoted 10 lines
libselinux/src/selinux_restorecon.c +2 -5
                                            "Directory cycle on %s.\n",
                                            ent_path);
                                close(ent_fd);
-                               close(rd_fd);
-                               errno = ELOOP;
-                               state->error = -1;
-                               state->abort = true;
-                               goto finish;
+                               if (rd_fd >= 0)
+                                       close(rd_fd);

You don't need the if (rd_fd >= 0) guard here because this branch is only executed if descend == true so that's always true.

Quoted 7 lines
libselinux/src/selinux_restorecon.c +2 -1
+                               prune_pathbuf(state);
+                               continue;
                        }
 
                        if (state->sfsb.f_type == SYSFS_MAGIC &&
--

2.55.0

#2 lore raw
Quoted 9 lines · Stephen Smalley
Quoted 4 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

This is a user-visible behavior change so we should note it; restorecon -R on a tree with a bind mount of an ancestor will now complete with rc 0 and only a WARNING rather than aborting with ELOOP.

Okay, I can write something up. But in general you would be fine with the behavior change?

Quoted 3 lines

Missing Signed-off-by line and the patch still doesn't apply for me (did you send it using git send-email and against current selinux/main?) but also see comment below on the code.

I start feel like an idiot. The first time I used this old repo with the wrong branch, but this time I made sure it applies, but didn't use git send-mail.

I did

# git format-patch -1 HEAD
0001-selinux_restorecon-Do-not-exit-on-directory-cycles.patch

which for me then applies:

# git checkout main
Already on 'main'
Your branch is up to date with 'origin/main'.
# git rev-parse --short HEAD
84d424b8
# git am 0001-selinux_restorecon-Do-not-exit-on-directory-cycles.patch
Applying: selinux_restorecon: Do not exit on directory cycles

Maybe my manual inclusion in the mail breaks it? I attach it now to the mail

Johannes

Signature · 4 lines
GPG Key                EE16 6BCE AD56 E034 BFB3  3ADD 7BF7 29D5 E7C8 1FA0
Subkey fingerprint:    250F 43F5 F7CE 6F1E 9C59  4F95 BC27 DD9D 2CC4 FD66
SUSE Software Solutions Germany GmbH, Frankenstr. 146, 90461 Nürnberg, Germany
www.suse.com, Geschäftsführer: Jochen Jaser, Andrew McDonald, Abhinav Puri, (HRB 36809, AG Nürnberg)

From 9155f98c8dec5b275ee0429cc19c9b42d8c3c8b5 Mon Sep 17 00:00:00 2001
From: Johannes Segitz <[email protected]>
Date: Thu, 23 Jul 2026 10:48:40 +0200
Subject: [PATCH] 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 | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
libselinux/src/selinux_restorecon.c +5 -6
index 30f1b836..fdefc92c 100644
@@ -1393,15 +1393,14 @@ 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;
+				if (rd_fd >= 0)
+					close(rd_fd);
+				prune_pathbuf(state);
+				continue;
 			}
 
 			if (state->sfsb.f_type == SYSFS_MAGIC &&
Signature · 1 line
2.55.0
maintainer
#3 lore raw
Quoted 8 lines · Johannes Segitz
Quoted 6 lines · Stephen Smalley
Quoted 4 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 6 lines
Quoted 3 lines

This is a user-visible behavior change so we should note it; restorecon -R on a tree with a bind mount of an ancestor will now complete with rc 0 and only a WARNING rather than aborting with ELOOP.

Okay, I can write something up. But in general you would be fine with the behavior change?

I am (although other maintainers are free to disagree).

Quoted 21 lines
Quoted 3 lines

Missing Signed-off-by line and the patch still doesn't apply for me (did you send it using git send-email and against current selinux/main?) but also see comment below on the code.

I start feel like an idiot. The first time I used this old repo with the wrong branch, but this time I made sure it applies, but didn't use git send-mail.

I did

# git format-patch -1 HEAD
0001-selinux_restorecon-Do-not-exit-on-directory-cycles.patch

which for me then applies:

# git checkout main
Already on 'main'
Your branch is up to date with 'origin/main'.
# git rev-parse --short HEAD
84d424b8
# git am 0001-selinux_restorecon-Do-not-exit-on-directory-cycles.patch
Applying: selinux_restorecon: Do not exit on directory cycles
Quoted 2 lines

Maybe my manual inclusion in the mail breaks it? I attach it now to the mail

Yes, unfortunately mail clients are unforgiving of inline patches these days so we recommend
using git send-email,
https://github.com/SELinuxProject/selinux/wiki/Contributing#contributing-code

We still prefer inline patches for ease of review and inline commenting.

#4 lore raw
Quoted 9 lines · Stephen Smalley
Quoted 8 lines · Johannes Segitz
Quoted 6 lines · Stephen Smalley
Quoted 4 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 8 lines
Quoted 6 lines
Quoted 3 lines

This is a user-visible behavior change so we should note it; restorecon -R on a tree with a bind mount of an ancestor will now complete with rc 0 and only a WARNING rather than aborting with ELOOP.

Okay, I can write something up. But in general you would be fine with the behavior change?

I am (although other maintainers are free to disagree).

okay, I'll prepare an updated version with wording that addresses this and
...

Quoted 21 lines
Quoted 21 lines
Quoted 3 lines

Missing Signed-off-by line and the patch still doesn't apply for me (did you send it using git send-email and against current selinux/main?) but also see comment below on the code.

I start feel like an idiot. The first time I used this old repo with the wrong branch, but this time I made sure it applies, but didn't use git send-mail.

I did

# git format-patch -1 HEAD
0001-selinux_restorecon-Do-not-exit-on-directory-cycles.patch

which for me then applies:

# git checkout main
Already on 'main'
Your branch is up to date with 'origin/main'.
# git rev-parse --short HEAD
84d424b8
# git am 0001-selinux_restorecon-Do-not-exit-on-directory-cycles.patch
Applying: selinux_restorecon: Do not exit on directory cycles
Quoted 7 lines
Quoted 2 lines

Maybe my manual inclusion in the mail breaks it? I attach it now to the mail

Yes, unfortunately mail clients are unforgiving of inline patches these days so we recommend
using git send-email,
https://github.com/SELinuxProject/selinux/wiki/Contributing#contributing-code

... will set this up. My mail setup is bit unusual, because of this I avoided it until now, but I agree that it makes reviewing easier

Johannes

Signature · 4 lines
GPG Key                EE16 6BCE AD56 E034 BFB3  3ADD 7BF7 29D5 E7C8 1FA0
Subkey fingerprint:    250F 43F5 F7CE 6F1E 9C59  4F95 BC27 DD9D 2CC4 FD66
SUSE Software Solutions Germany GmbH, Frankenstr. 146, 90461 Nürnberg, Germany
www.suse.com, Geschäftsführer: Jochen Jaser, Andrew McDonald, Abhinav Puri, (HRB 36809, AG Nürnberg)
maintainer
#5 lore raw
Quoted 47 lines · Johannes Segitz
Quoted 9 lines · Stephen Smalley
Quoted 8 lines · Johannes Segitz
Quoted 6 lines · Stephen Smalley
Quoted 4 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 8 lines
Quoted 6 lines
Quoted 3 lines

This is a user-visible behavior change so we should note it; restorecon -R on a tree with a bind mount of an ancestor will now complete with rc 0 and only a WARNING rather than aborting with ELOOP.

Okay, I can write something up. But in general you would be fine with the behavior change?

I am (although other maintainers are free to disagree).

okay, I'll prepare an updated version with wording that addresses this and
...

Quoted 24 lines
Quoted 23 lines
Quoted 3 lines

Missing Signed-off-by line and the patch still doesn't apply for me (did you send it using git send-email and against current selinux/main?) but also see comment below on the code.

I start feel like an idiot. The first time I used this old repo with the wrong branch, but this time I made sure it applies, but didn't use git send-mail.

I did

# git format-patch -1 HEAD
0001-selinux_restorecon-Do-not-exit-on-directory-cycles.patch

which for me then applies:

# git checkout main
Already on 'main'
Your branch is up to date with 'origin/main'.
# git rev-parse --short HEAD
84d424b8
# git am 0001-selinux_restorecon-Do-not-exit-on-directory-cycles.patch
Applying: selinux_restorecon: Do not exit on directory cycles

Maybe my manual inclusion in the mail breaks it? I attach it now to the mail

Quoted 7 lines
Quoted 4 lines

Yes, unfortunately mail clients are unforgiving of inline patches these days so we recommend
using git send-email,
https://github.com/SELinuxProject/selinux/wiki/Contributing#contributing-code

... will set this up. My mail setup is bit unusual, because of this I avoided it until now, but I agree that it makes reviewing easier

Also, if you could pass -v3 on your next iteration that would help b4 figure out which version is latest.