Add AppOps Checks for MANAGE_IPSEC_TUNNELS
Adds support for a new AppOp to permit services to
use IpSec tunnel mode. The IpSecService now needs
a context so change the service mode to a cached
service rather than a static service.
Bug: 66955045
Test: runtest frameworks-net
Merged-In: I17a4a286225b432c3e15ea1587d946189931b4f4
Change-Id: I17a4a286225b432c3e15ea1587d946189931b4f4
(cherry picked from commit 65ef843176)
This commit is contained in:
@@ -45,25 +45,31 @@ interface IIpSecService
|
||||
in String localAddr,
|
||||
in String remoteAddr,
|
||||
in Network underlyingNetwork,
|
||||
in IBinder binder);
|
||||
in IBinder binder,
|
||||
in String callingPackage);
|
||||
|
||||
void addAddressToTunnelInterface(
|
||||
int tunnelResourceId,
|
||||
in LinkAddress localAddr);
|
||||
in LinkAddress localAddr,
|
||||
in String callingPackage);
|
||||
|
||||
void removeAddressFromTunnelInterface(
|
||||
int tunnelResourceId,
|
||||
in LinkAddress localAddr);
|
||||
in LinkAddress localAddr,
|
||||
in String callingPackage);
|
||||
|
||||
void deleteTunnelInterface(int resourceId);
|
||||
void deleteTunnelInterface(int resourceId, in String callingPackage);
|
||||
|
||||
IpSecTransformResponse createTransform(in IpSecConfig c, in IBinder binder);
|
||||
IpSecTransformResponse createTransform(
|
||||
in IpSecConfig c, in IBinder binder, in String callingPackage);
|
||||
|
||||
void deleteTransform(int transformId);
|
||||
|
||||
void applyTransportModeTransform(in ParcelFileDescriptor socket, int direction, int transformId);
|
||||
void applyTransportModeTransform(
|
||||
in ParcelFileDescriptor socket, int direction, int transformId);
|
||||
|
||||
void applyTunnelModeTransform(int tunnelResourceId, int direction, int transformResourceId);
|
||||
void applyTunnelModeTransform(
|
||||
int tunnelResourceId, int direction, int transformResourceId, in String callingPackage);
|
||||
|
||||
void removeTransportModeTransforms(in ParcelFileDescriptor socket);
|
||||
}
|
||||
|
||||
@@ -140,6 +140,7 @@ public final class IpSecManager {
|
||||
}
|
||||
}
|
||||
|
||||
private final Context mContext;
|
||||
private final IIpSecService mService;
|
||||
|
||||
/**
|
||||
@@ -667,6 +668,7 @@ public final class IpSecManager {
|
||||
*/
|
||||
@SystemApi
|
||||
public static final class IpSecTunnelInterface implements AutoCloseable {
|
||||
private final String mOpPackageName;
|
||||
private final IIpSecService mService;
|
||||
private final InetAddress mRemoteAddress;
|
||||
private final InetAddress mLocalAddress;
|
||||
@@ -694,7 +696,8 @@ public final class IpSecManager {
|
||||
@RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
|
||||
public void addAddress(@NonNull LinkAddress address) throws IOException {
|
||||
try {
|
||||
mService.addAddressToTunnelInterface(mResourceId, address);
|
||||
mService.addAddressToTunnelInterface(
|
||||
mResourceId, address, mOpPackageName);
|
||||
} catch (RemoteException e) {
|
||||
throw e.rethrowFromSystemServer();
|
||||
}
|
||||
@@ -712,16 +715,18 @@ public final class IpSecManager {
|
||||
@RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
|
||||
public void removeAddress(@NonNull LinkAddress address) throws IOException {
|
||||
try {
|
||||
mService.removeAddressFromTunnelInterface(mResourceId, address);
|
||||
mService.removeAddressFromTunnelInterface(
|
||||
mResourceId, address, mOpPackageName);
|
||||
} catch (RemoteException e) {
|
||||
throw e.rethrowFromSystemServer();
|
||||
}
|
||||
}
|
||||
|
||||
private IpSecTunnelInterface(@NonNull IIpSecService service,
|
||||
private IpSecTunnelInterface(@NonNull Context ctx, @NonNull IIpSecService service,
|
||||
@NonNull InetAddress localAddress, @NonNull InetAddress remoteAddress,
|
||||
@NonNull Network underlyingNetwork)
|
||||
throws ResourceUnavailableException, IOException {
|
||||
mOpPackageName = ctx.getOpPackageName();
|
||||
mService = service;
|
||||
mLocalAddress = localAddress;
|
||||
mRemoteAddress = remoteAddress;
|
||||
@@ -733,7 +738,8 @@ public final class IpSecManager {
|
||||
localAddress.getHostAddress(),
|
||||
remoteAddress.getHostAddress(),
|
||||
underlyingNetwork,
|
||||
new Binder());
|
||||
new Binder(),
|
||||
mOpPackageName);
|
||||
switch (result.status) {
|
||||
case Status.OK:
|
||||
break;
|
||||
@@ -762,7 +768,7 @@ public final class IpSecManager {
|
||||
@Override
|
||||
public void close() {
|
||||
try {
|
||||
mService.deleteTunnelInterface(mResourceId);
|
||||
mService.deleteTunnelInterface(mResourceId, mOpPackageName);
|
||||
mResourceId = INVALID_RESOURCE_ID;
|
||||
} catch (RemoteException e) {
|
||||
throw e.rethrowFromSystemServer();
|
||||
@@ -807,7 +813,8 @@ public final class IpSecManager {
|
||||
public IpSecTunnelInterface createIpSecTunnelInterface(@NonNull InetAddress localAddress,
|
||||
@NonNull InetAddress remoteAddress, @NonNull Network underlyingNetwork)
|
||||
throws ResourceUnavailableException, IOException {
|
||||
return new IpSecTunnelInterface(mService, localAddress, remoteAddress, underlyingNetwork);
|
||||
return new IpSecTunnelInterface(
|
||||
mContext, mService, localAddress, remoteAddress, underlyingNetwork);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -833,7 +840,8 @@ public final class IpSecManager {
|
||||
@PolicyDirection int direction, @NonNull IpSecTransform transform) throws IOException {
|
||||
try {
|
||||
mService.applyTunnelModeTransform(
|
||||
tunnel.getResourceId(), direction, transform.getResourceId());
|
||||
tunnel.getResourceId(), direction,
|
||||
transform.getResourceId(), mContext.getOpPackageName());
|
||||
} catch (RemoteException e) {
|
||||
throw e.rethrowFromSystemServer();
|
||||
}
|
||||
@@ -845,7 +853,8 @@ public final class IpSecManager {
|
||||
* @param context the application context for this manager
|
||||
* @hide
|
||||
*/
|
||||
public IpSecManager(IIpSecService service) {
|
||||
public IpSecManager(Context ctx, IIpSecService service) {
|
||||
mContext = ctx;
|
||||
mService = checkNotNull(service, "missing service");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,7 +130,8 @@ public final class IpSecTransform implements AutoCloseable {
|
||||
synchronized (this) {
|
||||
try {
|
||||
IIpSecService svc = getIpSecService();
|
||||
IpSecTransformResponse result = svc.createTransform(mConfig, new Binder());
|
||||
IpSecTransformResponse result = svc.createTransform(
|
||||
mConfig, new Binder(), mContext.getOpPackageName());
|
||||
int status = result.status;
|
||||
checkResultStatus(status);
|
||||
mResourceId = result.resourceId;
|
||||
|
||||
@@ -24,6 +24,8 @@ import static android.system.OsConstants.IPPROTO_UDP;
|
||||
import static android.system.OsConstants.SOCK_DGRAM;
|
||||
import static com.android.internal.util.Preconditions.checkNotNull;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.app.AppOpsManager;
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.IIpSecService;
|
||||
@@ -42,6 +44,7 @@ import android.net.NetworkUtils;
|
||||
import android.net.TrafficStats;
|
||||
import android.net.util.NetdService;
|
||||
import android.os.Binder;
|
||||
import android.os.DeadSystemException;
|
||||
import android.os.IBinder;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
import android.os.RemoteException;
|
||||
@@ -973,6 +976,13 @@ public class IpSecService extends IIpSecService.Stub {
|
||||
return service;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private AppOpsManager getAppOpsManager() {
|
||||
AppOpsManager appOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
|
||||
if(appOps == null) throw new RuntimeException("System Server couldn't get AppOps");
|
||||
return appOps;
|
||||
}
|
||||
|
||||
/** @hide */
|
||||
@VisibleForTesting
|
||||
public IpSecService(Context context, IpSecServiceConfiguration config) {
|
||||
@@ -1239,7 +1249,9 @@ public class IpSecService extends IIpSecService.Stub {
|
||||
*/
|
||||
@Override
|
||||
public synchronized IpSecTunnelInterfaceResponse createTunnelInterface(
|
||||
String localAddr, String remoteAddr, Network underlyingNetwork, IBinder binder) {
|
||||
String localAddr, String remoteAddr, Network underlyingNetwork, IBinder binder,
|
||||
String callingPackage) {
|
||||
enforceTunnelPermissions(callingPackage);
|
||||
checkNotNull(binder, "Null Binder passed to createTunnelInterface");
|
||||
checkNotNull(underlyingNetwork, "No underlying network was specified");
|
||||
checkInetAddress(localAddr);
|
||||
@@ -1319,8 +1331,8 @@ public class IpSecService extends IIpSecService.Stub {
|
||||
*/
|
||||
@Override
|
||||
public synchronized void addAddressToTunnelInterface(
|
||||
int tunnelResourceId, LinkAddress localAddr) {
|
||||
enforceNetworkStackPermission();
|
||||
int tunnelResourceId, LinkAddress localAddr, String callingPackage) {
|
||||
enforceTunnelPermissions(callingPackage);
|
||||
UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid());
|
||||
|
||||
// Get tunnelInterface record; if no such interface is found, will throw
|
||||
@@ -1351,10 +1363,10 @@ public class IpSecService extends IIpSecService.Stub {
|
||||
*/
|
||||
@Override
|
||||
public synchronized void removeAddressFromTunnelInterface(
|
||||
int tunnelResourceId, LinkAddress localAddr) {
|
||||
enforceNetworkStackPermission();
|
||||
UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid());
|
||||
int tunnelResourceId, LinkAddress localAddr, String callingPackage) {
|
||||
enforceTunnelPermissions(callingPackage);
|
||||
|
||||
UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid());
|
||||
// Get tunnelInterface record; if no such interface is found, will throw
|
||||
// IllegalArgumentException
|
||||
TunnelInterfaceRecord tunnelInterfaceInfo =
|
||||
@@ -1382,7 +1394,9 @@ public class IpSecService extends IIpSecService.Stub {
|
||||
* server
|
||||
*/
|
||||
@Override
|
||||
public synchronized void deleteTunnelInterface(int resourceId) throws RemoteException {
|
||||
public synchronized void deleteTunnelInterface(
|
||||
int resourceId, String callingPackage) throws RemoteException {
|
||||
enforceTunnelPermissions(callingPackage);
|
||||
UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid());
|
||||
releaseResource(userRecord.mTunnelInterfaceRecords, resourceId);
|
||||
}
|
||||
@@ -1468,7 +1482,6 @@ public class IpSecService extends IIpSecService.Stub {
|
||||
case IpSecTransform.MODE_TRANSPORT:
|
||||
break;
|
||||
case IpSecTransform.MODE_TUNNEL:
|
||||
enforceNetworkStackPermission();
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException(
|
||||
@@ -1476,9 +1489,20 @@ public class IpSecService extends IIpSecService.Stub {
|
||||
}
|
||||
}
|
||||
|
||||
private void enforceNetworkStackPermission() {
|
||||
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_STACK,
|
||||
"IpSecService");
|
||||
private void enforceTunnelPermissions(String callingPackage) {
|
||||
checkNotNull(callingPackage, "Null calling package cannot create IpSec tunnels");
|
||||
switch (getAppOpsManager().noteOp(
|
||||
AppOpsManager.OP_MANAGE_IPSEC_TUNNELS,
|
||||
Binder.getCallingUid(), callingPackage)) {
|
||||
case AppOpsManager.MODE_DEFAULT:
|
||||
mContext.enforceCallingOrSelfPermission(
|
||||
android.Manifest.permission.MANAGE_IPSEC_TUNNELS, "IpSecService");
|
||||
break;
|
||||
case AppOpsManager.MODE_ALLOWED:
|
||||
return;
|
||||
default:
|
||||
throw new SecurityException("Request to ignore AppOps for non-legacy API");
|
||||
}
|
||||
}
|
||||
|
||||
private void createOrUpdateTransform(
|
||||
@@ -1534,8 +1558,12 @@ public class IpSecService extends IIpSecService.Stub {
|
||||
* result in all of those sockets becoming unable to send or receive data.
|
||||
*/
|
||||
@Override
|
||||
public synchronized IpSecTransformResponse createTransform(IpSecConfig c, IBinder binder)
|
||||
throws RemoteException {
|
||||
public synchronized IpSecTransformResponse createTransform(
|
||||
IpSecConfig c, IBinder binder, String callingPackage) throws RemoteException {
|
||||
checkNotNull(c);
|
||||
if (c.getMode() == IpSecTransform.MODE_TUNNEL) {
|
||||
enforceTunnelPermissions(callingPackage);
|
||||
}
|
||||
checkIpSecConfig(c);
|
||||
checkNotNull(binder, "Null Binder passed to createTransform");
|
||||
final int resourceId = mNextResourceId++;
|
||||
@@ -1656,8 +1684,9 @@ public class IpSecService extends IIpSecService.Stub {
|
||||
*/
|
||||
@Override
|
||||
public synchronized void applyTunnelModeTransform(
|
||||
int tunnelResourceId, int direction, int transformResourceId) throws RemoteException {
|
||||
enforceNetworkStackPermission();
|
||||
int tunnelResourceId, int direction,
|
||||
int transformResourceId, String callingPackage) throws RemoteException {
|
||||
enforceTunnelPermissions(callingPackage);
|
||||
checkDirection(direction);
|
||||
|
||||
UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid());
|
||||
|
||||
Reference in New Issue
Block a user