2828#include < unistd.h>
2929
3030/* Linux */
31+ #include < linux/elf.h>
3132#include < sys/ptrace.h>
3233#include < syscall.h>
3334#include < fcntl.h>
4041#include < string>
4142#include < vector>
4243
44+ #include " arch.h"
4345#include " inspect_utils.h"
4446#include " inspect_dns.h"
4547
@@ -169,7 +171,7 @@ std::string read_string(pid_t pid, unsigned long reg, unsigned long length) {
169171
170172void inspect_for_injection (pid_t pid, const user_regs_struct ®s) {
171173 // Inspect a PID's registers for the sign of shell injection.
172- std::string path = read_string (pid, regs. rdi , kTripWire .length ());
174+ std::string path = read_string (pid, REGS_ARG1 , kTripWire .length ());
173175 if (!path.length ()) {
174176 return ;
175177 }
@@ -181,7 +183,7 @@ void inspect_for_injection(pid_t pid, const user_regs_struct ®s) {
181183
182184std::string get_pathname (pid_t pid, const user_regs_struct ®s) {
183185 // Parse the pathname from the memory specified in the RDI register.
184- std::string pathname = read_string (pid, regs. rdi , kShellPathnameLength );
186+ std::string pathname = read_string (pid, REGS_ARG1 , kShellPathnameLength );
185187 debug_log (" Pathname is %s (len %lu)\n " , pathname.c_str (), pathname.length ());
186188 return pathname;
187189}
@@ -262,7 +264,7 @@ void match_error_pattern(std::string buffer, std::string shell, pid_t pid) {
262264
263265void inspect_for_corruption (pid_t pid, const user_regs_struct ®s) {
264266 // Inspect a PID's registers for shell corruption.
265- std::string buffer = read_string (pid, regs. rsi , regs. rdx );
267+ std::string buffer = read_string (pid, REGS_ARG2, REGS_ARG3 );
266268 debug_log (" Write buffer: %s\n " , buffer.c_str ());
267269 match_error_pattern (buffer, g_shell_pids[pid], pid);
268270}
@@ -297,12 +299,12 @@ bool has_unprintable(const std::string &value) {
297299
298300void inspect_for_arbitrary_file_open (pid_t pid, const user_regs_struct ®s) {
299301 // Inspect a PID's register for the sign of arbitrary file open.
300- std::string path = read_string (pid, regs. rsi , kRootDirMaxLength );
302+ std::string path = read_string (pid, REGS_ARG2 , kRootDirMaxLength );
301303 if (!path.length ()) {
302304 return ;
303305 }
304306 if (path.substr (0 , kFzAbsoluteDirectory .length ()) == kFzAbsoluteDirectory ) {
305- log_file_open (path, regs. rdx , pid);
307+ log_file_open (path, REGS_ARG3 , pid);
306308 return ;
307309 }
308310 if (path[0 ] == ' /' && path.length () > 1 ) {
@@ -314,7 +316,7 @@ void inspect_for_arbitrary_file_open(pid_t pid, const user_regs_struct ®s) {
314316 if (has_unprintable (path_absolute_topdir)) {
315317 struct stat dirstat;
316318 if (stat (path_absolute_topdir.c_str (), &dirstat) != 0 ) {
317- log_file_open (path, regs. rdx , pid);
319+ log_file_open (path, REGS_ARG3 , pid);
318320 }
319321 }
320322 }
@@ -399,13 +401,17 @@ int trace(std::map<pid_t, Tracee> pids) {
399401
400402 if (is_syscall) {
401403 user_regs_struct regs;
402- if (ptrace (PTRACE_GETREGS, pid, 0 , ®s) == -1 ) {
403- debug_log (" ptrace(PTRACE_GETREGS, %d): %s" , pid, strerror (errno));
404+ struct iovec iov {
405+ .iov_base = ®s,
406+ .iov_len = sizeof (struct user_regs_struct ),
407+ };
408+ if (ptrace (PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov) == -1 ) {
409+ debug_log (" ptrace(PTRACE_GETREGSET, %d): %s" , pid, strerror (errno));
404410 continue ;
405411 }
406412
407413 if (tracee.syscall_enter ) {
408- if (regs. orig_rax == __NR_execve) {
414+ if (REGS_SYSCALL == __NR_execve) {
409415 // This is a new process.
410416 auto parent = root_pids[pid];
411417 parent.ran_exec = true ;
@@ -420,12 +426,12 @@ int trace(std::map<pid_t, Tracee> pids) {
420426
421427 inspect_dns_syscalls (pid, regs);
422428
423- if (regs. orig_rax == __NR_openat) {
429+ if (REGS_SYSCALL == __NR_openat) {
424430 // TODO(metzman): Re-enable this once we have config/flag support.
425431 // inspect_for_arbitrary_file_open(pid, regs);
426432 }
427433
428- if (regs. orig_rax == __NR_write &&
434+ if (REGS_SYSCALL == __NR_write &&
429435 g_shell_pids.find (pid) != g_shell_pids.end ()) {
430436 debug_log (" Inspecting the `write` buffer of shell process %d." ,
431437 pid);
0 commit comments