/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: 2014-06-08 03:10:08 UTC
  • mto: (237.7.272 trunk)
  • mto: This revision was merged to the branch mainline in revision 317.
  • Revision ID: teddy@recompile.se-20140608031008-mc9bd7b024a3q0y0
Address a very theoretical possible security issue in mandos-client.

If there were to run some sort of "cleaner" process for /run/tmp (or
/tmp), and mandos-client were to run for long enough for that cleaner
process to remove the temporary directory for GPGME, there was a
possibility that another unprivileged process could trick the (also
unprivileged) mandos-client process to remove other files or symlinks
which the unprivileged mandos-client process was allowed to remove.
This is not currently known to have been exploitable, since there are
no known initramfs environments running such cleaner processes.

* plugins.d/mandos-client.c (main): Use O_NOFOLLOW when opening
                                    tempdir for cleaning.

Show diffs side-by-side

added added

removed removed

Lines of Context:
234
234
                          .af = af };
235
235
  if(new_server->ip == NULL){
236
236
    perror_plus("strdup");
237
 
    free(new_server);
238
237
    return false;
239
238
  }
240
239
  ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
241
240
  if(ret == -1){
242
241
    perror_plus("clock_gettime");
243
 
#ifdef __GNUC__
244
 
#pragma GCC diagnostic push
245
 
#pragma GCC diagnostic ignored "-Wcast-qual"
246
 
#endif
247
 
    free((char *)(new_server->ip));
248
 
#ifdef __GNUC__
249
 
#pragma GCC diagnostic pop
250
 
#endif
251
 
    free(new_server);
252
242
    return false;
253
243
  }
254
244
  /* Special case of first server */
493
483
  return plaintext_length;
494
484
}
495
485
 
496
 
__attribute__((warn_unused_result, const))
497
 
static const char *safe_string(const char *str){
498
 
  if(str == NULL)
499
 
    return "(unknown)";
500
 
  return str;
501
 
}
502
 
 
503
486
__attribute__((warn_unused_result))
504
487
static const char *safer_gnutls_strerror(int value){
505
488
  const char *ret = gnutls_strerror(value);
506
 
  return safe_string(ret);
 
489
  if(ret == NULL)
 
490
    ret = "(unknown)";
 
491
  return ret;
507
492
}
508
493
 
509
494
/* GnuTLS log function callback */
518
503
                              const char *seckeyfilename,
519
504
                              mandos_context *mc){
520
505
  int ret;
521
 
  unsigned int uret;
522
506
  
523
507
  if(debug){
524
508
    fprintf_plus(stderr, "Initializing GnuTLS\n");
575
559
                 safer_gnutls_strerror(ret));
576
560
    goto globalfail;
577
561
  }
