Chinaunix首页 | 论坛 | 博客
  • 博客访问: 920957
  • 博文数量: 194
  • 博客积分: 7991
  • 博客等级: 少将
  • 技术积分: 2067
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-09 22:03
文章分类

全部博文(194)

文章存档

2010年(8)

2009年(71)

2008年(109)

2007年(6)

我的朋友

分类: LINUX

2008-03-30 10:19:04


sometime probably you need to call some func in kernel space who has a user space pointer as its argument, e.g.:
ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos);
If you directly call vfs_read, kernel will find that your buf parameter is not a user space pointer, so it sucks! you can set task's addr_limit to KERNEL_DS to cheat the kernel, let it believe your buf parameter is safe. as follows:
mm_segment_t old_fs;
old_fs = get_fs();
set_fs(KERNEL_DS);
vfs_read(filp, your_buf, ..........);
set_fs(old_fs);
 
an example :
//fs/exec.c

int kernel_read(struct file *file, unsigned long offset, char *addr, unsigned long count)
{
    mm_segment_t old_fs;
    loff_t pos = offset;
    int result;

    old_fs = get_fs();
    set_fs(get_ds());
    /* The cast to a user pointer is valid due to the set_fs() */
    result = vfs_read(file, (void __user *)addr, count, &pos);
    set_fs(old_fs);
    return result;
}
EXPORT_SYMBOL(kernel_read);

sxg

阅读(1538) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~