Changeset 78 for trunk/src


Ignore:
Timestamp:
Jan 9, 2007, 10:32:21 PM (18 years ago)
Author:
rainer
Message:

Fix for ticket #41 (unable to deactivate processcheck), and minor code cleanup.

Location:
trunk/src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sh_cat.c

    r68 r78  
    127127  { MSG_PCK_CHECK,   SH_ERR_NOTICE,  RUN,   N_("msg=\"Checking processes in pid interval [%ld,%ld]\"")},
    128128  { MSG_PCK_OK,      SH_ERR_ALL,     RUN,   N_("msg=\"PID %ld found with tests %s\"")},
     129  { MSG_PCK_P_HIDDEN,SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [Process] Hidden pid: %ld tests: %s\" path=\"%s\" userid=\"%s\"")},
    129130  { MSG_PCK_HIDDEN,  SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [Process] Hidden pid: %ld tests: %s\"")},
    130131  { MSG_PCK_FAKE,    SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [Process] Fake pid: %ld tests: %s\"")},
     
    441442  { MSG_PCK_CHECK,   SH_ERR_NOTICE,  RUN,   N_("msg=<Checking processes in pid interval [%ld,%ld]>")},
    442443  { MSG_PCK_OK,      SH_ERR_ALL,     RUN,   N_("msg=<PID %ld found with tests %s>")},
     444  { MSG_PCK_P_HIDDEN,SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [Process] Hidden pid: %ld tests: %s> path=<%s> userid=<%s>")},
    443445  { MSG_PCK_HIDDEN,  SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [Process] Hidden pid: %ld tests: %s>")},
    444446  { MSG_PCK_FAKE,    SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [Process] Fake pid: %ld tests: %s>")},
  • trunk/src/sh_entropy.c

    r32 r78  
    310310    {
    311311      /* Test whether file is a character device, and is
    312        * not world writeable.
    313        */
    314       if (0 == sh_unix_file_exists(fd2))
     312       * readable.
     313       */
     314      if (0 == sh_unix_device_readable(fd2))
    315315        {
    316316
  • trunk/src/sh_files.c

    r77 r78  
    20772077      if (sl_strlen(testpath) == (17 + sl_strlen(fullpath)))
    20782078        {
    2079           if (0 != sh_unix_file_stat (testpath))
     2079          if (S_TRUE == sh_unix_file_exists (testpath))
    20802080            {
    20812081              sh_files_filecheck (class, fullpath, rsrc, &dummy, 1);
  • trunk/src/sh_portcheck.c

    r76 r78  
    593593  char             * p;
    594594  char               buf[8];
    595 
     595#ifndef TEST_ONLY
     596  char               errmsg[256];
     597  int                nerr;
     598#endif
    596599
    597600  /* inet_aton(interface, &haddr); */
     
    601604  sinr.sin_addr   = haddr;
    602605
    603   retval = connect(fd, (struct sockaddr *) &sinr, sizeof(sinr));
     606  do {
     607    retval = connect(fd, (struct sockaddr *) &sinr, sizeof(sinr));
     608  } while (retval < 0 && errno == EINTR);
     609
    604610  if (retval == -1)
    605611    {
     
    608614        perror(_("connect"));
    609615#else
    610       sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
    611                       sh_error_message(errno), _("connect"));
     616      nerr = errno;
     617      sl_snprintf(errmsg, sizeof(errmsg), _("check port: %5d/udp on %15s: %s"),
     618                  port, inet_ntoa(haddr), sh_error_message(errno));
     619      sh_error_handle((-1), FIL__, __LINE__, nerr, MSG_E_SUBGEN,
     620                      errmsg, _("connect"));
    612621#endif
    613622    }
    614623  else
    615624    {
    616       retval = send (fd, buf, 0, 0);
     625      do {
     626        retval = send (fd, buf, 0, 0);
     627      } while (retval < 0 && errno == EINTR);
    617628
    618629      if (retval == -1 && errno == ECONNREFUSED)
     
    624635      else
    625636        {
    626           retval = send (fd, buf, 0, 0);
     637          /* Only the second send() may catch the error
     638           */
     639          do {
     640            retval = send (fd, buf, 0, 0);
     641          } while (retval < 0 && errno == EINTR);
     642
    627643          if (retval == -1 && errno == ECONNREFUSED)
    628644            {
     
    661677  int                retval;
    662678  char             * p;
     679#ifndef TEST_ONLY
     680  char               errmsg[256];
     681  int                nerr;
     682#endif
    663683
    664684
     
    669689  sinr.sin_addr   = haddr;
    670690
    671   retval = connect(fd, (struct sockaddr *) &sinr, sizeof(sinr));
     691  do {
     692    retval = connect(fd, (struct sockaddr *) &sinr, sizeof(sinr));
     693  } while (retval < 0 && errno == EINTR);
     694
    672695  if (retval == -1 && errno == ECONNREFUSED)
    673696    {
     
    682705        perror(_("connect"));
    683706#else
    684       sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
    685                       sh_error_message(errno), _("connect"));
     707      nerr = errno;
     708      sl_snprintf(errmsg, sizeof(errmsg), _("check port: %5d/tcp on %15s: %s"),
     709                  port, inet_ntoa(haddr), sh_error_message(errno));
     710      sh_error_handle((-1), FIL__, __LINE__, nerr, MSG_E_SUBGEN,
     711                      errmsg, _("connect"));
    686712#endif
    687713    }
     
    738764  if (portchk_debug)
    739765    fprintf(stderr, _("checking ports on: %s\n"), portchk_hostname ? portchk_hostname : _("NULL"));
     766
    740767  if (!portchk_hostname)
     768    return -1;
     769
     770  if (sh_portchk_active == S_FALSE)
    741771    return -1;
    742772
     
    767797int sh_portchk_reconf ()
    768798{
    769   iface_initialized   = 0;
     799  iface_initialized    = 0;
     800
     801  sh_portchk_active    = 1;
     802  sh_portchk_check_upd = 1;
    770803
    771804  portlist_udp = sh_portchk_kill_list (portlist_udp);
     
    11391172  int min_port = 0;
    11401173
    1141   sh_portchk_reset_lists();
    1142   if (0 != geteuid())
    1143     {
    1144       min_port = 1024;
    1145 #ifdef TEST_ONLY
    1146       fprintf(stderr, "** WARNING not scanning ports < 1024\n");
    1147 #else
    1148       sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
    1149                       _("not scanning ports below 1024"), _("sh_portchk_check"));
    1150 #endif
    1151     }
    1152   if (sh_portchk_check_udp == 1)
    1153     sh_portchk_scan_ports_udp(min_port, -1);
    1154   sh_portchk_scan_ports_tcp(min_port, -1);
    1155   sh_portchk_check_list (&portlist_tcp, "tcp", SH_PORT_REPORT);
    1156   if (sh_portchk_check_udp == 1)
    1157     sh_portchk_check_list (&portlist_udp, "udp", SH_PORT_REPORT);
     1174  if (sh_portchk_active != S_FALSE)
     1175    {
     1176      sh_portchk_reset_lists();
     1177      if (0 != geteuid())
     1178        {
     1179          min_port = 1024;
     1180#ifdef TEST_ONLY
     1181          fprintf(stderr, "** WARNING not scanning ports < 1024\n");
     1182#else
     1183          sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
     1184                          _("not scanning ports below 1024"), _("sh_portchk_check"));
     1185#endif
     1186        }
     1187      if (sh_portchk_check_udp == 1)
     1188        sh_portchk_scan_ports_udp(min_port, -1);
     1189      sh_portchk_scan_ports_tcp(min_port, -1);
     1190      sh_portchk_check_list (&portlist_tcp, "tcp", SH_PORT_REPORT);
     1191      if (sh_portchk_check_udp == 1)
     1192        sh_portchk_check_list (&portlist_udp, "udp", SH_PORT_REPORT);
     1193    }
    11581194  return 0;
    11591195}
  • trunk/src/sh_processcheck.c

    r77 r78  
    4848#endif
    4949
     50#ifdef HAVE_SYS_STATVFS_H
     51#include <sys/statvfs.h>
     52#endif
     53
     54
    5055#ifdef HAVE_REGEX_H
    5156#include <regex.h>
     
    5863#include "sh_error.h"
    5964#include "sh_extern.h"
     65#include "sh_calls.h"
    6066
    6167#ifdef SH_USE_PROCESSCHECK
     
    157163
    158164static const short SH_PR_PRIORITY = 0x0100;
    159 
    160 static const short SH_PR_PS2      = 0x0200;
    161 static const short SH_PR_PS_ANY   = 0x0400;
    162 static const short SH_PR_ALL      = 0x0800;
    163 static const short SH_PR_ANY      = 0x1000;
     165static const short SH_PR_STATVSF  = 0x0200;
     166
     167static const short SH_PR_PS2      = 0x1000;
     168static const short SH_PR_PS_ANY   = 0x2000;
     169static const short SH_PR_ALL      = 0x4000;
     170static const short SH_PR_ANY      = 0x8000;
    164171
    165172/* /proc:
     
    168175 *        solaris10: /proc/pid/path/a.out
    169176 */
     177static char * get_user_and_path (pid_t pid, char * user, size_t usrlen)
     178{
     179  extern char *  sh_unix_getUIDname (int level, uid_t uid);
     180
     181  char        path[128];
     182  char *      buf;
     183  struct stat sbuf;
     184  int         len;
     185  char *      tmp;
     186
     187  sl_snprintf (path, sizeof(path), "/proc/%ld/exe", (unsigned long) pid);
     188
     189  if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
     190    {
     191      goto linkread;
     192    }
     193
     194  sl_snprintf (path, sizeof(path), "/proc/%ld/file", (unsigned long) pid);
     195
     196  if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
     197    {
     198      goto linkread;
     199    }
     200
     201  sl_snprintf (path, sizeof(path), "/proc/%ld/path/a.out", (unsigned long) pid);
     202
     203  if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
     204    {
     205      goto linkread;
     206    }
     207
     208  return NULL;
     209
     210 linkread:
     211
     212  buf = SH_ALLOC(PATH_MAX);
     213  len = readlink(path, buf, PATH_MAX);   /* flawfinder: ignore */
     214  len = (len >= PATH_MAX) ? (PATH_MAX-1) : len;
     215
     216  if (len > 0)
     217    {
     218      buf[len] = '\0';
     219    }
     220  else
     221    {
     222      SH_FREE(buf);
     223      return NULL;
     224    }
     225
     226  tmp = sh_unix_getUIDname (SH_ERR_ALL, sbuf.st_uid);
     227
     228  if (tmp)
     229    sl_strlcpy(user, tmp, usrlen);
     230  else
     231    sl_snprintf (user, usrlen, "%ld", (unsigned long) sbuf.st_uid);
     232
     233  return buf;
     234}
     235
    170236
    171237struct watchlist {
     
    483549  size_t  value;
    484550  char * foo;
    485   int    retval = 0;
     551  int    retval = -1;
    486552
    487553  SL_ENTER(_("sh_prochk_set_maxpid"));
    488554
    489555  value = (size_t) strtoul(str, &foo, 0);
    490   if (*foo != '\0') {
    491     retval = -1;
    492   } else {
     556
     557  if (*foo == '\0' && SL_TRUE == sl_ok_adds(value, 1)) {
    493558    sh_prochk_maxpid = value + 1;
    494559    userdef_maxpid   = 1;
     
    593658  if (res & SH_PR_GETPGID)  sl_strlcat(list, _(" getpgid"), len);
    594659  if (res & SH_PR_KILL)     sl_strlcat(list, _(" kill"), len);
     660  if (res & SH_PR_STATVSF)  sl_strlcat(list, _(" statvfs"), len);
    595661  if (res & SH_PR_PS2)      sl_strlcat(list, _(" ps(final)"), len);
    596662  return;
    597663}
    598664
    599 /* FIXME: add open, statvfs, sched_getaffinity
    600  */
     665
    601666static short sh_processes_check (pid_t pid, short res)
    602667{
     668  int  retval;
    603669  int  have_checks = 0;
    604670  int  need_checks = 0;
     
    608674  DIR * dir;
    609675#endif
     676#ifdef HAVE_STATVFS
     677  struct statvfs vfsbuf;
     678#endif
    610679
    611680#if !defined(sun) && !defined(__sun) && !defined(__sun__)
     
    624693      ++need_checks;
    625694    }
     695
    626696
    627697#ifdef HAVE_GETPGID
     
    666736  sl_snprintf (path, sizeof(path), "/proc/%ld", (unsigned long) pid);
    667737
    668   if (0 == lstat (path, &buf))
     738  if (0 == retry_lstat (FIL__, __LINE__, path, &buf))
    669739    {
    670740      res |= SH_PR_LSTAT;   res |= SH_PR_ANY; ++have_checks;
     
    678748    }
    679749  ++need_checks;
     750
     751#ifdef HAVE_STATVFS
     752  do {
     753    retval = statvfs (path, &vfsbuf);
     754  } while (retval < 0 && errno == EINTR);
     755
     756  if (0 == retval)
     757    {
     758      res |= SH_PR_STATVSF;   res |= SH_PR_ANY; ++have_checks;
     759    }
     760  ++need_checks;
     761#endif
    680762
    681763#if !defined(SH_PROFILE)
     
    9721054                  if (S_FALSE == is_in_list(&list_hidden, NULL, i))
    9731055                    {
    974                       sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0,
    975                                       MSG_PCK_HIDDEN,
    976                                       (unsigned long) i, tests);
     1056                      char   user[16];
     1057                      char * aout;
     1058                      char * safe;
     1059
     1060                      aout = get_user_and_path ((pid_t) i, user, sizeof(user));
     1061
     1062                      if (aout)
     1063                        {
     1064                          safe = sh_util_safe_name (aout);
     1065                          sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0,
     1066                                          MSG_PCK_P_HIDDEN,
     1067                                          (unsigned long) i, tests, safe, user);
     1068                          SH_FREE(safe);
     1069                          SH_FREE(aout);
     1070                        }
     1071                      else
     1072                        sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0,
     1073                                        MSG_PCK_HIDDEN,
     1074                                        (unsigned long) i, tests);
     1075                       
    9771076                    }
    9781077                }
  • trunk/src/sh_unix.c

    r76 r78  
    38333833/* Test whether file exists
    38343834 */
    3835 int sh_unix_file_stat(char * path)
     3835int sh_unix_file_exists(char * path)
    38363836{
    38373837  struct stat buf;
    38383838
    3839   SL_ENTER(_("sh_unix_file_stat"));
     3839  SL_ENTER(_("sh_unix_file_exists"));
    38403840
    38413841  if (-1 == retry_stat(FIL__, __LINE__, path, &buf))
    3842     SL_RETURN(  (0), _("sh_unix_file_stat"));
     3842    SL_RETURN( S_FALSE, _("sh_unix_file_exists"));
    38433843  else
    3844     SL_RETURN( (-1), _("sh_unix_file_stat"));
    3845 }
    3846 
    3847 
    3848 /* Test whether file exists, is a character device, allows read
    3849  * access, and is not world writeable.
     3844    SL_RETURN( S_TRUE,  _("sh_unix_file_exists"));
     3845}
     3846
     3847
     3848/* Test whether file exists, is a character device, and allows read
     3849 * access.
    38503850 */
    3851 int sh_unix_file_exists(int fd)
     3851int sh_unix_device_readable(int fd)
    38523852{
    38533853  struct stat buf;
    38543854
    3855   SL_ENTER(_("sh_unix_file_exists"));
     3855  SL_ENTER(_("sh_unix_device_readable"));
    38563856
    38573857  if (retry_fstat(FIL__, __LINE__, fd, &buf) == -1)
    3858     SL_RETURN( (-1), _("sh_unix_file_exists"));
    3859   else if ( S_ISCHR(buf.st_mode) &&  0 != (S_IROTH & buf.st_mode)
    3860             /*
    3861              * #if !defined(__CYGWIN32__) && !defined(__CYGWIN__)
    3862              * && 0 == (S_IWOTH & buf.st_mode)
    3863              * #endif
    3864              */
    3865             )
    3866     SL_RETURN( (0), _("sh_unix_file_exists"));
     3858    SL_RETURN( (-1), _("sh_unix_device_readable"));
     3859  else if ( S_ISCHR(buf.st_mode) &&  0 != (S_IROTH & buf.st_mode) )
     3860    SL_RETURN( (0), _("sh_unix_device_readable"));
    38673861  else
    3868     SL_RETURN( (-1), _("sh_unix_file_exists"));
     3862    SL_RETURN( (-1), _("sh_unix_device_readable"));
    38693863}
    38703864
Note: See TracChangeset for help on using the changeset viewer.