/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-02-09 03:38:33 UTC
  • Revision ID: teddy@recompile.se-20200209033833-2la1pujrnv2m0so4
Use reallocarray() if available, or check for overflow

* dracut-module/password-agent.c (add_to_queue): Check for overflow.
  (test_add_to_queue_overflow): New test.
* plugin-runner.c (add_to_char_array, main): Use reallocarray().
* plugins.d/plymouth.c (exec_and_wait): - '' -

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,
1018
1029
  }
1019
1030
 
1020
1031
  if(inotify_add_watch(fd, dir, IN_CLOSE_WRITE | IN_MOVED_TO
1021
 
                       | IN_MOVED_FROM| IN_DELETE | IN_EXCL_UNLINK)
 
1032
                       | IN_MOVED_FROM| IN_DELETE | IN_EXCL_UNLINK
 
1033
                       | IN_ONLYDIR)
1022
1034
     == -1){
1023
1035
    error(0, errno, "Failed to create inotify watch on %s", dir);
1024
1036
    return false;
1879
1891
  g_assert_true(queue->tasks[0].func == dummy_func);
1880
1892
}
1881
1893
 
 
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
 
1882
1917
static void dummy_func(__attribute__((unused))
1883
1918
                       const task_context task,
1884
1919
                       __attribute__((unused))
2222
2257
 
2223
2258
  {
2224
2259
    __attribute__((cleanup(cleanup_close)))
2225
 
      const int devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
 
2260
      const int devnull_fd = open("/dev/null",
 
2261
                                  O_WRONLY | O_CLOEXEC | O_NOCTTY);
2226
2262
    g_assert_cmpint(devnull_fd, >=, 0);
2227
2263
    __attribute__((cleanup(cleanup_close)))
2228
2264
      const int real_stderr_fd = dup(STDERR_FILENO);
2252
2288
    {
2253
2289
      __attribute__((cleanup(cleanup_close)))
2254
2290
        const int devnull_fd = open("/dev/null",
2255
 
                                    O_WRONLY | O_CLOEXEC);
 
2291
                                    O_WRONLY | O_CLOEXEC | O_NOCTTY);
2256
2292
      g_assert_cmpint(devnull_fd, >=, 0);
2257
2293
      __attribute__((cleanup(cleanup_close)))
2258
2294
        const int real_stderr_fd = dup(STDERR_FILENO);
2903
2939
 
2904
2940
  __attribute__((cleanup(cleanup_close)))
2905
2941
    const int devnull_fd = open("/dev/null",
2906
 
                                O_WRONLY | O_CLOEXEC);
 
2942
                                O_WRONLY | O_CLOEXEC | O_NOCTTY);
2907
2943
  g_assert_cmpint(devnull_fd, >=, 0);
2908
2944
  __attribute__((cleanup(cleanup_close)))
2909
2945
    const int real_stderr_fd = dup(STDERR_FILENO);
2974
3010
 
2975
3011
  __attribute__((cleanup(cleanup_close)))
2976
3012
    const int devnull_fd = open("/dev/null",
2977
 
                                O_WRONLY | O_CLOEXEC);
 
3013
                                O_WRONLY | O_CLOEXEC, O_NOCTTY);
2978
3014
  g_assert_cmpint(devnull_fd, >=, 0);
2979
3015
  __attribute__((cleanup(cleanup_close)))
2980
3016
    const int real_stderr_fd = dup(STDERR_FILENO);
3018
3054
    buffer password = {};
3019
3055
 
3020
3056
  /* Reading /proc/self/mem from offset 0 will always give EIO */
3021
 
  const int fd = open("/proc/self/mem", O_RDONLY | O_CLOEXEC);
 
3057
  const int fd = open("/proc/self/mem",
 
3058
                      O_RDONLY | O_CLOEXEC | O_NOCTTY);
3022
3059
 
3023
3060
  bool password_is_read = false;
3024
3061
  bool quit_now = false;
3452
3489
  g_assert_cmpuint((unsigned int)queue->length, ==, 0);
3453
3490
}
3454
3491
 
 
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
 
