/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 plugins.d/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2016-03-17 20:40:55 UTC
  • mto: (237.7.594 trunk)
  • mto: This revision was merged to the branch mainline in revision 341.
  • Revision ID: teddy@recompile.se-20160317204055-bhsh5xsidq7w5cxu
Client: Fix plymouth agent; broken since 1.7.2.

Fix an very old memory bug in the plymouth agent (which has been
present since its apperance in version 1.2), but which was only
recently detected at run time due to the new -fsanitize=address
compile- time flag, which has been used since version 1.7.2.  This
detection of a memory access violation causes the program to abort,
making the Plymouth graphical boot system unable to accept interactive
input of passwords when using the Mandos client.

* plugins.d/plymouth.c (exec_and_wait): Fix memory allocation bug when
  allocating new_argv.  Also tolerate a zero-length argv.

Show diffs side-by-side

added added

removed removed

Lines of Context:
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
58
                                   inet_pton(), connect(),
59
59
                                   getnameinfo() */
60
 
#include <fcntl.h>              /* open(), unlinkat() */
 
60
#include <fcntl.h>              /* open(), unlinkat(), AT_REMOVEDIR */
61
61
#include <dirent.h>             /* opendir(), struct dirent, readdir()
62
62
                                 */
63
63
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
64
64
                                   strtoimax() */
65
 
#include <errno.h>              /* perror(), errno,
 
65
#include <errno.h>              /* perror(), errno, EINTR, EINVAL,
 
66
                                   EAI_SYSTEM, ENETUNREACH,
 
67
                                   EHOSTUNREACH, ECONNREFUSED, EPROTO,
 
68
                                   EIO, ENOENT, ENXIO, ENOMEM, EISDIR,
 
69
                                   ENOTEMPTY,
66
70
                                   program_invocation_short_name */
67
71
#include <time.h>               /* nanosleep(), time(), sleep() */
68
72
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
513
517
  fprintf_plus(stderr, "GnuTLS: %s", string);
514
518
}
515
519
 
516
 
__attribute__((nonnull, warn_unused_result))
 
520
__attribute__((nonnull(1, 2, 4), warn_unused_result))
517
521
static int init_gnutls_global(const char *pubkeyfilename,
518
522
                              const char *seckeyfilename,
519
523
                              const char *dhparamsfilename,
525
529
    fprintf_plus(stderr, "Initializing GnuTLS\n");
526
530
  }
527
531
  
528
 
  ret = gnutls_global_init();
529
 
  if(ret != GNUTLS_E_SUCCESS){
530
 
    fprintf_plus(stderr, "GnuTLS global_init: %s\n",
531
 
                 safer_gnutls_strerror(ret));
532
 
    return -1;
533
 
  }
