Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 26 Jun 2000 18:14:52 -0700 (PDT)
From:      kbyanc@posi.net
To:        FreeBSD-gnats-submit@freebsd.org
Subject:   bin/19537: patch to prevent cat'ing directories
Message-ID:  <200006270114.SAA24083@kbyanc.corp.ONElist.com>

next in thread | raw e-mail | index | archive | help

>Number:         19537
>Category:       bin
>Synopsis:       patch to prevent cat'ing directories
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Jun 26 18:20:03 PDT 2000
>Closed-Date:
>Last-Modified:
>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:
>Unformatted:


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200006270114.SAA24083>