/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugin-runner.c

  • Committer: Teddy Hogeborn
  • Date: 2014-06-14 23:29:00 UTC
  • Revision ID: teddy@recompile.se-20140614232900-vhcywskev4u8rz2d
mandos-client: Fix some bugs on error conditions.

* plugins.d/mandos-client.c (run_network_hooks): Init "dirent" to NULL
                                                 and, later, always
                                                 free() it unless
                                                 scandirat() or
                                                 scandir() failed.
  (main): Fix free() of uninitalized memory in case scandirat() or
          scandir() of "/sys/class/net" failed.  Also, when cleaning
          up, even if GPGME temp directory is empty, do
          free(direntries) and remove the directory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
5
 
 * Copyright © 2008-2016 Teddy Hogeborn
6
 
 * Copyright © 2008-2016 Björn Påhlsson
 
5
 * Copyright © 2008-2014 Teddy Hogeborn
 
6
 * Copyright © 2008-2014 Björn Påhlsson
7
7
 * 
8
8
 * This program is free software: you can redistribute it and/or
9
9
 * modify it under the terms of the GNU General Public License as
37
37
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
38
38
                                   FD_SET(), FD_ISSET(), FD_CLR */
39
39
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
40
 
                                   WEXITSTATUS(), WTERMSIG() */
 
40
                                   WEXITSTATUS(), WTERMSIG(),
 
41
                                   WCOREDUMP() */
41
42
#include <sys/stat.h>           /* struct stat, fstat(), S_ISREG() */
42
43
#include <iso646.h>             /* and, or, not */
43
44
#include <dirent.h>             /* struct dirent, scandirat() */
75
76
#define BUFFER_SIZE 256
76
77
 
77
78
#define PDIR "/lib/mandos/plugins.d"
78
 
#define PHDIR "/lib/mandos/plugin-helpers"
79
79
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
80
80
 
81
81
const char *argp_program_version = "plugin-runner " VERSION;
347
347
 
348
348
int main(int argc, char *argv[]){
349
349
  char *plugindir = NULL;
350
 
  char *pluginhelperdir = NULL;
351
350
  char *argfile = NULL;
352
351
  FILE *conffp;
353
 
  struct dirent **direntries = NULL;
 
352
  struct dirent **direntries;
354
353
  struct stat st;
355
354
  fd_set rfds_all;
356
355
  int ret, maxfd = 0;
415
414
      .doc = "Group ID the plugins will run as", .group = 3 },
416
415
    { .name = "debug", .key = 132,
417
416
      .doc = "Debug mode", .group = 4 },
418
 
    { .name = "plugin-helper-dir", .key = 133,
419
 
      .arg = "DIRECTORY",
420
 
      .doc = "Specify a different plugin helper directory",
421
 
      .group = 2 },
422
417
    /*
423
418
     * These reproduce what we would get without ARGP_NO_HELP
424
419
     */
550
545
    case 132:                   /* --debug */
551
546
      debug = true;
552
547
      break;
553
 
    case 133:                   /* --plugin-helper-dir */
554
 
      free(pluginhelperdir);
555
 
      pluginhelperdir = strdup(arg);
556
 
      if(pluginhelperdir != NULL){
557
 
        errno = 0;
558
 
      }
559
 
      break;
560
548
      /*
561
549
       * These reproduce what we would get without ARGP_NO_HELP
562
550
       */
613
601
    case 130:                   /* --userid */
614
602
    case 131:                   /* --groupid */
615
603
    case 132:                   /* --debug */
616
 
    case 133:                   /* --plugin-helper-dir */
617
604
    case '?':                   /* --help */
618
605
    case -3:                    /* --usage */
619
606
    case 'V':                   /* --version */
700
687
        custom_argc += 1;
701
688
        {
702
689
          char **new_argv = realloc(custom_argv, sizeof(char *)
703
 
                                    * ((size_t)custom_argc + 1));
 
690
                                    * ((unsigned int)
 
691
                                       custom_argc + 1));
704
692
          if(new_argv == NULL){
705
693
            error(0, errno, "realloc");
706
694
            exitstatus = EX_OSERR;
773
761
    goto fallback;
774
762
  }
