/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 12:27:14 UTC
  • Revision ID: teddy@recompile.se-20190803122714-efudxsyomr1k0zbt
dracut-module/password-agent.c: Use O_NOCTTY

Whenever a file is opened using open(), use the O_NOCTTY flag, just to
be safe.

* dracut-module/password-agent.c (test_start_mandos_client_execv): Use
  O_NOCTTY.
  (test_wait_for_mandos_client_exit_failure): - '' -
  (test_wait_for_mandos_client_exit_killed): - '' -
  (test_read_mandos_client_output_readerror): - '' -
  (test_read_inotify_event_readerror): - '' -
  (test_connect_question_socket_bad_epoll): - '' -
  (test_send_password_to_socket_bad_epoll): - '' -
  (assert_add_existing_questions_to_devnull): - '' -

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(),
85
84
#include <fcntl.h>              /* O_CLOEXEC, O_NONBLOCK, fcntl(),
86
85
                                   F_GETFD, F_GETFL, FD_CLOEXEC,
87
86
                                   open(), O_WRONLY, O_NOCTTY,
88
 
                                   O_RDONLY, O_NOFOLLOW */
 
87
                                   O_RDONLY */
89
88
#include <sys/wait.h>           /* waitpid(), WNOHANG, WIFEXITED(),
90
89
                                   WEXITSTATUS() */
91
90
#include <limits.h>             /* PIPE_BUF, NAME_MAX, INT_MAX */
92
91
#include <sys/inotify.h>        /* inotify_init1(), IN_NONBLOCK,
93
92
                                   IN_CLOEXEC, inotify_add_watch(),
94
93
                                   IN_CLOSE_WRITE, IN_MOVED_TO,
95
 
                                   IN_MOVED_FROM, IN_DELETE,
96
 
                                   IN_EXCL_UNLINK, IN_ONLYDIR,
97
 
                                   struct inotify_event */
 
94
                                   IN_DELETE, struct inotify_event */
98
95
#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() */
 
96
#include <stdio.h>              /* asprintf(), FILE, fopen(),
 
97
                                   getline(), sscanf(), feof(),
 
98
                                   ferror(), fclose(), stderr,
 
99
                                   rename(), fdopen(), fprintf(),
 
100
                                   fscanf() */
103
101
#include <glib.h>    /* GKeyFile, g_key_file_free(), g_key_file_new(),
104
102
                        GError, g_key_file_load_from_file(),
105
103
                        G_KEY_FILE_NONE, TRUE, G_FILE_ERROR_NOENT,
148
146
  mono_microsecs next_run;
149
147
} __attribute__((designated_init)) task_queue;
150
148
 
151
 
/* "task_func" - A function type for task functions
 
149
/* "func_type" - A function type for task functions
152
150
 
153
151
   I.e. functions for the code which runs when a task is run, all have
154
152
   this type */
651
649
 
652
650
__attribute__((nonnull, warn_unused_result))
653
651
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
652
  const size_t needed_size = sizeof(task_context)*(queue->length + 1);
662
653
  if(needed_size > (queue->allocated)){
663
654
    task_context *const new_tasks = realloc(queue->tasks,
1891
1882
  g_assert_true(queue->tasks[0].func == dummy_func);
1892
1883
}
1893
1884
 
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
1885
static void dummy_func(__attribute__((unused))
1918
1886
                       const task_context task,
1919
1887
                       __attribute__((unused))
7892
7860
  test_add("/parse_arguments/mixed", test_parse_arguments_mixed);
7893
7861
  test_add("/queue/create", test_create_queue);
7894
7862
  test_add("/queue/add", test_add_to_queue);
7895
 
  test_add("/queue/add/overflow", test_add_to_queue_overflow);
7896
7863
  test_add("/queue/has_question/empty",
7897
7864
           test_queue_has_question_empty);
7898
7865
  test_add("/queue/has_question/false",
8123
8090
  g_option_context_set_help_enabled(context, FALSE);
8124
8091
  g_option_context_set_ignore_unknown_options(context, TRUE);
8125
8092
 
8126
 
  gboolean should_run_tests = FALSE;
 
8093
  gboolean run_tests = FALSE;
8127
8094
  GOptionEntry entries[] = {
8128
8095
    { "test", 0, 0, G_OPTION_ARG_NONE,
8129
 
      &should_run_tests, "Run tests", NULL },
 
8096
      &run_tests, "Run tests", NULL },
8130
8097
    { NULL }
8131
8098
  };
8132
8099
  g_option_context_add_main_entries(context, entries, NULL);
8139
8106
  }
8140
8107
 
8141
8108
  g_option_context_free(context);
8142
 
  return should_run_tests != FALSE;
 
8109
  return run_tests != FALSE;
8143
8110
}