这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@

<service
android:name=".app.TermuxService"
android:exported="false" />
android:exported="true"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enabling export of a service, activity, receiver, provider allows other apps to call them without any permission. This will allow any random app to run commands in termux without user permission, and even with root if termux has root permissions, i.e you are creating a privilege escalation vulnerability.

android:enabled="true" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


<service
android:name=".app.RunCommandService"
Expand Down
27 changes: 25 additions & 2 deletions app/src/main/java/com/termux/app/TermuxActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
Expand Down Expand Up @@ -198,6 +199,20 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo
@Override
public void onCreate(Bundle savedInstanceState) {
Logger.logDebug(LOG_TAG, "onCreate");

// For details see:
Copy link
Member

@agnostic-apollo agnostic-apollo Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TermuxService requests the permission if its required to start foreground terminal session from background. Permissions must not be requested unless needed.

// For android >= 10, apps require Display over other apps permission to start foreground activities

// https://stackoverflow.com/a/19856267/1455694
// https://stackoverflow.com/a/63250729/1455694
// https://www.reddit.com/r/tasker/comments/d7whyj/android_10_and_auto_starting_apps/
// https://stackoverflow.com/q/64642362/1455694
Context context = getApplicationContext();
// Q is Android 10
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (!Settings.canDrawOverlays(context)) {
startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION));
}
}

mIsOnResumeAfterOnCreate = true;

if (savedInstanceState != null)
Expand Down Expand Up @@ -257,9 +272,17 @@ public void onCreate(Bundle savedInstanceState) {
FileReceiverActivity.updateFileReceiverActivityComponentsState(this);

try {
Intent serviceIntent;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one issue that has already been solved locally and fix will be available in next version.

// Start the {@link TermuxService} and make it run regardless of who is bound to it
Intent serviceIntent = new Intent(this, TermuxService.class);
startService(serviceIntent);
// O is Oreo, Android 8
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
serviceIntent = new Intent(context, TermuxService.class);
context.startForegroundService(serviceIntent);
}
else {
serviceIntent = new Intent(this, TermuxService.class);
startService(serviceIntent);
}

// Attempt to bind to the service, this will call the {@link #onServiceConnected(ComponentName, IBinder)}
// callback if it succeeds.
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/com/termux/app/TermuxService.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,9 @@ public int onStartCommand(Intent intent, int flags, int startId) {
break;
}
}

// If this service really do get killed, there is no point restarting it automatically - let the user do on next
// start of {@link Term):
return Service.START_NOT_STICKY;
// If Service.START_NOT_STICKY is used this service will be killed when device not connected to charger
// (probably only for Xiaomi Redmi 7A with Android 10)
return Service.START_STICKY;
}

@Override
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/termux/app/event/SystemEventReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.termux.app.TermuxService;
import com.termux.shared.data.IntentUtils;
import com.termux.shared.logger.Logger;
import com.termux.shared.termux.TermuxUtils;
Expand Down Expand Up @@ -51,8 +53,20 @@ public void onReceive(@NonNull Context context, @Nullable Intent intent) {
}
}

private void runTermuxService(@NonNull Context context) {
Intent serviceIntent = new Intent(context, TermuxService.class);
// O is Oreo, Android 8
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(serviceIntent);
}
else {
context.startService(serviceIntent);
}
}

public synchronized void onActionBootCompleted(@NonNull Context context, @NonNull Intent intent) {
TermuxShellManager.onActionBootCompleted(context, intent);
runTermuxService(context);
}

public synchronized void onActionPackageUpdated(@NonNull Context context, @NonNull Intent intent) {
Expand Down