Skip to content

Commit 8e147b2

Browse files
Ben KomaloAndroid (Google) Code Review
authored andcommitted
Merge "Expose getting encryptstate through IMountService"
2 parents 2b72d93 + 444eca2 commit 8e147b2

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

core/java/android/os/storage/IMountService.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public interface IMountService extends IInterface {
3636
/** Local-side IPC implementation stub class. */
3737
public static abstract class Stub extends Binder implements IMountService {
3838
private static class Proxy implements IMountService {
39-
private IBinder mRemote;
39+
private final IBinder mRemote;
4040

4141
Proxy(IBinder remote) {
4242
mRemote = remote;
@@ -589,6 +589,22 @@ public boolean isExternalStorageEmulated() throws RemoteException {
589589
return _result;
590590
}
591591

592+
public int getEncryptionState() throws RemoteException {
593+
Parcel _data = Parcel.obtain();
594+
Parcel _reply = Parcel.obtain();
595+
int _result;
596+
try {
597+
_data.writeInterfaceToken(DESCRIPTOR);
598+
mRemote.transact(Stub.TRANSACTION_getEncryptionState, _data, _reply, 0);
599+
_reply.readException();
600+
_result = _reply.readInt();
601+
} finally {
602+
_reply.recycle();
603+
_data.recycle();
604+
}
605+
return _result;
606+
}
607+
592608
public int decryptStorage(String password) throws RemoteException {
593609
Parcel _data = Parcel.obtain();
594610
Parcel _reply = Parcel.obtain();
@@ -741,6 +757,8 @@ public String getSecureContainerFilesystemPath(String id) throws RemoteException
741757

742758
static final int TRANSACTION_getSecureContainerFilesystemPath = IBinder.FIRST_CALL_TRANSACTION + 30;
743759

760+
static final int TRANSACTION_getEncryptionState = IBinder.FIRST_CALL_TRANSACTION + 31;
761+
744762
/**
745763
* Cast an IBinder object into an IMountService interface, generating a
746764
* proxy if needed.
@@ -1062,6 +1080,13 @@ public boolean onTransact(int code, Parcel data, Parcel reply,
10621080
reply.writeString(path);
10631081
return true;
10641082
}
1083+
case TRANSACTION_getEncryptionState: {
1084+
data.enforceInterface(DESCRIPTOR);
1085+
int result = getEncryptionState();
1086+
reply.writeNoException();
1087+
reply.writeInt(result);
1088+
return true;
1089+
}
10651090
}
10661091
return super.onTransact(code, data, reply, flags);
10671092
}
@@ -1222,6 +1247,21 @@ public void unmountObb(String filename, boolean force, IObbActionListener token,
12221247
*/
12231248
public boolean isExternalStorageEmulated() throws RemoteException;
12241249

1250+
/** The volume is not encrypted. */
1251+
static final int ENCRYPTION_STATE_NONE = 1;
1252+
/** The volume has been encrypted succesfully. */
1253+
static final int ENCRYPTION_STATE_OK = 0;
1254+
/** The volume is in a bad state. */
1255+
static final int ENCRYPTION_STATE_ERROR_UNKNOWN = -1;
1256+
/** The volume is in a bad state - partially encrypted. Data is likely irrecoverable. */
1257+
static final int ENCRYPTION_STATE_ERROR_INCOMPLETE = -2;
1258+
1259+
/**
1260+
* Determines the encryption state of the volume.
1261+
* @return a numerical value. See {@code ENCRYPTION_STATE_*} for possible values.
1262+
*/
1263+
public int getEncryptionState() throws RemoteException;
1264+
12251265
/**
12261266
* Decrypts any encrypted volumes.
12271267
*/

services/java/com/android/server/MountService.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ private void waitForReady() {
454454
}
455455
}
456456

457-
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
457+
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
458458
@Override
459459
public void onReceive(Context context, Intent intent) {
460460
String action = intent.getAction();
@@ -484,7 +484,7 @@ public void run() {
484484
synchronized (mVolumeStates) {
485485
Set<String> keys = mVolumeStates.keySet();
486486
count = keys.size();
487-
paths = (String[])keys.toArray(new String[count]);
487+
paths = keys.toArray(new String[count]);
488488
states = new String[count];
489489
for (int i = 0; i < count; i++) {
490490
states[i] = mVolumeStates.get(paths[i]);
@@ -1761,6 +1761,37 @@ public void unmountObb(String filename, boolean force, IObbActionListener token,
17611761
Slog.i(TAG, "Send to OBB handler: " + action.toString());
17621762
}
17631763

1764+
@Override
1765+
public int getEncryptionState() {
1766+
mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1767+
"no permission to access the crypt keeper");
1768+
1769+
waitForReady();
1770+
1771+
try {
1772+
ArrayList<String> rsp = mConnector.doCommand("cryptfs cryptocomplete");
1773+
String[] tokens = rsp.get(0).split(" ");
1774+
1775+
if (tokens == null || tokens.length != 2) {
1776+
// Unexpected.
1777+
Slog.w(TAG, "Unexpected result from cryptfs cryptocomplete");
1778+
return ENCRYPTION_STATE_ERROR_UNKNOWN;
1779+
}
1780+
1781+
return Integer.parseInt(tokens[1]);
1782+
1783+
} catch (NumberFormatException e) {
1784+
// Bad result - unexpected.
1785+
Slog.w(TAG, "Unable to parse result from cryptfs cryptocomplete");
1786+
return ENCRYPTION_STATE_ERROR_UNKNOWN;
1787+
} catch (NativeDaemonConnectorException e) {
1788+
// Something bad happened.
1789+
Slog.w(TAG, "Error in communicating with cryptfs in validating");
1790+
return ENCRYPTION_STATE_ERROR_UNKNOWN;
1791+
}
1792+
}
1793+
1794+
@Override
17641795
public int decryptStorage(String password) {
17651796
if (TextUtils.isEmpty(password)) {
17661797
throw new IllegalArgumentException("password cannot be empty");
@@ -2090,7 +2121,7 @@ abstract class ObbAction {
20902121
public void execute(ObbActionHandler handler) {
20912122
try {
20922123
if (DEBUG_OBB)
2093-
Slog.i(TAG, "Starting to execute action: " + this.toString());
2124+
Slog.i(TAG, "Starting to execute action: " + toString());
20942125
mRetries++;
20952126
if (mRetries > MAX_RETRIES) {
20962127
Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up");
@@ -2147,7 +2178,7 @@ protected void sendNewStatusOrIgnore(int status) {
21472178
}
21482179

21492180
class MountObbAction extends ObbAction {
2150-
private String mKey;
2181+
private final String mKey;
21512182

21522183
MountObbAction(ObbState obbState, String key) {
21532184
super(obbState);
@@ -2258,7 +2289,7 @@ public String toString() {
22582289
}
22592290

22602291
class UnmountObbAction extends ObbAction {
2261-
private boolean mForceUnmount;
2292+
private final boolean mForceUnmount;
22622293

22632294
UnmountObbAction(ObbState obbState, boolean force) {
22642295
super(obbState);

0 commit comments

Comments
 (0)