From e421b997cd31088ca9cec96199b7acf965483eaa Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sun, 26 Nov 2017 23:45:41 +0100 Subject: Config: make parsing stricter Signed-off-by: Jason A. Donenfeld --- .../java/com/wireguard/android/backends/VpnService.java | 5 +++++ app/src/main/java/com/wireguard/config/Attribute.java | 2 +- app/src/main/java/com/wireguard/config/Config.java | 14 +++++++++++--- app/src/main/java/com/wireguard/config/Peer.java | 4 ++-- 4 files changed, 19 insertions(+), 6 deletions(-) (limited to 'app/src/main/java/com') diff --git a/app/src/main/java/com/wireguard/android/backends/VpnService.java b/app/src/main/java/com/wireguard/android/backends/VpnService.java index 084a2407..95736de5 100644 --- a/app/src/main/java/com/wireguard/android/backends/VpnService.java +++ b/app/src/main/java/com/wireguard/android/backends/VpnService.java @@ -369,6 +369,11 @@ public class VpnService extends Service config.setName(configName); configs.add(config); } catch (IllegalArgumentException | IOException e) { + try { + file.delete(); + } catch (Exception e2) { + Log.w(TAG, "Could not remove " + fileName, e2); + } Log.w(TAG, "Failed to load config from " + fileName, e); } } diff --git a/app/src/main/java/com/wireguard/config/Attribute.java b/app/src/main/java/com/wireguard/config/Attribute.java index 4ee4e9f5..b2aa0d53 100644 --- a/app/src/main/java/com/wireguard/config/Attribute.java +++ b/app/src/main/java/com/wireguard/config/Attribute.java @@ -17,7 +17,7 @@ enum Attribute { LISTEN_PORT("ListenPort"), MTU("MTU"), PERSISTENT_KEEPALIVE("PersistentKeepalive"), - PRE_SHARED_KEY("PresharedKey"), + PRESHARED_KEY("PresharedKey"), PRIVATE_KEY("PrivateKey"), PUBLIC_KEY("PublicKey"); diff --git a/app/src/main/java/com/wireguard/config/Config.java b/app/src/main/java/com/wireguard/config/Config.java index 2a282d09..d5f0c6e5 100644 --- a/app/src/main/java/com/wireguard/config/Config.java +++ b/app/src/main/java/com/wireguard/config/Config.java @@ -139,19 +139,27 @@ public class Config extends BaseObservable new InputStreamReader(stream, StandardCharsets.UTF_8))) { Peer currentPeer = null; String line; + boolean inInterfaceSection = false; while ((line = reader.readLine()) != null) { - if (line.isEmpty()) + if (line.isEmpty() || line.startsWith("#")) continue; if ("[Interface]".equals(line)) { currentPeer = null; + inInterfaceSection = true; } else if ("[Peer]".equals(line)) { currentPeer = addPeer(); - } else if (currentPeer == null) { + inInterfaceSection = false; + } else if (inInterfaceSection) { iface.parse(line); - } else { + } else if (currentPeer != null) { currentPeer.parse(line); + } else { + throw new IllegalArgumentException("Invalid configuration line: " + line); } } + if (!inInterfaceSection && currentPeer == null) { + throw new IllegalArgumentException("Did not find any config information"); + } } } diff --git a/app/src/main/java/com/wireguard/config/Peer.java b/app/src/main/java/com/wireguard/config/Peer.java index 718a5c3c..ea73155f 100644 --- a/app/src/main/java/com/wireguard/config/Peer.java +++ b/app/src/main/java/com/wireguard/config/Peer.java @@ -104,7 +104,7 @@ public class Peer extends BaseObservable implements Copyable, Observable, setEndpoint(key.parseFrom(line)); else if (key == Attribute.PERSISTENT_KEEPALIVE) setPersistentKeepalive(key.parseFrom(line)); - else if (key == Attribute.PRE_SHARED_KEY) + else if (key == Attribute.PRESHARED_KEY) setPreSharedKey(key.parseFrom(line)); else if (key == Attribute.PUBLIC_KEY) setPublicKey(key.parseFrom(line)); @@ -161,7 +161,7 @@ public class Peer extends BaseObservable implements Copyable, Observable, if (persistentKeepalive != null) sb.append(Attribute.PERSISTENT_KEEPALIVE.composeWith(persistentKeepalive)); if (preSharedKey != null) - sb.append(Attribute.PRE_SHARED_KEY.composeWith(preSharedKey)); + sb.append(Attribute.PRESHARED_KEY.composeWith(preSharedKey)); if (publicKey != null) sb.append(Attribute.PUBLIC_KEY.composeWith(publicKey)); return sb.toString(); -- cgit v1.2.3