Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8a593a31 authored by Michael Runge's avatar Michael Runge Committed by Android Git Automerger
Browse files

am 044dca28: Merge "Add logging for kernel audit failures, including SELinux."

# By Geremy Condra
# Via Android (Google) Code Review (1) and Geremy Condra (1)
* commit '044dca28':
  Add logging for kernel audit failures, including SELinux.
parents 0229d081 044dca28
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -126,6 +126,7 @@ public class BootReceiver extends BroadcastReceiver {
                    -LOG_SIZE, "APANIC_CONSOLE");
            addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_threads",
                    -LOG_SIZE, "APANIC_THREADS");
            addAuditErrorsToDropBox(db, prefs, headers, -LOG_SIZE, "SYSTEM_AUDIT");
        } else {
            if (db != null) db.addText("SYSTEM_RESTART", headers);
        }
@@ -174,4 +175,32 @@ public class BootReceiver extends BroadcastReceiver {
        Slog.i(TAG, "Copying " + filename + " to DropBox (" + tag + ")");
        db.addText(tag, headers + FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n"));
    }

    private static void addAuditErrorsToDropBox(DropBoxManager db,  SharedPreferences prefs,
            String headers, int maxSize, String tag) throws IOException {
        if (db == null || !db.isTagEnabled(tag)) return;  // Logging disabled
        Slog.i(TAG, "Copying audit failures to DropBox");

        File file = new File("/proc/last_kmsg");
        long fileTime = file.lastModified();
        if (fileTime <= 0) return;  // File does not exist

        if (prefs != null) {
            long lastTime = prefs.getLong(tag, 0);
            if (lastTime == fileTime) return;  // Already logged this particular file
            // TODO: move all these SharedPreferences Editor commits
            // outside this function to the end of logBootEvents
            prefs.edit().putLong(tag, fileTime).apply();
        }

        String log = FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n");
        StringBuilder sb = new StringBuilder();
        for (String line : log.split("\n")) {
            if (line.contains("audit")) {
                sb.append(line + "\n");
            }
        }
        Slog.i(TAG, "Copied " + sb.toString().length() + " worth of audits to DropBox");
        db.addText(tag, headers + sb.toString());
    }
}