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

  • Committer: teddy at recompile
  • Date: 2020-08-14 20:34:56 UTC
  • mto: This revision was merged to the branch mainline in revision 402.
  • Revision ID: teddy@recompile.se-20200814203456-m424np3qh2aaepaq
mandos: Shorten or break long lines

* mandos: Shorten or break long lines.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Mandos password agent - Simple password agent to run Mandos client
4
4
 *
5
 
 * Copyright © 2019 Teddy Hogeborn
6
 
 * Copyright © 2019 Björn Påhlsson
 
5
 * Copyright © 2019-2020 Teddy Hogeborn
 
6
 * Copyright © 2019-2020 Björn Påhlsson
7
7
 * 
8
8
 * This file is part of Mandos.
9
9
 * 
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,
1018
1035
  }
1019
1036
 
1020
1037
  if(inotify_add_watch(fd, dir, IN_CLOSE_WRITE | IN_MOVED_TO
1021
 
                       | IN_MOVED_FROM| IN_DELETE)
 
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;
1879
1897
  g_assert_true(queue->tasks[0].func == dummy_func);
1880
1898
}
1881
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
 
1882
1923
static void dummy_func(__attribute__((unused))
1883
1924
                       const task_context task,
1884
1925
                       __attribute__((unused))
2155
2196
    }
2156
2197
    exit(EXIT_SUCCESS);
2157
2198
  }
 
2199
  if(pid == -1){
 
2200
    error(EXIT_FAILURE, errno, "Failed to fork()");
 
2201
  }
 
2202
 
2158
2203
  int status;
2159
2204
  waitpid(pid, &status, 0);
2160
2205
  if(WIFEXITED(status) and (WEXITSTATUS(status) == EXIT_SUCCESS)){
2222
2267
 
2223
2268
  {
2224
2269
    __attribute__((cleanup(cleanup_close)))
2225
 
      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);
2226
2272
    g_assert_cmpint(devnull_fd, >=, 0);
2227
2273
    __attribute__((cleanup(cleanup_close)))
2228
2274
      const int real_stderr_fd = dup(STDERR_FILENO);
2252
2298
    {
2253
2299
      __attribute__((cleanup(cleanup_close)))
2254
2300
        const int devnull_fd = open("/dev/null",
2255
 
                                    O_WRONLY | O_CLOEXEC);
 
2301
                                    O_WRONLY | O_CLOEXEC | O_NOCTTY);
2256
2302
      g_assert_cmpint(devnull_fd, >=, 0);
2257
2303
      __attribute__((cleanup(cleanup_close)))
2258
2304
        const int real_stderr_fd = dup(STDERR_FILENO);
2903
2949
 
2904
2950
  __attribute__((cleanup(cleanup_close)))
2905
2951
    const int devnull_fd = open("/dev/null",
2906
 
                                O_WRONLY | O_CLOEXEC);
 
2952
                                O_WRONLY | O_CLOEXEC | O_NOCTTY);
2907
2953
  g_assert_cmpint(devnull_fd, >=, 0);
2908
2954
  __attribute__((cleanup(cleanup_close)))
2909
2955
    const int real_stderr_fd = dup(STDERR_FILENO);
2974
3020
 
2975
3021
  __attribute__((cleanup(cleanup_close)))
2976
3022
    const int devnull_fd = open("/dev/null",
2977
 
                                O_WRONLY | O_CLOEXEC);
 
3023
                                O_WRONLY | O_CLOEXEC, O_NOCTTY);
2978
3024
  g_assert_cmpint(devnull_fd, >=, 0);
2979
3025
  __attribute__((cleanup(cleanup_close)))
2980
3026
    const int real_stderr_fd = dup(STDERR_FILENO);
3018
3064
    buffer password = {};
3019
3065
 
3020
3066
  /* Reading /proc/self/mem from offset 0 will always give EIO */
3021
 
  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);
3022
3069
 
3023
3070
  bool password_is_read = false;
3024
3071
  bool quit_now = false;
3452
3499
  g_assert_cmpuint((unsigned int)queue->length, ==, 0);
3453
3500
}
3454
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
 
3455
3540
static void test_add_inotify_dir_watch_EAGAIN(__attribute__((unused))
3456
3541
                                              test_fixture *fixture,
3457
3542
                                              __attribute__((unused))
3807
3892
  g_assert_cmpint(rmdir(tempdir), ==, 0);
3808
3893
}
3809
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
 
