/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 Hogeborn
  • Date: 2019-08-03 10:45:18 UTC
  • Revision ID: teddy@recompile.se-20190803104518-0jljs59cdw352atv
dracut-module/password-agent.c: Bug fix: Handle IN_MOVED_FROM

If a question file ("ask.*") is moved away from the question directory
or is renamed in it, treat this the same as IN_DELETE.  If it was a
simple rename within the question directory from, say, "ask.foo" to
"ask.bar", the separate IN_MOVED_TO ievent will get the "ask.bar" name
and add it, so we don't need to consider this as a special case.

* dracut-module/password-agent.c (add_inotify_dir_watch): Add
  "IN_MOVED_FROM" to flags.
  (read_inotify_event): Treat IN_MOVED_FROM the same as IN_DELETE.
  (test_add_inotify_dir_watch_IN_MOVED_FROM): New test.
  (test_read_inotify_event_IN_MOVED_FROM): - '' -
  (test_read_inotify_event_IN_MOVED_FROM_badname): - '' -
  (run_tests): Add new tests.

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, ENOTDIR,
52
 
                                   ENOMEM, EEXIST, ECHILD, EPERM,
53
 
                                   EAGAIN, EINTR, ENOBUFS, EADDRINUSE,
 
51
                                   ENAMETOOLONG, ENOENT, EEXIST,
 
52
                                   ECHILD, EPERM, ENOMEM, EAGAIN,
 
53
                                   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 */
77
76
#include <unistd.h>             /* uid_t, gid_t, close(), pipe2(),
78
77
                                   fork(), _exit(), dup2(),
79
78
                                   STDOUT_FILENO, setresgid(),
84
83
#include <sys/mman.h>           /* munlock(), mlock() */
85
84
#include <fcntl.h>              /* O_CLOEXEC, O_NONBLOCK, fcntl(),
86
85
                                   F_GETFD, F_GETFL, FD_CLOEXEC,
87
 
                                   open(), O_WRONLY, O_NOCTTY,
88
 
                                   O_RDONLY, O_NOFOLLOW */
 
86
                                   open(), O_WRONLY, O_RDONLY */
89
87
#include <sys/wait.h>           /* waitpid(), WNOHANG, WIFEXITED(),
90
88
                                   WEXITSTATUS() */
91
89
#include <limits.h>             /* PIPE_BUF, NAME_MAX, INT_MAX */
92
90
#include <sys/inotify.h>        /* inotify_init1(), IN_NONBLOCK,
93
91
                                   IN_CLOEXEC, inotify_add_watch(),
94
92
                                   IN_CLOSE_WRITE, IN_MOVED_TO,
95
 
                                   IN_MOVED_FROM, IN_DELETE,
96
 
                                   IN_EXCL_UNLINK, IN_ONLYDIR,
97
 
                                   struct inotify_event */
 
93
                                   IN_DELETE, struct inotify_event */
98
94
#include <fnmatch.h>            /* fnmatch(), FNM_FILE_NAME */
99
 
#include <stdio.h>              /* asprintf(), FILE, stderr, fopen(),
100
 
                                   fclose(), getline(), sscanf(),
101
 
                                   feof(), ferror(), rename(),
102
 
                                   fdopen(), fprintf(), fscanf() */
 
95
#include <stdio.h>              /* asprintf(), FILE, fopen(),
 
96
                                   getline(), sscanf(), feof(),
 
97
                                   ferror(), fclose(), stderr,
 
98
                                   rename(), fdopen(), fprintf(),
 
99
                                   fscanf() */
103
100
#include <glib.h>    /* GKeyFile, g_key_file_free(), g_key_file_new(),
104
101
                        GError, g_key_file_load_from_file(),
105
102
                        G_KEY_FILE_NONE, TRUE, G_FILE_ERROR_NOENT,
148
145
  mono_microsecs next_run;
149
146
} __attribute__((designated_init)) task_queue;
150
147
 
151
 
/* "task_func" - A function type for task functions
 
148
/* "func_type" - A function type for task functions
152
149
 
153
150
   I.e. functions for the code which runs when a task is run, all have
154
151
   this type */
434
431
    case EACCES:
