Remove buffer overflow and tempfile race, remove <sys/time.h> hack (and

need for it), change definition of setbit() macro and friends to be
compatible with <sys/param.h>.
The bugs were discovered and fixed as a result of the FreeBSD code audit.

Submitted by:	Aaron Bornstein <aaronb@j51.com>, Mark Huizer <xaa@stack.nl>
This commit is contained in:
Eivind Eklund 1997-02-20 03:04:54 +00:00
parent 2745e1ab7e
commit c52e467e03
14 changed files with 122 additions and 87 deletions

View File

@ -8,5 +8,6 @@ MAN6= battlestar.6
DPADD= ${LIBCURSES} ${LIBTERMCAP} ${LIBCOMPAT}
LDADD= -lcurses -ltermcap -lcompat
HIDEGAME=hidegame
#CFLAGS+= -Wall -W
.include <bsd.prog.mk>

View File

@ -45,7 +45,7 @@ int thataway, token;
if (thataway) {
position = thataway;
newway(token);
time++;
gtime++;
}
else {
puts("You can't go this way.");
@ -88,24 +88,24 @@ news()
register int n;
int hurt;
if (time > 30 && position < 32){
if (gtime > 30 && position < 32){
puts("An explosion of shuddering magnitude splinters bulkheads and");
puts("ruptures the battlestar's hull. You are sucked out into the");
puts("frozen void of space and killed.");
die();
}
if (time > 20 && position < 32)
if (gtime > 20 && position < 32)
puts("Explosions rock the battlestar.");
if (time > snooze){
if (gtime > snooze){
puts("You drop from exhaustion...");
zzz();
}
if (time > snooze - 5)
if (gtime > snooze - 5)
puts("You're getting tired.");
if (time > (rythmn + CYCLE)) {
if (gtime > (rythmn + CYCLE)) {
if (location == nightfile) {
convert(TODAY);
if (OUTSIDE && time - rythmn - CYCLE < 10) {
if (OUTSIDE && gtime - rythmn - CYCLE < 10) {
puts("Dew lit sunbeams stretch out from a watery sunrise and herald the dawn.");
puts("You awake from a misty dream-world into stark reality.");
puts("It is day.");
@ -113,14 +113,14 @@ news()
} else {
convert(TONIGHT);
clearbit(location[POOLS].objects, BATHGOD);
if (OUTSIDE && time - rythmn - CYCLE < 10) {
if (OUTSIDE && gtime - rythmn - CYCLE < 10) {
puts("The dying sun sinks into the ocean, leaving a blood stained sunset.");
puts("The sky slowly fades from orange to violet to black. A few stars");
puts("flicker on, and it is night.");
puts("The world seems completly different at night.");
}
}
rythmn = time - time % CYCLE;
rythmn = gtime - gtime % CYCLE;
}
if (!wiz && !tempwiz)
if ((testbit(inven,TALISMAN) || testbit(wear,TALISMAN)) && (testbit(inven,MEDALION) || testbit(wear,MEDALION)) && (testbit(inven,AMULET) || testbit(wear,AMULET))){
@ -157,16 +157,16 @@ news()
}
if (testbit(location[position].objects, GIRL))
meetgirl = 1;
if (meetgirl && CYCLE * 1.5 - time < 10){
if (meetgirl && CYCLE * 1.5 - gtime < 10){
setbit(location[GARDEN].objects,GIRLTALK);
setbit(location[GARDEN].objects,LAMPON);
setbit(location[GARDEN].objects,ROPE);
}
if (position == DOCK && (beenthere[position] || time > CYCLE)){
if (position == DOCK && (beenthere[position] || gtime > CYCLE)){
clearbit(location[DOCK].objects, GIRL);
clearbit(location[DOCK].objects,MAN);
}
if (meetgirl && time - CYCLE * 1.5 > 10){
if (meetgirl && gtime - CYCLE * 1.5 > 10){
clearbit(location[GARDEN].objects,GIRLTALK);
clearbit(location[GARDEN].objects,LAMPON);
clearbit(location[GARDEN].objects,ROPE);
@ -174,7 +174,7 @@ news()
}
if (testbit(location[position].objects,CYLON)){
puts("Oh my God, you're being shot at by an alien spacecraft!");
printf("The targeting computer says we have %d seconds to attack!\n",clock);
printf("The targeting computer says we have %d seconds to attack!\n",gclock);
fflush(stdout);
sleep(1);
if (!visual()){
@ -236,7 +236,7 @@ crash()
}
notes[LAUNCHED] = 0;
setbit(location[position].objects,CRASH);
time += rnd(CYCLE/4);
gtime += rnd(CYCLE/4);
puts("The viper explodes into the ground and you lose consciousness...");
zzz();
hurt1 = rnd(NUMOFINJURIES - 2) + 2;

View File

@ -81,7 +81,7 @@ wearit() /* synonyms = {sheathe, sheath} */
setbit(wear,value);
carrying -= objwt[value];
encumber -= objcumber[value];
time++;
gtime++;
printf("You are now wearing %s %s.\n",(objsht[value][n-1] == 's' ? "the" : "a"), objsht[value]);
}
else if (testbit(wear,value))
@ -129,7 +129,7 @@ use()
location[position].down = 160;
whichway(location[position]);
puts("The waves subside and it is possible to descend to the sea cave now.");
time++;
gtime++;
return(-1);
}
}
@ -139,7 +139,7 @@ use()
position = 224;
else
position = 229;
time++;
gtime++;
return(0);
}
else if (position == FINAL)
@ -225,7 +225,7 @@ ravage()
{
while (wordtype[++wordnumber] != NOUNS && wordnumber <= wordcount);
if (wordtype[wordnumber] == NOUNS && testbit(location[position].objects,wordvalue[wordnumber])){
time++;
gtime++;
switch(wordvalue[wordnumber]){
case NORMGOD:
puts("You attack the goddess, and she screams as you beat her. She falls down");
@ -272,7 +272,7 @@ ravage()
follow()
{
if (followfight == time){
if (followfight == gtime){
puts("The Dark Lord leaps away and runs down secret tunnels and corridoors.");
puts("You chase him through the darkness and splash in pools of water.");
puts("You have cornered him. His laser sword extends as he steps forward.");
@ -282,7 +282,7 @@ follow()
setbit(location[position].objects,AMULET);
return(0);
}
else if (followgod == time){
else if (followgod == gtime){
puts("The goddess leads you down a steamy tunnel and into a high, wide chamber.");
puts("She sits down on a throne.");
position = 268;

View File

@ -41,7 +41,7 @@ dig()
{
if (testbit(inven,SHOVEL)){
puts("OK");
time++;
gtime++;
switch(position){
case 144: /* copse near beach */
if (!notes[DUG]){
@ -176,7 +176,7 @@ drink()
CUMBER = MAXCUMBER;
for (n=0; n < NUMOFINJURIES; n++)
injuries[n] = 0;
time++;
gtime++;
zzz();
}
else
@ -199,7 +199,7 @@ shoot()
for (n=0; objsht[value][n]; n++);
if (testbit(location[position].objects,value)){
clearbit(location[position].objects,value);
time++;
gtime++;
printf("The %s explode%s\n",objsht[value],(objsht[value][n-1]=='s' ? (objsht[value][n-2]=='s' ? "s." : ".") : "s."));
if (value == BOMB)
die();
@ -214,7 +214,7 @@ shoot()
/* special cases with their own return()'s */
if (wordnumber <= wordcount && wordtype[wordnumber] == NOUNS){
time++;
gtime++;
switch(wordvalue[wordnumber]){
case DOOR:
@ -278,7 +278,7 @@ shoot()
clearbit(location[position].objects,TIMER);
setbit(location[position].objects,DEADTIME);
}
else puts("What old timer?");
else puts("What old gtimer?");
break;
case MAN:
if (testbit(location[position].objects,MAN)){

View File

@ -61,7 +61,7 @@ unsigned int from[];
setbit(inven,value);
carrying += objwt[value];
encumber += objcumber[value];
time++;
gtime++;
if (testbit(from,value))
printf("Taken.\n");
else
@ -165,7 +165,7 @@ unsigned int from[];
puts("ties it at the waist. Around her neck hangs a golden amulet.");
puts("She bids you to follow her.");
pleasure++;
followgod = time;
followgod = gtime;
clearbit(location[position].objects,BATHGOD);
} else if (!testbit(location[position].objects,BATHGOD))
puts("You're in no position to take her.");
@ -285,7 +285,7 @@ char *name;
setbit(location[position].objects,value);
else
tempwiz = 0;
time++;
gtime++;
if (*name == 'K')
puts("Drop kicked.");
else
@ -354,16 +354,16 @@ eat()
case MANGO:
printf("%s:\n",objsht[value]);
if (testbit(inven,value) && time > ate - CYCLE && testbit(inven,KNIFE)){
if (testbit(inven,value) && gtime > ate - CYCLE && testbit(inven,KNIFE)){
clearbit(inven,value);
carrying -= objwt[value];
encumber -= objcumber[value];
ate = max(time,ate) + CYCLE/3;
ate = max(gtime,ate) + CYCLE/3;
snooze += CYCLE/10;
time++;
gtime++;
puts("Eaten. You can explore a little longer now.");
}
else if (time < ate - CYCLE)
else if (gtime < ate - CYCLE)
puts("You're stuffed.");
else if (!testbit(inven,KNIFE))
puts("You need a knife.");

View File

@ -100,7 +100,7 @@ love()
if (!loved)
setbit(location[position].objects,MEDALION);
loved = 1;
time += 10;
gtime += 10;
zzz();
}
else {
@ -112,7 +112,7 @@ love()
power++;
pleasure += 5;
printf("Girl:\n");
time += 10;
gtime += 10;
zzz();
}
printf("Loved.\n");
@ -125,16 +125,16 @@ zzz()
int oldtime;
register int n;
oldtime = time;
if ((snooze - time) < (0.75 * CYCLE)){
time += 0.75 * CYCLE - (snooze - time);
oldtime = gtime;
if ((snooze - gtime) < (0.75 * CYCLE)){
gtime += 0.75 * CYCLE - (snooze - gtime);
printf("<zzz>");
for (n = 0; n < time - oldtime; n++)
for (n = 0; n < gtime - oldtime; n++)
printf(".");
printf("\n");
snooze += 3 * (time - oldtime);
snooze += 3 * (gtime - oldtime);
if (notes[LAUNCHED]){
fuel -= (time - oldtime);
fuel -= (gtime - oldtime);
if (location[position].down){
position = location[position].down;
crash();
@ -177,8 +177,8 @@ zzz()
chime()
{
if ((time / CYCLE + 1) % 2 && OUTSIDE)
switch((time % CYCLE)/(CYCLE / 7)){
if ((gtime / CYCLE + 1) % 2 && OUTSIDE)
switch((gtime % CYCLE)/(CYCLE / 7)){
case 0:
puts("It is just after sunrise.");
break;
@ -202,7 +202,7 @@ chime()
break;
}
else if (OUTSIDE)
switch((time % CYCLE)/(CYCLE / 7)){
switch((gtime % CYCLE)/(CYCLE / 7)){
case 0:
puts("It is just after sunset.");
break;
@ -259,7 +259,7 @@ give()
}
if (result != -1 && (testbit(location[position].objects,obj) || obj == AMULET || obj == MEDALION || obj == TALISMAN)){
clearbit(location[position].objects,obj);
time++;
gtime++;
ego++;
switch(person){
case NATIVE:
@ -288,7 +288,7 @@ give()
puts("after having been out drinking with the girls, she kicks the throne particulary");
puts("hard and wakes you up. (If you want to win this game, you're going to have to\nshoot her!)");
clearbit(location[position].objects,MEDALION);
wintime = time;
wintime = gtime;
}
}
break;

View File

@ -45,7 +45,7 @@ launch()
clearbit(location[position].objects,VIPER);
position = location[position].up;
notes[LAUNCHED] = 1;
time++;
gtime++;
fuel -= 4;
puts("You climb into the viper and prepare for launch.");
puts("With a touch of your thumb the turbo engines ignite, thrusting you back into\nyour seat.");
@ -66,7 +66,7 @@ land()
position = location[position].down;
setbit(location[position].objects,VIPER);
fuel -= 2;
time++;
gtime++;
puts("You are down.");
return(1);
}
@ -168,7 +168,7 @@ drive()
clearbit(location[position].objects,CAR);
setbit(location[position].objects,CRASH);
injuries[5] = injuries[6] = injuries[7] = injuries[8] = 1;
time += 15;
gtime += 15;
zzz();
return(0);
}
@ -203,7 +203,7 @@ light() /* synonyms = {strike, smoke} */
{ /* for matches, cigars */
if (testbit(inven,MATCHES) && matchcount){
puts("Your match splutters to life.");
time++;
gtime++;
matchlight = 1;
matchcount--;
if (position == 217){

View File

@ -48,15 +48,15 @@ int enemy,strength;
int exhaustion;
fighton:
time++;
gtime++;
snooze -= 5;
if (snooze > time)
exhaustion = CYCLE/(snooze - time);
if (snooze > gtime)
exhaustion = CYCLE/(snooze - gtime);
else {
puts("You collapse exhausted, and he pulverizes your skull.");
die();
}
if (snooze - time < 20)
if (snooze - gtime < 20)
puts("You look tired! I hope you're able to fight.");
next = getcom(auxbuf, LINELENGTH, "<fight!>-: ", 0);
for (i=0; next && i < 10; i++)
@ -186,7 +186,7 @@ int enemy,strength;
puts("he flees down the dark caverns.");
clearbit(location[position].objects,DARK);
injuries[SKULL] = 1;
followfight = time;
followfight = gtime;
return (0);
}
else{
@ -231,7 +231,7 @@ int enemy,strength;
case DROP:
case DRAW:
cypher();
time--;
gtime--;
break;
default:

View File

@ -283,10 +283,10 @@ cypher()
fgets(buffer,10,stdin);
if (*buffer != '\n')
sscanf(buffer,"%d", &position);
printf("Time (was %d) = ",time);
printf("Time (was %d) = ",gtime);
fgets(buffer,10,stdin);
if (*buffer != '\n')
sscanf(buffer,"%d", &time);
sscanf(buffer,"%d", &gtime);
printf("Fuel (was %d) = ",fuel);
fgets(buffer,10,stdin);
if (*buffer != '\n')
@ -303,10 +303,10 @@ cypher()
fgets(buffer,10,stdin);
if (*buffer != '\n')
sscanf(buffer,"%d",&WEIGHT);
printf("Clock (was %d) = ",clock);
printf("Clock (was %d) = ",gclock);
fgets(buffer,10,stdin);
if (*buffer != '\n')
sscanf(buffer,"%d",&clock);
sscanf(buffer,"%d",&gclock);
printf("Wizard (was %d, %d) = ",wiz, tempwiz);
fgets(buffer,10,stdin);
if (*buffer != '\n'){
@ -324,7 +324,7 @@ cypher()
case SCORE:
printf("\tPLEASURE\tPOWER\t\tEGO\n");
printf("\t%3d\t\t%3d\t\t%3d\n\n",pleasure,power,ego);
printf("This gives you the rating of %s in %d turns.\n",rate(),time);
printf("This gives you the rating of %s in %d turns.\n",rate(),gtime);
printf("You have visited %d out of %d rooms this run (%d%%).\n",card(beenthere,NUMOFROOMS),NUMOFROOMS,card(beenthere,NUMOFROOMS)*100/NUMOFROOMS);
break;

View File

@ -33,16 +33,19 @@
* @(#)externs.h 8.1 (Berkeley) 5/31/93
*/
#include <sys/param.h>
#include <sys/signal.h>
#include <stdio.h>
#define BITS (8 * sizeof (int))
#define BITS (8)
#define OUTSIDE (position > 68 && position < 246 && position != 218)
#define rnd(x) (rand() % (x))
#define max(a,b) ((a) < (b) ? (b) : (a))
#define testbit(array, index) (array[index/BITS] & (1 << (index % BITS)))
#ifndef setbit
#define setbit(array, index) (array[index/BITS] |= (1 << (index % BITS)))
#endif
#define clearbit(array, index) (array[index/BITS] &= ~(1 << (index % BITS)))
/* well known rooms */
@ -224,7 +227,7 @@ struct room {
#define down link[6]
#define flyhere link[7]
char *desc;
unsigned int objects[NUMOFWORDS];
int objects[NUMOFWORDS];
};
struct room dayfile[];
struct room nightfile[];
@ -248,11 +251,11 @@ char *truedirec(), *rate();
char *getcom(), *getword();
/* state of the game */
int time;
int gtime;
int position;
int direction;
int left, right, ahead, back;
int clock, fuel, torps;
int gclock, fuel, torps;
int carrying, encumber;
int rythmn;
int followfight;
@ -271,12 +274,12 @@ int pleasure, power, ego;
int WEIGHT;
int CUMBER;
int notes[NUMOFNOTES];
unsigned int inven[NUMOFWORDS];
unsigned int wear[NUMOFWORDS];
int inven[NUMOFWORDS];
int wear[NUMOFWORDS];
char beenthere[NUMOFROOMS+1];
char injuries[NUMOFINJURIES];
char uname[9];
char uname[MAXLOGNAME];
struct wlist {
char *string;

View File

@ -46,7 +46,7 @@ static char sccsid[] = "@(#)fly.c 8.1 (Berkeley) 5/31/93";
int row, column;
int dr = 0, dc = 0;
char destroyed;
int clock = 120; /* time for all the flights in the game */
int gclock = 120; /* gtime for all the flights in the game */
char cross = 0;
sig_t oldsig;
@ -166,7 +166,7 @@ visual()
endfly();
return(1);
}
if (clock <= 0){
if (gclock <= 0){
endfly();
die();
}
@ -256,7 +256,7 @@ moveenemy()
row += (rnd(9) - 4) % (4 - abs(row - MIDR));
column += (rnd(9) - 4) % (4 - abs(column - MIDC));
}
clock--;
gclock--;
mvaddstr(oldr, oldc - 1, " ");
if (cross)
target();
@ -266,7 +266,7 @@ moveenemy()
move(LINES-1, 42);
printw("%3d", fuel);
move(LINES-1, 57);
printw("%3d", clock);
printw("%3d", gclock);
refresh();
signal(SIGALRM, moveenemy);
alarm(1);

View File

@ -56,7 +56,7 @@ initialize(startup)
wordinit();
if (startup) {
direction = NORTH;
time = 0;
gtime = 0;
snooze = CYCLE * 1.5;
position = 22;
setbit(wear, PAJAMAS);
@ -121,7 +121,7 @@ checkout(uname)
if (location != NULL) {
CUMBER = 3;
WEIGHT = 9; /* that'll get him! */
clock = 10;
gclock = 10;
setbit(location[7].objects, WOODSMAN); /* viper room */
setbit(location[20].objects, WOODSMAN); /* laser " */
setbit(location[13].objects, DARK); /* amulet " */

View File

@ -59,7 +59,7 @@ writedes()
printobjs()
{
register unsigned int *p = location[position].objects;
register int *p = location[position].objects;
register n;
printf("\n");

View File

@ -35,27 +35,33 @@
static char sccsid[] = "@(#)save.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h> /* MAXPATHLEN */
#include <fcntl.h>
#include "externs.h"
void
restore()
{
char *getenv();
char *home;
char home1[100];
char home1[MAXPATHLEN];
register int n;
int tmp;
register FILE *fp;
home = getenv("HOME");
strcpy(home1, home);
strcat(home1, "/Bstar");
if ( (home = getenv("HOME")) != NULL)
sprintf(home1, "%.*s/Bstar", MAXPATHLEN - 7, home);
else return;
if ((fp = fopen(home1, "r")) == 0) {
perror(home1);
return;
}
fread(&WEIGHT, sizeof WEIGHT, 1, fp);
fread(&CUMBER, sizeof CUMBER, 1, fp);
fread(&clock, sizeof clock, 1, fp);
fread(&gclock, sizeof gclock, 1, fp);
fread(&tmp, sizeof tmp, 1, fp);
location = tmp ? dayfile : nightfile;
for (n = 1; n <= NUMOFROOMS; n++) {
@ -68,7 +74,7 @@ restore()
fread(notes, sizeof notes, 1, fp);
fread(&direction, sizeof direction, 1, fp);
fread(&position, sizeof position, 1, fp);
fread(&time, sizeof time, 1, fp);
fread(&gtime, sizeof gtime, 1, fp);
fread(&fuel, sizeof fuel, 1, fp);
fread(&torps, sizeof torps, 1, fp);
fread(&carrying, sizeof carrying, 1, fp);
@ -90,26 +96,51 @@ restore()
fread(&ego, sizeof ego, 1, fp);
}
void
save()
{
struct stat sbuf;
char *getenv();
char *home;
char home1[100];
char home1[MAXPATHLEN];
register int n;
int tmp;
int tmp, fd;
FILE *fp;
home = getenv("HOME");
strcpy(home1, home);
strcat(home1, "/Bstar");
if ((fp = fopen(home1, "w")) == 0) {
if (home == 0)
return;
sprintf(home1, "%.*s/Bstar", MAXPATHLEN - 7, home);
/* Try to open the file safely. */
if (stat(home1, &sbuf) < 0) {
fd = open(home1, O_WRONLY|O_CREAT|O_EXCL);
if (fd < 0) {
fprintf(stderr, "Can't create %s\n", home1);
return;
}
} else {
if (sbuf.st_nlink > 1) {
fprintf(stderr, "No symlinks!\n");
return;
}
fd = open(home1, O_WRONLY|O_EXCL);
if (fd < 0) {
fprintf(stderr, "Can't open %s for writing\n", home1);
return;
}
}
if ((fp = fdopen(fd, "w")) == 0) {
perror(home1);
return;
}
printf("Saved in %s.\n", home1);
fwrite(&WEIGHT, sizeof WEIGHT, 1, fp);
fwrite(&CUMBER, sizeof CUMBER, 1, fp);
fwrite(&clock, sizeof clock, 1, fp);
fwrite(&gclock, sizeof gclock, 1, fp);
tmp = location == dayfile;
fwrite(&tmp, sizeof tmp, 1, fp);
for (n = 1; n <= NUMOFROOMS; n++) {
@ -122,7 +153,7 @@ save()
fwrite(notes, sizeof notes, 1, fp);
fwrite(&direction, sizeof direction, 1, fp);
fwrite(&position, sizeof position, 1, fp);
fwrite(&time, sizeof time, 1, fp);
fwrite(&gtime, sizeof gtime, 1, fp);
fwrite(&fuel, sizeof fuel, 1, fp);
fwrite(&torps, sizeof torps, 1, fp);
fwrite(&carrying, sizeof carrying, 1, fp);