3810
3971
static void test_read_inotify_event_readerror(__attribute__((unused))
3811
3972
                                              test_fixture *fixture,
3812
3973
                                              __attribute__((unused))
3818
3979
  const mono_microsecs current_time = 0;
3819
3980
 
3820
3981
  /* Reading /proc/self/mem from offset 0 will always result in EIO */
3821
 
  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);
3822
3984
 
3823
3985
  bool quit_now = false;
3824
3986
  __attribute__((cleanup(cleanup_queue)))
5509
5671
                                            __attribute__((unused))
5510
5672
                                            gconstpointer user_data){
5511
5673
  __attribute__((cleanup(cleanup_close)))
5512
 
    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);
5513
5676
  __attribute__((cleanup(cleanup_string)))
5514
5677
    char *const question_filename = strdup("/nonexistent/question");
5515
5678
  g_assert_nonnull(question_filename);
5795
5958
                                           test_fixture *fixture,
5796
5959
                                           __attribute__((unused))
5797
5960
                                           gconstpointer user_data){
 
5961
#ifndef __amd64__
 
5962
  g_test_skip("Skipping EMSGSIZE test on non-AMD64 platform");
 
5963
#else
5798
5964
  __attribute__((cleanup(cleanup_close)))
5799
5965
    const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
5800
5966
  g_assert_cmpint(epoll_fd, >=, 0);
5853
6019
  g_assert_cmpuint((unsigned int)queue->length, ==, 0);
5854
6020
  g_assert_true(string_set_contains(cancelled_filenames,
5855
6021
                                    question_filename));
 
6022
#endif
5856
6023
}
5857
6024
 
5858
6025
static void test_send_password_to_socket_retry(__attribute__((unused))
5919
6086
                                            __attribute__((unused))
5920
6087
                                            gconstpointer user_data){
5921
6088
  __attribute__((cleanup(cleanup_close)))
5922
 
    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);
5923
6091
  __attribute__((cleanup(cleanup_string)))
5924
6092
    char *const question_filename = strdup("/nonexistent/question");
5925
6093
  g_assert_nonnull(question_filename);
6188
6356
                                              const char *const
6189
6357
                                              dirname){
6190
6358
  __attribute__((cleanup(cleanup_close)))
6191
 
    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);
6192
6361
  g_assert_cmpint(devnull_fd, >=, 0);
6193
6362
  __attribute__((cleanup(cleanup_close)))
6194
6363
    const int real_stderr_fd = dup(STDERR_FILENO);
7737
7906
  test_add("/parse_arguments/mixed", test_parse_arguments_mixed);
7738
7907
  test_add("/queue/create", test_create_queue);
7739
7908
  test_add("/queue/add", test_add_to_queue);
 
7909
  test_add("/queue/add/overflow", test_add_to_queue_overflow);
7740
7910
  test_add("/queue/has_question/empty",
7741
7911
           test_queue_has_question_empty);
7742
7912
  test_add("/queue/has_question/false",
7829
7999
              test_add_inotify_dir_watch);
7830
8000
  test_add_st("/task-creators/add_inotify_dir_watch/fail",
7831
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);
7832
8004
  test_add_st("/task-creators/add_inotify_dir_watch/EAGAIN",
7833
8005
              test_add_inotify_dir_watch_EAGAIN);
7834
8006
  test_add_st("/task-creators/add_inotify_dir_watch/IN_CLOSE_WRITE",
7837
8009
              test_add_inotify_dir_watch_IN_MOVED_TO);
7838
8010
  test_add_st("/task-creators/add_inotify_dir_watch/IN_MOVED_FROM",
7839
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);
7840
8014
  test_add_st("/task-creators/add_inotify_dir_watch/IN_DELETE",
7841
8015
              test_add_inotify_dir_watch_IN_DELETE);
7842
8016
  test_add_st("/task/read_inotify_event/readerror",
7963
8137
  g_option_context_set_help_enabled(context, FALSE);
7964
8138
  g_option_context_set_ignore_unknown_options(context, TRUE);
7965
8139
 
7966
 
  gboolean run_tests = FALSE;
 
8140
  gboolean should_run_tests = FALSE;
7967
8141
  GOptionEntry entries[] = {
7968
8142
    { "test", 0, 0, G_OPTION_ARG_NONE,
7969
 
      &run_tests, "Run tests", NULL },
 
8143
      &should_run_tests, "Run tests", NULL },
7970
8144
    { NULL }
7971
8145
  };
7972
8146
  g_option_context_add_main_entries(context, entries, NULL);
7979
8153
  }
7980
8154
 
7981
8155
  g_option_context_free(context);
7982
 
  return run_tests != FALSE;
 
8156
  return should_run_tests != FALSE;
7983
8157
}