435
432
    case ENAMETOOLONG:
436
433
    case ENOENT:
437
 
    case ENOTDIR:
438
434
      return EX_OSFILE;
439
435
    default:
440
436
      return EX_OSERR;
651
647
 
652
648
__attribute__((nonnull, warn_unused_result))
653
649
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
 
  }
661
650
  const size_t needed_size = sizeof(task_context)*(queue->length + 1);
662
651
  if(needed_size > (queue->allocated)){
663
652
    task_context *const new_tasks = realloc(queue->tasks,
1029
1018
  }
1030
1019
 
1031
1020
  if(inotify_add_watch(fd, dir, IN_CLOSE_WRITE | IN_MOVED_TO
1032
 
                       | IN_MOVED_FROM| IN_DELETE | IN_EXCL_UNLINK
1033
 
                       | IN_ONLYDIR)
 
1021
                       | IN_MOVED_FROM| IN_DELETE)
1034
1022
     == -1){
1035
1023
    error(0, errno, "Failed to create inotify watch on %s", dir);
1036
1024
    return false;
1891
1879
  g_assert_true(queue->tasks[0].func == dummy_func);
1892
1880
}
1893
1881
 
1894
 
static void test_add_to_queue_overflow(__attribute__((unused))
1895
 
                                       test_fixture *fixture,
1896
 
                                       __attribute__((unused))
1897
 
                                       gconstpointer user_data){
1898
 
  __attribute__((cleanup(cleanup_queue)))
1899
 
    task_queue *queue = create_queue();
1900
 
  g_assert_nonnull(queue);
1901
 
  g_assert_true(queue->length == 0);
1902
 
  queue->length = SIZE_MAX / sizeof(task_context); /* fake max size */
1903
 
 
1904
 
  FILE *real_stderr = stderr;
1905
 
  FILE *devnull = fopen("/dev/null", "we");
1906
 
  g_assert_nonnull(devnull);
1907
 
  stderr = devnull;
1908
 
  const bool ret = add_to_queue(queue,
1909
 
                                (task_context){ .func=dummy_func });
1910
 
  g_assert_true(errno == ENOMEM);
1911
 
  g_assert_false(ret);
1912
 
  stderr = real_stderr;
1913
 
  g_assert_cmpint(fclose(devnull), ==, 0);
1914
 
  queue->length = 0;            /* Restore real size */
1915
 
}
1916
 
 
1917
1882
static void dummy_func(__attribute__((unused))
1918
1883
                       const task_context task,
1919
1884
                       __attribute__((unused))
2257
2222
 
2258
2223
  {
2259
2224
    __attribute__((cleanup(cleanup_close)))
2260
 
      const int devnull_fd = open("/dev/null",
2261
 
                                  O_WRONLY | O_CLOEXEC | O_NOCTTY);
 
2225
      const int devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
2262
2226
    g_assert_cmpint(devnull_fd, >=, 0);
2263
2227
    __attribute__((cleanup(cleanup_close)))
2264
2228
      const int real_stderr_fd = dup(STDERR_FILENO);
2288
2252
    {
2289
2253
      __attribute__((cleanup(cleanup_close)))
2290
2254
        const int devnull_fd = open("/dev/null",
2291
 
                                    O_WRONLY | O_CLOEXEC | O_NOCTTY);
 
2255
                                    O_WRONLY | O_CLOEXEC);
2292
2256
      g_assert_cmpint(devnull_fd, >=, 0);
2293
2257
      __attribute__((cleanup(cleanup_close)))
2294
2258
        const int real_stderr_fd = dup(STDERR_FILENO);
2939
2903
 
2940
2904
  __attribute__((cleanup(cleanup_close)))
2941
2905
    const int devnull_fd = open("/dev/null",
2942
 
                                O_WRONLY | O_CLOEXEC | O_NOCTTY);
 
2906
                                O_WRONLY | O_CLOEXEC);
2943
2907
  g_assert_cmpint(devnull_fd, >=, 0);
2944
2908
  __attribute__((cleanup(cleanup_close)))
2945
2909
    const int real_stderr_fd = dup(STDERR_FILENO);
3010
2974
 
3011
2975
  __attribute__((cleanup(cleanup_close)))
3012
2976
    const int devnull_fd = open("/dev/null",
3013
 
                                O_WRONLY | O_CLOEXEC, O_NOCTTY);
 
2977
                                O_WRONLY | O_CLOEXEC);
