/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 plugins.d/splashy.c

  • Committer: teddy at recompile
  • Date: 2020-04-05 21:30:59 UTC
  • Revision ID: teddy@recompile.se-20200405213059-fb2a61ckqynrmatk
Fix file descriptor leak in mandos-client

When the local network has Mandos servers announcing themselves using
real, globally reachable, IPv6 addresses (i.e. not link-local
addresses), but there is no router on the local network providing IPv6
RA (Router Advertisement) packets, the client cannot reach the server
by normal means, since the client only has a link-local IPv6 address,
and has no usable route to reach the server's global IPv6 address.
(This is not a common situation, and usually only happens when the
router itself reboots and runs a Mandos client, since it cannot then
give RA packets to itself.)  The client code has a solution for
this, which consists of adding a temporary local route to reach the
address of the server during communication, and removing this
temporary route afterwards.

This solution with a temporary route works, but has a file descriptor
leak; it leaks one file descriptor for each addition and for each
removal of a route.  If one server requiring an added route is present
on the network, but no servers gives a password, making the client
retry after the default ten seconds, and we furthermore assume a
default 1024 open files limit, the client runs out of file descriptors
after about 90 minutes, after which time the client process will be
useless and fail to retrieve any passwords, necessitating manual
password entry via the keyboard.

Fix this by eliminating the file descriptor leak in the client.

* plugins.d/mandos-client.c (add_delete_local_route): Do
  close(devnull) also in parent process, also if fork() fails, and on
  any failure in child process.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  -*- coding: utf-8 -*- */
 
2
/*
 
3
 * Splashy - Read a password from splashy and output it
 
4
 * 
 
5
 * Copyright © 2008-2018 Teddy Hogeborn
 
6
 * Copyright © 2008-2018 Björn Påhlsson
 
7
 * 
 
8
 * This file is part of Mandos.
 
9
 * 
 
10
 * Mandos is free software: you can redistribute it and/or modify it
 
11
 * under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation, either version 3 of the License, or
 
13
 * (at your option) any later version.
 
14
 * 
 
15
 * Mandos is distributed in the hope that it will be useful, but
 
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
18
 * General Public License for more details.
 
19
 * 
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
 
22
 * 
 
23
 * Contact the authors at <mandos@recompile.se>.
 
24
 */
 
25
 
 
26
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
 
27
#include <signal.h>             /* sig_atomic_t, struct sigaction,
 
28
                                   sigemptyset(), sigaddset(), SIGINT,
 
29
                                   SIGHUP, SIGTERM, sigaction,
 
30
                                   SIG_IGN, kill(), SIGKILL */
 
31
#include <stddef.h>             /* NULL */
 
32
#include <stdlib.h>             /* getenv() */
 
33
#include <stdio.h>              /* asprintf(), vasprintf(), vprintf(),
 
34
                                   fprintf() */
 
35
#include <stdlib.h>             /* EXIT_FAILURE, free(),
 
36
                                   EXIT_SUCCESS */
 
37
#include <sys/types.h>          /* pid_t, DIR, struct dirent,
 
38
                                   ssize_t */
 
39
#include <dirent.h>             /* opendir(), readdir(), closedir() */
 
40
#include <inttypes.h>           /* intmax_t, strtoimax() */
 
41
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
 
42
#include <iso646.h>             /* not, or, and */
 
43
#include <unistd.h>             /* readlink(), fork(), execl(),
 
44
                                   sleep(), dup2() STDERR_FILENO,
 
45
                                   STDOUT_FILENO, _exit(),
 
46
                                   pause() */
 
47
#include <string.h>             /* memcmp(), strerror() */
 
48
#include <errno.h>              /* errno, EACCES, ENOTDIR, ELOOP,
 
49
                                   ENOENT, ENAMETOOLONG, EMFILE,
 
50
                                   ENFILE, ENOMEM, ENOEXEC, EINVAL,
 
51
                                   E2BIG, EFAULT, EIO, ETXTBSY,
 
52
                                   EISDIR, ELIBBAD, EPERM, EINTR,
 
