Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c291e00c authored by Wenhao Wang's avatar Wenhao Wang Committed by Android (Google) Code Review
Browse files

Merge changes from topic "intrusiondetection-cts-manager" into main

* changes:
  [IntrusionDetection] Refactor the NetworkLogSource to register callbacks directly to the system service.
  Call transport connection initialization in the ID service.
parents 36fb9c92 0d57fc43
Loading
Loading
Loading
Loading
+10 −23
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import com.android.server.ServiceThread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public class DataAggregator {
    private static final String TAG = "IntrusionDetection DataAggregator";
@@ -36,11 +37,10 @@ public class DataAggregator {
    private static final int MSG_DISABLE = 2;

    private static final int STORED_EVENTS_SIZE_LIMIT = 1024;
    private static final IntrusionDetectionAdminReceiver ADMIN_RECEIVER =
            new IntrusionDetectionAdminReceiver();

    private final IntrusionDetectionService mIntrusionDetectionService;
    private final ArrayList<DataSource> mDataSources;
    private final AtomicBoolean mIsLoggingInitialized = new AtomicBoolean(false);

    private Context mContext;
    private List<IntrusionDetectionEvent> mStoredEvents = new ArrayList<>();
@@ -59,30 +59,20 @@ public class DataAggregator {
        mHandler = new EventHandler(looper, this);
    }

    /**
     * Initialize DataSources
     * @return Whether the initialization succeeds.
     */
    public boolean initialize() {
        SecurityLogSource securityLogSource = new SecurityLogSource(mContext, this);
        mDataSources.add(securityLogSource);

        NetworkLogSource networkLogSource = new NetworkLogSource(mContext, this);
        ADMIN_RECEIVER.setNetworkLogEventCallback(networkLogSource);
        mDataSources.add(networkLogSource);

        for (DataSource ds : mDataSources) {
            if (!ds.initialize()) {
                return false;
            }
        }
        return true;
    /** Initialize DataSources */
    private void initialize() {
        mDataSources.add(new SecurityLogSource(mContext, this));
        mDataSources.add(new NetworkLogSource(mContext, this));
    }

    /**
     * Enable the data collection of all DataSources.
     */
    public void enable() {
        if (!mIsLoggingInitialized.get()) {
            initialize();
            mIsLoggingInitialized.set(true);
        }
        mHandlerThread = new ServiceThread(TAG, android.os.Process.THREAD_PRIORITY_BACKGROUND,
                /* allowIo */ false);
        mHandlerThread.start();
@@ -111,9 +101,6 @@ public class DataAggregator {
     */
    public void disable() {
        mHandler.obtainMessage(MSG_DISABLE).sendToTarget();
        for (DataSource ds : mDataSources) {
            ds.disable();
        }
    }

    private void onNewSingleData(IntrusionDetectionEvent event) {
+0 −5
Original line number Diff line number Diff line
@@ -17,11 +17,6 @@
package com.android.server.security.intrusiondetection;

public interface DataSource {
    /**
     * Initialize the data source.
     */
    boolean initialize();

    /**
     * Enable the data collection.
     */
+0 −42
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.security.intrusiondetection;

import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Slog;

public class IntrusionDetectionAdminReceiver extends DeviceAdminReceiver {
    private static final String TAG = "IntrusionDetectionAdminReceiver";

    private static NetworkLogSource sNetworkLogSource;

    @Override
    public void onNetworkLogsAvailable(
            Context context, Intent intent, long batchToken, int networkLogsCount) {
        if (sNetworkLogSource != null) {
            sNetworkLogSource.onNetworkLogsAvailable(batchToken);
        } else {
            Slog.w(TAG, "Network log receiver is not initialized");
        }
    }

    public void setNetworkLogEventCallback(NetworkLogSource networkLogSource) {
        sNetworkLogSource = networkLogSource;
    }
}
+7 −3
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ public class IntrusionDetectionEventTransportConnection implements ServiceConnec
    private static final String PRODUCTION_BUILD = "user";
    private static final String PROPERTY_BUILD_TYPE = "ro.build.type";
    private static final String PROPERTY_INTRUSION_DETECTION_SERVICE_NAME =
            "intrusiondetection_service_name";
            "debug.intrusiondetection_package_name";
    private static final long FUTURE_TIMEOUT_MILLIS = 60 * 1000; // 1 min
    private static final String TAG = "IntrusionDetectionEventTransportConnection";
    private final Context mContext;
@@ -147,9 +147,9 @@ public class IntrusionDetectionEventTransportConnection implements ServiceConnec
    }

    private String getSystemPropertyValue(String propertyName) {
        String comamandString = "getprop " + propertyName;
        String commandString = "getprop " + propertyName;
        try {
            Process process = Runtime.getRuntime().exec(comamandString);
            Process process = Runtime.getRuntime().exec(commandString);
            BufferedReader reader =
                    new BufferedReader(new InputStreamReader(process.getInputStream()));
            String propertyValue = reader.readLine();
@@ -175,6 +175,10 @@ public class IntrusionDetectionEventTransportConnection implements ServiceConnec
            mIntrusionDetectionEventTransportConfig =
                    getSystemPropertyValue(PROPERTY_INTRUSION_DETECTION_SERVICE_NAME);
        }
        Slog.d(
                TAG,
                "mIntrusionDetectionEventTransportConfig: "
                        + mIntrusionDetectionEventTransportConfig);

        if (TextUtils.isEmpty(mIntrusionDetectionEventTransportConfig)) {
            Slog.e(TAG, "Unable to find a valid config for the transport service");
+5 −9
Original line number Diff line number Diff line
@@ -232,12 +232,10 @@ public class IntrusionDetectionService extends SystemService {
            return;
        }

        // TODO: temporarily disable the following for the CTS IntrusionDetectionManagerTest.
        //  Enable it when the transport component is ready.
        // if (!mIntrusionDetectionEventTransportConnection.initialize()) {
        //     callback.onFailure(ERROR_TRANSPORT_UNAVAILABLE);
        //   return;
        // }
        if (!mIntrusionDetectionEventTransportConnection.initialize()) {
            callback.onFailure(ERROR_TRANSPORT_UNAVAILABLE);
            return;
        }

        mDataAggregator.enable();
        mState = STATE_ENABLED;
@@ -252,9 +250,7 @@ public class IntrusionDetectionService extends SystemService {
            return;
        }

        // TODO: temporarily disable the following for the CTS IntrusionDetectionManagerTest.
        //  Enable it when the transport component is ready.
        // mIntrusionDetectionEventTransportConnection.release();
        mIntrusionDetectionEventTransportConnection.release();
        mDataAggregator.disable();
        mState = STATE_DISABLED;
        notifyStateMonitors();
Loading