Merge "Don't crash the system server when a misconfigured agent adds a DSCP policy" into main

This commit is contained in:
Maciej Żenczykowski 2025-03-19 11:04:57 -07:00 • committed by Gerrit Code Review
commit 51ac9ff7c4
4 changed files with 31 additions and 1 deletions

View file

@ -233,6 +233,11 @@ public class DscpPolicyTracker {
*/
public void addDscpPolicy(NetworkAgentInfo nai, DscpPolicy policy) {
String iface = nai.linkProperties.getInterfaceName();
if (null == iface) {
Log.e(TAG, "DSCP policies are not supported on null interfaces.");
sendStatus(nai, policy.getPolicyId(), DSCP_POLICY_STATUS_REQUEST_DECLINED);
return;
}
if (!isEthernet(iface)) {
Log.e(TAG, "DSCP policies are not supported on raw IP interfaces.");
sendStatus(nai, policy.getPolicyId(), DSCP_POLICY_STATUS_REQUEST_DECLINED);

View file

@ -16,6 +16,8 @@
package com.android.net.module.util;
import androidx.annotation.NonNull;
import java.io.IOException;
/**
@ -33,7 +35,7 @@ public class TcUtils {
* @return true if the interface uses an ethernet L2 header.
* @throws IOException
*/
public static native boolean isEthernet(String iface) throws IOException;
public static native boolean isEthernet(@NonNull String iface) throws IOException;
/**
* Attach a tc bpf filter.

View file

@ -32,6 +32,10 @@ static void throwIOException(JNIEnv *env, const char *msg, int error) {
static jboolean com_android_net_module_util_TcUtils_isEthernet(JNIEnv *env,
jclass clazz,
jstring iface) {
if (nullptr == iface) {
jniThrowNullPointerException(env, "iface is null");
return false;
}
ScopedUtfChars interface(env, iface);
bool result = false;
int error = isEthernet(interface.c_str(), result);

View file

@ -757,6 +757,25 @@ class DscpPolicyTest {
assertEquals(IPPROTO_UDP, policy2.protocol)
assertParcelingIsLossless(policy2)
}
@Test
fun testSendDscpPolicyWithoutInterfaceName() {
val nc = NetworkCapabilities().apply {
addTransportType(TRANSPORT_TEST)
}
val agent = TestableNetworkAgent(
realContext,
handlerThread.looper,
nc,
LinkProperties() /* note: no interface name */,
NetworkAgentConfig.Builder().build()
)
agentsToCleanUp.add(agent)
runAsShell(MANAGE_TEST_NETWORKS) { agent.register() }
// Without the fix, this will crash the system with SIGSEGV.
agent.sendAddDscpPolicy(DscpPolicy.Builder(1, 1).build())
agent.expectCallback<OnDscpPolicyStatusUpdated>()
}
}
private fun ByteBuffer.readAsArray(): ByteArray {