534
 
  
535
532
  if(debug){
536
533
    /* "Use a log level over 10 to enable all debugging options."
537
534
     * - GnuTLS manual
545
542
  if(ret != GNUTLS_E_SUCCESS){
546
543
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
547
544
                 safer_gnutls_strerror(ret));
548
 
    gnutls_global_deinit();
549
545
    return -1;
550
546
  }
551
547
  
755
751
 globalfail:
756
752
  
757
753
  gnutls_certificate_free_credentials(mc->cred);
758
 
  gnutls_global_deinit();
759
754
  gnutls_dh_params_deinit(mc->dh_params);
760
755
  return -1;
761
756
}
822
817
 
823
818
/* Set effective uid to 0, return errno */
824
819
__attribute__((warn_unused_result))
825
 
error_t raise_privileges(void){
826
 
  error_t old_errno = errno;
827
 
  error_t ret_errno = 0;
 
820
int raise_privileges(void){
 
821
  int old_errno = errno;
 
822
  int ret = 0;
828
823
  if(seteuid(0) == -1){
829
 
    ret_errno = errno;
 
824
    ret = errno;
830
825
  }
831
826
  errno = old_errno;
832
 
  return ret_errno;
 
827
  return ret;
833
828
}
834
829
 
835
830
/* Set effective and real user ID to 0.  Return errno. */
836
831
__attribute__((warn_unused_result))
837
 
error_t raise_privileges_permanently(void){
838
 
  error_t old_errno = errno;
839
 
  error_t ret_errno = raise_privileges();
840
 
  if(ret_errno != 0){
 
832
int raise_privileges_permanently(void){
 
833
  int old_errno = errno;
 
834
  int ret = raise_privileges();
 
835
  if(ret != 0){
841
836
    errno = old_errno;
842
 
    return ret_errno;
 
837
    return ret;
843
838
  }
844
839
  if(setuid(0) == -1){
845
 
    ret_errno = errno;
 
840
    ret = errno;
846
841
  }
847
842
  errno = old_errno;
848
 
  return ret_errno;
 
843
  return ret;
849
844
}
850
845
 
851
846
/* Set effective user ID to unprivileged saved user ID */
852
847
__attribute__((warn_unused_result))
853
 
error_t lower_privileges(void){
854
 
  error_t old_errno = errno;
855
 
  error_t ret_errno = 0;
 
848
int lower_privileges(void){
 
849
  int old_errno = errno;
 
850
  int ret = 0;
856
851
  if(seteuid(uid) == -1){
857
 
    ret_errno = errno;
 
852
    ret = errno;
858
853
  }
859
854
  errno = old_errno;
860
 
  return ret_errno;
 
855
  return ret;
861
856
}
862
857
 
863
858
/* Lower privileges permanently */
864
859
__attribute__((warn_unused_result))
865
 
error_t lower_privileges_permanently(void){
866
 
  error_t old_errno = errno;
867
 
  error_t ret_errno = 0;
 
860
int lower_privileges_permanently(void){
 
861
  int old_errno = errno;
 
862
  int ret = 0;
868
863
  if(setuid(uid) == -1){
869
 
    ret_errno = errno;
 
864
    ret = errno;
870
865
  }
871
866
  errno = old_errno;
872
 
  return ret_errno;
 
867
  return ret;
873
868
}
874
869
 
875
870
/* Helper function to add_local_route() and delete_local_route() */
1628
1623
__attribute__((nonnull, warn_unused_result))
1629
1624
bool get_flags(const char *ifname, struct ifreq *ifr){
1630
1625
  int ret;
1631
 
  error_t ret_errno;
 
1626
  int old_errno;
1632
1627
  
1633
1628
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1634
1629
  if(s < 0){
1635
 
    ret_errno = errno;
 
1630
    old_errno = errno;
1636
1631
    perror_plus("socket");
1637
 
    errno = ret_errno;
 
1632
    errno = old_errno;
1638
1633
    return false;
1639
1634
  }
1640
1635
  strncpy(ifr->ifr_name, ifname, IF_NAMESIZE);
1642
1637
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1643
1638
  if(ret == -1){
1644
1639
    if(debug){
1645
 
      ret_errno = errno;
 
1640
      old_errno = errno;
1646
1641
      perror_plus("ioctl SIOCGIFFLAGS");
1647
 
      errno = ret_errno;
 
1642
      errno = old_errno;
1648
1643
    }
1649
1644
    return false;
1650
1645
  }
2076
2071
}
2077
2072
 
2078
2073
__attribute__((nonnull, warn_unused_result))
2079
 
error_t bring_up_interface(const char *const interface,
2080
 
                           const float delay){
2081
 
  error_t old_errno = errno;
 
2074
int bring_up_interface(const char *const interface,
 
2075
                       const float delay){
 
2076
  int old_errno = errno;
2082
2077
  int ret;
2083
2078
  struct ifreq network;
2084
2079
  unsigned int if_index = if_nametoindex(interface);
2094
2089
  }
2095
2090
  
2096
2091
  if(not interface_is_up(interface)){
2097
 
    error_t ret_errno = 0, ioctl_errno = 0;
 
2092
    int ret_errno = 0;
 
2093
    int ioctl_errno = 0;
2098
2094
    if(not get_flags(interface, &network)){
2099
2095
      ret_errno = errno;
2100
2096
      fprintf_plus(stderr, "Failed to get flags for interface "
2203
2199
}
2204
2200
 
2205
2201
__attribute__((nonnull, warn_unused_result))
2206
 
error_t take_down_interface(const char *const interface){
2207
 
  error_t old_errno = errno;
 
2202
int take_down_interface(const char *const interface){
 
2203
  int old_errno = errno;
2208
2204
  struct ifreq network;
2209
2205
  unsigned int if_index = if_nametoindex(interface);
2210
2206
  if(if_index == 0){
2213
2209
    return ENXIO;
2214
2210
  }
2215
2211
  if(interface_is_up(interface)){
2216
 
    error_t ret_errno = 0, ioctl_errno = 0;
 
2212
    int ret_errno = 0;
 
2213
    int ioctl_errno = 0;
2217
2214
    if(not get_flags(interface, &network) and debug){
2218
2215
      ret_errno = errno;
2219
2216
      fprintf_plus(stderr, "Failed to get flags for interface "
2469
2466
                         .args_doc = "",
2470
2467
                         .doc = "Mandos client -- Get and decrypt"
2471
2468
                         " passwords from a Mandos server" };
2472
 
    ret = argp_parse(&argp, argc, argv,
2473
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
2474
 
    switch(ret){
 
2469
    ret_errno = argp_parse(&argp, argc, argv,
 
2470
                           ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
 
2471
    switch(ret_errno){
2475
2472
    case 0:
2476
2473
      break;
2477
2474
    case ENOMEM:
2478
2475
    default:
2479
 
      errno = ret;
 
2476
      errno = ret_errno;
2480
2477
      perror_plus("argp_parse");
2481
2478
      exitcode = EX_OSERR;
2482
2479
      goto end;
2491
2488
       <http://bugs.debian.org/633582> */
2492
2489
    
2493
2490
    /* Re-raise privileges */
2494
 
    ret_errno = raise_privileges();
2495
 
    if(ret_errno != 0){
2496
 
      errno = ret_errno;
 
2491
    ret = raise_privileges();
 
2492
    if(ret != 0){
 
2493
      errno = ret;
2497
2494
      perror_plus("Failed to raise privileges");
2498
2495
    } else {
2499
2496
      struct stat st;
2563
2560
      }
2564
2561
      
2565
2562
      /* Lower privileges */
2566
 
      ret_errno = lower_privileges();
2567
 
      if(ret_errno != 0){
2568
 
        errno = ret_errno;
 
2563
      ret = lower_privileges();
 
2564
      if(ret != 0){
 
2565
        errno = ret;
2569
2566
        perror_plus("Failed to lower privileges");
2570
2567
      }
2571
2568
    }
2899
2896
    
2900
2897
    /* Allocate a new server */
2901
2898
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2902
 
                                 &config, NULL, NULL, &ret_errno);
 
2899
                                 &config, NULL, NULL, &ret);
2903
2900
    
2904
2901
    /* Free the Avahi configuration data */
2905
2902
    avahi_server_config_free(&config);
2908
2905
  /* Check if creating the Avahi server object succeeded */
2909
2906
  if(mc.server == NULL){
2910
2907
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2911
 
                 avahi_strerror(ret_errno));
 
2908
                 avahi_strerror(ret));
2912
2909
    exitcode = EX_UNAVAILABLE;
2913
2910
    goto end;
2914
2911
  }
2966
2963
  
2967
2964
  if(gnutls_initialized){
2968
2965
    gnutls_certificate_free_credentials(mc.cred);
2969
 
    gnutls_global_deinit();
2970
2966
    gnutls_dh_params_deinit(mc.dh_params);
2971
2967
  }
2972
2968
  
2995
2991
  
2996
2992
  /* Re-raise privileges */
2997
2993
  {
2998
 
    ret_errno = raise_privileges();
2999
 
    if(ret_errno != 0){
3000
 
      errno = ret_errno;
 
2994
    ret = raise_privileges();
 
2995
    if(ret != 0){
 
2996
      errno = ret;
3001
2997
      perror_plus("Failed to raise privileges");
3002
2998
    } else {
3003
2999
      
3011
3007
        while((interface=argz_next(interfaces_to_take_down,
3012
3008
                                   interfaces_to_take_down_size,
3013
3009
                                   interface))){
3014
 
          ret_errno = take_down_interface(interface);
3015
 
          if(ret_errno != 0){
3016
 
            errno = ret_errno;
 
3010
          ret = take_down_interface(interface);
 
3011
          if(ret != 0){
 
3012
            errno = ret;
3017
3013
            perror_plus("Failed to take down interface");
3018
3014
          }
3019
3015
        }
3024
3020
      }
3025
3021
    }
3026
3022
    
3027
 
    ret_errno = lower_privileges_permanently();
3028
 
    if(ret_errno != 0){
3029
 
      errno = ret_errno;
 
3023
    ret = lower_privileges_permanently();
 
3024
    if(ret != 0){
 
3025
      errno = ret;
3030
3026
      perror_plus("Failed to lower privileges permanently");
3031
3027
    }
3032
3028
  }
3034
3030
  free(interfaces_to_take_down);
3035
3031
  free(interfaces_hooks);
3036
3032
  
 
3033
  void clean_dir_at(int base, const char * const dirname,
 
3034
                    uintmax_t level){
 
3035
    struct dirent **direntries = NULL;
 
3036
    int dret;
 
3037
    int dir_fd = (int)TEMP_FAILURE_RETRY(openat(base, dirname,
 
3038
                                                O_RDONLY
 
3039
                                                | O_NOFOLLOW
 
3040
                                                | O_DIRECTORY
 
3041
                                                | O_PATH));
 
3042
    if(dir_fd == -1){
 
3043
      perror_plus("open");
 
3044
    }
 
3045
    int numentries = scandirat(dir_fd, ".", &direntries,
 
3046
                               notdotentries, alphasort);
 
3047
    if(numentries >= 0){
 
3048
      for(int i = 0; i < numentries; i++){
 
3049
        if(debug){
 
3050
          fprintf_plus(stderr, "Unlinking \"%s/%s\"\n",
 
3051
                       dirname, direntries[i]->d_name);
 
3052
        }
 
3053
        dret = unlinkat(dir_fd, direntries[i]->d_name, 0);
 
3054
        if(dret == -1){
 
3055
          if(errno == EISDIR){
 
3056
              dret = unlinkat(dir_fd, direntries[i]->d_name,
 
3057
                              AT_REMOVEDIR);
 
3058
          }         
 
3059
          if((dret == -1) and (errno == ENOTEMPTY)
 
3060
             and (strcmp(direntries[i]->d_name, "private-keys-v1.d")
 
3061
                  == 0) and (level == 0)){
 
3062
            /* Recurse only in this special case */
 
3063
            clean_dir_at(dir_fd, direntries[i]->d_name, level+1);
 
3064
            dret = 0;
 
3065
          }
 
3066
          if(dret == -1){
 
3067
            fprintf_plus(stderr, "unlink(\"%s/%s\"): %s\n", dirname,
 
3068
                         direntries[i]->d_name, strerror(errno));
 
3069
          }
 
3070
        }
 
3071
        free(direntries[i]);
 
3072
      }
 
3073
      
 
3074
      /* need to clean even if 0 because man page doesn't specify */
 
3075
      free(direntries);
 
3076
      if(numentries == -1){
 
3077
        perror_plus("scandirat");
 
3078
      }
 
3079
      dret = unlinkat(base, dirname, AT_REMOVEDIR);
 
3080
      if(dret == -1 and errno != ENOENT){
 
3081
        perror_plus("rmdir");
 
3082
      }
 
3083
    } else {
 
3084
      perror_plus("scandirat");
 
3085
    }
 
3086
    close(dir_fd);
 
3087
  }
 
3088
  
3037
3089
  /* Removes the GPGME temp directory and all files inside */
3038
3090
  if(tempdir != NULL){
3039
 
    struct dirent **direntries = NULL;
3040
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
3041
 
                                                  | O_NOFOLLOW
3042
 
                                                  | O_DIRECTORY
3043
 
                                                  | O_PATH));
3044
 
    if(tempdir_fd == -1){
3045
 
      perror_plus("open");
3046
 
    } else {
3047
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
3048
 
                                 notdotentries, alphasort);
3049
 
      if(numentries >= 0){
3050
 
        for(int i = 0; i < numentries; i++){
3051
 
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
3052
 
          if(ret == -1){
3053
 
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
3054
 
                         " \"%s\", 0): %s\n", tempdir,
3055
 
                         direntries[i]->d_name, strerror(errno));
3056
 
          }
3057
 
          free(direntries[i]);
3058
 
        }
3059
 
        
3060
 
        /* need to clean even if 0 because man page doesn't specify */
3061
 
        free(direntries);
3062
 
        if(numentries == -1){
3063
 
          perror_plus("scandir");
3064
 
        }
3065
 
        ret = rmdir(tempdir);
3066
 
        if(ret == -1 and errno != ENOENT){
3067
 
          perror_plus("rmdir");
3068
 
        }
3069
 
      }
3070
 
      close(tempdir_fd);
3071
 
    }
 
3091
    clean_dir_at(-1, tempdir, 0);
3072
3092
  }
3073
3093
  
3074
3094
  if(quit_now){