From nobody@FreeBSD.org  Wed Nov 11 22:05:49 2009
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 31EBA106568B
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 11 Nov 2009 22:05:49 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 210B68FC21
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 11 Nov 2009 22:05:49 +0000 (UTC)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.3/8.14.3) with ESMTP id nABM5mkx045180
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 11 Nov 2009 22:05:48 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id nABM5mqn045179;
	Wed, 11 Nov 2009 22:05:48 GMT
	(envelope-from nobody)
Message-Id: <200911112205.nABM5mqn045179@www.freebsd.org>
Date: Wed, 11 Nov 2009 22:05:48 GMT
From: Erik Lax <erik@datahack.se>
To: freebsd-gnats-submit@FreeBSD.org
Subject: truss log file descriptor shared with traced program
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         140493
>Category:       bin
>Synopsis:       [patch] truss(1) log file descriptor shared with traced program
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    jh
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Nov 11 22:10:03 UTC 2009
>Closed-Date:    Mon Oct 11 13:48:28 UTC 2010
>Last-Modified:  Mon Oct 11 13:48:28 UTC 2010
>Originator:     Erik Lax
>Release:        FreeBSD 7.2-RELEASE
>Organization:
>Environment:
FreeBSD freebsd.datahack.se 7.2-RELEASE FreeBSD 7.2-RELEASE #0: Fri May  1 08:49:13 UTC 2009     root@walker.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  i386
>Description:
The truss -o log file descriptor is shared with the traced program when truss starts the program with vfork/execvp, for no obvious reason(?).

So it may play with your head when you are trying to debug a program if you are not aware of this!

As a security/technical problem this may affects badly written programs that  expects a certain file descriptor to be opened at some target, causing the log file to be modified (by accident or evil...ftruncate).
>How-To-Repeat:
In one terminal

# truss -o /tmp/truss.log sleep 60

followed by .. in another terminal

# fstat | grep sleep
root     sleep      37193 root /             2 drwxr-xr-x     512  r
root     sleep      37193   wd /         45516 drwxr-xr-x     512  r
root     sleep      37193 text /         46256 -r-xr-xr-x    5964  r
root     sleep      37193    0 /dev        100 crw--w----   ttyp2 rw
root     sleep      37193    1 /dev        100 crw--w----   ttyp2 rw
root     sleep      37193    2 /dev        100 crw--w----   ttyp2 rw
root     sleep      37193    3 /tmp          4 -rw-r--r--    2278  w

File descriptor 3 is pointing at the log file provided by -o
>Fix:
Close the file descriptor trussinfo->outfile after the vfork().

Suggested changes would be to either make setup_and_wait(char *command[]) (setup.c) also take the file descriptor that should be closed as an argument or close all file descriptors from fd#3 and above after the vfork().

>Release-Note:
>Audit-Trail:

