diff options
author | eivind <eivind@FreeBSD.org> | 1997-02-20 03:04:54 +0000 |
---|---|---|
committer | eivind <eivind@FreeBSD.org> | 1997-02-20 03:04:54 +0000 |
commit | 27eb6d09a03b6d51d025fafa9e46e0f64544d722 (patch) | |
tree | c09c18b60a67afd2e34d5a6138c86fe56288b8e3 /games/battlestar/save.c | |
parent | 1a5d9e17d08eb1b79c5918e8596ba8f18ea36461 (diff) | |
download | FreeBSD-src-27eb6d09a03b6d51d025fafa9e46e0f64544d722.zip FreeBSD-src-27eb6d09a03b6d51d025fafa9e46e0f64544d722.tar.gz |
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>
Diffstat (limited to 'games/battlestar/save.c')
-rw-r--r-- | games/battlestar/save.c | 57 |
1 files changed, 44 insertions, 13 deletions
diff --git a/games/battlestar/save.c b/games/battlestar/save.c index 4a6ccba..ec4b4f5 100644 --- a/games/battlestar/save.c +++ b/games/battlestar/save.c @@ -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(>ime, 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(>ime, sizeof gtime, 1, fp); fwrite(&fuel, sizeof fuel, 1, fp); fwrite(&torps, sizeof torps, 1, fp); fwrite(&carrying, sizeof carrying, 1, fp); |