summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRajesh Nyamagoud <nyamagoud@google.com>2023-03-02 13:33:29 +0000
committerRajesh Nyamagoud <nyamagoud@google.com>2023-03-02 13:33:29 +0000
commit1be9d2754cf955011c0e0cdb32fecde8883bb6e6 (patch)
treee9a458cb055a7f5c027e4eb44e2eabb5ed01ea4f
parenta71c519140e8ca3c0dbfd502947a66eccb410527 (diff)
downloadsecurity-android13-qpr3-c-s6-release.tar.gz
Limit the number of key descriptors sent back. Backporting - Ported https://r.android.com/2381494 Merged-In: I5e8543a25dd9f1bb504fbc23f35779da0a0153b0 Bug: 267270741 Test: Manual: Generate 2500 keys with a 200 characters name each. Change-Id: I6d370651441b4186b28cfed243975e64e3e8c85f
-rw-r--r--keystore2/src/utils.rs35
1 files changed, 34 insertions, 1 deletions
diff --git a/keystore2/src/utils.rs b/keystore2/src/utils.rs
index 9db2eb9d..08d3a8ef 100644
--- a/keystore2/src/utils.rs
+++ b/keystore2/src/utils.rs
@@ -279,7 +279,40 @@ pub fn list_key_entries(
);
result.sort_unstable();
result.dedup();
- Ok(result)
+
+ let mut items_to_return = 0;
+ let mut returned_bytes: usize = 0;
+ const RESPONSE_SIZE_LIMIT: usize = 358400;
+ // Estimate the transaction size to avoid returning more items than what
+ // could fit in a binder transaction.
+ for kd in result.iter() {
+ // 4 bytes for the Domain enum
+ // 8 bytes for the Namespace long.
+ returned_bytes += 4 + 8;
+ // Size of the alias string. Includes 4 bytes for length encoding.
+ if let Some(alias) = &kd.alias {
+ returned_bytes += 4 + alias.len();
+ }
+ // Size of the blob. Includes 4 bytes for length encoding.
+ if let Some(blob) = &kd.blob {
+ returned_bytes += 4 + blob.len();
+ }
+ // The binder transaction size limit is 1M. Empirical measurements show
+ // that the binder overhead is 60% (to be confirmed). So break after
+ // 350KB and return a partial list.
+ if returned_bytes > RESPONSE_SIZE_LIMIT {
+ log::warn!(
+ "Key descriptors list ({} items) may exceed binder \
+ size, returning {} items est {} bytes.",
+ result.len(),
+ items_to_return,
+ returned_bytes
+ );
+ break;
+ }
+ items_to_return += 1;
+ }
+ Ok(result[..items_to_return].to_vec())
}
/// This module provides helpers for simplified use of the watchdog module.