3014
2978
  g_assert_cmpint(devnull_fd, >=, 0);
3015
2979
  __attribute__((cleanup(cleanup_close)))
3016
2980
    const int real_stderr_fd = dup(STDERR_FILENO);
3054
3018
    buffer password = {};
3055
3019
 
3056
3020
  /* Reading /proc/self/mem from offset 0 will always give EIO */
3057
 
  const int fd = open("/proc/self/mem",
3058
 
                      O_RDONLY | O_CLOEXEC | O_NOCTTY);
 
3021
  const int fd = open("/proc/self/mem", O_RDONLY | O_CLOEXEC);
3059
3022
 
3060
3023
  bool password_is_read = false;
3061
3024
  bool quit_now = false;
3489
3452
  g_assert_cmpuint((unsigned int)queue->length, ==, 0);
3490
3453
}
3491
3454
 
3492
 
static void test_add_inotify_dir_watch_nondir(__attribute__((unused))
3493
 
                                              test_fixture *fixture,
3494
 
                                            __attribute__((unused))
3495
 
                                              gconstpointer
3496
 
                                              user_data){
3497
 
  __attribute__((cleanup(cleanup_close)))
3498
 
    const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3499
 
  g_assert_cmpint(epoll_fd, >=, 0);
3500
 
  __attribute__((cleanup(cleanup_queue)))
3501
 
    task_queue *queue = create_queue();
3502
 
  g_assert_nonnull(queue);
3503
 
  __attribute__((cleanup(string_set_clear)))
3504
 
    string_set cancelled_filenames = {};
3505
 
  const mono_microsecs current_time = 0;
3506
 
 
3507
 
  bool quit_now = false;
3508
 
  buffer password = {};
3509
 
  bool mandos_client_exited = false;
3510
 
  bool password_is_read = false;
3511
 
 
3512
 
  const char not_a_directory[] = "/dev/tty";
3513
 
 
3514
 
  FILE *real_stderr = stderr;
3515
 
  FILE *devnull = fopen("/dev/null", "we");
3516
 
  g_assert_nonnull(devnull);
3517
 
  stderr = devnull;
3518
 
  g_assert_false(add_inotify_dir_watch(queue, epoll_fd, &quit_now,
3519
 
                                       &password, not_a_directory,
3520
 
                                       &cancelled_filenames,
3521
 
                                       &current_time,
3522
 
                                       &mandos_client_exited,
3523
 
                                       &password_is_read));
3524
 
  stderr = real_stderr;
3525
 
  g_assert_cmpint(fclose(devnull), ==, 0);
3526
 
 
3527
 
  g_assert_cmpuint((unsigned int)queue->length, ==, 0);
3528
 
}
3529
 
 
3530
3455
static void test_add_inotify_dir_watch_EAGAIN(__attribute__((unused))
3531
3456
                                              test_fixture *fixture,
3532
3457
                                              __attribute__((unused))
3882
3807
  g_assert_cmpint(rmdir(tempdir), ==, 0);
3883
3808
}
3884
3809
 
3885
 
static
3886
 