775
763
  
776
 
  {
777
 
    char *pluginhelperenv;
778
 
    bool bret = true;
779
 
    ret = asprintf(&pluginhelperenv, "MANDOSPLUGINHELPERDIR=%s",
780
 
                   pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
781
 
    if(ret != -1){
782
 
      bret = add_environment(getplugin(NULL), pluginhelperenv, true);
783
 
    }
784
 
    if(ret == -1 or not bret){
785
 
      error(0, errno, "Failed to set MANDOSPLUGINHELPERDIR"
786
 
            " environment variable to \"%s\" for all plugins\n",
787
 
            pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
788
 
    }
789
 
    if(ret != -1){
790
 
      free(pluginhelperenv);
791
 
    }
792
 
  }
793
 
  
794
764
  if(debug){
795
 
    for(plugin *p = plugin_list; p != NULL; p = p->next){
 
765
    for(plugin *p = plugin_list; p != NULL; p=p->next){
796
766
      fprintf(stderr, "Plugin: %s has %d arguments\n",
797
767
              p->name ? p->name : "Global", p->argc - 1);
798
768
      for(char **a = p->argv; *a != NULL; a++){
807
777
  
808
778
  if(getuid() == 0){
809
779
    /* Work around Debian bug #633582:
810
 
       <https://bugs.debian.org/633582> */
 
780
       <http://bugs.debian.org/633582> */
811
781
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
812
782
    if(plugindir_fd == -1){
813
783
      if(errno != ENOENT){
825
795
          }
826
796
        }
827
797
      }
828
 
      close(plugindir_fd);
 
798
      TEMP_FAILURE_RETRY(close(plugindir_fd));
829
799
    }
830
800
  }
831
801
  
859
829
    ret = set_cloexec_flag(dir_fd);
860
830
    if(ret < 0){
861
831
      error(0, errno, "set_cloexec_flag");
 
832
      TEMP_FAILURE_RETRY(close(dir_fd));
862
833
      exitstatus = EX_OSERR;
863
834
      goto fallback;
864
835
    }
890
861
    return 1;
891
862
  }
892
863
  
 
864
#ifdef __GLIBC__
 
865
#if __GLIBC_PREREQ(2, 15)
893
866
  int numplugins = scandirat(dir_fd, ".", &direntries, good_name,
894
867
                             alphasort);
 
868
#else  /* not __GLIBC_PREREQ(2, 15) */
 
869
  int numplugins = scandir(plugindir != NULL ? plugindir : PDIR,
 
870
                           &direntries, good_name, alphasort);
 
871
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
872
#else   /* not __GLIBC__ */
 
873
  int numplugins = scandir(plugindir != NULL ? plugindir : PDIR,
 
874
                           &direntries, good_name, alphasort);
 
875
#endif  /* not __GLIBC__ */
895
876
  if(numplugins == -1){
896
877
    error(0, errno, "Could not scan plugin dir");
897
 
    direntries = NULL;
 
878
    TEMP_FAILURE_RETRY(close(dir_fd));
898
879
    exitstatus = EX_OSERR;
899
880
    goto fallback;
900
881
  }
907
888
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
908
889
    if(plugin_fd == -1){
909
890
      error(0, errno, "Could not open plugin");
910
 
      free(direntries[i]);
911
891
      continue;
912
892
    }
913
893
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
914
894
    if(ret == -1){
915
895
      error(0, errno, "stat");
916
 
      close(plugin_fd);
917
 
      free(direntries[i]);
 
896
      TEMP_FAILURE_RETRY(close(plugin_fd));
918
897
      continue;
919
898
    }
920
899
    
928
907
                plugindir != NULL ? plugindir : PDIR,
929
908
                direntries[i]->d_name);
930
909
      }
931
 
      close(plugin_fd);
932
 
      free(direntries[i]);
 
910
      TEMP_FAILURE_RETRY(close(plugin_fd));
933
911
      continue;
934
912
    }
935
913
    
936
914
    plugin *p = getplugin(direntries[i]->d_name);
