From owner-freebsd-bugs Mon Jun 26 18:20: 7 2000 Delivered-To: freebsd-bugs@freebsd.org Received: from freefall.freebsd.org (freefall.FreeBSD.ORG [204.216.27.21]) by hub.freebsd.org (Postfix) with ESMTP id 3C6FA37BD95 for ; Mon, 26 Jun 2000 18:20:04 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.9.3/8.9.2) id SAA47236; Mon, 26 Jun 2000 18:20:04 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Received: from teapot.egroups.net (teapot.egroups.net [63.204.207.250]) by hub.freebsd.org (Postfix) with SMTP id 6A19337B8E4 for ; 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 Reply-To: kbyanc@posi.net To: FreeBSD-gnats-submit@freebsd.org X-Send-Pr-Version: 3.2 Subject: bin/19537: patch to prevent cat'ing directories Sender: owner-freebsd-bugs@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org >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