From 047a2ef6974e4c0ab7b02b6c346f7199983fb0bd Mon Sep 17 00:00:00 2001 From: Brooks Davis Date: Fri, 6 Apr 2018 17:17:34 +0000 Subject: [PATCH] Remove caching from getlogin(2). This caching has existed since the CSRG import, but serves no obvious purpose. Sure, setlogin() is called rarely, but calls to getlogin() should also be infrequent. The required invalidation was not implemented on aarch64, arm, mips, amd riscv so updates would never occur if getlogin() was called before setlogin(). Reported by: Ali Mashtizadeh Reviewed by: kib Sponsored by: DARPA, AFRL Differential Revision: https://reviews.freebsd.org/D14965 --- lib/libc/amd64/sys/Makefile.inc | 2 +- lib/libc/amd64/sys/setlogin.S | 53 --------------------------- lib/libc/gen/getlogin.c | 53 +++++++-------------------- lib/libc/i386/sys/Makefile.inc | 2 +- lib/libc/i386/sys/setlogin.S | 50 -------------------------- lib/libc/powerpc/sys/Makefile.inc | 2 +- lib/libc/powerpc/sys/setlogin.S | 51 -------------------------- lib/libc/powerpc64/sys/Makefile.inc | 2 +- lib/libc/powerpc64/sys/setlogin.S | 41 --------------------- lib/libc/sparc64/sys/Makefile.inc | 2 +- lib/libc/sparc64/sys/setlogin.S | 55 ----------------------------- 11 files changed, 17 insertions(+), 296 deletions(-) delete mode 100644 lib/libc/amd64/sys/setlogin.S delete mode 100644 lib/libc/i386/sys/setlogin.S delete mode 100644 lib/libc/powerpc/sys/setlogin.S delete mode 100644 lib/libc/powerpc64/sys/setlogin.S delete mode 100644 lib/libc/sparc64/sys/setlogin.S diff --git a/lib/libc/amd64/sys/Makefile.inc b/lib/libc/amd64/sys/Makefile.inc index 1f0391efd1a..2ca89eaf093 100644 --- a/lib/libc/amd64/sys/Makefile.inc +++ b/lib/libc/amd64/sys/Makefile.inc @@ -9,7 +9,7 @@ SRCS+= \ amd64_set_gsbase.c MDASM= vfork.S brk.S cerror.S exect.S getcontext.S \ - sbrk.S setlogin.S + sbrk.S # Don't generate default code for these syscalls: NOASM+= vfork.o diff --git a/lib/libc/amd64/sys/setlogin.S b/lib/libc/amd64/sys/setlogin.S deleted file mode 100644 index 0d38d3a3bc4..00000000000 --- a/lib/libc/amd64/sys/setlogin.S +++ /dev/null @@ -1,53 +0,0 @@ -/*- - * Copyright (c) 1991 The Regents of the University of California. - * All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * William Jolitz. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) - .asciz "@(#)setlogin.s 5.2 (Berkeley) 4/12/91" -#endif /* LIBC_SCCS and not lint */ -#include -__FBSDID("$FreeBSD$"); - -#include "SYS.h" - -.globl CNAME(_logname_valid) /* in _getlogin() */ - - WEAK_REFERENCE(__sys_setlogin, _setlogin) - WEAK_REFERENCE(__sys_setlogin, setlogin) -ENTRY(__sys_setlogin) - mov $SYS_setlogin,%rax - KERNCALL - jb HIDENAME(cerror) - movl $0,CNAME(_logname_valid)(%rip) - ret /* setlogin(name) */ -END(__sys_setlogin) - - .section .note.GNU-stack,"",%progbits diff --git a/lib/libc/gen/getlogin.c b/lib/libc/gen/getlogin.c index e52d6be724b..f2ac9c5b35d 100644 --- a/lib/libc/gen/getlogin.c +++ b/lib/libc/gen/getlogin.c @@ -47,62 +47,33 @@ __FBSDID("$FreeBSD$"); #include "libc_private.h" -#define THREAD_LOCK() if (__isthreaded) _pthread_mutex_lock(&logname_mutex) -#define THREAD_UNLOCK() if (__isthreaded) _pthread_mutex_unlock(&logname_mutex) - extern int _getlogin(char *, int); -int _logname_valid __hidden; /* known to setlogin() */ -static pthread_mutex_t logname_mutex = PTHREAD_MUTEX_INITIALIZER; - -static char * -getlogin_basic(int *status) -{ - static char logname[MAXLOGNAME]; - - if (_logname_valid == 0) { - if (_getlogin(logname, sizeof(logname)) < 0) { - *status = errno; - return (NULL); - } - _logname_valid = 1; - } - *status = 0; - return (*logname ? logname : NULL); -} - char * getlogin(void) { - char *result; - int status; + static char logname[MAXLOGNAME]; - THREAD_LOCK(); - result = getlogin_basic(&status); - THREAD_UNLOCK(); - return (result); + if (_getlogin(logname, sizeof(logname)) < 0) + return (NULL); + return (logname[0] != '\0' ? logname : NULL); } int getlogin_r(char *logname, int namelen) { - char *result; + char tmpname[MAXLOGNAME]; int len; - int status; if (namelen < 1) return (ERANGE); logname[0] = '\0'; - THREAD_LOCK(); - result = getlogin_basic(&status); - if (status == 0 && result != NULL) { - len = strlen(result) + 1; - if (len > namelen) - status = ERANGE; - else - strncpy(logname, result, len); - } - THREAD_UNLOCK(); - return (status); + if (_getlogin(tmpname, sizeof(tmpname)) < 0) + return (errno); + len = strlen(tmpname) + 1; + if (len > namelen) + return (ERANGE); + strlcpy(logname, tmpname, len); + return (0); } diff --git a/lib/libc/i386/sys/Makefile.inc b/lib/libc/i386/sys/Makefile.inc index d2627e75ea5..a16097005db 100644 --- a/lib/libc/i386/sys/Makefile.inc +++ b/lib/libc/i386/sys/Makefile.inc @@ -8,7 +8,7 @@ SRCS+= i386_get_fsbase.c i386_get_gsbase.c i386_get_ioperm.c i386_get_ldt.c \ i386_set_fsbase.c i386_set_gsbase.c i386_set_ioperm.c i386_set_ldt.c MDASM= Ovfork.S brk.S cerror.S exect.S getcontext.S \ - sbrk.S setlogin.S syscall.S + sbrk.S syscall.S NOASM+= vfork.o diff --git a/lib/libc/i386/sys/setlogin.S b/lib/libc/i386/sys/setlogin.S deleted file mode 100644 index ab62e124836..00000000000 --- a/lib/libc/i386/sys/setlogin.S +++ /dev/null @@ -1,50 +0,0 @@ -/*- - * Copyright (c) 1991 The Regents of the University of California. - * All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * William Jolitz. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) - .asciz "@(#)setlogin.s 5.2 (Berkeley) 4/12/91" -#endif /* LIBC_SCCS and not lint */ -#include -__FBSDID("$FreeBSD$"); - -#include "SYS.h" - -.globl CNAME(_logname_valid) /* in _getlogin() */ - -SYSCALL(setlogin) - PIC_PROLOGUE - movl $0,PIC_GOTOFF(CNAME(_logname_valid)) - PIC_EPILOGUE - ret /* setlogin(name) */ -END(__sys_setlogin) - - .section .note.GNU-stack,"",%progbits diff --git a/lib/libc/powerpc/sys/Makefile.inc b/lib/libc/powerpc/sys/Makefile.inc index edb469c6fbc..3436dccf8ef 100644 --- a/lib/libc/powerpc/sys/Makefile.inc +++ b/lib/libc/powerpc/sys/Makefile.inc @@ -1,3 +1,3 @@ # $FreeBSD$ -MDASM+= brk.S cerror.S exect.S sbrk.S setlogin.S +MDASM+= brk.S cerror.S exect.S sbrk.S diff --git a/lib/libc/powerpc/sys/setlogin.S b/lib/libc/powerpc/sys/setlogin.S deleted file mode 100644 index e0d6d3c012c..00000000000 --- a/lib/libc/powerpc/sys/setlogin.S +++ /dev/null @@ -1,51 +0,0 @@ -/*- - * Copyright (c) 2002 Peter Grehan. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ -/* $NetBSD: setlogin.S,v 1.3 1998/11/24 11:14:57 tsubai Exp $ */ - -#include -__FBSDID("$FreeBSD$"); - -#include "SYS.h" - - .globl CNAME(_logname_valid) /* in _getlogin() */ - -SYSCALL(setlogin) -#ifdef PIC - mflr %r10 - bl _GLOBAL_OFFSET_TABLE_@local-4 - mflr %r4 - lwz %r4,CNAME(_logname_valid)@got(%r4) - li %r5,%r0 - stw %r5,0(%r4) - mtlr %r10 -#else - lis %r4,CNAME(_logname_valid)@ha - li %r5,0 - stw %r5,CNAME(_logname_valid)@l(%r4) -#endif - blr - - .section .note.GNU-stack,"",%progbits diff --git a/lib/libc/powerpc64/sys/Makefile.inc b/lib/libc/powerpc64/sys/Makefile.inc index edb469c6fbc..3436dccf8ef 100644 --- a/lib/libc/powerpc64/sys/Makefile.inc +++ b/lib/libc/powerpc64/sys/Makefile.inc @@ -1,3 +1,3 @@ # $FreeBSD$ -MDASM+= brk.S cerror.S exect.S sbrk.S setlogin.S +MDASM+= brk.S cerror.S exect.S sbrk.S diff --git a/lib/libc/powerpc64/sys/setlogin.S b/lib/libc/powerpc64/sys/setlogin.S deleted file mode 100644 index 6183407b3fe..00000000000 --- a/lib/libc/powerpc64/sys/setlogin.S +++ /dev/null @@ -1,41 +0,0 @@ -/*- - * Copyright (c) 2002 Peter Grehan. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ -/* $NetBSD: setlogin.S,v 1.3 1998/11/24 11:14:57 tsubai Exp $ */ - -#include -__FBSDID("$FreeBSD$"); - -#include "SYS.h" - - .globl CNAME(_logname_valid) /* in _getlogin() */ - -SYSCALL(setlogin) - addis %r4,%r2,CNAME(_logname_valid)@toc@ha - li %r5,0 - stw %r5,CNAME(_logname_valid)@toc@l(%r4) - blr - - .section .note.GNU-stack,"",%progbits diff --git a/lib/libc/sparc64/sys/Makefile.inc b/lib/libc/sparc64/sys/Makefile.inc index 65e1353cb6a..adbbeaca46d 100644 --- a/lib/libc/sparc64/sys/Makefile.inc +++ b/lib/libc/sparc64/sys/Makefile.inc @@ -12,4 +12,4 @@ SRCS+= __sparc_sigtramp_setup.c \ CFLAGS+= -I${LIBC_SRCTOP}/sparc64/fpu -MDASM+= brk.S cerror.S exect.S sbrk.S setlogin.S sigaction1.S +MDASM+= brk.S cerror.S exect.S sbrk.S sigaction1.S diff --git a/lib/libc/sparc64/sys/setlogin.S b/lib/libc/sparc64/sys/setlogin.S deleted file mode 100644 index 4d81806f304..00000000000 --- a/lib/libc/sparc64/sys/setlogin.S +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. - * - * This software was developed by the Computer Systems Engineering group - * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and - * contributed to Berkeley. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * from: Header: setlogin.s,v 1.1 91/07/06 13:06:00 torek Exp - */ - -#if defined(SYSLIBC_SCCS) && !defined(lint) - .asciz "@(#)setlogin.s 8.1 (Berkeley) 6/4/93" -#if 0 - RCSID("$NetBSD: setlogin.S,v 1.3 2000/07/21 03:14:15 eeh Exp $") -#endif -#endif /* SYSLIBC_SCCS and not lint */ -#include -__FBSDID("$FreeBSD$"); - -#include "SYS.h" - - .globl CNAME(_logname_valid) /* in _getlogin() */ - -_SYSENTRY(setlogin) - _SYSCALL(setlogin) - PIC_PROLOGUE(%o3, %o2) - SET(CNAME(_logname_valid), %o2, %o3) - retl - stw %g0, [%o3] -_SYSEND(setlogin)