Skip to contentSkip to conversation

libselinux: reset scanned file count at selinux_restorecon(3) entry

PATCH In review Christian Göttsche 3 participants 10 messages May 20 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH v2 2/2 libselinux: reset scanned file count at selinux_restorecon(3) entry

From: Christian Göttsche <[email protected]>

Reset the scanned file count at startup in case the calling process calls selinux_restorecon(3) multiple times during its lifetime.

---
 libselinux/src/selinux_restorecon.c | 2 ++
 1 file changed, 2 insertions(+)
libselinux/src/selinux_restorecon.c +2 -0
index b4d58b05..b7defffb 100644
@@ -1446,6 +1446,8 @@ static int selinux_restorecon_common(const char *pathname_orig,
 	int error;
 	struct dir_hash_node *current = NULL;
 
+	fc_count = 0;
+
 	if (state.flags.verbose && state.flags.progress)
 		state.flags.verbose = false;
 
Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 1/2 libselinux: improve restorecon progress locking

From: Christian Göttsche <[email protected]>

Use an atomic counter to avoid acquiring the synchronizing mutex for each file. Only acquire when actually writing to stdout.

Suggested-by Copilot

---
v2: extract fc_count reset into separate commit

---
 libselinux/src/selinux_restorecon.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
libselinux/src/selinux_restorecon.c +11 -9
index 8b6eba0c..b4d58b05 100644
@@ -40,8 +40,6 @@
 #include "label_file.h"
 #include "sha1.h"
 
-#define STAR_COUNT 1024
-
 static struct selabel_handle *fc_sehandle = NULL;
 static bool selabel_no_digest;
 static char *rootpath = NULL;
@@ -734,19 +732,23 @@ static int restorecon_sb(int fd, const char *pathname, const struct stat *sb,
 	}
 
 	if (flags->progress) {
-		__pthread_mutex_lock(&progress_mutex);
-		fc_count++;
-		if (fc_count % STAR_COUNT == 0) {
+		const unsigned STAR_COUNT = 1024;
+		uint64_t fc_count_local;
+
+		fc_count_local = __atomic_add_fetch(&fc_count, 1, __ATOMIC_RELAXED);
+
+		if (fc_count_local % STAR_COUNT == 0) {
+			__pthread_mutex_lock(&progress_mutex);
 			if (flags->mass_relabel && efile_count > 0) {
-				float pc = (fc_count < efile_count) ? (100.0 *
-					     fc_count / efile_count) : 100;
+				float pc = (fc_count_local < efile_count) ? (100.0 *
+					     fc_count_local / efile_count) : 100;
 				fprintf(stdout, "\r%-.1f%%", (double)pc);
 			} else {
-				fprintf(stdout, "\r%" PRIu64 "k", fc_count / STAR_COUNT);
+				fprintf(stdout, "\r%" PRIu64 "k", fc_count_local / STAR_COUNT);
 			}
 			fflush(stdout);
+			__pthread_mutex_unlock(&progress_mutex);
 		}
-		__pthread_mutex_unlock(&progress_mutex);
 	}
 
 	if (flags->add_assoc) {
Signature · 1 line
2.53.0
maintainer
#2 lore raw

Re: [PATCH v2 1/2] libselinux: improve restorecon progress locking

Quoted 31 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Use an atomic counter to avoid acquiring the synchronizing mutex for each file. Only acquire when actually writing to stdout.

Suggested-by Copilot

---
v2: extract fc_count reset into separate commit

---
 libselinux/src/selinux_restorecon.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
libselinux/src/selinux_restorecon.c +0 -2
index 8b6eba0c..b4d58b05 100644
@@ -40,8 +40,6 @@
 #include "label_file.h"
 #include "sha1.h"
 
-#define STAR_COUNT 1024
-
 static struct selabel_handle *fc_sehandle = NULL;
 static bool selabel_no_digest;
 static char *rootpath = NULL;
@@ -734,19 +732,23 @@ static int restorecon_sb(int fd, const char *pathname, const struct stat *sb,
        }
 
        if (flags->progress) {

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

Quoted 10 lines
libselinux/src/selinux_restorecon.c +7 -3
-               __pthread_mutex_lock(&progress_mutex);
-               fc_count++;
-               if (fc_count % STAR_COUNT == 0) {
+               const unsigned STAR_COUNT = 1024;
+               uint64_t fc_count_local;
+
+               fc_count_local = __atomic_add_fetch(&fc_count, 1, __ATOMIC_RELAXED);
+
+               if (fc_count_local % STAR_COUNT == 0) {
+                       __pthread_mutex_lock(&progress_mutex);

Do we actually need to take a mutex at all here?

Quoted 21 lines
libselinux/src/selinux_restorecon.c +4 -5
                        if (flags->mass_relabel && efile_count > 0) {
-                               float pc = (fc_count < efile_count) ? (100.0 *
-                                            fc_count / efile_count) : 100;
+                               float pc = (fc_count_local < efile_count) ? (100.0 *
+                                            fc_count_local / efile_count) : 100;
                                fprintf(stdout, "\r%-.1f%%", (double)pc);
                        } else {
-                               fprintf(stdout, "\r%" PRIu64 "k", fc_count / STAR_COUNT);
+                               fprintf(stdout, "\r%" PRIu64 "k", fc_count_local / STAR_COUNT);
                        }
                        fflush(stdout);
+                       __pthread_mutex_unlock(&progress_mutex);
                }
-               __pthread_mutex_unlock(&progress_mutex);
        }
 
        if (flags->add_assoc) {
--

2.53.0

#3 lore raw

Re: [PATCH v2 1/2] libselinux: improve restorecon progress locking

Quoted 33 lines · Stephen Smalley
Quoted 30 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Use an atomic counter to avoid acquiring the synchronizing mutex for each file. Only acquire when actually writing to stdout.

Suggested-by Copilot

---
v2: extract fc_count reset into separate commit

---
 libselinux/src/selinux_restorecon.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
libselinux/src/selinux_restorecon.c +0 -2
index 8b6eba0c..b4d58b05 100644
@@ -40,8 +40,6 @@
 #include "label_file.h"
 #include "sha1.h"
 
-#define STAR_COUNT 1024
-
 static struct selabel_handle *fc_sehandle = NULL;
 static bool selabel_no_digest;
 static char *rootpath = NULL;
@@ -734,19 +732,23 @@ static int restorecon_sb(int fd, const char *pathname, const struct stat *sb,
        }
 
Quoted 13 lines

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

Quoted 11 lines
libselinux/src/selinux_restorecon.c +7 -3
        if (flags->progress) {
-               __pthread_mutex_lock(&progress_mutex);
-               fc_count++;
-               if (fc_count % STAR_COUNT == 0) {
+               const unsigned STAR_COUNT = 1024;
+               uint64_t fc_count_local;
+
+               fc_count_local = __atomic_add_fetch(&fc_count, 1, __ATOMIC_RELAXED);
+
+               if (fc_count_local % STAR_COUNT == 0) {
+                       __pthread_mutex_lock(&progress_mutex);

Do we actually need to take a mutex at all here?

writing to stdout concurrently can interleave output, which might break the line reset via \r

Quoted 22 lines
Quoted 22 lines
libselinux/src/selinux_restorecon.c +4 -5
                        if (flags->mass_relabel && efile_count > 0) {
-                               float pc = (fc_count < efile_count) ? (100.0 *
-                                            fc_count / efile_count) : 100;
+                               float pc = (fc_count_local < efile_count) ? (100.0 *
+                                            fc_count_local / efile_count) : 100;
                                fprintf(stdout, "\r%-.1f%%", (double)pc);
                        } else {
-                               fprintf(stdout, "\r%" PRIu64 "k", fc_count / STAR_COUNT);
+                               fprintf(stdout, "\r%" PRIu64 "k", fc_count_local / STAR_COUNT);
                        }
                        fflush(stdout);
+                       __pthread_mutex_unlock(&progress_mutex);
                }
-               __pthread_mutex_unlock(&progress_mutex);
        }
 
        if (flags->add_assoc) {
--

2.53.0

maintainer Acked-by on v2 1/2
#4 lore raw

Re: [PATCH v2 1/2] libselinux: improve restorecon progress locking

In reply to Christian Göttsche
Quoted 8 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Use an atomic counter to avoid acquiring the synchronizing mutex for each file. Only acquire when actually writing to stdout.

Suggested-by Copilot
Quoted 54 lines

---
v2: extract fc_count reset into separate commit

---
 libselinux/src/selinux_restorecon.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
libselinux/src/selinux_restorecon.c +11 -10
index 8b6eba0c..b4d58b05 100644
@@ -40,8 +40,6 @@
 #include "label_file.h"
 #include "sha1.h"
 
-#define STAR_COUNT 1024
-
 static struct selabel_handle *fc_sehandle = NULL;
 static bool selabel_no_digest;
 static char *rootpath = NULL;
@@ -734,19 +732,23 @@ static int restorecon_sb(int fd, const char *pathname, const struct stat *sb,
        }
 
        if (flags->progress) {
-               __pthread_mutex_lock(&progress_mutex);
-               fc_count++;
-               if (fc_count % STAR_COUNT == 0) {
+               const unsigned STAR_COUNT = 1024;
+               uint64_t fc_count_local;
+
+               fc_count_local = __atomic_add_fetch(&fc_count, 1, __ATOMIC_RELAXED);
+
+               if (fc_count_local % STAR_COUNT == 0) {
+                       __pthread_mutex_lock(&progress_mutex);
                        if (flags->mass_relabel && efile_count > 0) {
-                               float pc = (fc_count < efile_count) ? (100.0 *
-                                            fc_count / efile_count) : 100;
+                               float pc = (fc_count_local < efile_count) ? (100.0 *
+                                            fc_count_local / efile_count) : 100;
                                fprintf(stdout, "\r%-.1f%%", (double)pc);
                        } else {
-                               fprintf(stdout, "\r%" PRIu64 "k", fc_count / STAR_COUNT);
+                               fprintf(stdout, "\r%" PRIu64 "k", fc_count_local / STAR_COUNT);
                        }
                        fflush(stdout);
+                       __pthread_mutex_unlock(&progress_mutex);
                }
-               __pthread_mutex_unlock(&progress_mutex);
        }
 
        if (flags->add_assoc) {
--

2.53.0

maintainer
#5 lore raw

Re: [PATCH v2 1/2] libselinux: improve restorecon progress locking

Quoted 6 lines · Stephen Smalley
Quoted 3 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Quoted 7 lines
Quoted 5 lines

Use an atomic counter to avoid acquiring the synchronizing mutex for each file. Only acquire when actually writing to stdout.

Suggested-by Copilot

Merged.

maintainer
#6 lore raw
In reply to Christian Göttsche
Quoted 16 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Reset the scanned file count at startup in case the calling process calls selinux_restorecon(3) multiple times during its lifetime.

---
 libselinux/src/selinux_restorecon.c | 2 ++
 1 file changed, 2 insertions(+)
libselinux/src/selinux_restorecon.c +0 -0
index b4d58b05..b7defffb 100644

libselinux/src/selinux_restorecon.c static int selinux_restorecon_common(const char *pathname_orig, · line 1450

Quoted 5 lines
libselinux/src/selinux_restorecon.c +2 -0
@@ -1446,6 +1446,8 @@ static int selinux_restorecon_common(const char *pathname_orig,
        int error;
        struct dir_hash_node *current = NULL;
 
+       fc_count = 0;
+

Should we use an atomic operation for clearing fc_count?

Quoted 7 lines
libselinux/src/selinux_restorecon.c +0 -1
        if (state.flags.verbose && state.flags.progress)
                state.flags.verbose = false;
 
--

2.53.0

#7 lore raw
Quoted 22 lines · Stephen Smalley
Quoted 19 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Reset the scanned file count at startup in case the calling process calls selinux_restorecon(3) multiple times during its lifetime.

---
 libselinux/src/selinux_restorecon.c | 2 ++
 1 file changed, 2 insertions(+)
libselinux/src/selinux_restorecon.c +0 -0
index b4d58b05..b7defffb 100644
@@ -1446,6 +1446,8 @@ static int selinux_restorecon_common(const char *pathname_orig,
        int error;
        struct dir_hash_node *current = NULL;
 
Quoted 4 lines

libselinux/src/selinux_restorecon.c static int selinux_restorecon_common(const char *pathname_orig, · line 1450

Quoted 2 lines
libselinux/src/selinux_restorecon.c +2 -0
+       fc_count = 0;
+

Should we use an atomic operation for clearing fc_count?

I think calling selinux_restorecon(3) concurrently from multiple threads is not fully thread-safe yet, for example the assignments to skipped_errors and relabeled_files. selinux_restorecon_xattr(3) is completely unsafe due the unsynchronized usage of dir_xattr_list and dir_xattr_last. At least for selinux_restorecon(3) I think it might be not too complicated to support concurrent usage, then the reset should be atomic, yes.

Quoted 8 lines
Quoted 8 lines
libselinux/src/selinux_restorecon.c +0 -1
        if (state.flags.verbose && state.flags.progress)
                state.flags.verbose = false;
 
--

2.53.0

maintainer Acked-by on v2 2/2
#8 lore raw
In reply to Christian Göttsche
Quoted 7 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Reset the scanned file count at startup in case the calling process calls selinux_restorecon(3) multiple times during its lifetime.

Quoted 21 lines
---
 libselinux/src/selinux_restorecon.c | 2 ++
 1 file changed, 2 insertions(+)
libselinux/src/selinux_restorecon.c +2 -1
index b4d58b05..b7defffb 100644
@@ -1446,6 +1446,8 @@ static int selinux_restorecon_common(const char *pathname_orig,
        int error;
        struct dir_hash_node *current = NULL;
 
+       fc_count = 0;
+
        if (state.flags.verbose && state.flags.progress)
                state.flags.verbose = false;
 
--

2.53.0

maintainer
#9 lore raw
Quoted 4 lines · Stephen Smalley
Quoted 1 line · Christian Göttsche
Quoted 8 lines
Quoted 6 lines

From: Christian Göttsche <[email protected]>

Reset the scanned file count at startup in case the calling process calls selinux_restorecon(3) multiple times during its lifetime.

Merged.

Quoted 22 lines
Quoted 22 lines
---
 libselinux/src/selinux_restorecon.c | 2 ++
 1 file changed, 2 insertions(+)
libselinux/src/selinux_restorecon.c +2 -1
index b4d58b05..b7defffb 100644
@@ -1446,6 +1446,8 @@ static int selinux_restorecon_common(const char *pathname_orig,
        int error;
        struct dir_hash_node *current = NULL;
 
+       fc_count = 0;
+
        if (state.flags.verbose && state.flags.progress)
                state.flags.verbose = false;
 
--

2.53.0