53
                                   ECHILD */
 
54
#include <error.h>              /* error() */
 
55
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
 
56
                                   WEXITSTATUS() */
 
57
#include <sysexits.h>           /* EX_OSERR, EX_OSFILE,
 
58
                                   EX_UNAVAILABLE */
 
59
#include <stdarg.h>             /* va_list, va_start(), ... */
 
60
 
 
61
sig_atomic_t interrupted_by_signal = 0;
 
62
int signal_received;
 
63
 
 
64
/* Function to use when printing errors */
 
65
__attribute__((format (gnu_printf, 3, 4)))
 
66
void error_plus(int status, int errnum, const char *formatstring,
 
67
                ...){
 
68
  va_list ap;
 
69
  char *text;
 
70
  int ret;
 
71
  
 
72
  va_start(ap, formatstring);
 
73
  ret = vasprintf(&text, formatstring, ap);
 
74
  if(ret == -1){
 
75
    fprintf(stderr, "Mandos plugin %s: ",
 
76
            program_invocation_short_name);
 
77
    vfprintf(stderr, formatstring, ap);
 
78
    fprintf(stderr, ": ");
 
79
    fprintf(stderr, "%s\n", strerror(errnum));
 
80
    error(status, errno, "vasprintf while printing error");
 
81
    return;
 
82
  }
 
83
  fprintf(stderr, "Mandos plugin ");
 
84
  error(status, errnum, "%s", text);
 
85
  free(text);
 
86
}
 
87
 
 
88
 
 
89
static void termination_handler(int signum){
 
90
  if(interrupted_by_signal){
 
91
    return;
 
92
  }
 
93
  interrupted_by_signal = 1;
 
94
  signal_received = signum;
 
95
}
 