578
 
  if(mc->dh_bits == 0){
579
 
    /* Find out the optimal number of DH bits */
580
 
    /* Try to read the private key file */
581
 
    gnutls_datum_t buffer = { .data = NULL, .size = 0 };
582
 
    {
583
 
      int secfile = open(seckeyfilename, O_RDONLY);
584
 
      size_t buffer_capacity = 0;
585
 
      while(true){
586
 
        buffer_capacity = incbuffer((char **)&buffer.data,
587
 
                                    (size_t)buffer.size,
588
 
                                    (size_t)buffer_capacity);
589
 
        if(buffer_capacity == 0){
590
 
          perror_plus("incbuffer");
591
 
          free(buffer.data);
592
 
          buffer.data = NULL;
593
 
          break;
594
 
        }
595
 
        ssize_t bytes_read = read(secfile, buffer.data + buffer.size,
596
 
                                  BUFFER_SIZE);
597
 
        /* EOF */
598
 
        if(bytes_read == 0){
599
 
          break;
600
 
        }
601
 
        /* check bytes_read for failure */
602
 
        if(bytes_read < 0){
603
 
          perror_plus("read");
604
 
          free(buffer.data);
605
 
          buffer.data = NULL;
606
 
          break;
607
 
        }
608
 
        buffer.size += (unsigned int)bytes_read;
609
 
      }
610
 
      close(secfile);
611
 
    }
612
 
    /* If successful, use buffer to parse private key */
613
 
    gnutls_sec_param_t sec_param = GNUTLS_SEC_PARAM_ULTRA;
614
 
    if(buffer.data != NULL){
615
 
      {
616
 
        gnutls_openpgp_privkey_t privkey = NULL;
617
 
        ret = gnutls_openpgp_privkey_init(&privkey);
618
 
        if(ret != GNUTLS_E_SUCCESS){
619
 
          fprintf_plus(stderr, "Error initializing OpenPGP key"
620
 
                       " structure: %s", safer_gnutls_strerror(ret));
621
 
          free(buffer.data);
622
 
          buffer.data = NULL;
623
 
        } else {
624
 
          ret = gnutls_openpgp_privkey_import(privkey, &buffer,
625
 
                                            GNUTLS_OPENPGP_FMT_BASE64,
626
 
                                              "", 0);
627
 
          if(ret != GNUTLS_E_SUCCESS){
628
 
            fprintf_plus(stderr, "Error importing OpenPGP key : %s",
629
 
                         safer_gnutls_strerror(ret));
630
 
            privkey = NULL;
631
 
          }
632
 
          free(buffer.data);
633
 
          buffer.data = NULL;
634
 
          if(privkey != NULL){
635
 
            /* Use private key to suggest an appropriate sec_param */
636
 
            sec_param = gnutls_openpgp_privkey_sec_param(privkey);
637
 
            gnutls_openpgp_privkey_deinit(privkey);
638
 
            if(debug){
639
 
              fprintf_plus(stderr, "This OpenPGP key implies using a"
640
 
                           " GnuTLS security parameter \"%s\".\n",
641
 
                           safe_string(gnutls_sec_param_get_name
642
 
                                       (sec_param)));
643
 
            }
644
 
          }
645
 
        }
646
 
      }
647
 
      if(sec_param == GNUTLS_SEC_PARAM_UNKNOWN){
648
 
        /* Err on the side of caution */
649
 
        sec_param = GNUTLS_SEC_PARAM_ULTRA;
650
 
        if(debug){
651
 
          fprintf_plus(stderr, "Falling back to security parameter"
652
 
                       " \"%s\"\n",
653
 
                       safe_string(gnutls_sec_param_get_name
654
 
                                   (sec_param)));
655
 
        }
656
 
      }
657
 
    }
658
 
    uret = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, sec_param);
659
 
    if(uret != 0){
660
 
      mc->dh_bits = uret;
661
 
      if(debug){
662
 
        fprintf_plus(stderr, "A \"%s\" GnuTLS security parameter"
663
 
                     " implies %u DH bits; using that.\n",
664
 
                     safe_string(gnutls_sec_param_get_name
665
 
                                 (sec_param)),
666
 
                     mc->dh_bits);
667
 
      }
668
 
    } else {
669
 
      fprintf_plus(stderr, "Failed to get implied number of DH"
670
 
                   " bits for security parameter \"%s\"): %s\n",
671
 
                   safe_string(gnutls_sec_param_get_name(sec_param)),
672
 
                   safer_gnutls_strerror(ret));
673
 
      goto globalfail;
674
 
    }
675
 
  } else if(debug){
676
 
    fprintf_plus(stderr, "DH bits explicitly set to %u\n",
677
 
                 mc->dh_bits);
678
 
  }
679
562
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
680
563
  if(ret != GNUTLS_E_SUCCESS){
681
 
    fprintf_plus(stderr, "Error in GnuTLS prime generation (%u bits):"
682
 
                 " %s\n", mc->dh_bits, safer_gnutls_strerror(ret));
 
564
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
 
565
                 safer_gnutls_strerror(ret));
683
566
    goto globalfail;
684
567
  }
685
568
  
1183
1066
     timed out */
1184
1067
  
