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

  • Committer: Teddy Hogeborn
  • Date: 2014-06-14 02:57:16 UTC
  • Revision ID: teddy@recompile.se-20140614025716-i6887rk3u1iy9l67
In plugin-runner, protect against a larger FD_SETSIZE than fd_set.

* plugin-runner.c (main): Before forking, check that the first FD from
                          pipe() does not exceed FD_SETSIZE.

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 */
1076
1066
     timed out */
1077
1067
  
1078
1068
  if(quit_now){
1079
 
    avahi_s_service_resolver_free(r);
1080
1069
    return;
1081
1070
  }
1082
1071
  
1469
1458
  error_t ret_errno = 0;
1470
1459
  if(seteuid(0) == -1){
1471
1460
    ret_errno = errno;
 
1461
    perror_plus("seteuid");
1472
1462
  }
1473
1463
  errno = old_errno;
1474
1464
  return ret_errno;
1485
1475
  }
1486
1476
  if(setuid(0) == -1){
1487
1477
    ret_errno = errno;
 
1478
    perror_plus("seteuid");
1488
1479
  }
1489
1480
  errno = old_errno;
1490
1481
  return ret_errno;
1497
1488
  error_t ret_errno = 0;
1498
1489
  if(seteuid(uid) == -1){
1499
1490
    ret_errno = errno;
 
1491
    perror_plus("seteuid");
1500
1492
  }
1501
1493
  errno = old_errno;
1502
1494
  return ret_errno;
1509
1501
  error_t ret_errno = 0;
1510
1502
  if(setuid(uid) == -1){
1511
1503
    ret_errno = errno;
 
1504
    perror_plus("setuid");
1512
1505
  }
1513
1506
  errno = old_errno;
1514
1507
  return ret_errno;
1517
1510
__attribute__((nonnull))
1518
1511
void run_network_hooks(const char *mode, const char *interface,
1519
1512
                       const float delay){
1520
 
  struct dirent **direntries = NULL;
 
1513
  struct dirent **direntries;
1521
1514
  if(hookdir_fd == -1){
1522
1515
    hookdir_fd = open(hookdir, O_RDONLY);
1523
1516
    if(hookdir_fd == -1){
1561
1554
    if(hook_pid == 0){
1562
1555
      /* Child */
1563
1556
      /* Raise privileges */
1564
 
      errno = raise_privileges_permanently();
1565
 
      if(errno != 0){
 
1557
      if(raise_privileges_permanently() != 0){
1566
1558
        perror_plus("Failed to raise privileges");
1567
1559
        _exit(EX_NOPERM);
1568
1560
      }
1653
1645
      int status;
1654
1646
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1655
1647
        perror_plus("waitpid");
1656
 
        free(direntry);
1657
1648
        continue;
1658
1649
      }
1659
1650
      if(WIFEXITED(status)){
1661
1652
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1662
1653
                       " with status %d\n", direntry->d_name,
1663
1654
                       WEXITSTATUS(status));
1664
 
          free(direntry);
1665
1655
          continue;
1666
1656
        }
1667
1657
      } else if(WIFSIGNALED(status)){
1668
1658
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1669
1659
                     " signal %d\n", direntry->d_name,
1670
1660
                     WTERMSIG(status));
1671
 
        free(direntry);
1672
1661
        continue;
1673
1662
      } else {
1674
1663
        fprintf_plus(stderr, "Warning: network hook \"%s\""
1675
1664
                     " crashed\n", direntry->d_name);
1676
 
        free(direntry);
1677
1665
        continue;
1678
1666
      }
1679
1667
    }
1681
1669
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1682
1670
                   direntry->d_name);
1683
1671
    }
1684
 
    free(direntry);
1685
1672
  }
1686
 
  free(direntries);
1687
1673
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1688
1674
    perror_plus("close");
1689
1675
  } else {
1746
1732
    /* Raise privileges */
1747
1733
    ret_errno = raise_privileges();
1748
1734
    if(ret_errno != 0){
1749
 
      errno = ret_errno;
1750
1735
      perror_plus("Failed to raise privileges");
1751
1736
    }
1752
1737
    
1856
1841
    /* Raise privileges */
1857
1842
    ret_errno = raise_privileges();
1858
1843
    if(ret_errno != 0){
1859
 
      errno = ret_errno;
1860
1844
      perror_plus("Failed to raise privileges");
1861
1845
    }
1862
1846
    
2281
2265
        if(ret_errno != 0){
2282
2266
          errno = ret_errno;
2283
2267
          perror_plus("argz_add");
2284
 
          free(direntries[i]);
2285
2268
          continue;
2286
2269
        }
2287
2270
        if(debug){
2288
2271
          fprintf_plus(stderr, "Will use interface \"%s\"\n",
2289
2272
                       direntries[i]->d_name);
2290
2273
        }
2291
 
        free(direntries[i]);
2292
2274
      }
2293
2275
      free(direntries);
2294
2276
    } else {
2295
 
      if(ret == 0){
2296
 
        free(direntries);
2297
 
      }
 
2277
      free(direntries);
2298
2278
      fprintf_plus(stderr, "Could not find a network interface\n");
2299
2279
      exitcode = EXIT_FAILURE;
2300
2280
      goto end;
2564
2544
    mc.current_server->prev->next = NULL;
2565
2545
    while(mc.current_server != NULL){
2566
2546
      server *next = mc.current_server->next;
2567
 
#ifdef __GNUC__
2568
 
#pragma GCC diagnostic push
2569
 
#pragma GCC diagnostic ignored "-Wcast-qual"
2570
 
#endif
2571
 
      free((char *)(mc.current_server->ip));
2572
 
#ifdef __GNUC__
2573
 
#pragma GCC diagnostic pop
2574
 
#endif
2575
2547
      free(mc.current_server);
2576
2548
      mc.current_server = next;
2577
2549
    }
2581
2553
  {
2582
2554
    ret_errno = raise_privileges();
2583
2555
    if(ret_errno != 0){
2584
 
      errno = ret_errno;
2585
2556
      perror_plus("Failed to raise privileges");
2586
2557
    } else {
2587
2558
      
2610
2581
    
2611
2582
    ret_errno = lower_privileges_permanently();
2612
2583
    if(ret_errno != 0){
2613
 
      errno = ret_errno;
2614
2584
      perror_plus("Failed to lower privileges permanently");
2615
2585
    }
2616
2586
  }
2638
2608
      int numentries = scandir(tempdir, &direntries, notdotentries,
2639
2609
                               alphasort);
2640
2610
#endif  /* not __GLIBC__ */
2641
 
      if(numentries >= 0){
 
2611
      if(numentries > 0){
2642
2612
        for(int i = 0; i < numentries; i++){
2643
2613
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2644
2614
          if(ret == -1){
2646
2616
                         " \"%s\", 0): %s\n", tempdir,
2647
2617
                         direntries[i]->d_name, strerror(errno));
2648
2618
          }
2649
 
          free(direntries[i]);
2650
2619
        }
2651
2620
        
2652
2621
        /* need to clean even if 0 because man page doesn't specify */