96
 
 
97
int main(__attribute__((unused))int argc,
 
98
         __attribute__((unused))char **argv){
 
99
  int ret = 0;
 
100
  char *prompt = NULL;
 
101
  DIR *proc_dir = NULL;
 
102
  pid_t splashy_pid = 0;
 
103
  pid_t splashy_command_pid = 0;
 
104
  int exitstatus = EXIT_FAILURE;
 
105
  
 
106
  /* Create prompt string */
 
107
  {
 
108
    const char *const cryptsource = getenv("cryptsource");
 
109
    const char *const crypttarget = getenv("crypttarget");
 
110
    const char *const prompt_start = "getpass "
 
111
      "Enter passphrase to unlock the disk";
 
112
    
 
113
    if(cryptsource == NULL){
 
114
      if(crypttarget == NULL){
 
115
        ret = asprintf(&prompt, "%s: ", prompt_start);
 
116
      } else {
 
117
        ret = asprintf(&prompt, "%s (%s): ", prompt_start,
 
118
                       crypttarget);
 
119
      }
 
120
    } else {
 
121
      if(crypttarget == NULL){
 
122
        ret = asprintf(&prompt, "%s %s: ", prompt_start, cryptsource);
 
123
      } else {
 
124
        ret = asprintf(&prompt, "%s %s (%s): ", prompt_start,
 
125
                       cryptsource, crypttarget);
 
126
      }
 
127
    }
 
128
    if(ret == -1){
 
129
      prompt = NULL;
 
130
      exitstatus = EX_OSERR;
 
131
      goto failure;
 
132
    }
 
133
  }
 
134
  
 
135
  /* Find splashy process */
 
136
  {
 
137
    const char splashy_name[] = "/sbin/splashy";
 
138
    proc_dir = opendir("/proc");
 
139
    if(proc_dir == NULL){
 
140
      int e = errno;
 
141
      error_plus(0, errno, "opendir");
 
142
      switch(e){
 
143
      case EACCES:
 
144
      case ENOTDIR:
 
145
      case ELOOP:
 
146
      case ENOENT:
 
147
      default:
 
148
        exitstatus = EX_OSFILE;
 
149
        break;
 
150
      case ENAMETOOLONG:
 
151
      case EMFILE:
 
152
      case ENFILE:
 
153
      case ENOMEM:
 
154
        exitstatus = EX_OSERR;
 
155
        break;
 
156
      }
 
157
      goto failure;
 
158
    }
 
159
    for(struct dirent *proc_ent = readdir(proc_dir);
 
160
        proc_ent != NULL;
 
161
        proc_ent = readdir(proc_dir)){
 
162
      pid_t pid;
 
163
      {
 
164
        intmax_t tmpmax;
 
165
        char *tmp;
 
166
        errno = 0;
 
167
        tmpmax = strtoimax(proc_ent->d_name, &tmp, 10);
 
168
        if(errno != 0 or tmp == proc_ent->d_name or *tmp != '\0'
 
169
           or tmpmax != (pid_t)tmpmax){
 
170
          /* Not a process */
 
171
          continue;
 
172
        }
 
173
        pid = (pid_t)tmpmax;
 
174
      }
 
175
      /* Find the executable name by doing readlink() on the
 
176
         /proc/<pid>/exe link */
 
177
      char exe_target[sizeof(splashy_name)];
 
178
      ssize_t sret;
 
179
      {
 
180
        char *exe_link;
 
181
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
 
182
        if(ret == -1){
 
183
          error_plus(0, errno, "asprintf");
 
184
          exitstatus = EX_OSERR;
 
185
          goto failure;
 
186
        }
 
187
        
 
188
        /* Check that it refers to a symlink owned by root:root */
 
189
        struct stat exe_stat;
 
190
        ret = lstat(exe_link, &exe_stat);
 
191
        if(ret == -1){
 
192
          if(errno == ENOENT){
 
193
            free(exe_link);
 
194
            continue;
 
195
          }
 
196
          int e = errno;
 
197
          error_plus(0, errno, "lstat");
 
198
          free(exe_link);
 
199
          switch(e){
 
200
          case EACCES:
 
201
          case ENOTDIR:
 
202
          case ELOOP:
 
203
          default:
 
204
            exitstatus = EX_OSFILE;
 
205
            break;
 
206
          case ENAMETOOLONG:
 
207
            exitstatus = EX_OSERR;
 
208
            break;
 
209
          }
 
210
          goto failure;
 
211
        }
 
212
        if(not S_ISLNK(exe_stat.st_mode)
 
213
           or exe_stat.st_uid != 0
 
214
           or exe_stat.st_gid != 0){
 
215
          free(exe_link);
 
216
          continue;
 
217
        }
 
218
        
 
219
        sret = readlink(exe_link, exe_target, sizeof(exe_target));
 
220
        free(exe_link);
 
221
      }
 
222
      if((sret == ((ssize_t)sizeof(exe_target)-1))
 
223
         and (memcmp(splashy_name, exe_target,
 
224
                     sizeof(exe_target)-1) == 0)){
 
225
        splashy_pid = pid;
 
226
        break;
 
227
      }
 
228
    }
 
229
    closedir(proc_dir);
 
230
    proc_dir = NULL;
 
231
  }
 
232
  if(splashy_pid == 0){
 
233
    exitstatus = EX_UNAVAILABLE;
 
234
    goto failure;
 
235
  }
 
236
  
 
237
  /* Set up the signal handler */
 
238
  {
 
239
    struct sigaction old_action,
 
240
      new_action = { .sa_handler = termination_handler,
 
241
                     .sa_flags = 0 };
 
242
    sigemptyset(&new_action.sa_mask);
 
243
    ret = sigaddset(&new_action.sa_mask, SIGINT);
 
244
    if(ret == -1){
 
245
      error_plus(0, errno, "sigaddset");
 
246
      exitstatus = EX_OSERR;
 
247
      goto failure;
 
248
    }
 
249
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
 
250
    if(ret == -1){
 
251
      error_plus(0, errno, "sigaddset");
 
252
      exitstatus = EX_OSERR;
 
253
      goto failure;
 
254
    }
 
255
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
 
256
    if(ret == -1){
 
257
      error_plus(0, errno, "sigaddset");
 
258
      exitstatus = EX_OSERR;
 
259
      goto failure;
 
260
    }
 
261
    ret = sigaction(SIGINT, NULL, &old_action);
 
262
    if(ret == -1){
 
263
      error_plus(0, errno, "sigaction");
 
264
      exitstatus = EX_OSERR;
 
265
      goto failure;
 
266
    }
 
267
    if(old_action.sa_handler != SIG_IGN){
 
268
      ret = sigaction(SIGINT, &new_action, NULL);
 
269
      if(ret == -1){
 
270
        error_plus(0, errno, "sigaction");
 
271
        exitstatus = EX_OSERR;
 
272
        goto failure;
 
273
      }
 
274
    }
 
275
    ret = sigaction(SIGHUP, NULL, &old_action);
 
276
    if(ret == -1){
 
277
      error_plus(0, errno, "sigaction");
 
278
      exitstatus = EX_OSERR;
 
279
      goto failure;
 
280
    }
 
281
    if(old_action.sa_handler != SIG_IGN){
 
282
      ret = sigaction(SIGHUP, &new_action, NULL);
 
283
      if(ret == -1){
 
284
        error_plus(0, errno, "sigaction");
 
285
        exitstatus = EX_OSERR;
 
286
        goto failure;
 
287
      }
 
288
    }
 
289
    ret = sigaction(SIGTERM, NULL, &old_action);
 
290
    if(ret == -1){
 
291
      error_plus(0, errno, "sigaction");
 
292
      exitstatus = EX_OSERR;
 
293
      goto failure;
 
294
    }
 
295
    if(old_action.sa_handler != SIG_IGN){
 
296
      ret = sigaction(SIGTERM, &new_action, NULL);
 
297
      if(ret == -1){
 
298
        error_plus(0, errno, "sigaction");
 
299
        exitstatus = EX_OSERR;
 
300
        goto failure;
 
301
      }
 
302
    }
 
303
  }
 
304
  
 
305
  if(interrupted_by_signal){
 
306
    goto failure;
 
307
  }
 
308
  
 
309
  /* Fork off the splashy command to prompt for password */
 
310
  splashy_command_pid = fork();
 
311
  if(splashy_command_pid != 0 and interrupted_by_signal){
 
312
    goto failure;
 
313
  }
 
314
  if(splashy_command_pid == -1){
 
315
    error_plus(0, errno, "fork");
 
316
    exitstatus = EX_OSERR;
 
317
    goto failure;
 
318
  }
 
319
  /* Child */
 
320
  if(splashy_command_pid == 0){
 
321
    if(not interrupted_by_signal){
 
322
      const char splashy_command[] = "/sbin/splashy_update";
 
323
      execl(splashy_command, splashy_command, prompt, (char *)NULL);
 
324
      int e = errno;
 
325
      error_plus(0, errno, "execl");
 
326
      switch(e){
 
327
      case EACCES:
 
328
      case ENOENT:
 
329
      case ENOEXEC:
 
330
      case EINVAL:
 
331
        _exit(EX_UNAVAILABLE);
 
332
      case ENAMETOOLONG:
 
333
      case E2BIG:
 
334
      case ENOMEM:
 
335
      case EFAULT:
 
336
      case EIO:
 
337
      case EMFILE:
 
338
      case ENFILE:
 
339
      case ETXTBSY:
 
340
      default:
 
341
        _exit(EX_OSERR);
 
342
      case ENOTDIR:
 
343
      case ELOOP:
 
344
      case EISDIR:
 
345
#ifdef ELIBBAD
 
346
      case ELIBBAD:             /* Linux only */
 
347
#endif
 
348
      case EPERM:
 
349
        _exit(EX_OSFILE);
 
350
      }
 
351
    }
 
352
    free(prompt);
 
353
    _exit(EXIT_FAILURE);
 
354
  }
 
355
  
 
356
  /* Parent */
 
357
  free(prompt);
 
358
  prompt = NULL;
 
359
  
 
360
  if(interrupted_by_signal){
 
361
    goto failure;
 
362
  }
 
363
  
 
364
  /* Wait for command to complete */
 
365
  {
 
366
    int status;
 
367
    do {
 
368
      ret = waitpid(splashy_command_pid, &status, 0);
 
369
    } while(ret == -1 and errno == EINTR
 
370
            and not interrupted_by_signal);
 
371
    if(interrupted_by_signal){
 
372
      goto failure;
 
373
    }
 
374
    if(ret == -1){
 
375
      error_plus(0, errno, "waitpid");
 
376
      if(errno == ECHILD){
 
377
        splashy_command_pid = 0;
 
378
      }
 
379
    } else {
 
380
      /* The child process has exited */
 
381
      splashy_command_pid = 0;
 
382
      if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
 
383
        return EXIT_SUCCESS;
 
384
      }
 
385
    }
 
386
  }
 