1185
1068
  if(quit_now){
1186
 
    avahi_s_service_resolver_free(r);
1187
1069
    return;
1188
1070
  }
1189
1071
  
1576
1458
  error_t ret_errno = 0;
1577
1459
  if(seteuid(0) == -1){
1578
1460
    ret_errno = errno;
 
1461
    perror_plus("seteuid");
1579
1462
  }
1580
1463
  errno = old_errno;
1581
1464
  return ret_errno;
1592
1475
  }
1593
1476
  if(setuid(0) == -1){
1594
1477
    ret_errno = errno;
 
1478
    perror_plus("seteuid");
1595
1479
  }
1596
1480
  errno = old_errno;
1597
1481
  return ret_errno;
1604
1488
  error_t ret_errno = 0;
1605
1489
  if(seteuid(uid) == -1){
1606
1490
    ret_errno = errno;
 
1491
    perror_plus("seteuid");
1607
1492
  }
1608
1493
  errno = old_errno;
1609
1494
  return ret_errno;
1616
1501
  error_t ret_errno = 0;
1617
1502
  if(setuid(uid) == -1){
1618
1503
    ret_errno = errno;
 
1504
    perror_plus("setuid");
1619
1505
  }
1620
1506
  errno = old_errno;
1621
1507
  return ret_errno;
1624
1510
__attribute__((nonnull))
1625
1511
void run_network_hooks(const char *mode, const char *interface,
1626
1512
                       const float delay){
1627
 
  struct dirent **direntries = NULL;
 
1513
  struct dirent **direntries;
1628
1514
  if(hookdir_fd == -1){
1629
1515
    hookdir_fd = open(hookdir, O_RDONLY);
1630
1516
    if(hookdir_fd == -1){
1668
1554
    if(hook_pid == 0){
1669
1555
      /* Child */
1670
1556
      /* Raise privileges */
1671
 
      errno = raise_privileges_permanently();
1672
 
      if(errno != 0){
 
1557
      if(raise_privileges_permanently() != 0){
1673
1558
        perror_plus("Failed to raise privileges");
1674
1559
        _exit(EX_NOPERM);
1675
1560
      }
1757
1642
        _exit(EXIT_FAILURE);
1758
1643
      }
1759
1644
    } else {
1760
 
      if(hook_pid == -1){
1761
 
        perror_plus("fork");
1762
 
        free(direntry);
1763
 
        continue;
1764
 
      }
1765
1645
      int status;
1766
1646
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1767
1647
        perror_plus("waitpid");
1768
 
        free(direntry);
1769
1648
        continue;
1770
1649
      }
1771
1650
      if(WIFEXITED(status)){
1773
1652
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1774
1653
                       " with status %d\n", direntry->d_name,
1775
1654
                       WEXITSTATUS(status));
1776
 
          free(direntry);
1777
1655
          continue;
1778
1656
        }
1779
1657
      } else if(WIFSIGNALED(status)){
1780
1658
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1781
1659
                     " signal %d\n", direntry->d_name,
1782
1660
                     WTERMSIG(status));
1783
 
        free(direntry);
1784
1661
        continue;
1785
1662
      } else {
1786
1663
        fprintf_plus(stderr, "Warning: network hook \"%s\""
1787
1664
                     " crashed\n", direntry->d_name);
1788
 
        free(direntry);
1789
1665
        continue;
1790
1666
      }
1791
1667
    }
1793
1669
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1794
1670
                   direntry->d_name);
1795
1671
    }
1796
 
    free(direntry);
1797
1672
  }
1798
 
  free(direntries);
1799
1673
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1800
1674
    perror_plus("close");
1801
1675
  } else {
1858
1732
    /* Raise privileges */
1859
1733
    ret_errno = raise_privileges();
1860
1734
    if(ret_errno != 0){
1861
 
      errno = ret_errno;
1862
1735
      perror_plus("Failed to raise privileges");
1863
1736
    }
1864
1737
    
1968
1841
    /* Raise privileges */
1969
1842
    ret_errno = raise_privileges();
1970
1843
    if(ret_errno != 0){
1971
 
      errno = ret_errno;
1972
1844
      perror_plus("Failed to raise privileges");
1973
1845
    }
1974
1846
    
2007
1879
}
2008
1880
 
