jitterentropy: remove deprecated env() usage

This change introduces a Genode specific init function, which sets the
backend allocator used by jent_zalloc/zfree(). As consequence the
library can solely be used by native Genode components, direct libc
usage is not supported.

Fixes #2274.
This commit is contained in:
Josef Söntgen 2017-02-08 13:54:47 +01:00 committed by Norman Feske
parent 22f02396c0
commit c407fb80fa
7 changed files with 92 additions and 33 deletions

View File

@ -1,24 +1,26 @@
/**
* \brief Add random support for CGD
* \author Sebastian Sumpf
* \author Josef Soentgen
* \date 2015-02-13
*/
/*
* Copyright (C) 2015 Genode Labs GmbH
* Copyright (C) 2015-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
/* Genode includes */
#include <base/log.h>
/* local rump includes */
#include <rump/env.h>
#include <util/random.h>
extern "C" {
namespace Jitter {
#include <jitterentropy.h>
}
}
/* library includes */
#include <jitterentropy.h>
typedef Genode::size_t size_t;
@ -29,24 +31,31 @@ typedef Genode::size_t size_t;
struct Entropy
{
struct Jitter::rand_data *ec_stir;
struct rand_data *ec_stir;
Entropy()
{
Jitter::jent_entropy_init();
ec_stir = Jitter::jent_entropy_collector_alloc(0, 0);
}
struct Initialization_failed : Genode::Exception { };
static Entropy *e()
Entropy(Genode::Allocator &alloc)
{
static Entropy _e;
return &_e;
jitterentropy_init(alloc);
int err = jent_entropy_init();
if (err) {
Genode::error("could not initialize jitterentropy library");
throw Initialization_failed();
}
ec_stir = jent_entropy_collector_alloc(0, 0);
if (ec_stir == nullptr) {
Genode::error("could not initialize jitterentropy library");
throw Initialization_failed();
}
}
size_t read(char *buf, size_t len)
{
int err;
if ((err = Jitter::jent_read_entropy(ec_stir, buf, len) < 0)) {
if ((err = jent_read_entropy(ec_stir, buf, len) < 0)) {
Genode::error("failed to read entropy: ", err);
return 0;
}
@ -56,8 +65,25 @@ struct Entropy
};
static Genode::Constructible<Entropy> _entropy;
static bool _init_failed;
int rumpuser_getrandom_backend(void *buf, size_t buflen, int flags, Genode::size_t *retp)
{
*retp = Entropy::e()->read((char *)buf, buflen);
if (!_entropy.constructed()) {
if (_init_failed) {
*retp = 0;
return -1;
}
try { _entropy.construct(Rump::env().heap()); }
catch (Entropy::Initialization_failed) {
_init_failed = true;
return -1;
}
}
*retp = _entropy->read((char *)buf, buflen);
return 0;
}

View File

@ -1 +1 @@
0105c3c8c4b39c25aa0ecb7cd1ba6894571f37f0
518a548281860815460342cf0e4afec305c07eee

View File

@ -5,28 +5,40 @@
*/
/*
* Copyright (C) 2014 Genode Labs GmbH
* Copyright (C) 2014-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
/* Genode includes */
#include <base/env.h>
#include <base/allocator.h>
#include <util/string.h>
/* local includes */
#include <jitterentropy-base-genode.h>
static Genode::Allocator *_alloc;
void jitterentropy_init(Genode::Allocator &alloc)
{
_alloc = &alloc;
}
void *jent_zalloc(size_t len)
{
return Genode::env()->heap()->alloc(len);
if (!_alloc) { return 0; }
return _alloc->alloc(len);
}
void jent_zfree(void *ptr, unsigned int len)
{
Genode::env()->heap()->free(ptr, len);
if (!_alloc) { return; }
_alloc->free(ptr, 0);
}

View File

@ -5,7 +5,7 @@
*/
/*
* Copyright (C) 2014 Genode Labs GmbH
* Copyright (C) 2014-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
@ -17,7 +17,7 @@
/* needed type definitions */
#include <base/fixed_stdint.h>
typedef __SIZE_TYPE__ size_t;
typedef unsigned long size_t;
typedef signed long ssize_t;
typedef genode_uint32_t uint32_t;
typedef genode_uint64_t uint64_t;
@ -30,6 +30,12 @@ typedef genode_int64_t __s64;
#endif
#ifdef __cplusplus
#include <base/allocator.h>
/* Genode specific function to set the backend allocator */
void jitterentropy_init(Genode::Allocator &alloc);
extern "C" {
#endif

View File

@ -1,6 +1,6 @@
--- a/jitterentropy.h 2014-04-07 21:07:13.000000000 +0200
+++ b/jitterentropy.h 2014-08-18 14:42:32.609235231 +0200
@@ -42,11 +42,15 @@
--- a/jitterentropy.h.orig
+++ b/jitterentropy.h
@@ -42,11 +42,19 @@
#ifndef _JITTERENTROPY_H
#define _JITTERENTROPY_H
@ -12,7 +12,20 @@
#else
#include "jitterentropy-base-user.h"
#endif /* __KERNEL__ */
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
/* Statistical data from the entropy source */
struct entropy_stat {
@@ -171,4 +179,8 @@
/* -- END of statistical test function -- */
+#ifdef __cplusplus
+}
+#endif /* extern "C" */
+
#endif /* _JITTERENTROPY_H */

View File

@ -24,7 +24,7 @@ struct Jitterentropy_factory : Vfs::File_system_factory
Genode::Xml_node node,
Vfs::Io_response_handler &) override
{
return new (alloc) Jitterentropy_file_system(node);
return new (alloc) Jitterentropy_file_system(alloc, node);
}
};

View File

@ -19,9 +19,7 @@
#include <vfs/single_file_system.h>
/* jitterentropy includes */
extern "C" {
#include <jitterentropy.h>
}
class Jitterentropy_file_system : public Vfs::Single_file_system
{
@ -30,8 +28,11 @@ class Jitterentropy_file_system : public Vfs::Single_file_system
struct rand_data *_ec_stir;
bool _initialized;
bool _init_jitterentropy()
bool _init_jitterentropy(Genode::Allocator &alloc)
{
/* initialize private allocator backend */
jitterentropy_init(alloc);
int err = jent_entropy_init();
if (err) {
Genode::error("jitterentropy library could not be initialized!");
@ -50,11 +51,12 @@ class Jitterentropy_file_system : public Vfs::Single_file_system
public:
Jitterentropy_file_system(Genode::Xml_node config)
Jitterentropy_file_system(Genode::Allocator &alloc,
Genode::Xml_node config)
:
Single_file_system(NODE_TYPE_CHAR_DEVICE, name(), config),
_ec_stir(0),
_initialized(_init_jitterentropy())
_initialized(_init_jitterentropy(alloc))
{ }
~Jitterentropy_file_system()