/mandos/release

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

« back to all changes in this revision

Viewing changes to plugin-runner.c

  • Committer: Teddy Hogeborn
  • Date: 2009-05-17 00:50:09 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090517005009-c0iptxampo6nf177
* initramfs-tools-hook-conf: Security bug fix: Add code to handle
                             being called by "mkinitramfs-kpkg"
                             instead of "update-initramfs".

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
#include <signal.h>             /* struct sigaction, sigemptyset(),
63
63
                                   sigaddset(), sigaction(),
64
64
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
 
                                   SIG_UNBLOCK, kill() */
 
65
                                   SIG_UNBLOCK, kill(), sig_atomic_t
 
66
                                */
66
67
#include <errno.h>              /* errno, EBADF */
 
68
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
67
69
 
68
70
#define BUFFER_SIZE 256
69
71
 
80
82
  char **environ;
81
83
  int envc;
82
84
  bool disabled;
83
 
 
 
85
  
84
86
  /* Variables used for running processes*/
85
87
  pid_t pid;
86
88
  int fd;
88
90
  size_t buffer_size;
89
91
  size_t buffer_length;
90
92
  bool eof;
91
 
  volatile bool completed;
92
 
  volatile int status;
 
93
  volatile sig_atomic_t completed;
 
94
  int status;
93
95
  struct plugin *next;
94
96
} plugin;
95
97
 
114
116
  if(name != NULL){
115
117
    copy_name = strdup(name);
116
118
    if(copy_name == NULL){
 
119
      free(new_plugin);
117
120
      return NULL;
118
121
    }
119
122
  }
120
123
  
121
 
  *new_plugin = (plugin) { .name = copy_name,
122
 
                           .argc = 1,
123
 
                           .disabled = false,
124
 
                           .next = plugin_list };
 
124
  *new_plugin = (plugin){ .name = copy_name,
 
125
                          .argc = 1,
 
126
                          .disabled = false,
 
127
                          .next = plugin_list };
125
128
  
126
129
  new_plugin->argv = malloc(sizeof(char *) * 2);
127
130
  if(new_plugin->argv == NULL){
221
224
/* Mark processes as completed when they exit, and save their exit
222
225
   status. */
223
226
static void handle_sigchld(__attribute__((unused)) int sig){
 
227
  int old_errno = errno;
224
228
  while(true){
225
229
    plugin *proc = plugin_list;
226
230
    int status;
230
234
      break;
231
235
    }
232
236
    if(pid == -1){
233
 
      if(errno != ECHILD){
234
 
        perror("waitpid");
 
237
      if(errno == ECHILD){
 
238
        /* No child processes */
 
239
        break;
235
240
      }
236
 
      /* No child processes */
237
 
      break;
 
241
      perror("waitpid");
238
242
    }
239
243
    
240
244
    /* A child exited, find it in process_list */
246
250
      continue;
247
251
    }
248
252
    proc->status = status;
249
 
    proc->completed = true;
 
253
    proc->completed = 1;
250
254
  }
 
255
  errno = old_errno;
251
256
}
252
257
 
253
258
/* Prints out a password to stdout */
275
280
  }
276
281
  free(plugin_node->environ);
277
282
  free(plugin_node->buffer);
278
 
 
 
283
  
279
284
  /* Removes the plugin from the singly-linked list */
280
285
  if(plugin_node == plugin_list){
281
286
    /* First one - simple */
310
315
  fd_set rfds_all;
311
316
  int ret, maxfd = 0;
312
317
  ssize_t sret;
 
318
  intmax_t tmpmax;
313
319
  uid_t uid = 65534;
314
320
  gid_t gid = 65534;
315
321
  bool debug = false;
373
379
  };
374
380
  
