From 7d03e0811239d6f413d313214dab463af86d2a82 Mon Sep 17 00:00:00 2001 From: Kyle Evans Date: Tue, 14 Apr 2020 18:07:42 +0000 Subject: [PATCH] Mark closefrom(2) COMPAT12, reimplement in libc to wrap close_range Include a temporarily compatibility shim as well for kernels predating close_range, since closefrom is used in some critical areas. Reviewed by: markj (previous version), kib Differential Revision: https://reviews.freebsd.org/D24399 --- lib/libc/include/libc_private.h | 1 + lib/libc/sys/Makefile.inc | 1 + lib/libc/sys/Symbol.map | 2 -- lib/libc/sys/closefrom.c | 47 ++++++++++++++++++++++++++++ sys/compat/freebsd32/syscalls.master | 2 +- sys/kern/kern_descrip.c | 6 ++-- sys/kern/syscalls.master | 2 +- tools/build/depend-cleanup.sh | 2 ++ 8 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 lib/libc/sys/closefrom.c diff --git a/lib/libc/include/libc_private.h b/lib/libc/include/libc_private.h index 654baeaaae55..2585855c5191 100644 --- a/lib/libc/include/libc_private.h +++ b/lib/libc/include/libc_private.h @@ -331,6 +331,7 @@ int __sys_clock_gettime(__clockid_t, struct timespec *ts); int __sys_clock_nanosleep(__clockid_t, int, const struct timespec *, struct timespec *); int __sys_close(int); +int __sys_close_range(unsigned, unsigned, int); int __sys_connect(int, const struct sockaddr *, __socklen_t); int __sys_fcntl(int, int, ...); int __sys_fdatasync(int); diff --git a/lib/libc/sys/Makefile.inc b/lib/libc/sys/Makefile.inc index 913fe56eefbe..38156ccf507d 100644 --- a/lib/libc/sys/Makefile.inc +++ b/lib/libc/sys/Makefile.inc @@ -45,6 +45,7 @@ NOASM+= getdirentries.o PSEUDO+= _getdirentries.o SRCS+= brk.c +SRCS+= closefrom.c SRCS+= pipe.c SRCS+= shm_open.c SRCS+= vadvise.c diff --git a/lib/libc/sys/Symbol.map b/lib/libc/sys/Symbol.map index 4d3bd5115af3..98c9af3fad03 100644 --- a/lib/libc/sys/Symbol.map +++ b/lib/libc/sys/Symbol.map @@ -520,8 +520,6 @@ FBSDprivate_1.0 { __sys_clock_settime; _close; __sys_close; - _closefrom; - __sys_closefrom; _connect; __sys_connect; _cpuset; diff --git a/lib/libc/sys/closefrom.c b/lib/libc/sys/closefrom.c new file mode 100644 index 000000000000..ceab46872d07 --- /dev/null +++ b/lib/libc/sys/closefrom.c @@ -0,0 +1,47 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2020 Kyle Evans + * + * 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 COPYRIGHT HOLDER(S) ``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 COPYRIGHT HOLDER(S) 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. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include + +#include "libc_private.h" + +#define CLOSE_RANGE_OSREL 1300091 + +void +closefrom(int lowfd) +{ + + if (__getosreldate() >= CLOSE_RANGE_OSREL) + __sys_close_range(lowfd, ~0U, 0); + else + /* Fallback to closefrom(2) on older kernels. */ + syscall(SYS_freebsd12_closefrom, lowfd); +} diff --git a/sys/compat/freebsd32/syscalls.master b/sys/compat/freebsd32/syscalls.master index 0f735faca8e1..65d9b37a862a 100644 --- a/sys/compat/freebsd32/syscalls.master +++ b/sys/compat/freebsd32/syscalls.master @@ -980,7 +980,7 @@ 507 AUE_JAIL_SET STD { int freebsd32_jail_set(struct iovec32 *iovp, \ unsigned int iovcnt, int flags); } 508 AUE_JAIL_REMOVE NOPROTO { int jail_remove(int jid); } -509 AUE_CLOSEFROM NOPROTO { int closefrom(int lowfd); } +509 AUE_CLOSEFROM COMPAT12|NOPROTO { int closefrom(int lowfd); } 510 AUE_SEMCTL NOSTD { int freebsd32_semctl(int semid, int semnum, \ int cmd, union semun32 *arg); } 511 AUE_MSGCTL NOSTD { int freebsd32_msgctl(int msqid, int cmd, \ diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index dce930b09fe6..d11618a3a6a2 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -1372,17 +1372,18 @@ sys_close_range(struct thread *td, struct close_range_args *uap) return (kern_close_range(td, uap->lowfd, uap->highfd)); } +#ifdef COMPAT_FREEBSD12 /* * Close open file descriptors. */ #ifndef _SYS_SYSPROTO_H_ -struct closefrom_args { +struct freebsd12_closefrom_args { int lowfd; }; #endif /* ARGSUSED */ int -sys_closefrom(struct thread *td, struct closefrom_args *uap) +freebsd12_closefrom(struct thread *td, struct freebsd12_closefrom_args *uap) { u_int lowfd; @@ -1395,6 +1396,7 @@ sys_closefrom(struct thread *td, struct closefrom_args *uap) lowfd = MAX(0, uap->lowfd); return (kern_close_range(td, lowfd, ~0U)); } +#endif /* COMPAT_FREEBSD12 */ #if defined(COMPAT_43) /* diff --git a/sys/kern/syscalls.master b/sys/kern/syscalls.master index ec8e03b65ee9..6fb90faeb58e 100644 --- a/sys/kern/syscalls.master +++ b/sys/kern/syscalls.master @@ -2776,7 +2776,7 @@ int jid ); } -509 AUE_CLOSEFROM STD { +509 AUE_CLOSEFROM COMPAT12 { int closefrom( int lowfd ); diff --git a/tools/build/depend-cleanup.sh b/tools/build/depend-cleanup.sh index e0cfdcc03b32..d25d3399865b 100755 --- a/tools/build/depend-cleanup.sh +++ b/tools/build/depend-cleanup.sh @@ -36,3 +36,5 @@ clean_dep() clean_dep lib/libc shm_open S # 20200310 r358851 rename of openmp's ittnotify_static.c to .cpp clean_dep lib/libomp ittnotify_static c +# 20200414 r359930 closefrom +clean_dep lib/libc closefrom S