/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 dracut-module/password-agent.c

  • Committer: teddy at recompile
  • Date: 2020-04-08 19:15:13 UTC
  • mfrom: (237.4.130 release)
  • Revision ID: teddy@recompile.se-20200408191513-n0ke303478rw1g0b
Merge from release branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
#include <error.h>              /* error() */
49
49
#include <sysexits.h>           /* EX_USAGE, EX_OSERR, EX_OSFILE */
50
50
#include <errno.h>              /* errno, error_t, EACCES,
51
 
                                   ENAMETOOLONG, ENOENT, EEXIST,
52
 
                                   ECHILD, EPERM, ENOMEM, EAGAIN,
53
 
                                   EINTR, ENOBUFS, EADDRINUSE,
 
51
                                   ENAMETOOLONG, ENOENT, ENOTDIR,
 
52
                                   ENOMEM, EEXIST, ECHILD, EPERM,
 
53
                                   EAGAIN, EINTR, ENOBUFS, EADDRINUSE,
54
54
                                   ECONNREFUSED, ECONNRESET,
55
55
                                   ETOOMANYREFS, EMSGSIZE, EBADF,
56
56
                                   EINVAL */
73
73
                                   ARGP_ERR_UNKNOWN, ARGP_KEY_ARGS,
74
74
                                   struct argp, argp_parse(),
75
75
                                   ARGP_NO_EXIT */
 
76
#include <stdint.h>             /* SIZE_MAX */
76
77
#include <unistd.h>             /* uid_t, gid_t, close(), pipe2(),
77
78
                                   fork(), _exit(), dup2(),
78
79
                                   STDOUT_FILENO, setresgid(),
83
84
#include <sys/mman.h>           /* munlock(), mlock() */
84
85
#include <fcntl.h>              /* O_CLOEXEC, O_NONBLOCK, fcntl(),
85
86
                                   F_GETFD, F_GETFL, FD_CLOEXEC,
86
 
                                   open(), O_WRONLY, O_RDONLY */
 
87
                                   open(), O_WRONLY, O_NOCTTY,
 
88
                                   O_RDONLY, O_NOFOLLOW */
87
89
#include <sys/wait.h>           /* waitpid(), WNOHANG, WIFEXITED(),
88
90
                                   WEXITSTATUS() */
89
91
#include <limits.h>             /* PIPE_BUF, NAME_MAX, INT_MAX */
90
92
#include <sys/inotify.h>        /* inotify_init1(), IN_NONBLOCK,
91
93
                                   IN_CLOEXEC, inotify_add_watch(),
92
94
                                   IN_CLOSE_WRITE, IN_MOVED_TO,
93
 
                                   IN_DELETE, struct inotify_event */
 
95
                                   IN_MOVED_FROM, IN_DELETE,
 
96
                                   IN_EXCL_UNLINK, IN_ONLYDIR,
 
97
                                   struct inotify_event */
94
98
#include <fnmatch.h>            /* fnmatch(), FNM_FILE_NAME */
95
 
#include <stdio.h>              /* asprintf(), FILE, fopen(),
96
 
                                   getline(), sscanf(), feof(),
97
 
                                   ferror(), fclose(), stderr,
98
 
                                   rename(), fdopen(), fprintf(),
99
 
                                   fscanf() */
 
99
#include <stdio.h>              /* asprintf(), FILE, stderr, fopen(),
 
100
                                   fclose(), getline(), sscanf(),
 
101
                                   feof(), ferror(), rename(),
 
102
                                   fdopen(), fprintf(), fscanf() */
100
103
#include <glib.h>    /* GKeyFile, g_key_file_free(), g_key_file_new(),
101
104
                        GError, g_key_file_load_from_file(),
102
105
                        G_KEY_FILE_NONE, TRUE, G_FILE_ERROR_NOENT,
145
148
  mono_microsecs next_run;
146
149
} __attribute__((designated_init)) task_queue;
147
150
 
148
 
/* "func_type" - A function type for task functions
 
151
/* "task_func" - A function type for task functions
149
152
 
150
153
   I.e. functions for the code which runs when a task is run, all have
151
154
   this type */
431
434
    case EACCES:
432
435
    case ENAMETOOLONG:
433
436
    case ENOENT:
 
437
    case ENOTDIR:
434
438
      return EX_OSFILE;
435
439
    default:
436
440
      return EX_OSERR;
647
651
 
