V4L/DVB: v4l2-dev: remove unnecessary lock around atomic clear_bit
[safe/jmp/linux-2.6] / drivers / char / random.c
index e0d0e37..2fd3d39 100644 (file)
 #include <linux/spinlock.h>
 #include <linux/percpu.h>
 #include <linux/cryptohash.h>
+#include <linux/fips.h>
+
+#ifdef CONFIG_GENERIC_HARDIRQS
+# include <linux/irq.h>
+#endif
 
 #include <asm/processor.h>
 #include <asm/uaccess.h>
@@ -409,6 +414,7 @@ struct entropy_store {
        unsigned add_ptr;
        int entropy_count;
        int input_rotate;
+       __u8 *last_data;
 };
 
 static __u32 input_pool_data[INPUT_POOL_WORDS];
@@ -520,6 +526,7 @@ static void mix_pool_bytes(struct entropy_store *r, const void *in, int bytes)
 static void credit_entropy_bits(struct entropy_store *r, int nbits)
 {
        unsigned long flags;
+       int entropy_count;
 
        if (!nbits)
                return;
@@ -527,20 +534,20 @@ static void credit_entropy_bits(struct entropy_store *r, int nbits)
        spin_lock_irqsave(&r->lock, flags);
 
        DEBUG_ENT("added %d entropy credits to %s\n", nbits, r->name);
-       r->entropy_count += nbits;
-       if (r->entropy_count < 0) {
+       entropy_count = r->entropy_count;
+       entropy_count += nbits;
+       if (entropy_count < 0) {
                DEBUG_ENT("negative entropy/overflow\n");
-               r->entropy_count = 0;
-       } else if (r->entropy_count > r->poolinfo->POOLBITS)
-               r->entropy_count = r->poolinfo->POOLBITS;
+               entropy_count = 0;
+       } else if (entropy_count > r->poolinfo->POOLBITS)
+               entropy_count = r->poolinfo->POOLBITS;
+       r->entropy_count = entropy_count;
 
        /* should we wake readers? */
-       if (r == &input_pool &&
-           r->entropy_count >= random_read_wakeup_thresh) {
+       if (r == &input_pool && entropy_count >= random_read_wakeup_thresh) {
                wake_up_interruptible(&random_read_wait);
                kill_fasync(&fasync, SIGIO, POLL_IN);
        }
-
        spin_unlock_irqrestore(&r->lock, flags);
 }
 
@@ -557,9 +564,45 @@ struct timer_rand_state {
        unsigned dont_count_entropy:1;
 };
 
-static struct timer_rand_state input_timer_state;
+#ifndef CONFIG_GENERIC_HARDIRQS
+
 static struct timer_rand_state *irq_timer_state[NR_IRQS];
 
+static struct timer_rand_state *get_timer_rand_state(unsigned int irq)
+{
+       return irq_timer_state[irq];
+}
+
+static void set_timer_rand_state(unsigned int irq,
+                                struct timer_rand_state *state)
+{
+       irq_timer_state[irq] = state;
+}
+
+#else
+
+static struct timer_rand_state *get_timer_rand_state(unsigned int irq)
+{
+       struct irq_desc *desc;
+
+       desc = irq_to_desc(irq);
+
+       return desc->timer_rand_state;
+}
+
+static void set_timer_rand_state(unsigned int irq,
+                                struct timer_rand_state *state)
+{
+       struct irq_desc *desc;
+
+       desc = irq_to_desc(irq);
+
+       desc->timer_rand_state = state;
+}
+#endif
+
+static struct timer_rand_state input_timer_state;
+
 /*
  * This function adds entropy to the entropy "pool" by using timing
  * delays.  It uses the timer_rand_state structure to make an estimate
@@ -647,11 +690,15 @@ EXPORT_SYMBOL_GPL(add_input_randomness);
 
 void add_interrupt_randomness(int irq)
 {
-       if (irq >= NR_IRQS || irq_timer_state[irq] == NULL)
+       struct timer_rand_state *state;
+
+       state = get_timer_rand_state(irq);
+
+       if (state == NULL)
                return;
 
        DEBUG_ENT("irq event %d\n", irq);
-       add_timer_randomness(irq_timer_state[irq], 0x100 + irq);
+       add_timer_randomness(state, 0x100 + irq);
 }
 
 #ifdef CONFIG_BLOCK
@@ -660,10 +707,10 @@ void add_disk_randomness(struct gendisk *disk)
        if (!disk || !disk->random)
                return;
        /* first major is 1, so we get >= 0x200 here */
-       DEBUG_ENT("disk event %d:%d\n", disk->major, disk->first_minor);
+       DEBUG_ENT("disk event %d:%d\n",
+                 MAJOR(disk_devt(disk)), MINOR(disk_devt(disk)));
 
-       add_timer_randomness(disk->random,
-                            0x100 + MKDEV(disk->major, disk->first_minor));
+       add_timer_randomness(disk->random, 0x100 + disk_devt(disk));
 }
 #endif
 
