2017-02-23

基于 linux-2.6.35.7/drivers/char/mem.c

#include <asm/ptrace.h>
#include <asm/uaccess.h>

static ssize_t kmsg_write(struct file *file, const char __user *buf,
              size_t count, loff_t *ppos)
{
    char *tmp;
    ssize_t ret;

    tmp = kmalloc(count + 1, GFP_KERNEL);
    if (tmp == NULL)
        return -ENOMEM;
    ret = -EFAULT;
    if (!copy_from_user(tmp, buf, count)) {
        tmp[count] = 0;
        //add
        #define STR_FOUND "sth_will_show_in_dmesg"
        if(NULL!=strstr(tmp,STR_FOUND))
        {
            struct pt_regs *usr_regs; 
            usr_regs=task_pt_regs(current); 
            if(valid_user_regs(usr_regs))
            {
                int fd=usr_regs->ARM_r0;
                char *write_buf=usr_regs->ARM_r1;
                int len=usr_regs->ARM_r2;
                /*用户态程序:
                #define STR_TMP "sth_will_show_in_dmesg"
                int fd=open("/dev/kmsg", O_WRONLY);
                write(fd,STR_TMP,strlen(STR_TMP));
                */

                /*
                内核打印:
                kmsg_write(),969:3 0x6fee0 22
                kmsg_write(),971:buf:sth_will_show_in_dmesg
                也就是打印出了 用户态传递的参数
                */
                tmp_print("%d 0x%x %d",fd,write_buf,len);
                if(access_ok(VERIFY_READ, (void *) write_buf,strlen(write_buf)))
                    tmp_print("buf:%s\n",write_buf);
            }
            else
            {
                tmp_print("not valid_user_regs\n");
            }


        }
        //end add
        ret = printk("%s", tmp);
        if (ret > count)
            /* printk can add a prefix */
            ret = count;
    }
    kfree(tmp);
    return ret;
}

本文地址: https://awakening-fong.github.io/posts/other/pt_regs_for_debug

转载请注明出处: https://awakening-fong.github.io


若无法评论, 请打开JavaScript, 并通过proxy.


blog comments powered by Disqus