mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-04 19:10:47 -05:00
sepolicy: switch to open_memstream
Replace BSD funopen with POSIX open_memstream when dumping the policy image into memory. Also update the crt0 submodule which implements open_memstream and fixes duplicate mmap64 definitions on 32-bit targets. Assisted-by: Gemini 3.8 Flash
This commit is contained in:
Vendored
+1
-1
Submodule native/src/external/crt0 updated: 49bbd472a2...2b990d8076
@@ -230,19 +230,14 @@ sepol_impl::~sepol_impl() {
|
||||
free(db);
|
||||
}
|
||||
|
||||
static int vec_write(void *v, const char *buf, int len) {
|
||||
auto vec = static_cast<vector<char> *>(v);
|
||||
vec->insert(vec->end(), buf, buf + len);
|
||||
return len;
|
||||
}
|
||||
|
||||
bool SePolicy::to_file(::Utf8CStr file) const noexcept {
|
||||
// No partial writes are allowed to /sys/fs/selinux/load, thus the reason why we
|
||||
// first dump everything into memory, then directly call write system call
|
||||
vector<char> out;
|
||||
FILE *fp = funopen(&out, nullptr, vec_write, nullptr, nullptr);
|
||||
// Since we're directly writing to memory, disable buffering
|
||||
setbuf(fp, nullptr);
|
||||
char *buf = nullptr;
|
||||
size_t len = 0;
|
||||
FILE *fp = open_memstream(&buf, &len);
|
||||
if (!fp)
|
||||
return false;
|
||||
|
||||
policy_file_t pf;
|
||||
policy_file_init(&pf);
|
||||
@@ -251,18 +246,22 @@ bool SePolicy::to_file(::Utf8CStr file) const noexcept {
|
||||
if (policydb_write(impl->db, &pf)) {
|
||||
LOGE("Fail to create policy image\n");
|
||||
fclose(fp);
|
||||
free(buf);
|
||||
return false;
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
int fd = xopen(file.data(), O_WRONLY | O_CREAT | O_CLOEXEC, 0644);
|
||||
if (fd < 0)
|
||||
if (fd < 0) {
|
||||
free(buf);
|
||||
return false;
|
||||
}
|
||||
if (struct stat st{}; xfstat(fd, &st) == 0 && st.st_size > 0) {
|
||||
ftruncate(fd, 0);
|
||||
}
|
||||
xwrite(fd, out.data(), out.size());
|
||||
xwrite(fd, buf, len);
|
||||
|
||||
close(fd);
|
||||
free(buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user