From kbyanc@teapot.egroups.com  Mon Jun 26 18:14:54 2000
Return-Path: <kbyanc@teapot.egroups.com>
Received: from teapot.egroups.net (teapot.egroups.net [63.204.207.250])
	by hub.freebsd.org (Postfix) with SMTP id 6A19337B8E4
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 26 Jun 2000 18:14:54 -0700 (PDT)
	(envelope-from kbyanc@teapot.egroups.com)
Received: (qmail 20860 invoked from network); 27 Jun 2000 01:14:54 -0000
Received: (QMFILT: 1.0); 27 Jun 2000 02:14:54 -0000
Received: from dhcp147.corp.onelist.com (HELO kbyanc.corp.ONElist.com) (192.168.10.147)
  by teapot.egroups.net with SMTP; 27 Jun 2000 01:14:53 -0000
Received: (from kbyanc@localhost)
	by kbyanc.corp.ONElist.com (8.9.3/8.9.3) id SAA24083;
	Mon, 26 Jun 2000 18:14:52 -0700 (PDT)
	(envelope-from kbyanc@teapot.egroups.com)
Message-Id: <200006270114.SAA24083@kbyanc.corp.ONElist.com>
Date: Mon, 26 Jun 2000 18:14:52 -0700 (PDT)
From: kbyanc@posi.net
Sender: kbyanc@teapot.egroups.com
Reply-To: kbyanc@posi.net
To: FreeBSD-gnats-submit@freebsd.org
Subject: patch to prevent cat'ing directories
X-Send-Pr-Version: 3.2

>Number:         19537
>Category:       bin
>Synopsis:       patch to prevent cat'ing directories
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Jun 26 18:20:03 PDT 2000
>Closed-Date:    Mon Jul 3 22:10:47 PDT 2000
>Last-Modified:  Mon Jul 03 22:11:24 PDT 2000
>Originator:     Kelly Yancey
>Release:        FreeBSD 4.0-STABLE i386
>Organization:
>Environment:

FreeBSD backroom.corp.ONElist.com 5.0-CURRENT FreeBSD 5.0-CURRENT #3: Sat
Jun 10 12:08:26 PDT 2000
kbyanc@backroom.corp.ONElist.com:/usr/src/sys/compile/BACKROOM  i386

>Description:

	Similar to my last 2 PR's (PR 19514 and PR 19536), this patch
	prevents the user from cat'ing a directory (ala more(1)). With
	true 20/20 hindsight, I should have rolled all these patches
	together, but I promise this is the last one in this series...for
	now :).

>How-To-Repeat:

	cat .

>Fix:

Index: bin/cat/cat.c
===================================================================
RCS file: /home/cvs/src/bin/cat/cat.c,v
retrieving revision 1.15
diff -u -r1.15 cat.c
--- bin/cat/cat.c	2000/04/14 21:01:35	1.15
+++ bin/cat/cat.c	2000/06/27 01:07:13
@@ -68,6 +68,7 @@
 int main __P((int argc, char *argv[]));
 void raw_args __P((char *argv[]));
 void raw_cat __P((int));
+void checkmode __P((struct stat *, char *));
 
 int
 main(argc, argv)
