/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-03-23 20:48:58 UTC
  • Revision ID: teddy@recompile.se-20140323204858-l1x3aiick21oog4y
Improve error message when working around Debian bug #633582.

* plugins-runner.c (main): Improve error message when failing to work
                           around Debian bug #633582.  Also simplify
                           code to always use execve().

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-2014 Teddy Hogeborn
6
 
 * Copyright © 2008-2014 Björn Påhlsson
 
5
 * Copyright © 2008-2013 Teddy Hogeborn
 
6
 * Copyright © 2008-2013 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
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
26
 
                                   O_CLOEXEC, pipe2() */
 
26
                                   asprintf(), O_CLOEXEC */
27
27
#include <stddef.h>             /* size_t, NULL */
28
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_SUCCESS,
29
29
                                   realloc() */
30
30
#include <stdbool.h>            /* bool, true, false */
31
31
#include <stdio.h>              /* fileno(), fprintf(),
32
 
                                   stderr, STDOUT_FILENO, fclose() */
33
 
#include <sys/types.h>          /* fstat(), struct stat, waitpid(),
34
 
                                   WIFEXITED(), WEXITSTATUS(), wait(),
35
 
                                   pid_t, uid_t, gid_t, getuid(),
36
 
                                   getgid() */
 
32
                                   stderr, STDOUT_FILENO */
 
33
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
 
34
                                   stat, waitpid(), WIFEXITED(),
 
35
                                   WEXITSTATUS(), wait(), pid_t,
 
36
                                   uid_t, gid_t, getuid(), getgid(),
 
37
                                   dirfd() */
37
38
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
38
39
                                   FD_SET(), FD_ISSET(), FD_CLR */
39
40
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
40
41
                                   WEXITSTATUS(), WTERMSIG(),
41
42
                                   WCOREDUMP() */
42
 
#include <sys/stat.h>           /* struct stat, fstat(), S_ISREG() */
 
43
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
43
44
#include <iso646.h>             /* and, or, not */
44
 
#include <dirent.h>             /* struct dirent, scandirat() */
45
 
#include <unistd.h>             /* fcntl(), F_GETFD, F_SETFD,
46
 
                                   FD_CLOEXEC, write(), STDOUT_FILENO,
47
 
                                   struct stat, fstat(), close(),
48
 
                                   setgid(), setuid(), S_ISREG(),
49
 
                                   faccessat() pipe2(), fork(),
50
 
                                   _exit(), dup2(), fexecve(), read()
51
 
                                */
 
45
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
 
46
                                   readdir(), closedir(), dirfd() */
 
47
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
 
48
                                   fcntl(), setuid(), setgid(),
 
49
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
 
50
                                   access(), pipe(), fork(), close()
 
51
                                   dup2(), STDOUT_FILENO, _exit(),
 
52
                                   execve(), write(), read(),
 
53
                                   close() */
52
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
53
 
                                   FD_CLOEXEC, openat(), scandirat(),
54
 
                                   pipe2() */
55
 
#include <string.h>             /* strsep, strlen(), strsignal(),
56
 
                                   strcmp(), strncmp() */
 
55
                                   FD_CLOEXEC */
 
56
#include <string.h>             /* strsep, strlen(), asprintf(),
 
57
                                   strsignal(), strcmp(), strncmp() */
57
58
#include <errno.h>              /* errno */
58
59
#include <argp.h>               /* struct argp_option, struct
59
60
                                   argp_state, struct argp,
71
72
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
72
73
#include <errno.h>              /* errno */
73
74
#include <error.h>              /* error() */
74
 
#include <fnmatch.h>            /* fnmatch() */
75
75
 
76
76
#define BUFFER_SIZE 256
77
77
 
240
240
  return add_to_char_array(def, &(p->environ), &(p->envc));
241
241
}
242
242
 
243
 
#ifndef O_CLOEXEC
244
243
/*
245
244
 * Based on the example in the GNU LibC manual chapter 13.13 "File
246
245
 * Descriptor Flags".
257
256
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
258
257
                                       ret | FD_CLOEXEC));
259
258
}
260
 
#endif  /* not O_CLOEXEC */
261
259
 
262
260
 
263
261
/* Mark processes as completed when they exit, and save their exit
349
347
  char *plugindir = NULL;
350
348
  char *argfile = NULL;
351
349
  FILE *conffp;
352
 
  struct dirent **direntries = NULL;
 
350
  size_t d_name_len;
 
351
  DIR *dir = NULL;
 
352
  struct dirent *dirst;
353
353
  struct stat st;
354
354
  fd_set rfds_all;
355
355
  int ret, maxfd = 0;
363
363
                                      .sa_flags = SA_NOCLDSTOP };
364
364
  char **custom_argv = NULL;
365
365
  int custom_argc = 0;
366
 
  int dir_fd = -1;
367
366
  
368
367
  /* Establish a signal handler */
