博客
让人疯狂的”五一“三天(2)
五月二号:小方回来 前几天小方说要回来了,博士终于考完了,在等成绩,需要放松一下心情,也是故地重游。这一天她是终于来了。不过前几天也是发生了一点小意外。我的手机两天了有信号,但是就是收不到什么电话和信号,以至于把给她买票的事耽误了,最后她只买到5号回去的票。不过我还是心里窃喜的,可以多留几天么。。。呵呵。
May 4, 2009
自定义ELF文件中的section
在看内核代码的时候发现,原来ELF文件的section是完全可以自己定义的。当然这只是作新的增加。 今天请教了王老师,并做了一个简单的测试。但是具体的用意还有点不是很清楚,还有待继续分析ELF文件。
April 30, 2009
内核审计系统初步分析(1)
最近一段时间在看内核关于审计的东西,今天作一点最近看的整理,内核的审计系统也算是比较新的东西了,还有内核跟踪一类的动西,也是在2。6。30中才看到有独立的目录了。 其实内核的审计还是没有跳出对进程的管理,也主要是是对每个进程的活动情况进行记录。在struct thread_info结构中的flags项中有增加了许多的内容,其中就有几个标志就是关于是否启用内核审计的。如下面的几项:
April 27, 2009
系统调用简单分析
#define CFI_STARTPROC .cfi_startproc //用在每个函数的开始,用于初始化一些内部数据结构 #define CFI_ENDPROC .cfi_endproc //在函数结束的时候使用与.cfi_startproc相配套使用 #define CFI_DEF_CFA .cfi_def_cfa //定义计算CFA的规则 #define CFI_DEF_CFA_REGISTER .cfi_def_cfa_register //xx reg ,offset reg中的值保存在offset中,offset是CFA的 #define CFI_DEF_CFA_OFFSET .cfi_def_cfa_offset //xx offset 修改计算CFA的规则,reg中的值不发生变化,之改变offset #define CFI_ADJUST_CFA_OFFSET .cfi_adjust_cfa_offset //与上面相似但是修改前面一个offset #define CFI_OFFSET .cfi_offset //xx reg ,offset reg中的值保存在offset中,offset是CFA的 #define CFI_REL_OFFSET .cfi_rel_offset #define CFI_REGISTER .cfi_register #define CFI_RESTORE .cfi_restore #define CFI_REMEMBER_STATE .cfi_remember_state #define CFI_RESTORE_STATE .cfi_restore_state #define CFI_UNDEFINED .cfi_undefined .macro RING0_INT_FRAME CFI_STARTPROC simple CFI_SIGNAL_FRAME CFI_DEF_CFA esp, 3*4 /*CFI_OFFSET cs, -2*4;*/ CFI_OFFSET eip, -3*4 .endm # system call handler stub ENTRY(system_call) RING0_INT_FRAME # can't unwind into user space anyway pushl %eax # save orig_eax //保存原来的eax中的值 CFI_ADJUST_CFA_OFFSET 4 SAVE_ALL //保存系统寄存器信息 GET_THREAD_INFO(%ebp) //获取thread_info结构的地址 # system call tracing in operation / emulation testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%ebp) //检测thread_info中的相关标志看是否有系统跟踪 jnz syscall_trace_entry //有系统跟踪则先执行系统跟踪的代码,然后转跳回来(这在syscall_trace_entry中有写的) cmpl $(nr_syscalls), %eax //比较请求的系统调用号和最大系统调用号(验证系统调用号是否有效) jae syscall_badsys //如果请求系统调用号无效则退出 syscall_call: call *sys_call_table(,%eax,4) //跳转到系统调用表中,系统调用表是4字节对齐。 movl %eax,PT_EAX(%esp) # store the return value syscall_exit: LOCKDEP_SYS_EXIT //用于调试使用,只有在开启调试的时候才会去检测系统调用深度 DISABLE_INTERRUPTS(CLBR_ANY) # make sure we don't miss an interrupt # setting need_resched or sigpending # between sampling and the iret //#define ENABLE_INTERRUPTS(x) sti //#define DISABLE_INTERRUPTS(x) cli TRACE_IRQS_OFF //关闭中断跟踪 movl TI_flags(%ebp), %ecx // testl $_TIF_ALLWORK_MASK, %ecx # current->work //检测是否可以返回用户空间 jne syscall_exit_work syscall_exit_work: testl $_TIF_WORK_SYSCALL_EXIT, %ecx jz work_pending TRACE_IRQS_ON //开启系统中断跟踪 ENABLE_INTERRUPTS(CLBR_ANY) # could let syscall_trace_leave() call # schedule() instead //允许中断 movl %esp, %eax call syscall_trace_leave jmp resume_userspace END(syscall_exit_work) 看完本文有收获?请分享给更多人 关注「黑光技术」,关注大数据+微服务
April 25, 2009
内核的同步和互斥实例学习
#include <linux/module.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/mutex.h> #include <linux/semaphore.h> #include <linux/sched.h> static DEFINE_MUTEX(mut1); struct semaphore sem1,sem2; int test1(void *p) { down(&sem2); mutex_lock(&mut1); printk("get out test1\n"); mutex_unlock(&mut1); up(&sem1); return 0; } int test2(void *p) { down(&sem1); mutex_lock(&mut1); printk("get out test2\n"); mutex_unlock(&mut1); up(&sem2); return 0; } static int __init mutex_init_test() { init_MUTEX(&sem1); init_MUTEX_LOCKED(&sem2); printk("get out \n"); kernel_thread(test1, test1, CLONE_KERNEL); kernel_thread(test2, test2, CLONE_KERNEL); return 0; } static void __exit mutex_exit_test() { printk("get out \n"); return; } module_init(mutex_init_test); module_exit(mutex_exit_test); MODULE_AUTHOR("Helight.Xu"); MODULE_LICENSE("Dual BDS/GPL"); 看完本文有收获?请分享给更多人 关注「黑光技术」,关注大数据+微服务
April 21, 2009
为佛家的“色即是空,空即是色”正意
“色即是空,空即是色”这句佛经名言,包含着很深的哲学思想,一般人包括文人 们喜欢以此开玩笑,认为色就是指女人,空就是虚无或乌有.说和尚看破红尘,把女人 看做是虚无或乌有,便出家做和尚了.其实色并非女色,空也非虚无乌有.要认识此言 的真正含义,还得从佛教的基本教义说起. 佛教的教义,主要是所谓的"四谛",即 苦、集、灭、道。苦、集二谛阐明人生的本质及形成原因;灭、道二谛指明人生解 脱的归宿和解脱之路。
April 7, 2009
截获Linux系统调用
#include <linux/kernel.h> #include <linux/module.h> #include <linux/moduleparam.h> #include <linux/unistd.h> #include <linux/init.h> #include <asm/ptrace.h> #define NRB 2 typedef asmlinkage int (*__routine)(struct pt_regs); __routine old, new; unsigned long *sys_call_table = 0; static int counts = 0; unsigned long* find_sys_call_table(void) { struct { unsigned short limit; unsigned int base; } __attribute__ ( ( packed ) ) idtr; struct { unsigned short offset_low; unsigned short segment_select; unsigned char reserved, flags; unsigned short offset_high; } __attribute__ ( ( packed ) ) * idt; unsigned long system_call = 0; // x80中断处理程序system_call 地址 char *call_hex = "\xff\x14\x85"; // call 指令 char *code_ptr = NULL; char *p = NULL; unsigned long sct = 0x0; int i = 0; __asm__ ( "sidt %0": "=m" ( idtr ) ); idt = ( void * ) ( idtr.base + 8 * 0x80 ); system_call = ( idt->offset_high << 16 ) | idt->offset_low; code_ptr = (char *)system_call; for(i = 0;i < ( 100 - 2 ); i++) { if(code_ptr[i] == call_hex[0] && code_ptr[i+1] == call_hex[1] && code_ptr[i+2] == call_hex[2] ) { p = &code_ptr[i] + 3; break; } } if ( p ){ sct = *(unsigned long*)p; } return (unsigned long*)sct; } asmlinkage int audit_sys_call(struct pt_regs regs) { int ret = 0; counts++; printk("audit_sys_call!\n"); printk("call %ld sys_call! times: %d\n", regs.ax, counts); ret = ((__routine)old)(regs); return ret; } int mysys_init ( void ) { if (( sys_call_table = find_sys_call_table())) { printk( "sys_call_table = %p\n", sys_call_table ); old = (__routine)sys_call_table[NRB]; new = &audit_sys_call; sys_call_table[NRB] = (unsigned long)new; } return 0; } void mysys_exit ( void ) { printk("keep back the sys_call_table \n"); sys_call_table[NRB] = (unsigned long)old; } module_init(mysys_init); module_exit(mysys_exit); MODULE_LICENSE("GPL2.0"); MODULE_AUTHOR("Helight.Xu"); 看完本文有收获?请分享给更多人 关注「黑光技术」,关注大数据+微服务
March 31, 2009
使用valgrind检测内存泄露
Valgrind是x86架构Linux上的多重用途代码剖析和内存调试工具。但它的主要功能还是对内存的调试,默认工具也是启动 memcheck。用户可以在它的环境中运行程序来监视内存的使用情况,比如C语言中的malloc和free或者C++中的new和delete。
February 27, 2009