Skip to content
Permalink
Browse files

introduce rand_i() (libc can't change rand's seed by calling it inter…

…nally)
  • Loading branch information...
leitner
leitner committed Jan 29, 2019
1 parent 89dfdcc commit 8c8df9579962dc7449fe1f3205fd19eec461aa23
Showing with 34 additions and 4 deletions.
  1. +1 −0 CHANGES
  2. +3 −1 Makefile
  3. +2 −1 lib/qsort.c
  4. +17 −0 lib/rand_i.c
  5. +2 −1 libcruft/res_mkquery.c
  6. +2 −1 libcruft/tmpnam.c
  7. +2 −0 rand_i.h
  8. +5 −0 t.c
@@ -4,6 +4,7 @@
SECURITY: fix regex stack corruption (Thorsten Glaser)
printf("%#.0g", 1.0) -> "1." (Christopher Wellons)
rename WANT_DYNAMIC to WANT_CTOR, add WANT_EXCEPTIONS to dietfeatures.h
libc can't call rand() and change the seed, so introduce rand_i (Christopher Wellons)

0.34:
move stpcpy from libcompat to lib as it is now part of POSIX (Enrico Scholz)
@@ -397,7 +397,7 @@ $(OBJDIR)/exports: $(OBJDIR)/dietlibc.a

.PHONY: t t1
t:
$(CCC) -g $(CFLAGS) -fno-builtin -nostdlib -isystem include -o t t.c $(OBJDIR)/start.o $(OBJDIR)/dyn_start.o $(OBJDIR)/dietlibc.a -lgcc $(OBJDIR)/dyn_stop.o -Wl,-Map,mapfile
$(CCC) -g $(CFLAGS) -fno-builtin -nostdlib -isystem include -o t t.c $(OBJDIR)/start.o $(OBJDIR)/dyn_start.o $(OBJDIR)/dietlibc.a -lgcc $(OBJDIR)/dyn_stop.o $(OBJDIR)/crtend.o -Wl,-Map,mapfile

t1:
$(CCC) -g -o t1 t.c
@@ -667,6 +667,8 @@ $(OBJDIR)/tmpnam.o $(OBJDIR)/utxent.o $(OBJDIR)/verr.o \
$(OBJDIR)/verrx.o $(OBJDIR)/vwarn.o $(OBJDIR)/warn.o \
$(OBJDIR)/wcsrtombs.o $(OBJDIR)/wcstombs.o $(OBJDIR)/eventfd.o: include/errno_definition.h

$(OBJDIR)/qsort.o $(OBJDIR)/tmpnam.o $(OBJDIR)/res_mkquery.o: rand_i.h

$(OBJDIR)/errno_location.o $(OBJDIR)/errno.o: dietfeatures.h

$(OBJDIR)/abort.o $(OBJDIR)/pselect.o $(OBJDIR)/__utmp_block_signals.o \
@@ -1,5 +1,6 @@
#include <sys/cdefs.h>
#include <stdlib.h>
#include "rand_i.h"

static void exch(char* base,size_t size,size_t a,size_t b) {
char* x=base+a*size;
@@ -30,7 +31,7 @@ static void quicksort(char* base,size_t size,ssize_t l,ssize_t r,
data is already sorted. Try to improve by exchanging it with a
random other pivot.
*/
exch(base,size,l+(rand()%(r-l)),r);
exch(base,size,l+(rand_i()%(r-l)),r);
#elif defined MID
/*
We chose the rightmost element in the array to be sorted as pivot,
@@ -0,0 +1,17 @@
#include <stdlib.h>
#include <sys/auxv.h>
#include "rand_i.h"

static unsigned int seed;

int rand_i(void) {
if (seed==0) {
const int* tmp=(const int*)getauxval(AT_RANDOM);
if (tmp)
seed=tmp[3];
else
seed=(uintptr_t)&seed; // really old crappy kernel
}
return rand_r(&seed);
}

@@ -10,6 +10,7 @@
#include <errno.h>
#include <arpa/nameser.h>
#include "dietfeatures.h"
#include "rand_i.h"

static char dnspacket[]="\xfe\xfe\001\000\000\001\000\000\000\000\000\000";

@@ -49,7 +50,7 @@ int res_mkquery(int op, const char *dname, int class, int type, char* data,
(void)datalen;

memcpy(packet,dnspacket,12);
len=rand();
len=rand_i();
packet[0]=len;
packet[1]=len>>8;
len=0;
@@ -7,6 +7,7 @@
#include <dietwarning.h>
#include <stdlib.h>
#include <stdio.h>
#include "rand_i.h"

link_warning("tmpnam","\e[1;33;41m>>> tmpnam stinks! NEVER ! NEVER USE IT ! <<<\e[0m");

@@ -18,7 +19,7 @@ char* tmpnam(char* s) {
for (;;) {
struct stat ss;
int i,j;
i=rand();
i=rand_i();
for (j=0; j<8; ++j) {
char c=i&0xf;
tmp[9+j]=c>9?c+'a'-10:c+'0';
@@ -0,0 +1,2 @@
#include <sys/cdefs.h>
extern int rand_i(void) __THROW;
5 t.c
@@ -108,12 +108,17 @@ extern char* strcpy2(char*a,char*b);

#define malloc(x) ({typeof(x) y=x; (y<0 || (size_t)(y)!=y ? 0 : malloc(y));})

#include "rand_i.h"

int main(int argc,char *argv[]) {
printf("%d\n",rand_i());
#if 0
int i;
for (i=0; i<1024; ++i) {
printf("%08x%c",arc4random(),(i&15)==15 ? '\n' : ' ');
}
perror("write");
#endif
#if 0
int n;
struct ucontext uc;

0 comments on commit 8c8df95

Please sign in to comment.
You can’t perform that action at this time.