Magisk/native/src/core/selinux.cpp

135 lines
4.0 KiB
C++
Raw Normal View History

2023-10-16 17:38:44 -07:00
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/xattr.h>
#include <magisk.hpp>
2022-05-12 02:03:42 -07:00
#include <base.hpp>
2023-10-16 17:38:44 -07:00
#include <selinux.hpp>
#include <flags.h>
using namespace std;
2023-10-17 16:04:59 -07:00
int setcon(const char *con) {
2023-10-16 17:38:44 -07:00
int fd = open("/proc/self/attr/current", O_WRONLY | O_CLOEXEC);
if (fd < 0)
return fd;
2023-10-17 16:04:59 -07:00
size_t len = strlen(con) + 1;
int rc = write(fd, con, len);
2023-10-16 17:38:44 -07:00
close(fd);
return rc != len;
}
2023-10-17 16:04:59 -07:00
int getfilecon(const char *path, byte_data con) {
return syscall(__NR_getxattr, path, XATTR_NAME_SELINUX, con.buf(), con.sz());
2023-10-16 17:38:44 -07:00
}
2023-10-17 16:04:59 -07:00
int lgetfilecon(const char *path, byte_data con) {
return syscall(__NR_lgetxattr, path, XATTR_NAME_SELINUX, con.buf(), con.sz());
2023-10-16 17:38:44 -07:00
}
2023-10-17 16:04:59 -07:00
int fgetfilecon(int fd, byte_data con) {
return syscall(__NR_fgetxattr, fd, XATTR_NAME_SELINUX, con.buf(), con.sz());
2023-10-16 17:38:44 -07:00
}
2023-10-17 16:04:59 -07:00
int setfilecon(const char *path, const char *con) {
return syscall(__NR_setxattr, path, XATTR_NAME_SELINUX, con, strlen(con) + 1, 0);
2023-10-16 17:38:44 -07:00
}
2023-10-17 16:04:59 -07:00
int lsetfilecon(const char *path, const char *con) {
return syscall(__NR_lsetxattr, path, XATTR_NAME_SELINUX, con, strlen(con) + 1, 0);
2023-10-16 17:38:44 -07:00
}
2023-10-17 16:04:59 -07:00
int fsetfilecon(int fd, const char *con) {
return syscall(__NR_fsetxattr, fd, XATTR_NAME_SELINUX, con, strlen(con) + 1, 0);
2023-10-16 17:38:44 -07:00
}
2023-10-17 16:04:59 -07:00
int getfilecon_at(int dirfd, const char *name, byte_data con) {
2023-10-16 17:38:44 -07:00
char path[4096];
fd_pathat(dirfd, name, path, sizeof(path));
2023-10-17 16:04:59 -07:00
return lgetfilecon(path, con);
2023-10-16 17:38:44 -07:00
}
void setfilecon_at(int dirfd, const char *name, const char *con) {
char path[4096];
fd_pathat(dirfd, name, path, sizeof(path));
lsetfilecon(path, con);
}
#define UNLABEL_CON "u:object_r:unlabeled:s0"
#define SYSTEM_CON "u:object_r:system_file:s0"
#define ADB_CON "u:object_r:adb_data_file:s0"
#define ROOT_CON "u:object_r:rootfs:s0"
2023-03-15 00:24:33 +08:00
static void restore_syscon_from_null(int dirfd) {
struct dirent *entry;
2023-10-17 16:04:59 -07:00
char con[1024];
2023-10-17 16:04:59 -07:00
if (fgetfilecon(dirfd, { con, sizeof(con) }) >= 0) {
if (con[0] == '\0' || strcmp(con, UNLABEL_CON) == 0)
fsetfilecon(dirfd, SYSTEM_CON);
}
auto dir = xopen_dir(dirfd);
while ((entry = xreaddir(dir.get()))) {
int fd = openat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC);
if (entry->d_type == DT_DIR) {
2023-03-15 00:24:33 +08:00
restore_syscon_from_null(fd);
continue;
} else if (entry->d_type == DT_REG) {
2023-10-17 16:04:59 -07:00
if (fgetfilecon(fd, { con, sizeof(con) }) >= 0) {
if (con[0] == '\0' || strcmp(con, UNLABEL_CON) == 0)
fsetfilecon(fd, SYSTEM_CON);
}
} else if (entry->d_type == DT_LNK) {
2023-10-17 16:04:59 -07:00
if (getfilecon_at(dirfd, entry->d_name, { con, sizeof(con) }) >= 0) {
if (con[0] == '\0' || strcmp(con, UNLABEL_CON) == 0)
setfilecon_at(dirfd, entry->d_name, SYSTEM_CON);
}
}
close(fd);
}
}
2023-03-15 00:24:33 +08:00
static void restore_syscon(int dirfd) {
struct dirent *entry;
2023-03-15 00:24:33 +08:00
fsetfilecon(dirfd, SYSTEM_CON);
fchown(dirfd, 0, 0);
auto dir = xopen_dir(dirfd);
while ((entry = xreaddir(dir.get()))) {
int fd = xopenat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC);
if (entry->d_type == DT_DIR) {
2023-03-15 00:24:33 +08:00
restore_syscon(fd);
continue;
} else if (entry->d_type) {
2023-03-15 00:24:33 +08:00
fsetfilecon(fd, SYSTEM_CON);
fchown(fd, 0, 0);
}
close(fd);
}
}
void restorecon() {
2023-10-16 17:38:44 -07:00
int fd = xopen("/sys/fs/selinux/context", O_WRONLY | O_CLOEXEC);
if (write(fd, ADB_CON, sizeof(ADB_CON)) >= 0)
lsetfilecon(SECURE_DIR, ADB_CON);
close(fd);
lsetfilecon(MODULEROOT, SYSTEM_CON);
2023-03-15 00:24:33 +08:00
restore_syscon_from_null(xopen(MODULEROOT, O_RDONLY | O_CLOEXEC));
restore_syscon(xopen(DATABIN, O_RDONLY | O_CLOEXEC));
}
2020-04-19 03:33:25 -07:00
void restore_tmpcon() {
if (MAGISKTMP == "/sbin")
setfilecon(MAGISKTMP.data(), ROOT_CON);
else
2023-03-15 00:24:33 +08:00
chmod(MAGISKTMP.data(), 0711);
auto dir = xopen_dir(MAGISKTMP.data());
int dfd = dirfd(dir.get());
2021-01-18 12:37:08 -08:00
for (dirent *entry; (entry = xreaddir(dir.get()));)
setfilecon_at(dfd, entry->d_name, SYSTEM_CON);
}