369
368
  sigemptyset(&sigchld_action.sa_mask);
780
779
       <http://bugs.debian.org/633582> */
781
780
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
782
781
    if(plugindir_fd == -1){
783
 
      if(errno != ENOENT){
784
 
        error(0, errno, "open(\"" PDIR "\")");
785
 
      }
 
782
      error(0, errno, "open(\"" PDIR "\")");
786
783
    } else {
787
784
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
788
785
      if(ret == -1){
811
808
  
812
809
  /* Open plugin directory with close_on_exec flag */
813
810
  {
814
 
    dir_fd = open(plugindir != NULL ? plugindir : PDIR, O_RDONLY |
815
 
#ifdef O_CLOEXEC
816
 
                  O_CLOEXEC
817
 
#else  /* not O_CLOEXEC */
818
 
                  0
819
 
#endif  /* not O_CLOEXEC */
820
 
                  );
 
811
    int dir_fd = -1;
 
812
    if(plugindir == NULL){
 
813
      dir_fd = open(PDIR, O_RDONLY |
 
814
#ifdef O_CLOEXEC
 
815
                    O_CLOEXEC
 
816
#else  /* not O_CLOEXEC */
 
817
                    0
 
818
#endif  /* not O_CLOEXEC */
 
819
                    );
 
820
    } else {
 
821
      dir_fd = open(plugindir, O_RDONLY |
 
822
#ifdef O_CLOEXEC
 
823
                    O_CLOEXEC
 
824
#else  /* not O_CLOEXEC */
 
825
                    0
 
826
#endif  /* not O_CLOEXEC */
 
827
                    );
 
828
    }
821
829
    if(dir_fd == -1){
822
830
      error(0, errno, "Could not open plugin dir");
823
831
      exitstatus = EX_UNAVAILABLE;
829
837
    ret = set_cloexec_flag(dir_fd);
830
838
    if(ret < 0){
831
839
      error(0, errno, "set_cloexec_flag");
 
840
      TEMP_FAILURE_RETRY(close(dir_fd));
832
841
      exitstatus = EX_OSERR;
833
842
      goto fallback;
834
843
    }
835
844
#endif  /* O_CLOEXEC */
836
 
  }
837
 
  
838
 
  int good_name(const struct dirent * const dirent){
839
 
    const char * const patterns[] = { ".*", "#*#", "*~", "*.dpkg-new",
840
 
                                      "*.dpkg-old", "*.dpkg-bak",
841
 
                                      "*.dpkg-divert", NULL };
842
 
#ifdef __GNUC__
843
 
#pragma GCC diagnostic push
844
 
#pragma GCC diagnostic ignored "-Wcast-qual"
845
 
#endif
846
 
    for(const char **pat = (const char **)patterns;
847
 
        *pat != NULL; pat++){
848
 
#ifdef __GNUC__
849
 
#pragma GCC diagnostic pop
850
 
#endif
851
 
      if(fnmatch(*pat, dirent->d_name, FNM_FILE_NAME | FNM_PERIOD)
852
 
         != FNM_NOMATCH){
853
 
        if(debug){
854
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
855
 
                    " matching pattern %s\n", dirent->d_name, *pat);
856
 
        }
857
 
        return 0;
858
 
      }
 
845
    
 
846
    dir = fdopendir(dir_fd);
 
847
    if(dir == NULL){
 
848
      error(0, errno, "Could not open plugin dir");
 
849
      TEMP_FAILURE_RETRY(close(dir_fd));
 
850
      exitstatus = EX_OSERR;
 
851
      goto fallback;
859
852
    }
860
 
    return 1;
861
 
  }
862
 
  
863
 
#ifdef __GLIBC__
864
 
#if __GLIBC_PREREQ(2, 15)
865
 
  int numplugins = scandirat(dir_fd, ".", &direntries, good_name,
866
 
                             alphasort);
867
 
#else  /* not __GLIBC_PREREQ(2, 15) */
868
 
  int numplugins = scandir(plugindir != NULL ? plugindir : PDIR,
869
 
                           &direntries, good_name, alphasort);
870
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
871
 
#else   /* not __GLIBC__ */
872
 
  int numplugins = scandir(plugindir != NULL ? plugindir : PDIR,
873
 
                           &direntries, good_name, alphasort);
874
 
#endif  /* not __GLIBC__ */
875
 
  if(numplugins == -1){
876
 
    error(0, errno, "Could not scan plugin dir");
877
 
    direntries = NULL;
878
 
    exitstatus = EX_OSERR;
879
 
    goto fallback;
880
853
  }
881
854
  
882
855
  FD_ZERO(&rfds_all);
883
856
  
884
857
  /* Read and execute any executable in the plugin directory*/
885
 
  for(int i = 0; i < numplugins; i++){
886
 
    
887
 
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
888
 
    if(plugin_fd == -1){
889
 
      error(0, errno, "Could not open plugin");
 
858
  while(true){
 
859
    do {
 
860
      dirst = readdir(dir);
 
861
    } while(dirst == NULL and errno == EINTR);
 
862
    
 
863
    /* All directory entries have been processed */
 
864
    if(dirst == NULL){
 
865
      if(errno == EBADF){
 
866
        error(0, errno, "readdir");
 
867
        exitstatus = EX_IOERR;
 
868
        goto fallback;
 
869
      }
 
870
      break;
 
871
    }
 
872
    
 
873
    d_name_len = strlen(dirst->d_name);
 
874
    
 
875
    /* Ignore dotfiles, backup files and other junk */
 
876
    {
 
877
      bool bad_name = false;
 
878
      
 
879
      const char * const bad_prefixes[] = { ".", "#", NULL };
 
880
      
 
881
      const char * const bad_suffixes[] = { "~", "#", ".dpkg-new",
 
882
                                           ".dpkg-old",
 
883
                                           ".dpkg-bak",
 
884
                                           ".dpkg-divert", NULL };
 
885
#ifdef __GNUC__
 
886
#pragma GCC diagnostic push
 
887
#pragma GCC diagnostic ignored "-Wcast-qual"
 
888
#endif
 
889
      for(const char **pre = (const char **)bad_prefixes;
 
890
          *pre != NULL; pre++){
 
891
#ifdef __GNUC__
 
892
#pragma GCC diagnostic pop
 
893
#endif
 
894
        size_t pre_len = strlen(*pre);
 
895
        if((d_name_len >= pre_len)
 
896
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
 
897
          if(debug){
 
898
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
899
                    " with bad prefix %s\n", dirst->d_name, *pre);
 
900
          }
 
901
          bad_name = true;
 
902
          break;
 
903
        }
 
904
      }
 
905
      if(bad_name){
 
906
        continue;
 
907
      }
 
908
#ifdef __GNUC__
 
909
#pragma GCC diagnostic push
 
910
#pragma GCC diagnostic ignored "-Wcast-qual"
 
911
#endif
 
912
      for(const char **suf = (const char **)bad_suffixes;
 
913
          *suf != NULL; suf++){
 
914
#ifdef __GNUC__
 
915
#pragma GCC diagnostic pop
 
916
#endif
 
917
        size_t suf_len = strlen(*suf);
 
918
        if((d_name_len >= suf_len)
 
919
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
 
920
                == 0)){
 
921
          if(debug){
 
922
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
923
                    " with bad suffix %s\n", dirst->d_name, *suf);
 
924
          }
 
925
          bad_name = true;
 
926
          break;
 
927
        }
 
928
      }
 
929
      
 
930
      if(bad_name){
 
931
        continue;
 
932
      }
 
933
    }
 
934
    
 
935
    char *filename;
 
936
    if(plugindir == NULL){
 
937
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
 
938
                                             dirst->d_name));
 
939
    } else {
 
940
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
 
941
                                             plugindir,
 
942
                                             dirst->d_name));
 
