summaryrefslogtreecommitdiffstats
path: root/lib/randutils.c
Commit message (Collapse)AuthorAgeFilesLines
* lib/randutils: don't break on EAGAIN, use usleep()Karel Zak2018-03-201-5/+12
| | | | | | | | | | | | | | | | | The current code uses lose_counter to make more attempts to read random numbers. It seems better to wait a moment between attempts to avoid busy loop (we do the same in all-io.h). The worst case is 1 second delay for all random_get_bytes() on systems with uninitialized entropy pool -- for example you call sfdisk (MBR Id or GPT UUIDs) on very first boot, etc. In this case it will use libc rand() as a fallback solution. Note that we do not use random numbers for security sensitive things like keys or so. It's used for random based UUIDs etc. Addresses: https://github.com/karelzak/util-linux/pull/603 Signed-off-by: Karel Zak <kzak@redhat.com>
* lib/randutils: Do not block on getrandom()Carlo Caione2018-03-191-1/+5
| | | | | | | | | | | | In Endless we have hit a problem when using 'sfdisk' on the really first boot to automatically expand the rootfs partition. On this platform 'sfdisk' is blocking on getrandom() because not enough random bytes are available. This is an ARM platform without a hwrng. We fix this passing GRND_NONBLOCK to getrandom(). 'sfdisk' will use the best entropy it has available and fallback only as necessary. Signed-off-by: Carlo Caione <carlo@endlessm.com>
* lib/randutils: remove superfluous continueKarel Zak2017-10-021-6/+3Star
| | | | | Addresses: https://github.com/karelzak/util-linux/commit/fa949792072530b49ef5760d7ecd6d250c66e35f#commitcomment-24678670 Signed-off-by: Karel Zak <kzak@redhat.com>
* misc: fix some printf format stringsRuediger Meier2017-08-161-2/+4
| | | | | | Noticed on xcode/OSX. Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
* lib/randutils: reset lose counterKarel Zak2017-08-141-1/+1
| | | | | | | It's probaly good idea to reset lose counter when we fallback from getrandom() to /dev/urandom. Signed-off-by: Karel Zak <kzak@redhat.com>
* lib/randutils: improve getrandom() usageKarel Zak2017-08-141-18/+46
| | | | | | | | | | | | | | | | | | | | The getrandom() does not have to return all requested bytes (missing entropy or when interrupted by signal). The current implementation in util-linux stupidly asks for all random data again, rather than only for missing bytes. The current code also does not care if we repeat our requests for ever; that's bad. This patch uses the same way as we already use for reading from /dev/urandom. It means: * repeat getrandom() for only missing bytes * limit number of unsuccessful request (16 times) * fallback to /dev/urandom on ENOSYS (old kernel or so...) Addresses: https://github.com/karelzak/util-linux/issues/496 Signed-off-by: Karel Zak <kzak@redhat.com>
* lib/randutils.c: More paranoia in getrandom() call.Christopher James Halse Rogers2017-08-081-1/+1
| | | | | | | | If getrandom() is called with nbytes ≥ 256 then it can return with less than the requested bytes filled. In this case we *could* adjust the buffer by the number of bytes actually read, but it's simpler to just redo the call.
* lib/randutils.c: Fall back gracefully when kernel doesn't support getrandom(2).Christopher James Halse Rogers2017-08-081-18/+26
| | | | | | | The 3.16 kernel is supported until 2020, and various distros have kernels of the same vintage. It's entirely possible for code built against newer headers to be run against these kernels, so fall-back to the old “read /dev/{u,}random” method if the kernel doesn' support getrandom()
* build-sys: remove duplicate includesKarel Zak2017-05-121-1/+0Star
| | | | Signed-off-by: Karel Zak <kzak@redhat.com>
* misc: do not use plain 0 as NULL [smatch scan]Sami Kerola2017-02-201-2/+2
| | | | | | | | | | text-utils/tailf.c:69:21: warning: Using plain integer as NULL pointer Since many 'struct option' has used zero as NULL make them more readable in same go by reindenting, and using named argument requirements. Reference: https://lwn.net/Articles/93577/ Signed-off-by: Sami Kerola <kerolasa@iki.fi>
* lib/randutils: glibc 2.25 has getrandom(2) declarationSami Kerola2017-02-131-1/+1
| | | | | | | Use correct header file to include the function. Reference: http://git.kernel.org/cgit/docs/man-pages/man-pages.git/commit/?id=2cbb6fb4e926e56dc3985b19ac02389321a0af5d Signed-off-by: Sami Kerola <kerolasa@iki.fi>
* lib: use unique ifdefs for testsKarel Zak2017-01-041-2/+2
| | | | | | | Let's use unique TEST_PROGRAM_<NAME> ifdefs to make build system more robust. Signed-off-by: Karel Zak <kzak@redhat.com>
* lib/randutils: use getrandom(2) when it is availableSami Kerola2016-12-111-16/+53
| | | | | | | | | | | | | | | | | | | | System call getrandom(2) is relatively new, available since kernel 3.17 but not supported by glibc 2.24. That in mind autotools is made to check availability of this function and keep old code as fallback. It is reasonable assume it will take years before the syscall(2) and fallback are unproblematic to remove. One might ask why bother using getrandom(2). Main reason is to avoid unnecessary system calls to achieve exactly same end result. That demonstrated with 'strace -c ./mcookie' showing 36 calls before, and 32 after this change. Secondly the getrandom(2) function got to kernel with promise it can be used to avoid file descriptor run down, and since uuidd uses random_get_bytes() it should fulfill it's promise here. Reference: http://man7.org/linux/man-pages/man2/getrandom.2.html Reference: https://lwn.net/Articles/606141/ Reviewed-by: Karel Zak <kzak@redhat.com> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
* lib/randutils: remove unnecessary functionKarel Zak2016-10-041-9/+3Star
| | | | Signed-off-by: Karel Zak <kzak@redhat.com>
* lib/randutils: add xsrand() and rand_get_number()Karel Zak2016-09-061-3/+15
| | | | Signed-off-by: Karel Zak <kzak@redhat.com>
* mcookie: use lib/randutilsSami Kerola2014-03-261-0/+22
| | | | | | | | | The mcookie should reuse existing code, and there is definitely no need to prefer /dev/random for this utility. See reference for explanation about later statement. References: http://www.2uo.de/myths-about-urandom/ Signed-off-by: Sami Kerola <kerolasa@iki.fi>
* lib: use O_CLOEXEC in libcommonKarel Zak2013-04-031-2/+2
| | | | Signed-off-by: Karel Zak <kzak@redhat.com>
* include: cleanup copyright headersKarel Zak2013-01-081-1/+5
| | | | | | | We use the code from include/ and lib/ on many places, so use public domain if possible or LGPL for code copied from libs. Signed-off-by: Karel Zak <kzak@redhat.com>
* build: fix unused parameter warningsSami Kerola2012-06-111-1/+2
| | | | | | | | | | | | | | | pager.c:203:14: warning: unused parameter 'argc' [-Wunused-parameter] pager.c:203:26: warning: unused parameter 'argv' [-Wunused-parameter] randutils.c:108:14: warning: unused parameter 'argc' [-Wunused-parameter] randutils.c:108:26: warning: unused parameter 'argv' [-Wunused-parameter] optstr.c:774:37: warning: unused parameter 'optstr' [-Wunused-parameter] optstr.c:774:51: warning: unused parameter 'value' [-Wunused-parameter] optstr.c:774:65: warning: unused parameter 'valsz' [-Wunused-parameter] optstr.c:774:79: warning: unused parameter 'next' [-Wunused-parameter] Signed-off-by: Sami Kerola <kerolasa@iki.fi>
* libuuid: avoid double open and leaking fd (reworked)Petr Uzel2012-05-101-5/+3Star
| | | | | | | | | | | | | | This reverts commit 6126f7a53c57485a9a29ddd772765695f23c92e6 and fixes the double open and leaking descriptor in a different way, that is by using newly introduced function 'have_random_source()' to check whether good random source is available while deciding which uuid type to generate (random/time). This is better than calling random_get_fd() twice, passing the file descriptor down the stack and reusing it in next call to random_get_fd(). Signed-off-by: Petr Uzel <petr.uzel@suse.cz>
* libuuid: avoid double open and leaking descriptorPetr Uzel2012-05-041-3/+5
| | | | | | | | | | | | | We are opening /dev/urandom twice in uuid_generate(): first to check if the file is available and then later __uuid_generate_random() again to actually get the random data. Moreover, descriptor from the first open is leaking. Fix by passign the descriptor down the stack and reusing it there. References: http://marc.info/?l=util-linux-ng&m=133406051131131&w=2 Signed-off-by: Petr Uzel <petr.uzel@suse.cz>
* lib: random utilitiesDavidlohr Bueso2012-04-101-0/+120
Add a random number(s) generator specific file. The intial functions are based on what libuuid provide. I did some modifications like avoid WIN32 checks - this is util-LINUX. [kzak@redhat.com: - move jrand_seed to lib/randutils.c - use TLS for jrand_seed (like original code from libuuid) - use size_t for buffer sizes - add close() to random_get_bytes] Signed-off-by: Davidlohr Bueso <dave@gnu.org> Signed-off-by: Karel Zak <kzak@redhat.com>