Skip to content

Commit c3bd85c

Browse files
committed
add hook
1 parent 9e26834 commit c3bd85c

File tree

9 files changed

+211
-73
lines changed

9 files changed

+211
-73
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,6 @@ nanosleep hooked
168168
- [ ] open
169169
- [ ] chdir
170170
- [ ] chroot
171-
- [ ] mkdir
172-
- [ ] rmdir
173-
- [ ] link
174-
- [ ] unlink
175171
- [ ] readlink
176172
- [ ] stat
177173
- [ ] dup

core/src/common/constants.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,13 @@ pub enum Syscall {
107107
renameat,
108108
#[cfg(target_os = "linux")]
109109
renameat2,
110+
mkdir,
110111
mkdirat,
112+
rmdir,
113+
lseek,
111114
openat,
115+
link,
116+
unlink,
112117
pthread_cond_timedwait,
113118
pthread_mutex_trylock,
114119
pthread_mutex_lock,

core/src/syscall/unix/link.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use std::ffi::{c_char, c_int};
2+
use once_cell::sync::Lazy;
3+
4+
#[must_use]
5+
pub extern "C" fn link(
6+
fn_ptr: Option<&extern "C" fn(*const c_char, *const c_char) -> c_int>,
7+
src: *const c_char,
8+
dst: *const c_char
9+
) -> c_int{
10+
static CHAIN: Lazy<LinkSyscallFacade<RawLinkSyscall>> = Lazy::new(Default::default);
11+
CHAIN.link(fn_ptr, src, dst)
12+
}
13+
14+
trait LinkSyscall {
15+
extern "C" fn link(
16+
&self,
17+
fn_ptr: Option<&extern "C" fn(*const c_char, *const c_char) -> c_int>,
18+
src: *const c_char,
19+
dst: *const c_char
20+
) -> c_int;
21+
}
22+
23+
impl_facade!(LinkSyscallFacade, LinkSyscall,
24+
link(src: *const c_char, dst: *const c_char) -> c_int
25+
);
26+
27+
impl_raw!(RawLinkSyscall, LinkSyscall,
28+
link(src: *const c_char, dst: *const c_char) -> c_int
29+
);

core/src/syscall/unix/lseek.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use libc::off_t;
2+
use std::ffi::c_int;
3+
use once_cell::sync::Lazy;
4+
5+
#[must_use]
6+
pub extern "C" fn lseek(
7+
fn_ptr: Option<&extern "C" fn(c_int, off_t, c_int) -> off_t>,
8+
fd: c_int,
9+
offset: off_t,
10+
whence: c_int,
11+
) -> off_t{
12+
static CHAIN: Lazy<LseekSyscallFacade<RawLseekSyscall>> = Lazy::new(Default::default);
13+
CHAIN.lseek(fn_ptr, fd, offset,whence)
14+
}
15+
16+
trait LseekSyscall {
17+
extern "C" fn lseek(
18+
&self,
19+
fn_ptr: Option<&extern "C" fn(c_int, off_t, c_int) -> off_t>,
20+
fd: c_int,
21+
offset: off_t,
22+
whence: c_int,
23+
) -> off_t;
24+
}
25+
26+
impl_facade!(LseekSyscallFacade, LseekSyscall,
27+
lseek(fd: c_int, offset: off_t, whence: c_int) -> off_t
28+
);
29+
30+
impl_raw!(RawLseekSyscall, LseekSyscall,
31+
lseek(fd: c_int, offset: off_t, whence: c_int) -> off_t
32+
);

core/src/syscall/unix/mkdir.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use libc::mode_t;
2+
use once_cell::sync::Lazy;
3+
use std::ffi::{c_char, c_int};
4+
5+
#[must_use]
6+
pub extern "C" fn mkdir(
7+
fn_ptr: Option<&extern "C" fn(*const c_char, mode_t) -> c_int>,
8+
path: *const c_char,
9+
mode: mode_t,
10+
) -> c_int {
11+
static CHAIN: Lazy<MkdirSyscallFacade<RawMkdirSyscall>> = Lazy::new(Default::default);
12+
CHAIN.mkdir(fn_ptr, path, mode)
13+
}
14+
15+
trait MkdirSyscall {
16+
extern "C" fn mkdir(
17+
&self,
18+
fn_ptr: Option<&extern "C" fn(*const c_char, mode_t) -> c_int>,
19+
path: *const c_char,
20+
mode: mode_t,
21+
) -> c_int;
22+
}
23+
24+
impl_facade!(MkdirSyscallFacade, MkdirSyscall,
25+
mkdir(path: *const c_char, mode: mode_t) -> c_int
26+
);
27+
28+
impl_raw!(RawMkdirSyscall, MkdirSyscall,
29+
mkdir(path: *const c_char, mode: mode_t) -> c_int
30+
);

core/src/syscall/unix/mod.rs

Lines changed: 45 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,13 @@
11
use std::ffi::c_int;
22

3-
pub use accept::accept;
4-
#[cfg(target_os = "linux")]
5-
pub use accept4::accept4;
6-
pub use close::close;
7-
pub use connect::connect;
8-
pub use listen::listen;
9-
pub use nanosleep::nanosleep;
10-
pub use poll::poll;
11-
pub use pread::pread;
12-
pub use preadv::preadv;
13-
pub use pthread_cond_timedwait::pthread_cond_timedwait;
14-
pub use pthread_mutex_lock::pthread_mutex_lock;
15-
pub use pthread_mutex_trylock::pthread_mutex_trylock;
16-
pub use pthread_mutex_unlock::pthread_mutex_unlock;
17-
pub use pwrite::pwrite;
18-
pub use pwritev::pwritev;
19-
pub use read::read;
20-
pub use readv::readv;
21-
pub use recv::recv;
22-
pub use recvfrom::recvfrom;
23-
pub use recvmsg::recvmsg;
24-
pub use select::select;
25-
pub use send::send;
26-
pub use sendmsg::sendmsg;
27-
pub use sendto::sendto;
28-
pub use shutdown::shutdown;
29-
pub use sleep::sleep;
30-
pub use socket::socket;
31-
pub use usleep::usleep;
32-
pub use write::write;
33-
pub use writev::writev;
3+
macro_rules! syscall_mod {
4+
($($mod_name: ident);*) => {
5+
$(
6+
pub use $mod_name::$mod_name;
7+
mod $mod_name;
8+
)*
9+
}
10+
}
3411

3512
macro_rules! impl_facade {
3613
( $struct_name:ident, $trait_name: ident, $syscall: ident($($arg: ident : $arg_type: ty),*) -> $result: ty ) => {
@@ -543,37 +520,44 @@ macro_rules! impl_raw {
543520
}
544521
}
545522

546-
mod accept;
547523
#[cfg(target_os = "linux")]
548-
mod accept4;
549-
mod close;
550-
mod connect;
551-
mod listen;
552-
mod nanosleep;
553-
mod poll;
554-
mod pread;
555-
mod preadv;
556-
mod pthread_cond_timedwait;
557-
mod pthread_mutex_lock;
558-
mod pthread_mutex_trylock;
559-
mod pthread_mutex_unlock;
560-
mod pwrite;
561-
mod pwritev;
562-
mod read;
563-
mod readv;
564-
mod recv;
565-
mod recvfrom;
566-
mod recvmsg;
567-
mod select;
568-
mod send;
569-
mod sendmsg;
570-
mod sendto;
571-
mod shutdown;
572-
mod sleep;
573-
mod socket;
574-
mod usleep;
575-
mod write;
576-
mod writev;
524+
syscall_mod!(accept4);
525+
syscall_mod!(
526+
accept;
527+
close;
528+
connect;
529+
listen;
530+
nanosleep;
531+
poll;
532+
pread;
533+
preadv;
534+
pthread_cond_timedwait;
535+
pthread_mutex_lock;
536+
pthread_mutex_trylock;
537+
pthread_mutex_unlock;
538+
pwrite;
539+
pwritev;
540+
read;
541+
readv;
542+
recv;
543+
recvfrom;
544+
recvmsg;
545+
select;
546+
send;
547+
sendmsg;
548+
sendto;
549+
shutdown;
550+
sleep;
551+
socket;
552+
usleep;
553+
write;
554+
writev;
555+
mkdir;
556+
rmdir;
557+
lseek;
558+
link;
559+
unlink
560+
);
577561

578562
extern "C" {
579563
#[cfg(not(any(target_os = "dragonfly", target_os = "vxworks")))]

core/src/syscall/unix/rmdir.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use once_cell::sync::Lazy;
2+
use std::ffi::{c_char, c_int};
3+
4+
#[must_use]
5+
pub extern "C" fn rmdir(
6+
fn_ptr: Option<&extern "C" fn(*const c_char) -> c_int>,
7+
path: *const c_char,
8+
) -> c_int {
9+
static CHAIN: Lazy<RmdirSyscallFacade<RawRmdirSyscall>> = Lazy::new(Default::default);
10+
CHAIN.rmdir(fn_ptr, path)
11+
}
12+
13+
trait RmdirSyscall {
14+
extern "C" fn rmdir(
15+
&self,
16+
fn_ptr: Option<&extern "C" fn(*const c_char) -> c_int>,
17+
path: *const c_char,
18+
) -> c_int;
19+
}
20+
21+
impl_facade!(RmdirSyscallFacade, RmdirSyscall,
22+
rmdir(path: *const c_char) -> c_int
23+
);
24+
25+
impl_raw!(RawRmdirSyscall, RmdirSyscall,
26+
rmdir(path: *const c_char) -> c_int
27+
);

core/src/syscall/unix/unlink.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::ffi::{c_char, c_int};
2+
use once_cell::sync::Lazy;
3+
4+
#[must_use]
5+
pub extern "C" fn unlink(
6+
fn_ptr: Option<&extern "C" fn(*const c_char) -> c_int>,
7+
src: *const c_char,
8+
) -> c_int{
9+
static CHAIN: Lazy<UnlinkSyscallFacade<RawUnlinkSyscall>> = Lazy::new(Default::default);
10+
CHAIN.unlink(fn_ptr, src)
11+
}
12+
13+
trait LinkSyscall {
14+
extern "C" fn unlink(
15+
&self,
16+
fn_ptr: Option<&extern "C" fn(*const c_char) -> c_int>,
17+
src: *const c_char
18+
) -> c_int;
19+
}
20+
21+
impl_facade!(UnlinkSyscallFacade, LinkSyscall,
22+
unlink(src: *const c_char) -> c_int
23+
);
24+
25+
impl_raw!(RawUnlinkSyscall, LinkSyscall,
26+
unlink(src: *const c_char) -> c_int
27+
);

hook/src/syscall/unix.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use libc::{
2-
fd_set, iovec, msghdr, off_t, pthread_cond_t, pthread_mutex_t, size_t, sockaddr, socklen_t,
3-
ssize_t, timespec, timeval,
2+
fd_set, iovec, mode_t, msghdr, off_t, pthread_cond_t, pthread_mutex_t, size_t, sockaddr,
3+
socklen_t, ssize_t, timespec, timeval,
44
};
5-
use std::ffi::{c_int, c_uint, c_void};
5+
use std::ffi::{c_char, c_int, c_uint, c_void};
66

77
// check https://www.rustwiki.org.cn/en/reference/introduction.html for help information
88
#[allow(unused_macros)]
@@ -34,8 +34,6 @@ macro_rules! impl_hook {
3434
impl_hook!(SLEEP, sleep(secs: c_uint) -> c_uint);
3535
impl_hook!(USLEEP, usleep(microseconds: c_uint) -> c_int);
3636
impl_hook!(NANOSLEEP, nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int);
37-
// NOTE: unhook poll due to mio's poller
38-
// impl_hook!(POLL, poll(fds: *mut pollfd, nfds: nfds_t, timeout: c_int) -> c_int);
3937
impl_hook!(SELECT, select(nfds: c_int, readfds: *mut fd_set, writefds: *mut fd_set, errorfds: *mut fd_set, timeout: *mut timeval) -> c_int);
4038
impl_hook!(SOCKET, socket(domain: c_int, type_: c_int, protocol: c_int) -> c_int);
4139
impl_hook!(CONNECT, connect(fd: c_int, address: *const sockaddr, len: socklen_t) -> c_int);
@@ -51,6 +49,7 @@ impl_hook!(ACCEPT4, accept4(fd: c_int, addr: *mut sockaddr, len: *mut socklen_t,
5149
impl_hook!(SHUTDOWN, shutdown(fd: c_int, how: c_int) -> c_int);
5250
impl_hook!(RECV, recv(fd: c_int, buf: *mut c_void, len: size_t, flags: c_int) -> ssize_t);
5351
impl_hook!(RECVFROM, recvfrom(fd: c_int, buf: *mut c_void, len: size_t, flags: c_int, addr: *mut sockaddr, addrlen: *mut socklen_t) -> ssize_t);
52+
impl_hook!(READ, read(fd: c_int, buf: *mut c_void, count: size_t) -> ssize_t);
5453
impl_hook!(PREAD, pread(fd: c_int, buf: *mut c_void, count: size_t, offset: off_t) -> ssize_t);
5554
impl_hook!(READV, readv(fd: c_int, iov: *const iovec, iovcnt: c_int) -> ssize_t);
5655
impl_hook!(PREADV, preadv(fd: c_int, iov: *const iovec, iovcnt: c_int, offset: off_t) -> ssize_t);
@@ -62,8 +61,17 @@ impl_hook!(WRITEV, writev(fd: c_int, iov: *const iovec, iovcnt: c_int) -> ssize_
6261
impl_hook!(PWRITEV, pwritev(fd: c_int, iov: *const iovec, iovcnt: c_int, offset: off_t) -> ssize_t);
6362
impl_hook!(SENDMSG, sendmsg(fd: c_int, msg: *const msghdr, flags: c_int) -> ssize_t);
6463
impl_hook!(PTHREAD_COND_TIMEDWAIT, pthread_cond_timedwait(cond: *mut pthread_cond_t, lock: *mut pthread_mutex_t, abstime: *const timespec) -> c_int);
65-
// NOTE: unhook pthread_mutex_lock due to stack overflow
66-
// impl_hook!(PTHREAD_MUTEX_LOCK, pthread_mutex_lock(lock: *mut pthread_mutex_t) -> c_int);
6764
impl_hook!(PTHREAD_MUTEX_TRYLOCK, pthread_mutex_trylock(lock: *mut pthread_mutex_t) -> c_int);
68-
// NOTE: unhook pthread_mutex_unlock due to stack overflow
65+
impl_hook!(MKDIR, mkdir(path: *const c_char, mode: mode_t) -> c_int);
66+
impl_hook!(RMDIR, rmdir(path: *const c_char) -> c_int);
67+
impl_hook!(LSEEK, lseek(fd: c_int, offset: off_t, whence: c_int) -> off_t);
68+
impl_hook!(LINK, link(src: *const c_char, dst: *const c_char) -> c_int);
69+
impl_hook!(UNLINK, unlink(src: *const c_char) -> c_int);
70+
71+
// NOTE: unhook poll due to mio's poller
72+
// impl_hook!(POLL, poll(fds: *mut pollfd, nfds: nfds_t, timeout: c_int) -> c_int);
73+
74+
// NOTE: unhook write/pthread_mutex_lock/pthread_mutex_unlock due to stack overflow or bug
75+
// impl_hook!(WRITE, write(fd: c_int, buf: *const c_void, count: size_t) -> ssize_t);
76+
// impl_hook!(PTHREAD_MUTEX_LOCK, pthread_mutex_lock(lock: *mut pthread_mutex_t) -> c_int);
6977
// impl_hook!(PTHREAD_MUTEX_UNLOCK, pthread_mutex_unlock(lock: *mut pthread_mutex_t) -> c_int);

0 commit comments

Comments
 (0)