From: Erik <erik@datahack.se>
To: bug-followup@FreeBSD.org, erik@datahack.se
Cc:  
Subject: Re: bin/140493: truss(1) log file descriptor shared with traced program
Date: Thu, 12 Nov 2009 18:06:41 +0100

 This is a multi-part message in MIME format.
 --------------020501070508070709040003
 Content-Type: text/plain; charset=ISO-8859-1; format=flowed
 Content-Transfer-Encoding: 7bit
 
 Not reporting issues 10pm...we could of course set FD_CLOEXEC on the fd. 
 See proposed patched.
 
 --------------020501070508070709040003
 Content-Type: text/x-patch;
  name="truss-cloexec.patch"
 Content-Transfer-Encoding: 7bit
 Content-Disposition: inline;
  filename="truss-cloexec.patch"
 
 --- main.c-original	2009-11-12 18:52:25.000000000 +0000
 +++ main.c	2009-11-12 18:59:42.000000000 +0000
 @@ -227,6 +227,17 @@
  	if (fname != NULL) { /* Use output file */
  		if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
  			errx(1, "cannot open %s", fname);
 +
 +		/*
 +		 * Do not share the file descriptor with the traced
 +		 * process executed with execvp().
 +		 */
 +		int fd = fileno(trussinfo->outfile);
 +		int flags = fcntl(fd, F_GETFD, 0);
 +		if (flags != -1)
 +		{
 +			fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
 +		}
  	}
  
  	/*
 
 --------------020501070508070709040003--
Responsible-Changed-From-To: freebsd-bugs->jh 
Responsible-Changed-By: jh 
Responsible-Changed-When: Sat Dec 19 09:32:52 UTC 2009 
Responsible-Changed-Why:  
Take. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=140493 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/140493: commit references a PR
Date: Sun, 20 Dec 2009 11:01:07 +0000 (UTC)

 Author: jh
 Date: Sun Dec 20 11:00:53 2009
 New Revision: 200752
 URL: http://svn.freebsd.org/changeset/base/200752
 
 Log:
   Avoid sharing the file descriptor of the output file with traced
   processes by setting the FD_CLOEXEC flag for the output file.
   
   PR:		bin/140493
   Submitted by:	Erik Lax
   OK'ed by:	delphij
   Approved by:	trasz (mentor)
 
 Modified:
   head/usr.bin/truss/main.c
 
 Modified: head/usr.bin/truss/main.c
 ==============================================================================
 --- head/usr.bin/truss/main.c	Sun Dec 20 10:58:34 2009	(r200751)
 +++ head/usr.bin/truss/main.c	Sun Dec 20 11:00:53 2009	(r200752)
 @@ -239,6 +239,12 @@ main(int ac, char **av)
  		if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
  			errx(1, "cannot open %s", fname);
  	}
 +	/*
 +	 * Set FD_CLOEXEC, so that the output file is not shared with
 +	 * the traced process.
 +	 */
 +	if (fcntl(fileno(trussinfo->outfile), F_SETFD, FD_CLOEXEC) == -1)
 +		warn("fcntl()");
  
  	/*
  	 * If truss starts the process itself, it will ignore some signals --
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->patched 
State-Changed-By: jh 
State-Changed-When: Sun Dec 20 11:13:17 UTC 2009 
State-Changed-Why:  
Patched in head (r200752). 

http://www.freebsd.org/cgi/query-pr.cgi?pr=140493 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/140493: commit references a PR
Date: Mon,  8 Feb 2010 15:51:03 +0000 (UTC)

 Author: jh
 Date: Mon Feb  8 15:50:51 2010
 New Revision: 203667
 URL: http://svn.freebsd.org/changeset/base/203667
 
 Log:
   MFC r200752:
   
   Avoid sharing the file descriptor of the output file with traced
   processes by setting the FD_CLOEXEC flag for the output file.
   
   PR:		bin/140493
 
 Modified:
   stable/8/usr.bin/truss/main.c
 Directory Properties:
   stable/8/usr.bin/truss/   (props changed)
 
 Modified: stable/8/usr.bin/truss/main.c
 ==============================================================================
 --- stable/8/usr.bin/truss/main.c	Mon Feb  8 15:48:18 2010	(r203666)
 +++ stable/8/usr.bin/truss/main.c	Mon Feb  8 15:50:51 2010	(r203667)
 @@ -239,6 +239,12 @@ main(int ac, char **av)
  		if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
  			errx(1, "cannot open %s", fname);
  	}
 +	/*
 +	 * Set FD_CLOEXEC, so that the output file is not shared with
 +	 * the traced process.
 +	 */
 +	if (fcntl(fileno(trussinfo->outfile), F_SETFD, FD_CLOEXEC) == -1)
 +		warn("fcntl()");
  
  	/*
  	 * If truss starts the process itself, it will ignore some signals --
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/140493: commit references a PR
Date: Mon, 11 Oct 2010 13:31:15 +0000 (UTC)

 Author: jh
 Date: Mon Oct 11 13:31:09 2010
 New Revision: 213690
 URL: http://svn.freebsd.org/changeset/base/213690
 
 Log:
   MFC r200752:
   
   Avoid sharing the file descriptor of the output file with traced
   processes by setting the FD_CLOEXEC flag for the output file.
   
   PR:		bin/140493
 
 Modified:
   stable/7/usr.bin/truss/main.c
 Directory Properties:
   stable/7/usr.bin/truss/   (props changed)
 
 Modified: stable/7/usr.bin/truss/main.c
 ==============================================================================
 --- stable/7/usr.bin/truss/main.c	Mon Oct 11 12:57:41 2010	(r213689)
 +++ stable/7/usr.bin/truss/main.c	Mon Oct 11 13:31:09 2010	(r213690)
 @@ -231,6 +231,12 @@ main(int ac, char **av)
  		if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
  			errx(1, "cannot open %s", fname);
  	}
 +	/*
 +	 * Set FD_CLOEXEC, so that the output file is not shared with
 +	 * the traced process.
 +	 */
 +	if (fcntl(fileno(trussinfo->outfile), F_SETFD, FD_CLOEXEC) == -1)
 +		warn("fcntl()");
  
  	/*
  	 * If truss starts the process itself, it will ignore some signals --
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: patched->closed 
State-Changed-By: jh 
State-Changed-When: Mon Oct 11 13:48:27 UTC 2010 
State-Changed-Why:  
Fixed in head, stable/8 and stable/7. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=140493 
>Unformatted:
