linux __user 宏的含义是什么?
我希望有人能够解释 Linux 内核源代码中使用的 __user 宏的细微差别。
首先,宏:
# define __user __attribute__((noderef, address_space(1)))
现在,经过一番谷歌搜索后,我读到该宏允许将指针指定为属于用户地址空间,并且不应取消引用它。
我可能遗漏了一些明显的事实,但是有人可以解释一下这样一个宏的含义吗?例如,这个宏的用途的一个很好的例子是什么?再次,如果我遗漏了一些明显的东西,请原谅我。
为了将其置于某种上下文中,我在检查一些 USB 代码 (linux/usbdevice_fs.h) 时遇到了该宏。我只是在寻找对内核中使用的这个宏(或其他类似宏)的一般理解。
感谢您的关注!
I was hoping someone could explain the nuances of the __user macro used in the linux kernel source.
First of all, the macro:
# define __user __attribute__((noderef, address_space(1)))
Now, after some googling I read that this macro allows one to designate a pointer as belonging to the user address space, and that it should not be dereferenced.
I may be missing some obvious facts, but could someone please explain the implications of such a macro? For instance, what is a good example of where this macro would be of use? Again, forgive me if I am missing something obvious.
To put this in some context, I came accross the macro while examining some USB code (linux/usbdevice_fs.h). I am only looking for a general understanding of this macros( or others like it) use within the kernel.
Thanks for looking!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它允许诸如 sparse 之类的工具告诉内核开发人员他们可能正在使用不受信任的指针(或当前虚拟地址映射中可能无效的指针)不正确。
It allows tools like sparse to tell kernel developers that they're possibly using an untrusted pointer (or a pointer that may be invalid in the current virtual address mapping) improperly.
我认为 __user 标记了用户空间指针并告诉开发人员/系统不要信任它。如果用户给你“无效”指针,那么内核会尝试引用它(请注意,内核可以在任何地方引用),并且它可能会损坏它自己的空间。
例如,在“read”(在 usbdevice_fs.h 中)中应该为您提供一个(__user)缓冲区来写入结果。所以你必须使用 copy_to_user,但不能使用 memcopy、strcpy 或类似的东西。
注意:这不是正式的定义/描述,而是我所知道的唯一部分。
I think __user marks user space pointers and tells the developer/system not to trust it. If user gives you "invalid" pointer, then kernel tries to reference it (note that kernel can reference everywhere) and it can corrupt it's own space.
For example in "read"(in you usbdevice_fs.h) should provide you a (__user) buffer to write the result to. So you have to use copy_to_user, but not memcopy, strcpy or anything like this.
Note: This is not formal definition/description, but the only part I'm aware of.
__user
宏是在compiler.h 头文件中与其他一些宏一起定义的,例如__force
/__kernel
等。它们实际上对传统编译器(包括 GCC/ICC 等)没有任何用处。但对于像稀疏这样的内核静态分析工具很有用(更多信息请参见:Sparse - Linux Kernel Newbies)。当您提到像__user
/__kernel
/__force
等宏时,它为稀疏保留了特殊含义。在 Linux 内核邮件列表中,Linus Torvalds 解释了它的用法:The
__user
macro is defined with some other macros like__force
/__kernel
etc in the compiler.h header file. They are actually not of any use to traditional compilers, including GCC/ICC etc. But it's useful for kernel static analysis tools like sparse (more information here: Sparse - Linux Kernel Newbies). When you mention the macros like__user
/__kernel
/__force
etc, it keeps special meaning for sparse. In the Linux kernel mailing list, Linus Torvalds explains the use of it like this: