2023-10-16 17:38:44 -07:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/xattr.h>
|
2020-04-11 04:40:40 -07:00
|
|
|
|
|
|
|
#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>
|
2020-04-11 04:40:40 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-04-11 04:40:40 -07:00
|
|
|
#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) {
|
2020-12-30 22:11:24 -08:00
|
|
|
struct dirent *entry;
|
2023-10-17 16:04:59 -07:00
|
|
|
char con[1024];
|
2020-04-11 04:40:40 -07:00
|
|
|
|
2023-10-17 16:04:59 -07:00
|
|
|
if (fgetfilecon(dirfd, { con, sizeof(con) }) >= 0) {
|
|
|
|
if (con[0] == '\0' || strcmp(con, UNLABEL_CON) == 0)
|
2021-04-03 16:08:36 +08:00
|
|
|
fsetfilecon(dirfd, SYSTEM_CON);
|
|
|
|
}
|
2020-04-11 04:40:40 -07:00
|
|
|
|
2022-02-02 18:39:13 +08:00
|
|
|
auto dir = xopen_dir(dirfd);
|
|
|
|
while ((entry = xreaddir(dir.get()))) {
|
2020-12-30 22:11:24 -08:00
|
|
|
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);
|
2022-02-02 18:39:13 +08:00
|
|
|
continue;
|
2020-12-30 22:11:24 -08:00
|
|
|
} else if (entry->d_type == DT_REG) {
|
2023-10-17 16:04:59 -07:00
|
|
|
if (fgetfilecon(fd, { con, sizeof(con) }) >= 0) {
|
2023-03-02 18:20:38 +08:00
|
|
|
if (con[0] == '\0' || strcmp(con, UNLABEL_CON) == 0)
|
2021-04-03 16:08:36 +08:00
|
|
|
fsetfilecon(fd, SYSTEM_CON);
|
|
|
|
}
|
2020-12-30 22:11:24 -08:00
|
|
|
} 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);
|
|
|
|
}
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
2020-04-11 04:40:40 -07:00
|
|
|
}
|
|
|
|
|
2023-03-15 00:24:33 +08:00
|
|
|
static void restore_syscon(int dirfd) {
|
2020-12-30 22:11:24 -08:00
|
|
|
struct dirent *entry;
|
2020-04-11 04:40:40 -07:00
|
|
|
|
2023-03-15 00:24:33 +08:00
|
|
|
fsetfilecon(dirfd, SYSTEM_CON);
|
2020-12-30 22:11:24 -08:00
|
|
|
fchown(dirfd, 0, 0);
|
2020-04-11 04:40:40 -07:00
|
|
|
|
2022-02-02 18:39:13 +08:00
|
|
|
auto dir = xopen_dir(dirfd);
|
|
|
|
while ((entry = xreaddir(dir.get()))) {
|
2020-12-30 22:11:24 -08:00
|
|
|
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);
|
2022-02-02 18:39:13 +08:00
|
|
|
continue;
|
2020-12-30 22:11:24 -08:00
|
|
|
} else if (entry->d_type) {
|
2023-03-15 00:24:33 +08:00
|
|
|
fsetfilecon(fd, SYSTEM_CON);
|
2020-12-30 22:11:24 -08:00
|
|
|
fchown(fd, 0, 0);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
2020-04-11 04:40:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void restorecon() {
|
2023-10-16 17:38:44 -07:00
|
|
|
int fd = xopen("/sys/fs/selinux/context", O_WRONLY | O_CLOEXEC);
|
2020-12-30 22:11:24 -08:00
|
|
|
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-11 04:40:40 -07:00
|
|
|
}
|
|
|
|
|
2020-04-19 03:33:25 -07:00
|
|
|
void restore_tmpcon() {
|
2020-12-30 22:11:24 -08:00
|
|
|
if (MAGISKTMP == "/sbin")
|
|
|
|
setfilecon(MAGISKTMP.data(), ROOT_CON);
|
|
|
|
else
|
2023-03-15 00:24:33 +08:00
|
|
|
chmod(MAGISKTMP.data(), 0711);
|
2020-04-11 04:40:40 -07:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
auto dir = xopen_dir(MAGISKTMP.data());
|
|
|
|
int dfd = dirfd(dir.get());
|
2020-04-11 04:40:40 -07:00
|
|
|
|
2021-01-18 12:37:08 -08:00
|
|
|
for (dirent *entry; (entry = xreaddir(dir.get()));)
|
|
|
|
setfilecon_at(dfd, entry->d_name, SYSTEM_CON);
|
2020-04-11 04:40:40 -07:00
|
|
|
}
|