@@ -726,11 +773,10 @@ static size_t account(struct entropy_store *r, size_t nbytes, int min,
 {
        unsigned long flags;
 
-       BUG_ON(r->entropy_count > r->poolinfo->POOLBITS);
-
        /* Hold lock while accounting */
        spin_lock_irqsave(&r->lock, flags);
 
+       BUG_ON(r->entropy_count > r->poolinfo->POOLBITS);
        DEBUG_ENT("trying to extract %d bits from %s\n",
                  nbytes * 8, r->name);
 
@@ -808,12 +854,21 @@ static ssize_t extract_entropy(struct entropy_store *r, void *buf,
 {
        ssize_t ret = 0, i;
        __u8 tmp[EXTRACT_SIZE];
+       unsigned long flags;
 
        xfer_secondary_pool(r, nbytes);
        nbytes = account(r, nbytes, min, reserved);
 
        while (nbytes) {
                extract_buf(r, tmp);
+
+               if (r->last_data) {
+                       spin_lock_irqsave(&r->lock, flags);
+                       if (!memcmp(tmp, r->last_data, EXTRACT_SIZE))
+                               panic("Hardware RNG duplicated output!\n");
+                       memcpy(r->last_data, tmp, EXTRACT_SIZE);
+                       spin_unlock_irqrestore(&r->lock, flags);
+               }
                i = min_t(int, nbytes, EXTRACT_SIZE);
                memcpy(buf, tmp, i);
                nbytes -= i;
@@ -896,6 +951,9 @@ static void init_std_data(struct entropy_store *r)
        now = ktime_get_real();
        mix_pool_bytes(r, &now, sizeof(now));
        mix_pool_bytes(r, utsname(), sizeof(*(utsname())));
+       /* Enable continuous test in fips mode */
+       if (fips_enabled)
+               r->last_data = kmalloc(EXTRACT_SIZE, GFP_KERNEL);
 }
 
 static int rand_initialize(void)
@@ -911,7 +969,9 @@ void rand_initialize_irq(int irq)
 {
        struct timer_rand_state *state;
 
-       if (irq >= NR_IRQS || irq_timer_state[irq])
+       state = get_timer_rand_state(irq);
+
+       if (state)
                return;
 
        /*
@@ -920,7 +980,7 @@ void rand_initialize_irq(int irq)
         */
        state = kzalloc(sizeof(struct timer_rand_state), GFP_KERNEL);
        if (state)
-               irq_timer_state[irq] = state;
+               set_timer_rand_state(irq, state);
 }
 
 #ifdef CONFIG_BLOCK
@@ -991,12 +1051,6 @@ random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
                                /* like a named pipe */
        }
 
-       /*
-        * If we gave the user some bytes, update the access time.
-        */
-       if (count)
-               file_accessed(file);
-
        return (count ? count : retval);
 }
 
@@ -1047,7 +1101,6 @@ static ssize_t random_write(struct file *file, const char __user *buffer,
                            size_t count, loff_t *ppos)
 {
        size_t ret;
-       struct inode *inode = file->f_path.dentry->d_inode;
 
        ret = write_pool(&blocking_pool, buffer, count);
        if (ret)
@@ -1056,8 +1109,6 @@ static ssize_t random_write(struct file *file, const char __user *buffer,
        if (ret)
                return ret;
 
-       inode->i_mtime = current_fs_time(inode->i_sb);
-       mark_inode_dirty(inode);
        return (ssize_t)count;
 }
 
@@ -1112,18 +1163,12 @@ static int random_fasync(int fd, struct file *filp, int on)
        return fasync_helper(fd, filp, on, &fasync);
 }
 
-static int random_release(struct inode *inode, struct file *filp)
-{
-       return fasync_helper(-1, filp, 0, &fasync);
-}
-
 const struct file_operations random_fops = {
        .read  = random_read,
        .write = random_write,
        .poll  = random_poll,
        .unlocked_ioctl = random_ioctl,
        .fasync = random_fasync,
-       .release = random_release,
 };
 
 const struct file_operations urandom_fops = {
@@ -1131,7 +1176,6 @@ const struct file_operations urandom_fops = {
        .write = random_write,
        .unlocked_ioctl = random_ioctl,
        .fasync = random_fasync,
-       .release = random_release,
 };
 
 /***************************************************************
@@ -1147,7 +1191,7 @@ const struct file_operations urandom_fops = {
 void generate_random_uuid(unsigned char uuid_out[16])
 {
        get_random_bytes(uuid_out, 16);
-       /* Set UUID version to 4 --- truely random generation */
+       /* Set UUID version to 4 --- truly random generation */
        uuid_out[6] = (uuid_out[6] & 0x0F) | 0x40;
        /* Set the UUID variant to DCE */
        uuid_out[8] = (uuid_out[8] & 0x3F) | 0x80;
@@ -1178,7 +1222,7 @@ static char sysctl_bootid[16];
  * as an ASCII string in the standard UUID format.  If accesses via the
  * sysctl system call, it is returned as 16 bytes of binary data.
  */
-static int proc_do_uuid(ctl_table *table, int write, struct file *filp,
+static int proc_do_uuid(ctl_table *table, int write,
                        void __user *buffer, size_t *lenp, loff_t *ppos)
 {
        ctl_table fake_table;
@@ -1192,112 +1236,68 @@ static int proc_do_uuid(ctl_table *table, int write, struct file *filp,
        if (uuid[8] == 0)
                generate_random_uuid(uuid);
 
-       sprintf(buf, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
-               "%02x%02x%02x%02x%02x%02x",
-               uuid[0],  uuid[1],  uuid[2],  uuid[3],
-               uuid[4],  uuid[5],  uuid[6],  uuid[7],
-               uuid[8],  uuid[9],  uuid[10], uuid[11],
-               uuid[12], uuid[13], uuid[14], uuid[15]);
+       sprintf(buf, "%pU", uuid);
+
        fake_table.data = buf;
        fake_table.maxlen = sizeof(buf);
 
-       return proc_dostring(&fake_table, write, filp, buffer, lenp, ppos);
-}
-
-static int uuid_strategy(ctl_table *table, int __user *name, int nlen,
-                        void __user *oldval, size_t __user *oldlenp,
-                        void __user *newval, size_t newlen)
-{
-       unsigned char tmp_uuid[16], *uuid;
-       unsigned int len;
-
-       if (!oldval || !oldlenp)
-               return 1;
-
-       uuid = table->data;
-       if (!uuid) {
-               uuid = tmp_uuid;
-               uuid[8] = 0;
-       }
-       if (uuid[8] == 0)
-               generate_random_uuid(uuid);
-
-       if (get_user(len, oldlenp))
-               return -EFAULT;
-       if (len) {
-               if (len > 16)
-                       len = 16;
-               if (copy_to_user(oldval, uuid, len) ||
-                   put_user(len, oldlenp))
-                       return -EFAULT;
-       }
-       return 1;
+       return proc_dostring(&fake_table, write, buffer, lenp, ppos);
 }
 
 static int sysctl_poolsize = INPUT_POOL_WORDS * 32;
 ctl_table random_table[] = {
        {
-               .ctl_name       = RANDOM_POOLSIZE,
                .procname       = "poolsize",
                .data           = &sysctl_poolsize,
                .maxlen         = sizeof(int),
                .mode           = 0444,
-               .proc_handler   = &proc_dointvec,
+               .proc_handler   = proc_dointvec,
        },
        {
-               .ctl_name       = RANDOM_ENTROPY_COUNT,
                .procname       = "entropy_avail",
                .maxlen         = sizeof(int),
                .mode           = 0444,
-               .proc_handler   = &proc_dointvec,
+               .proc_handler   = proc_dointvec,
                .data           = &input_pool.entropy_count,
        },
        {
-               .ctl_name       = RANDOM_READ_THRESH,
                .procname       = "read_wakeup_threshold",
                .data           = &random_read_wakeup_thresh,
                .maxlen         = sizeof(int),
                .mode           = 0644,
-               .proc_handler   = &proc_dointvec_minmax,
-               .strategy       = &sysctl_intvec,
+               .proc_handler   = proc_dointvec_minmax,
                .extra1         = &min_read_thresh,
                .extra2         = &max_read_thresh,
        },
        {
-               .ctl_name       = RANDOM_WRITE_THRESH,
                .procname       = "write_wakeup_threshold",
                .data           = &random_write_wakeup_thresh,
                .maxlen         = sizeof(int),
                .mode           = 0644,
-               .proc_handler   = &proc_dointvec_minmax,
-               .strategy       = &sysctl_intvec,
+               .proc_handler   = proc_dointvec_minmax,
                .extra1         = &min_write_thresh,
                .extra2         = &max_write_thresh,
        },
        {
-               .ctl_name       = RANDOM_BOOT_ID,
                .procname       = "boot_id",
                .data           = &sysctl_bootid,
                .maxlen         = 16,
                .mode           = 0444,
-               .proc_handler   = &proc_do_uuid,
-               .strategy       = &uuid_strategy,
+               .proc_handler   = proc_do_uuid,
        },
        {
-               .ctl_name       = RANDOM_UUID,
                .procname       = "uuid",
                .maxlen         = 16,
                .mode           = 0444,
-               .proc_handler   = &proc_do_uuid,
-               .strategy       = &uuid_strategy,
+               .proc_handler   = proc_do_uuid,
        },
-       { .ctl_name = 0 }
+       { }
 };
 #endif         /* CONFIG_SYSCTL */
 
 /********************************************************************
  *
- * Random funtions for networking
+ * Random functions for networking
  *
  ********************************************************************/
 
@@ -1449,7 +1449,8 @@ static void rekey_seq_generator(struct work_struct *work)
        keyptr->count = (ip_cnt & COUNT_MASK) << HASH_BITS;
        smp_wmb();
        ip_cnt++;
-       schedule_delayed_work(&rekey_work, REKEY_INTERVAL);
+       schedule_delayed_work(&rekey_work,
+                             round_jiffies_relative(REKEY_INTERVAL));
 }
 
 static inline struct keydata *get_keyptr(void)
@@ -1571,6 +1572,7 @@ u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
 
        return half_md4_transform(hash, keyptr->secret);
 }
+EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
 
 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
 u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
@@ -1624,15 +1626,20 @@ EXPORT_SYMBOL(secure_dccp_sequence_number);
  * value is not cryptographically secure but for several uses the cost of
  * depleting entropy is too high
  */
+DEFINE_PER_CPU(__u32 [4], get_random_int_hash);
 unsigned int get_random_int(void)
 {
-       /*
-        * Use IP's RNG. It suits our purpose perfectly: it re-keys itself
-        * every second, from the entropy pool (and thus creates a limited
-        * drain on it), and uses halfMD4Transform within the second. We
-        * also mix it with jiffies and the PID:
-        */
-       return secure_ip_id((__force __be32)(current->pid + jiffies));
+       struct keydata *keyptr;
+       __u32 *hash = get_cpu_var(get_random_int_hash);
+       int ret;
+
+       keyptr = get_keyptr();
+       hash[0] += current->pid + jiffies + get_cycles();
+
+       ret = half_md4_transform(hash, keyptr->secret);
+       put_cpu_var(get_random_int_hash);
+
+       return ret;
 }
 
 /*