// examples/inert-sample/GuardService.java
//
// =========================================================================
// INERT TEACHING SAMPLE — chapter 69. THIS IS NOT MALWARE.
// This is the second half of the watchdog pair. Like the first half, the
// restart call has been REMOVED. It logs the peer it would have restarted.
// =========================================================================
//
// WHY THIS ONE IS THE INTERESTING HALF
//
// SentinelService is the visible arm: it would run in the foreground, post a
// notification, and be the thing a user finds when they go looking. This is
// the invisible arm. It is declared with
//
//     android:process=":guard"
//
// which asks the platform to run it in a SECOND process. Chapter 09 explains
// what that actually means at the operating-system level, and the summary
// matters here:
//
//   * The second process is forked from Zygote exactly as the first was. It
//     has the SAME uid, the SAME SELinux label, the SAME data directory and
//     the SAME granted permissions. A second process buys the app NOTHING in
//     terms of privilege. It is not a sandbox escape and it is not an
//     escalation.
//   * It gets its OWN Application instance, its OWN static fields, and its
//     OWN copy of every provider declared without android:multiprocess.
//     Developers get this wrong constantly, and malware relies on it: state
//     kept in a static field in one process is invisible to the other, so the
//     two arms coordinate through the filesystem or through Binder.
//   * It has its own oom_score_adj and is killed independently. That is the
//     entire point.
//
// So the value of the second process is survivability, not privilege. Say
// that precisely in a report. "The sample runs a second process to escalate
// privileges" is wrong and a reviewer will catch it. "The sample runs a
// second process so that a single kill does not stop it" is right.
//
// WHAT THE PLATFORM DOES ABOUT IT
//
// This is worth knowing because it bounds how severe the finding is:
//
//   * `am force-stop <pkg>` kills EVERY process of the package atomically and
//     also cancels its alarms and jobs, and puts the package back into the
//     STOPPED state where it receives no broadcasts. It defeats a pure
//     process pair completely. A user cannot easily do this; `adb` can.
//   * Since API 26, a background app cannot start a background service at
//     all. A watchdog therefore needs one of: a foreground service, a
//     JobScheduler job, an exact alarm, or an already-running process. This
//     is why the permission list of a modern persistent sample contains
//     FOREGROUND_SERVICE and REQUEST_IGNORE_BATTERY_OPTIMIZATIONS.
//   * Since API 31, an app in the background cannot start a foreground
//     service at all except through a short list of exemptions. This closed
//     the simplest form of the restart, and pushed samples toward
//     accessibility (which keeps the process alive by itself) and toward
//     JobScheduler.
//
// The trend line is the honest conclusion for the reader: each release makes
// the naive watchdog worse, and each release makes ACCESSIBILITY relatively
// more attractive, because an enabled accessibility service is a bound
// service the system itself keeps running and restarts.

package com.pentest.ch69.inert;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class GuardService extends Service {

    /** The peer this service would supervise. Named, never started. */
    static final String PEER = "com.pentest.ch69.inert.SentinelService";

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(BootstrapProvider.TAG, "GuardService.onCreate  pid="
                + android.os.Process.myPid() + "  process=:guard");
        Log.i(BootstrapProvider.TAG, "  note: this pid differs from the main "
                + "process pid, same uid, same SELinux label, same data dir.");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(BootstrapProvider.TAG, "GuardService.onStartCommand  peer=" + PEER);

        if (BootstrapProvider.INERT) {
            Log.i(BootstrapProvider.TAG,
                    "INERT=true: a live guard would register a "
                    + "ServiceConnection to " + PEER + " and call "
                    + "startForegroundService() from onServiceDisconnected(). "
                    + "That call is not present in this build.");
            return START_NOT_STICKY;
        }
        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        Log.i(BootstrapProvider.TAG, "GuardService.onDestroy — nothing brings "
                + "this back. Compare with a live pair, where the peer's "
                + "onServiceDisconnected would fire within milliseconds.");
        super.onDestroy();
    }
}