648
652
__attribute__((nonnull, warn_unused_result))
649
653
bool add_to_queue(task_queue *const queue, const task_context task){
 
654
  if((queue->length + 1) > (SIZE_MAX / sizeof(task_context))){
 
655
    /* overflow */
 
656
    error(0, ENOMEM, "Failed to allocate %" PRIuMAX
 
657
          " tasks for queue->tasks", (uintmax_t)(queue->length + 1));
 
658
    errno = ENOMEM;
 
659
    return false;
 
660
  }
650
661
  const size_t needed_size = sizeof(task_context)*(queue->length + 1);
651
662
  if(needed_size > (queue->allocated)){
652
663
    task_context *const new_tasks = realloc(queue->tasks,
857
868
  }
858
869
  close(pipefds[1]);
859
870
 
 
871
  if(pid == -1){
 
872
    error(0, errno, "Failed to fork()");
 
873
    close(pipefds[0]);
 
874
    return false;
 
875
  }
 
876
 
860
877
  if(not add_to_queue(queue, (task_context){
861
878
        .func=wait_for_mandos_client_exit,
862
879
        .pid=pid,
1017
1034
    return false;
1018
1035
  }
1019
1036
 
1020
 
  if(inotify_add_watch(fd, dir, IN_CLOSE_WRITE
1021
 
                       | IN_MOVED_TO | IN_DELETE)
 
1037
  if(inotify_add_watch(fd, dir, IN_CLOSE_WRITE | IN_MOVED_TO
 
1038
                       | IN_MOVED_FROM| IN_DELETE | IN_EXCL_UNLINK
 
1039
                       | IN_ONLYDIR)
1022
1040
     == -1){
1023
1041
    error(0, errno, "Failed to create inotify watch on %s", dir);
1024
1042
    return false;
1071
1089
  /* "sufficient to read at least one event." - inotify(7) */
1072
1090
  const size_t ievent_size = (sizeof(struct inotify_event)
1073
1091
                              + NAME_MAX + 1);
1074
 
  char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
1075
 
  struct inotify_event *ievent = ((struct inotify_event *)
1076
 
                                  ievent_buffer);
 
1092
  struct {
 
1093
    struct inotify_event event;
 
1094
    char name_buffer[NAME_MAX + 1];
 
1095
  } ievent_buffer;
 
1096
  struct inotify_event *const ievent = &ievent_buffer.event;
1077
1097
 
1078
1098
  const ssize_t read_length = read(fd, ievent, ievent_size);
1079
1099
  if(read_length == 0){ /* EOF */
1117
1137
             immediately */
1118
1138
          queue->next_run = 1;
1119
1139
        }
1120
 
      } else if(ievent->mask & IN_DELETE){
 
1140
      } else if(ievent->mask & (IN_MOVED_FROM | IN_DELETE)){
1121
1141
        if(not string_set_add(cancelled_filenames,
1122
1142
                              question_filename)){
1123
1143
          error(0, errno, "Could not add question %s to"
1877
1897
  g_assert_true(queue->tasks[0].func == dummy_func);
1878
1898
}
1879
1899
 
 
1900
static void test_add_to_queue_overflow(__attribute__((unused))
 
1901
                                       test_fixture *fixture,
 
1902
                                       __attribute__((unused))
 
1903
                                       gconstpointer user_data){
 
1904
  __attribute__((cleanup(cleanup_queue)))
 
1905
    task_queue *queue = create_queue();
 
1906
  g_assert_nonnull(queue);
 
1907
  g_assert_true(queue->length == 0);
 
1908
  queue->length = SIZE_MAX / sizeof(task_context); /* fake max size */
 
1909
 
 
1910
  FILE *real_stderr = stderr;
 
1911
  FILE *devnull = fopen("/dev/null", "we");
 
1912
  g_assert_nonnull(devnull);
 
1913
  stderr = devnull;
 
1914
  const bool ret = add_to_queue(queue,
 
1915
                                (task_context){ .func=dummy_func });
 
1916
  g_assert_true(errno == ENOMEM);
 
1917
  g_assert_false(ret);
 
1918
  stderr = real_stderr;
 
1919
  g_assert_cmpint(fclose(devnull), ==, 0);
 
1920
  queue->length = 0;            /* Restore real size */
 
1921
}
 
1922
 
1880
1923
static void dummy_func(__attribute__((unused))
1881
1924
                       const task_context task,
1882
1925
                       __attribute__((unused))
2153
2196
    }
2154
2197
    exit(EXIT_SUCCESS);
2155
2198
  }
 
2199
  if(pid == -1){
 
2200
    error(EXIT_FAILURE, errno, "Failed to fork()");
 
2201
  }
 
2202
 
2156
2203
  int status;
2157
2204
  waitpid(pid, &status, 0);
2158
2205
  if(WIFEXITED(status) and (WEXITSTATUS(status) == EXIT_SUCCESS)){
2220
2267
 
2221
2268
  {
2222
2269
    __attribute__((cleanup(cleanup_close)))
2223
 
      const int devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
 
2270
      const int devnull_fd = open("/dev/null",
 
2271
                                  O_WRONLY | O_CLOEXEC | O_NOCTTY);
2224
2272
    g_assert_cmpint(devnull_fd, >=, 0);
2225
2273
    __attribute__((cleanup(cleanup_close)))
2226
2274
      const int real_stderr_fd = dup(STDERR_FILENO);
2250
2298
    {
2251
2299
      __attribute__((cleanup(cleanup_close)))
2252
2300
        const int devnull_fd = open("/dev/null",
2253
 
                                    O_WRONLY | O_CLOEXEC);
 
2301
                                    O_WRONLY | O_CLOEXEC | O_NOCTTY);
2254
2302
      g_assert_cmpint(devnull_fd, >=, 0);
2255
2303
      __attribute__((cleanup(cleanup_close)))
2256
2304
        const int real_stderr_fd = dup(STDERR_FILENO);
2901
2949
 
2902
2950
  __attribute__((cleanup(cleanup_close)))
2903
2951
    const int devnull_fd = open("/dev/null",
2904
 
                                O_WRONLY | O_CLOEXEC);
 
2952
                                O_WRONLY | O_CLOEXEC | O_NOCTTY);
2905
2953
  g_assert_cmpint(devnull_fd, >=, 0);
2906
2954
  __attribute__((cleanup(cleanup_close)))
2907
2955
    const int real_stderr_fd = dup(STDERR_FILENO);
2972
3020
 
2973
3021
  __attribute__((cleanup(cleanup_close)))
2974
3022
    const int devnull_fd = open("/dev/null",
2975
 
                                O_WRONLY | O_CLOEXEC);
 
3023
                                O_WRONLY | O_CLOEXEC, O_NOCTTY);
2976
3024
  g_assert_cmpint(devnull_fd, >=, 0);
2977
3025
  __attribute__((cleanup(cleanup_close)))
2978
3026
    const int real_stderr_fd = dup(STDERR_FILENO);
3016
3064
    buffer password = {};
3017
3065
 
3018
3066
  /* Reading /proc/self/mem from offset 0 will always give EIO */
3019
 
  const int fd = open("/proc/self/mem", O_RDONLY | O_CLOEXEC);
 
3067
  const int fd = open("/proc/self/mem",
 
3068
                      O_RDONLY | O_CLOEXEC | O_NOCTTY);
3020
3069
 
3021
3070
  bool password_is_read = false;
3022
3071
  bool quit_now = false;
3450
3499
  g_assert_cmpuint((unsigned int)queue->length, ==, 0);
3451
3500
}
3452
3501
 
 
3502
static void test_add_inotify_dir_watch_nondir(__attribute__((unused))
 
3503
                                              test_fixture *fixture,
 
3504
                                            __attribute__((unused))
 
3505
                                              gconstpointer
 
3506
                                              user_data){
 
3507
  __attribute__((cleanup(cleanup_close)))
 
3508
    const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
 
3509
  g_assert_cmpint(epoll_fd, >=, 0);
 
3510
  __attribute__((cleanup(cleanup_queue)))
 
3511
    task_queue *queue = create_queue();
 
3512
  g_assert_nonnull(queue);
 
3513
  __attribute__((cleanup(string_set_clear)))
 
3514
    string_set cancelled_filenames = {};
 
3515
  const mono_microsecs current_time = 0;
 
3516
 
 
3517
  bool quit_now = false;
 
3518
  buffer password = {};
 
3519
  bool mandos_client_exited = false;
 
3520
  bool password_is_read = false;
 
3521
 
 
3522
  const char not_a_directory[] = "/dev/tty";
 
3523
 
 
3524
  FILE *real_stderr = stderr;
 
3525
  FILE *devnull = fopen("/dev/null", "we");
 
3526
  g_assert_nonnull(devnull);
 
3527
  stderr = devnull;
 
3528
  g_assert_false(add_inotify_dir_watch(queue, epoll_fd, &quit_now,
 
3529
                                       &password, not_a_directory,
 
3530
                                       &cancelled_filenames,
 
3531
                                       &current_time,
 
3532
                                       &mandos_client_exited,
 
3533
                                       &password_is_read));
 
3534
  stderr = real_stderr;
 
3535
  g_assert_cmpint(fclose(devnull), ==, 0);
 
3536
 
 
3537
  g_assert_cmpuint((unsigned int)queue->length, ==, 0);
 
3538
}
 
3539
 
3453
3540
static void test_add_inotify_dir_watch_EAGAIN(__attribute__((unused))
3454
3541
                                              test_fixture *fixture,
3455
3542
                                              __attribute__((unused))
3670
3757
}
3671
3758
 
3672
3759
static
 
3760
void test_add_inotify_dir_watch_IN_MOVED_FROM(__attribute__((unused))
 
3761
                                              test_fixture *fixture,
 
3762
                                              __attribute__((unused))
 
3763
                                              gconstpointer
 
3764
                                              user_data){
 
3765
  __attribute__((cleanup(cleanup_close)))
 
3766
    const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
 
3767
  g_assert_cmpint(epoll_fd, >=, 0);
 
3768
  __attribute__((cleanup(cleanup_queue)))
 
3769
    task_queue *queue = create_queue();
 
3770
  g_assert_nonnull(queue);
 
3771
  __attribute__((cleanup(string_set_clear)))
 
3772
    string_set cancelled_filenames = {};
 
3773
  const mono_microsecs current_time = 0;
 
3774
 
 
3775
  bool quit_now = false;
 
3776
  buffer password = {};
 
3777
  bool mandos_client_exited = false;
 
3778
  bool password_is_read = false;
 
3779
 
 
3780
  __attribute__((cleanup(cleanup_string)))
 
3781
    char *tempdir = make_temporary_directory();
 
3782
  g_assert_nonnull(tempdir);
 
3783
 
 
3784
  __attribute__((cleanup(cleanup_string)))
 
3785
    char *tempfilename = make_temporary_file_in_directory(tempdir);
 
3786
  g_assert_nonnull(tempfilename);
 
3787
 
 
3788
  __attribute__((cleanup(cleanup_string)))
 
3789
    char *targetdir = make_temporary_directory();
 
3790
  g_assert_nonnull(targetdir);
 
3791
 
 
3792
  __attribute__((cleanup(cleanup_string)))
 
3793
    char *targetfilename = NULL;
 
3794
  g_assert_cmpint(asprintf(&targetfilename, "%s/%s", targetdir,
 
3795
                           basename(tempfilename)), >, 0);
 
3796
  g_assert_nonnull(targetfilename);
 
3797
 
 
3798
  g_assert_true(add_inotify_dir_watch(queue, epoll_fd, &quit_now,
 
3799
                                      &password, tempdir,
 
3800
                                      &cancelled_filenames,
 
3801
                                      &current_time,
 
3802
                                      &mandos_client_exited,
 
3803
                                      &password_is_read));
 
3804
 
 
3805
  g_assert_cmpint(rename(tempfilename, targetfilename), ==, 0);
 
3806
 
 
3807
  const task_context *const added_read_task
 
3808
    = find_matching_task(queue,
 
3809
                         (task_context){ .func=read_inotify_event });
 
3810
  g_assert_nonnull(added_read_task);
 
3811
 
 
3812
  /* "sufficient to read at least one event." - inotify(7) */
 
3813
  const size_t ievent_size = (sizeof(struct inotify_event)
 
3814
                              + NAME_MAX + 1);
 
3815
  struct inotify_event *ievent = malloc(ievent_size);
 
3816
  g_assert_nonnull(ievent);
 
3817
 
 
3818
  ssize_t read_size = read(added_read_task->fd, ievent, ievent_size);
 
3819
 
 
3820
  g_assert_cmpint((int)read_size, >, 0);
 
3821
  g_assert_true(ievent->mask & IN_MOVED_FROM);
 
3822
  g_assert_cmpstr(ievent->name, ==, basename(tempfilename));
 
3823
 
 
3824
  free(ievent);
 
3825
 
 
3826
  g_assert_cmpint(unlink(targetfilename), ==, 0);
 
3827
  g_assert_cmpint(rmdir(targetdir), ==, 0);
 
3828
  g_assert_cmpint(rmdir(tempdir), ==, 0);
 
3829
}
 
3830
 
 
3831
static
3673
3832
void test_add_inotify_dir_watch_IN_DELETE(__attribute__((unused))
3674
3833
                                          test_fixture *fixture,
3675
3834
                                          __attribute__((unused))
3733
3892
  g_assert_cmpint(rmdir(tempdir), ==, 0);
3734
3893
}
3735
3894
 
 
3895
static
 
3896
void test_add_inotify_dir_watch_IN_EXCL_UNLINK(__attribute__((unused))
 
3897
                                               test_fixture *fixture,
 
3898
                                               __attribute__((unused))
 
3899
                                               gconstpointer
 
3900
                                               user_data){
 
3901
  __attribute__((cleanup(cleanup_close)))
 
3902
    const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
 
3903
  g_assert_cmpint(epoll_fd, >=, 0);
 
3904
  __attribute__((cleanup(cleanup_queue)))
 
3905
    task_queue *queue = create_queue();
 
3906
  g_assert_nonnull(queue);
 
3907
  __attribute__((cleanup(string_set_clear)))
 
3908
    string_set cancelled_filenames = {};
 
3909
  const mono_microsecs current_time = 0;
 
3910
 
 
3911
  bool quit_now = false;
 
3912
  buffer password = {};
 
3913
  bool mandos_client_exited = false;
 
3914
  bool password_is_read = false;
 
3915
 
 
3916
  __attribute__((cleanup(cleanup_string)))
 
3917
    char *tempdir = make_temporary_directory();
 
3918
  g_assert_nonnull(tempdir);
 
3919
 
 
3920
  __attribute__((cleanup(cleanup_string)))
 
3921
    char *tempfile = make_temporary_file_in_directory(tempdir);
 
3922
  g_assert_nonnull(tempfile);
 
3923
  int tempfile_fd = open(tempfile, O_WRONLY | O_CLOEXEC | O_NOCTTY
 
3924
                         | O_NOFOLLOW);
 
3925
  g_assert_cmpint(tempfile_fd, >, 2);
 
3926
 
 
3927
  g_assert_true(add_inotify_dir_watch(queue, epoll_fd, &quit_now,
 
3928
                                      &password, tempdir,
 
3929
                                      &cancelled_filenames,
 
3930
                                      &current_time,
 
3931
                                      &mandos_client_exited,
 
3932
                                      &password_is_read));
 
3933
  g_assert_cmpint(unlink(tempfile), ==, 0);
 
3934
 
 
3935
  g_assert_cmpuint((unsigned int)queue->length, >, 0);
 
3936
 
 
3937
  const task_context *const added_read_task
 
3938
    = find_matching_task(queue,
 
3939
                         (task_context){ .func=read_inotify_event });
 
3940
  g_assert_nonnull(added_read_task);
 
3941
 
 
3942
  g_assert_cmpint(added_read_task->fd, >, 2);
 
3943
  g_assert_true(fd_has_cloexec_and_nonblock(added_read_task->fd));
 
3944
 
 
3945
  /* "sufficient to read at least one event." - inotify(7) */
 
3946
  const size_t ievent_size = (sizeof(struct inotify_event)
 
3947
                              + NAME_MAX + 1);
 
3948
  struct inotify_event *ievent = malloc(ievent_size);
 
3949
  g_assert_nonnull(ievent);
 
3950
 
 
3951
  ssize_t read_size = 0;
 
3952
  read_size = read(added_read_task->fd, ievent, ievent_size);
 
3953
 
 
3954
  g_assert_cmpint((int)read_size, >, 0);
 
3955
  g_assert_true(ievent->mask & IN_DELETE);
 
3956
  g_assert_cmpstr(ievent->name, ==, basename(tempfile));
 
3957
 
 
3958
  g_assert_cmpint(close(tempfile_fd), ==, 0);
 
3959
 
 
3960
  /* IN_EXCL_UNLINK should make the closing of the previously unlinked
 
3961
     file not appear as an ievent, so we should not see it now. */
 
3962
  read_size = read(added_read_task->fd, ievent, ievent_size);
 
3963
  g_assert_cmpint((int)read_size, ==, -1);
 
3964
  g_assert_true(errno == EAGAIN);
 
3965
 
 
3966
  free(ievent);
 
3967
 
 
3968
  g_assert_cmpint(rmdir(tempdir), ==, 0);
 
3969
}
 
3970
 
3736
3971
static void test_read_inotify_event_readerror(__attribute__((unused))
3737
3972
                                              test_fixture *fixture,
3738
3973
                                              __attribute__((unused))
3744
3979
  const mono_microsecs current_time = 0;
3745
3980
 
3746
3981
  /* Reading /proc/self/mem from offset 0 will always result in EIO */
3747
 
  const int fd = open("/proc/self/mem", O_RDONLY | O_CLOEXEC);
 
3982
  const int fd = open("/proc/self/mem",
 
3983
                      O_RDONLY | O_CLOEXEC | O_NOCTTY);
3748
3984
 
3749
3985
  bool quit_now = false;
3750
3986
  __attribute__((cleanup(cleanup_queue)))
3929
4165
  const size_t ievent_max_size = (sizeof(struct inotify_event)
3930
4166
                                  + NAME_MAX + 1);
3931
4167
  g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
3932
 
  char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
3933
 
  struct inotify_event *ievent = ((struct inotify_event *)
3934
 
                                  ievent_buffer);
 
4168
  struct {
 
4169
    struct inotify_event event;
 
4170
    char name_buffer[NAME_MAX + 1];
 
4171
  } ievent_buffer;
 
4172
  struct inotify_event *const ievent = &ievent_buffer.event;
3935
4173
 
3936
4174
  const char dummy_file_name[] = "ask.dummy_file_name";
3937
4175
  ievent->mask = IN_CLOSE_WRITE;
3939
4177
  memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
3940
4178
  const size_t ievent_size = (sizeof(struct inotify_event)
3941
4179
                              + sizeof(dummy_file_name));
3942
 
  g_assert_cmpint(write(pipefds[1], ievent_buffer, ievent_size),
 
4180
  g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
3943
4181
                  ==, ievent_size);
3944
4182
  g_assert_cmpint(close(pipefds[1]), ==, 0);
3945
4183
 
4022
4260
  const size_t ievent_max_size = (sizeof(struct inotify_event)
4023
4261
                                  + NAME_MAX + 1);
4024
4262
  g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4025
 
  char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
4026
 
  struct inotify_event *ievent = ((struct inotify_event *)
4027
 
                                  ievent_buffer);
 
4263
  struct {
 
4264
    struct inotify_event event;
 
4265
    char name_buffer[NAME_MAX + 1];
 
4266
  } ievent_buffer;
 
4267
  struct inotify_event *const ievent = &ievent_buffer.event;
4028
4268
 
4029
4269
  const char dummy_file_name[] = "ask.dummy_file_name";
4030
4270
  ievent->mask = IN_MOVED_TO;
4032
4272
  memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4033
4273
  const size_t ievent_size = (sizeof(struct inotify_event)
4034
4274
                              + sizeof(dummy_file_name));
4035
 
  g_assert_cmpint(write(pipefds[1], ievent_buffer, ievent_size),
 
4275
  g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4036
4276
                  ==, ievent_size);
4037
4277
  g_assert_cmpint(close(pipefds[1]), ==, 0);
4038
4278
 
4098
4338
      }));
4099
4339
}
4100
4340
 
 
4341
static
 
4342
void test_read_inotify_event_IN_MOVED_FROM(__attribute__((unused))
 
4343
                                           test_fixture *fixture,
 
4344
                                           __attribute__((unused))
 
4345
                                           gconstpointer user_data){
 
4346
  __attribute__((cleanup(cleanup_close)))
 
4347
    const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
 
4348
  g_assert_cmpint(epoll_fd, >=, 0);
 
4349
  __attribute__((cleanup(string_set_clear)))
 
4350
    string_set cancelled_filenames = {};
 
4351
  const mono_microsecs current_time = 0;
 
4352
 
 
4353
  int pipefds[2];
 
4354
  g_assert_cmpint(pipe2(pipefds, O_CLOEXEC | O_NONBLOCK), ==, 0);
 
4355
 
 
4356
  /* "sufficient to read at least one event." - inotify(7) */
 
4357
  const size_t ievent_max_size = (sizeof(struct inotify_event)
 
4358
                                  + NAME_MAX + 1);
 
4359
  g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
 
4360
  struct {
 
4361
    struct inotify_event event;
 
4362
    char name_buffer[NAME_MAX + 1];
 
4363
  } ievent_buffer;
 
4364
  struct inotify_event *const ievent = &ievent_buffer.event;
 
4365
 
 
4366
  const char dummy_file_name[] = "ask.dummy_file_name";
 
4367
  ievent->mask = IN_MOVED_FROM;
 
4368
  ievent->len = sizeof(dummy_file_name);
 
4369
  memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
 
4370
  const size_t ievent_size = (sizeof(struct inotify_event)
 
4371
                              + sizeof(dummy_file_name));
 
4372
  g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
 
4373
                  ==, ievent_size);
 
4374
  g_assert_cmpint(close(pipefds[1]), ==, 0);
 
4375
 
 
4376
  bool quit_now = false;
 
4377
  buffer password = {};
 
4378
  bool mandos_client_exited = false;
 
4379
  bool password_is_read = false;
 
4380
  __attribute__((cleanup(cleanup_queue)))
 
4381
    task_queue *queue = create_queue();
 
4382
  g_assert_nonnull(queue);
 
4383
 
 
4384
  task_context task = {
 
4385
    .func=read_inotify_event,
 
4386
    .epoll_fd=epoll_fd,
 
4387
    .fd=pipefds[0],
 
4388
    .quit_now=&quit_now,
 
4389
    .password=&password,
 
4390
    .filename=strdup("/nonexistent"),
 
4391
    .cancelled_filenames=&cancelled_filenames,
 
4392
    .current_time=&current_time,
 
4393
    .mandos_client_exited=&mandos_client_exited,
 
4394
    .password_is_read=&password_is_read,
 
4395
  };
 
4396
  task.func(task, queue);
 
4397
  g_assert_false(quit_now);
 
4398
  g_assert_true(queue->next_run == 0);
 
4399
  g_assert_cmpuint((unsigned int)queue->length, ==, 1);
 
4400
 
 
4401
  g_assert_nonnull(find_matching_task(queue, (task_context){
 
4402
        .func=read_inotify_event,
 
4403
        .epoll_fd=epoll_fd,
 
4404
        .fd=pipefds[0],
 
4405
        .quit_now=&quit_now,
 
4406
        .password=&password,
 
4407
        .filename=task.filename,
 
4408
        .cancelled_filenames=&cancelled_filenames,
 
4409
        .current_time=&current_time,
 
4410
        .mandos_client_exited=&mandos_client_exited,
 
4411
        .password_is_read=&password_is_read,
 
4412
      }));
 
4413
 
 
4414
  g_assert_true(epoll_set_contains(epoll_fd, pipefds[0],
 
4415
                                   EPOLLIN | EPOLLRDHUP));
 
4416
 
 
4417
  __attribute__((cleanup(cleanup_string)))
 
4418
    char *filename = NULL;
 
4419
  g_assert_cmpint(asprintf(&filename, "%s/%s", task.filename,
 
4420
                           dummy_file_name), >, 0);
 
4421
  g_assert_nonnull(filename);
 
4422
  g_assert_true(string_set_contains(*task.cancelled_filenames,
 
4423
                                    filename));
 
4424
}
 
4425
 
4101
4426
static void test_read_inotify_event_IN_DELETE(__attribute__((unused))
4102
4427
                                              test_fixture *fixture,
4103
4428
                                              __attribute__((unused))
4117
4442
  const size_t ievent_max_size = (sizeof(struct inotify_event)
4118
4443
                                  + NAME_MAX + 1);
4119
4444
  g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4120
 
  char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
4121
 
  struct inotify_event *ievent = ((struct inotify_event *)
4122
 
                                  ievent_buffer);
 
4445
  struct {
 
4446
    struct inotify_event event;
 
4447
    char name_buffer[NAME_MAX + 1];
 
4448
  } ievent_buffer;
 
4449
  struct inotify_event *const ievent = &ievent_buffer.event;
4123
4450
 
4124
4451
  const char dummy_file_name[] = "ask.dummy_file_name";
4125
4452
  ievent->mask = IN_DELETE;
4127
4454
  memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4128
4455
  const size_t ievent_size = (sizeof(struct inotify_event)
4129
4456
                              + sizeof(dummy_file_name));
4130
 
  g_assert_cmpint(write(pipefds[1], ievent_buffer, ievent_size),
 
4457
  g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4131
4458
                  ==, ievent_size);
4132
4459
  g_assert_cmpint(close(pipefds[1]), ==, 0);
4133
4460
 
4199
4526
  const size_t ievent_max_size = (sizeof(struct inotify_event)
4200
4527
                                  + NAME_MAX + 1);
4201
4528
  g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4202
 
  char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
4203
 
  struct inotify_event *ievent = ((struct inotify_event *)
4204
 
                                  ievent_buffer);
 
4529
  struct {
 
4530
    struct inotify_event event;
 
4531
    char name_buffer[NAME_MAX + 1];
 
4532
  } ievent_buffer;
 
4533
  struct inotify_event *const ievent = &ievent_buffer.event;
4205
4534
 
4206
4535
  const char dummy_file_name[] = "ignored.dummy_file_name";
4207
4536
  ievent->mask = IN_CLOSE_WRITE;
4209
4538
  memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4210
4539
  const size_t ievent_size = (sizeof(struct inotify_event)
4211
4540
                              + sizeof(dummy_file_name));
4212
 
  g_assert_cmpint(write(pipefds[1], ievent_buffer, ievent_size),
 
4541
  g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4213
4542
                  ==, ievent_size);
4214
4543
  g_assert_cmpint(close(pipefds[1]), ==, 0);
4215
4544
 
4273
4602
  const size_t ievent_max_size = (sizeof(struct inotify_event)
4274
4603
                                  + NAME_MAX + 1);
4275
4604
  g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4276
 
  char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
4277
 
  struct inotify_event *ievent = ((struct inotify_event *)
4278
 
                                  ievent_buffer);
 
4605
  struct {
 
4606
    struct inotify_event event;
 
4607
    char name_buffer[NAME_MAX + 1];
 
4608
  } ievent_buffer;
 
4609
  struct inotify_event *const ievent = &ievent_buffer.event;
4279
4610
 
4280
4611
  const char dummy_file_name[] = "ignored.dummy_file_name";
4281
4612
  ievent->mask = IN_MOVED_TO;
4283
4614
  memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4284
4615
  const size_t ievent_size = (sizeof(struct inotify_event)
4285
4616
                              + sizeof(dummy_file_name));
4286
 
  g_assert_cmpint(write(pipefds[1], ievent_buffer, ievent_size),
 
4617
  g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4287
4618
                  ==, ievent_size);
4288
4619
  g_assert_cmpint(close(pipefds[1]), ==, 0);
4289
4620
 
4330
4661
                                   EPOLLIN | EPOLLRDHUP));
