Skip to content

Commit 9a4cbc2

Browse files
MhmRddXiaoTong6666
authored andcommitted
Add dir class to sepolicy and crash safety for binder interceptors
(cherry picked from commit 7a7e362)
1 parent 74263c4 commit 9a4cbc2

3 files changed

Lines changed: 55 additions & 7 deletions

File tree

app/src/main/cpp/binder_interceptor.cpp

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,11 @@ class BinderStub : public BBinder {
314314
}
315315

316316
if (!found_context) {
317-
LOGW("BinderStub received transaction but no context found for thread");
317+
LOGW("BinderStub received transaction but no context found for thread (code=%u)", code);
318+
#ifndef NDEBUG
319+
std::lock_guard<std::mutex> dbg_lock(g_thread_context_mutex);
320+
LOGW(" Thread context map has %zu entries", g_thread_context_map.size());
321+
#endif
318322
return UNKNOWN_TRANSACTION;
319323
}
320324

@@ -400,6 +404,9 @@ void inspectAndRewriteTransaction(binder_transaction_data *txn_data) {
400404
}
401405
// Manually release the temporary strong reference we acquired at the start.
402406
target_binder_ptr->decStrong(nullptr);
407+
} else {
408+
LOGD("[Hook] attemptIncStrong failed for target %p (code=%u, uid=%d) — binder may be dying",
409+
reinterpret_cast<void*>(txn_data->target.ptr), txn_data->code, txn_data->sender_euid);
403410
}
404411
}
405412

@@ -416,7 +423,13 @@ void inspectAndRewriteTransaction(binder_transaction_data *txn_data) {
416423

417424
// Store context for the stub to retrieve later in its onTransact
418425
std::lock_guard<std::mutex> lock(g_thread_context_mutex);
419-
g_thread_context_map[std::this_thread::get_id()].push(std::move(info));
426+
auto &queue = g_thread_context_map[std::this_thread::get_id()];
427+
queue.push(std::move(info));
428+
#ifndef NDEBUG
429+
if (queue.size() > 8) {
430+
LOGW("[Hook] Thread context queue depth=%zu for thread — possible leak", queue.size());
431+
}
432+
#endif
420433
}
421434
}
422435

@@ -613,8 +626,24 @@ bool BinderInterceptor::processInterceptedTransaction(uint64_t tx_id, sp<BBinder
613626
Parcel pre_req, pre_resp;
614627
writeTransactionData(pre_req, tx_id, target, code, flags, request);
615628

616-
if (callback->transact(intercept::kPreTransact, pre_req, &pre_resp) != OK) {
617-
LOGW("[TX_ID: %" PRIu64 "] Pre-transaction callback failed. Forwarding original call.", tx_id);
629+
#ifndef NDEBUG
630+
struct timespec ts_start{};
631+
clock_gettime(CLOCK_MONOTONIC, &ts_start);
632+
#endif
633+
634+
status_t pre_cb_status = callback->transact(intercept::kPreTransact, pre_req, &pre_resp);
635+
636+
#ifndef NDEBUG
637+
struct timespec ts_end{};
638+
clock_gettime(CLOCK_MONOTONIC, &ts_end);
639+
double pre_ms = (ts_end.tv_sec - ts_start.tv_sec) * 1000.0 + (ts_end.tv_nsec - ts_start.tv_nsec) / 1e6;
640+
if (pre_ms > 5000.0) {
641+
LOGW("[TX_ID: %" PRIu64 "] Pre-callback took %.0fms (code=%u) — possible hang", tx_id, pre_ms, code);
642+
}
643+
#endif
644+
645+
if (pre_cb_status != OK) {
646+
LOGW("[TX_ID: %" PRIu64 "] Pre-transaction callback failed (status=%d). Forwarding original call.", tx_id, pre_cb_status);
618647
return false; // Callback failed, proceed as if not intercepted
619648
}
620649

@@ -648,8 +677,10 @@ bool BinderInterceptor::processInterceptedTransaction(uint64_t tx_id, sp<BBinder
648677
if (action == intercept::kActionOverrideData) {
649678
size_t size = pre_resp.readUint64();
650679
final_request.appendFrom(&pre_resp, pre_resp.dataPosition(), size);
680+
} else if (action == intercept::kActionContinue) {
681+
final_request.appendFrom(&request, 0, request.dataSize());
651682
} else {
652-
// Default (kActionContinue): Use original data
683+
LOGW("[TX_ID: %" PRIu64 "] Unknown pre-callback action %d (code=%u). Forwarding original data.", tx_id, action, code);
653684
final_request.appendFrom(&request, 0, request.dataSize());
654685
}
655686

@@ -668,14 +699,18 @@ bool BinderInterceptor::processInterceptedTransaction(uint64_t tx_id, sp<BBinder
668699
VALIDATE_STATUS(tx_id, post_req.appendFrom(reply, 0, reply_size));
669700
}
670701

671-
if (callback->transact(intercept::kPostTransact, post_req, &post_resp) == OK) {
702+
status_t post_cb_status = callback->transact(intercept::kPostTransact, post_req, &post_resp);
703+
if (post_cb_status == OK) {
672704
int32_t post_action = post_resp.readInt32();
673705
if (post_action == intercept::kActionOverrideReply && reply) {
674706
result = post_resp.readInt32(); // Read new status
675707
size_t new_size = post_resp.readUint64();
676708
reply->setDataSize(0); // Clear original reply
677709
VALIDATE_STATUS(tx_id, reply->appendFrom(&post_resp, post_resp.dataPosition(), new_size));
678710
}
711+
} else {
712+
LOGW("[TX_ID: %" PRIu64 "] Post-transaction callback failed (status=%d, code=%u). Using original reply.",
713+
tx_id, post_cb_status, code);
679714
}
680715

681716
return true; // We handled the flow, even if we just forwarded it

app/src/main/java/org/matrix/TEESimulator/interception/core/BinderInterceptor.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ abstract class BinderInterceptor : Binder() {
148148
callingPid,
149149
transactionData,
150150
)
151+
} catch (e: Exception) {
152+
SystemLogger.error(
153+
"[TX_ID: $txId] onPreTransact crashed (code=$transactionCode, uid=$callingUid)",
154+
e,
155+
)
156+
TransactionResult.ContinueAndSkipPost
151157
} finally {
152158
transactionData.recycle()
153159
}
@@ -191,6 +197,12 @@ abstract class BinderInterceptor : Binder() {
191197
reply,
192198
resultCode,
193199
)
200+
} catch (e: Exception) {
201+
SystemLogger.error(
202+
"[TX_ID: $txId] onPostTransact crashed (code=$transactionCode, uid=$callingUid, resultCode=${data.readInt()})",
203+
e,
204+
)
205+
TransactionResult.SkipTransaction
194206
} finally {
195207
transactionData.recycle()
196208
transactionReply.recycle()

module/sepolicy.rule

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
allow keystore {adb_data_file shell_data_file} file *
1+
allow keystore {adb_data_file shell_data_file} {file dir} *
22
allow crash_dump keystore process *
3+
allow crash_dump keystore {dir file lnk_file} *

0 commit comments

Comments
 (0)