-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SIGIO signal to fill fake /dev/urandom (#310)
- Loading branch information
1 parent
f89b24d
commit 6955cd8
Showing
6 changed files
with
170 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
Copyright 2015-2020 Clément Gallet <clement.gallet@ens-lyon.org> | ||
This file is part of libTAS. | ||
libTAS is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
libTAS 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 General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with libTAS. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "URandom.h" | ||
|
||
#include "FileHandleList.h" | ||
#include "../logging.h" | ||
#include <fcntl.h> | ||
#include <unistd.h> // getpid() | ||
|
||
namespace libtas { | ||
|
||
static int readfd = -1; | ||
static int writefd = -1; | ||
static char* datestr = nullptr; | ||
static FILE* stream = nullptr; | ||
|
||
static void urandom_handler(int signum) | ||
{ | ||
/* TODO: write a simple PRNG which is seeded by shared_config.initial_time_sec | ||
* instead of outputting it directly as a string. | ||
*/ | ||
debuglogstdio(LCF_FILEIO | LCF_RANDOM, "Filling urandom fd"); | ||
if (!datestr) { | ||
time_t tsec = static_cast<time_t>(shared_config.initial_time_sec); | ||
datestr = asctime(gmtime(&tsec)); | ||
} | ||
|
||
/* Fill the pipe with data from initial time */ | ||
int err = write(writefd, datestr, strlen(datestr)); | ||
while (err != -1) { | ||
err = write(writefd, datestr, strlen(datestr)); | ||
} | ||
} | ||
|
||
int urandom_create_fd() | ||
{ | ||
debuglogstdio(LCF_FILEIO | LCF_RANDOM, "Open /dev/urandom"); | ||
|
||
if (readfd == -1) { | ||
std::pair<int, int> fds = FileHandleList::createPipe(O_NONBLOCK); | ||
readfd = fds.first; | ||
writefd = fds.second; | ||
|
||
/* Set the pipe size to some small value, because we won't need much. | ||
* It should be at least 2*page_size, because on Linux a pipe is | ||
* considered writeable if at least page_size can be written. | ||
*/ | ||
MYASSERT(fcntl(writefd, F_SETPIPE_SZ, 2*4096) != -1); | ||
|
||
/* Fill the pipe */ | ||
urandom_handler(0); | ||
|
||
GlobalNative gn; | ||
|
||
/* Add async signal for when the pipe is writeable */ | ||
MYASSERT(fcntl(writefd, F_SETOWN, getpid()) != -1); | ||
MYASSERT(fcntl(writefd, F_SETSIG, 0) != -1); | ||
MYASSERT(fcntl(writefd, F_SETFL, O_ASYNC | O_NONBLOCK) != -1); | ||
|
||
/* Unblock SIGIO signal */ | ||
sigset_t mask; | ||
sigemptyset(&mask); | ||
sigaddset(&mask, SIGIO); | ||
MYASSERT(pthread_sigmask(SIG_UNBLOCK, &mask, nullptr) == 0); | ||
|
||
/* Add signal handler for SIGIO signal */ | ||
struct sigaction sigio; | ||
sigfillset(&sigio.sa_mask); | ||
sigio.sa_handler = urandom_handler; | ||
MYASSERT(sigaction(SIGIO, &sigio, nullptr) == 0) | ||
} | ||
|
||
debuglog(LCF_FILEIO | LCF_RANDOM, "Return fd ", readfd); | ||
return readfd; | ||
} | ||
|
||
int urandom_get_fd() { | ||
return readfd; | ||
} | ||
|
||
FILE* urandom_create_file() { | ||
if (!stream) { | ||
int readfd = urandom_create_fd(); | ||
stream = fdopen(readfd, "r"); | ||
setvbuf(stream, nullptr, _IONBF, 0); | ||
} | ||
return stream; | ||
} | ||
|
||
FILE* urandom_get_file() { | ||
return stream; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright 2015-2020 Clément Gallet <clement.gallet@ens-lyon.org> | ||
This file is part of libTAS. | ||
libTAS is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
libTAS 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 General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with libTAS. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef LIBTAS_URANDOM_H_INCLUDED | ||
#define LIBTAS_URANDOM_H_INCLUDED | ||
|
||
#include <cstdio> | ||
|
||
namespace libtas { | ||
|
||
/* Creates a fd implementing our own deterministic /dev/urandom */ | ||
int urandom_create_fd(); | ||
|
||
/* Returns that fd, or -1 if not created */ | ||
int urandom_get_fd(); | ||
|
||
/* Creates a FILE* stream containing the above fd */ | ||
FILE* urandom_create_file(); | ||
|
||
/* Returns the /dev/urandom FILE* stream */ | ||
FILE* urandom_get_file(); | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters