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

Commit bcdae181 authored by Hui Peng's avatar Hui Peng
Browse files

Add support for address hiding in java layer

- Add isLogRedactionEnabled API in IBluetooth
- Add toStringForLogging to BluetoothAddress
  which returns either redacted string or full address string
- In AdapterService add a private isLogRedactionEnabled API

Test: refactoring CL. Existing unit tests still pass
Bug: 174487588
Tag: #security
Change-Id: I08f4ec5632b067cca827aca81ee1706bf3ecdbae
parent 6c42d4b8
Loading
Loading
Loading
Loading
+15 −10
Original line number Diff line number Diff line
@@ -15,25 +15,23 @@
 */

#define LOG_TAG "BluetoothServiceJni"
#include "com_android_bluetooth.h"
#include "hardware/bt_sock.h"
#include "utils/Log.h"
#include "utils/misc.h"

#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <hardware/bluetooth.h>
#include <nativehelper/JNIPlatformHelp.h>
#include <pthread.h>
#include <string.h>

#include <fcntl.h>
#include <sys/prctl.h>
#include <sys/stat.h>

#include <hardware/bluetooth.h>
#include <nativehelper/JNIPlatformHelp.h>
#include <mutex>

#include <pthread.h>
#include "com_android_bluetooth.h"
#include "hardware/bt_sock.h"
#include "os/logging/log_redaction.h"
#include "utils/Log.h"
#include "utils/misc.h"

using bluetooth::Uuid;
#ifndef DYNAMIC_LOAD_BLUETOOTH
@@ -1810,6 +1808,7 @@ static jboolean allowLowLatencyAudioNative(JNIEnv* env, jobject obj,
    jniThrowIOException(env, EINVAL);
    return false;
  }

  RawAddress addr_obj = {};
  addr_obj.FromOctets((uint8_t*)addr);
  sBluetoothInterface->allow_low_latency_audio(allowed, addr_obj);
@@ -1845,6 +1844,11 @@ static void metadataChangedNative(JNIEnv* env, jobject obj, jbyteArray address,
  return;
}

static jboolean isLogRedactionEnabled(JNIEnv* env, jobject obj) {
  ALOGV("%s", __func__);
  return bluetooth::os::should_log_be_redacted();
}

static JNINativeMethod sMethods[] = {
    /* name, signature, funcPtr */
    {"classInitNative", "()V", (void*)classInitNative},
@@ -1888,6 +1892,7 @@ static JNINativeMethod sMethods[] = {
     (void*)requestMaximumTxDataLengthNative},
    {"allowLowLatencyAudioNative", "(Z[B)Z", (void*)allowLowLatencyAudioNative},
    {"metadataChangedNative", "([BI[B)V", (void*)metadataChangedNative},
    {"isLogRedactionEnabled", "()Z", (void*)isLogRedactionEnabled},
};

int register_com_android_bluetooth_btservice_AdapterService(JNIEnv* env) {
+12 −0
Original line number Diff line number Diff line
@@ -1707,6 +1707,16 @@ public class AdapterService extends Service {
            return Utils.getAddressStringFromByte(service.mAdapterProperties.getAddress());
        }

        @Override
        public boolean isLogRedactionEnabled() {
            AdapterService service = getService();
            if (service == null) {
                // by default return true
                return true;
            }
            return service.isLogRedactionEnabled();
        }

        @Override
        public void getUuids(AttributionSource source, SynchronousResultReceiver receiver) {
            try {
@@ -4172,6 +4182,8 @@ public class AdapterService extends Service {
        return mAdapterProperties.getName();
    }

    private native boolean isLogRedactionEnabled();

    public int getNameLengthForAdvertise() {
        return mAdapterProperties.getName().length();
    }
+62 −1
Original line number Diff line number Diff line
@@ -1310,6 +1310,9 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    private final String mAddress;
    @AddressType private final int mAddressType;

    private static boolean sIsLogRedactionFlagSynced = false;
    private static boolean sIsLogRedactionEnabled = true;

    private AttributionSource mAttributionSource;

    static IBluetooth getService() {
@@ -1402,6 +1405,46 @@ public final class BluetoothDevice implements Parcelable, Attributable {
        return mAddress;
    }

    private static boolean shouldLogBeRedacted() {
        boolean defaultValue = true;
        if (!sIsLogRedactionFlagSynced) {
            BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
            if (adapter == null || !adapter.isEnabled()) {
                return defaultValue;
            }
            IBluetooth service = adapter.getBluetoothService();

            if (service == null) {
                Log.e(TAG, "Bluetooth service is not enabled");
                return defaultValue;
            }

            try {
                sIsLogRedactionEnabled = service.isLogRedactionEnabled();
                sIsLogRedactionFlagSynced = true;
            } catch (RemoteException e) {
                // by default, set to true
                Log.e(TAG, "Failed to call IBluetooth.isLogRedactionEnabled"
                            + e.toString() + "\n"
                            + Log.getStackTraceString(new Throwable()));
                return true;
            }
        }
        return sIsLogRedactionEnabled;
    }

    /**
     * Returns a string representation of this BluetoothDevice for logging.
     * So far, this function only returns hardware address.
     * If more information is needed, add it here
     *
     * @return string representation of this BluetoothDevice used for logging
     * @hide
     */
    public String toStringForLogging() {
        return getAddressForLogging();
    }

    @Override
    public int describeContents() {
        return 0;
@@ -1456,7 +1499,25 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @NonNull
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED)
    public String getAnonymizedAddress() {
        return "XX:XX:XX" + getAddress().substring(8);
        return BluetoothUtils.toAnonymizedAddress(mAddress);
    }

    /**
     * Returns string representation of the hardware address of this BluetoothDevice
     * for logging purpose. Depending on the build type and device config,
     * this function returns either full address string (returned by getAddress),
     * or a redacted string with the leftmost 4 bytes shown as 'xx',
     * <p> For example, "xx:xx:xx:xx:aa:bb".
     * This function is intended to avoid leaking full address in logs.
     *
     * @return string representation of the hardware address for logging
     * @hide
     */
    public String getAddressForLogging() {
        if (shouldLogBeRedacted()) {
            return getAnonymizedAddress();
        }
        return mAddress;
    }

    /**
+1 −1
Original line number Diff line number Diff line
@@ -185,6 +185,6 @@ public final class BluetoothUtils {
        if (address == null || address.length() != 17) {
            return null;
        }
        return "XX:XX:XX" + address.substring(8);
        return "XX:XX:XX:XX" + address.substring(11);
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -54,6 +54,8 @@ interface IBluetooth

    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(allOf={android.Manifest.permission.BLUETOOTH_CONNECT,android.Manifest.permission.LOCAL_MAC_ADDRESS})")
    oneway void getAddress(in AttributionSource attributionSource, in SynchronousResultReceiver receiver);
    @JavaPassthrough(annotation="@android.annotation.RequiresNoPermission")
    boolean isLogRedactionEnabled();
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)")
    oneway void getUuids(in AttributionSource attributionSource, in SynchronousResultReceiver receiver);
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)")