mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-04-01 05:52:31 +00:00
Make SELinux support a feature
This commit is contained in:
parent
66788dc58c
commit
4b8a0388e7
8
build.py
8
build.py
@ -270,10 +270,7 @@ def run_cargo_build(args):
|
|||||||
return
|
return
|
||||||
|
|
||||||
# Start building the actual build commands
|
# Start building the actual build commands
|
||||||
cmds = ["build"]
|
cmds = ["build", "-p", ""]
|
||||||
for target in targets:
|
|
||||||
cmds.append("-p")
|
|
||||||
cmds.append(target)
|
|
||||||
rust_out = "debug"
|
rust_out = "debug"
|
||||||
if args.release:
|
if args.release:
|
||||||
cmds.append("-r")
|
cmds.append("-r")
|
||||||
@ -289,6 +286,9 @@ def run_cargo_build(args):
|
|||||||
"thumbv7neon-linux-androideabi" if triple.startswith("armv7") else triple
|
"thumbv7neon-linux-androideabi" if triple.startswith("armv7") else triple
|
||||||
)
|
)
|
||||||
cmds[-1] = rust_triple
|
cmds[-1] = rust_triple
|
||||||
|
|
||||||
|
for target in targets:
|
||||||
|
cmds[2] = target
|
||||||
proc = run_cargo(cmds, triple)
|
proc = run_cargo(cmds, triple)
|
||||||
if proc.returncode != 0:
|
if proc.returncode != 0:
|
||||||
error("Build binary failed!")
|
error("Build binary failed!")
|
||||||
|
@ -6,6 +6,10 @@ edition = "2021"
|
|||||||
[lib]
|
[lib]
|
||||||
path = "lib.rs"
|
path = "lib.rs"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
selinux = []
|
||||||
|
dyn_selinux = []
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
cxx-gen = { workspace = true }
|
cxx-gen = { workspace = true }
|
||||||
|
|
||||||
|
@ -8,7 +8,6 @@ use std::os::android::fs::MetadataExt;
|
|||||||
use std::os::fd::{AsFd, BorrowedFd, IntoRawFd};
|
use std::os::fd::{AsFd, BorrowedFd, IntoRawFd};
|
||||||
use std::os::unix::fs::FileTypeExt;
|
use std::os::unix::fs::FileTypeExt;
|
||||||
use std::os::unix::io::{AsRawFd, FromRawFd, OwnedFd, RawFd};
|
use std::os::unix::io::{AsRawFd, FromRawFd, OwnedFd, RawFd};
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
|
||||||
use std::{io, mem, ptr, slice};
|
use std::{io, mem, ptr, slice};
|
||||||
|
|
||||||
use bytemuck::{bytes_of_mut, Pod};
|
use bytemuck::{bytes_of_mut, Pod};
|
||||||
@ -140,15 +139,22 @@ impl<T: Write> WriteExt for T {
|
|||||||
|
|
||||||
pub struct FileAttr {
|
pub struct FileAttr {
|
||||||
pub st: libc::stat,
|
pub st: libc::stat,
|
||||||
|
#[cfg(feature = "selinux")]
|
||||||
pub con: Utf8CStrBufArr<128>,
|
pub con: Utf8CStrBufArr<128>,
|
||||||
}
|
}
|
||||||
|
|
||||||
const XATTR_NAME_SELINUX: &[u8] = b"security.selinux\0";
|
impl FileAttr {
|
||||||
static SELINUX_ENABLED: AtomicBool = AtomicBool::new(false);
|
fn new() -> Self {
|
||||||
|
FileAttr {
|
||||||
pub fn enable_selinux() {
|
st: unsafe { mem::zeroed() },
|
||||||
SELINUX_ENABLED.store(true, Ordering::Relaxed);
|
#[cfg(feature = "selinux")]
|
||||||
|
con: Utf8CStrBufArr::new(),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "selinux")]
|
||||||
|
const XATTR_NAME_SELINUX: &[u8] = b"security.selinux\0";
|
||||||
|
|
||||||
pub struct DirEntry<'a> {
|
pub struct DirEntry<'a> {
|
||||||
dir: &'a Directory,
|
dir: &'a Directory,
|
||||||
@ -625,14 +631,12 @@ impl FsPath {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_attr(&self) -> io::Result<FileAttr> {
|
pub fn get_attr(&self) -> io::Result<FileAttr> {
|
||||||
let mut attr: FileAttr;
|
let mut attr = FileAttr::new();
|
||||||
unsafe {
|
unsafe {
|
||||||
attr = FileAttr {
|
|
||||||
st: mem::zeroed(),
|
|
||||||
con: Utf8CStrBufArr::new(),
|
|
||||||
};
|
|
||||||
libc::lstat(self.as_ptr(), &mut attr.st).as_os_err()?;
|
libc::lstat(self.as_ptr(), &mut attr.st).as_os_err()?;
|
||||||
if SELINUX_ENABLED.load(Ordering::Relaxed) {
|
|
||||||
|
#[cfg(feature = "selinux")]
|
||||||
|
{
|
||||||
let sz = libc::lgetxattr(
|
let sz = libc::lgetxattr(
|
||||||
self.as_ptr(),
|
self.as_ptr(),
|
||||||
XATTR_NAME_SELINUX.as_ptr().cast(),
|
XATTR_NAME_SELINUX.as_ptr().cast(),
|
||||||
@ -652,6 +656,8 @@ impl FsPath {
|
|||||||
libc::chmod(self.as_ptr(), (attr.st.st_mode & 0o777).as_()).as_os_err()?;
|
libc::chmod(self.as_ptr(), (attr.st.st_mode & 0o777).as_()).as_os_err()?;
|
||||||
}
|
}
|
||||||
libc::lchown(self.as_ptr(), attr.st.st_uid, attr.st.st_gid).as_os_err()?;
|
libc::lchown(self.as_ptr(), attr.st.st_uid, attr.st.st_gid).as_os_err()?;
|
||||||
|
|
||||||
|
#[cfg(feature = "selinux")]
|
||||||
if !attr.con.is_empty() {
|
if !attr.con.is_empty() {
|
||||||
libc::lsetxattr(
|
libc::lsetxattr(
|
||||||
self.as_ptr(),
|
self.as_ptr(),
|
||||||
@ -721,14 +727,12 @@ impl FsPath {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn fd_get_attr(fd: RawFd) -> io::Result<FileAttr> {
|
pub fn fd_get_attr(fd: RawFd) -> io::Result<FileAttr> {
|
||||||
let mut attr: FileAttr;
|
let mut attr = FileAttr::new();
|
||||||
unsafe {
|
unsafe {
|
||||||
attr = FileAttr {
|
|
||||||
st: mem::zeroed(),
|
|
||||||
con: Utf8CStrBufArr::new(),
|
|
||||||
};
|
|
||||||
libc::fstat(fd, &mut attr.st).as_os_err()?;
|
libc::fstat(fd, &mut attr.st).as_os_err()?;
|
||||||
if SELINUX_ENABLED.load(Ordering::Relaxed) {
|
|
||||||
|
#[cfg(feature = "selinux")]
|
||||||
|
{
|
||||||
let sz = libc::fgetxattr(
|
let sz = libc::fgetxattr(
|
||||||
fd,
|
fd,
|
||||||
XATTR_NAME_SELINUX.as_ptr().cast(),
|
XATTR_NAME_SELINUX.as_ptr().cast(),
|
||||||
@ -746,6 +750,8 @@ pub fn fd_set_attr(fd: RawFd, attr: &FileAttr) -> io::Result<()> {
|
|||||||
unsafe {
|
unsafe {
|
||||||
libc::fchmod(fd, (attr.st.st_mode & 0o777).as_()).as_os_err()?;
|
libc::fchmod(fd, (attr.st.st_mode & 0o777).as_()).as_os_err()?;
|
||||||
libc::fchown(fd, attr.st.st_uid, attr.st.st_gid).as_os_err()?;
|
libc::fchown(fd, attr.st.st_uid, attr.st.st_gid).as_os_err()?;
|
||||||
|
|
||||||
|
#[cfg(feature = "selinux")]
|
||||||
if !attr.con.is_empty() {
|
if !attr.con.is_empty() {
|
||||||
libc::fsetxattr(
|
libc::fsetxattr(
|
||||||
fd,
|
fd,
|
||||||
|
@ -44,7 +44,6 @@ pub mod ffi {
|
|||||||
fn set_log_level_state_cxx(level: LogLevelCxx, enabled: bool);
|
fn set_log_level_state_cxx(level: LogLevelCxx, enabled: bool);
|
||||||
fn exit_on_error(b: bool);
|
fn exit_on_error(b: bool);
|
||||||
fn cmdline_logging();
|
fn cmdline_logging();
|
||||||
fn enable_selinux();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[namespace = "rust"]
|
#[namespace = "rust"]
|
||||||
|
@ -12,7 +12,7 @@ cxx-gen = { workspace = true }
|
|||||||
pb-rs = { workspace = true }
|
pb-rs = { workspace = true }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
base = { path = "../base" }
|
base = { path = "../base", features = ["selinux"] }
|
||||||
cxx = { workspace = true }
|
cxx = { workspace = true }
|
||||||
num-traits = { workspace = true }
|
num-traits = { workspace = true }
|
||||||
num-derive = { workspace = true }
|
num-derive = { workspace = true }
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc < 1)
|
if (argc < 1)
|
||||||
return 1;
|
return 1;
|
||||||
enable_selinux();
|
|
||||||
cmdline_logging();
|
cmdline_logging();
|
||||||
init_argv0(argc, argv);
|
init_argv0(argc, argv);
|
||||||
umask(0);
|
umask(0);
|
||||||
|
@ -26,7 +26,6 @@ int main(int argc, char *argv[]) {
|
|||||||
if (argc < 1)
|
if (argc < 1)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
enable_selinux();
|
|
||||||
cmdline_logging();
|
cmdline_logging();
|
||||||
init_argv0(argc, argv);
|
init_argv0(argc, argv);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user