Unbreak adventure(6)

2014-12-31 Thread Theo Buehler
The adventure game is currently broken.  When it's started without
any arguments, it spits a pile of garbage to stdout before eventually
dumping its core.

The game data of adventure(6) is obfuscated at compile time with a
scheme relying on deterministic random() and deobfuscated at runtime.
This is done ``to prevent casual snooping of the executable'' (cf.
status.c).  Thus the program must use the deterministic random generator
for that elaborate scheme.

Randomness during game play comes exclusively from the ran() function in
wizard.c -- which currently suffers from modulo bias -- better use
arc4random_uniform() there.

Index: init.c
===
RCS file: /cvs/src/games/adventure/init.c,v
retrieving revision 1.12
diff -u -p -r1.12 init.c
--- init.c  8 Dec 2014 21:56:27 -   1.12
+++ init.c  31 Dec 2014 15:11:34 -
@@ -56,6 +56,11 @@ int setbit[16] = {1, 2, 4, 010, 020,
 void
 init(void) /* everything for 1st time run */
 {
+   /*
+* We need deterministic randomness for the obfuscation schemes
+* in io.c and setup.c.
+*/
+   srandom_deterministic(1);
rdata();/* read data from orig. file */
linkdata();
poof();
Index: setup.c
===
RCS file: /cvs/src/games/adventure/setup.c,v
retrieving revision 1.11
diff -u -p -r1.11 setup.c
--- setup.c 8 Dec 2014 21:56:27 -   1.11
+++ setup.c 31 Dec 2014 15:11:34 -
@@ -78,6 +78,8 @@ main(int argc, char *argv[])
count = 0;
linestart = YES;
 
+   srandom_deterministic(1);
+
while ((c = getc(infile)) != EOF) {
if (count++ % LINE == 0)
printf(\n\t);
Index: wizard.c
===
RCS file: /cvs/src/games/adventure/wizard.c,v
retrieving revision 1.16
diff -u -p -r1.16 wizard.c
--- wizard.c16 Nov 2014 04:49:48 -  1.16
+++ wizard.c31 Dec 2014 15:11:34 -
@@ -141,8 +141,5 @@ ciao(void)
 int
 ran(int range)
 {
-   longi;
-
-   i = random() % range;
-   return (i);
+   return (arc4random_uniform(range));
 }



Re: Unbreak adventure(6)

2014-12-31 Thread Adam Wolk
On Wed, Dec 31, 2014, at 04:16 PM, Theo Buehler wrote:
 The adventure game is currently broken.  When it's started without
 any arguments, it spits a pile of garbage to stdout before eventually
 dumping its core.
 
Confirmed true for i386 running a snapshot from 27-Dec-2014.
With your patch (obtained from CVS) the game starts up properly
and I'm able to quit without breaking the terminal.

 The game data of adventure(6) is obfuscated at compile time with a
 scheme relying on deterministic random() and deobfuscated at runtime.
 This is done ``to prevent casual snooping of the executable'' (cf.
 status.c).  Thus the program must use the deterministic random generator
 for that elaborate scheme.
 
 Randomness during game play comes exclusively from the ran() function in
 wizard.c -- which currently suffers from modulo bias -- better use
 arc4random_uniform() there.
 
 Index: init.c
 ===
 RCS file: /cvs/src/games/adventure/init.c,v
 retrieving revision 1.12
 diff -u -p -r1.12 init.c
 --- init.c  8 Dec 2014 21:56:27 -   1.12
 +++ init.c  31 Dec 2014 15:11:34 -
 @@ -56,6 +56,11 @@ int setbit[16] = {1, 2, 4, 010, 020,
  void
  init(void)  /* everything for 1st time run */
  {
 +   /*
 +* We need deterministic randomness for the obfuscation schemes
 +* in io.c and setup.c.
 +*/
 +   srandom_deterministic(1);
   rdata();/* read data from orig. file */
   linkdata();
   poof();
 Index: setup.c
 ===
 RCS file: /cvs/src/games/adventure/setup.c,v
 retrieving revision 1.11
 diff -u -p -r1.11 setup.c
 --- setup.c 8 Dec 2014 21:56:27 -   1.11
 +++ setup.c 31 Dec 2014 15:11:34 -
 @@ -78,6 +78,8 @@ main(int argc, char *argv[])
   count = 0;
   linestart = YES;
  
 +   srandom_deterministic(1);
 +
   while ((c = getc(infile)) != EOF) {
   if (count++ % LINE == 0)
   printf(\n\t);
 Index: wizard.c
 ===
 RCS file: /cvs/src/games/adventure/wizard.c,v
 retrieving revision 1.16
 diff -u -p -r1.16 wizard.c
 --- wizard.c16 Nov 2014 04:49:48 -  1.16
 +++ wizard.c31 Dec 2014 15:11:34 -
 @@ -141,8 +141,5 @@ ciao(void)
  int
  ran(int range)
  {
 -   longi;
 -
 -   i = random() % range;
 -   return (i);
 +   return (arc4random_uniform(range));
  }