diff options
author | dwmalone <dwmalone@FreeBSD.org> | 2000-11-06 11:17:37 +0000 |
---|---|---|
committer | dwmalone <dwmalone@FreeBSD.org> | 2000-11-06 11:17:37 +0000 |
commit | 10728d93bce493063d23ddc3ab30f8031d541d00 (patch) | |
tree | c85e3d52e38e2ace7052bdecfc8bfbe7d773b203 /usr.sbin/cron/crontab/crontab.c | |
parent | 208f492793555d0b9d29675131253d80e53c226b (diff) | |
download | FreeBSD-src-10728d93bce493063d23ddc3ab30f8031d541d00.zip FreeBSD-src-10728d93bce493063d23ddc3ab30f8031d541d00.tar.gz |
Attempt to fix problem with users being able to convince the crontab
program to read any file which is a valid crontab file.
The fix is based on that used in NetBSD and OpenBSD - we keep the
file open while the user is editing it. This means that files must
be edited in place. Cron attempts to warn you if your editor does
not do this. The fact that the file must be edited in place is also
noted in the man page.
This patch has been confirmed to work by atleast one person on
-security and has been tested locally.
Obtained from: OpenBSD
Diffstat (limited to 'usr.sbin/cron/crontab/crontab.c')
-rw-r--r-- | usr.sbin/cron/crontab/crontab.c | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/usr.sbin/cron/crontab/crontab.c b/usr.sbin/cron/crontab/crontab.c index 5490f4c..0071f6a 100644 --- a/usr.sbin/cron/crontab/crontab.c +++ b/usr.sbin/cron/crontab/crontab.c @@ -285,7 +285,7 @@ edit_cmd() { char n[MAX_FNAME], q[MAX_TEMPSTR], *editor; FILE *f; int ch, t, x; - struct stat statbuf; + struct stat statbuf, fsbuf; time_t mtime; WAIT_T waiter; PID_T pid, xpid; @@ -317,7 +317,7 @@ edit_cmd() { warn("fchown"); goto fatal; } - if (!(NewCrontab = fdopen(t, "w"))) { + if (!(NewCrontab = fdopen(t, "r+"))) { warn("fdopen"); goto fatal; } @@ -347,14 +347,20 @@ edit_cmd() { while (EOF != (ch = get_char(f))) putc(ch, NewCrontab); fclose(f); - if (fclose(NewCrontab)) + if (fflush(NewCrontab)) err(ERROR_EXIT, "%s", Filename); + if (fstat(t, &fsbuf) < 0) { + warn("unable to fstat temp file"); + goto fatal; + } again: if (stat(Filename, &statbuf) < 0) { warn("stat"); fatal: unlink(Filename); exit(ERROR_EXIT); } + if (statbuf.st_dev != fsbuf.st_dev || statbuf.st_ino != fsbuf.st_ino) + errx(ERROR_EXIT, "temp file must be edited in place"); mtime = statbuf.st_mtime; if ((!(editor = getenv("VISUAL"))) @@ -419,15 +425,13 @@ edit_cmd() { warn("stat"); goto fatal; } + if (statbuf.st_dev != fsbuf.st_dev || statbuf.st_ino != fsbuf.st_ino) + errx(ERROR_EXIT, "temp file must be edited in place"); if (mtime == statbuf.st_mtime) { warnx("no changes made to crontab"); goto remove; } warnx("installing new crontab"); - if (!(NewCrontab = fopen(Filename, "r"))) { - warn("%s", Filename); - goto fatal; - } switch (replace_cmd()) { case 0: break; @@ -497,10 +501,10 @@ replace_cmd() { /* copy the crontab to the tmp */ + rewind(NewCrontab); Set_LineNum(1) while (EOF != (ch = get_char(NewCrontab))) putc(ch, tmp); - fclose(NewCrontab); ftruncate(fileno(tmp), ftell(tmp)); fflush(tmp); rewind(tmp); |