API for apps to tag sockets with their own UID.
This enables app A to create a socket, pass it to app B, and have app B accept blame for the traffic performed on that socket. Also adds helpful public APIs for tagging raw FileDescriptor sockets instead of making developers go through shady SocketImpl wrappers. Test: cts-tradefed run commandAndExit cts-dev -m CtsAppSecurityHostTestCases -t android.appsecurity.cts.AppSecurityTests#testAppFailAccessPrivateData Bug: 63932076 Change-Id: I08925c843974675fc82e4080cec2eaab9ab7cd41
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
package android.net;
|
package android.net;
|
||||||
|
|
||||||
import android.annotation.RequiresPermission;
|
import android.annotation.RequiresPermission;
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
import android.annotation.SystemApi;
|
import android.annotation.SystemApi;
|
||||||
import android.app.DownloadManager;
|
import android.app.DownloadManager;
|
||||||
import android.app.backup.BackupManager;
|
import android.app.backup.BackupManager;
|
||||||
@@ -30,6 +31,8 @@ import com.android.server.NetworkManagementSocketTagger;
|
|||||||
|
|
||||||
import dalvik.system.SocketTagger;
|
import dalvik.system.SocketTagger;
|
||||||
|
|
||||||
|
import java.io.FileDescriptor;
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
@@ -263,15 +266,26 @@ public class TrafficStats {
|
|||||||
NetworkManagementSocketTagger.setThreadSocketStatsUid(uid);
|
NetworkManagementSocketTagger.setThreadSocketStatsUid(uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set specific UID to use when accounting {@link Socket} traffic
|
||||||
|
* originating from the current thread as the calling UID. Designed for use
|
||||||
|
* when another application is performing operations on your behalf.
|
||||||
|
* <p>
|
||||||
|
* Changes only take effect during subsequent calls to
|
||||||
|
* {@link #tagSocket(Socket)}.
|
||||||
|
*/
|
||||||
|
public static void setThreadStatsUidSelf() {
|
||||||
|
setThreadStatsUid(android.os.Process.myUid());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear any active UID set to account {@link Socket} traffic originating
|
* Clear any active UID set to account {@link Socket} traffic originating
|
||||||
* from the current thread.
|
* from the current thread.
|
||||||
*
|
*
|
||||||
* @see #setThreadStatsUid(int)
|
* @see #setThreadStatsUid(int)
|
||||||
* @hide
|
|
||||||
*/
|
*/
|
||||||
@SystemApi
|
@SystemApi
|
||||||
@RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS)
|
@SuppressLint("Doclava125")
|
||||||
public static void clearThreadStatsUid() {
|
public static void clearThreadStatsUid() {
|
||||||
NetworkManagementSocketTagger.setThreadSocketStatsUid(-1);
|
NetworkManagementSocketTagger.setThreadSocketStatsUid(-1);
|
||||||
}
|
}
|
||||||
@@ -315,6 +329,27 @@ public class TrafficStats {
|
|||||||
SocketTagger.get().untag(socket);
|
SocketTagger.get().untag(socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tag the given {@link FileDescriptor} socket with any statistics
|
||||||
|
* parameters active for the current thread. Subsequent calls always replace
|
||||||
|
* any existing parameters. When finished, call
|
||||||
|
* {@link #untagFileDescriptor(FileDescriptor)} to remove statistics
|
||||||
|
* parameters.
|
||||||
|
*
|
||||||
|
* @see #setThreadStatsTag(int)
|
||||||
|
*/
|
||||||
|
public static void tagFileDescriptor(FileDescriptor fd) throws IOException {
|
||||||
|
SocketTagger.get().tag(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove any statistics parameters from the given {@link FileDescriptor}
|
||||||
|
* socket.
|
||||||
|
*/
|
||||||
|
public static void untagFileDescriptor(FileDescriptor fd) throws IOException {
|
||||||
|
SocketTagger.get().untag(fd);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start profiling data usage for current UID. Only one profiling session
|
* Start profiling data usage for current UID. Only one profiling session
|
||||||
* can be active at a time.
|
* can be active at a time.
|
||||||
|
|||||||
Reference in New Issue
Block a user