From c851fce6d779e8340609d7e2364829c61cbcb1ef Mon Sep 17 00:00:00 2001 From: Mariusz Zaborski Date: Sun, 9 Jun 2019 22:55:21 +0000 Subject: [PATCH] tail: fix the checks if the file was rotated The freopen(3) was replaced with fileargs_open(3) and fclose(3). In the following function, we skip if the stream is standard in, so it is safe to do so. This also requires us to change the logic first to open the file and then check its status. The stat(2) is disallowed in capability mode. This commit unbrakes the -F option. The bug was introduced in the r348708. Reported by: pho Tested by: pho --- usr.bin/tail/extern.h | 1 + usr.bin/tail/forward.c | 28 +++++++++++++++++++--------- usr.bin/tail/misc.c | 3 +++ usr.bin/tail/read.c | 3 +++ usr.bin/tail/reverse.c | 3 +++ usr.bin/tail/tail.c | 4 ++-- 6 files changed, 31 insertions(+), 11 deletions(-) diff --git a/usr.bin/tail/extern.h b/usr.bin/tail/extern.h index ef2c9ce0ee9a..65ddb519dc61 100644 --- a/usr.bin/tail/extern.h +++ b/usr.bin/tail/extern.h @@ -78,3 +78,4 @@ int maparound(struct mapinfo *, off_t); void printfn(const char *, int); extern int Fflag, fflag, qflag, rflag, rval, no_files; +extern fileargs_t *fa; diff --git a/usr.bin/tail/forward.c b/usr.bin/tail/forward.c index 3b697e0a5589..2888bd18816e 100644 --- a/usr.bin/tail/forward.c +++ b/usr.bin/tail/forward.c @@ -57,6 +57,9 @@ static const char sccsid[] = "@(#)forward.c 8.1 (Berkeley) 6/6/93"; #include #include +#include +#include + #include "extern.h" static void rlines(FILE *, const char *fn, off_t, struct stat *); @@ -310,6 +313,7 @@ follow(file_info_t *files, enum STYLE style, off_t off) int active, ev_change, i, n = -1; struct stat sb2; file_info_t *file; + FILE *ftmp; struct timespec ts; /* Position each of the files */ @@ -346,7 +350,9 @@ follow(file_info_t *files, enum STYLE style, off_t off) if (Fflag) { for (i = 0, file = files; i < no_files; i++, file++) { if (!file->fp) { - file->fp = fopen(file->file_name, "r"); + file->fp = + fileargs_fopen(fa, file->file_name, + "r"); if (file->fp != NULL && fstat(fileno(file->fp), &file->st) == -1) { @@ -359,7 +365,9 @@ follow(file_info_t *files, enum STYLE style, off_t off) } if (fileno(file->fp) == STDIN_FILENO) continue; - if (stat(file->file_name, &sb2) == -1) { + ftmp = fileargs_fopen(fa, file->file_name, "r"); + if (ftmp == NULL || + fstat(fileno(file->fp), &sb2) == -1) { if (errno != ENOENT) ierr(file->file_name); show(file); @@ -367,6 +375,9 @@ follow(file_info_t *files, enum STYLE style, off_t off) fclose(file->fp); file->fp = NULL; } + if (ftmp != NULL) { + fclose(ftmp); + } ev_change++; continue; } @@ -375,14 +386,13 @@ follow(file_info_t *files, enum STYLE style, off_t off) sb2.st_dev != file->st.st_dev || sb2.st_nlink == 0) { show(file); - file->fp = freopen(file->file_name, "r", - file->fp); - if (file->fp != NULL) - memcpy(&file->st, &sb2, - sizeof(struct stat)); - else if (errno != ENOENT) - ierr(file->file_name); + fclose(file->fp); + file->fp = ftmp; + memcpy(&file->st, &sb2, + sizeof(struct stat)); ev_change++; + } else { + fclose(ftmp); } } } diff --git a/usr.bin/tail/misc.c b/usr.bin/tail/misc.c index 137a38829b47..537cf00ccfe3 100644 --- a/usr.bin/tail/misc.c +++ b/usr.bin/tail/misc.c @@ -51,6 +51,9 @@ static const char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93"; #include #include +#include +#include + #include "extern.h" void diff --git a/usr.bin/tail/read.c b/usr.bin/tail/read.c index c5638d961399..1e757c86195c 100644 --- a/usr.bin/tail/read.c +++ b/usr.bin/tail/read.c @@ -51,6 +51,9 @@ static const char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/6/93"; #include #include +#include +#include + #include "extern.h" /* diff --git a/usr.bin/tail/reverse.c b/usr.bin/tail/reverse.c index 422724c0ebab..3373f072c228 100644 --- a/usr.bin/tail/reverse.c +++ b/usr.bin/tail/reverse.c @@ -55,6 +55,9 @@ __FBSDID("$FreeBSD$"); #include #include +#include +#include + #include "extern.h" static void r_buf(FILE *, const char *); diff --git a/usr.bin/tail/tail.c b/usr.bin/tail/tail.c index 5985fcf51e80..abbe715e0496 100644 --- a/usr.bin/tail/tail.c +++ b/usr.bin/tail/tail.c @@ -65,6 +65,7 @@ static const char sccsid[] = "@(#)tail.c 8.1 (Berkeley) 6/6/93"; #include "extern.h" int Fflag, fflag, qflag, rflag, rval, no_files; +fileargs_t *fa; static file_info_t *files; @@ -90,10 +91,9 @@ main(int argc, char *argv[]) int i, ch, first; file_info_t *file; char *p; - fileargs_t *fa; cap_rights_t rights; - cap_rights_init(&rights, CAP_FSTAT, CAP_FCNTL, CAP_MMAP_RW); + cap_rights_init(&rights, CAP_FSTAT, CAP_FSTATFS, CAP_FCNTL, CAP_MMAP_RW); if (caph_rights_limit(STDIN_FILENO, &rights) < 0 || caph_limit_stderr() < 0 || caph_limit_stdout() < 0) err(1, "can't limit stdio rights");