31 Commits

Author SHA1 Message Date
Eric Hawicz
8c987e0b73 Issue #919: Fix build on non-x86 windows. Patch thanks to @dotanbrk 2026-02-19 18:48:05 -05:00
Rosen Penev
66f7869219 rename WIN32 to _WIN32
The latter is the proper macro defined by Windows headers.

Fixes compilation under at least clang-cl which mandates function
declarations.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2024-02-07 18:06:55 -08:00
Even Rouault
f6c8eba84e random_seed.c: add a Coverity Scan suppression
Coverity Scan warns about the use of open() after stat() being a
potential TOCTOU (Time of check time of use) issue. But here there is no
such issue.
2024-01-07 20:55:38 +01:00
Even Rouault
57ea393004 get_time_seed(): silence warning emitted by Coverity Scan static analyzer
It warns about the return of time() being truncated to 32 bit, which is
not an issue here.
(this warning was emitted because of the https://github.com/OSGeo/gdal
project embedding a copy of libjson-c and running Coverity Scan
analysis)
2022-08-16 11:11:58 +02:00
Kizuna-Meraki
d783ad76c7 Close file on error path.
The file was only be closed when there was no error and
was being left open when there was an error. By moving
the close(fd) statement out of the if-clause, the file
can be close regardless if there is an error or not.
After the file is closed, it can be checked for errors.
2022-02-17 21:27:01 +01:00
Robert Bielik
286b4fdd27 Fix uninitialized value error for clang-8 msan 2021-11-11 10:23:05 +01:00
Philosoph228
9c0565100a random_seed: fix unused variable for win32 build 2021-04-13 11:47:01 +05:00
Eric Haszlakiewicz
f787810890 If arc4random is used, don't bother compiling in the other fallback methods since they'll never be used. Fixes PR#695 about unreachable code too. 2021-02-13 03:23:58 +00:00
Eric Haszlakiewicz
0f61f6921b Iesue #692: use arc4random() if it's available (in libc on BSD systems, and libbsd on Linux). 2021-01-13 01:57:25 +00:00
Pierce Lopez
0fd3b7d316 random_seed: on error, continue to next method
instead of exiting the process
2020-10-07 01:22:30 -04:00
Tobias Stoeckmann
df62119b7f Prevent signed overflow in get_time_seed
Casting time(2) return value to int and multiplying the result with
such a constant will definitely lead to a signed overflow by this day.

Since signed overflows are undefined behaviour in C, avoid this.

Casting to unsigned is more than enough since the upper bits of a
64 bit time_t value will be removed with the int conversion anyway.
2020-08-22 13:25:21 +02:00
Tobias Stoeckmann
f052e42f56 Use GRND_NONBLOCK with getrandom.
The json-c library is used in cryptsetup for LUKS2 header information.
Since cryptsetup can be called very early during boot, the developers
avoid getrandom() calls in their own code base for now. [1]

Introducing a blocking getrandom() call in json-c therefore introduces
this issue for cryptsetup as well. Even though cryptsetup issues do not
have to be json-c issues, here is my proposal:

Let's use a non-blocking call, falling back to other sources if the call
would block. Since getrandom() accesses urandom, it must mean that we
are in an early boot phase -- otherwise the call would not block
according to its manual page.

As stated in manual page of random(4), accessing /dev/urandom won't
block but return weak random numbers, therefore this fallback would work
for json-c.

While at it, fixed the debug message.

[1] https://gitlab.com/cryptsetup/cryptsetup/-/merge_requests/47
    which references to https://lwn.net/Articles/800509/
