misuse of S_IF*

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

misuse of S_IF*

Paul Stoeber
For example, the test (mode & S_IFSOCK) matches
directories, block devices, regular files, symlinks,
and sockets.

Below is a diff for systrace.  The tree has more suspects:

/usr/src/sys/compat/hpux/hpux_file.c: if (SCARG(uap, mode) & S_IFIFO) {
/usr/src/sys/compat/linux/linux_file.c: if (SCARG(uap, mode) & S_IFIFO) {
/usr/src/games/fortune/fortune/fortune.c: return (sbuf.st_mode & S_IFDIR);
/usr/src/gnu/usr.bin/binutils/gdb/remote-fileio.c:  if (mode & S_IFREG)
/usr/src/gnu/usr.bin/binutils/gdb/remote-fileio.c:  if (mode & S_IFDIR)
/usr/src/gnu/usr.bin/binutils/gdb/remote-fileio.c:  if (mode & S_IFCHR)
/usr/src/gnu/usr.bin/cvs/contrib/dirfns.shar:X if (fstat(fd, &statbuf) == -1 || !(statbuf.st_mode & S_IFDIR)) {
/usr/src/gnu/usr.bin/gcc/gcc/fixinc/inclhack.def:    test_text = '#define S_ISREG(m) (m & S_IFREG) /* is regular? */';
/usr/src/lib/libssl/src/apps/ca.c: if (!(sb.st_mode & S_IFDIR))
/usr/src/sbin/isakmpd/x509.c: if (!(sb.st_mode & S_IFREG)) {
/usr/src/sbin/isakmpd/x509.c: if (!(sb.st_mode & S_IFREG)) {
/usr/src/usr.bin/grep/mmfile.c: if ((st.st_mode & S_IFREG) == 0) /* only mmap regular files */
/usr/src/usr.bin/mg/fileio.c: if (statbuf.st_mode & S_IFDIR)
/usr/src/usr.bin/pkill/pkill.c: if ((st.st_mode & S_IFCHR) == 0)
/usr/src/usr.bin/rcs/rcsutil.c: if (stat(rcsdir, &sb) == 0 && (sb.st_mode & S_IFDIR))
/usr/src/usr.bin/sdiff/sdiff.c: if (sb.st_mode & S_IFREG)
/usr/src/usr.bin/ssh/sftp.c: return(sb.st_mode & S_IFDIR);
/usr/src/usr.bin/ssh/sftp.c: return(a->perm & S_IFDIR);
/usr/src/usr.sbin/bind/bin/named/unix/os.c: } else if ((sb.st_mode & S_IFREG) == 0) {
/usr/src/usr.sbin/bind/bin/rndc/unix/os.c:        } else if ((sb.st_mode & S_IFREG) == 0) {
/usr/src/usr.sbin/bind/lib/isc/unix/fsaccess.c: if ((statb.st_mode & S_IFDIR) != 0)
/usr/src/usr.sbin/bind/lib/isc/unix/fsaccess.c: else if ((statb.st_mode & S_IFREG) == 0)
/usr/src/usr.sbin/ppp/ppp/ether.c:    if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) {
/usr/src/usr.sbin/ppp/ppp/server.c:      if (!(st.st_mode & S_IFSOCK) || unlink(server.cfg.sockname) != 0)
/usr/src/usr.sbin/ppp/ppp/tcp.c:    if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) {
/usr/src/usr.sbin/ppp/ppp/udp.c:    if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) {
/usr/src/usr.sbin/ypserv/ypserv/ypserv_proc.c:    (finfo.st_mode & S_IFDIR));
/usr/src/usr.sbin/ypserv/ypserv/ypserv_proc.c:    (finfo.st_mode & S_IFDIR));
/usr/src/usr.sbin/ypserv/ypserv/ypserv_proc.c:    (finfo.st_mode & S_IFDIR));
/usr/src/usr.sbin/ypserv/ypserv/ypserv_proc.c:    (finfo.st_mode & S_IFDIR));


--- bin/systrace/policy.c.orig Wed Sep 13 11:24:10 2006
+++ bin/systrace/policy.c Wed Sep 13 11:34:24 2006
@@ -127,7 +127,7 @@
 
 
  if (stat(policydir, &sb) != -1) {
- if (!(sb.st_mode & S_IFDIR))
+ if (!S_ISDIR(sb.st_mode))
  errx(1, "Not a directory: \"%s\"", policydir);
  } else if (mkdir(policydir, 0700) == -1)
  err(1, "mkdir(%s)", policydir);
@@ -438,7 +438,7 @@
  goto error;
 
  /* Check if template directory exists */
- if (stat(filename, &sb) != -1 && (sb.st_mode & S_IFDIR))
+ if (stat(filename, &sb) != -1 && S_ISDIR(sb.st_mode))
  dir = opendir(filename);
  }
 
@@ -446,7 +446,7 @@
  if (dir == NULL) {
  strlcpy(filename, POLICY_PATH, sizeof(filename));
  strlcat(filename, "/templates", sizeof(filename));
- if (stat(filename, &sb) != -1 && (sb.st_mode & S_IFDIR))
+ if (stat(filename, &sb) != -1 && S_ISDIR(sb.st_mode))
  dir = opendir(filename);
  if (dir == NULL)
  return (-1);
@@ -462,7 +462,7 @@
     sizeof(filename))
  goto error;
 
- if (stat(filename, &sb) == -1 || !(sb.st_mode & S_IFREG))
+ if (stat(filename, &sb) == -1 || !S_ISREG(sb.st_mode))
  continue;
 
  template = systrace_readtemplate(filename, NULL, NULL);

Reply | Threaded
Open this post in threaded view
|

Re: misuse of S_IF*

Paul Stoeber
Somehow this one fell through the cracks:

--- bin/systrace/intercept.c.orig Wed Sep 13 11:24:16 2006
+++ bin/systrace/intercept.c Wed Sep 13 12:03:47 2006
@@ -704,7 +704,7 @@
  */
  if (userp != ICLINK_NOLAST) {
  if (lstat(rcwd, &st) == -1 ||
-    !(st.st_mode & S_IFDIR))
+    !S_ISDIR(st.st_mode))
  failed = 1;
  }
  }