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

Commit 1198c4cf authored by Hui Peng's avatar Hui Peng Committed by Gerrit Code Review
Browse files

Merge changes from topic "addr_hiding"

* changes:
  Replace log statements in BluetoothDevice
  Add support for address hiding in java layer
parents 7c09ffe0 761c81b1
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();
    }
+68 −7
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;
@@ -1429,7 +1472,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
     * @return Bluetooth hardware address as string
     */
    public String getAddress() {
        if (DBG) Log.d(TAG, "mAddress: " + mAddress);
        if (DBG) Log.d(TAG, "getAddress: mAddress=" + getAddressForLogging());
        return mAddress;
    }

@@ -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;
    }

    /**
@@ -1824,7 +1885,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
            Log.e(TAG, "BT not enabled. Cannot cancel Remote Device bond");
            if (DBG) log(Log.getStackTraceString(new Throwable()));
        } else {
            Log.i(TAG, "cancelBondProcess() for device " + getAddress()
            Log.i(TAG, "cancelBondProcess() for device " + toStringForLogging()
                    + " called by pid: " + Process.myPid()
                    + " tid: " + Process.myTid());
            try {
@@ -1857,7 +1918,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
            Log.e(TAG, "BT not enabled. Cannot remove Remote Device bond");
            if (DBG) log(Log.getStackTraceString(new Throwable()));
        } else {
            Log.i(TAG, "removeBond() for device " + getAddress()
            Log.i(TAG, "removeBond() for device " + toStringForLogging()
                    + " called by pid: " + Process.myPid()
                    + " tid: " + Process.myTid());
            try {
@@ -1902,7 +1963,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
                    AttributionSource source = pairQuery.second.first;
                    BluetoothDevice device = pairQuery.second.second;
                    if (DBG) {
                        log("getBondState(" + device.getAnonymizedAddress() + ") uncached");
                        log("getBondState(" + device.toStringForLogging() + ") uncached");
                    }
                    try {
                        final SynchronousResultReceiver<Integer> recv =
@@ -1944,7 +2005,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @RequiresBluetoothConnectPermission
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public int getBondState() {
        if (DBG) log("getBondState(" + getAnonymizedAddress() + ")");
        if (DBG) log("getBondState(" + toStringForLogging() + ")");
        final IBluetooth service = getService();
        if (service == null) {
            Log.e(TAG, "BT not enabled. Cannot get bond state");
@@ -1978,7 +2039,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
            android.Manifest.permission.BLUETOOTH_PRIVILEGED,
    })
    public boolean canBondWithoutDialog() {
        if (DBG) log("canBondWithoutDialog, device: " + this);
        if (DBG) log("canBondWithoutDialog, device: " + toStringForLogging());
        final IBluetooth service = getService();
        final boolean defaultValue = false;
        if (service == null || !isBluetoothEnabled()) {
+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)")