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

Commit 578d6963 authored by Hui Peng's avatar Hui Peng Committed by Automerger Merge Worker
Browse files

Add support for address hiding in java layer am: bcdae181

parents 834b9698 bcdae181
Loading
Loading
Loading
Loading
+15 −10
Original line number Original line Diff line number Diff line
@@ -15,25 +15,23 @@
 */
 */


#define LOG_TAG "BluetoothServiceJni"
#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 <dlfcn.h>
#include <errno.h>
#include <errno.h>
#include <fcntl.h>
#include <hardware/bluetooth.h>
#include <nativehelper/JNIPlatformHelp.h>
#include <pthread.h>
#include <pthread.h>
#include <string.h>
#include <string.h>

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


#include <hardware/bluetooth.h>
#include <nativehelper/JNIPlatformHelp.h>
#include <mutex>
#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;
using bluetooth::Uuid;
#ifndef DYNAMIC_LOAD_BLUETOOTH
#ifndef DYNAMIC_LOAD_BLUETOOTH
@@ -1810,6 +1808,7 @@ static jboolean allowLowLatencyAudioNative(JNIEnv* env, jobject obj,
    jniThrowIOException(env, EINVAL);
    jniThrowIOException(env, EINVAL);
    return false;
    return false;
  }
  }

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


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

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


int register_com_android_bluetooth_btservice_AdapterService(JNIEnv* env) {
int register_com_android_bluetooth_btservice_AdapterService(JNIEnv* env) {
+12 −0
Original line number Original line Diff line number Diff line
@@ -1707,6 +1707,16 @@ public class AdapterService extends Service {
            return Utils.getAddressStringFromByte(service.mAdapterProperties.getAddress());
            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
        @Override
        public void getUuids(AttributionSource source, SynchronousResultReceiver receiver) {
        public void getUuids(AttributionSource source, SynchronousResultReceiver receiver) {
            try {
            try {
@@ -4172,6 +4182,8 @@ public class AdapterService extends Service {
        return mAdapterProperties.getName();
        return mAdapterProperties.getName();
    }
    }


    private native boolean isLogRedactionEnabled();

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


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

    private AttributionSource mAttributionSource;
    private AttributionSource mAttributionSource;


    static IBluetooth getService() {
    static IBluetooth getService() {
@@ -1402,6 +1405,46 @@ public final class BluetoothDevice implements Parcelable, Attributable {
        return mAddress;
        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
    @Override
    public int describeContents() {
    public int describeContents() {
        return 0;
        return 0;
@@ -1456,7 +1499,25 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    @NonNull
    @NonNull
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED)
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED)
    public String getAnonymizedAddress() {
    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 Original line Diff line number Diff line
@@ -185,6 +185,6 @@ public final class BluetoothUtils {
        if (address == null || address.length() != 17) {
        if (address == null || address.length() != 17) {
            return null;
            return null;
        }
        }
        return "XX:XX:XX" + address.substring(8);
        return "XX:XX:XX:XX" + address.substring(11);
    }
    }
}
}
+2 −0
Original line number Original line 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})")
    @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);
    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)")
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)")
    oneway void getUuids(in AttributionSource attributionSource, in SynchronousResultReceiver receiver);
    oneway void getUuids(in AttributionSource attributionSource, in SynchronousResultReceiver receiver);
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)")
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)")