Simplify C++ SELinux routines

This commit is contained in:
topjohnwu 2023-10-17 16:04:59 -07:00
parent 4b8a0388e7
commit 91c92051f1
2 changed files with 31 additions and 50 deletions

View File

@ -9,61 +9,44 @@
using namespace std; using namespace std;
void freecon(char *s) { int setcon(const char *con) {
free(s);
}
int setcon(const char *ctx) {
int fd = open("/proc/self/attr/current", O_WRONLY | O_CLOEXEC); int fd = open("/proc/self/attr/current", O_WRONLY | O_CLOEXEC);
if (fd < 0) if (fd < 0)
return fd; return fd;
size_t len = strlen(ctx) + 1; size_t len = strlen(con) + 1;
int rc = write(fd, ctx, len); int rc = write(fd, con, len);
close(fd); close(fd);
return rc != len; return rc != len;
} }
int getfilecon(const char *path, char **ctx) { int getfilecon(const char *path, byte_data con) {
char buf[1024]; return syscall(__NR_getxattr, path, XATTR_NAME_SELINUX, con.buf(), con.sz());
int rc = syscall(__NR_getxattr, path, XATTR_NAME_SELINUX, buf, sizeof(buf) - 1);
if (rc >= 0)
*ctx = strdup(buf);
return rc;
} }
int lgetfilecon(const char *path, char **ctx) { int lgetfilecon(const char *path, byte_data con) {
char buf[1024]; return syscall(__NR_lgetxattr, path, XATTR_NAME_SELINUX, con.buf(), con.sz());
int rc = syscall(__NR_lgetxattr, path, XATTR_NAME_SELINUX, buf, sizeof(buf) - 1);
if (rc >= 0)
*ctx = strdup(buf);
return rc;
} }
int fgetfilecon(int fd, char **ctx) { int fgetfilecon(int fd, byte_data con) {
char buf[1024]; return syscall(__NR_fgetxattr, fd, XATTR_NAME_SELINUX, con.buf(), con.sz());
int rc = syscall(__NR_fgetxattr, fd, XATTR_NAME_SELINUX, buf, sizeof(buf) - 1);
if (rc >= 0)
*ctx = strdup(buf);
return rc;
} }
int setfilecon(const char *path, const char *ctx) { int setfilecon(const char *path, const char *con) {
return syscall(__NR_setxattr, path, XATTR_NAME_SELINUX, ctx, strlen(ctx) + 1, 0); return syscall(__NR_setxattr, path, XATTR_NAME_SELINUX, con, strlen(con) + 1, 0);
} }
int lsetfilecon(const char *path, const char *ctx) { int lsetfilecon(const char *path, const char *con) {
return syscall(__NR_lsetxattr, path, XATTR_NAME_SELINUX, ctx, strlen(ctx) + 1, 0); return syscall(__NR_lsetxattr, path, XATTR_NAME_SELINUX, con, strlen(con) + 1, 0);
} }
int fsetfilecon(int fd, const char *ctx) { int fsetfilecon(int fd, const char *con) {
return syscall(__NR_fsetxattr, fd, XATTR_NAME_SELINUX, ctx, strlen(ctx) + 1, 0); return syscall(__NR_fsetxattr, fd, XATTR_NAME_SELINUX, con, strlen(con) + 1, 0);
} }
void getfilecon_at(int dirfd, const char *name, char **con) { int getfilecon_at(int dirfd, const char *name, byte_data con) {
char path[4096]; char path[4096];
fd_pathat(dirfd, name, path, sizeof(path)); fd_pathat(dirfd, name, path, sizeof(path));
if (lgetfilecon(path, con)) return lgetfilecon(path, con);
*con = strdup("");
} }
void setfilecon_at(int dirfd, const char *name, const char *con) { void setfilecon_at(int dirfd, const char *name, const char *con) {
@ -79,12 +62,11 @@ void setfilecon_at(int dirfd, const char *name, const char *con) {
static void restore_syscon_from_null(int dirfd) { static void restore_syscon_from_null(int dirfd) {
struct dirent *entry; struct dirent *entry;
char *con; char con[1024];
if (fgetfilecon(dirfd, &con) >= 0) { if (fgetfilecon(dirfd, { con, sizeof(con) }) >= 0) {
if (strlen(con) == 0 || strcmp(con, UNLABEL_CON) == 0) if (con[0] == '\0' || strcmp(con, UNLABEL_CON) == 0)
fsetfilecon(dirfd, SYSTEM_CON); fsetfilecon(dirfd, SYSTEM_CON);
freecon(con);
} }
auto dir = xopen_dir(dirfd); auto dir = xopen_dir(dirfd);
@ -94,16 +76,15 @@ static void restore_syscon_from_null(int dirfd) {
restore_syscon_from_null(fd); restore_syscon_from_null(fd);
continue; continue;
} else if (entry->d_type == DT_REG) { } else if (entry->d_type == DT_REG) {
if (fgetfilecon(fd, &con) >= 0) { if (fgetfilecon(fd, { con, sizeof(con) }) >= 0) {
if (con[0] == '\0' || strcmp(con, UNLABEL_CON) == 0) if (con[0] == '\0' || strcmp(con, UNLABEL_CON) == 0)
fsetfilecon(fd, SYSTEM_CON); fsetfilecon(fd, SYSTEM_CON);
freecon(con);
} }
} else if (entry->d_type == DT_LNK) { } else if (entry->d_type == DT_LNK) {
getfilecon_at(dirfd, entry->d_name, &con); if (getfilecon_at(dirfd, entry->d_name, { con, sizeof(con) }) >= 0) {
if (con[0] == '\0' || strcmp(con, UNLABEL_CON) == 0) if (con[0] == '\0' || strcmp(con, UNLABEL_CON) == 0)
setfilecon_at(dirfd, entry->d_name, con); setfilecon_at(dirfd, entry->d_name, SYSTEM_CON);
freecon(con); }
} }
close(fd); close(fd);
} }

View File

@ -1,16 +1,16 @@
#pragma once #pragma once
void freecon(char *con); #include <base.hpp>
int setcon(const char *con); int setcon(const char *con);
int getfilecon(const char *path, char **con); int getfilecon(const char *path, byte_data con);
int lgetfilecon(const char *path, char **con); int lgetfilecon(const char *path, byte_data con);
int fgetfilecon(int fd, char **con); int fgetfilecon(int fd, byte_data con);
int setfilecon(const char *path, const char *con); int setfilecon(const char *path, const char *con);
int lsetfilecon(const char *path, const char *con); int lsetfilecon(const char *path, const char *con);
int fsetfilecon(int fd, const char *con); int fsetfilecon(int fd, const char *con);
void getfilecon_at(int dirfd, const char *name, char **con); int getfilecon_at(int dirfd, const char *name, byte_data con);
void setfilecon_at(int dirfd, const char *name, const char *con); void setfilecon_at(int dirfd, const char *name, const char *con);
//void enable_selinux();
void restorecon(); void restorecon();
void restore_tmpcon(); void restore_tmpcon();