4331
4662
}
4332
4663
 
 
4664
static void
 
4665
test_read_inotify_event_IN_MOVED_FROM_badname(__attribute__((unused))
 
4666
                                              test_fixture *fixture,
 
4667
                                              __attribute__((unused))
 
4668
                                              gconstpointer
 
4669
                                              user_data){
 
4670
  __attribute__((cleanup(cleanup_close)))
 
4671
    const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
 
4672
  g_assert_cmpint(epoll_fd, >=, 0);
 
4673
  __attribute__((cleanup(string_set_clear)))
 
4674
    string_set cancelled_filenames = {};
 
4675
  const mono_microsecs current_time = 0;
 
4676
 
 
4677
  int pipefds[2];
 
4678
  g_assert_cmpint(pipe2(pipefds, O_CLOEXEC | O_NONBLOCK), ==, 0);
 
4679
 
 
4680
  /* "sufficient to read at least one event." - inotify(7) */
 
4681
  const size_t ievent_max_size = (sizeof(struct inotify_event)
 
4682
                                  + NAME_MAX + 1);
 
4683
  g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
 
4684
  struct {
 
4685
    struct inotify_event event;
 
4686
    char name_buffer[NAME_MAX + 1];
 
4687
  } ievent_buffer;
 
4688
  struct inotify_event *const ievent = &ievent_buffer.event;
 
4689
 
 
4690
  const char dummy_file_name[] = "ignored.dummy_file_name";
 
4691
  ievent->mask = IN_MOVED_FROM;
 
4692
  ievent->len = sizeof(dummy_file_name);
 
4693
  memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
 
4694
  const size_t ievent_size = (sizeof(struct inotify_event)
 
4695
                              + sizeof(dummy_file_name));
 
4696
  g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
 
4697
                  ==, ievent_size);
 
