内核模块中的get/dev/random
我需要在内核模块中获取内核模块中的get/dev/random,random,linux-kernel,linux-device-driver,kernel-module,Random,Linux Kernel,Linux Device Driver,Kernel Module,我需要在内核模块中获取/dev/random和/dev/uradom get\u random\u bytesAPI,用于获取/dev/uradom static void read_file() { struct file *file; loff_t pos = 0; //ssize_t wc; unsigned char buf_ent[21]={0,}; int ent_c; int i; ssize_t length = 0; mm_segment_t
/dev/random
和/dev/uradom
get\u random\u bytes
API,用于获取/dev/uradom
static void read_file()
{
struct file *file;
loff_t pos = 0;
//ssize_t wc;
unsigned char buf_ent[21]={0,};
int ent_c;
int i;
ssize_t length = 0;
mm_segment_t old_fs = get_fs();
set_fs(KERNEL_DS);
file = filp_open("/dev/random", O_WRONLY, 0);
file->f_op->unlocked_ioctl(file, RNDGETENTCNT, &ent_c);
if(ent_c < sizeof(char))
{
printk("not enough entropy\n");
}
printk("ent counter : %d\n", ent_c);
//file->f_op->unlocked_ioctl(file, RNDGETPOOL, &ent_st.buf);
length = file->f_op->read(file, buf_ent, ent_c/ 8, &pos);
if(length <0)
{
printk("failed to random_read\n");
}
printk("length : %d\n", length);
printk("ent: ");
for(i=0;i<length; i++)
{
printk("%02x", buf_ent[i]);
}
printk("\n");
filp_close(file,0);
set_fs(old_fs);
}
但是没有用于/dev/random
的API,所以我尝试在内核空间中ioctl和读取文件。这就是我所做的
RNDGETPOOL
ioctlinclude/linux/random.h
RNDGETPOOL
已声明
/* Get the contents of the entropy pool. (Superuser only.) */
#define RNDGETPOOL _IOR( 'R', 0x02, int [2] )
但是,它不起作用,所以我检查了driver/char/random.h
注意到RNDGETPOOL
不见了
static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
{
int size, ent_count;
int __user *p = (int __user *)arg;
int retval;
switch (cmd) {
case RNDGETENTCNT:
/* inherently racy, no point locking */
if (put_user(input_pool.entropy_count, p))
return -EFAULT;
return 0;
case RNDADDTOENTCNT:
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
if (get_user(ent_count, p))
return -EFAULT;
credit_entropy_bits(&input_pool, ent_count);
return 0;
case RNDADDENTROPY:
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
if (get_user(ent_count, p++))
return -EFAULT;
if (ent_count < 0)
return -EINVAL;
if (get_user(size, p++))
return -EFAULT;
retval = write_pool(&input_pool, (const char __user *)p,
size);
if (retval < 0)
return retval;
credit_entropy_bits(&input_pool, ent_count);
return 0;
case RNDZAPENTCNT:
case RNDCLEARPOOL:
/* Clear the entropy pool counters. */
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
rand_initialize();
return 0;
default:
return -EINVAL;
}
}
下面是我的内核模块访问/dev/random
的函数
static void read_file()
{
struct file *file;
loff_t pos = 0;
//ssize_t wc;
unsigned char buf_ent[21]={0,};
int ent_c;
int i;
ssize_t length = 0;
mm_segment_t old_fs = get_fs();
set_fs(KERNEL_DS);
file = filp_open("/dev/random", O_WRONLY, 0);
file->f_op->unlocked_ioctl(file, RNDGETENTCNT, &ent_c);
if(ent_c < sizeof(char))
{
printk("not enough entropy\n");
}
printk("ent counter : %d\n", ent_c);
//file->f_op->unlocked_ioctl(file, RNDGETPOOL, &ent_st.buf);
length = file->f_op->read(file, buf_ent, ent_c/ 8, &pos);
if(length <0)
{
printk("failed to random_read\n");
}
printk("length : %d\n", length);
printk("ent: ");
for(i=0;i<length; i++)
{
printk("%02x", buf_ent[i]);
}
printk("\n");
filp_close(file,0);
set_fs(old_fs);
}
第二次尝试
[1290911.493990] ent_c : 33
[1290911.493994] length : 4
[1290911.493994] ent: 7832640a
顺便说一下,random\u read
函数参数具有\u用户
关键字。代码中的BufBuf
位于内核空间
在内核空间中使用
random\u read
函数是否合适???内核中获取随机字节的接口是get\u random\u bytes()
:
static void read_文件(void)
{
无符号字符bufu ent[21];
获取随机字节(buf_ent,21);
打印十六进制转储字节(“ent:”,转储前缀\u NONE,buf\u ent,21);
}
输出似乎是随机的-为什么您认为两个池都需要位?是的,/dev/urandom有get\u random\u bytes函数,但我希望读取/dev/random。
[1290911.493990] ent_c : 33
[1290911.493994] length : 4
[1290911.493994] ent: 7832640a