diff --git a/native/src/external/crt0 b/native/src/external/crt0 index 49bbd472a..2b990d807 160000 --- a/native/src/external/crt0 +++ b/native/src/external/crt0 @@ -1 +1 @@ -Subproject commit 49bbd472a250e75315a9183586fab1e0c0982618 +Subproject commit 2b990d8076a6a3e7b9f32bbe663bdeac976d3430 diff --git a/native/src/sepolicy/policydb.cpp b/native/src/sepolicy/policydb.cpp index ff9bcf075..99cc95e3c 100644 --- a/native/src/sepolicy/policydb.cpp +++ b/native/src/sepolicy/policydb.cpp @@ -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 *>(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 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; }