11

I have an Android service trying to bind to a server socket port 24. As it is privileged port, it is failing with a bind exception. I am wondering what I need to do to get this working. I see this was asked couple of times in this forum, but without a resolution.

This service runs on a device that runs on Android. We build android platform for the device. We have all the control we need.

Reno
  • 33,594
  • 11
  • 89
  • 102
videoguy
  • 1,732
  • 2
  • 24
  • 49
  • 1
    Based on general Linux knowledge, only processes that run as root can listen on port 24. Can you even run Java apps as root? – Seva Alekseyev Oct 24 '11 at 03:02
  • Agreed. The process must run as root, or perhaps as a user ID with admin privileges (not sure if Android supports that Linux concept). Usually, if you control the device, you're not writing daemons in Java, but in C/C++, and starting them using normal Linux daemon support. – CommonsWare Oct 24 '11 at 05:55
  • 1
    The "su ..." way of elevating privileges is not an option as I am binding to a port. I am not trying to spawn a shell script. As every android app is basically a process forked from zygote, unless zygote elevates privilege based on SOMETHING, you can't bind. I am wondering if zygote allows that or not. If it does, what is that SOMETHING? – videoguy Oct 25 '11 at 03:15

2 Answers2

0

To bind to a port less than 1024, you need to be root and there are two ways of doing that.

  1. System app : The app should be installed into /system/apps folder from where it shall be run as root. However, there are some signature issues for which help is available. Distribution is also a problem with system apps.

  2. su binary : The su binary can be invoked using a simple exec("su -c [command]"). But things are easier said than done. You need to run an android processes not just a single command. Hence, there is libsuperuser which provides you the methods and has a full-length doc page.

Community
  • 1
  • 1
cnvzmxcvmcx
  • 1,061
  • 2
  • 15
  • 32
  • 1
    For the record, with 1 even if you become a system app, you still don't run as `root`, only `system`, and still can't bind low ports. – DuBistKomisch Jul 30 '19 at 08:23
  • So, does the proposed solution #1 (system app privileges) work or not? @DuBistKomisch's comment suggests that it doesn't but this is the accepted answer. Has anybody tried? – The Theoretical Astronaut Oct 27 '22 at 18:56
0

This is a very similar question: Run secure API calls as root, android, though they are trying to run as root for a different reason.

That made me wonder about installing 'system apps', and I saw this: https://android.stackexchange.com/questions/27/rooted-how-to-install-a-system-app

Community
  • 1
  • 1
P.T.
  • 24,557
  • 7
  • 64
  • 95