Aiur – ZelluX 的技术博客

Security, Kernel, Virtualization, Programming Languages

强制程序使用int 0×80做系统调用

with 5 comments

因为大多数情况下程序都是通过libc间接地发出系统调用的,所以只要编译一个只使用int 0×80的glibc库,然后在执行程序的时候用LD_LIBRARY_PATH或其他方法指定使用新编译的glibc库即可。

以glibc-2.9, Linux i386为例,在sysdeps/unix/sysv/linux/i386/syscall.S中可以看到

 ENTRY (syscall)

     PUSHARGS_6      /* Save register contents.  */
     _DOARGS_6(44)       /* Load arguments.  */
     movl 20(%esp), %eax /* Load syscall number into %eax.  */
     ENTER_KERNEL        /* Do the system call.  */
     POPARGS_6       /* Restore register contents.  */
     cmpl $-4095, %eax   /* Check %eax for error.  */
     jae SYSCALL_ERROR_LABEL /* Jump to error handler if error.  */

这里使用了ENTER_KERNEL这个宏做系统调用,接下来在sysdeps/unix/sysv/linux/i386/sysdep.h里可以找到这个宏的定义

/* The original calling convention for system calls on Linux/i386 is
   to use int $0x80.  */
#ifdef I386_USE_SYSENTER
# ifdef SHARED
#  define ENTER_KERNEL call *%gs:SYSINFO_OFFSET
# else
#  define ENTER_KERNEL call *_dl_sysinfo
# endif
#else
# define ENTER_KERNEL int $0x80
#endif

而I386_USE_SYSENTER这个宏也是在同一个头文件中定义的

#if defined USE_DL_SYSINFO \
    && (!defined NOT_IN_libc || defined IS_IN_libpthread)
# define I386_USE_SYSENTER   1
#else
# undef I386_USE_SYSENTER
#endif

把这个条件宏改成

#undef I386_USE_SYSENTER

就能强制glibc使用int 0×80了。

这个方法只能过滤通过glibc做的系统调用。对于程序里写死使用sysenter的情况就要使用反汇编或者其他手段了。

Related Posts

Written by zellux

January 26th, 2010 at 10:56 am

Posted in Programming

Tagged with ,

5 Responses to '强制程序使用int 0×80做系统调用'

Subscribe to comments with RSS or TrackBack to '强制程序使用int 0×80做系统调用'.

  1. Even if you do not have sep flags enabled on cpu,call *%gs:0×10 can still get the correct result. Is the dispatch job done by linux kernel or glibc?

    [Reply]

    zellux Reply:

    我猜应该还是通过sysenter指令做了系统调用吧,不知道这个测试里SEP flag是怎么禁的。因为我觉得call *%gs:0x10调用的还是linux-gate.so里面的sysenter相关的代码。

    [Reply]

    Zhongshu Reply:

    I just use the kvm to run linux and sep flag is disabled in this situation. Do u know how to enable it in kvm? Do you use msn or other IM?

    [Reply]

    zellux Reply:

    没接触过KVM,不大清楚。zellux at hotmail/gmail都可以。

    zellux Reply:

    问了下实验室的学长,好像不是我理解的那样。他说glibc.so相关的代码只负责跳转(类似于call *%gs:0x10),而至于跳过去的代码是int还是sysenter就是Linux决定的了。

    [Reply]

    Zhongshu

    6 Mar 10 at 12:05 pm

Leave a Reply

FireStats icon Powered by FireStatsBetter Tag Cloud