2020-08-15 15:52:17 +02:00
Alan Coopersmith
6cf4847796 Use getrandom() if available in json_c_get_random_seed
Lower overhead than opening & reading from /dev/urandom, and works
in chroots and other situtations where /dev/urandom is not available.
Falls back to existing methods when kernel doesn't support the syscall.
2020-07-31 08:28:07 -07:00
Eric Haszlakiewicz
730e3d044f Issue #594 - provide an OVERRIDE_GET_RANDOM_SEED cmake variable to override json_c_get_random_seed() for embedded platforms where time(NULL) doesn't work.
Example:
mkdir build && cd build
cmake -DOVERRIDE_GET_RANDOM_SEED='do { extern uint32_t getMsTicks(void); int ms = getMsTicks() * 433494437; return ms; } while(0)' ..
2020-07-22 02:25:03 +00:00
Gianluigi Tiesi
481d0a8ede get_cryptgenrandom_seed: compat with old windows + fallback 2020-06-03 07:51:42 +02:00
Eric Haszlakiewicz
8086314026 Issue #589: drop the rdrand test loops to just 3, tweak comments and add some links to bug reports, and decrease the nesting level of the has_rdrand() function. 2020-05-04 01:33:15 +00:00
Tudor Brindus
4d36b0287d Detect broken RDRAND during initialization
Some CPUs advertise RDRAND in CPUID, but return 0xFFFFFFFF
unconditionally. To avoid locking up later, test RDRAND during
initialization, and if it returns 0xFFFFFFFF, mark it as nonexistent.

Fixes #588.
2020-05-03 15:15:24 -04:00
Tudor Brindus
0e5bbcaa16 Fix segmentation fault in CPUID check 2020-05-03 14:39:31 -04:00
Eric Haszlakiewicz
5e19c26cae Don't let clang-format rearrange Windows includes, since they need to be in a particular order. 2020-04-06 02:47:34 +00:00
Eric Haszlakiewicz
9acc4e9d71 Format random_seed.c with clang-format, with key asm sections excluded. 2020-04-06 01:52:28 +00:00
Eric Haszlakiewicz
7f30afc6e5 Fix some Windows compile issues, add JSON_EXPORT's, fix bogus character escapes, define __func__ and omit unistd.h if needed. 2019-11-23 20:31:14 -05:00
Rubasri Kalidas
3003161eff Fix compiler warnings 2018-12-18 11:30:57 -06:00
Haffon
3141c3976b 1.make it can been compiled with Visual Studio 2010
2.replace json_object_get/put API with json_object_retain/release, as they operate the reference counter, and confused with array_list_get/put_idx.
3.replace array_list_get/put_idx API with array_list_get/insert to make them more clear to use.
2017-08-22 13:53:47 +08:00
Alexandru Ardelean
fb72160caf build: make strerror() override-able
If we want to override `strerror()` in libjson-c
to make tests consistent across platforms, we
need to do it build-wide as configure/build
option.

Apple linkers make it really hard to override functions
at link-time, and this seems to be locked down on travis-ci.org
[ for security reasons I assume ].
While I got it to work locally, it did not work
when running on travis.

Signed-off-by: Alexandru Ardelean <ardeleanalex@gmail.com>
2017-07-13 10:27:24 +03:00
James Myatt
bf32650c83 Add missing include file to random_seed 2015-03-04 10:45:37 +00:00
Eric Haszlakiewicz
9db3099572 Merge pull request #156 from jubalh/master
Remove trailing whitespaces
2015-03-03 21:37:02 -05:00
Eric Haszlakiewicz
2c722277ee Merge pull request #150 from ams-cs/master
Fix build using MinGW.
2014-09-13 22:17:57 -04:00
Michael Vetter
fcf5ad1bd6 Remove trailing whitespace 2014-08-26 14:48:59 +02:00
Andrew Stubbs
ca0ebe0f71 Fix build using MinGW.
MinGW requires wincrypt.h.
GCC does not support #pragma comment, which trips Werror.
2014-08-04 11:44:25 +01:00
Alexandru Costache
4841c48f81 Removed duplicate check in random_seed test - bug #140 2014-07-04 12:28:35 +03:00
Michael Clark
64e36901a0 Patch to address the following issues:
* CVE-2013-6371: hash collision denial of service
* CVE-2013-6370: buffer overflow if size_t is larger than int
2014-04-09 13:48:21 +08:00