selinux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] libselinux: Ignore files removed during relabeling
@ 2025-10-16 18:08 Vit Mojzis
  2025-10-16 18:24 ` Rahul Sandhu
  2025-10-16 19:38 ` Stephen Smalley
  0 siblings, 2 replies; 6+ messages in thread
From: Vit Mojzis @ 2025-10-16 18:08 UTC (permalink / raw)
  To: selinux

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

Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
---
 libselinux/src/selinux_restorecon.c | 30 ++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/libselinux/src/selinux_restorecon.c b/libselinux/src/selinux_restorecon.c
index 39eabeb9..7cad4c8d 100644
--- a/libselinux/src/selinux_restorecon.c
+++ b/libselinux/src/selinux_restorecon.c
@@ -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;
-- 
2.51.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-10-21 13:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-16 18:08 [PATCH] libselinux: Ignore files removed during relabeling Vit Mojzis
2025-10-16 18:24 ` Rahul Sandhu
2025-10-16 19:38 ` Stephen Smalley
2025-10-17 15:08   ` [PATCH v2] " Vit Mojzis
2025-10-20 12:46     ` Stephen Smalley
2025-10-21 13:46       ` Stephen Smalley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).