From b34242015e67f6ebb3d5481de266d70475a2c2e2 Mon Sep 17 00:00:00 2001 From: guenther <> Date: Mon, 28 Dec 2015 20:11:36 +0000 Subject: [PATCH] Switch login(3) from lseek+read/write to pread/pwrite and only do the pread() if the data is needed. Use O_CLOEXEC on the internal fd as MT paranoia. Fix cast in offset calculation; delete register keyword; prefer memset() over bzero() ok millert@ --- src/lib/libutil/login.c | 21 +++++++++++---------- src/lib/libutil/logout.c | 8 ++++---- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/lib/libutil/login.c b/src/lib/libutil/login.c index 7d3eec4a..95210578 100644 --- a/src/lib/libutil/login.c +++ b/src/lib/libutil/login.c @@ -1,4 +1,4 @@ -/* $OpenBSD: login.c,v 1.10 2005/08/02 21:46:23 espie Exp $ */ +/* $OpenBSD: login.c,v 1.11 2015/12/28 20:11:36 guenther Exp $ */ /* * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. @@ -43,28 +43,29 @@ void login(struct utmp *utp) { struct utmp old_ut; - register int fd; - int tty; + int fd, tty; + off_t pos; tty = ttyslot(); - if (tty > 0 && (fd = open(_PATH_UTMP, O_RDWR|O_CREAT, 0644)) >= 0) { - (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET); + if (tty > 0 && (fd = open(_PATH_UTMP, O_RDWR|O_CREAT|O_CLOEXEC, 0644)) + >= 0) { /* * Prevent luser from zero'ing out ut_host. * If the new ut_line is empty but the old one is not * and ut_line and ut_name match, preserve the old ut_line. */ - if (read(fd, &old_ut, sizeof(struct utmp)) == - sizeof(struct utmp) && utp->ut_host[0] == '\0' && + pos = (off_t)tty * sizeof(struct utmp); + if (utp->ut_host[0] == '\0' && + pread(fd, &old_ut, sizeof(struct utmp), pos) == + sizeof(struct utmp) && old_ut.ut_host[0] != '\0' && strncmp(old_ut.ut_line, utp->ut_line, UT_LINESIZE) == 0 && strncmp(old_ut.ut_name, utp->ut_name, UT_NAMESIZE) == 0) (void)memcpy(utp->ut_host, old_ut.ut_host, UT_HOSTSIZE); - (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET); - (void)write(fd, utp, sizeof(struct utmp)); + (void)pwrite(fd, utp, sizeof(struct utmp), pos); (void)close(fd); } - if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) >= 0) { + if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND|O_CLOEXEC)) >= 0) { (void)write(fd, utp, sizeof(struct utmp)); (void)close(fd); } diff --git a/src/lib/libutil/logout.c b/src/lib/libutil/logout.c index 51116f1f..13c11fa8 100644 --- a/src/lib/libutil/logout.c +++ b/src/lib/libutil/logout.c @@ -1,4 +1,4 @@ -/* $OpenBSD: logout.c,v 1.8 2005/08/02 21:46:23 espie Exp $ */ +/* $OpenBSD: logout.c,v 1.9 2015/12/28 20:11:36 guenther Exp $ */ /* * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. @@ -47,14 +47,14 @@ logout(const char *line) int fd, rval; UTMP ut; - if ((fd = open(_PATH_UTMP, O_RDWR, 0)) < 0) + if ((fd = open(_PATH_UTMP, O_RDWR|O_CLOEXEC)) < 0) return(0); rval = 0; while (read(fd, &ut, sizeof(UTMP)) == sizeof(UTMP)) { if (!ut.ut_name[0] || strncmp(ut.ut_line, line, UT_LINESIZE)) continue; - bzero(ut.ut_name, UT_NAMESIZE); - bzero(ut.ut_host, UT_HOSTSIZE); + memset(ut.ut_name, 0, UT_NAMESIZE); + memset(ut.ut_host, 0, UT_HOSTSIZE); (void)time(&ut.ut_time); (void)lseek(fd, -(off_t)sizeof(UTMP), SEEK_CUR); (void)write(fd, &ut, sizeof(UTMP));