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: tetris(6): fix select() - poll() conversion

2014-12-31 Thread Theo Buehler
On Fri, Nov 07, 2014 at 11:57:50AM -0800, patrick keshishian wrote:

[...]

 
  I propose getting rid of the s pointer all together as such:
 
 After replacing select() with poll(), not removing
 `struct timeval *s' seems an oversight; Its use was
 solely for select()'s benefit.
 
 Once more, proposing removal of `struct timeval *s' and
 using an `int timo' for the time-out value, that gets fed
 into poll(). It also improves readability of the code;
 rumors floating around that that is a good thing.
 
 Index: input.c
 ===
 RCS file: /cvs/obsd/src/games/tetris/input.c,v
 retrieving revision 1.14
 diff -u -p -u -r1.14 input.c
 --- input.c 5 Nov 2014 20:23:38 -   1.14
 +++ input.c 7 Nov 2014 19:37:07 -
 @@ -76,7 +76,8 @@
  int
  rwait(struct timeval *tvp)
  {
 -   struct timeval starttv, endtv, *s;
 +   int timo = INFTIM;
 +   struct timeval starttv, endtv;
 struct pollfd pfd[1];
 
  #defineNILTZ ((struct timezone *)0)
 @@ -84,15 +85,12 @@ rwait(struct timeval *tvp)
 if (tvp) {
 (void) gettimeofday(starttv, NILTZ);
 endtv = *tvp;
 -   s = endtv;
 -   } else
 -   s = NULL;
 +   timo = endtv.tv_sec * 1000 + endtv.tv_usec / 1000;
 +   }
  again:
 pfd[0].fd = STDIN_FILENO;
 pfd[0].events = POLLIN;
 -   switch (poll(pfd, 1, s ? s-tv_sec * 1000 + s-tv_usec / 1000 :
 -   INFTIM)) {
 -
 +   switch (poll(pfd, 1, timo)) {
 case -1:
 if (tvp == 0)
 return (-1);

[...]

I believe this mail was overlooked since patrick's solution is surely
better than mine that went in.  If somebody finds the time to submit
this patch, that would be great.



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));
  }
 



simplify lm sensor alias removal

2014-12-31 Thread David Gwynne
i cant see why a task is necessary to defer the removal of iic
sensors that alias isa ones we find. we should be able to remove
the iic sensors directly.

i also dont have a box with this hardware in it, so i cant test.

anyone else willing to try?

Index: lm78_isa.c
===
RCS file: /cvs/src/sys/dev/isa/lm78_isa.c,v
retrieving revision 1.8
diff -u -p -r1.8 lm78_isa.c
--- lm78_isa.c  19 Dec 2014 13:53:08 -  1.8
+++ lm78_isa.c  1 Jan 2015 02:49:38 -
@@ -20,7 +20,6 @@
 #include sys/systm.h
 #include sys/device.h
 #include sys/sensors.h
-#include sys/task.h
 #include machine/bus.h
 
 #include dev/isa/isareg.h
@@ -42,7 +41,6 @@ extern struct cfdriver lm_cd;
 
 struct lm_isa_softc {
struct lm_softc sc_lmsc;
-   struct task sc_remove_alias_task;
 
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
@@ -53,7 +51,7 @@ int  lm_wbsio_match(struct device *, voi
 void lm_isa_attach(struct device *, struct device *, void *);
 u_int8_t lm_isa_readreg(struct lm_softc *, int);
 void lm_isa_writereg(struct lm_softc *, int, int);
-void lm_isa_remove_alias(void *, void *);
+void lm_isa_remove_alias(struct lm_softc *, const char *);
 
 struct cfattach lm_isa_ca = {
sizeof(struct lm_isa_softc),
@@ -233,10 +231,7 @@ lm_isa_attach(struct device *parent, str
continue;
if (lmsc  lmsc-sbusaddr == sbusaddr 
lmsc-chipid == sc-sc_lmsc.chipid) {
-   task_set(sc-sc_remove_alias_task,
-   lm_isa_remove_alias, lmsc,
-   sc-sc_lmsc.sc_dev.dv_xname);
-   task_add(systq, sc-sc_remove_alias_task);
+   lm_isa_remove_alias(lmsc, sc-sc_lmsc.sc_dev.dv_xname);
break;
}
}
@@ -244,14 +239,12 @@ lm_isa_attach(struct device *parent, str
 
 /* Remove sensors of the i2c alias, since we prefer to use the isa access */
 void
-lm_isa_remove_alias(void *v, void *arg)
+lm_isa_remove_alias(struct lm_softc *sc, const char *isa)
 {
-   struct lm_softc *sc = v;
-   char *iic = arg;
int i;
 
printf(%s: disabling sensors due to alias with %s\n,
-   sc-sc_dev.dv_xname, iic);
+   sc-sc_dev.dv_xname, isa);
sensordev_deinstall(sc-sensordev);
for (i = 0; i  sc-numsensors; i++)
sensor_detach(sc-sensordev, sc-sensors[i]);