Merge "Remove ParcelFileDescriptor hidden API usage from IpSecService"

This commit is contained in:
Treehugger Robot
2022-01-06 13:40:26 +00:00
committed by Gerrit Code Review

View File

@@ -1236,37 +1236,53 @@ public class IpSecService extends IIpSecService.Stub {
int callingUid = Binder.getCallingUid(); int callingUid = Binder.getCallingUid();
UserRecord userRecord = mUserResourceTracker.getUserRecord(callingUid); UserRecord userRecord = mUserResourceTracker.getUserRecord(callingUid);
final int resourceId = mNextResourceId++; final int resourceId = mNextResourceId++;
FileDescriptor sockFd = null;
ParcelFileDescriptor pFd = null;
try { try {
if (!userRecord.mSocketQuotaTracker.isAvailable()) { if (!userRecord.mSocketQuotaTracker.isAvailable()) {
return new IpSecUdpEncapResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE); return new IpSecUdpEncapResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE);
} }
sockFd = Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); FileDescriptor sockFd = null;
mUidFdTagger.tag(sockFd, callingUid); try {
sockFd = Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
pFd = ParcelFileDescriptor.dup(sockFd);
} finally {
IoUtils.closeQuietly(sockFd);
}
mUidFdTagger.tag(pFd.getFileDescriptor(), callingUid);
// This code is common to both the unspecified and specified port cases // This code is common to both the unspecified and specified port cases
Os.setsockoptInt( Os.setsockoptInt(
sockFd, pFd.getFileDescriptor(),
OsConstants.IPPROTO_UDP, OsConstants.IPPROTO_UDP,
OsConstants.UDP_ENCAP, OsConstants.UDP_ENCAP,
OsConstants.UDP_ENCAP_ESPINUDP); OsConstants.UDP_ENCAP_ESPINUDP);
mNetd.ipSecSetEncapSocketOwner(new ParcelFileDescriptor(sockFd), callingUid); mNetd.ipSecSetEncapSocketOwner(pFd, callingUid);
if (port != 0) { if (port != 0) {
Log.v(TAG, "Binding to port " + port); Log.v(TAG, "Binding to port " + port);
Os.bind(sockFd, INADDR_ANY, port); Os.bind(pFd.getFileDescriptor(), INADDR_ANY, port);
} else { } else {
port = bindToRandomPort(sockFd); port = bindToRandomPort(pFd.getFileDescriptor());
} }
userRecord.mEncapSocketRecords.put( userRecord.mEncapSocketRecords.put(
resourceId, resourceId,
new RefcountedResource<EncapSocketRecord>( new RefcountedResource<EncapSocketRecord>(
new EncapSocketRecord(resourceId, sockFd, port), binder)); new EncapSocketRecord(resourceId, pFd.getFileDescriptor(), port),
return new IpSecUdpEncapResponse(IpSecManager.Status.OK, resourceId, port, sockFd); binder));
return new IpSecUdpEncapResponse(IpSecManager.Status.OK, resourceId, port,
pFd.getFileDescriptor());
} catch (IOException | ErrnoException e) { } catch (IOException | ErrnoException e) {
IoUtils.closeQuietly(sockFd); try {
if (pFd != null) {
pFd.close();
}
} catch (IOException ex) {
// Nothing can be done at this point
Log.e(TAG, "Failed to close pFd.");
}
} }
// If we make it to here, then something has gone wrong and we couldn't open a socket. // If we make it to here, then something has gone wrong and we couldn't open a socket.
// The only reasonable condition that would cause that is resource unavailable. // The only reasonable condition that would cause that is resource unavailable.