Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block non-VPN incoming traffic in lockdown mode #235

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac
private Shapeshifter shapeshifter;
private ObfsVpnClient obfsVpnClient;
private FirewallManager firewallManager;
private boolean mIsLockdownEnabled = false;

private final IBinder mBinder = new IOpenVPNServiceInternal.Stub() {

Expand Down Expand Up @@ -576,10 +577,38 @@ private String getTunConfigString() {
return cfg;
}

public void determineLockdownState() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added to work around the fact that isLockdownEnabled() always returns false unless the VPN is established.

Builder builder = new Builder();

try {
builder.addAddress(mLocalIP.mIp, mLocalIP.len);
} catch (IllegalArgumentException iae) {
return;
}

ParcelFileDescriptor tun = null;
try {
tun = builder.establish();
mIsLockdownEnabled = isLockdownEnabledCompat();
} catch (Exception e) {
VpnStatus.logError(getString(R.string.error) + e.getLocalizedMessage());
} finally {
if (tun != null) {
try {
tun.close();
} catch (Exception e) {
VpnStatus.logError(getString(R.string.error) + e.getLocalizedMessage());
}
}
}
}

public ParcelFileDescriptor openTun() {

//Debug.startMethodTracing(getExternalFilesDir(null).toString() + "/opentun.trace", 40* 1024 * 1024);

determineLockdownState();

if (mProfile == null) {
VpnStatus.logError("Refusing to open tun device without profile.");
return null;
Expand Down Expand Up @@ -641,6 +670,15 @@ public ParcelFileDescriptor openTun() {
builder.setMtu(mMtu);
}

// Don't exclude local addresses at all in lockdown mode.
// Otherwise, incoming traffic can still bypass lockdown (AOSP quirk/bug).
if (mIsLockdownEnabled) {
mRoutes.clear();
mRoutesv6.clear();
addRoute(new CIDRIP("0.0.0.0", 0), true);
addRoutev6("::/0", true);
}

Collection<IpAddress> positiveIPv4Routes = mRoutes.getPositiveIPList();
Collection<IpAddress> positiveIPv6Routes = mRoutesv6.getPositiveIPList();

Expand Down Expand Up @@ -715,7 +753,7 @@ public ParcelFileDescriptor openTun() {
ipv6info = mLocalIPv6;
}

if ((!mRoutes.getNetworks(false).isEmpty() || !mRoutesv6.getNetworks(false).isEmpty()) && isLockdownEnabledCompat())
if ((!mRoutes.getNetworks(false).isEmpty() || !mRoutesv6.getNetworks(false).isEmpty()) && mIsLockdownEnabled)
{
VpnStatus.logInfo("VPN lockdown enabled (do not allow apps to bypass VPN) enabled. Route exclusion will not allow apps to bypass VPN (e.g. bypass VPN for local networks)");
}
Expand Down Expand Up @@ -1168,4 +1206,4 @@ public void trigger_url_open(String info) {
mNotificationManager.notify(notificationId, notification);
*/
}
}
}