937
915
    if(p == NULL){
938
916
      error(0, errno, "getplugin");
939
 
      close(plugin_fd);
940
 
      free(direntries[i]);
 
917
      TEMP_FAILURE_RETRY(close(plugin_fd));
941
918
      continue;
942
919
    }
943
920
    if(p->disabled){
945
922
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
946
923
                direntries[i]->d_name);
947
924
      }
948
 
      close(plugin_fd);
949
 
      free(direntries[i]);
 
925
      TEMP_FAILURE_RETRY(close(plugin_fd));
950
926
      continue;
951
927
    }
952
928
    {
985
961
    if(ret == -1){
986
962
      error(0, errno, "pipe");
987
963
      exitstatus = EX_OSERR;
988
 
      free(direntries[i]);
989
964
      goto fallback;
990
965
    }
991
966
    if(pipefd[0] >= FD_SETSIZE){
992
967
      fprintf(stderr, "pipe()[0] (%d) >= FD_SETSIZE (%d)", pipefd[0],
993
968
              FD_SETSIZE);
994
 
      close(pipefd[0]);
995
 
      close(pipefd[1]);
 
969
      TEMP_FAILURE_RETRY(close(pipefd[0]));
 
970
      TEMP_FAILURE_RETRY(close(pipefd[1]));
996
971
      exitstatus = EX_OSERR;
997
 
      free(direntries[i]);
998
972
      goto fallback;
999
973
    }
1000
974
#ifndef O_CLOEXEC
1002
976
    ret = set_cloexec_flag(pipefd[0]);
1003
977
    if(ret < 0){
1004
978
      error(0, errno, "set_cloexec_flag");
1005
 
      close(pipefd[0]);
1006
 
      close(pipefd[1]);
 
979
      TEMP_FAILURE_RETRY(close(pipefd[0]));
 
980
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1007
981
      exitstatus = EX_OSERR;
1008
 
      free(direntries[i]);
1009
982
      goto fallback;
1010
983
    }
1011
984
    ret = set_cloexec_flag(pipefd[1]);
1012
985
    if(ret < 0){
1013
986
      error(0, errno, "set_cloexec_flag");
1014
 
      close(pipefd[0]);
1015
 
      close(pipefd[1]);
 
987
      TEMP_FAILURE_RETRY(close(pipefd[0]));
 
988
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1016
989
      exitstatus = EX_OSERR;
1017
 
      free(direntries[i]);
1018
990
      goto fallback;
1019
991
    }
1020
992
#endif  /* not O_CLOEXEC */
1025
997
    if(ret < 0){
1026
998
      error(0, errno, "sigprocmask");
1027
999
      exitstatus = EX_OSERR;
1028
 
      free(direntries[i]);
1029
1000
      goto fallback;
1030
1001
    }
1031
1002
    /* Starting a new process to be watched */
1037
1008
      error(0, errno, "fork");
1038
1009
      TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1039
1010
                                     &sigchld_action.sa_mask, NULL));
1040
 
      close(pipefd[0]);
1041
 
      close(pipefd[1]);
 
1011
      TEMP_FAILURE_RETRY(close(pipefd[0]));
 
1012
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1042
1013
      exitstatus = EX_OSERR;
1043
 
      free(direntries[i]);
1044
1014
      goto fallback;
1045
1015
    }
1046
1016
    if(pid == 0){
1072
1042
      /* no return */
1073
1043
    }
1074
1044
    /* Parent process */
1075
 
    close(pipefd[1]);           /* Close unused write end of pipe */
1076
 
    close(plugin_fd);
 
1045
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
 
1046
                                             pipe */
 
1047
    TEMP_FAILURE_RETRY(close(plugin_fd));
1077
1048
    plugin *new_plugin = getplugin(direntries[i]->d_name);
1078
1049
    if(new_plugin == NULL){
1079
1050
      error(0, errno, "getplugin");
1084
1055
        error(0, errno, "sigprocmask");
1085
1056
      }
1086
1057
      exitstatus = EX_OSERR;
1087
 
      free(direntries[i]);
1088
1058
      goto fallback;
1089
1059
    }
1090
 
    free(direntries[i]);
1091
1060
    
