mirror of
https://github.com/clearlinux/graphene.git
synced 2026-09-05 21:31:36 +00:00
[LibOS] Don't use insecure random
This commit is contained in:
@@ -785,7 +785,6 @@ static_always_inline void * current_stack(void)
|
||||
|
||||
void get_brk_region (void ** start, void ** end, void ** current);
|
||||
|
||||
int init_randgen (void);
|
||||
int reset_brk (void);
|
||||
int init_brk_region (void * brk_region);
|
||||
int init_heap (void);
|
||||
|
||||
@@ -208,9 +208,6 @@ void md5_final (struct shim_md5_ctx * mdContext);
|
||||
/* prompt user for confirmation */
|
||||
int message_confirm (const char * message, const char * options);
|
||||
|
||||
/* get random bytes (not for crypto!) */
|
||||
void getrand (void * buffer, size_t size);
|
||||
|
||||
/* ELF binary loading */
|
||||
int check_elf_object (struct shim_handle * file);
|
||||
int load_elf_object (struct shim_handle * file, void * addr, size_t mapped);
|
||||
|
||||
@@ -50,7 +50,7 @@ objs = $(addprefix bookkeep/shim_,handle vma thread signal) \
|
||||
$(addprefix ipc/shim_,ipc ipc_helper ipc_child) \
|
||||
$(addprefix ipc/shim_ipc_,$(ipcns)) \
|
||||
elf/shim_rtld \
|
||||
$(addprefix shim_,init table syscalls checkpoint random malloc \
|
||||
$(addprefix shim_,init table syscalls checkpoint malloc \
|
||||
async parser debug) syscallas start \
|
||||
$(patsubst %.c,%,$(wildcard sys/*.c))
|
||||
graphene_lib = .lib/graphene-lib.a
|
||||
|
||||
@@ -342,7 +342,7 @@ int init_vma (void)
|
||||
uint64_t addr_rand_size =
|
||||
(PAL_CB(user_address.end) - PAL_CB(user_address.start)) * 5 / 6;
|
||||
uint64_t rand;
|
||||
getrand(&rand, sizeof(rand));
|
||||
DkRandomBitsRead(&rand, sizeof(rand));
|
||||
current_heap_top -= ALIGN_DOWN(rand % addr_rand_size);
|
||||
#endif
|
||||
|
||||
|
||||
@@ -117,16 +117,13 @@ static int dev_random_mode (const char * name, mode_t * mode)
|
||||
static int dev_random_read (struct shim_handle * hdl, void * buf,
|
||||
size_t count)
|
||||
{
|
||||
int rv = DkRandomBitsRead(buf, count);
|
||||
return rv;
|
||||
return DkRandomBitsRead(buf, count);
|
||||
}
|
||||
|
||||
static int dev_urandom_read (struct shim_handle * hdl, void * buf,
|
||||
size_t count)
|
||||
{
|
||||
// THIS IS NOT CRYPTO-SECURE, FIX!!!
|
||||
getrand(buf, count);
|
||||
return count;
|
||||
return DkRandomBitsRead(buf, count);
|
||||
}
|
||||
|
||||
static int dev_random_stat (const char * name, struct stat * stat)
|
||||
|
||||
@@ -636,7 +636,6 @@ DEFINE_PROFILE_INTERVAL(pal_tail_startup_time, pal);
|
||||
DEFINE_PROFILE_INTERVAL(pal_child_creation_time, pal);
|
||||
|
||||
DEFINE_PROFILE_CATAGORY(init, );
|
||||
DEFINE_PROFILE_INTERVAL(init_randgen, init);
|
||||
DEFINE_PROFILE_INTERVAL(init_vma, init);
|
||||
DEFINE_PROFILE_INTERVAL(init_slab, init);
|
||||
DEFINE_PROFILE_INTERVAL(init_str_mgr, init);
|
||||
@@ -725,7 +724,6 @@ int shim_init (int argc, void * args, void ** return_stack)
|
||||
#endif
|
||||
|
||||
BEGIN_PROFILE_INTERVAL();
|
||||
RUN_INIT(init_randgen);
|
||||
RUN_INIT(init_vma);
|
||||
RUN_INIT(init_slab);
|
||||
RUN_INIT(read_environs, envp);
|
||||
@@ -862,7 +860,7 @@ static int name_pipe (char * uri, size_t size, void * id)
|
||||
{
|
||||
IDTYPE pipeid;
|
||||
int len;
|
||||
getrand(&pipeid, sizeof(pipeid));
|
||||
DkRandomBitsRead(&pipeid, sizeof(pipeid));
|
||||
debug("creating pipe: pipe.srv:%u\n", pipeid);
|
||||
if ((len = snprintf(uri, size, "pipe.srv:%u", pipeid)) == size)
|
||||
return -ERANGE;
|
||||
@@ -911,7 +909,7 @@ static int name_path (char * path, size_t size, void * id)
|
||||
unsigned int suffix;
|
||||
int prefix_len = strlen(path);
|
||||
int len;
|
||||
getrand(&suffix, sizeof(suffix));
|
||||
DkRandomBitsRead(&suffix, sizeof(suffix));
|
||||
len = snprintf(path + prefix_len, size - prefix_len, "%08x", suffix);
|
||||
if (len == size)
|
||||
return -ERANGE;
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
/* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
|
||||
/* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
|
||||
|
||||
/* Copyright (C) 2014 Stony Brook University
|
||||
This file is part of Graphene Library OS.
|
||||
|
||||
Graphene Library OS is free software: you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public License
|
||||
as published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
Graphene Library OS is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
/*
|
||||
* shim_random.c
|
||||
*
|
||||
* This file contains codes for generating random numbers.
|
||||
*/
|
||||
|
||||
#include <shim_internal.h>
|
||||
#include <shim_utils.h>
|
||||
#include <shim_checkpoint.h>
|
||||
|
||||
#include <pal.h>
|
||||
|
||||
static LOCKTYPE randgen_lock;
|
||||
static unsigned long randval;
|
||||
|
||||
int init_randgen (void)
|
||||
{
|
||||
if (DkRandomBitsRead (&randval, sizeof(randval)) < sizeof(randval))
|
||||
return -EACCES;
|
||||
|
||||
debug("initial random value: %08lx\n", randval);
|
||||
create_lock(randgen_lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void getrand (void * buffer, size_t size)
|
||||
{
|
||||
size_t bytes = 0;
|
||||
lock(randgen_lock);
|
||||
|
||||
while (bytes + sizeof(uint64_t) <= size) {
|
||||
*(uint64_t *) (buffer + bytes) = randval;
|
||||
bytes += sizeof(uint64_t);
|
||||
randval = hash64(randval);
|
||||
}
|
||||
|
||||
if (bytes < size) {
|
||||
memcpy(buffer + bytes, &randval, size - bytes);
|
||||
randval = hash64(randval);
|
||||
}
|
||||
|
||||
unlock(randgen_lock);
|
||||
}
|
||||
extern_alias(getrand);
|
||||
@@ -85,7 +85,7 @@ int init_brk_region (void * brk_region)
|
||||
if (brk_region) {
|
||||
while (true) {
|
||||
uint32_t rand;
|
||||
getrand(&rand, sizeof(rand));
|
||||
DkRandomBitsRead(&rand, sizeof(rand));
|
||||
rand %= 0x2000000;
|
||||
rand = ALIGN_UP(rand);
|
||||
|
||||
|
||||
@@ -113,7 +113,7 @@ int create_checkpoint (const char * cpdir, IDTYPE * sid)
|
||||
}
|
||||
} else {
|
||||
retry:
|
||||
getrand(&cpsession->sid, sizeof(IDTYPE));
|
||||
DkRandomBitsRead(&cpsession->sid, sizeof(cpsession->sid));
|
||||
|
||||
listp_for_each_entry(s, &cp_sessions, list)
|
||||
if (s->sid == cpsession->sid)
|
||||
|
||||
@@ -184,52 +184,6 @@ int _DkInstructionCacheFlush (const void * addr, int size)
|
||||
{
|
||||
return -PAL_ERROR_NOTIMPLEMENTED;
|
||||
}
|
||||
static PAL_LOCK lock = LOCK_INIT;
|
||||
static unsigned long randval = 0;
|
||||
|
||||
static int init_randgen (void)
|
||||
{
|
||||
unsigned long val;
|
||||
|
||||
if (_DkRandomBitsRead(&val, sizeof(val)) < sizeof(val))
|
||||
return -PAL_ERROR_DENIED;
|
||||
|
||||
_DkInternalLock(&lock);
|
||||
randval = val;
|
||||
_DkInternalUnlock(&lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int getrand (void * buffer, size_t size)
|
||||
{
|
||||
unsigned long val;
|
||||
size_t bytes = 0;
|
||||
|
||||
int ret = init_randgen();
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
_DkInternalLock(&lock);
|
||||
val = randval;
|
||||
randval = hash64(~randval);
|
||||
_DkInternalUnlock(&lock);
|
||||
|
||||
while (bytes + sizeof(uint64_t) <= size) {
|
||||
*(uint64_t *) (buffer + bytes) = val;
|
||||
val = hash64(val);
|
||||
bytes += sizeof(uint64_t);
|
||||
}
|
||||
|
||||
if (bytes < size) {
|
||||
memcpy(buffer + bytes, &val, size - bytes);
|
||||
val = hash64(val);
|
||||
}
|
||||
|
||||
_DkInternalLock(&lock);
|
||||
randval = val;
|
||||
_DkInternalUnlock(&lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _DkCpuIdRetrieve (unsigned int leaf, unsigned int subleaf,
|
||||
unsigned int values[4])
|
||||
|
||||
@@ -47,8 +47,6 @@ typedef int __kernel_pid_t;
|
||||
#define ERRNO INTERNAL_SYSCALL_ERRNO
|
||||
#define ERRNO_P INTERNAL_SYSCALL_ERRNO_P
|
||||
|
||||
int getrand (void * buffer, size_t size);
|
||||
|
||||
struct timespec;
|
||||
struct timeval;
|
||||
extern struct pal_bsd_state {
|
||||
|
||||
Reference in New Issue
Block a user