aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Kralevich <nnk@google.com>2018-11-15 10:23:04 -0800
committerNick Kralevich <nnk@google.com>2018-11-15 10:23:04 -0800
commitbde047803c9a1c569656fe6dbb9002f3f785217f (patch)
tree5ee974697cd09258ec48834accf1fbc5ef7cb43e
parentbf16586a33b1432a11526754523fceb62944dc1c (diff)
downloadipsec-tools-bde047803c9a1c569656fe6dbb9002f3f785217f.tar.gz
android_hook: fix leaking file descriptor
/dev/tun is opened but never closed, leaking the file descriptor. Properly close the file descriptor. Add O_CLOEXEC. As the FD number is never used, it would be meaningless to attempt to pass it to a child, as the child would have no way to figure out what FD was passed from parent to child. Add O_RDONLY. The original open() call used "0" instead of O_RDONLY. It's the same thing, but it's more readable to use the macro. Error check the return value from open(). Otherwise, the remaining code will attempt to continue and perform an ioctl on an invalid file descriptor. Test: none Change-Id: I5083d175fa9b8e3e8d4707a49f29d0cebe9965f9
-rw-r--r--main.c8
1 files changed, 7 insertions, 1 deletions
diff --git a/main.c b/main.c
index 524155a..10c774f 100644
--- a/main.c
+++ b/main.c
@@ -103,7 +103,12 @@ static int android_get_control_and_arguments(int *argc, char ***argv)
const char *android_hook(char **envp)
{
struct ifreq ifr = {.ifr_flags = IFF_TUN};
- int tun = open("/dev/tun", 0);
+ int tun = open("/dev/tun", O_RDONLY | O_CLOEXEC);
+
+ if (tun == -1) {
+ do_plog(LLV_ERROR, "error opening /dev/tun: %s\n", strerror(errno));
+ exit(1);
+ }
/* Android does not support INTERNAL_WINS4_LIST, so we just use it. */
while (*envp && strncmp(*envp, "INTERNAL_WINS4_LIST=", 20)) {
@@ -117,6 +122,7 @@ const char *android_hook(char **envp)
do_plog(LLV_ERROR, "Cannot allocate TUN: %s\n", strerror(errno));
exit(1);
}
+ close(tun);
sprintf(*envp, "INTERFACE=%s", ifr.ifr_name);
return "/etc/ppp/ip-up-vpn";
}