Commit Graph

30 Commits

Author SHA1 Message Date
Samanta Navarro e4be3ee01d libuuid: check quality of random bytes
If a libuuid application is unable to access /dev/random or /dev/urandom
then uuid generation by uuid_generate falls back to uuid_generate_time.
This could happen in chroot or container environments.

The function ul_random_get_bytes from lib/randutils.c uses getrandom if
it is available. This could either mean that the libuuid application
skips good random bytes because the character special files do not exist
or the application trusts in good random bytes just because these files
are accessible but not necessarily usable, e.g. limit of open file
descriptors reached, lack of data, kernel without getrandom, etc.

This commit modifies ul_random_get_bytes to return an integer which
indicates if random bytes are of good quality (0) or not (1). Callers
can decide based on this information if they want to discard the random
bytes. Only libuuid checks the return value. I decided to return 1
instead of -1 because -1 feels more like an error, but weak random bytes
can be totally fine.

Another issue is that getrandom sets errno to specific values only in
case of an error, i.e. with return value -1. Set errno to 0 explicitly
if getrandom succeeds so we do not enter the fallback routine for
ENOSYS by mistake. I do not think that this is likely to happen, but it
really depends on possible wrapper function supplied by a C library.

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
2020-11-08 11:48:23 +00:00
Samanta Navarro bd0f347f86 misc: fix typos
Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
2020-11-04 11:42:28 +00:00
Sami Kerola 364de8f4f5 lib/randutils: rename random_get_bytes()
Rename random_get_bytes() to avoid colliding a Solaris library function that
has the same name.

Reported-by: Sad Clouds <cryintothebluesky@gmail.com>
Reference: https://lore.kernel.org/util-linux/20201101141608.ba365cf67d92ee3973226de9@gmail.com/
Reference: https://blogs.oracle.com/solaris/solaris-random-number-generation-v2
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-11-03 12:38:45 +01:00
Eric Simpson 811351ac98 do not require sys/syscall.h on non-linux platforms
In order to build util-linux on other platforms (such as IBM i), do not require the linux-specific sys/syscall.h.
2020-10-13 12:55:05 -04:00
Yuri Chornoivan 311e33afef Fix minor typos 2020-07-09 20:14:32 +03:00
Rosen Penev bd89499e07
[clang-tidy] do not return in void functions
Found with readability-redundant-control-flow

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2020-04-19 14:03:21 -07:00
Karel Zak 3534b2c7c7 lib/randutils: use explicit data types for bit ops
ASAN is pretty unhappy with getpid() << 16, it seems better to save
into unsigned int and than do the bit-op.

Addresses: https://github.com/karelzak/util-linux/issues/942
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-01-28 12:30:23 +01:00
Karel Zak dde4b59369 lib/randutils: re-licensing back to BSD
The file is originally from libuuid, this library is under BSD
licence. Unfortunately, I have added LGPL header by accident to the
file (commit 0f23ee0c85).

The file under LGPL was modified (in relevant way) by Sami,
Christopher and me. We all agree with re-licensing back to BSD.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Christopher James Halse Rogers <chris@cooperteam.net>
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-12-09 11:30:55 +01:00
Karel Zak edc1c90cb9 lib/randutils: don't break on EAGAIN, use usleep()
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>
2018-03-20 14:17:24 +01:00
Carlo Caione a9cf659e05 lib/randutils: Do not block on getrandom()
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>
2018-03-19 14:06:09 +01:00
Karel Zak 43dc893449 lib/randutils: remove superfluous continue
Addresses: fa94979207 (commitcomment-24678670)
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-10-02 11:30:49 +02:00
Ruediger Meier e282116f6d misc: fix some printf format strings
Noticed on xcode/OSX.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-08-16 15:51:15 +02:00
Karel Zak 51013d3efa lib/randutils: reset lose counter
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>
2017-08-14 10:56:08 +02:00
Karel Zak 5264aebb4f lib/randutils: improve getrandom() usage
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>
2017-08-14 10:33:06 +02:00
Christopher James Halse Rogers cc7d1fd16d lib/randutils.c: More paranoia in getrandom() call.
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.
2017-08-08 11:28:25 +10:00
Christopher James Halse Rogers a7df0f5f35 lib/randutils.c: Fall back gracefully when kernel doesn't support getrandom(2).
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()
2017-08-08 11:27:49 +10:00
Karel Zak b8af37d767 build-sys: remove duplicate includes
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-05-12 11:17:02 +02:00
Sami Kerola 8791804065 misc: do not use plain 0 as NULL [smatch scan]
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>
2017-02-20 12:58:49 +01:00
Sami Kerola b192dd6943 lib/randutils: glibc 2.25 has getrandom(2) declaration
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>
2017-02-13 14:14:28 +01:00
Karel Zak e8f7acb0d3 lib: use unique ifdefs for tests
Let's use unique TEST_PROGRAM_<NAME> ifdefs to make build system more
robust.

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-01-04 11:44:37 +01:00
Sami Kerola cc01c2dca4
lib/randutils: use getrandom(2) when it is available
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>
2016-12-11 11:46:54 +00:00
Karel Zak 0b27fcc189 lib/randutils: remove unnecessary function
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-10-04 16:18:16 +02:00
Karel Zak a55d646bd3 lib/randutils: add xsrand() and rand_get_number()
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-09-06 10:19:52 +02:00
Sami Kerola 0720d60cc1 mcookie: use lib/randutils
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>
2014-03-26 10:36:18 +01:00
Karel Zak b1fa3e2234 lib: use O_CLOEXEC in libcommon
Signed-off-by: Karel Zak <kzak@redhat.com>
2013-04-03 16:13:06 +02:00
Karel Zak 0f23ee0c85 include: cleanup copyright headers
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>
2013-01-08 15:10:17 +01:00
Sami Kerola 838c5f6bb8 build: fix unused parameter warnings
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>
2012-06-11 19:51:35 +02:00
Petr Uzel c544aa2c25 libuuid: avoid double open and leaking fd (reworked)
This reverts commit 6126f7a53c
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>
2012-05-10 11:43:49 +02:00
Petr Uzel 6126f7a53c libuuid: avoid double open and leaking descriptor
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>
2012-05-04 15:14:24 +02:00
Davidlohr Bueso fe72459e41 lib: random utilities
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>
2012-04-10 13:12:43 +02:00