- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我试图在 Linux 中编写一个加密驱动程序,但是当我加密字符串并使用 copy_from_user()
将其复制到缓冲区时,它不起作用并且 k 值是字符串中的字符数
如果我将 copy_from_user
中的 temp
更改为 buf
它工作正常
ssize_t encdec_write_caesar(struct file *filp, const char *buf, size_t count, loff_t *f_pos)
{
char *temp ;
temp = kmalloc(memory_size * sizeof(char),GFP_USER);
memset(temp, 0, memory_size);
int i , k ;
k = ((encdec_private_date *)(filp->private_data))->key;
for(i = 0; i < count; i++)
{
temp[i] = (buf[i] + k) % 128;
}
k = copy_from_user ( caesar_Buf , temp , count);
printk("write-> %d\n" ,k);
kfree(temp);
return 0;
}
这是代码的一部分
#include <linux/ctype.h>
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
#include <linux/fcntl.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <linux/string.h>
#include "encdec.h"
#define MODULE_NAME "encdec"
MODULE_LICENSE("GPL");
MODULE_AUTHOR("YOUR NAME");
int encdec_open(struct inode *inode, struct file *filp);
int encdec_release(struct inode *inode, struct file *filp);
int encdec_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
ssize_t encdec_read_caesar( struct file *filp, char *buf, size_t count, loff_t *f_pos );
ssize_t encdec_write_caesar(struct file *filp, const char *buf, size_t count, loff_t *f_pos);
int memory_size = 0;
MODULE_PARM(memory_size, "i");
int major = 0;
char *caesar_Buf = NULL;
struct file_operations fops_caesar = {
.open = encdec_open,
.release = encdec_release,
.read = encdec_read_caesar,
.write = encdec_write_caesar,
.llseek = NULL,
.ioctl = encdec_ioctl,
.owner = THIS_MODULE
};
// Implemetation suggestion:
// -------------------------
// Use this structure as your file-object's private data structure
typedef struct {
unsigned char key;
int read_state;
} encdec_private_date;
int init_module(void)
{
major = register_chrdev(major, MODULE_NAME, &fops_caesar);
if(major < 0)
{
return major;
}
caesar_Buf = (char *)kmalloc(memory_size * sizeof(char),GFP_KERNEL);
return 0;
}
最佳答案
您应该使用 copy_from_user
而不是直接访问 buf
(在循环内,而不是在访问 temp
时(在内核空间中内存。)
copy_from_user
旨在确保在用户上下文中的访问仅访问用户空间数据——即,有人不会在系统调用中传递指向内核内存的指针并从中泄漏数据内核不知何故。
关于c - 如何修复 copy_from_user 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56150495/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!