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
This commit is contained in:
Kyle Evans 2020-04-14 18:07:42 +00:00
parent a1af70e58b
commit 7d03e08112
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=359930
8 changed files with 57 additions and 6 deletions

View File

@ -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);

View File

@ -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

View File

@ -520,8 +520,6 @@ FBSDprivate_1.0 {
__sys_clock_settime;
_close;
__sys_close;
_closefrom;
__sys_closefrom;
_connect;
__sys_connect;
_cpuset;

47
lib/libc/sys/closefrom.c Normal file
View File

@ -0,0 +1,47 @@
/*
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2020 Kyle Evans <kevans@FreeBSD.org>
*
* 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 <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/syscall.h>
#include <unistd.h>
#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);
}

View File

@ -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, \

View File

@ -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)
/*

View File

@ -2776,7 +2776,7 @@
int jid
);
}
509 AUE_CLOSEFROM STD {
509 AUE_CLOSEFROM COMPAT12 {
int closefrom(
int lowfd
);

View File

@ -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