Skip to contentSkip to conversation

libselinux: Ignore files removed during relabeling

PATCH Superseded Vit Mojzis 3 participants 6 messages Oct 16, 2025 – Oct 21, 2025 SELinux security module

lore
Share & tools
lore

Vit Mojzis

author
#0 lore raw

PATCH 1/1 libselinux: Ignore files removed during relabeling

In case ignore_noent is specified, ignore files removed during relabeling (race condition between folder read, file read and label set).

---
 libselinux/src/selinux_restorecon.c | 30 ++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)
libselinux/src/selinux_restorecon.c +25 -5
index 39eabeb9..7cad4c8d 100644
@@ -726,6 +726,14 @@ static int restorecon_sb(const char *pathname, const struct stat *sb,
 			    pathname, newcon);
 
 	if (lgetfilecon_raw(pathname, &curcon) < 0) {
+		/* Ignore files removed during relabeling if ignore_noent is set */
+		if (flags->ignore_noent && errno == ENOENT){
+			if (flags->verbose)
+				selinux_log(SELINUX_INFO,
+					    "%s removed during relabeling\n",
+					    pathname);
+			goto out;
+		}
 		if (errno != ENODATA)
 			goto err;
 
@@ -765,8 +773,19 @@ static int restorecon_sb(const char *pathname, const struct stat *sb,
 		}
 
 		if (!flags->nochange) {
-			if (lsetfilecon(pathname, newcon) < 0)
-				goto err;
+			if (lsetfilecon(pathname, newcon) < 0){
+				/* Ignore files removed during relabeling if ignore_noent is set */
+				if (flags->ignore_noent && errno == ENOENT){
+					if (flags->verbose)
+						selinux_log(SELINUX_INFO,
+							    "%s removed during relabeling\n",
+							    pathname);
+					goto out;
+				}
+				else
+					goto err;
+			}
+
 			updated = true;
 		}
 
@@ -932,9 +951,10 @@ loop_body:
 		case FTS_NS:
 			error = errno;
 			errno = ftsent->fts_errno;
-			selinux_log(SELINUX_ERROR,
-				    "Could not stat %s: %m.\n",
-				    ftsent->fts_path);
+			if (!state->flags.ignore_noent || errno != ENOENT)
+				selinux_log(SELINUX_ERROR,
+					    "Could not stat %s: %m.\n",
+					    ftsent->fts_path);
 			errno = error;
 			fts_set(fts, ftsent, FTS_SKIP);
 			continue;
Signature · 1 line
2.51.0
#1 lore raw
Quoted 14 lines · Vit Mojzis

In case ignore_noent is specified, ignore files removed during relabeling (race condition between folder read, file read and label set).

---
 libselinux/src/selinux_restorecon.c | 30 ++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)
libselinux/src/selinux_restorecon.c +0 -0
index 39eabeb9..7cad4c8d 100644

