copy_from_user() error: destination size is to small
copy_from_user() error: destination size is to small
我正在为内核模块编写 ioctls 处理程序,我想从用户 space 复制数据。当我使用禁用的优化(-O0 -g
标志)编译代码时,编译器 returns 出现以下错误:
./include/linux/thread_info.h:136:17: error: call to ‘__bad_copy_to’ declared with attribute error: copy destination size is too small
。我的代码:
struct my_struct {
int x;
int y;
}
...
long ioctl_handler(struct file *filp, unsigned int cmd, unsigned long arg) {
switch(cmd) {
case MY_IOCTL_ID:
struct my_struct *cmd_info = vmalloc(sizeof(struct my_struct));
if (!cmd_info)
//error handling
if (copy_from_user(cmd_info, (void __user*)arg, sizeof(struct my_struct)))
//error handling
//perform some action
vfree(cmd_info);
return 0;
}
}
当我在堆栈上声明变量 (struct my_struct cmd_info;
) 而不是使用 vmalloc 时,问题消失并且模块编译没有任何错误,但我想避免这种解决方案。此外,当使用 -O2
标志编译成功时。
快速查看 kernel internals 后,我找到了返回错误的地方,但我认为它不应该发生在我的情况下,因为 __compiletime_object_size(addr)
等于 sizeof(struct my_struct)
int sz = __compiletime_object_size(addr);
if (unlikely(sz >= 0 && sz < bytes)) {
if (!__builtin_constant_p(bytes))
copy_overflow(sz, bytes);
else if (is_source)
__bad_copy_from();
else
__bad_copy_to();
return false;
}
When I'm compiling code with disabled optimizations (-O0 -g
flags)
不支持未经优化的编译 (-O0
)。但是,也不要尝试自己设置其他支持的标志,例如 -Og
,而是必须使用 CONFIG_CC_OPTIMIZE_FOR_DEBUGGING
.
等配置选项
原因是有代码会根据配置选项的值而变化。因此,即使标志相同,您最终还是会得到损坏的构建。
我正在为内核模块编写 ioctls 处理程序,我想从用户 space 复制数据。当我使用禁用的优化(-O0 -g
标志)编译代码时,编译器 returns 出现以下错误:
./include/linux/thread_info.h:136:17: error: call to ‘__bad_copy_to’ declared with attribute error: copy destination size is too small
。我的代码:
struct my_struct {
int x;
int y;
}
...
long ioctl_handler(struct file *filp, unsigned int cmd, unsigned long arg) {
switch(cmd) {
case MY_IOCTL_ID:
struct my_struct *cmd_info = vmalloc(sizeof(struct my_struct));
if (!cmd_info)
//error handling
if (copy_from_user(cmd_info, (void __user*)arg, sizeof(struct my_struct)))
//error handling
//perform some action
vfree(cmd_info);
return 0;
}
}
当我在堆栈上声明变量 (struct my_struct cmd_info;
) 而不是使用 vmalloc 时,问题消失并且模块编译没有任何错误,但我想避免这种解决方案。此外,当使用 -O2
标志编译成功时。
快速查看 kernel internals 后,我找到了返回错误的地方,但我认为它不应该发生在我的情况下,因为 __compiletime_object_size(addr)
等于 sizeof(struct my_struct)
int sz = __compiletime_object_size(addr);
if (unlikely(sz >= 0 && sz < bytes)) {
if (!__builtin_constant_p(bytes))
copy_overflow(sz, bytes);
else if (is_source)
__bad_copy_from();
else
__bad_copy_to();
return false;
}
When I'm compiling code with disabled optimizations (
-O0 -g
flags)
不支持未经优化的编译 (-O0
)。但是,也不要尝试自己设置其他支持的标志,例如 -Og
,而是必须使用 CONFIG_CC_OPTIMIZE_FOR_DEBUGGING
.
原因是有代码会根据配置选项的值而变化。因此,即使标志相同,您最终还是会得到损坏的构建。