Prevent Closure of Underlying Socket FDs

The version of applyTransportModeTransform() and
removeTransportModeTransform() that accepted
Socket and DatagramSocket were closing the underlying
FDs upon return. It's unclear whether this is due to
a behavior change elsewhere in ParcelFileDescriptor,
but either way, converting to using getFileDescriptor$
and then calling dup() explicitly rather than relying
on ParcelFileDescriptor seems like a better idea anyway.

Bug: 72047396
Test: CTS - IpSecManagerTest.testCreateTransform()
Change-Id: Ia2f02564e1289f25bf113dbb861fcfd2240537a7
This commit is contained in:
Nathan Harold
2018-01-16 12:08:43 -08:00
parent 5a19b9500d
commit 3167625a15

View File

@@ -313,9 +313,7 @@ public final class IpSecManager {
public void applyTransportModeTransform( public void applyTransportModeTransform(
Socket socket, int direction, IpSecTransform transform) Socket socket, int direction, IpSecTransform transform)
throws IOException { throws IOException {
try (ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket)) { applyTransportModeTransform(socket.getFileDescriptor$(), direction, transform);
applyTransportModeTransform(pfd, direction, transform);
}
} }
/** /**
@@ -347,9 +345,7 @@ public final class IpSecManager {
*/ */
public void applyTransportModeTransform( public void applyTransportModeTransform(
DatagramSocket socket, int direction, IpSecTransform transform) throws IOException { DatagramSocket socket, int direction, IpSecTransform transform) throws IOException {
try (ParcelFileDescriptor pfd = ParcelFileDescriptor.fromDatagramSocket(socket)) { applyTransportModeTransform(socket.getFileDescriptor$(), direction, transform);
applyTransportModeTransform(pfd, direction, transform);
}
} }
/** /**
@@ -383,18 +379,8 @@ public final class IpSecManager {
FileDescriptor socket, int direction, IpSecTransform transform) FileDescriptor socket, int direction, IpSecTransform transform)
throws IOException { throws IOException {
// We dup() the FileDescriptor here because if we don't, then the ParcelFileDescriptor() // We dup() the FileDescriptor here because if we don't, then the ParcelFileDescriptor()
// constructor takes control and closes the user's FD when we exit the method // constructor takes control and closes the user's FD when we exit the method.
// This is behaviorally the same as the other versions, but the PFD constructor does not
// dup() automatically, whereas PFD.fromSocket() and PDF.fromDatagramSocket() do dup().
try (ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(socket)) { try (ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(socket)) {
applyTransportModeTransform(pfd, direction, transform);
}
}
/* Call down to activate a transform */
private void applyTransportModeTransform(
ParcelFileDescriptor pfd, int direction, IpSecTransform transform) throws IOException {
try {
mService.applyTransportModeTransform(pfd, direction, transform.getResourceId()); mService.applyTransportModeTransform(pfd, direction, transform.getResourceId());
} catch (RemoteException e) { } catch (RemoteException e) {
throw e.rethrowFromSystemServer(); throw e.rethrowFromSystemServer();
@@ -433,9 +419,7 @@ public final class IpSecManager {
*/ */
public void removeTransportModeTransforms(Socket socket, IpSecTransform transform) public void removeTransportModeTransforms(Socket socket, IpSecTransform transform)
throws IOException { throws IOException {
try (ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket)) { removeTransportModeTransforms(socket.getFileDescriptor$(), transform);
removeTransportModeTransforms(pfd, transform);
}
} }
/** /**
@@ -455,9 +439,7 @@ public final class IpSecManager {
*/ */
public void removeTransportModeTransforms(DatagramSocket socket, IpSecTransform transform) public void removeTransportModeTransforms(DatagramSocket socket, IpSecTransform transform)
throws IOException { throws IOException {
try (ParcelFileDescriptor pfd = ParcelFileDescriptor.fromDatagramSocket(socket)) { removeTransportModeTransforms(socket.getFileDescriptor$(), transform);
removeTransportModeTransforms(pfd, transform);
}
} }
/** /**
@@ -478,13 +460,6 @@ public final class IpSecManager {
public void removeTransportModeTransforms(FileDescriptor socket, IpSecTransform transform) public void removeTransportModeTransforms(FileDescriptor socket, IpSecTransform transform)
throws IOException { throws IOException {
try (ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(socket)) { try (ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(socket)) {
removeTransportModeTransforms(pfd, transform);
}
}
/* Call down to remove a transform */
private void removeTransportModeTransforms(ParcelFileDescriptor pfd, IpSecTransform transform) {
try {
mService.removeTransportModeTransforms(pfd, transform.getResourceId()); mService.removeTransportModeTransforms(pfd, transform.getResourceId());
} catch (RemoteException e) { } catch (RemoteException e) {
throw e.rethrowFromSystemServer(); throw e.rethrowFromSystemServer();