@@ -121,6 +121,11 @@ extern int selinux_restorecon_parallel(const char *pathname,
*/
#define SELINUX_RESTORECON_CONFLICT_ERROR 0x10000
+/*
+ * Count, but otherwise ignore, errors during the file tree walk.
+ */
+#define SELINUX_RESTORECON_COUNT_ERRORS 0x20000
+
/**
* selinux_restorecon_set_sehandle - Set the global fc handle.
* @hndl: specifies handle to set as the global fc handle.
@@ -205,6 +210,16 @@ extern int selinux_restorecon_xattr(const char *pathname,
/* Do not read /proc/mounts. */
#define SELINUX_RESTORECON_XATTR_IGNORE_MOUNTS 0x0008
+/* selinux_restorecon_get_skipped_errors - Get the number of errors ignored
+ * during re-labeling.
+ *
+ * If SELINUX_RESTORECON_COUNT_ERRORS was passed to selinux_restorecon(3) or
+ * selinux_restorecon_parallel(3), and that function returned successfully
+ * (i.e., with a zero return value), then this function returns the number of
+ * errors ignored during the file tree walk.
+ */
+extern long unsigned selinux_restorecon_get_skipped_errors(void);
+
#ifdef __cplusplus
}
#endif
@@ -66,6 +66,9 @@ static pthread_mutex_t progress_mutex = PTHREAD_MUTEX_INITIALIZER;
static struct dir_xattr *dir_xattr_list;
static struct dir_xattr *dir_xattr_last;
+/* Number of errors ignored during the file tree walk. */
+static long unsigned skipped_errors;
+
/* restorecon_flags for passing to restorecon_sb() */
struct rest_flags {
bool nochange;
@@ -83,6 +86,7 @@ struct rest_flags {
bool ignore_noent;
bool warnonnomatch;
bool conflicterror;
+ bool count_errors;
};
static void restorecon_init(void)
@@ -827,6 +831,7 @@ struct rest_state {
struct dir_hash_node *head, *current;
bool abort;
int error;
+ long unsigned skipped_errors;
int saved_errno;
pthread_mutex_t mutex;
};
@@ -949,11 +954,17 @@ loop_body:
goto unlock;
}
- state->error |= error;
first = false;
- if (error && state->flags.abort_on_error) {
- state->abort = true;
- goto finish;
+ if (error) {
+ if (state->flags.abort_on_error) {
+ state->error = error;
+ state->abort = true;
+ goto finish;
+ }
+ if (state->flags.count_errors)
+ state->skipped_errors++;
+ else
+ state->error = error;
}
break;
}
@@ -1007,12 +1018,15 @@ static int selinux_restorecon_common(const char *pathname_orig,
SELINUX_RESTORECON_IGNORE_MOUNTS) ? true : false;
state.ignore_digest = (restorecon_flags &
SELINUX_RESTORECON_IGNORE_DIGEST) ? true : false;
+ state.flags.count_errors = (restorecon_flags &
+ SELINUX_RESTORECON_COUNT_ERRORS) ? true : false;
state.setrestorecondigest = true;
state.head = NULL;
state.current = NULL;
state.abort = false;
state.error = 0;
+ state.skipped_errors = 0;
state.saved_errno = 0;
struct stat sb;
@@ -1225,8 +1239,11 @@ static int selinux_restorecon_common(const char *pathname_orig,
/*
* Labeling successful. Write partial match digests for subdirectories.
* TODO: Write digest upon FTS_DP if no error occurs in its descents.
+ * Note: we can't ignore errors here that we've masked due to
+ * SELINUX_RESTORECON_COUNT_ERRORS.
*/
- if (state.setrestorecondigest && !state.flags.nochange && !error) {
+ if (state.setrestorecondigest && !state.flags.nochange && !error &&
+ state.skipped_errors == 0) {
current = state.head;
while (current != NULL) {
if (setxattr(current->path,
@@ -1241,6 +1258,8 @@ static int selinux_restorecon_common(const char *pathname_orig,
}
}
+ skipped_errors = state.skipped_errors;
+
out:
if (state.flags.progress && state.flags.mass_relabel)
fprintf(stdout, "\r%s 100.0%%\n", pathname);
@@ -1520,3 +1539,8 @@ cleanup:
}
return -1;
}
+
+long unsigned selinux_restorecon_get_skipped_errors(void)
+{
+ return skipped_errors;
+}
@@ -78,7 +78,10 @@ specfile entries SHA1 digest. The specfile entries digest will be written to the
.IR security.sehash
extended attribute once relabeling has been completed successfully provided the
.B SELINUX_RESTORECON_NOCHANGE
-flag has not been set.
+flag has not been set, and no errors have been skipped during the file tree walk
+due to the
+.B SELINUX_RESTORECON_COUNT_ERRORS
+flag.
.sp
.B SELINUX_RESTORECON_NOCHANGE
don't change any file labels (passive check) or update the digest in the
@@ -164,6 +167,21 @@ on a directory below this.
.B SELINUX_RESTORECON_CONFLICT_ERROR
to treat conflicting specifications, such as where two hardlinks for the
same inode have different contexts, as errors.
+.sp
+.B SELINUX_RESTORECON_COUNT_ERRORS
+Count, but otherwise ignore, errors during the file tree walk. Only makes a
+difference if the
+.B SELINUX_RESTORECON_ABORT_ON_ERROR
+flag is clear. Call
+.BR selinux_restorecon_get_skipped_errors (3)
+for fetching the ignored (skipped) error count after
+.BR selinux_restorecon (3)
+or
+.BR selinux_restorecon_parallel (3)
+completes with success. In case any errors were skipped during the file tree
+walk, the specfile entries SHA1 digest will not have been written to the
+.IR security.sehash
+extended attribute.
.RE
.sp
The behavior regarding the checking and updating of the SHA1 digest described
@@ -279,6 +297,8 @@ option.
.br
.BR selinux_restorecon_default_handle (3),
.br
+.BR selinux_restorecon_get_skipped_errors (3),
+.br
.BR selinux_restorecon_set_exclude_list (3),
.br
.BR selinux_restorecon_set_alt_rootpath (3),
new file mode 100644
@@ -0,0 +1,28 @@
+.TH "selinux_restorecon_get_skipped_errors" "3" "27 Apr 2022" "Security Enhanced Linux" "SELinux API documentation"
+
+.SH "NAME"
+selinux_restorecon_get_skipped_errors \- get the number of errors ignored by
+.BR selinux_restorecon (3)
+or
+.BR selinux_restorecon_parallel (3)
+during the file tree walk
+.
+.SH "SYNOPSIS"
+.B #include <selinux/restorecon.h>
+.sp
+.BI "long unsigned selinux_restorecon_get_skipped_errors(void);"
+.in +\w'long unsigned selinux_restorecon_get_skipped_errors('u
+.
+.SH "DESCRIPTION"
+If
+.B SELINUX_RESTORECON_COUNT_ERRORS
+was passed to
+.BR selinux_restorecon (3)
+or
+.BR selinux_restorecon_parallel (3)
+and that function returned successfully (i.e., with a zero return value), then
+.BR selinux_restorecon_get_skipped_errors ()
+returns the number of errors ignored during the file tree walk.
+.
+.SH "SEE ALSO"
+.BR selinux_restorecon (3)
@@ -243,5 +243,6 @@ LIBSELINUX_1.0 {
LIBSELINUX_3.4 {
global:
+ selinux_restorecon_get_skipped_errors;
selinux_restorecon_parallel;
} LIBSELINUX_1.0;
Currently, if the SELINUX_RESTORECON_ABORT_ON_ERROR flag is clear, then selinux_restorecon[_parallel]() does not abort the file tree walk upon an error, but the function itself fails the same, with the same (-1) return value. This in turn is reported by the setfiles(8) utility to its parent process with the same exit code (255). In libguestfs we want to proceed after setfiles(8) fails *at most* with such errors that occur during the file tree walk. We need setfiles(8) to exit with a distinct exit status in that situation. For this, introduce the SELINUX_RESTORECON_COUNT_ERRORS flag, and the corresponding selinux_restorecon_get_skipped_errors() function, for selinux_restorecon[_parallel]() to count, but otherwise ignore, errors during the file tree walk. When no other kind of error occurs, the relabeling functions will return zero, and the caller can fetch the number of errors ignored during the file tree walk with selinux_restorecon_get_skipped_errors(). Importantly, when at least one such error is skipped, we don't write partial match digests for subdirectories, as any masked error means that any subdirectory may not have been completely relabeled. Cc: "Richard W.M. Jones" <rjones@redhat.com> Cc: Petr Lautrbach <plautrba@redhat.com> Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1794518 Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- Notes: v2: - introduce the "selinux_restorecon_get_skipped_errors" symbol in the LIBSELINUX_3.4 section of "libselinux.map"; do not create the LIBSELINUX_3.5 section [Petr Lautrbach] libselinux/include/selinux/restorecon.h | 15 +++++++++ libselinux/src/selinux_restorecon.c | 34 +++++++++++++++++--- libselinux/man/man3/selinux_restorecon.3 | 22 ++++++++++++- libselinux/man/man3/selinux_restorecon_get_skipped_errors.3 | 28 ++++++++++++++++ libselinux/src/libselinux.map | 1 + 5 files changed, 94 insertions(+), 6 deletions(-)