void test_add_inotify_dir_watch_IN_EXCL_UNLINK(__attribute__((unused))
3887
 
                                               test_fixture *fixture,
3888
 
                                               __attribute__((unused))
3889
 
                                               gconstpointer
3890
 
                                               user_data){
3891
 
  __attribute__((cleanup(cleanup_close)))
3892
 
    const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3893
 
  g_assert_cmpint(epoll_fd, >=, 0);
3894
 
  __attribute__((cleanup(cleanup_queue)))
3895
 
    task_queue *queue = create_queue();
3896
 
  g_assert_nonnull(queue);
3897
 
  __attribute__((cleanup(string_set_clear)))
3898
 
    string_set cancelled_filenames = {};
3899
 
  const mono_microsecs current_time = 0;
3900
 
 
3901
 
  bool quit_now = false;
3902
 
  buffer password = {};
3903
 
  bool mandos_client_exited = false;
3904
 
  bool password_is_read = false;
3905
 
 
3906
 
  __attribute__((cleanup(cleanup_string)))
3907
 
    char *tempdir = make_temporary_directory();
3908
 
  g_assert_nonnull(tempdir);
3909
 
 
3910
 
  __attribute__((cleanup(cleanup_string)))
3911
 
    char *tempfile = make_temporary_file_in_directory(tempdir);
3912
 
  g_assert_nonnull(tempfile);
3913
 
  int tempfile_fd = open(tempfile, O_WRONLY | O_CLOEXEC | O_NOCTTY
3914
 
                         | O_NOFOLLOW);
3915
 
  g_assert_cmpint(tempfile_fd, >, 2);
3916
 
 
3917
 
  g_assert_true(add_inotify_dir_watch(queue, epoll_fd, &quit_now,
3918
 
                                      &password, tempdir,
3919
 
                                      &cancelled_filenames,
3920
 
                                      &current_time,
3921
 
                                      &mandos_client_exited,
3922
 
                                      &password_is_read));
3923
 
  g_assert_cmpint(unlink(tempfile), ==, 0);
3924
 
 
3925
 
  g_assert_cmpuint((unsigned int)queue->length, >, 0);
3926
 
 
3927
 
  const task_context *const added_read_task
3928
 
    = find_matching_task(queue,
3929
 
                         (task_context){ .func=read_inotify_event });
3930
 
  g_assert_nonnull(added_read_task);
3931
 
 
3932
 
  g_assert_cmpint(added_read_task->fd, >, 2);
3933
 
  g_assert_true(fd_has_cloexec_and_nonblock(added_read_task->fd));
3934
 
 
3935
 
  /* "sufficient to read at least one event." - inotify(7) */
3936
 
  const size_t ievent_size = (sizeof(struct inotify_event)
3937
 
                              + NAME_MAX + 1);
3938
 
  struct inotify_event *ievent = malloc(ievent_size);
3939
 
  g_assert_nonnull(ievent);
3940
 
 
3941
 
  ssize_t read_size = 0;
3942
 
  read_size = read(added_read_task->fd, ievent, ievent_size);
3943
 
 
3944
 
  g_assert_cmpint((int)read_size, >, 0);
3945
 
  g_assert_true(ievent->mask & IN_DELETE);
3946
 
  g_assert_cmpstr(ievent->name, ==, basename(tempfile));
3947
 
 
3948
 
  g_assert_cmpint(close(tempfile_fd), ==, 0);
3949
 
 
3950
 
  /* IN_EXCL_UNLINK should make the closing of the previously unlinked
3951
 
     file not appear as an ievent, so we should not see it now. */
3952
 
  read_size = read(added_read_task->fd, ievent, ievent_size);
3953
 
  g_assert_cmpint((int)read_size, ==, -1);
3954
 
  g_assert_true(errno == EAGAIN);
3955
 
 
3956
 
  free(ievent);
3957
 
 
3958
 
  g_assert_cmpint(rmdir(tempdir), ==, 0);
3959
 
}
3960
 
 
3961
3810
static void test_read_inotify_event_readerror(__attribute__((unused))
3962
3811
                                              test_fixture *fixture,
3963
3812
                                              __attribute__((unused))
3969
3818
  const mono_microsecs current_time = 0;
3970
3819
 
3971
3820
  /* Reading /proc/self/mem from offset 0 will always result in EIO */
3972
 
  const int fd = open("/proc/self/mem",
3973
 
                      O_RDONLY | O_CLOEXEC | O_NOCTTY);
 
3821
  const int fd = open("/proc/self/mem", O_RDONLY | O_CLOEXEC);
3974
3822
 
3975
3823
  bool quit_now = false;
3976
3824
  __attribute__((cleanup(cleanup_queue)))
5661
5509
                                            __attribute__((unused))
5662
5510
                                            gconstpointer user_data){
5663
5511
  __attribute__((cleanup(cleanup_close)))
5664
 
    const int epoll_fd = open("/dev/null",
5665
 
                              O_WRONLY | O_CLOEXEC | O_NOCTTY);
 
5512
    const int epoll_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
5666
5513
  __attribute__((cleanup(cleanup_string)))
5667
5514
    char *const question_filename = strdup("/nonexistent/question");
5668
5515
  g_assert_nonnull(question_filename);
6072
5919
                                            __attribute__((unused))
6073
5920
                                            gconstpointer user_data){
6074
5921
  __attribute__((cleanup(cleanup_close)))
6075
 
    const int epoll_fd = open("/dev/null",
6076
 
                              O_WRONLY | O_CLOEXEC | O_NOCTTY);
 
5922
    const int epoll_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
6077
5923
  __attribute__((cleanup(cleanup_string)))
6078
5924
    char *const question_filename = strdup("/nonexistent/question");
6079
5925
  g_assert_nonnull(question_filename);
6342
6188
                                              const char *const
6343
6189
                                              dirname){
6344
6190
  __attribute__((cleanup(cleanup_close)))
6345
 
    const int devnull_fd = open("/dev/null",
6346
 
                                O_WRONLY | O_CLOEXEC | O_NOCTTY);
 
6191
    const int devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
6347
6192
  g_assert_cmpint(devnull_fd, >=, 0);
6348
6193
  __attribute__((cleanup(cleanup_close)))
6349
6194
    const int real_stderr_fd = dup(STDERR_FILENO);
7892
7737
  test_add("/parse_arguments/mixed", test_parse_arguments_mixed);
7893
7738
  test_add("/queue/create", test_create_queue);
7894
7739
  test_add("/queue/add", test_add_to_queue);
7895
 
  test_add("/queue/add/overflow", test_add_to_queue_overflow);
7896
7740
  test_add("/queue/has_question/empty",
7897
7741
           test_queue_has_question_empty);
7898
7742
  test_add("/queue/has_question/false",
7985
7829
              test_add_inotify_dir_watch);
7986
7830
  test_add_st("/task-creators/add_inotify_dir_watch/fail",
7987
7831
              test_add_inotify_dir_watch_fail);
7988
 
  test_add_st("/task-creators/add_inotify_dir_watch/not-a-directory",
7989
 
              test_add_inotify_dir_watch_nondir);
7990
7832
  test_add_st("/task-creators/add_inotify_dir_watch/EAGAIN",
7991
7833
              test_add_inotify_dir_watch_EAGAIN);
7992
7834
  test_add_st("/task-creators/add_inotify_dir_watch/IN_CLOSE_WRITE",
7995
7837
              test_add_inotify_dir_watch_IN_MOVED_TO);
7996
7838
  test_add_st("/task-creators/add_inotify_dir_watch/IN_MOVED_FROM",
7997
7839
              test_add_inotify_dir_watch_IN_MOVED_FROM);
7998
 
  test_add_st("/task-creators/add_inotify_dir_watch/IN_EXCL_UNLINK",
7999
 
              test_add_inotify_dir_watch_IN_EXCL_UNLINK);
8000
7840
  test_add_st("/task-creators/add_inotify_dir_watch/IN_DELETE",
8001
7841
              test_add_inotify_dir_watch_IN_DELETE);
8002
7842
  test_add_st("/task/read_inotify_event/readerror",
8123
7963
  g_option_context_set_help_enabled(context, FALSE);
8124
7964
  g_option_context_set_ignore_unknown_options(context, TRUE);
8125
7965
 
8126
 
  gboolean should_run_tests = FALSE;
 
7966
  gboolean run_tests = FALSE;
8127
7967
  GOptionEntry entries[] = {
8128
7968
    { "test", 0, 0, G_OPTION_ARG_NONE,
8129
 
      &should_run_tests, "Run tests", NULL },
 
7969
      &run_tests, "Run tests", NULL },
8130
7970
    { NULL }
8131
7971
  };
8132
7972
  g_option_context_add_main_entries(context, entries, NULL);
8139
7979
  }
8140
7980
 
8141
7981
  g_option_context_free(context);
8142
 
  return should_run_tests != FALSE;
 
7982
  return run_tests != FALSE;
8143
7983
}