Restructure native codebase

This commit is contained in:
topjohnwu
2022-05-12 02:03:42 -07:00
parent 7505599ea0
commit 622e09862a
78 changed files with 102 additions and 98 deletions

View File

@@ -0,0 +1,7 @@
#pragma once
#include "../missing.hpp"
#include "../xwrap.hpp"
#include "../files.hpp"
#include "../misc.hpp"
#include "../logging.hpp"

View File

@@ -0,0 +1,41 @@
#pragma once
// selinuxfs paths
#define SELINUX_MNT "/sys/fs/selinux"
#define SELINUX_ENFORCE SELINUX_MNT "/enforce"
#define SELINUX_POLICY SELINUX_MNT "/policy"
#define SELINUX_LOAD SELINUX_MNT "/load"
#define SELINUX_CONTEXT SELINUX_MNT "/context"
#define SELINUX_VERSION SELINUX_MNT "/policyvers"
// sepolicy paths
#define PLAT_POLICY_DIR "/system/etc/selinux/"
#define VEND_POLICY_DIR "/vendor/etc/selinux/"
#define PROD_POLICY_DIR "/product/etc/selinux/"
#define ODM_POLICY_DIR "/odm/etc/selinux/"
#define SYSEXT_POLICY_DIR "/system_ext/etc/selinux/"
#define SPLIT_PLAT_CIL PLAT_POLICY_DIR "plat_sepolicy.cil"
// Unconstrained domain the daemon and root processes run in
#define SEPOL_PROC_DOMAIN "magisk"
// Highly constrained domain, sole purpose is to connect to daemon
#define SEPOL_CLIENT_DOMAIN "magisk_client"
// Unconstrained file type that anyone can access
#define SEPOL_FILE_TYPE "magisk_file"
// Special file type to allow clients to transit to client domain automatically
#define SEPOL_EXEC_TYPE "magisk_exec"
extern void (*freecon)(char *con);
extern int (*setcon)(const char *con);
extern int (*getfilecon)(const char *path, char **con);
extern int (*lgetfilecon)(const char *path, char **con);
extern int (*fgetfilecon)(int fd, char **con);
extern int (*setfilecon)(const char *path, const char *con);
extern int (*lsetfilecon)(const char *path, const char *con);
extern int (*fsetfilecon)(int fd, const char *con);
void getfilecon_at(int dirfd, const char *name, char **con);
void setfilecon_at(int dirfd, const char *name, const char *con);
void enable_selinux();
void restorecon();
void restore_tmpcon();

View File

@@ -0,0 +1,133 @@
#pragma once
#include <sys/uio.h>
#include <cstdio>
#include <memory>
#include "../files.hpp"
class stream {
public:
virtual ssize_t read(void *buf, size_t len);
virtual ssize_t readFully(void *buf, size_t len);
virtual ssize_t readv(const iovec *iov, int iovcnt);
virtual bool write(const void *buf, size_t len);
virtual ssize_t writev(const iovec *iov, int iovcnt);
virtual off_t seek(off_t off, int whence);
virtual ~stream() = default;
};
using stream_ptr = std::unique_ptr<stream>;
// Delegates all operations to base stream
class filter_stream : public stream {
public:
filter_stream(stream_ptr &&base) : base(std::move(base)) {}
ssize_t read(void *buf, size_t len) override;
bool write(const void *buf, size_t len) override;
virtual bool write(const void *buf, size_t len, bool final);
// Seeking while filtering does not make sense
off_t seek(off_t off, int whence) final { return stream::seek(off, whence); }
protected:
stream_ptr base;
};
using filter_strm_ptr = std::unique_ptr<filter_stream>;
// Buffered output stream, writing in chunks
class chunk_out_stream : public filter_stream {
public:
chunk_out_stream(stream_ptr &&base, size_t buf_sz, size_t chunk_sz)
: filter_stream(std::move(base)), chunk_sz(chunk_sz), buf_sz(buf_sz) {}
chunk_out_stream(stream_ptr &&base, size_t buf_sz = 4096)
: chunk_out_stream(std::move(base), buf_sz, buf_sz) {}
~chunk_out_stream() override { delete[] _buf; }
// Reading does not make sense
ssize_t read(void *buf, size_t len) final { return stream::read(buf, len); }
bool write(const void *buf, size_t len) final;
bool write(const void *buf, size_t len, bool final) final;
protected:
// Classes inheriting this class has to call finalize() in its destructor
void finalize();
virtual bool write_chunk(const void *buf, size_t len, bool final) = 0;
size_t chunk_sz;
private:
size_t buf_sz;
size_t buf_off = 0;
uint8_t *_buf = nullptr;
};
// Byte stream that dynamically allocates memory
class byte_stream : public stream {
public:
byte_stream(uint8_t *&buf, size_t &len);
template <class Byte>
byte_stream(Byte *&buf, size_t &len) : byte_stream(reinterpret_cast<uint8_t *&>(buf), len) {}
ssize_t read(void *buf, size_t len) override;
bool write(const void *buf, size_t len) override;
off_t seek(off_t off, int whence) override;
private:
uint8_t *&_buf;
size_t &_len;
size_t _pos = 0;
size_t _cap = 0;
void resize(size_t new_pos, bool zero = false);
};
class file_stream : public stream {
public:
bool write(const void *buf, size_t len) final;
protected:
virtual ssize_t do_write(const void *buf, size_t len) = 0;
};
// File stream but does not close the file descriptor at any time
class fd_stream : public file_stream {
public:
fd_stream(int fd) : fd(fd) {}
ssize_t read(void *buf, size_t len) override;
ssize_t readv(const iovec *iov, int iovcnt) override;
ssize_t writev(const iovec *iov, int iovcnt) override;
off_t seek(off_t off, int whence) override;
protected:
ssize_t do_write(const void *buf, size_t len) override;
private:
int fd;
};
/* ****************************************
* Bridge between stream class and C stdio
* ****************************************/
// sFILE -> stream_ptr
class fp_stream final : public file_stream {
public:
fp_stream(FILE *fp = nullptr) : fp(fp, fclose) {}
fp_stream(sFILE &&fp) : fp(std::move(fp)) {}
ssize_t read(void *buf, size_t len) override;
off_t seek(off_t off, int whence) override;
protected:
ssize_t do_write(const void *buf, size_t len) override;
private:
sFILE fp;
};
// stream_ptr -> sFILE
sFILE make_stream_fp(stream_ptr &&strm);
template <class T, class... Args>
sFILE make_stream_fp(Args &&... args) {
return make_stream_fp(stream_ptr(new T(std::forward<Args>(args)...)));
}