libselinux/src/selinux_restorecon.c static int restorecon_sb(const char *pathname, const struct stat *sb, · line 730

Quoted 5 lines
libselinux/src/selinux_restorecon.c +2 -0
@@ -726,6 +726,14 @@ static int restorecon_sb(const char *pathname, const struct stat *sb,
 			    pathname, newcon);
 
 	if (lgetfilecon_raw(pathname, &curcon) < 0) {
+		/* Ignore files removed during relabeling if ignore_noent is set */
+		if (flags->ignore_noent && errno == ENOENT){

Nit: there should be a space after the ( and before the {, inline with the rest of the code style.

Quoted 10 lines
libselinux/src/selinux_restorecon.c +6 -0
+			if (flags->verbose)
+				selinux_log(SELINUX_INFO,
+					    "%s removed during relabeling\n",
+					    pathname);
+			goto out;
+		}
 		if (errno != ENODATA)
 			goto err;
 

libselinux/src/selinux_restorecon.c static int restorecon_sb(const char *pathname, const struct stat *sb, · line 776

Quoted 6 lines
libselinux/src/selinux_restorecon.c +1 -2
@@ -765,8 +773,19 @@ static int restorecon_sb(const char *pathname, const struct stat *sb,
 		}
 
 		if (!flags->nochange) {
-			if (lsetfilecon(pathname, newcon) < 0)
-				goto err;
+			if (lsetfilecon(pathname, newcon) < 0){

Same as above.

libselinux/src/selinux_restorecon.c static int restorecon_sb(const char *pathname, const struct stat *sb, · line 778

Quoted 2 lines
libselinux/src/selinux_restorecon.c +2 -0
+				/* Ignore files removed during relabeling if ignore_noent is set */
+				if (flags->ignore_noent && errno == ENOENT){

And here too.

Quoted 27 lines
libselinux/src/selinux_restorecon.c +14 -3
+					if (flags->verbose)
+						selinux_log(SELINUX_INFO,
+							    "%s removed during relabeling\n",
+							    pathname);
+					goto out;
+				}
+				else
+					goto err;
+			}
+
 			updated = true;
 		}
 
@@ -932,9 +951,10 @@ loop_body:
 		case FTS_NS:
 			error = errno;
 			errno = ftsent->fts_errno;
-			selinux_log(SELINUX_ERROR,
-				    "Could not stat %s: %m.\n",
-				    ftsent->fts_path);
+			if (!state->flags.ignore_noent || errno != ENOENT)
+				selinux_log(SELINUX_ERROR,
+					    "Could not stat %s: %m.\n",
+					    ftsent->fts_path);
 			errno = error;
 			fts_set(fts, ftsent, FTS_SKIP);
 			continue;
maintainer
#2 lore raw
In reply to Vit Mojzis
Quoted 15 lines · Vit Mojzis

In case ignore_noent is specified, ignore files removed during relabeling (race condition between folder read, file read and label set).

---
 libselinux/src/selinux_restorecon.c | 30 ++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)
libselinux/src/selinux_restorecon.c +0 -0
index 39eabeb9..7cad4c8d 100644

libselinux/src/selinux_restorecon.c static int restorecon_sb(const char *pathname, const struct stat *sb, · line 734

Quoted 9 lines
libselinux/src/selinux_restorecon.c +6 -0
@@ -726,6 +726,14 @@ static int restorecon_sb(const char *pathname, const struct stat *sb,
                            pathname, newcon);
 
        if (lgetfilecon_raw(pathname, &curcon) < 0) {
+               /* Ignore files removed during relabeling if ignore_noent is set */
+               if (flags->ignore_noent && errno == ENOENT){
+                       if (flags->verbose)
+                               selinux_log(SELINUX_INFO,
+                                           "%s removed during relabeling\n",
+                                           pathname);

I wouldn't bother with these log messages.

Quoted 8 lines
libselinux/src/selinux_restorecon.c +2 -0
+                       goto out;
+               }
                if (errno != ENODATA)
                        goto err;
 
@@ -765,8 +773,19 @@ static int restorecon_sb(const char *pathname, const struct stat *sb,
                }
 

libselinux/src/selinux_restorecon.c static int restorecon_sb(const char *pathname, const struct stat *sb, · line 782

Quoted 10 lines
libselinux/src/selinux_restorecon.c +7 -2
                if (!flags->nochange) {
-                       if (lsetfilecon(pathname, newcon) < 0)
-                               goto err;
+                       if (lsetfilecon(pathname, newcon) < 0){
+                               /* Ignore files removed during relabeling if ignore_noent is set */
+                               if (flags->ignore_noent && errno == ENOENT){
+                                       if (flags->verbose)
+                                               selinux_log(SELINUX_INFO,
+                                                           "%s removed during relabeling\n",
+                                                           pathname);

Ditto.

Quoted 27 lines
libselinux/src/selinux_restorecon.c +10 -4
+                                       goto out;
+                               }
+                               else
+                                       goto err;
+                       }
+
                        updated = true;
                }
 
@@ -932,9 +951,10 @@ loop_body:
                case FTS_NS:
                        error = errno;
                        errno = ftsent->fts_errno;
-                       selinux_log(SELINUX_ERROR,
-                                   "Could not stat %s: %m.\n",
-                                   ftsent->fts_path);
+                       if (!state->flags.ignore_noent || errno != ENOENT)
+                               selinux_log(SELINUX_ERROR,
+                                           "Could not stat %s: %m.\n",
+                                           ftsent->fts_path);
                        errno = error;
                        fts_set(fts, ftsent, FTS_SKIP);
                        continue;
--

2.51.0

author
#3 lore raw

PATCH v2 1/1 libselinux: Ignore files removed during relabeling

In case ignore_noent is specified, ignore files removed during relabeling (race condition between folder read, file read and label set).

---
 libselinux/src/selinux_restorecon.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
libselinux/src/selinux_restorecon.c +15 -5
index 39eabeb9..681c69db 100644
@@ -726,6 +726,9 @@ static int restorecon_sb(const char *pathname, const struct stat *sb,
 			    pathname, newcon);
 
 	if (lgetfilecon_raw(pathname, &curcon) < 0) {
+		/* Ignore files removed during relabeling if ignore_noent is set */
+		if (flags->ignore_noent && errno == ENOENT)
+			goto out;
 		if (errno != ENODATA)
 			goto err;
 
@@ -765,8 +768,14 @@ static int restorecon_sb(const char *pathname, const struct stat *sb,
 		}
 
 		if (!flags->nochange) {
-			if (lsetfilecon(pathname, newcon) < 0)
-				goto err;
+			if (lsetfilecon(pathname, newcon) < 0) {
+				/* Ignore files removed during relabeling if ignore_noent is set */
+				if (flags->ignore_noent && errno == ENOENT)
+					goto out;
+				else
+					goto err;
+			}
+
 			updated = true;
 		}
 
@@ -932,9 +941,10 @@ loop_body:
 		case FTS_NS:
 			error = errno;
 			errno = ftsent->fts_errno;
-			selinux_log(SELINUX_ERROR,
-				    "Could not stat %s: %m.\n",
-				    ftsent->fts_path);
+			if (!state->flags.ignore_noent || errno != ENOENT)
+				selinux_log(SELINUX_ERROR,
+					    "Could not stat %s: %m.\n",
+					    ftsent->fts_path);
 			errno = error;
 			fts_set(fts, ftsent, FTS_SKIP);
 			continue;
Signature · 1 line
2.51.0
maintainer Acked-by on v2 1/1
#4 lore raw
Quoted 6 lines · Vit Mojzis

In case ignore_noent is specified, ignore files removed during relabeling (race condition between folder read, file read and label set).

Quoted 53 lines
---
 libselinux/src/selinux_restorecon.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
libselinux/src/selinux_restorecon.c +15 -6
index 39eabeb9..681c69db 100644
@@ -726,6 +726,9 @@ static int restorecon_sb(const char *pathname, const struct stat *sb,
                            pathname, newcon);
 
        if (lgetfilecon_raw(pathname, &curcon) < 0) {
+               /* Ignore files removed during relabeling if ignore_noent is set */
+               if (flags->ignore_noent && errno == ENOENT)
+                       goto out;
                if (errno != ENODATA)
                        goto err;
 
@@ -765,8 +768,14 @@ static int restorecon_sb(const char *pathname, const struct stat *sb,
                }
 
                if (!flags->nochange) {
-                       if (lsetfilecon(pathname, newcon) < 0)
-                               goto err;
+                       if (lsetfilecon(pathname, newcon) < 0) {
+                               /* Ignore files removed during relabeling if ignore_noent is set */
+                               if (flags->ignore_noent && errno == ENOENT)
+                                       goto out;
+                               else
+                                       goto err;
+                       }
+
                        updated = true;
                }
 
@@ -932,9 +941,10 @@ loop_body:
                case FTS_NS:
                        error = errno;
                        errno = ftsent->fts_errno;
-                       selinux_log(SELINUX_ERROR,
-                                   "Could not stat %s: %m.\n",
-                                   ftsent->fts_path);
+                       if (!state->flags.ignore_noent || errno != ENOENT)
+                               selinux_log(SELINUX_ERROR,
+                                           "Could not stat %s: %m.\n",
+                                           ftsent->fts_path);
                        errno = error;
                        fts_set(fts, ftsent, FTS_SKIP);
                        continue;
--

2.51.0