Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sh_suidchk.c

    r19 r30  
    415415  long            sl_status = SL_ENONE;
    416416  struct stat     fileInfo;
     417  struct stat     fileInfo_F;
     418  int             file_d;
    417419
    418420  file_type       theFile;
    419421  char            fileHash[2*(KEY_LEN + 1)];
     422
     423  mode_t          umask_old;
     424  int             cperm_status;
    420425
    421426  SL_ENTER(_("sh_suidchk_check_internal"));
     
    519524               */
    520525              fs = filesystem_type (tmpcat, tmpcat, &buf);
    521               if (fs != NULL &&
     526              if (fs != NULL
     527#ifndef SH_SUIDTESTDIR
     528                  &&
     529                  0 != strncmp (_("afs"),     fs, 3) &&
     530                  0 != strncmp (_("devfs"),   fs, 5) &&
     531                  0 != strncmp (_("iso9660"), fs, 7) &&
     532                  0 != strncmp (_("lustre"),  fs, 6) &&
     533                  0 != strncmp (_("mmfs"),    fs, 4) &&
     534                  0 != strncmp (_("msdos"),   fs, 5) &&
    522535                  0 != strncmp (_("nfs"),     fs, 3) &&
     536                  0 != strncmp (_("nosuid"),  fs, 6) &&
    523537                  0 != strncmp (_("proc"),    fs, 4) &&
    524                   0 != strncmp (_("iso9660"), fs, 7) &&
    525                   0 != strncmp (_("vfat"),    fs, 4) &&
    526                   0 != strncmp (_("msdos"),   fs, 5) &&
    527                   0 != strncmp (_("devfs"),   fs, 5) &&
    528                   0 != strncmp (_("nosuid"),  fs, 6)
     538                  0 != strncmp (_("vfat"),    fs, 4)
     539#endif
    529540                  )
    530541                {
     
    666677                                    break;
    667678                                  case SH_Q_CHANGEPERM:
     679                                    cperm_status = 0;
     680                                    file_d = -1;
    668681                                    if (retry_lstat(FIL__, __LINE__, tmpcat, &fileInfo) == -1)
    669682                                      {
     
    677690                                                         tmp );
    678691                                        SH_FREE(msg);
     692                                        cperm_status = -1;
    679693                                      }
    680                                     else
     694
     695                                    if (cperm_status == 0)
    681696                                      {
    682697                                        if (0 != (caperr = sl_get_cap_qdel()))
     
    686701                                                            sh_error_message (caperr),
    687702                                                            _("sl_get_cap_qdel"));
     703                                            cperm_status = -1;
    688704                                          }
    689 
    690                                         if ((fileInfo.st_mode & S_ISUID) > 0)
    691                                           fileInfo.st_mode -= S_ISUID;
    692                                         if ((fileInfo.st_mode & S_ISGID) > 0)
    693                                           fileInfo.st_mode -= S_ISGID;
    694                                         if (chmod(tmpcat, fileInfo.st_mode) == -1)
     705                                      }
     706
     707                                    if (cperm_status == 0)
     708                                      {
     709                                        file_d = aud_open (FIL__, __LINE__, SL_YESPRIV,
     710                                                           tmpcat, O_RDONLY, 0);
     711                                        if (-1 == file_d)
     712                                          {
     713                                            status = errno;
     714                                            msg = SH_ALLOC(SH_BUFSIZE);
     715                                            (void) sl_snprintf(msg, SH_BUFSIZE, _("I/O error.  errno = %ld"), status);
     716                                            sh_error_handle (ShSuidchkSeverity,
     717                                                             FIL__, __LINE__,
     718                                                             status,
     719                                                             MSG_SUID_QREPORT, msg,
     720                                                             tmp );
     721                                            SH_FREE(msg);
     722                                            cperm_status = -1;
     723                                          }
     724                                      }
     725
     726                                    if (cperm_status == 0)
     727                                      {
     728                                        if (retry_fstat(FIL__, __LINE__, file_d, &fileInfo_F) == -1)
     729                                          {
     730                                            status = errno;
     731                                            msg = SH_ALLOC(SH_BUFSIZE);
     732                                            (void) sl_snprintf(msg, SH_BUFSIZE,
     733                                                               _("I/O error.  errno = %ld"), status);
     734                                            sh_error_handle (ShSuidchkSeverity,
     735                                                             FIL__, __LINE__,
     736                                                             status,
     737                                                             MSG_SUID_QREPORT, msg,
     738                                                             tmp );
     739                                            SH_FREE(msg);
     740                                            cperm_status = -1;
     741                                          }
     742                                      }
     743
     744                                    if (cperm_status == 0)
     745                                      {
     746                                        if (fileInfo_F.st_ino  != fileInfo.st_ino ||
     747                                            fileInfo_F.st_dev  != fileInfo.st_dev ||
     748                                            fileInfo_F.st_mode != fileInfo.st_mode)
     749                                          {
     750                                            status = errno;
     751                                            msg = SH_ALLOC(SH_BUFSIZE);
     752                                            (void) sl_snprintf(msg, SH_BUFSIZE,
     753                                                               _("Race detected.  errno = %ld"), status);
     754                                            sh_error_handle (ShSuidchkSeverity,
     755                                                             FIL__, __LINE__,
     756                                                             status,
     757                                                             MSG_SUID_QREPORT, msg,
     758                                                             tmp );
     759                                            SH_FREE(msg);
     760                                            cperm_status = -1;
     761                                          }
     762                                      }
     763
     764                                    if ((fileInfo.st_mode & S_ISUID) > 0)
     765                                      fileInfo.st_mode -= S_ISUID;
     766                                    if ((fileInfo.st_mode & S_ISGID) > 0)
     767                                      fileInfo.st_mode -= S_ISGID;
     768
     769                                    if (cperm_status == 0)
     770                                      {
     771                                        if (fchmod(file_d, fileInfo.st_mode) == -1)
    695772                                          {
    696773                                            status = errno;
     
    713790                                                             tmp );
    714791                                          }
    715                                         if (0 != (caperr = sl_drop_cap_qdel()))
     792                                      }
     793
     794                                    if (0 != (caperr = sl_drop_cap_qdel()))
     795                                      {
     796                                        sh_error_handle((-1), FIL__, __LINE__,
     797                                                        caperr, MSG_E_SUBGEN,
     798                                                        sh_error_message (caperr),
     799                                                        _("sl_drop_cap_qdel"));
     800                                      }
     801
     802                                    if (file_d != -1)
     803                                      {
     804                                        do {
     805                                          status = close (file_d);
     806                                        } while (status == -1 && errno == EINTR);
     807
     808                                        if (-1 == status)
    716809                                          {
    717                                             sh_error_handle((-1), FIL__, __LINE__,
    718                                                             caperr, MSG_E_SUBGEN,
    719                                                             sh_error_message (caperr),
    720                                                             _("sl_drop_cap_qdel"));
     810                                            status = errno;
     811                                            msg = SH_ALLOC(SH_BUFSIZE);
     812                                            (void) sl_snprintf(msg, SH_BUFSIZE,
     813                                                               _("I/O error.  errno = %ld"), status);
     814                                            sh_error_handle (ShSuidchkSeverity,
     815                                                             FIL__, __LINE__,
     816                                                             status,
     817                                                             MSG_SUID_QREPORT, msg,
     818                                                             tmp );
     819                                            SH_FREE(msg);
     820                                            cperm_status = -1;
    721821                                          }
    722822                                      }
     
    725825                                    dir = SH_ALLOC(PATH_MAX+1);
    726826                                    (void) sl_strlcpy (dir, DEFAULT_QDIR, PATH_MAX+1);
    727                                     if (access (dir, F_OK) != 0)
     827                                    if (retry_stat (FIL__, __LINE__, dir, &fileInfo) != 0)
    728828                                      {
    729829                                        status = errno;
    730830                                        msg = SH_ALLOC(SH_BUFSIZE);
    731                                         (void) sl_snprintf(msg, SH_BUFSIZE, _("Problem quarantining file.  File NOT quarantined.  errno = %ld (access)"), status);
     831                                        (void) sl_snprintf(msg, SH_BUFSIZE, _("Problem quarantining file.  File NOT quarantined.  errno = %ld (stat)"), status);
    732832                                        sh_error_handle (ShSuidchkSeverity,
    733833                                                         FIL__, __LINE__,
     
    815915                                                                   DEFAULT_QDIR,
    816916                                                                   basename(theFile.fullpath));
     917                                                /*
     918                                                 * avoid chmod by setting umask
     919                                                 */
     920                                                umask_old = umask (0077);
    817921                                                filePtr = fopen (filetmp, "w+");
    818922                                                /*@-usedef@*/
     
    828932                                                  }
    829933                                                /*@+usedef@*/
     934                                                umask (umask_old);
    830935                                       
    831936                                                sh_error_handle (ShSuidchkSeverity,
     
    834939                                                                 _("Quarantine method applied"),
    835940                                                                 tmp );
    836                                                 if (chmod(filetmp, S_IRUSR | S_IWUSR) == -1)
    837                                                   {
    838                                                     status = errno;
    839                                                     msg = SH_ALLOC(SH_BUFSIZE);
    840                                                     (void) sl_snprintf(msg, SH_BUFSIZE, _("Problem setting permissions on quarantined file.  errno = %ld"), status);
    841                                                     sh_error_handle (ShSuidchkSeverity,
    842                                                                      FIL__,__LINE__,
    843                                                                      status, MSG_SUID_QREPORT,
    844                                                                      msg, tmp );
    845                                                     SH_FREE(msg);
    846                                                   }
    847941                                              }
    848942                                            SH_FREE(filetmp);
     
    9761070  FileLimTotal      = 0;
    9771071
     1072#ifdef SH_SUIDTESTDIR
     1073  status = sh_suidchk_check_internal (SH_SUIDTESTDIR);
     1074#else
    9781075  status = sh_suidchk_check_internal ("/");
     1076#endif
    9791077
    9801078  sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_SUID_SUMMARY,
     
    11691267  long val;
    11701268  int  ret = 0;
     1269  struct stat buf;
    11711270
    11721271  SL_ENTER(_("sh_suidchk_set_qmethod"));
     
    11901289          break;
    11911290        case SH_Q_MOVE:
    1192           if (access (DEFAULT_QDIR, F_OK) != 0)
     1291          if (retry_stat (FIL__, __LINE__, DEFAULT_QDIR, &buf) != 0)
    11931292            {
    11941293              if (mkdir (DEFAULT_QDIR, 0750) == -1)
Note: See TracChangeset for help on using the changeset viewer.