@@ -121,6 +122,7 @@
 cook_args(argv)
 	char **argv;
 {
+	struct stat sb;
 	register FILE *fp;
 
 	fp = stdin;
@@ -129,12 +131,14 @@
 		if (*argv) {
 			if (!strcmp(*argv, "-"))
 				fp = stdin;
-			else if ((fp = fopen(*argv, "r")) == NULL) {
+			else if ((fp = fopen(*argv, "r")) == NULL ||
+			    fstat(fileno(fp), &sb)) {
 				warn("%s", *argv);
 				rval = 1;
 				++argv;
 				continue;
 			}
+			checkmode(&sb, *argv);
 			filename = *argv++;
 		}
 		cook_buf(fp);
@@ -211,6 +215,7 @@
 raw_args(argv)
 	char **argv;
 {
+	struct stat sb;
 	register int fd;
 
 	fd = fileno(stdin);
@@ -219,12 +224,14 @@
 		if (*argv) {
 			if (!strcmp(*argv, "-"))
 				fd = fileno(stdin);
-			else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
+			else if ((fd = open(*argv, O_RDONLY, 0)) < 0 ||
+			    fstat(fd, &sb)) {
 				warn("%s", *argv);
 				rval = 1;
 				++argv;
 				continue;
 			}
+			checkmode(&sb, *argv);
 			filename = *argv++;
 		}
 		raw_cat(fd);
@@ -259,4 +266,21 @@
 		warn("%s", filename);
 		rval = 1;
 	}
+}
+
+void
+checkmode(sb, fname)
+	struct stat *sb;	
+	char *fname;
+{
+	if (sb->st_mode & S_IFDIR)
+		errx(1, "%s is a directory", fname);
+	if (sb->st_mode & S_IFLNK)
+		/* This should transparently be resolved and
+		 * thus never happen.
+		 */
+		errx(1, "%s is a symlink", fname);
+	if (sb->st_mode & S_IFWHT)
+		/* This should never happen. */
+		errx(1, "%s is a whiteout entry", fname);
 }

>Release-Note:
>Audit-Trail:

From: Kelly Yancey <kbyanc@posi.net>
To: freebsd-gnats-submit@FreeBSD.org
Cc:  
Subject: Re: bin/19537: patch to prevent cat'ing directories
Date: Mon, 26 Jun 2000 22:32:59 -0700 (PDT)

   Actually, the following is a more correct patch, please apply it instead.
 
   Kelly
 
 Index: bin/cat/cat.c
 ===================================================================
 RCS file: /home/cvs/src/bin/cat/cat.c,v
 retrieving revision 1.15
 diff -u -r1.15 cat.c
 --- bin/cat/cat.c	2000/04/14 21:01:35	1.15
 +++ bin/cat/cat.c	2000/06/27 05:24:39
 @@ -68,6 +68,7 @@
  int main __P((int argc, char *argv[]));
  void raw_args __P((char *argv[]));
  void raw_cat __P((int));
 +void checkmode __P((struct stat *, char *));
  
  int
  main(argc, argv)
 @@ -121,6 +122,7 @@
  cook_args(argv)
  	char **argv;
  {
 +	struct stat sb;
  	register FILE *fp;
  
  	fp = stdin;
 @@ -129,12 +131,14 @@
  		if (*argv) {
  			if (!strcmp(*argv, "-"))
  				fp = stdin;
 -			else if ((fp = fopen(*argv, "r")) == NULL) {
 +			else if ((fp = fopen(*argv, "r")) == NULL ||
 +			    fstat(fileno(fp), &sb)) {
  				warn("%s", *argv);
  				rval = 1;
  				++argv;
  				continue;
  			}
 +			checkmode(&sb, *argv);
  			filename = *argv++;
  		}
  		cook_buf(fp);
 @@ -211,6 +215,7 @@
  raw_args(argv)
  	char **argv;
  {
 +	struct stat sb;
  	register int fd;
  
  	fd = fileno(stdin);
 @@ -219,12 +224,14 @@
  		if (*argv) {
  			if (!strcmp(*argv, "-"))
  				fd = fileno(stdin);
 -			else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
 +			else if ((fd = open(*argv, O_RDONLY, 0)) < 0 ||
 +			    fstat(fd, &sb)) {
  				warn("%s", *argv);
  				rval = 1;
  				++argv;
  				continue;
  			}
 +			checkmode(&sb, *argv);
  			filename = *argv++;
  		}
  		raw_cat(fd);
 @@ -259,4 +266,21 @@
  		warn("%s", filename);
  		rval = 1;
  	}
 +}
 +
 +void
 +checkmode(sb, fname)
 +	struct stat *sb;	
 +	char *fname;
 +{
 +	if (S_ISDIR(sb->st_mode))
 +		errx(1, "%s is a directory", fname);
 +	if (S_ISLNK(sb->st_mode))
 +		/* This should be transparently resolved and
 +		 * thus never happen.
 +		 */
 +		errx(1, "%s is a symlink", fname);
 +	if (S_ISWHT(sb->st_mode))
 +		/* This should never happen. */
 +		errx(1, "%s is a whiteout entry", fname);
  }
 
 

From: Kelly Yancey <kbyanc@egroups.net>
To: freebsd-gnats-submit@FreeBSD.org
Cc:  
Subject: Re: bin/19537: patch to prevent cat'ing directories
Date: Wed, 28 Jun 2000 16:47:19 -0700 (PDT)

   BDE kindly pointed out that POSIX specifies that for cat:
 	The input files can be any file type.
 
   That includes directories, so this PR is bogus. Please close. Thanks,
 
   Kelly
 
 
State-Changed-From-To: open->closed 
State-Changed-By: jhb 
State-Changed-When: Mon Jul 3 22:10:47 PDT 2000 
State-Changed-Why:  
Closed at the request of the originator. 

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