1092
1061
    new_plugin->pid = pid;
1093
1062
    new_plugin->fd = pipefd[0];
1103
1072
      goto fallback;
1104
1073
    }
1105
1074
    
1106
 
    FD_SET(new_plugin->fd, &rfds_all);
 
1075
#if defined (__GNUC__) and defined (__GLIBC__)
 
1076
#if not __GLIBC_PREREQ(2, 16)
 
1077
#pragma GCC diagnostic push
 
1078
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1079
#endif
 
1080
#endif
 
1081
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
 
1082
                                          -Wconversion in GNU libc
 
1083
                                          before 2.16 */
 
1084
#if defined (__GNUC__) and defined (__GLIBC__)
 
1085
#if not __GLIBC_PREREQ(2, 16)
 
1086
#pragma GCC diagnostic pop
 
1087
#endif
 
1088
#endif
1107
1089
    
1108
1090
    if(maxfd < new_plugin->fd){
1109
1091
      maxfd = new_plugin->fd;
1110
1092
    }
1111
1093
  }
1112
1094
  
1113
 
  free(direntries);
1114
 
  direntries = NULL;
1115
 
  close(dir_fd);
1116
 
  dir_fd = -1;
 
1095
  TEMP_FAILURE_RETRY(close(dir_fd));
1117
1096
  free_plugin(getplugin(NULL));
1118
1097
  
1119
1098
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1158
1137
                      (intmax_t) (proc->pid),
1159
1138
                      WTERMSIG(proc->status),
1160
1139
                      strsignal(WTERMSIG(proc->status)));
 
1140
            } else if(WCOREDUMP(proc->status)){
 
1141
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
 
1142
                      " core\n", proc->name, (intmax_t) (proc->pid));
1161
1143
            }
1162
1144
          }
1163
1145
          
1164
1146
          /* Remove the plugin */
1165
 
          FD_CLR(proc->fd, &rfds_all);
 
1147
#if defined (__GNUC__) and defined (__GLIBC__)
 
1148
#if not __GLIBC_PREREQ(2, 16)
 
1149
#pragma GCC diagnostic push
 
1150
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1151
#endif
 
1152
#endif
 
1153
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
 
1154
                                          -Wconversion in GNU libc
 
1155
                                          before 2.16 */
 
1156
#if defined (__GNUC__) and defined (__GLIBC__)
 
1157
#if not __GLIBC_PREREQ(2, 16)
 
1158
#pragma GCC diagnostic pop
 
1159
#endif
 
1160
#endif
1166
1161
          
1167
1162
          /* Block signal while modifying process_list */
1168
1163
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1208
1203
      }
1209
1204
      
1210
1205
      /* This process has not completed.  Does it have any output? */
1211
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
1206
#if defined (__GNUC__) and defined (__GLIBC__)
 
1207
#if not __GLIBC_PREREQ(2, 16)
 
1208
#pragma GCC diagnostic push
 
1209
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1210
#endif
 
1211
#endif
 
1212
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
 
1213
                                                         warning from
 
1214
                                                         -Wconversion
 
1215
                                                         in GNU libc
 
1216
                                                         before
 
1217
                                                         2.16 */
 
1218
#if defined (__GNUC__) and defined (__GLIBC__)
 
1219
#if not __GLIBC_PREREQ(2, 16)
 
1220
#pragma GCC diagnostic pop
 
1221
#endif
 
1222
#endif
1212
1223
        /* This process had nothing to say at this time */
1213
1224
        proc = proc->next;
1214
1225
        continue;
1281
1292
    free(custom_argv);
1282
1293
  }
1283
1294
  
1284
 
  free(direntries);
1285
 
  
1286
1295
  if(dir_fd != -1){
1287
 
    close(dir_fd);
 
1296
    TEMP_FAILURE_RETRY(close(dir_fd));
1288
1297
  }
1289
1298
  
1290
1299
  /* Kill the processes */
1310
1319
  free_plugin_list();
1311
1320
  
1312
1321
  free(plugindir);
1313
 
  free(pluginhelperdir);
1314
1322
  free(argfile);
1315
1323
  
1316
1324
  return exitstatus;