jeudi 23 juin 2016

get /dev/random in kernel module

I need to get both /dev/random and /dev/urandom within kernel module.

get_random_bytes API provided to get /dev/urandom.

But there is no API for /dev/random so I tried to ioctl and read file in kernel space.
Here is what I have done.

  1. using RNDGETPOOL ioctl

in include/linux/random.h
RNDGETPOOL is declared

/* Get the contents of the entropy pool.  (Superuser only.) */
#define RNDGETPOOL      _IOR( 'R', 0x02, int [2] )

but, It won't work so I checked driver/char/random.h noticed RNDGETPOOL is gone!!

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;
    }
}

I searched google and find out ioctl RNDGETPOOL is removed. done! (http://ift.tt/28U8qXl)

  1. using random_read function (driver/char/random.c:997 static ssize_t random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos))

here is my kernel module's function accesses to /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);
}

outputs seems to be random

first try [1290902.992048] ent_c : 165
[1290902.992060] length : 20
[1290902.992060] ent: d89290f4a5eea8e087a63943ed0129041e80b568

second try [1290911.493990] ent_c : 33
[1290911.493994] length : 4
[1290911.493994] ent: 7832640a

by the way random_read function argument has __user keyword. Buf buf in code is in kernel space.

Is appropriate using random_read function in kernel space??




Aucun commentaire:

Enregistrer un commentaire