From thomas@cuivre.fr.eu.org  Wed Sep  4 10:21:33 2002
Return-Path: <thomas@cuivre.fr.eu.org>
Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id A9FAE37B400
	for <FreeBSD-gnats-submit@freebsd.org>; Wed,  4 Sep 2002 10:21:33 -0700 (PDT)
Received: from melusine.cuivre.fr.eu.org (melusine.cuivre.fr.eu.org [62.212.105.185])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 1AA2843E6A
	for <FreeBSD-gnats-submit@freebsd.org>; Wed,  4 Sep 2002 10:21:33 -0700 (PDT)
	(envelope-from thomas@cuivre.fr.eu.org)
Received: by melusine.cuivre.fr.eu.org (Postfix, from userid 1000)
	id 077722C3D1; Wed,  4 Sep 2002 19:21:32 +0200 (CEST)
Message-Id: <20020904172132.077722C3D1@melusine.cuivre.fr.eu.org>
Date: Wed,  4 Sep 2002 19:21:32 +0200 (CEST)
From: Thomas Quinot <thomas@freebsd.org>
Sender: thomas@cuivre.fr.eu.org
Reply-To: Thomas Quinot <thomas@freebsd.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [patch] perl wrapper loops on itself
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         42418
>Category:       bin
>Synopsis:       [patch] perl wrapper loops on itself
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    thomas
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Sep 04 10:30:02 PDT 2002
>Closed-Date:    Tue Sep 10 07:04:46 PDT 2002
>Last-Modified:  Tue Sep 10 07:04:46 PDT 2002
>Originator:     Thomas Quinot
>Release:        FreeBSD 5.0-CURRENT i386
>Organization:
>Environment:
System: FreeBSD shalmaneser.enst.fr 5.0-CURRENT FreeBSD 5.0-CURRENT #1: Tue Sep  3 14:05:44 CEST 2002     root@shalmaneser.enst.fr:/usr/obj/usr/src/sys/SHALMANESER  i386


	
>Description:
	When PATH contains a symbolic link to /usr/bin/perl (typically,
	a /usr/local/bin/perl link that used to point the the base
	system perl), the wrapper loops on itself.

>How-To-Repeat:
	mkdir /tmp/foo
	PATH=/tmp/foo:$PATH
	ln -s /usr/bin/perl /tmp/foo
	/usr/bin/perl
	# will loop forever
>Fix:
	The proposed solution is to make the wrapper stat the files
	it is considering as candidate real interpreter, and skip
	itself. Patch included.

Index: perl.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/perl/perl.c,v
retrieving revision 1.4
diff -u -r1.4 perl.c
--- perl.c	21 Aug 2002 20:54:45 -0000	1.4
+++ perl.c	4 Sep 2002 17:21:11 -0000
@@ -30,6 +30,7 @@
 __FBSDID("$FreeBSD: src/usr.bin/perl/perl.c,v 1.4 2002/08/21 20:54:45 joerg Exp $");
 
 #include <sys/param.h>
+#include <sys/stat.h>
 #include <sys/sysctl.h>
 
 #include <err.h>
@@ -49,8 +50,13 @@
 	char path[PATH_MAX], *cp;
 	const char *cmd, *p, *q, *self;
 	size_t len;
+	struct stat self_stat, perl_stat;
 
 	self = argv[0];
+	if (stat (self, &self_stat) != 0) {
+		self_stat.st_dev = makedev (0, 0);
+		self_stat.st_ino = 0;
+	}
 	if ((cmd = strrchr(self, '/')) == NULL)
 		cmd = self;
 	else
@@ -78,6 +84,10 @@
 			/* nothing */ ;
 		len = snprintf(path, sizeof path, "%.*s/%s", (int)(q - p), p, cmd);
 		if (len >= PATH_MAX || strcmp(path, self) == 0)
+			continue;
+		if (stat (path, &perl_stat) == 0
+		    && self_stat.st_dev == perl_stat.st_dev
+		    && self_stat.st_ino == perl_stat.st_ino)
 			continue;
 		execve(path, argv, environ);
 		if (errno != ENOENT)
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: thomas 
State-Changed-When: Tue Sep 10 07:01:42 PDT 2002 
State-Changed-Why:  
Patch applied after review by mentor. 


Responsible-Changed-From-To: freebsd-bugs->thomas 
Responsible-Changed-By: thomas 
Responsible-Changed-When: Tue Sep 10 07:01:42 PDT 2002 
Responsible-Changed-Why:  
I'll take it. 

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