3455
3530
static void test_add_inotify_dir_watch_EAGAIN(__attribute__((unused))
3456
3531
                                              test_fixture *fixture,
3457
3532
                                              __attribute__((unused))
3894
3969
  const mono_microsecs current_time = 0;
3895
3970
 
3896
3971
  /* Reading /proc/self/mem from offset 0 will always result in EIO */
3897
 
  const int fd = open("/proc/self/mem", O_RDONLY | O_CLOEXEC);
 
3972
  const int fd = open("/proc/self/mem",
 
3973
                      O_RDONLY | O_CLOEXEC | O_NOCTTY);
3898
3974
 
3899
3975
  bool quit_now = false;
3900
3976
  __attribute__((cleanup(cleanup_queue)))
5585
5661
                                            __attribute__((unused))
5586
5662
                                            gconstpointer user_data){
5587
5663
  __attribute__((cleanup(cleanup_close)))
5588
 
    const int epoll_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
 
5664
    const int epoll_fd = open("/dev/null",
 
5665
                              O_WRONLY | O_CLOEXEC | O_NOCTTY);
5589
5666
  __attribute__((cleanup(cleanup_string)))
5590
5667
    char *const question_filename = strdup("/nonexistent/question");
5591
5668
  g_assert_nonnull(question_filename);
5995
6072
                                            __attribute__((unused))
5996
6073
                                            gconstpointer user_data){
5997
6074
  __attribute__((cleanup(cleanup_close)))
5998
 
    const int epoll_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
 
6075
    const int epoll_fd = open("/dev/null",
 
6076
                              O_WRONLY | O_CLOEXEC | O_NOCTTY);
5999
6077
  __attribute__((cleanup(cleanup_string)))
6000
6078
    char *const question_filename = strdup("/nonexistent/question");
6001
6079
  g_assert_nonnull(question_filename);
6264
6342
                                              const char *const
6265
6343
                                              dirname){
6266
6344
  __attribute__((cleanup(cleanup_close)))
6267
 
    const int devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
 
6345
    const int devnull_fd = open("/dev/null",
 
6346
                                O_WRONLY | O_CLOEXEC | O_NOCTTY);
6268
6347
  g_assert_cmpint(devnull_fd, >=, 0);
6269
6348
  __attribute__((cleanup(cleanup_close)))
6270
6349
    const int real_stderr_fd = dup(STDERR_FILENO);
7813
7892
  test_add("/parse_arguments/mixed", test_parse_arguments_mixed);
7814
7893
  test_add("/queue/create", test_create_queue);
7815
7894
  test_add("/queue/add", test_add_to_queue);
 
7895
  test_add("/queue/add/overflow", test_add_to_queue_overflow);
7816
7896
  test_add("/queue/has_question/empty",
7817
7897
           test_queue_has_question_empty);
7818
7898
  test_add("/queue/has_question/false",
7905
7985
              test_add_inotify_dir_watch);
7906
7986
  test_add_st("/task-creators/add_inotify_dir_watch/fail",
7907
7987
              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);
7908
7990
  test_add_st("/task-creators/add_inotify_dir_watch/EAGAIN",
7909
7991
              test_add_inotify_dir_watch_EAGAIN);
7910
7992
  test_add_st("/task-creators/add_inotify_dir_watch/IN_CLOSE_WRITE",
8041
8123
  g_option_context_set_help_enabled(context, FALSE);
8042
8124
  g_option_context_set_ignore_unknown_options(context, TRUE);
8043
8125
 
8044
 
  gboolean run_tests = FALSE;
 
8126
  gboolean should_run_tests = FALSE;
8045
8127
  GOptionEntry entries[] = {
8046
8128
    { "test", 0, 0, G_OPTION_ARG_NONE,
8047
 
      &run_tests, "Run tests", NULL },
 
8129
      &should_run_tests, "Run tests", NULL },
8048
8130
    { NULL }
8049
8131
  };
8050
8132
  g_option_context_add_main_entries(context, entries, NULL);
8057
8139
  }
8058
8140
 
8059
8141
  g_option_context_free(context);
8060
 
  return run_tests != FALSE;
 
8142
  return should_run_tests != FALSE;
8061
8143
}