Skip to content

Commit

Permalink
Merge pull request #354 from smartdevicelink/bugfix/issue_353
Browse files Browse the repository at this point in the history
Add protection against incorrect implementations of SdlRouterService
  • Loading branch information
joeygrover authored Nov 10, 2016
2 parents d7a2eb3 + 3f4f074 commit 4d99bfd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.Vector;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.smartdevicelink.util.AndroidTools;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.bluetooth.BluetoothAdapter;
Expand Down Expand Up @@ -189,7 +191,7 @@ private static boolean isRouterServiceRunning(Context context, boolean pingServi
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
//We will check to see if it contains this name, should be pretty specific
//Log.d(TAG, "Found Service: "+ service.service.getClassName());
if ((service.service.getClassName()).toLowerCase(Locale.US).contains(SDL_ROUTER_SERVICE_CLASS_NAME)) {
if ((service.service.getClassName()).toLowerCase(Locale.US).contains(SDL_ROUTER_SERVICE_CLASS_NAME) && AndroidTools.isServiceExported(context, service.service)) {

runningBluetoothServicePackage.add(service.service); //Store which instance is running
if(pingService){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
Expand Down Expand Up @@ -67,6 +70,7 @@
import com.smartdevicelink.transport.enums.TransportType;
import com.smartdevicelink.transport.utl.ByteAraryMessageAssembler;
import com.smartdevicelink.transport.utl.ByteArrayMessageSpliter;
import com.smartdevicelink.util.AndroidTools;
import com.smartdevicelink.util.BitConverter;

/**
Expand Down Expand Up @@ -223,7 +227,7 @@ public void onReceive(Context context, Intent intent)
if(tempService.name!=null){
sdlMultiList.remove(tempService.name.getPackageName());
}
if((localCompareTo == null || localCompareTo.isNewer(tempService))){
if((localCompareTo == null || localCompareTo.isNewer(tempService)) && AndroidTools.isServiceExported(context, tempService.name)){
LocalRouterService self = getLocalRouterService();
if(!self.isEqual(tempService)){ //We want to ignore self
Log.i(TAG, "Newer service received than previously stored service - " + tempService.launchIntent.getAction());
Expand Down Expand Up @@ -759,7 +763,7 @@ private boolean processCheck(){
return false;

}

@Override
public void onCreate() {
super.onCreate();
Expand All @@ -770,6 +774,11 @@ public void onCreate() {
stopSelf();
return;
}
if(!AndroidTools.isServiceExported(this, new ComponentName(this, this.getClass()))){ //We want to check to see if our service is actually exported
Log.e(TAG, "Service isn't exported. Shutting down");
stopSelf();
return;
}
else{Log.d(TAG, "We are in the correct process");}
synchronized(REGISTERED_APPS_LOCK){
registeredApps = new HashMap<Long,RegisteredApp>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

import java.lang.ref.WeakReference;

import com.smartdevicelink.util.AndroidTools;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
Expand Down Expand Up @@ -70,7 +75,7 @@ public void setFlags(int flags){
this.flags = flags;
}
public void checkIsConnected(){
if(!bindToService()){
if(!AndroidTools.isServiceExported(context,routerService) || !bindToService()){
//We are unable to bind to service
cb.onConnectionStatusUpdate(false, routerService, context);
unBindFromService();
Expand All @@ -87,6 +92,9 @@ private boolean bindToService(){
if(isBound){
return true;
}
if(clientMessenger == null){
return false;
}
Intent bindingIntent = new Intent();
bindingIntent.setClassName(this.routerService.getPackageName(), this.routerService.getClassName());//This sets an explicit intent
//Quickly make sure it's just up and running
Expand Down
25 changes: 25 additions & 0 deletions sdl_android_lib/src/com/smartdevicelink/util/AndroidTools.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.smartdevicelink.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.content.pm.PackageManager.NameNotFoundException;

public class AndroidTools {
/**
* Check to see if a component is exported
* @param Context object used to retrieve the package manager
* @param componentName of the component in question
* @return true if this component is tagged as exported
*/
public static boolean isServiceExported(Context context, ComponentName name) {
try {
ServiceInfo serviceInfo = context.getPackageManager().getServiceInfo(name, PackageManager.GET_META_DATA);
return serviceInfo.exported;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return false;
}
}

0 comments on commit 4d99bfd

Please sign in to comment.