2009
1881
int main(int argc, char *argv[]){
2010
 
  mandos_context mc = { .server = NULL, .dh_bits = 0,
 
1882
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
2011
1883
                        .priority = "SECURE256:!CTYPE-X.509:"
2012
 
                        "+CTYPE-OPENPGP:!RSA", .current_server = NULL,
 
1884
                        "+CTYPE-OPENPGP", .current_server = NULL,
2013
1885
                        .interfaces = NULL, .interfaces_size = 0 };
2014
1886
  AvahiSServiceBrowser *sb = NULL;
2015
1887
  error_t ret_errno;
2381
2253
  
2382
2254
  /* If no interfaces were specified, make a list */
2383
2255
  if(mc.interfaces == NULL){
2384
 
    struct dirent **direntries = NULL;
 
2256
    struct dirent **direntries;
2385
2257
    /* Look for any good interfaces */
2386
2258
    ret = scandir(sys_class_net, &direntries, good_interface,
2387
2259
                  alphasort);
2393
2265
        if(ret_errno != 0){
2394
2266
          errno = ret_errno;
2395
2267
          perror_plus("argz_add");
2396
 
          free(direntries[i]);
2397
2268
          continue;
2398
2269
        }
2399
2270
        if(debug){
2400
2271
          fprintf_plus(stderr, "Will use interface \"%s\"\n",
2401
2272
                       direntries[i]->d_name);
2402
2273
        }
2403
 
        free(direntries[i]);
2404
2274
      }
2405
2275
      free(direntries);
2406
2276
    } else {
2407
 
      if(ret == 0){
2408
 
        free(direntries);
2409
 
      }
 
2277
      free(direntries);
2410
2278
      fprintf_plus(stderr, "Could not find a network interface\n");
2411
2279
      exitcode = EXIT_FAILURE;
2412
2280
      goto end;
2676
2544
    mc.current_server->prev->next = NULL;
2677
2545
    while(mc.current_server != NULL){
2678
2546
      server *next = mc.current_server->next;
2679
 
#ifdef __GNUC__
2680
 
#pragma GCC diagnostic push
2681
 
#pragma GCC diagnostic ignored "-Wcast-qual"
2682
 
#endif
2683
 
      free((char *)(mc.current_server->ip));
2684
 
#ifdef __GNUC__
2685
 
#pragma GCC diagnostic pop
2686
 
#endif
2687
2547
      free(mc.current_server);
2688
2548
      mc.current_server = next;
2689
2549
    }
2693
2553
  {
2694
2554
    ret_errno = raise_privileges();
2695
2555
    if(ret_errno != 0){
2696
 
      errno = ret_errno;
2697
2556
      perror_plus("Failed to raise privileges");
2698
2557
    } else {
2699
2558
      
2722
2581
    
2723
2582
    ret_errno = lower_privileges_permanently();
2724
2583
    if(ret_errno != 0){
2725
 
      errno = ret_errno;
2726
2584
      perror_plus("Failed to lower privileges permanently");
2727
2585
    }
2728
2586
  }
2750
2608
      int numentries = scandir(tempdir, &direntries, notdotentries,
2751
2609
                               alphasort);
2752
2610
#endif  /* not __GLIBC__ */
2753
 
      if(numentries >= 0){
 
2611
      if(numentries > 0){
2754
2612
        for(int i = 0; i < numentries; i++){
2755
2613
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2756
2614
          if(ret == -1){
2758
2616
                         " \"%s\", 0): %s\n", tempdir,
2759
2617
                         direntries[i]->d_name, strerror(errno));
2760
2618
          }
2761
 
          free(direntries[i]);
2762
2619
        }
2763
2620
        
2764
2621
        /* need to clean even if 0 because man page doesn't specify */