Changeset 2511 for neercs


Ignore:
Timestamp:
Jul 1, 2008, 4:14:54 PM (12 years ago)
Author:
Sam Hocevar
Message:
  • Allow to grab 32-bit applications from a 64-bit neercs process.
Location:
neercs/trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • neercs/trunk/src/mytrace.c

    r2509 r2511  
    3838                              char* dest, long src, size_t n);
    3939static int memcpy_into_target(struct mytrace *t,
    40                               long dest, char *src, size_t n);
     40                              long dest, char const *src, size_t n);
    4141static long remote_syscall(pid_t pid, long call,
    4242                           long arg1, long arg2, long arg3);
    43 #if 0
     43#   if defined DEBUG
    4444static void print_registers(pid_t pid);
    45 #endif
     45#   else
     46#       define print_registers(x) do {} while(0)
     47#   endif
    4648#endif
    4749
    4850#define X(x) #x
    4951#define STRINGIFY(x) X(x)
     52
     53#define SYSCALL_X86   0x80cd  /* CD 80 = int $0x80 */
     54#define SYSCALL_AMD64 0x050fL /* 0F 05 = syscall */
     55
     56#define MYCALL_OPEN     0
     57#define MYCALL_CLOSE    1
     58#define MYCALL_DUP2     2
     59#define MYCALL_SETPGID  3
     60#define MYCALL_SETSID   4
    5061
    5162#if defined __x86_64__
     
    5869#   define RDI rdi
    5970#   define RSI rsi
    60 #   define SYSCALL 0x050fL /* 0F 05 = syscall */
    6171#   define FMT "%016lx"
     72int syscalls64[] = { SYS_open, SYS_close, SYS_dup2, SYS_setpgid, SYS_setsid };
     73int syscalls32[] = { 5, 6, 63, 57, 66 };
    6274#else
    6375#   define RAX eax
     
    6981#   define RDI edi
    7082#   define RSI esi
    71 #   define SYSCALL 0x80cd /* CD 80 = int $0x80 */
    7283#   define FMT "%08lx"
     84int syscalls32[] = { SYS_open, SYS_close, SYS_dup2, SYS_setpgid, SYS_setsid };
    7385#endif
    7486
     
    136148    memcpy_into_target(t, regs.RSP, path, size);
    137149
    138     ret = remote_syscall(t->pid, SYS_open, regs.RSP, O_RDWR, 0755);
    139 
    140     /* Restore the datas */
     150    ret = remote_syscall(t->pid, MYCALL_OPEN, regs.RSP, O_RDWR, 0755);
     151
     152    /* Restore the data */
    141153    memcpy_into_target(t, regs.RSP, backup_data, size);
    142154
     
    157169{
    158170#if defined USE_GRAB
    159     return remote_syscall(t->pid, SYS_close, fd, 0, 0);
     171    return remote_syscall(t->pid, MYCALL_CLOSE, fd, 0, 0);
    160172#else
    161173    errno = ENOSYS;
     
    167179{
    168180#if defined USE_GRAB
    169     return remote_syscall(t->pid, SYS_dup2, oldfd, newfd, 0);
     181    return remote_syscall(t->pid, MYCALL_DUP2, oldfd, newfd, 0);
    170182#else
    171183    errno = ENOSYS;
     
    177189{
    178190#if defined USE_GRAB
    179     return remote_syscall(t->pid, SYS_setpgid, pid, pgid, 0);
     191    return remote_syscall(t->pid, MYCALL_SETPGID, pid, pgid, 0);
    180192#else
    181193    errno = ENOSYS;
     
    187199{
    188200#if defined USE_GRAB
    189     return remote_syscall(t->pid, SYS_setsid, 0, 0, 0);
     201    return remote_syscall(t->pid, MYCALL_SETSID, 0, 0, 0);
    190202#else
    191203    errno = ENOSYS;
     
    198210 */
    199211
     212#if defined USE_GRAB
    200213static int memcpy_from_target(struct mytrace *t,
    201214                              char* dest, long src, size_t n)
    202215{
     216    static int const align = sizeof(long) - 1;
     217
    203218    while(n)
    204219    {
    205220        long data;
    206         int align = sizeof(long) - 1;
    207221        size_t todo = sizeof(long) - (src & align);
    208222
     
    227241
    228242static int memcpy_into_target(struct mytrace *t,
    229                               long dest, char *src, size_t n)
    230 {
     243                              long dest, char const *src, size_t n)
     244{
     245    static int const align = sizeof(long) - 1;
     246
    231247    while(n)
    232248    {
    233249        long data;
    234         int align = sizeof(long) - 1;
    235250        size_t todo = sizeof(long) - (dest & align);
    236251
     
    271286     *  - rewind eip/rip to point on the syscall instruction
    272287     *  - single step: execute syscall instruction
     288     *  - retrieve resulting registers
    273289     *  - restore registers */
    274290    struct user_regs_struct regs, oldregs;
    275291    long oinst;
     292    int bits;
     293
     294print_registers(pid);
     295#if defined __x86_64__
     296    bits = 64;
     297#else
     298    bits = 32;
     299#endif
    276300
    277301    for(;;)
     
    285309        oinst = ptrace(PTRACE_PEEKTEXT, pid, oldregs.RIP - 2, 0) & 0xffff;
    286310
    287         if(oinst == SYSCALL)
     311#if defined __x86_64__
     312        if(oinst == SYSCALL_AMD64)
    288313            break;
     314        if(oinst == SYSCALL_X86)
     315        {
     316            bits = 32;
     317            break;
     318        }
     319#else
     320        if(oinst == SYSCALL_X86)
     321            break;
     322#endif
    289323
    290324        if(ptrace(PTRACE_SYSCALL, pid, NULL, 0) < 0)
     
    303337    }
    304338
     339    print_registers(pid);
     340
    305341    regs = oldregs;
    306342    regs.RIP = regs.RIP - 2;
    307     regs.RAX = call;
    308343#if defined __x86_64__
    309     regs.RDI = arg1;
    310     regs.RSI = arg2;
    311     regs.RDX = arg3;
    312 #else
    313     regs.RBX = arg1;
    314     regs.RCX = arg2;
    315     regs.RDX = arg3;
    316 #endif
     344    if(bits == 64)
     345    {
     346        regs.RAX = syscalls64[call];
     347        regs.RDI = arg1;
     348        regs.RSI = arg2;
     349        regs.RDX = arg3;
     350    }
     351    else
     352#endif
     353    {
     354        regs.RAX = syscalls32[call];
     355        regs.RBX = arg1;
     356        regs.RCX = arg2;
     357        regs.RDX = arg3;
     358    }
    317359
    318360    if(ptrace(PTRACE_SETREGS, pid, NULL, &regs) < 0)
     
    322364    }
    323365
     366    print_registers(pid);
     367
    324368    if(ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL) < 0)
    325369    {
     
    329373    waitpid(pid, NULL, 0);
    330374
     375    print_registers(pid);
     376
    331377    if(ptrace(PTRACE_GETREGS, pid, NULL, &regs) < 0)
    332378    {
     
    340386        return -1;
    341387    }
     388    print_registers(pid);
    342389
    343390    debug("syscall %ld returned %ld", call, regs.RAX);
     
    354401
    355402/* For debugging purposes only. Prints register and stack information. */
    356 #if 0
     403#if defined DEBUG
    357404static void print_registers(pid_t pid)
    358405{
     
    400447    fprintf(stderr, "...\n");
    401448}
    402 #endif
    403 
     449#endif /* DEBUG */
     450
     451#endif /* USE_GRAB */
     452
  • neercs/trunk/src/neercs.h

    r2510 r2511  
    212212static inline void debug(const char *format, ...)
    213213{
    214     int saved_errno = geterrno();
    215214    va_list args;
    216215    va_start(args, format);
     
    219218    fprintf(stderr, "\n");
    220219    va_end(args);
    221     seterrno(saved_errno);
    222220}
    223221#else
Note: See TracChangeset for help on using the changeset viewer.