4698
  g_assert_cmpint(close(pipefds[1]), ==, 0);
 
4699
 
 
4700
  bool quit_now = false;
 
4701
  buffer password = {};
 
4702
  bool mandos_client_exited = false;
 
4703
  bool password_is_read = false;
 
4704
  __attribute__((cleanup(cleanup_queue)))
 
4705
    task_queue *queue = create_queue();
 
4706
  g_assert_nonnull(queue);
 
4707
 
 
4708
  task_context task = {
 
4709
    .func=read_inotify_event,
 
4710
    .epoll_fd=epoll_fd,
 
4711
    .fd=pipefds[0],
 
4712
    .quit_now=&quit_now,
 
4713
    .password=&password,
 
4714
    .filename=strdup("/nonexistent"),
 
4715
    .cancelled_filenames=&cancelled_filenames,
 
4716
    .current_time=&current_time,
 
4717
    .mandos_client_exited=&mandos_client_exited,
 
4718
    .password_is_read=&password_is_read,
 
4719
  };
 
4720
  task.func(task, queue);
 
4721
  g_assert_false(quit_now);
 
4722
  g_assert_true(queue->next_run == 0);
 
4723
  g_assert_cmpuint((unsigned int)queue->length, ==, 1);
 
4724
 
 
4725
  g_assert_nonnull(find_matching_task(queue, (task_context){
 
4726
        .func=read_inotify_event,
 
4727
        .epoll_fd=epoll_fd,
 
4728
        .fd=pipefds[0],
 
4729
        .quit_now=&quit_now,
 
4730
        .password=&password,
 
4731
        .filename=task.filename,
 
4732
        .cancelled_filenames=&cancelled_filenames,
 
4733
        .current_time=&current_time,
 
4734
        .mandos_client_exited=&mandos_client_exited,
 
4735
        .password_is_read=&password_is_read,
 
4736
      }));
 