943
    }
 
944
    if(ret < 0){
 
945
      error(0, errno, "asprintf");
890
946
      continue;
891
947
    }
892
 
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
 
948
    
 
949
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
893
950
    if(ret == -1){
894
951
      error(0, errno, "stat");
895
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
952
      free(filename);
896
953
      continue;
897
954
    }
898
955
    
899
956
    /* Ignore non-executable files */
900
957
    if(not S_ISREG(st.st_mode)
901
 
       or (TEMP_FAILURE_RETRY(faccessat(dir_fd, direntries[i]->d_name,
902
 
                                        X_OK, 0)) != 0)){
 
958
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
903
959
      if(debug){
904
 
        fprintf(stderr, "Ignoring plugin dir entry \"%s/%s\""
905
 
                " with bad type or mode\n",
906
 
                plugindir != NULL ? plugindir : PDIR,
907
 
                direntries[i]->d_name);
 
960
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
961
                " with bad type or mode\n", filename);
908
962
      }
909
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
963
      free(filename);
910
964
      continue;
911
965
    }
912
966
    
913
 
    plugin *p = getplugin(direntries[i]->d_name);
 
967
    plugin *p = getplugin(dirst->d_name);
914
968
    if(p == NULL){
915
969
      error(0, errno, "getplugin");
916
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
970
      free(filename);
917
971
      continue;
918
972
    }
919
973
    if(p->disabled){
920
974
      if(debug){
921
975
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
922
 
                direntries[i]->d_name);
 
976
                dirst->d_name);
923
977
      }
924
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
978
      free(filename);
925
979
      continue;
926
980
    }
927
981
    {
941
995
        }
942
996
      }
943
997
    }
