/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/mandos-client.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:
1074
1074
      ret = setgid(0);
1075
1075
      if(ret == -1){
1076
1076
        perror_plus("setgid");
 
1077
        close(devnull);
1077
1078
        _exit(EX_NOPERM);
1078
1079
      }
1079
1080
      /* Reset supplementary groups */
1081
1082
      ret = setgroups(0, NULL);
1082
1083
      if(ret == -1){
1083
1084
        perror_plus("setgroups");
 
1085
        close(devnull);
1084
1086
        _exit(EX_NOPERM);
1085
1087
      }
1086
1088
    }
1087
1089
    ret = dup2(devnull, STDIN_FILENO);
1088
1090
    if(ret == -1){
1089
1091
      perror_plus("dup2(devnull, STDIN_FILENO)");
 
1092
      close(devnull);
1090
1093
      _exit(EX_OSERR);
1091
1094
    }
1092
1095
    ret = close(devnull);
1093
1096
    if(ret == -1){
1094
1097
      perror_plus("close");
1095
 
      _exit(EX_OSERR);
1096
1098
    }
1097
1099
    ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1098
1100
    if(ret == -1){
1133
1135
  }
1134
1136
  if(pid == -1){
1135
1137
    perror_plus("fork");
 
1138
    close(devnull);
1136
1139
    return false;
1137
1140
  }
 
1141
  ret = close(devnull);
 
1142
  if(ret == -1){
 
1143
    perror_plus("close");
 
1144
  }
1138
1145
  int status;
1139
1146
  pid_t pret = -1;
1140
1147
  errno = 0;