random.c (92c653cf14400946f376a29b828d6af7e01f38dd) random.c (a6adf8e7a605250b911e94793fd077933709ff9e)
1// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2/*
3 * Copyright (C) 2017-2022 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4 * Copyright Matt Mackall <mpm@selenic.com>, 2003, 2004, 2005
5 * Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999. All
6 * rights reserved.
7 */
8

--- 1463 unchanged lines hidden (view full) ---

1472 stack.now = random_get_entropy();
1473 }
1474
1475 del_timer_sync(&stack.timer);
1476 destroy_timer_on_stack(&stack.timer);
1477 mix_pool_bytes(&stack.now, sizeof(stack.now));
1478}
1479
1// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2/*
3 * Copyright (C) 2017-2022 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4 * Copyright Matt Mackall <mpm@selenic.com>, 2003, 2004, 2005
5 * Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999. All
6 * rights reserved.
7 */
8

--- 1463 unchanged lines hidden (view full) ---

1472 stack.now = random_get_entropy();
1473 }
1474
1475 del_timer_sync(&stack.timer);
1476 destroy_timer_on_stack(&stack.timer);
1477 mix_pool_bytes(&stack.now, sizeof(stack.now));
1478}
1479
1480static ssize_t urandom_read(struct file *file, char __user *buf, size_t nbytes,
1481 loff_t *ppos)
1480
1481/**********************************************************************
1482 *
1483 * Userspace reader/writer interfaces.
1484 *
1485 * getrandom(2) is the primary modern interface into the RNG and should
1486 * be used in preference to anything else.
1487 *
1488 * Reading from /dev/random has the same functionality as calling
1489 * getrandom(2) with flags=0. In earlier versions, however, it had
1490 * vastly different semantics and should therefore be avoided, to
1491 * prevent backwards compatibility issues.
1492 *
1493 * Reading from /dev/urandom has the same functionality as calling
1494 * getrandom(2) with flags=GRND_INSECURE. Because it does not block
1495 * waiting for the RNG to be ready, it should not be used.
1496 *
1497 * Writing to either /dev/random or /dev/urandom adds entropy to
1498 * the input pool but does not credit it.
1499 *
1500 * Polling on /dev/random indicates when the RNG is initialized, on
1501 * the read side, and when it wants new entropy, on the write side.
1502 *
1503 * Both /dev/random and /dev/urandom have the same set of ioctls for
1504 * adding entropy, getting the entropy count, zeroing the count, and
1505 * reseeding the crng.
1506 *
1507 **********************************************************************/
1508
1509SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, unsigned int,
1510 flags)
1482{
1511{
1483 static int maxwarn = 10;
1512 if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE))
1513 return -EINVAL;
1484
1514
1485 if (!crng_ready() && maxwarn > 0) {
1486 maxwarn--;
1487 if (__ratelimit(&urandom_warning))
1488 pr_notice("%s: uninitialized urandom read (%zd bytes read)\n",
1489 current->comm, nbytes);
1490 }
1515 /*
1516 * Requesting insecure and blocking randomness at the same time makes
1517 * no sense.
1518 */
1519 if ((flags & (GRND_INSECURE | GRND_RANDOM)) == (GRND_INSECURE | GRND_RANDOM))
1520 return -EINVAL;
1491
1521
1492 return get_random_bytes_user(buf, nbytes);
1493}
1522 if (count > INT_MAX)
1523 count = INT_MAX;
1494
1524
1495static ssize_t random_read(struct file *file, char __user *buf, size_t nbytes,
1496 loff_t *ppos)
1497{
1498 int ret;
1525 if (!(flags & GRND_INSECURE) && !crng_ready()) {
1526 int ret;
1499
1527
1500 ret = wait_for_random_bytes();
1501 if (ret != 0)
1502 return ret;
1503 return get_random_bytes_user(buf, nbytes);
1528 if (flags & GRND_NONBLOCK)
1529 return -EAGAIN;
1530 ret = wait_for_random_bytes();
1531 if (unlikely(ret))
1532 return ret;
1533 }
1534 return get_random_bytes_user(buf, count);
1504}
1505
1506static __poll_t random_poll(struct file *file, poll_table *wait)
1507{
1508 __poll_t mask;
1509
1510 poll_wait(file, &crng_init_wait, wait);
1511 poll_wait(file, &random_write_wait, wait);

--- 35 unchanged lines hidden (view full) ---

1547
1548 ret = write_pool(buffer, count);
1549 if (ret)
1550 return ret;
1551
1552 return (ssize_t)count;
1553}
1554
1535}
1536
1537static __poll_t random_poll(struct file *file, poll_table *wait)
1538{
1539 __poll_t mask;
1540
1541 poll_wait(file, &crng_init_wait, wait);
1542 poll_wait(file, &random_write_wait, wait);

--- 35 unchanged lines hidden (view full) ---

1578
1579 ret = write_pool(buffer, count);
1580 if (ret)
1581 return ret;
1582
1583 return (ssize_t)count;
1584}
1585
1586static ssize_t urandom_read(struct file *file, char __user *buf, size_t nbytes,
1587 loff_t *ppos)
1588{
1589 static int maxwarn = 10;
1590
1591 if (!crng_ready() && maxwarn > 0) {
1592 maxwarn--;
1593 if (__ratelimit(&urandom_warning))
1594 pr_notice("%s: uninitialized urandom read (%zd bytes read)\n",
1595 current->comm, nbytes);
1596 }
1597
1598 return get_random_bytes_user(buf, nbytes);
1599}
1600
1601static ssize_t random_read(struct file *file, char __user *buf, size_t nbytes,
1602 loff_t *ppos)
1603{
1604 int ret;
1605
1606 ret = wait_for_random_bytes();
1607 if (ret != 0)
1608 return ret;
1609 return get_random_bytes_user(buf, nbytes);
1610}
1611
1555static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
1556{
1557 int size, ent_count;
1558 int __user *p = (int __user *)arg;
1559 int retval;
1560
1561 switch (cmd) {
1562 case RNDGETENTCNT:
1612static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
1613{
1614 int size, ent_count;
1615 int __user *p = (int __user *)arg;
1616 int retval;
1617
1618 switch (cmd) {
1619 case RNDGETENTCNT:
1563 /* inherently racy, no point locking */
1620 /* Inherently racy, no point locking. */
1564 if (put_user(input_pool.entropy_count, p))
1565 return -EFAULT;
1566 return 0;
1567 case RNDADDTOENTCNT:
1568 if (!capable(CAP_SYS_ADMIN))
1569 return -EPERM;
1570 if (get_user(ent_count, p))
1571 return -EFAULT;

--- 59 unchanged lines hidden (view full) ---

1631 .read = urandom_read,
1632 .write = random_write,
1633 .unlocked_ioctl = random_ioctl,
1634 .compat_ioctl = compat_ptr_ioctl,
1635 .fasync = random_fasync,
1636 .llseek = noop_llseek,
1637};
1638
1621 if (put_user(input_pool.entropy_count, p))
1622 return -EFAULT;
1623 return 0;
1624 case RNDADDTOENTCNT:
1625 if (!capable(CAP_SYS_ADMIN))
1626 return -EPERM;
1627 if (get_user(ent_count, p))
1628 return -EFAULT;

--- 59 unchanged lines hidden (view full) ---

1688 .read = urandom_read,
1689 .write = random_write,
1690 .unlocked_ioctl = random_ioctl,
1691 .compat_ioctl = compat_ptr_ioctl,
1692 .fasync = random_fasync,
1693 .llseek = noop_llseek,
1694};
1695
1639SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, unsigned int,
1640 flags)
1641{
1642 if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE))
1643 return -EINVAL;
1644
1645 /*
1646 * Requesting insecure and blocking randomness at the same time makes
1647 * no sense.
1648 */
1649 if ((flags & (GRND_INSECURE | GRND_RANDOM)) == (GRND_INSECURE | GRND_RANDOM))
1650 return -EINVAL;
1651
1652 if (count > INT_MAX)
1653 count = INT_MAX;
1654
1655 if (!(flags & GRND_INSECURE) && !crng_ready()) {
1656 int ret;
1657
1658 if (flags & GRND_NONBLOCK)
1659 return -EAGAIN;
1660 ret = wait_for_random_bytes();
1661 if (unlikely(ret))
1662 return ret;
1663 }
1664 return get_random_bytes_user(buf, count);
1665}
1666
1667/********************************************************************
1668 *
1669 * Sysctl interface
1670 *
1671 ********************************************************************/
1672
1673#ifdef CONFIG_SYSCTL
1674

--- 99 unchanged lines hidden ---
1696/********************************************************************
1697 *
1698 * Sysctl interface
1699 *
1700 ********************************************************************/
1701
1702#ifdef CONFIG_SYSCTL
1703

--- 99 unchanged lines hidden ---