2019-07-01 22:58:19 -07:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-12-13 00:37:06 -05:00
|
|
|
#include <sys/stat.h>
|
2019-11-19 02:04:47 -05:00
|
|
|
#include <functional>
|
|
|
|
|
#include <string_view>
|
2020-04-01 04:39:28 -07:00
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
2019-11-19 02:04:47 -05:00
|
|
|
|
2023-06-25 07:21:35 +08:00
|
|
|
#include <linux/fs.h>
|
2023-06-07 16:49:40 -07:00
|
|
|
#include "misc.hpp"
|
2019-12-13 00:37:06 -05:00
|
|
|
|
2021-11-29 19:56:37 -08:00
|
|
|
template <typename T>
|
|
|
|
|
static inline T align_to(T v, int a) {
|
|
|
|
|
static_assert(std::is_integral<T>::value);
|
|
|
|
|
return (v + a - 1) / a * a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
static inline T align_padding(T v, int a) {
|
|
|
|
|
return align_to(v, a) - v;
|
|
|
|
|
}
|
2019-07-01 22:58:19 -07:00
|
|
|
|
2021-11-30 01:50:55 -08:00
|
|
|
struct mmap_data : public byte_data {
|
2023-06-25 07:21:35 +08:00
|
|
|
static_assert((sizeof(void *) == 8 && BLKGETSIZE64 == 0x80081272) ||
|
|
|
|
|
(sizeof(void *) == 4 && BLKGETSIZE64 == 0x80041272));
|
2023-06-07 16:49:40 -07:00
|
|
|
ALLOW_MOVE_ONLY(mmap_data)
|
2023-05-20 14:19:40 -07:00
|
|
|
|
2023-06-06 17:11:42 -07:00
|
|
|
explicit mmap_data(const char *name, bool rw = false);
|
2024-07-30 04:00:12 -07:00
|
|
|
mmap_data(int dirfd, const char *name, bool rw = false);
|
2023-06-12 01:07:43 -07:00
|
|
|
mmap_data(int fd, size_t sz, bool rw = false);
|
2023-06-06 17:11:42 -07:00
|
|
|
~mmap_data();
|
2020-04-01 04:39:28 -07:00
|
|
|
};
|
|
|
|
|
|
2022-09-15 01:17:05 -07:00
|
|
|
extern "C" {
|
|
|
|
|
|
2022-08-07 04:06:18 -07:00
|
|
|
int mkdirs(const char *path, mode_t mode);
|
2022-10-31 16:35:33 -07:00
|
|
|
ssize_t canonical_path(const char * __restrict__ path, char * __restrict__ buf, size_t bufsiz);
|
2023-06-09 02:00:37 -07:00
|
|
|
bool rm_rf(const char *path);
|
|
|
|
|
bool frm_rf(int dirfd);
|
2024-08-07 23:39:41 +08:00
|
|
|
bool cp_afc(const char *src, const char *dest);
|
|
|
|
|
bool mv_path(const char *src, const char *dest);
|
|
|
|
|
bool link_path(const char *src, const char *dest);
|
|
|
|
|
bool clone_attr(const char *src, const char *dest);
|
|
|
|
|
bool fclone_attr(int src, int dest);
|
2022-09-15 01:17:05 -07:00
|
|
|
|
|
|
|
|
} // extern "C"
|
|
|
|
|
|
2022-10-31 16:35:33 -07:00
|
|
|
static inline ssize_t realpath(
|
|
|
|
|
const char * __restrict__ path, char * __restrict__ buf, size_t bufsiz) {
|
2022-09-21 03:09:46 +02:00
|
|
|
return canonical_path(path, buf, bufsiz);
|
|
|
|
|
}
|
2022-06-17 02:36:04 -07:00
|
|
|
void full_read(int fd, std::string &str);
|
Introduce new sepolicy injection mechanism
In the current implementation, Magisk will either have to recreate
all early mount implementation (for legacy SAR and rootfs devices) or
delegate early mount to first stage init (for 2SI devices) to access
required partitions for loading sepolicy. It then has to recreate the
split sepolicy loading implementation in-house, apply patches, then
dump the compiled + patched policies into monolithic format somewhere.
Finally, it patches the original init to force it to load the sepolicy
file we just created.
With the increasing complexity involved in early mount and split
sepolicy (there is even APEX module involved in the future!),
it is about time to rethink Magisk's sepolicy strategy as rebuilding
init's functionality is not scalable and easy to maintain.
In this commit, instead of building sepolicy ourselves, we mock
selinuxfs with FIFO files connected to a pre-init daemon, waiting
for the actual init process to directly write the sepolicy file into
MagiskInit. We then patch the file and load it into the kernel. Some
FIFO tricks has to be used to hijack the original init process's
control flow and prevent race conditions, details are directly in the
comments in code.
At the moment, only system-as-root (read-only root) support is added.
Support for legacy rootfs devices will come with a follow up commit.
2022-03-16 00:31:53 -07:00
|
|
|
void full_read(const char *filename, std::string &str);
|
2022-06-17 02:36:04 -07:00
|
|
|
std::string full_read(int fd);
|
2020-04-25 23:19:36 -07:00
|
|
|
std::string full_read(const char *filename);
|
2019-07-01 22:58:19 -07:00
|
|
|
void write_zero(int fd, size_t size);
|
2023-03-16 04:07:00 -07:00
|
|
|
std::string resolve_preinit_dir(const char *base_dir);
|
2022-04-08 18:03:58 +08:00
|
|
|
|
2025-08-24 15:13:56 -07:00
|
|
|
// Functor = function<bool(string_view)>
|
|
|
|
|
template <typename Functor>
|
|
|
|
|
void file_readline(int fd, Functor &&fn) {
|
|
|
|
|
file_readline_rs(fd, [&](rust::String &line) -> bool {
|
|
|
|
|
return fn(std::string_view(line.c_str(), line.size()));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functor = function<bool(string_view, string_view)>
|
|
|
|
|
template <typename Functor>
|
|
|
|
|
void parse_prop_file(const char *file, Functor &&fn) {
|
|
|
|
|
parse_prop_file_rs(file, [&](rust::Str key, rust::Str val) -> bool {
|
|
|
|
|
// Null terminate all strings
|
|
|
|
|
*(const_cast<char *>(key.data()) + key.size()) = '\0';
|
|
|
|
|
*(const_cast<char *>(val.data()) + val.size()) = '\0';
|
|
|
|
|
return fn(std::string_view(key.data(), key.size()), std::string_view(val.data(), val.size()));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-13 00:37:06 -05:00
|
|
|
using sFILE = std::unique_ptr<FILE, decltype(&fclose)>;
|
|
|
|
|
using sDIR = std::unique_ptr<DIR, decltype(&closedir)>;
|
2020-12-03 20:15:18 -08:00
|
|
|
sDIR make_dir(DIR *dp);
|
|
|
|
|
sFILE make_file(FILE *fp);
|
2019-12-13 00:37:06 -05:00
|
|
|
|
|
|
|
|
static inline sDIR open_dir(const char *path) {
|
2020-12-30 22:11:24 -08:00
|
|
|
return make_dir(opendir(path));
|
2019-12-13 00:37:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline sDIR xopen_dir(const char *path) {
|
2020-12-30 22:11:24 -08:00
|
|
|
return make_dir(xopendir(path));
|
2019-12-13 00:37:06 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-01 04:39:28 -07:00
|
|
|
static inline sDIR xopen_dir(int dirfd) {
|
2020-12-30 22:11:24 -08:00
|
|
|
return make_dir(xfdopendir(dirfd));
|
2020-04-01 04:39:28 -07:00
|
|
|
}
|
|
|
|
|
|
2019-12-13 00:37:06 -05:00
|
|
|
static inline sFILE open_file(const char *path, const char *mode) {
|
2020-12-30 22:11:24 -08:00
|
|
|
return make_file(fopen(path, mode));
|
2019-12-13 00:37:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline sFILE xopen_file(const char *path, const char *mode) {
|
2020-12-30 22:11:24 -08:00
|
|
|
return make_file(xfopen(path, mode));
|
2019-12-13 00:37:06 -05:00
|
|
|
}
|
2020-06-19 03:52:25 -07:00
|
|
|
|
|
|
|
|
static inline sFILE xopen_file(int fd, const char *mode) {
|
2020-12-30 22:11:24 -08:00
|
|
|
return make_file(xfdopen(fd, mode));
|
2020-06-19 03:52:25 -07:00
|
|
|
}
|