387
  
 
388
 failure:
 
389
  
 
390
  free(prompt);
 
391
  
 
392
  if(proc_dir != NULL){
 
393
    TEMP_FAILURE_RETRY(closedir(proc_dir));
 
394
  }
 
395
  
 
396
  if(splashy_command_pid != 0){
 
397
    TEMP_FAILURE_RETRY(kill(splashy_command_pid, SIGTERM));
 
398
    
 
399
    TEMP_FAILURE_RETRY(kill(splashy_pid, SIGTERM));
 
400
    sleep(2);
 
401
    while(TEMP_FAILURE_RETRY(kill(splashy_pid, 0)) == 0){
 
402
      TEMP_FAILURE_RETRY(kill(splashy_pid, SIGKILL));
 
403
      sleep(1);
 
404
    }
 
405
    pid_t new_splashy_pid = (pid_t)TEMP_FAILURE_RETRY(fork());
 
406
    if(new_splashy_pid == 0){
 
407
      /* Child; will become new splashy process */
 
408
      
 
409
      /* Make the effective user ID (root) the only user ID instead of
 
410
         the real user ID (_mandos) */
 
411
      ret = setuid(geteuid());
 
412
      if(ret == -1){
 
413
        error_plus(0, errno, "setuid");
 
414
      }
 
415
      
 
416
      setsid();
 
417
      ret = chdir("/");
 
418
      if(ret == -1){
 
419
        error_plus(0, errno, "chdir");
 
420
      }
 
421
/*       if(fork() != 0){ */
 
422
/*      _exit(EXIT_SUCCESS); */
 
423
/*       } */
 
424
      ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace stdout */
 
425
      if(ret == -1){
 
426
        error_plus(0, errno, "dup2");
 
427
        _exit(EX_OSERR);
 
428
      }
 
429
      
 
430
      execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
 
431
      {
 
432
        int e = errno;
 
433
        error_plus(0, errno, "execl");
 
434
        switch(e){
 
435
        case EACCES:
 
436
        case ENOENT:
 
437
        case ENOEXEC:
 
438
        default:
 
439
          _exit(EX_UNAVAILABLE);
 
440
        case ENAMETOOLONG:
 
441
        case E2BIG:
 
442
        case ENOMEM:
 
443
          _exit(EX_OSERR);
 
444
        case ENOTDIR:
 
445
        case ELOOP:
 
446
          _exit(EX_OSFILE);
 
447
        }
 
448
      }
 
449
    }
 
450
  }
 
451
  
 
452
  if(interrupted_by_signal){
 
453
    struct sigaction signal_action;
 
454
    sigemptyset(&signal_action.sa_mask);
 
455
    signal_action.sa_handler = SIG_DFL;
 
456
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
 
457
                                            &signal_action, NULL));
 
458
    if(ret == -1){
 
459
      error_plus(0, errno, "sigaction");
 
460
    }
 
461
    do {
 
462
      ret = raise(signal_received);
 
463
    } while(ret != 0 and errno == EINTR);
 
464
    if(ret != 0){
 
465
      error_plus(0, errno, "raise");
 
466
      abort();
 
467
    }
 
468
    TEMP_FAILURE_RETRY(pause());
 
469
  }
 
470
  
 
471
  return exitstatus;
 
472
}