Do not inherit open file descriptors on fork() in MacEthernetTapAgent

This commit is contained in:
Adam Ierymenko 2021-04-28 16:16:02 -04:00
parent ed8fe8990b
commit d2974f2e60
No known key found for this signature in database
GPG Key ID: C8877CF2D7A5D7F3

View File

@ -64,6 +64,7 @@
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/resource.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/bpf.h>
@ -181,6 +182,14 @@ static void die()
run("/sbin/ifconfig",s_peerDeviceName,"destroy",(char *)0);
}
static inline void close_inherited_fds()
{
struct rlimit lim;
getrlimit(RLIMIT_NOFILE, &lim);
for (int i=3,j=(int)lim.rlim_cur;i<j;++i)
close(i);
}
int main(int argc,char **argv)
{
char buf[128];
@ -206,6 +215,8 @@ int main(int argc,char **argv)
signal(SIGINT,&exit);
signal(SIGPIPE,&exit);
close_inherited_fds();
if (getuid() != 0) {
if (setuid(0) != 0) {
fprintf(stderr,"E must be run as root or with root setuid bit on executable\n");