label: rework label_fix() implementations (#8583)
This reworks the SELinux and SMACK label fixing calls in a number of ways: 1. The two separate boolean arguments of these functions are converted into a flags type LabelFixFlags. 2. The operations are now implemented based on O_PATH. This should resolve TTOCTTOU races between determining the label for the file system object and applying it, as it it allows to pin the object while we are operating on it. 3. When changing a label fails we'll query the label previously set, and if matches what we want to set anyway we'll suppress the error. Also, all calls to label_fix() are now (void)ified, when we ignore the return values. Fixes: #8566
This commit is contained in:
committed by
Zbigniew Jędrzejewski-Szmek
parent
ffb3c2bd70
commit
08c849815c
+50
-35
@@ -21,18 +21,21 @@
|
||||
***/
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/xattr.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "log.h"
|
||||
#include "macro.h"
|
||||
#include "path-util.h"
|
||||
#include "process-util.h"
|
||||
#include "smack-util.h"
|
||||
#include "stdio-util.h"
|
||||
#include "string-table.h"
|
||||
#include "xattr-util.h"
|
||||
|
||||
@@ -134,7 +137,10 @@ int mac_smack_apply_pid(pid_t pid, const char *label) {
|
||||
return r;
|
||||
}
|
||||
|
||||
int mac_smack_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
|
||||
int mac_smack_fix(const char *path, LabelFixFlags flags) {
|
||||
char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
|
||||
_cleanup_close_ int fd = -1;
|
||||
const char *label;
|
||||
struct stat st;
|
||||
int r;
|
||||
|
||||
@@ -143,50 +149,59 @@ int mac_smack_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
|
||||
if (!mac_smack_use())
|
||||
return 0;
|
||||
|
||||
/*
|
||||
* Path must be in /dev and must exist
|
||||
*/
|
||||
/* Path must be in /dev */
|
||||
if (!path_startswith(path, "/dev"))
|
||||
return 0;
|
||||
|
||||
r = lstat(path, &st);
|
||||
if (r >= 0) {
|
||||
const char *label;
|
||||
|
||||
/*
|
||||
* Label directories and character devices "*".
|
||||
* Label symlinks "_".
|
||||
* Don't change anything else.
|
||||
*/
|
||||
|
||||
if (S_ISDIR(st.st_mode))
|
||||
label = SMACK_STAR_LABEL;
|
||||
else if (S_ISLNK(st.st_mode))
|
||||
label = SMACK_FLOOR_LABEL;
|
||||
else if (S_ISCHR(st.st_mode))
|
||||
label = SMACK_STAR_LABEL;
|
||||
else
|
||||
fd = open(path, O_NOFOLLOW|O_CLOEXEC|O_PATH);
|
||||
if (fd < 0) {
|
||||
if ((flags & LABEL_IGNORE_ENOENT) && errno == ENOENT)
|
||||
return 0;
|
||||
|
||||
r = lsetxattr(path, "security.SMACK64", label, strlen(label), 0);
|
||||
return -errno;
|
||||
}
|
||||
|
||||
if (fstat(fd, &st) < 0)
|
||||
return -errno;
|
||||
|
||||
/*
|
||||
* Label directories and character devices "*".
|
||||
* Label symlinks "_".
|
||||
* Don't change anything else.
|
||||
*/
|
||||
|
||||
if (S_ISDIR(st.st_mode))
|
||||
label = SMACK_STAR_LABEL;
|
||||
else if (S_ISLNK(st.st_mode))
|
||||
label = SMACK_FLOOR_LABEL;
|
||||
else if (S_ISCHR(st.st_mode))
|
||||
label = SMACK_STAR_LABEL;
|
||||
else
|
||||
return 0;
|
||||
|
||||
xsprintf(procfs_path, "/proc/self/fd/%i", fd);
|
||||
if (setxattr(procfs_path, "security.SMACK64", label, strlen(label), 0) < 0) {
|
||||
_cleanup_free_ char *old_label = NULL;
|
||||
|
||||
r = -errno;
|
||||
|
||||
/* If the FS doesn't support labels, then exit without warning */
|
||||
if (r < 0 && errno == EOPNOTSUPP)
|
||||
if (r == -EOPNOTSUPP)
|
||||
return 0;
|
||||
|
||||
/* It the FS is read-only and we were told to ignore failures caused by that, suppress error */
|
||||
if (r == -EROFS && (flags & LABEL_IGNORE_EROFS))
|
||||
return 0;
|
||||
|
||||
/* If the old label is identical to the new one, suppress any kind of error */
|
||||
if (getxattr_malloc(procfs_path, "security.SMACK64", &old_label, false) >= 0 &&
|
||||
streq(old_label, label))
|
||||
return 0;
|
||||
|
||||
return log_debug_errno(r, "Unable to fix SMACK label of %s: %m", path);
|
||||
}
|
||||
|
||||
if (r < 0) {
|
||||
/* Ignore ENOENT in some cases */
|
||||
if (ignore_enoent && errno == ENOENT)
|
||||
return 0;
|
||||
|
||||
if (ignore_erofs && errno == EROFS)
|
||||
return 0;
|
||||
|
||||
r = log_debug_errno(errno, "Unable to fix SMACK label of %s: %m", path);
|
||||
}
|
||||
|
||||
return r;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mac_smack_copy(const char *dest, const char *src) {
|
||||
|
||||
Reference in New Issue
Block a user