4737
 
 
4738
  g_assert_true(epoll_set_contains(epoll_fd, pipefds[0],
 
4739
                                   EPOLLIN | EPOLLRDHUP));
 
4740
 
 
4741
  __attribute__((cleanup(cleanup_string)))
 
4742
    char *filename = NULL;
 
4743
  g_assert_cmpint(asprintf(&filename, "%s/%s", task.filename,
 
4744
                           dummy_file_name), >, 0);
 
4745
  g_assert_nonnull(filename);
 
4746
  g_assert_false(string_set_contains(cancelled_filenames, filename));
 
4747
}
 
4748
 
4333
4749
static
4334
4750
void test_read_inotify_event_IN_DELETE_badname(__attribute__((unused))
4335
4751
                                               test_fixture *fixture,
4350
4766
  const size_t ievent_max_size = (sizeof(struct inotify_event)
4351
4767
                                  + NAME_MAX + 1);
4352
4768
  g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4353
 
  char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
4354
 
  struct inotify_event *ievent = ((struct inotify_event *)
4355
 
                                  ievent_buffer);
 
4769
  struct {
 
4770
    struct inotify_event event;
 
4771
    char name_buffer[NAME_MAX + 1];
 
4772
  } ievent_buffer;
 
4773
  struct inotify_event *const ievent = &ievent_buffer.event;
4356
4774
 
4357
4775
  const char dummy_file_name[] = "ignored.dummy_file_name";
4358
4776
  ievent->mask = IN_DELETE;
4360
4778
  memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4361
4779
  const size_t ievent_size = (sizeof(struct inotify_event)
4362
4780
                              + sizeof(dummy_file_name));
4363
 
  g_assert_cmpint(write(pipefds[1], ievent_buffer, ievent_size),
 
4781
  g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4364
4782
                  ==, ievent_size);
4365
4783
  g_assert_cmpint(close(pipefds[1]), ==, 0);
4366
4784
 
5253
5671
                                            __attribute__((unused))
5254
5672
                                            gconstpointer user_data){
5255
5673
  __attribute__((cleanup(cleanup_close)))
5256
 
    const int epoll_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
 
5674
    const int epoll_fd = open("/dev/null",
 
5675
                              O_WRONLY | O_CLOEXEC | O_NOCTTY);
5257
5676
  __attribute__((cleanup(cleanup_string)))
5258
5677
    char *const question_filename = strdup("/nonexistent/question");
5259
5678
  g_assert_nonnull(question_filename);
5539
5958
                                           test_fixture *fixture,
5540
5959
                                           __attribute__((unused))
5541
5960
                                           gconstpointer user_data){
 
5961
#ifndef __amd64__
 
5962
  g_test_skip("Skipping EMSGSIZE test on non-AMD64 platform");
 
5963
#else
5542
5964
  __attribute__((cleanup(cleanup_close)))
5543
5965
    const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
5544
5966
  g_assert_cmpint(epoll_fd, >=, 0);
5597
6019
  g_assert_cmpuint((unsigned int)queue->length, ==, 0);
5598
6020
  g_assert_true(string_set_contains(cancelled_filenames,
5599
6021
                                    question_filename));
 
6022
#endif
5600
6023
}
5601
6024
 