375
381
  error_t parse_opt(int key, char *arg, __attribute__((unused))
376
 
                    struct argp_state *state) {
377
 
    switch(key) {
 
382
                    struct argp_state *state){
 
383
    char *tmp;
 
384
    switch(key){
378
385
    case 'g':                   /* --global-options */
379
386
      if(arg != NULL){
380
 
        char *p;
381
 
        while((p = strsep(&arg, ",")) != NULL){
382
 
          if(p[0] == '\0'){
 
387
        char *plugin_option;
 
388
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
389
          if(plugin_option[0] == '\0'){
383
390
            continue;
384
391
          }
385
 
          if(not add_argument(getplugin(NULL), p)){
 
392
          if(not add_argument(getplugin(NULL), plugin_option)){
386
393
            perror("add_argument");
387
394
            return ARGP_ERR_UNKNOWN;
388
395
          }
399
406
      break;
400
407
    case 'o':                   /* --options-for */
401
408
      if(arg != NULL){
402
 
        char *p_name = strsep(&arg, ":");
403
 
        if(p_name[0] == '\0' or arg == NULL){
404
 
          break;
405
 
        }
406
 
        char *opt = strsep(&arg, ":");
407
 
        if(opt[0] == '\0' or opt == NULL){
408
 
          break;
409
 
        }
410
 
        char *p;
411
 
        while((p = strsep(&opt, ",")) != NULL){
412
 
          if(p[0] == '\0'){
413
 
            continue;
414
 
          }
415
 
          if(not add_argument(getplugin(p_name), p)){
 
409
        char *plugin_name = strsep(&arg, ":");
 
410
        if(plugin_name[0] == '\0'){
 
411
          break;
 
412
        }
 
413
        char *plugin_option;
 
414
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
415
          if(not add_argument(getplugin(plugin_name), plugin_option)){
416
416
            perror("add_argument");
417
417
            return ARGP_ERR_UNKNOWN;
418
418
          }
463
463
      /* This is already done by parse_opt_config_file() */
464
464
      break;
465
465
    case 130:                   /* --userid */
466
 
      /* In the GNU C library, uid_t is always unsigned int */
467
 
      ret = sscanf(arg, "%u", &uid);
468
 
      if(ret != 1){
469
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %u\n", arg,
470
 
                uid);
 
466
      errno = 0;
 
467
      tmpmax = strtoimax(arg, &tmp, 10);
 
468
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
469
         or tmpmax != (uid_t)tmpmax){
 
470
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
 
471
                PRIdMAX "\n", arg, (intmax_t)uid);
 
472
      } else {
 
473
        uid = (uid_t)tmpmax;
471
474
      }
472
475
      break;
473
476
    case 131:                   /* --groupid */
474
 
      /* In the GNU C library, gid_t is always unsigned int */
475
 
      ret = sscanf(arg, "%u", &gid);
476
 
      if(ret != 1){
477
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %u\n",
478
 
                arg, gid);
 
477
      errno = 0;
 
478
      tmpmax = strtoimax(arg, &tmp, 10);
 
479
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
480
         or tmpmax != (gid_t)tmpmax){
 
481
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
 
482
                PRIdMAX "\n", arg, (intmax_t)gid);
 
483
      } else {
 
484
        gid = (gid_t)tmpmax;
479
485
      }
480
486
      break;
481
487
    case 132:                   /* --debug */
505
511
     ignores everything but the --config-file option. */
506
512
  error_t parse_opt_config_file(int key, char *arg,
507
513
                                __attribute__((unused))
508
 
                                struct argp_state *state) {
509
 
    switch(key) {
 
514
                                struct argp_state *state){
 
515
    switch(key){
510
516
    case 'g':                   /* --global-options */
511
517
    case 'G':                   /* --global-env */
512
518
    case 'o':                   /* --options-for */
563
569
    size_t size = 0;
564
570
    const char whitespace_delims[] = " \r\t\f\v\n";
565
571
    const char comment_delim[] = "#";
566
 
 
 
572
    
567
573
    custom_argc = 1;
568
574
    custom_argv = malloc(sizeof(char*) * 2);
569
575
    if(custom_argv == NULL){
573
579
    }
574
580
    custom_argv[0] = argv[0];
575
581
    custom_argv[1] = NULL;
576
 
 
 
582
    
577
583
    /* for each line in the config file, strip whitespace and ignore
578
584
       commented text */
579
585
    while(true){
581
587
      if(sret == -1){
582
588
        break;
583
589
      }
584
 
 
 
590
      
585
591
      line = org_line;
586
592
      arg = strsep(&line, comment_delim);
587
593
      while((p = strsep(&arg, whitespace_delims)) != NULL){
647
653
      for(char **a = p->argv; *a != NULL; a++){
648
654
        fprintf(stderr, "\tArg: %s\n", *a);
649
655
      }
650
 
      fprintf(stderr, "...and %u environment variables\n", p->envc);
 
656
      fprintf(stderr, "...and %d environment variables\n", p->envc);
651
657
      for(char **a = p->environ; *a != NULL; a++){
652
658
        fprintf(stderr, "\t%s\n", *a);
653
659
      }
655
661
  }
656
662
  
657
663
  /* Strip permissions down to nobody */
 
664
  setgid(gid);
 
665
  if(ret == -1){
 
666
    perror("setgid");
 
667
  }
658
668
  ret = setuid(uid);
659
669
  if(ret == -1){
660
670
    perror("setuid");
661
 
  }  
662
 
  setgid(gid);
663
 
  if(ret == -1){
664
 
    perror("setgid");
665
671
  }
666
672
  
667
673
  if(plugindir == NULL){
750
756
        continue;
751
757
      }
752
758
    }
753
 
 
 
759
    
754
760
    char *filename;
755
761
    if(plugindir == NULL){
756
762
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
768
774
      free(filename);
769
775
      continue;
770
776
    }
771
 
 
 
777
    
772
778
    /* Ignore non-executable files */
773
779
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
774
780
      if(debug){
927
933
  
928
934
  closedir(dir);
929
935
  dir = NULL;
 
936
  free_plugin(getplugin(NULL));
930
937
  
931
938
  for(plugin *p = plugin_list; p != NULL; p = p->next){
932
939
    if(p->pid != 0){
952
959
       from one of them */
953
960
    for(plugin *proc = plugin_list; proc != NULL;){
954
961
      /* Is this process completely done? */
955
 
      if(proc->eof and proc->completed){
 
962
      if(proc->completed and proc->eof){
956
963
        /* Only accept the plugin output if it exited cleanly */
957
964
        if(not WIFEXITED(proc->status)
958
965
           or WEXITSTATUS(proc->status) != 0){
959
966
          /* Bad exit by plugin */
960
 
 
 
967
          
961
968
          if(debug){
962
969
            if(WIFEXITED(proc->status)){
963
 
              fprintf(stderr, "Plugin %u exited with status %d\n",
964
 
                      (unsigned int) (proc->pid),
 
970
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
 
971
                      " status %d\n", proc->name,
 
972
                      (intmax_t) (proc->pid),
965
973
                      WEXITSTATUS(proc->status));
966
 
            } else if(WIFSIGNALED(proc->status)) {
967
 
              fprintf(stderr, "Plugin %u killed by signal %d\n",
968
 
                      (unsigned int) (proc->pid),
 
974
            } else if(WIFSIGNALED(proc->status)){
 
975
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
 
976
                      " signal %d\n", proc->name,
 
977
                      (intmax_t) (proc->pid),
969
978
                      WTERMSIG(proc->status));
970
979
            } else if(WCOREDUMP(proc->status)){
971
 
              fprintf(stderr, "Plugin %u dumped core\n",
972
 
                      (unsigned int) (proc->pid));
 
980
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
 
981
                      " core\n", proc->name, (intmax_t) (proc->pid));
973
982
            }
974
983
          }
975
984
          
976
985
          /* Remove the plugin */
977
986
          FD_CLR(proc->fd, &rfds_all);
978
 
 
 
987
          
979
988
          /* Block signal while modifying process_list */
980
989
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
981
990
          if(ret < 0){
1048
1057
      }
1049
1058
    }
1050
1059
  }
1051
 
 
1052
 
 
 
1060
  
 
1061
  
1053
1062
 fallback:
1054
1063
  
1055
1064
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){