1

After going through several posts I figured out I could send the intent with the following line of code (second line used for debugging):

int ret = system("am broadcast -a android.intent.action.MIKE_ACCESSED --user 0"); 

__android_log_print(ANDROID_LOG_DEBUG, "gxp18", "Shell command returned %i", ret);

Unfortunately, this returns always (No matter what is the command used in the system("...")):

Shell command returned 32512

Interestingly, I can successfully send the intent through adb using:

adb shell am broadcast -a android.intent.action.MIKE_ACCESSED

Notice! I am not using NDK. The piece of code I reported in the post is a modification of a portion of code in the Android Framework. In particular, it is part of one of the Android services. I am working with the AOSP and modifying part of the source code.

Giuseppe
  • 447
  • 2
  • 5
  • 14
  • I am hacking some of the system services in the android framework and I need to send an intent from c++ to java. Any other idea how to make this kind of signaling between a portion of code written in c++ and the other written in java? – Giuseppe Mar 07 '15 at 17:07
  • "hacking". You should really be using `Intent`s in Android's API. Sending an Intent from Android's NDK(JNI) or Android's SDK(Java) will not make a difference for what you are trying to do as it comes from the same PID. See @alijandro's answer. – Jared Burrows Mar 07 '15 at 23:18
  • Ok. Not sure what are u trying to convey. Could you suggest a solution? – Giuseppe Mar 07 '15 at 23:29
  • Are you using the Android NDK? Are you creating a binary to run on Android or you adding JNI to an Android app? Adb Shell works because it has the correct permissions. – Jared Burrows Mar 07 '15 at 23:48
  • @JaredBurrows I am not using NDK. The piece of code I reported in the post is a modification of a portion of code in the Android Framework. In particular, it is part of one of the Android services. I am working with the AOSP and modifying part of the source code. Thanks. – Giuseppe Mar 09 '15 at 15:20
  • You should add all that in the issue. – Jared Burrows Mar 09 '15 at 15:57
  • Please do not add "SOLVED" to the question. If the question is solved, accept the answer or write the answer yourself and accept it. This is not a forum :) – m0skit0 Mar 30 '15 at 13:50
  • @m0skit0 Good to know. I'll do it. – Giuseppe Mar 30 '15 at 13:51

2 Answers2

1

1) Disable SELinux (Set in Permissive mode) 2) Modify ActivityManagerNative.java to bypass userId check

Enjoy :)

Giuseppe
  • 447
  • 2
  • 5
  • 14
0

As I remembered, you cannot execute am command directly in your app due to the permission check in Android 5.0 version.

These two scenarios are different. The command you execute from shell has the shell UID and GID, which is like following.

uid=2000(shell) gid=2000(shell) groups=1003(graphics),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) context=u:r:shell:s0

The command you do in your code with system() call is executed with your app's UID and GID which has less permissions in comparison with shell UID and GID.

So you will end with a failed code. You could optimize your design to send this intent from your Java code. If you insist to do in your JNI, a better solution might be calling a static void method in your own Application. In that way, you don't have to worry about the Context which isn't accessible in JNI most of time.

public class MyApplication extends Application {

   private static Context context;

   @Override
   public void onCreate() {
       super.onCreate();
       context = this;
    }

    public static void sendIntent() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://www.google.com"));
        context.startActivity(intent);
    }
}
alijandro
  • 11,627
  • 2
  • 58
  • 74
  • Thank alijandro. I really need to send an intent from c++ to java. I am hacking some of the system services in android framework and I need a way to call a method written in java from a method written in c++. I don't have a lot of flexibility – Giuseppe Mar 07 '15 at 17:14
  • The code that I reported in my question is part of a service in the Android Framework. I am changing the source code. It is not part of an app. Most probably there are permission even for processes mapped to system services. How to change these permissions and allow my code to execute commands? – Giuseppe Mar 07 '15 at 21:05
  • @GiuseppePetracca If you are doing framework level programming also, try to dig the code from [am](https://android.googlesource.com/platform/frameworks/base/+/android-5.0.2_r1/cmds/am/). Meanwhile, is it your service is part of `com/android/server`, if so, there are a lots example to start activity under the package `com/android/server`. – alijandro Mar 07 '15 at 23:57