Skip to contentSkip to conversation

Opened file descriptors in script calls

Openvpn-devel Alberto Gonzalez Iniesta 6 participants 10 messages Feb 6, 2003 – Feb 12, 2003

lore
Share & tools
lore
#0 lore raw

Hi,

I got another bug report on Debian's openvpn package. It states that openvpn's file descriptors are kept open while scripts are called. It claims they should be closed. My C knowledge is far from make a patch for this one :) And maybe you don't agree with this (James?).

You can see the report here:

http://bugs.debian.org/179551

Thanks.

Signature · 5 lines
Alberto Gonzalez Iniesta       | They that give up essential liberty
agi@(agi.as|debian.org)        | to obtain a little temporary safety
Encrypted mail preferred       | deserve neither liberty nor safety.

Key fingerprint = 9782 04E7 2B75 405C F5E9  0C81 C514 AF8E 4BA4 01C3
#1 lore raw

Alberto,

Yes, I agree. The child process that executes a script doesn't need those file descriptors, so they can be closed. Since openvpn uses the system() function to run scripts, and because the system() function doesn't close any file descriptors on its own, it would be necessary to write an alternate version of the function (unless it already exists somewhere) which closes the file descriptors between the fork and the execve calls.

James

Quoted 30 lines · Alberto Gonzalez Iniesta

Hi,

I got another bug report on Debian's openvpn package. It states that openvpn's file descriptors are kept open while scripts are called. It claims they should be closed. My C knowledge is far from make a patch for this one :) And maybe you don't agree with this (James?).

You can see the report here:

http://bugs.debian.org/179551

Thanks.

Signature · 15 lines
Alberto Gonzalez Iniesta       | They that give up essential liberty
agi@(agi.as|debian.org)        | to obtain a little temporary safety
Encrypted mail preferred       | deserve neither liberty nor safety.

Key fingerprint = 9782 04E7 2B75 405C F5E9  0C81 C514 AF8E 4BA4 01C3


-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
#2 lore raw

Here is a simple little replacement for system() that does close file descriptors. The main issue with it is though, it ends up picking an arbitrary number of fds to close. I picked closing 0 to 99.

Aaron

int s_system(const char *string)

{
	pid_t pid;
	int x;

pid = fork()

if(pid == -1)

 	{
         	return -1;

}

if(pid > 0)
{

 	 	int status;
         	wait(&status);
         	return(status);
        }

        for(x = 0; x < 100; x++)
 	{
 		close(x);
 	}
        execlp("/bin/sh", "-c", string);

}

#3 lore raw
Quoted 32 lines · Aaron Sethman

Here is a simple little replacement for system() that does close file descriptors. The main issue with it is though, it ends up picking an arbitrary number of fds to close. I picked closing 0 to 99.

Aaron

int s_system(const char *string)

{
	pid_t pid;
	int x;
(quoted diff) +0 -0
 	pid = fork()
 
 	if(pid == -1)
 	{
         	return -1;
 	}
 
 	if(pid > 0)
 	{
 	 	int status;
         	wait(&status);
         	return(status);
        }
 
        for(x = 0; x < 100; x++)
 	{
 		close(x);
 	}
        execlp("/bin/sh", "-c", string);
Quoted 2 lines

}

Hi,

Again, I'm no C hacker, but I think this should be better:

for(x = 3; x < 100; x++)

Since the first 3 fds (stdin, stdout and stderr) should be kept open.

Regards,

Alberto

Signature · 5 lines
Alberto Gonzalez Iniesta       | They that give up essential liberty
agi@(agi.as|debian.org)        | to obtain a little temporary safety
Encrypted mail preferred       | deserve neither liberty nor safety.

Key fingerprint = 9782 04E7 2B75 405C F5E9  0C81 C514 AF8E 4BA4 01C3
#4 lore raw
Quoted 9 lines · Alberto Gonzalez Iniesta

Hi,

Again, I'm no C hacker, but I think this should be better:

for(x = 3; x < 100; x++)

Since the first 3 fds (stdin, stdout and stderr) should be kept open.

Wasn't sure if stdin, stdout and stderr needed to be left open or not in this case. Obviously this is an easy fix :)

Regards,

Aaron

#5 lore raw
Quoted 5 lines · Aaron Sethman
Quoted 3 lines · Alberto Gonzalez Iniesta

Hi,

Quoted 8 lines
Quoted 6 lines

Again, I'm no C hacker, but I think this should be better:

for(x = 3; x < 100; x++)

Since the first 3 fds (stdin, stdout and stderr) should be kept open.

Wasn't sure if stdin, stdout and stderr needed to be left open or not in this case. Obviously this is an easy fix :)

Actually, I was just thinking what a pain to have to reimplement system() just to close a few fds. There must be a better way, and as it turns out, there is:

/* Set a file descriptor to not be passed across execs */ void
set_cloexec (int fd)
{
if (fcntl (fd, F_SETFD, FD_CLOEXEC) < 0)

    msg (M_ERR, "Set file descriptor to FD_CLOEXEC failed");
}

Just set the FD_CLOEXEC flag on the fd and it won't be passed across the exec that runs the shell.

I already wrote the patch and it's in the CVS.

James

#6 lore raw
Quoted 10 lines · James Yonan

/* Set a file descriptor to not be passed across execs */ void
set_cloexec (int fd)
{
if (fcntl (fd, F_SETFD, FD_CLOEXEC) < 0)

    msg (M_ERR, "Set file descriptor to FD_CLOEXEC failed");
}

Just set the FD_CLOEXEC flag on the fd and it won't be passed across the exec that runs the shell.

That works even better then :)

Regards,

Aaron

#7 lore raw
In reply to Aaron Sethman
Quoted 3 lines · Aaron Sethman

Here is a simple little replacement for system() that does close file descriptors. The main issue with it is though, it ends up picking an arbitrary number of fds to close. I picked closing 0 to 99.

You can use getdtablesize() to determine the maximum number of open files available to the process.

Wolfgang

#8 lore raw
In reply to Aaron Sethman
Quoted 3 lines · Aaron Sethman

Here is a simple little replacement for system() that does close file descriptors. The main issue with it is though, it ends up picking an arbitrary number of fds to close. I picked closing 0 to 99.

I don't think this is necessary. Every file descriptor has a "close on exec" flag that you can set with fcntl(). Then, you can use plain system(). It should work like this:

  fcntl(fd, F_SETFD, FD_CLOEXEC);

Ideally, one would do this for each file descriptor as it is opened.

Hope this helps,
chrisp

Signature · 3 lines
chrisp a.k.a. Christoph Pfisterer   "A computer scientist is
[email protected]... - http://chrisp.de      someone who fixes things
PGP key & geek code available        that aren't broken."
#9 lore raw
Quoted 5 lines · Christoph Pfisterer
   fcntl(fd, F_SETFD, FD_CLOEXEC);

Ideally, one would do this for each file descriptor as it is opened.

Yeah I hadn't thought about that, James already added this to the CVS tree.

Aaron