From 48565214692a74792706e4f09508afe348eabfe3 Mon Sep 17 00:00:00 2001 From: millert Date: Mon, 5 Feb 2018 03:52:37 +0000 Subject: [PATCH] Pass a FILE * instead of a file descriptor into load_user() and perform the fclose() in process_crontab(). Previously we were closing the crontab fd twice--once in load_user() via fclose() and once in process_crontab(). OK tb@ --- usr.sbin/cron/database.c | 23 ++++++++++++++--------- usr.sbin/cron/funcs.h | 4 ++-- usr.sbin/cron/user.c | 10 ++-------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/usr.sbin/cron/database.c b/usr.sbin/cron/database.c index 7265dfb75bd..e399d74f4c4 100644 --- a/usr.sbin/cron/database.c +++ b/usr.sbin/cron/database.c @@ -1,4 +1,4 @@ -/* $OpenBSD: database.c,v 1.36 2017/10/25 17:08:58 jca Exp $ */ +/* $OpenBSD: database.c,v 1.37 2018/02/05 03:52:37 millert Exp $ */ /* Copyright 1988,1990,1993,1994 by Paul Vixie * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") @@ -170,9 +170,10 @@ process_crontab(int dfd, const char *uname, const char *fname, struct stat *statbuf, cron_db *new_db, cron_db *old_db) { struct passwd *pw = NULL; - int crontab_fd = -1; + FILE *crontab_fp = NULL; user *u, *new_u; mode_t tabmask, tabperm; + int fd; /* Note: pw must remain NULL for system crontab (see below). */ if (fname[0] != '/' && (pw = getpwnam(uname)) == NULL) { @@ -182,16 +183,20 @@ process_crontab(int dfd, const char *uname, const char *fname, goto next_crontab; } - crontab_fd = openat(dfd, fname, - O_RDONLY|O_NONBLOCK|O_NOFOLLOW|O_CLOEXEC); - if (crontab_fd < 0) { + fd = openat(dfd, fname, O_RDONLY|O_NONBLOCK|O_NOFOLLOW|O_CLOEXEC); + if (fd < 0) { /* crontab not accessible? */ syslog(LOG_ERR, "(%s) CAN'T OPEN (%s)", uname, fname); goto next_crontab; } + if (!(crontab_fp = fdopen(fd, "r"))) { + syslog(LOG_ERR, "(%s) FDOPEN (%m)", fname); + close(fd); + goto next_crontab; + } - if (fstat(crontab_fd, statbuf) < 0) { + if (fstat(fileno(crontab_fp), statbuf) < 0) { syslog(LOG_ERR, "(%s) FSTAT FAILED (%s)", uname, fname); goto next_crontab; } @@ -233,7 +238,7 @@ process_crontab(int dfd, const char *uname, const char *fname, syslog(LOG_INFO, "(%s) RELOAD (%s)", uname, fname); } - new_u = load_user(crontab_fd, pw, fname); + new_u = load_user(crontab_fp, pw, fname); if (new_u != NULL) { /* Insert user into the new database and remove from old. */ new_u->mtime = statbuf->st_mtim; @@ -249,7 +254,7 @@ process_crontab(int dfd, const char *uname, const char *fname, } next_crontab: - if (crontab_fd >= 0) { - close(crontab_fd); + if (crontab_fp != NULL) { + fclose(crontab_fp); } } diff --git a/usr.sbin/cron/funcs.h b/usr.sbin/cron/funcs.h index 29a721a2d09..ee42477007d 100644 --- a/usr.sbin/cron/funcs.h +++ b/usr.sbin/cron/funcs.h @@ -1,4 +1,4 @@ -/* $OpenBSD: funcs.h,v 1.28 2015/11/14 13:09:14 millert Exp $ */ +/* $OpenBSD: funcs.h,v 1.29 2018/02/05 03:52:37 millert Exp $ */ /* * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") @@ -48,7 +48,7 @@ char *env_get(char *, char **), **env_copy(char **), **env_set(char **, char *); -user *load_user(int, struct passwd *, const char *), +user *load_user(FILE *, struct passwd *, const char *), *find_user(cron_db *, const char *); entry *load_entry(FILE *, diff --git a/usr.sbin/cron/user.c b/usr.sbin/cron/user.c index 69a2640ed03..0c0d25642da 100644 --- a/usr.sbin/cron/user.c +++ b/usr.sbin/cron/user.c @@ -1,4 +1,4 @@ -/* $OpenBSD: user.c,v 1.20 2017/06/07 23:36:43 millert Exp $ */ +/* $OpenBSD: user.c,v 1.21 2018/02/05 03:52:37 millert Exp $ */ /* Copyright 1988,1990,1993,1994 by Paul Vixie * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") @@ -58,19 +58,14 @@ parse_error(const char *msg) } user * -load_user(int crontab_fd, struct passwd *pw, const char *name) +load_user(FILE *file, struct passwd *pw, const char *name) { char envstr[MAX_ENVSTR]; - FILE *file; user *u; entry *e; int status, save_errno; char **envp = NULL, **tenvp; - if (!(file = fdopen(crontab_fd, "r"))) { - syslog(LOG_ERR, "(%s) FDOPEN (%m)", name); - return (NULL); - } CrontabFilename = name; LineNumber = 0; @@ -134,6 +129,5 @@ load_user(int crontab_fd, struct passwd *pw, const char *name) done: if (envp != NULL) env_free(envp); - fclose(file); return (u); } -- 2.20.1