core/smack-setup: add helper function for openat+fdopen

Unlocked operations are used in all three places. I don't see why just one was
special.

This also improves logging, since we don't just log the final component of the
path, but the full name.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2019-04-04 11:27:21 +02:00
parent 02e23d1a1a
commit b636d78aee
1 changed files with 34 additions and 59 deletions

View File

@ -26,12 +26,36 @@
#if ENABLE_SMACK
static int write_access2_rules(const char* srcdir) {
static int fdopen_unlocked_at(int dfd, const char *dir, const char *name, int *status, FILE **ret_file) {
int fd, r;
FILE *f;
fd = openat(dfd, name, O_RDONLY|O_CLOEXEC);
if (fd < 0) {
if (*status == 0)
*status = -errno;
return log_warning_errno(errno, "Failed to open \"%s/%s\": %m", dir, name);
}
r = fdopen_unlocked(fd, "r", &f);
if (r < 0) {
if (*status == 0)
*status = r;
safe_close(fd);
return log_error_errno(r, "Failed to open \"%s/%s\": %m", dir, name);
}
*ret_file = f;
return 0;
}
static int write_access2_rules(const char *srcdir) {
_cleanup_close_ int load2_fd = -1, change_fd = -1;
_cleanup_closedir_ DIR *dir = NULL;
struct dirent *entry;
int dfd = -1;
int r = 0;
int dfd = -1, r = 0;
load2_fd = open("/sys/fs/smackfs/load2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (load2_fd < 0) {
@ -59,28 +83,13 @@ static int write_access2_rules(const char* srcdir) {
assert(dfd >= 0);
FOREACH_DIRENT(entry, dir, return 0) {
int fd;
_cleanup_fclose_ FILE *policy = NULL;
if (!dirent_is_file(entry))
continue;
fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
if (fd < 0) {
if (r == 0)
r = -errno;
log_warning_errno(errno, "Failed to open '%s': %m", entry->d_name);
if (fdopen_unlocked_at(dfd, srcdir, entry->d_name, &r, &policy) < 0)
continue;
}
policy = fdopen(fd, "r");
if (!policy) {
if (r == 0)
r = -errno;
safe_close(fd);
log_error_errno(errno, "Failed to open '%s': %m", entry->d_name);
continue;
}
/* load2 write rules in the kernel require a line buffered stream */
for (;;) {
@ -115,12 +124,11 @@ static int write_access2_rules(const char* srcdir) {
return r;
}
static int write_cipso2_rules(const char* srcdir) {
static int write_cipso2_rules(const char *srcdir) {
_cleanup_close_ int cipso2_fd = -1;
_cleanup_closedir_ DIR *dir = NULL;
struct dirent *entry;
int dfd = -1;
int r = 0;
int dfd = -1, r = 0;
cipso2_fd = open("/sys/fs/smackfs/cipso2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (cipso2_fd < 0) {
@ -141,28 +149,13 @@ static int write_cipso2_rules(const char* srcdir) {
assert(dfd >= 0);
FOREACH_DIRENT(entry, dir, return 0) {
int fd;
_cleanup_fclose_ FILE *policy = NULL;
if (!dirent_is_file(entry))
continue;
fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
if (fd < 0) {
if (r == 0)
r = -errno;
log_error_errno(errno, "Failed to open '%s': %m", entry->d_name);
if (fdopen_unlocked_at(dfd, srcdir, entry->d_name, &r, &policy) < 0)
continue;
}
policy = fdopen(fd, "r");
if (!policy) {
if (r == 0)
r = -errno;
safe_close(fd);
log_error_errno(errno, "Failed to open '%s': %m", entry->d_name);
continue;
}
/* cipso2 write rules in the kernel require a line buffered stream */
for (;;) {
@ -191,12 +184,11 @@ static int write_cipso2_rules(const char* srcdir) {
return r;
}
static int write_netlabel_rules(const char* srcdir) {
static int write_netlabel_rules(const char *srcdir) {
_cleanup_fclose_ FILE *dst = NULL;
_cleanup_closedir_ DIR *dir = NULL;
struct dirent *entry;
int dfd = -1;
int r = 0;
int dfd = -1, r = 0;
dst = fopen("/sys/fs/smackfs/netlabel", "we");
if (!dst) {
@ -217,27 +209,10 @@ static int write_netlabel_rules(const char* srcdir) {
assert(dfd >= 0);
FOREACH_DIRENT(entry, dir, return 0) {
int fd;
_cleanup_fclose_ FILE *policy = NULL;
fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
if (fd < 0) {
if (r == 0)
r = -errno;
log_warning_errno(errno, "Failed to open %s: %m", entry->d_name);
if (fdopen_unlocked_at(dfd, srcdir, entry->d_name, &r, &policy) < 0)
continue;
}
policy = fdopen(fd, "r");
if (!policy) {
if (r == 0)
r = -errno;
safe_close(fd);
log_error_errno(errno, "Failed to open %s: %m", entry->d_name);
continue;
}
(void) __fsetlocking(policy, FSETLOCKING_BYCALLER);
/* load2 write rules in the kernel require a line buffered stream */
for (;;) {