免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1435 | 回复: 5
打印 上一主题 下一主题

[内核模块] 请教关于copy_from_user函数的应用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-06-12 09:51 |只看该作者 |倒序浏览
购买主题 本主题需向作者支付 50 可用积分 才能浏览

论坛徽章:
15
射手座
日期:2014-02-26 13:45:082015年迎新春徽章
日期:2015-03-04 09:54:452015年辞旧岁徽章
日期:2015-03-03 16:54:15羊年新春福章
日期:2015-02-26 08:47:552015年亚洲杯之卡塔尔
日期:2015-02-03 08:33:45射手座
日期:2014-12-31 08:36:51水瓶座
日期:2014-06-04 08:33:52天蝎座
日期:2014-05-14 14:30:41天秤座
日期:2014-04-21 08:37:08处女座
日期:2014-04-18 16:57:05戌狗
日期:2014-04-04 12:21:33技术图书徽章
日期:2014-03-25 09:00:29
2 [报告]
发表于 2014-06-12 10:08 |只看该作者
咋又这样?“本主题需向作者支付 50 可用积分 才能浏览”

论坛徽章:
17
水瓶座
日期:2013-08-29 12:09:27白羊座
日期:2014-08-07 12:36:42丑牛
日期:2014-07-24 12:44:41寅虎
日期:2014-04-16 16:15:33寅虎
日期:2014-03-12 09:28:43摩羯座
日期:2014-03-06 13:22:04技术图书徽章
日期:2014-03-06 11:34:50天蝎座
日期:2014-01-09 11:31:44寅虎
日期:2013-12-27 17:01:44双子座
日期:2013-12-27 12:32:29双子座
日期:2013-12-25 09:03:33丑牛
日期:2013-12-24 16:18:44
3 [报告]
发表于 2014-06-12 10:11 |只看该作者
回复 1# mousexqshe

kernel space不允许发生 page fault,但是从系统调用传入的用户空间地址有可能是非法的,访问它可能会发生 page fault!解决方案:

1. 每次都去检查该传入地址有效性。。。性能悲剧

2. 采用出了问题补救的方式,又回到原点, kernel space page fault。

kernel hacker发明了高招,既然 kernel space不允许 page fault,我们又有这个需要,只要记录所有解引用用户空间地址的指令位置,发生 page fault的时候对比一下就OK了,SO 这就是 copy_from_user的由来。

至于copy_from_user是怎么做的,记得以前我有回复过,可以搜索下!

论坛徽章:
0
4 [报告]
发表于 2014-06-12 17:55 |只看该作者
回复 3# asuka2001

不好意思,可否解釋下面這一段詳細一點,我有點兜不起來你說的
  1. kernel hacker發明了高招,既然 kernel space不允許 page fault,我們又有這個需要,只要記錄所有解引用用戶空間地址的指令位置,發生 page fault的時候對比一下就OK了,SO 這就是 copy_from_user的由來。
复制代码
以下是我自己的觀點,有錯請指教

基本上會call到copy_from_user救代表已經進入到kernel space裡面了

下面那個from應該是user space address吧?

如果此時發現讀取from時,發生do_page_fault()則會跳到Oops.

所以from勢必一定要在記憶體內

下面copy_from_user()也不過就是一步一步的copy到kernel space某一塊地方
  1. static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
  2. {
  3.     if (access_ok(VERIFY_READ, from, n))
  4.         n = __copy_from_user(to, from, n);
  5.     else /* security hole - plug it */
  6.         memset(to, 0, n);
  7.     return n;
  8. }
复制代码

论坛徽章:
17
水瓶座
日期:2013-08-29 12:09:27白羊座
日期:2014-08-07 12:36:42丑牛
日期:2014-07-24 12:44:41寅虎
日期:2014-04-16 16:15:33寅虎
日期:2014-03-12 09:28:43摩羯座
日期:2014-03-06 13:22:04技术图书徽章
日期:2014-03-06 11:34:50天蝎座
日期:2014-01-09 11:31:44寅虎
日期:2013-12-27 17:01:44双子座
日期:2013-12-27 12:32:29双子座
日期:2013-12-25 09:03:33丑牛
日期:2013-12-24 16:18:44
5 [报告]
发表于 2014-06-12 19:13 |只看该作者
回复 4# wth0722

你需要注意,在 page fault流程中,如果是在内核空间中,会有搜索 exception_table的动作。

__do_page_fault() @ arch/x86/mm/fault.c
/*
         * When running in the kernel we expect faults to occur only to
         * addresses in user space.
  All other faults represent errors in
         * the kernel and should generate an OOPS.  Unfortunately, in the
         * case of an erroneous fault occurring in a code path which already
         * holds mmap_sem we will deadlock attempting to validate the fault
         * against the address space.  Luckily the kernel only validly
         * references user space from well defined areas of code, which are
         * listed in the exceptions table.
         *
         * As the vast majority of faults will be valid we will only perform
         * the source reference check when there is a possibility of a
         * deadlock. Attempt to lock the address space, if we cannot we then
         * validate the source. If this is invalid we can skip the address
         * space check, thus avoiding the deadlock:
         */
        if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
                if ((error_code & PF_USER) == 0 &&
                    !search_exception_tables(regs->ip)) {
                        bad_area_nosemaphore(regs, error_code, address);
                        return;
                }
retry:
                down_read(&mm->mmap_sem);
        } else {
                /*
                 * The above down_read_trylock() might have succeeded in
                 * which case we'll have missed the might_sleep() from
                 * down_read():
                 */
                might_sleep();
        }


至于 exception_tables怎么建立的,你可以看看

http://bbs.chinaunix.net/forum.p ... ;page=1#pid24043101

虽然是arm,但基本思想是一样的,可以作为参考!

论坛徽章:
0
6 [报告]
发表于 2014-06-12 19:33 |只看该作者
回复 5# asuka2001


感謝,我聊解你想說的了

基本上在ARM這邊,do_page_fault也有用那去判斷是否是正常流程
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP