From d044dba50bdd21bb4cf26747a5fb5544948be475 Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Thu, 23 May 2019 13:55:47 -0700 Subject: [PATCH] Set permissions on adb key file after write. We're using AtomicFile to atomically update the key file, but that does so by creating a new file with 0600 permissions, and then moving it into place, which results in adbd not being able to read the file when not root. Manually set the permissions after we write to the file to prevent this. Also, while we're at it, delete the explicit creation of the file, since FileOutputStream does so automatically. Bug: http://b/132774621 Test: manual Change-Id: I286b90adfdc591c5e7d7e417ef06be287d77261e --- .../server/adb/AdbDebuggingManager.java | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/services/core/java/com/android/server/adb/AdbDebuggingManager.java b/services/core/java/com/android/server/adb/AdbDebuggingManager.java index bdbff3db943d5..4b48ef917744b 100644 --- a/services/core/java/com/android/server/adb/AdbDebuggingManager.java +++ b/services/core/java/com/android/server/adb/AdbDebuggingManager.java @@ -661,12 +661,6 @@ public class AdbDebuggingManager { return mTestUserKeyFile == null ? getAdbFile(ADB_KEYS_FILE) : mTestUserKeyFile; } - private void createKeyFile(File keyFile) throws IOException { - keyFile.createNewFile(); - FileUtils.setPermissions(keyFile.toString(), - FileUtils.S_IRUSR | FileUtils.S_IWUSR | FileUtils.S_IRGRP, -1, -1); - } - private void writeKey(String key) { try { File keyFile = getUserKeyFile(); @@ -675,14 +669,13 @@ public class AdbDebuggingManager { return; } - if (!keyFile.exists()) { - createKeyFile(keyFile); - } - FileOutputStream fo = new FileOutputStream(keyFile, true); fo.write(key.getBytes()); fo.write('\n'); fo.close(); + + FileUtils.setPermissions(keyFile.toString(), + FileUtils.S_IRUSR | FileUtils.S_IWUSR | FileUtils.S_IRGRP, -1, -1); } catch (IOException ex) { Slog.e(TAG, "Error writing key:" + ex); } @@ -698,10 +691,6 @@ public class AdbDebuggingManager { return; } - if (!keyFile.exists()) { - createKeyFile(keyFile); - } - atomicKeyFile = new AtomicFile(keyFile); fo = atomicKeyFile.startWrite(); for (String key : keys) { @@ -709,6 +698,9 @@ public class AdbDebuggingManager { fo.write('\n'); } atomicKeyFile.finishWrite(fo); + + FileUtils.setPermissions(keyFile.toString(), + FileUtils.S_IRUSR | FileUtils.S_IWUSR | FileUtils.S_IRGRP, -1, -1); } catch (IOException ex) { Slog.e(TAG, "Error writing keys: " + ex); if (atomicKeyFile != null) {