944
 
    /* If this plugin has any environment variables, we need to
945
 
       duplicate the environment from this process, too. */
 
998
    /* If this plugin has any environment variables, we will call
 
999
       using execve and need to duplicate the environment from this
 
1000
       process, too. */
946
1001
    if(p->environ[0] != NULL){
947
1002
      for(char **e = environ; *e != NULL; e++){
948
1003
        if(not add_environment(p, *e, false)){
952
1007
    }
953
1008
    
954
1009
    int pipefd[2];
955
 
#ifndef O_CLOEXEC
956
1010
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
957
 
#else  /* O_CLOEXEC */
958
 
    ret = (int)TEMP_FAILURE_RETRY(pipe2(pipefd, O_CLOEXEC));
959
 
#endif  /* O_CLOEXEC */
960
1011
    if(ret == -1){
961
1012
      error(0, errno, "pipe");
962
1013
      exitstatus = EX_OSERR;
963
1014
      goto fallback;
964
1015
    }
965
 
    if(pipefd[0] >= FD_SETSIZE){
966
 
      fprintf(stderr, "pipe()[0] (%d) >= FD_SETSIZE (%d)", pipefd[0],
967
 
              FD_SETSIZE);
968
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
969
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
970
 
      exitstatus = EX_OSERR;
971
 
      goto fallback;
972
 
    }
973
 
#ifndef O_CLOEXEC
974
1016
    /* Ask OS to automatic close the pipe on exec */
975
1017
    ret = set_cloexec_flag(pipefd[0]);
976
1018
    if(ret < 0){
977
1019
      error(0, errno, "set_cloexec_flag");
978
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
979
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
980
1020
      exitstatus = EX_OSERR;
981
1021
      goto fallback;
982
1022
    }
983
1023
    ret = set_cloexec_flag(pipefd[1]);
984
1024
    if(ret < 0){
985
1025
      error(0, errno, "set_cloexec_flag");
986
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
987
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
988
1026
      exitstatus = EX_OSERR;
989
1027
      goto fallback;
990
1028
    }
991
 
#endif  /* not O_CLOEXEC */
992
1029
    /* Block SIGCHLD until process is safely in process list */
993
1030
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
994
1031
                                              &sigchld_action.sa_mask,
1005
1042
    } while(pid == -1 and errno == EINTR);
1006
1043
    if(pid == -1){
1007
1044
      error(0, errno, "fork");
1008
 
      TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1009
 
                                     &sigchld_action.sa_mask, NULL));
1010
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
1011
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1012
1045
      exitstatus = EX_OSERR;
1013
1046
      goto fallback;
1014
1047
    }
1031
1064
        _exit(EX_OSERR);
1032
1065
      }
1033
1066
      
1034
 
      if(fexecve(plugin_fd, p->argv,
 
1067
      if(dirfd(dir) < 0){
 
1068
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
 
1069
           above and must now close it manually here. */
 
1070
        closedir(dir);
 
1071
      }
 
1072
      if(execve(filename, p->argv,
1035
1073
                (p->environ[0] != NULL) ? p->environ : environ) < 0){
1036
 
        error(0, errno, "fexecve for %s/%s",
1037
 
              plugindir != NULL ? plugindir : PDIR,
1038
 
              direntries[i]->d_name);
 
1074
        error(0, errno, "execve for %s", filename);
1039
1075
        _exit(EX_OSERR);
1040
1076
      }
1041
1077
      /* no return */
1043
1079
    /* Parent process */
1044
1080
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1045
1081
                                             pipe */
1046
 
    TEMP_FAILURE_RETRY(close(plugin_fd));
1047
 
    plugin *new_plugin = getplugin(direntries[i]->d_name);
 
1082
    free(filename);
 
1083
    plugin *new_plugin = getplugin(dirst->d_name);
1048
1084
    if(new_plugin == NULL){
1049
1085
      error(0, errno, "getplugin");
1050
1086
      ret = (int)(TEMP_FAILURE_RETRY
1091
1127
    }
1092
1128
  }
1093
1129
  
1094
 
  free(direntries);
1095
 
  direntries = NULL;
1096
 
  TEMP_FAILURE_RETRY(close(dir_fd));
1097
 
  dir_fd = -1;
 
1130
  TEMP_FAILURE_RETRY(closedir(dir));
 
1131
  dir = NULL;
1098
1132
  free_plugin(getplugin(NULL));
1099
1133
  
1100
1134
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1294
1328
    free(custom_argv);
1295
1329
  }
1296
1330
  
1297
 
  free(direntries);
1298
 
  
1299
 
  if(dir_fd != -1){
1300
 
    TEMP_FAILURE_RETRY(close(dir_fd));
 
1331
  if(dir != NULL){
 
1332
    closedir(dir);
1301
1333
  }
1302
1334
  
1303
1335
  /* Kill the processes */