Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #70409
| From | Paavo Helde <myfirstname@osa.pri.ee> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: One agnostic random function for big project (4 GB of code) |
| Date | 2019-12-12 14:31 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <qstbuv$pem$1@dont-email.me> (permalink) |
| References | <6803eb03-e599-47eb-9e1b-fc064e5df946@googlegroups.com> |
On 12.12.2019 13:53, Frederick Gotham wrote:
>
> I'm working on a large embedded Linux project with about 4 gigs of source code.
>
> I'm trying to create one header file that can be included in any sub-project, which will have a function for generating random bytes of data. The only two requirement of using this random number generator will be:
> (1) You must include "agnostic_random.hpp" in the source file(s) where it's used
> (2) When linking, you must link with "libdl.so"
>
> In this header file, I'm taking advantage of the "inline" keyword because of its unique linking characteristics, specifically that I can have a static object inside an inline function and it will only exist once (and it will only be initialised once for the current process).
>
> This random number generator must be fully thread-safe.
>
> At the moment I've written it to use "RAND_bytes" provided by OpenSSL, but this might change later (e.g. I might use tpm2-tss directly).
>
> Please give me a critique of this header file and let me know if I've overlooked anything.
TBH, at the moment it looks like you are trying to sneak in "accidental"
back-doors into this large Linux project. More below.
>
> #ifndef HPP_INCLUSION_GUARD_AGNOSTIC_RANDOM
> #define HPP_INCLUSION_GUARD_AGNOSTIC_RANDOM
>
> #ifndef __cplusplus
> # error "This is a C++ header file but you've included it in a C source file (or you've used a C compiler on a C++ source file). Don't do that."
> #endif
>
> #include <cstdint> /* uintmax_t */
>
> #include <atomic> /* atomic_flag */
> #include <limits> /* numeric_limits<int> */
>
> // The next two lines are declarations of functions found in
> // "libdl.so". (Unfortunately the compiler won't let me put
> // these declarations inside the body of a function).
> extern "C" void *dlopen(char const *, int);
> extern "C" void *dlsym(void *,char const *);
>
Namespace is missing. All C++ code should be in a namespace, especially
if used so widely.
> inline void Agnostic_Random(void *pvoid, std::uintmax_t len_bytes) // len_bytes can be 0
Buffer length should be in size_t, not some random type.
> {
> static std::atomic_flag is_not_first_time = ATOMIC_FLAG_INIT;
>
> typedef int (*RAND_bytes_t)(char unsigned *, int);
> static std::atomic<RAND_bytes_t> ptr_to_RAND_bytes(nullptr);
>
> if ( !is_not_first_time.test_and_set() )
> {
> void *const handle = dlopen("libcrypto.so", 1); /* 1 == RTLD_LAZY */
The handle is leaked. There should be a matching call to dlclose(). This
could be achieved pretty easily by having a static C++ class object with
a destructor.
>
> if ( nullptr != handle )
> ptr_to_RAND_bytes = reinterpret_cast<RAND_bytes_t>( dlsym(handle, "RAND_bytes") );
> }
A race condition. There is a time slice when the flag is set, but the
function pointer is not yet assigned. If another thread calls this
function during this time, it will see a wrong (NULL) function pointer.
Instead suggesting a simple:
static RAND_bytes_t ptr_to_RAND_bytes = SomeInitFunction();
This is guaranteed to do the correct thing since C++11.
> if ( nullptr == ptr_to_RAND_bytes )
> return;
No error return or exception throw. Extremely bad style, especially if
this has anything to do with crypto.
>
> while ( 0 != len_bytes )
> {
> int bytes_this_time; // RAND_bytes unfortunately takes an 'int' for length
>
> if ( len_bytes > std::numeric_limits<int>::max() )
Signed-unsigned comparison, compilers will produce warnings.
> bytes_this_time = std::numeric_limits<int>::max();
> else
> bytes_this_time = len_bytes;
>
> ptr_to_RAND_bytes( static_cast<char unsigned *>(pvoid), bytes_this_time );
No error check! Extremely bad style, especially in anything related to
crypto!
>
> pvoid = static_cast<char*>(pvoid) + bytes_this_time; // Cast to char pointer for arithmetic
> len_bytes -= bytes_this_time;
> }
> }
>
> #endif // ifndef HPP_INCLUSION_GUARD_JCI_INSIGHT_RANDOM
Comment is wrong.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
One agnostic random function for big project (4 GB of code) Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-12-12 03:53 -0800
Re: One agnostic random function for big project (4 GB of code) Bart <bc@freeuk.com> - 2019-12-12 12:01 +0000
Re: One agnostic random function for big project (4 GB of code) Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-12-12 04:06 -0800
Re: One agnostic random function for big project (4 GB of code) boltar@nowhere.co.uk - 2019-12-12 12:22 +0000
Iceream is your friend (was Re: One agnostic random function for big project (4 GB of code)) Vir Campestris <vir.campestris@invalid.invalid> - 2019-12-13 21:51 +0000
Re: One agnostic random function for big project (4 GB of code) Paavo Helde <myfirstname@osa.pri.ee> - 2019-12-12 14:31 +0200
Re: One agnostic random function for big project (4 GB of code) Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-12-12 04:47 -0800
Re: One agnostic random function for big project (4 GB of code) Paavo Helde <myfirstname@osa.pri.ee> - 2019-12-12 15:15 +0200
Re: One agnostic random function for big project (4 GB of code) Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-12-12 05:27 -0800
Re: One agnostic random function for big project (4 GB of code) Paavo Helde <myfirstname@osa.pri.ee> - 2019-12-12 16:25 +0200
Re: One agnostic random function for big project (4 GB of code) Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-12-12 06:33 -0800
Re: One agnostic random function for big project (4 GB of code) Paavo Helde <myfirstname@osa.pri.ee> - 2019-12-12 16:40 +0200
Re: One agnostic random function for big project (4 GB of code) Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-12-12 07:26 -0800
Re: One agnostic random function for big project (4 GB of code) Paavo Helde <myfirstname@osa.pri.ee> - 2019-12-12 17:37 +0200
Re: One agnostic random function for big project (4 GB of code) Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-12-12 07:48 -0800
Re: One agnostic random function for big project (4 GB of code) Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-12-12 08:27 -0800
Re: One agnostic random function for big project (4 GB of code) Manfred <noname@add.invalid> - 2019-12-12 17:41 +0100
csiph-web