Restrict pointer aliasing

Close #6354, close #6353
This commit is contained in:
topjohnwu 2022-10-31 16:35:33 -07:00
parent 1e53a5555e
commit 44643ad7b3
5 changed files with 16 additions and 14 deletions

View File

@ -61,8 +61,7 @@ struct mmap_data : public byte_data {
extern "C" { extern "C" {
int mkdirs(const char *path, mode_t mode); int mkdirs(const char *path, mode_t mode);
ssize_t canonical_path(const char *path, char *buf, size_t bufsiz); ssize_t canonical_path(const char * __restrict__ path, char * __restrict__ buf, size_t bufsiz);
ssize_t read_link(const char *pathname, char *buf, size_t bufsiz);
} // extern "C" } // extern "C"
@ -74,7 +73,8 @@ void mv_dir(int src, int dest);
void cp_afc(const char *src, const char *dest); void cp_afc(const char *src, const char *dest);
void link_path(const char *src, const char *dest); void link_path(const char *src, const char *dest);
void link_dir(int src, int dest); void link_dir(int src, int dest);
static inline ssize_t realpath(const char *path, char *buf, size_t bufsiz) { static inline ssize_t realpath(
const char * __restrict__ path, char * __restrict__ buf, size_t bufsiz) {
return canonical_path(path, buf, bufsiz); return canonical_path(path, buf, bufsiz);
} }
int getattr(const char *path, file_attr *a); int getattr(const char *path, file_attr *a);

View File

@ -12,8 +12,7 @@ pub mod unsafe_impl {
use crate::slice_from_ptr_mut; use crate::slice_from_ptr_mut;
#[no_mangle] pub unsafe fn readlink(path: *const c_char, buf: *mut u8, bufsz: usize) -> isize {
pub unsafe extern "C" fn read_link(path: *const c_char, buf: *mut u8, bufsz: usize) -> isize {
let r = libc::readlink(path, buf.cast(), bufsz - 1); let r = libc::readlink(path, buf.cast(), bufsz - 1);
if r >= 0 { if r >= 0 {
*buf.offset(r) = b'\0'; *buf.offset(r) = b'\0';
@ -68,7 +67,7 @@ macro_rules! xopen_fd {
} }
pub fn readlink(path: &CStr, data: &mut [u8]) -> isize { pub fn readlink(path: &CStr, data: &mut [u8]) -> isize {
unsafe { unsafe_impl::read_link(path.as_ptr(), data.as_mut_ptr(), data.len()) } unsafe { unsafe_impl::readlink(path.as_ptr(), data.as_mut_ptr(), data.len()) }
} }
pub fn fd_path(fd: RawFd, buf: &mut [u8]) -> isize { pub fn fd_path(fd: RawFd, buf: &mut [u8]) -> isize {

View File

@ -40,8 +40,9 @@ int xfstatat(int dirfd, const char *pathname, struct stat *buf, int flags);
int xdup(int fd); int xdup(int fd);
int xdup2(int oldfd, int newfd); int xdup2(int oldfd, int newfd);
int xdup3(int oldfd, int newfd, int flags); int xdup3(int oldfd, int newfd, int flags);
ssize_t xreadlink(const char *pathname, char *buf, size_t bufsiz); ssize_t xreadlink(const char * __restrict__ pathname, char * __restrict__ buf, size_t bufsiz);
ssize_t xreadlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz); ssize_t xreadlinkat(
int dirfd, const char * __restrict__ pathname, char * __restrict__ buf, size_t bufsiz);
int xsymlink(const char *target, const char *linkpath); int xsymlink(const char *target, const char *linkpath);
int xsymlinkat(const char *target, int newdirfd, const char *linkpath); int xsymlinkat(const char *target, int newdirfd, const char *linkpath);
int xlinkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags); int xlinkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags);
@ -58,7 +59,7 @@ void *xmmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset
ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count); ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count);
pid_t xfork(); pid_t xfork();
int xpoll(pollfd *fds, nfds_t nfds, int timeout); int xpoll(pollfd *fds, nfds_t nfds, int timeout);
ssize_t xrealpath(const char *path, char *buf, size_t bufsiz); ssize_t xrealpath(const char * __restrict__ path, char * __restrict__ buf, size_t bufsiz);
int xmknod(const char * pathname, mode_t mode, dev_t dev); int xmknod(const char * pathname, mode_t mode, dev_t dev);
} // extern "C" } // extern "C"

View File

@ -16,7 +16,7 @@ mod unsafe_impl {
use cfg_if::cfg_if; use cfg_if::cfg_if;
use libc::{c_char, nfds_t, off_t, pollfd}; use libc::{c_char, nfds_t, off_t, pollfd};
use crate::unsafe_impl::read_link; use crate::unsafe_impl::readlink;
use crate::{perror, ptr_to_str, slice_from_ptr, slice_from_ptr_mut}; use crate::{perror, ptr_to_str, slice_from_ptr, slice_from_ptr_mut};
#[no_mangle] #[no_mangle]
@ -41,7 +41,7 @@ mod unsafe_impl {
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xreadlink(path: *const c_char, buf: *mut u8, bufsz: usize) -> isize { pub unsafe extern "C" fn xreadlink(path: *const c_char, buf: *mut u8, bufsz: usize) -> isize {
let r = read_link(path, buf, bufsz); let r = readlink(path, buf, bufsz);
if r < 0 { if r < 0 {
perror!("readlink"); perror!("readlink");
} }

View File

@ -161,8 +161,10 @@ static vector<int> get_module_fds(bool is_64_bit) {
} }
static bool get_exe(int pid, char *buf, size_t sz) { static bool get_exe(int pid, char *buf, size_t sz) {
ssprintf(buf, sz, "/proc/%d/exe", pid); char exe[128];
return xreadlink(buf, buf, sz) > 0; if (ssprintf(exe, sizeof(exe), "/proc/%d/exe", pid) < 0)
return false;
return xreadlink(exe, buf, sz) > 0;
} }
static pthread_mutex_t zygiskd_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t zygiskd_lock = PTHREAD_MUTEX_INITIALIZER;