5602
6025
static void test_send_password_to_socket_retry(__attribute__((unused))
5663
6086
                                            __attribute__((unused))
5664
6087
                                            gconstpointer user_data){
5665
6088
  __attribute__((cleanup(cleanup_close)))
5666
 
    const int epoll_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
 
6089
    const int epoll_fd = open("/dev/null",
 
6090
                              O_WRONLY | O_CLOEXEC | O_NOCTTY);
5667
6091
  __attribute__((cleanup(cleanup_string)))
5668
6092
    char *const question_filename = strdup("/nonexistent/question");
5669
6093
  g_assert_nonnull(question_filename);
5932
6356
                                              const char *const
5933
6357
                                              dirname){
5934
6358
  __attribute__((cleanup(cleanup_close)))
5935
 
    const int devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
 
6359
    const int devnull_fd = open("/dev/null",
 
6360
                                O_WRONLY | O_CLOEXEC | O_NOCTTY);
5936
6361
  g_assert_cmpint(devnull_fd, >=, 0);
5937
6362
  __attribute__((cleanup(cleanup_close)))
5938
6363
    const int real_stderr_fd = dup(STDERR_FILENO);
7481
7906
  test_add("/parse_arguments/mixed", test_parse_arguments_mixed);
7482
7907
  test_add("/queue/create", test_create_queue);
7483
7908
  test_add("/queue/add", test_add_to_queue);
 
7909
  test_add("/queue/add/overflow", test_add_to_queue_overflow);
7484
7910
  test_add("/queue/has_question/empty",
7485
7911
           test_queue_has_question_empty);
7486
7912
  test_add("/queue/has_question/false",
7573
7999
              test_add_inotify_dir_watch);
7574
8000
  test_add_st("/task-creators/add_inotify_dir_watch/fail",
7575
8001
              test_add_inotify_dir_watch_fail);
 
8002
  test_add_st("/task-creators/add_inotify_dir_watch/not-a-directory",
 
8003
              test_add_inotify_dir_watch_nondir);
7576
8004
  test_add_st("/task-creators/add_inotify_dir_watch/EAGAIN",
7577
8005
              test_add_inotify_dir_watch_EAGAIN);
7578
8006
  test_add_st("/task-creators/add_inotify_dir_watch/IN_CLOSE_WRITE",
7579
8007
              test_add_inotify_dir_watch_IN_CLOSE_WRITE);
7580
8008
  test_add_st("/task-creators/add_inotify_dir_watch/IN_MOVED_TO",
7581
8009
              test_add_inotify_dir_watch_IN_MOVED_TO);
 
8010
  test_add_st("/task-creators/add_inotify_dir_watch/IN_MOVED_FROM",
 
8011
              test_add_inotify_dir_watch_IN_MOVED_FROM);
 
8012
  test_add_st("/task-creators/add_inotify_dir_watch/IN_EXCL_UNLINK",
 
8013
              test_add_inotify_dir_watch_IN_EXCL_UNLINK);
7582
8014
  test_add_st("/task-creators/add_inotify_dir_watch/IN_DELETE",
7583
8015
              test_add_inotify_dir_watch_IN_DELETE);
7584
8016
  test_add_st("/task/read_inotify_event/readerror",
7593
8025
              test_read_inotify_event_IN_CLOSE_WRITE);
7594
8026
  test_add_st("/task/read_inotify_event/IN_MOVED_TO",
7595
8027
              test_read_inotify_event_IN_MOVED_TO);
 
8028
  test_add_st("/task/read_inotify_event/IN_MOVED_FROM",
 
8029
              test_read_inotify_event_IN_MOVED_FROM);
7596
8030
  test_add_st("/task/read_inotify_event/IN_DELETE",
7597
8031
              test_read_inotify_event_IN_DELETE);
7598
8032
  test_add_st("/task/read_inotify_event/IN_CLOSE_WRITE/badname",
7599
8033
              test_read_inotify_event_IN_CLOSE_WRITE_badname);
7600
8034
  test_add_st("/task/read_inotify_event/IN_MOVED_TO/badname",
7601
8035
              test_read_inotify_event_IN_MOVED_TO_badname);
 
8036
  test_add_st("/task/read_inotify_event/IN_MOVED_FROM/badname",
 
8037
              test_read_inotify_event_IN_MOVED_FROM_badname);
7602
8038
  test_add_st("/task/read_inotify_event/IN_DELETE/badname",
7603
8039
              test_read_inotify_event_IN_DELETE_badname);
7604
8040
  test_add_st("/task/open_and_parse_question/ENOENT",
7701
8137
  g_option_context_set_help_enabled(context, FALSE);
7702
8138
  g_option_context_set_ignore_unknown_options(context, TRUE);
7703
8139
 
7704
 
  gboolean run_tests = FALSE;
 
8140
  gboolean should_run_tests = FALSE;
7705
8141
  GOptionEntry entries[] = {
7706
8142
    { "test", 0, 0, G_OPTION_ARG_NONE,
7707
 
      &run_tests, "Run tests", NULL },
 
8143
      &should_run_tests, "Run tests", NULL },
7708
8144
    { NULL }
7709
8145
  };
7710
8146
  g_option_context_add_main_entries(context, entries, NULL);
7717
8153
  }
7718
8154
 
7719
8155
  g_option_context_free(context);
7720
 
  return run_tests != FALSE;
 
8156
  return should_run_tests != FALSE;
7721
8157
}