/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 11:52:41 UTC
  • Revision ID: teddy@recompile.se-20190803115241-oe9726lcojsaij33
dracut-module/password-agent.c: Require agent directory

Require the --agent-directory (by default "/run/systemd/ask-password")
to be an actual directory; fail otherwise.

* dracut-module/password-agent.c (main): Make ENOTDIR from
  add_inotify_dir_watch() result in EX_OSFILE.
  (add_inotify_dir_watch): Add "IN_ONLYDIR" flag to
                           inotify_add_watch().
  (test_add_inotify_dir_watch_nondir): New test.
  (run_tests): Add new test.

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 */
651
648
 
652
649
__attribute__((nonnull, warn_unused_result))
653
650
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
651
  const size_t needed_size = sizeof(task_context)*(queue->length + 1);
662
652
  if(needed_size > (queue->allocated)){
663
653
    task_context *const new_tasks = realloc(queue->tasks,
868
858
  }
869
859
  close(pipefds[1]);
870
860
 
871
 
  if(pid == -1){
872
 
    error(0, errno, "Failed to fork()");
873
 
    close(pipefds[0]);
874
 
    return false;
875
 
  }
876
 
 
877
861
  if(not add_to_queue(queue, (task_context){
878
862
        .func=wait_for_mandos_client_exit,
879
863
        .pid=pid,
1897
1881
  g_assert_true(queue->tasks[0].func == dummy_func);
1898
1882
}
1899
1883
 
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
 
 
1923
1884
static void dummy_func(__attribute__((unused))
1924
1885
                       const task_context task,
1925
1886
                       __attribute__((unused))
2196
2157
    }
2197
2158
    exit(EXIT_SUCCESS);
2198
2159
  }
2199
 
  if(pid == -1){
2200
 
    error(EXIT_FAILURE, errno, "Failed to fork()");
2201
 
  }
2202
 
 
2203
2160
  int status;
2204
2161
  waitpid(pid, &status, 0);
2205
2162
  if(WIFEXITED(status) and (WEXITSTATUS(status) == EXIT_SUCCESS)){
2267
2224
 
2268
2225
  {
2269
2226
    __attribute__((cleanup(cleanup_close)))
2270
 
      const int devnull_fd = open("/dev/null",
2271
 
                                  O_WRONLY | O_CLOEXEC | O_NOCTTY);
 
2227
      const int devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
2272
2228
    g_assert_cmpint(devnull_fd, >=, 0);
2273
2229
    __attribute__((cleanup(cleanup_close)))
2274
2230
      const int real_stderr_fd = dup(STDERR_FILENO);
2298
2254
    {
2299
2255
      __attribute__((cleanup(cleanup_close)))
2300
2256
        const int devnull_fd = open("/dev/null",
2301
 
                                    O_WRONLY | O_CLOEXEC | O_NOCTTY);
 
2257
                                    O_WRONLY | O_CLOEXEC);
2302
2258
      g_assert_cmpint(devnull_fd, >=, 0);
2303
2259
      __attribute__((cleanup(cleanup_close)))
2304
2260
        const int real_stderr_fd = dup(STDERR_FILENO);
2949
2905
 
2950
2906
  __attribute__((cleanup(cleanup_close)))
2951
2907
    const int devnull_fd = open("/dev/null",
2952
 
                                O_WRONLY | O_CLOEXEC | O_NOCTTY);
 
2908
                                O_WRONLY | O_CLOEXEC);
2953
2909
  g_assert_cmpint(devnull_fd, >=, 0);
2954
2910
  __attribute__((cleanup(cleanup_close)))
2955
2911
    const int real_stderr_fd = dup(STDERR_FILENO);
3020
2976
 
3021
2977
  __attribute__((cleanup(cleanup_close)))
3022
2978
    const int devnull_fd = open("/dev/null",
3023
 
                                O_WRONLY | O_CLOEXEC, O_NOCTTY);
 
2979
                                O_WRONLY | O_CLOEXEC);
3024
2980
  g_assert_cmpint(devnull_fd, >=, 0);
3025
2981
  __attribute__((cleanup(cleanup_close)))
3026
2982
    const int real_stderr_fd = dup(STDERR_FILENO);
3064
3020
    buffer password = {};
3065
3021
 
3066
3022
  /* Reading /proc/self/mem from offset 0 will always give EIO */
3067
 
  const int fd = open("/proc/self/mem",
3068
 
                      O_RDONLY | O_CLOEXEC | O_NOCTTY);
 
3023
  const int fd = open("/proc/self/mem", O_RDONLY | O_CLOEXEC);
3069
3024
 
3070
3025
  bool password_is_read = false;
3071
3026
  bool quit_now = false;
3979
3934
  const mono_microsecs current_time = 0;
3980
3935
 
3981
3936
  /* Reading /proc/self/mem from offset 0 will always result in EIO */
3982
 
  const int fd = open("/proc/self/mem",
3983
 
                      O_RDONLY | O_CLOEXEC | O_NOCTTY);
 
3937
  const int fd = open("/proc/self/mem", O_RDONLY | O_CLOEXEC);
3984
3938
 
3985
3939
  bool quit_now = false;
3986
3940
  __attribute__((cleanup(cleanup_queue)))
5671
5625
                                            __attribute__((unused))
5672
5626
                                            gconstpointer user_data){
5673
5627
  __attribute__((cleanup(cleanup_close)))
5674
 
    const int epoll_fd = open("/dev/null",
5675
 
                              O_WRONLY | O_CLOEXEC | O_NOCTTY);
 
5628
    const int epoll_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
5676
5629
  __attribute__((cleanup(cleanup_string)))
5677
5630
    char *const question_filename = strdup("/nonexistent/question");
5678
5631
  g_assert_nonnull(question_filename);
5958
5911
                                           test_fixture *fixture,
5959
5912
                                           __attribute__((unused))
5960
5913
                                           gconstpointer user_data){
5961
 
#ifndef __amd64__
5962
 
  g_test_skip("Skipping EMSGSIZE test on non-AMD64 platform");
5963
 
#else
5964
5914
  __attribute__((cleanup(cleanup_close)))
5965
5915
    const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
5966
5916
  g_assert_cmpint(epoll_fd, >=, 0);
6019
5969
  g_assert_cmpuint((unsigned int)queue->length, ==, 0);
6020
5970
  g_assert_true(string_set_contains(cancelled_filenames,
6021
5971
                                    question_filename));
6022
 
#endif
6023
5972
}
6024
5973
 
6025
5974
static void test_send_password_to_socket_retry(__attribute__((unused))
6086
6035
                                            __attribute__((unused))
6087
6036
                                            gconstpointer user_data){
6088
6037
  __attribute__((cleanup(cleanup_close)))
6089
 
    const int epoll_fd = open("/dev/null",
6090
 
                              O_WRONLY | O_CLOEXEC | O_NOCTTY);
 
6038
    const int epoll_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
6091
6039
  __attribute__((cleanup(cleanup_string)))
6092
6040
    char *const question_filename = strdup("/nonexistent/question");
6093
6041
  g_assert_nonnull(question_filename);
6356
6304
                                              const char *const
6357
6305
                                              dirname){
6358
6306
  __attribute__((cleanup(cleanup_close)))
6359
 
    const int devnull_fd = open("/dev/null",
6360
 
                                O_WRONLY | O_CLOEXEC | O_NOCTTY);
 
6307
    const int devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
6361
6308
  g_assert_cmpint(devnull_fd, >=, 0);
6362
6309
  __attribute__((cleanup(cleanup_close)))
6363
6310
    const int real_stderr_fd = dup(STDERR_FILENO);
7906
7853
  test_add("/parse_arguments/mixed", test_parse_arguments_mixed);
7907
7854
  test_add("/queue/create", test_create_queue);
7908
7855
  test_add("/queue/add", test_add_to_queue);
7909
 
  test_add("/queue/add/overflow", test_add_to_queue_overflow);
7910
7856
  test_add("/queue/has_question/empty",
7911
7857
           test_queue_has_question_empty);
7912
7858
  test_add("/queue/has_question/false",
8137
8083
  g_option_context_set_help_enabled(context, FALSE);
8138
8084
  g_option_context_set_ignore_unknown_options(context, TRUE);
8139
8085
 
8140
 
  gboolean should_run_tests = FALSE;
 
8086
  gboolean run_tests = FALSE;
8141
8087
  GOptionEntry entries[] = {
8142
8088
    { "test", 0, 0, G_OPTION_ARG_NONE,
8143
 
      &should_run_tests, "Run tests", NULL },
 
8089
      &run_tests, "Run tests", NULL },
8144
8090
    { NULL }
8145
8091
  };
8146
8092
  g_option_context_add_main_entries(context, entries, NULL);
8153
8099
  }
8154
8100
 
8155
8101
  g_option_context_free(context);
8156
 
  return should_run_tests != FALSE;
 
8102
  return run_tests != FALSE;
8157
8103
}