1 // SPDX-License-Identifier: GPL-2.0 2 3 // Copyright (C) 2025 Google LLC. 4 5 use kernel::{ 6 net::netlink::GENLMSG_DEFAULT_SIZE, 7 prelude::*, 8 seq_file::SeqFile, 9 seq_print, 10 str::BStr, 11 sync::atomic::{ordering::Relaxed, Atomic}, 12 sync::{Arc, SetOnce, SpinLock}, 13 task::{Kuid, Pid}, 14 time::{Instant, Monotonic}, 15 types::ScopeGuard, 16 uapi, 17 }; 18 19 use crate::{ 20 allocation::{Allocation, TranslatedFds}, 21 defs::*, 22 error::{BinderError, BinderResult, ErrorLocation}, 23 netlink::Report, 24 node::{Node, NodeRef}, 25 process::{Process, ProcessInner}, 26 ptr_align, 27 thread::{PushWorkRes, Thread}, 28 trace::trace_transaction_update_buffer_release, 29 BinderReturnWriter, DArc, DLArc, DTRWrap, DeliverToRead, 30 }; 31 32 kernel::impl_flags!( 33 /// Represents multiple transaction flags. 34 #[derive(Debug, Clone, Default, Copy, PartialEq, Eq, Zeroable)] 35 pub struct TransactionFlags(u32); 36 37 /// Represents a single transaction flag. 38 #[derive(Debug, Clone, Copy, PartialEq, Eq)] 39 pub enum TransactionFlag { 40 OneWay = TF_ONE_WAY, 41 AcceptFds = TF_ACCEPT_FDS, 42 ClearBuf = TF_CLEAR_BUF, 43 UpdateTxn = TF_UPDATE_TXN, 44 DeferComplete = TF_DEFER_COMPLETE, 45 } 46 ); 47 48 impl TransactionFlags { 49 /// Creates a `TransactionFlags` from a raw `u32` value. 50 pub(crate) fn from_bits(bits: u32) -> Self { 51 Self(bits) 52 } 53 54 /// Checks if the Oneway flag is set. 55 pub(crate) fn is_oneway(self) -> bool { 56 self.contains(TransactionFlag::OneWay) 57 } 58 } 59 60 const LOG_SIZE: usize = 32; 61 62 pub(crate) static TRANSACTION_LOG: TransactionLog = TransactionLog::new(); 63 pub(crate) static FAILED_TRANSACTION_LOG: TransactionLog = TransactionLog::new(); 64 65 #[derive(Copy, Clone)] 66 enum CallType { 67 Call, 68 Async, 69 Reply, 70 } 71 72 #[derive(Copy, Clone)] 73 pub(crate) struct TransactionLogEntry { 74 debug_id: usize, 75 call_type: CallType, 76 from_proc: Pid, 77 from_thread: Pid, 78 // The kernel ignores `target.handle` on replies (where `libbinder` passes `-1` / `0xffffffff`), 79 // and C Binder stores and prints this field as a signed `int` (`%d`). 80 target_handle: i32, 81 to_proc: Pid, 82 to_thread: Pid, 83 to_node: usize, 84 data_size: usize, 85 offsets_size: usize, 86 return_error_line: Option<ErrorLocation>, 87 return_error: u32, 88 return_error_param: i32, 89 context_name: [u8; 16], 90 } 91 92 impl TransactionLogEntry { 93 const fn empty() -> Self { 94 Self { 95 debug_id: 0, 96 call_type: CallType::Call, 97 from_proc: 0, 98 from_thread: 0, 99 target_handle: 0, 100 to_proc: 0, 101 to_thread: 0, 102 to_node: 0, 103 data_size: 0, 104 offsets_size: 0, 105 return_error_line: None, 106 return_error: 0, 107 return_error_param: 0, 108 context_name: [0; 16], 109 } 110 } 111 112 fn new(info: &TransactionInfo, ctx: &crate::Context) -> Self { 113 let call_type = if info.is_reply { 114 CallType::Reply 115 } else if info.is_oneway() { 116 CallType::Async 117 } else { 118 CallType::Call 119 }; 120 let name_bytes = ctx.name.to_bytes(); 121 let mut context_name = [0u8; 16]; 122 let len = usize::min(name_bytes.len(), context_name.len()); 123 context_name[..len].copy_from_slice(&name_bytes[..len]); 124 125 let failed = info.reply != 0 && info.reply != BR_TRANSACTION_PENDING_FROZEN; 126 Self { 127 debug_id: info.debug_id, 128 call_type, 129 from_proc: info.from_pid, 130 from_thread: info.from_tid, 131 target_handle: info.target_handle as i32, 132 to_proc: info.to_pid, 133 to_thread: info.to_tid, 134 to_node: info.to_node_debug_id, 135 data_size: info.data_size, 136 offsets_size: info.offsets_size, 137 return_error_line: if failed { info.error_line } else { None }, 138 return_error: if failed { info.reply } else { 0 }, 139 return_error_param: if failed { 140 info.errno.map_or(0, |e| e.to_errno()) 141 } else { 142 0 143 }, 144 context_name, 145 } 146 } 147 } 148 149 #[pin_data] 150 #[repr(align(64))] 151 struct TransactionLogSlot { 152 #[pin] 153 entry: SpinLock<TransactionLogEntry>, 154 } 155 156 pub(crate) struct TransactionLog { 157 cur: Atomic<usize>, 158 entries: SetOnce<Pin<KBox<[TransactionLogSlot; LOG_SIZE]>>>, 159 } 160 161 impl TransactionLog { 162 const fn new() -> Self { 163 Self { 164 cur: Atomic::new(0), 165 entries: SetOnce::new(), 166 } 167 } 168 169 pub(crate) fn init(&self) -> Result { 170 let entries = KBox::pin_init( 171 pin_init::pin_init_array_from_fn(|_| { 172 pin_init!(TransactionLogSlot { 173 entry <- kernel::new_spinlock!( 174 TransactionLogEntry::empty(), 175 "TransactionLog::entries" 176 ), 177 }) 178 }), 179 GFP_KERNEL, 180 )?; 181 self.entries.populate(entries); 182 Ok(()) 183 } 184 185 fn add(&self, entry: &TransactionLogEntry) { 186 let Some(entries) = self.entries.as_ref() else { 187 return; 188 }; 189 let idx = self.cur.fetch_add(1, Relaxed) % LOG_SIZE; 190 let mut slot = entries[idx].entry.lock(); 191 // Avoid overwriting a newer entry if the ring buffer wraps around between `fetch_add` and 192 // acquiring the lock. 193 // CAST: Overflowing behavior of this cast is intentional to handle `debug_id` wrap-around. 194 let diff = slot.debug_id.wrapping_sub(entry.debug_id) as isize; 195 if slot.debug_id == 0 || diff < 0 { 196 *slot = *entry; 197 } 198 } 199 200 pub(crate) fn debug_print(&self, m: &SeqFile) { 201 let Some(entries) = self.entries.as_ref() else { 202 return; 203 }; 204 let cur = self.cur.load(Relaxed); 205 for i in 0..LOG_SIZE { 206 let idx = cur.wrapping_add(i) % LOG_SIZE; 207 let entry = *entries[idx].entry.lock(); 208 if entry.debug_id == 0 { 209 continue; 210 } 211 let call_type = match entry.call_type { 212 CallType::Call => "call ", 213 CallType::Async => "async", 214 CallType::Reply => "reply", 215 }; 216 let ctx_name = match CStr::from_bytes_until_nul(&entry.context_name) { 217 Ok(cstr) => BStr::from_bytes(cstr.to_bytes()), 218 Err(_) => BStr::from_bytes(&entry.context_name), 219 }; 220 let return_error_line: &dyn kernel::fmt::Display = match &entry.return_error_line { 221 Some(line) => line, 222 None => &0, 223 }; 224 seq_print!( 225 m, 226 "{}: {} from {}:{} to {}:{} context {} node {} handle {} size {}:{} ret {}/{} l={}\n", 227 entry.debug_id, 228 call_type, 229 entry.from_proc, 230 entry.from_thread, 231 entry.to_proc, 232 entry.to_thread, 233 ctx_name, 234 entry.to_node, 235 entry.target_handle, 236 entry.data_size, 237 entry.offsets_size, 238 entry.return_error, 239 entry.return_error_param, 240 return_error_line, 241 ); 242 } 243 } 244 } 245 246 #[derive(Zeroable)] 247 pub(crate) struct TransactionInfo { 248 pub(crate) from_pid: Pid, 249 pub(crate) from_tid: Pid, 250 pub(crate) to_pid: Pid, 251 pub(crate) to_tid: Pid, 252 pub(crate) to_node_debug_id: usize, 253 pub(crate) code: u32, 254 pub(crate) flags: TransactionFlags, 255 pub(crate) data_ptr: UserPtr, 256 pub(crate) data_size: usize, 257 pub(crate) offsets_ptr: UserPtr, 258 pub(crate) offsets_size: usize, 259 pub(crate) buffers_size: usize, 260 pub(crate) target_handle: u32, 261 pub(crate) errno: Option<Error>, 262 pub(crate) reply: u32, 263 pub(crate) error_line: Option<ErrorLocation>, 264 pub(crate) oneway_spam_suspect: bool, 265 pub(crate) is_reply: bool, 266 pub(crate) debug_id: usize, 267 } 268 269 impl TransactionInfo { 270 #[inline] 271 pub(crate) fn is_oneway(&self) -> bool { 272 self.flags.is_oneway() 273 } 274 275 pub(crate) fn write_log(&self, ctx: &crate::Context) { 276 let entry = TransactionLogEntry::new(self, ctx); 277 TRANSACTION_LOG.add(&entry); 278 if self.reply != 0 && self.reply != BR_TRANSACTION_PENDING_FROZEN { 279 FAILED_TRANSACTION_LOG.add(&entry); 280 } 281 } 282 283 pub(crate) fn report_netlink(&self, reply: u32, ctx: &crate::Context) { 284 if let Err(err) = self.report_netlink_inner(reply, ctx) { 285 pr_warn_ratelimited!( 286 "{}:{} netlink report failed: {err:?}\n", 287 self.from_pid, 288 self.from_tid 289 ); 290 } 291 } 292 293 fn report_netlink_inner(&self, reply: u32, ctx: &crate::Context) -> kernel::error::Result { 294 if !Report::has_listeners() { 295 return Ok(()); 296 } 297 let mut report = Report::new(GENLMSG_DEFAULT_SIZE, 0, 0, GFP_KERNEL)?; 298 299 report.error(reply)?; 300 report.context(&ctx.name)?; 301 report.from_pid(self.from_pid as u32)?; 302 report.from_tid(self.from_tid as u32)?; 303 if self.to_pid != 0 { 304 report.to_pid(self.to_pid as u32)?; 305 } 306 if self.to_tid != 0 { 307 report.to_tid(self.to_tid as u32)?; 308 } 309 310 if self.is_reply { 311 report.is_reply()?; 312 } 313 report.flags(u32::from(self.flags))?; 314 report.code(self.code)?; 315 report.data_size(self.data_size as u32)?; 316 317 report.multicast(0, GFP_KERNEL)?; 318 Ok(()) 319 } 320 } 321 322 use core::mem::offset_of; 323 use kernel::bindings::rb_transaction_layout; 324 pub(crate) const TRANSACTION_LAYOUT: rb_transaction_layout = rb_transaction_layout { 325 debug_id: offset_of!(Transaction, debug_id), 326 code: offset_of!(Transaction, code), 327 flags: offset_of!(Transaction, flags), 328 from_thread: offset_of!(Transaction, from), 329 to_proc: offset_of!(Transaction, to), 330 target_node: offset_of!(Transaction, target_node), 331 }; 332 333 #[pin_data(PinnedDrop)] 334 pub(crate) struct Transaction { 335 pub(crate) debug_id: usize, 336 target_node: Option<DArc<Node>>, 337 pub(crate) from_parent: Option<DArc<Transaction>>, 338 pub(crate) from: Arc<Thread>, 339 pub(crate) to: Arc<Process>, 340 #[pin] 341 allocation: SpinLock<Option<Allocation>>, 342 is_outstanding: Atomic<bool>, 343 code: u32, 344 pub(crate) flags: TransactionFlags, 345 data_size: usize, 346 offsets_size: usize, 347 data_address: usize, 348 sender_euid: Kuid, 349 txn_security_ctx_off: Option<usize>, 350 start_time: Instant<Monotonic>, 351 } 352 353 kernel::list::impl_list_arc_safe! { 354 impl ListArcSafe<0> for Transaction { untracked; } 355 } 356 357 impl Transaction { 358 pub(crate) fn new( 359 node_ref: NodeRef, 360 from_parent: Option<DArc<Transaction>>, 361 from: &Arc<Thread>, 362 info: &mut TransactionInfo, 363 ) -> BinderResult<DLArc<Self>> { 364 let allow_fds = node_ref.node.flags & FLAT_BINDER_FLAG_ACCEPTS_FDS != 0; 365 let txn_security_ctx = node_ref.node.flags & FLAT_BINDER_FLAG_TXN_SECURITY_CTX != 0; 366 let mut txn_security_ctx_off = if txn_security_ctx { Some(0) } else { None }; 367 let to = node_ref.node.owner.clone(); 368 let mut alloc = from.copy_transaction_data( 369 to.clone(), 370 info, 371 info.debug_id, 372 allow_fds, 373 txn_security_ctx_off.as_mut(), 374 )?; 375 if info.is_oneway() { 376 if from_parent.is_some() { 377 pr_warn_ratelimited!("Oneway transaction should not be in a transaction stack.\n"); 378 return Err(EINVAL.into()); 379 } 380 alloc.set_info_oneway_node(node_ref.node.clone()); 381 } 382 if info.flags.contains(TransactionFlag::ClearBuf) { 383 alloc.set_info_clear_on_drop(); 384 } 385 let target_node = node_ref.node.clone(); 386 alloc.set_info_target_node(node_ref); 387 let data_address = alloc.ptr; 388 389 Ok(DTRWrap::arc_pin_init(pin_init!(Transaction { 390 debug_id: info.debug_id, 391 target_node: Some(target_node), 392 from_parent, 393 sender_euid: Kuid::current_euid(), 394 from: from.clone(), 395 to, 396 code: info.code, 397 flags: info.flags, 398 data_size: info.data_size, 399 offsets_size: info.offsets_size, 400 data_address, 401 allocation <- kernel::new_spinlock!(Some(alloc.success()), "Transaction::new"), 402 is_outstanding: Atomic::new(false), 403 txn_security_ctx_off, 404 start_time: Instant::now(), 405 }))?) 406 } 407 408 pub(crate) fn new_reply( 409 from: &Arc<Thread>, 410 to: Arc<Process>, 411 info: &mut TransactionInfo, 412 allow_fds: bool, 413 ) -> BinderResult<DLArc<Self>> { 414 let mut alloc = 415 from.copy_transaction_data(to.clone(), info, info.debug_id, allow_fds, None)?; 416 if info.flags.contains(TransactionFlag::ClearBuf) { 417 alloc.set_info_clear_on_drop(); 418 } 419 Ok(DTRWrap::arc_pin_init(pin_init!(Transaction { 420 debug_id: info.debug_id, 421 target_node: None, 422 from_parent: None, 423 sender_euid: Kuid::current_euid(), 424 from: from.clone(), 425 to, 426 code: info.code, 427 flags: info.flags, 428 data_size: info.data_size, 429 offsets_size: info.offsets_size, 430 data_address: alloc.ptr, 431 allocation <- kernel::new_spinlock!(Some(alloc.success()), "Transaction::new"), 432 is_outstanding: Atomic::new(false), 433 txn_security_ctx_off: None, 434 start_time: Instant::now(), 435 }))?) 436 } 437 438 #[inline(never)] 439 pub(crate) fn debug_print_inner(&self, m: &SeqFile, prefix: &str) { 440 seq_print!( 441 m, 442 "{}{}: from {}:{} to {} code {:x} flags {:x} elapsed {}ms", 443 prefix, 444 self.debug_id, 445 self.from.process.task.pid(), 446 self.from.id, 447 self.to.task.pid(), 448 self.code, 449 u32::from(self.flags), 450 self.start_time.elapsed().as_millis(), 451 ); 452 if let Some(target_node) = &self.target_node { 453 seq_print!(m, " node {}", target_node.debug_id); 454 } 455 seq_print!(m, " size {}:{}\n", self.data_size, self.offsets_size); 456 } 457 458 /// Determines if the transaction is stacked on top of the given transaction. 459 pub(crate) fn is_stacked_on(&self, onext: &Option<DArc<Self>>) -> bool { 460 match (&self.from_parent, onext) { 461 (None, None) => true, 462 (Some(from_parent), Some(next)) => Arc::ptr_eq(from_parent, next), 463 _ => false, 464 } 465 } 466 467 /// Returns a pointer to the next transaction on the transaction stack, if there is one. 468 pub(crate) fn clone_next(&self) -> Option<DArc<Self>> { 469 Some(self.from_parent.as_ref()?.clone()) 470 } 471 472 /// Searches in the transaction stack for a thread that belongs to the target process. This is 473 /// useful when finding a target for a new transaction: if the node belongs to a process that 474 /// is already part of the transaction stack, we reuse the thread. 475 fn find_target_thread(&self) -> Option<Arc<Thread>> { 476 let mut it = &self.from_parent; 477 while let Some(transaction) = it { 478 if Arc::ptr_eq(&transaction.from.process, &self.to) { 479 return Some(transaction.from.clone()); 480 } 481 it = &transaction.from_parent; 482 } 483 None 484 } 485 486 /// Searches in the transaction stack for a transaction originating at the given thread. 487 pub(crate) fn find_from(&self, thread: &Thread) -> Option<&DArc<Transaction>> { 488 let mut it = &self.from_parent; 489 while let Some(transaction) = it { 490 if core::ptr::eq(thread, transaction.from.as_ref()) { 491 return Some(transaction); 492 } 493 494 it = &transaction.from_parent; 495 } 496 None 497 } 498 499 pub(crate) fn set_outstanding(&self, to_process: &mut ProcessInner) { 500 // No race because this method is only called once. 501 if !self.is_outstanding.load(Relaxed) { 502 self.is_outstanding.store(true, Relaxed); 503 to_process.add_outstanding_txn(); 504 } 505 } 506 507 /// Decrement `outstanding_txns` in `to` if it hasn't already been decremented. 508 fn drop_outstanding_txn(&self) { 509 // No race because this is called at most twice, and one of the calls are in the 510 // destructor, which is guaranteed to not race with any other operations on the 511 // transaction. It also cannot race with `set_outstanding`, since submission happens 512 // before delivery. 513 if self.is_outstanding.load(Relaxed) { 514 self.is_outstanding.store(false, Relaxed); 515 self.to.drop_outstanding_txn(); 516 } 517 } 518 519 /// Submits the transaction to a work queue. Uses a thread if there is one in the transaction 520 /// stack, otherwise uses the destination process. 521 /// 522 /// Not used for replies. 523 pub(crate) fn submit(self: DLArc<Self>, info: &mut TransactionInfo) -> BinderResult { 524 // Defined before `process_inner` so that the destructor runs after releasing the lock. 525 let _t_outdated; 526 let _oneway_node; 527 528 let oneway = self.flags.is_oneway(); 529 let process = self.to.clone(); 530 let mut process_inner = process.inner.lock(); 531 532 self.set_outstanding(&mut process_inner); 533 534 if oneway { 535 if let Some(target_node) = self.target_node.clone() { 536 crate::trace::trace_transaction(false, &self, None); 537 if process_inner.is_frozen.is_frozen() { 538 process_inner.async_recv = true; 539 if self.flags.contains(TransactionFlag::UpdateTxn) { 540 if let Some(t_outdated) = 541 target_node.take_outdated_transaction(&self, &mut process_inner) 542 { 543 trace_transaction_update_buffer_release(t_outdated.debug_id); 544 let mut alloc_guard = t_outdated.allocation.lock(); 545 if let Some(alloc) = (*alloc_guard).as_mut() { 546 // Take the oneway node to prevent `Allocation::drop` from calling 547 // `pending_oneway_finished()`, which would be incorrect as this 548 // transaction is not being submitted. 549 _oneway_node = alloc.take_oneway_node(); 550 } 551 drop(alloc_guard); 552 // Save the transaction to be dropped after locks are released. 553 _t_outdated = t_outdated; 554 } 555 } 556 } 557 match target_node.submit_oneway(self, &mut process_inner) { 558 Ok(()) => {} 559 Err((err, work)) => { 560 drop(process_inner); 561 // Drop work after releasing process lock. 562 drop(work); 563 return Err(err); 564 } 565 } 566 567 if process_inner.is_frozen.is_frozen() { 568 return Err(BinderError::new_frozen_oneway()); 569 } else { 570 return Ok(()); 571 } 572 } else { 573 pr_err_ratelimited!("Failed to submit oneway transaction to node.\n"); 574 } 575 } 576 577 if process_inner.is_frozen.is_frozen() { 578 process_inner.sync_recv = true; 579 return Err(BinderError::new_frozen()); 580 } 581 582 let res = if let Some(thread) = self.find_target_thread() { 583 info.to_tid = thread.id; 584 crate::trace::trace_transaction(false, &self, Some(&thread.task)); 585 match thread.push_work(self) { 586 PushWorkRes::Ok => Ok(()), 587 PushWorkRes::OkNotifyPoll => { 588 process.notify_poll(true); 589 Ok(()) 590 } 591 PushWorkRes::FailedDead(me) => Err((BinderError::new_dead(), me)), 592 } 593 } else { 594 crate::trace::trace_transaction(false, &self, None); 595 process_inner.push_work(&process, self) 596 }; 597 drop(process_inner); 598 599 match res { 600 Ok(()) => Ok(()), 601 Err((err, work)) => { 602 // Drop work after releasing process lock. 603 drop(work); 604 Err(err) 605 } 606 } 607 } 608 609 /// Check whether one oneway transaction can supersede another. 610 pub(crate) fn can_replace(&self, old: &Transaction) -> bool { 611 if self.from.process.task.pid() != old.from.process.task.pid() { 612 return false; 613 } 614 615 let required = TransactionFlag::OneWay | TransactionFlag::UpdateTxn; 616 if !(self.flags.contains_all(required) && old.flags.contains_all(required)) { 617 return false; 618 } 619 620 let target_node_match = match (self.target_node.as_ref(), old.target_node.as_ref()) { 621 (None, None) => true, 622 (Some(tn1), Some(tn2)) => Arc::ptr_eq(tn1, tn2), 623 _ => false, 624 }; 625 626 self.code == old.code && self.flags == old.flags && target_node_match 627 } 628 629 fn prepare_file_list(&self) -> Result<TranslatedFds> { 630 let mut alloc = self.allocation.lock().take().ok_or(ESRCH)?; 631 632 match alloc.translate_fds() { 633 Ok(translated) => { 634 *self.allocation.lock() = Some(alloc); 635 Ok(translated) 636 } 637 Err(err) => { 638 // Free the allocation eagerly. 639 drop(alloc); 640 Err(err) 641 } 642 } 643 } 644 } 645 646 impl DeliverToRead for Transaction { 647 fn do_work( 648 self: DArc<Self>, 649 thread: &Thread, 650 writer: &mut BinderReturnWriter<'_>, 651 ) -> Result<bool> { 652 let send_failed_reply = ScopeGuard::new(|| { 653 if self.target_node.is_some() && !self.flags.is_oneway() { 654 let reply = Err(BR_FAILED_REPLY); 655 self.from.deliver_reply(reply, &self, None); 656 } 657 self.drop_outstanding_txn(); 658 }); 659 660 let files = if let Ok(list) = self.prepare_file_list() { 661 list 662 } else { 663 // On failure to process the list, we send a reply back to the sender and ignore the 664 // transaction on the recipient. 665 binder_debug!( 666 FailedTransaction, 667 "transaction {} to {} failed, fd fixups failed, size {}-{}", 668 self.debug_id, 669 self.to.task.pid(), 670 self.data_size, 671 self.offsets_size 672 ); 673 return Ok(true); 674 }; 675 676 let mut tr_sec = BinderTransactionDataSecctx::default(); 677 let tr = tr_sec.tr_data(); 678 if let Some(target_node) = &self.target_node { 679 let (ptr, cookie) = target_node.get_id(); 680 tr.target.ptr = ptr as uapi::binder_uintptr_t; 681 tr.cookie = cookie as uapi::binder_uintptr_t; 682 }; 683 tr.code = self.code; 684 tr.flags = u32::from(self.flags); 685 tr.data_size = self.data_size as uapi::binder_size_t; 686 tr.data.ptr.buffer = self.data_address as uapi::binder_uintptr_t; 687 tr.offsets_size = self.offsets_size as uapi::binder_size_t; 688 if tr.offsets_size > 0 { 689 tr.data.ptr.offsets = 690 (self.data_address + ptr_align(self.data_size).unwrap()) as uapi::binder_uintptr_t; 691 } 692 tr.sender_euid = self.sender_euid.into_uid_in_current_ns(); 693 tr.sender_pid = 0; 694 if self.target_node.is_some() && !self.flags.is_oneway() { 695 // Not a reply and not one-way. 696 tr.sender_pid = self.from.process.pid_in_current_ns(); 697 } 698 let code = if self.target_node.is_none() { 699 BR_REPLY 700 } else if self.txn_security_ctx_off.is_some() { 701 BR_TRANSACTION_SEC_CTX 702 } else { 703 BR_TRANSACTION 704 }; 705 706 // Write the transaction code and data to the user buffer. 707 writer.write_code(code)?; 708 if let Some(off) = self.txn_security_ctx_off { 709 tr_sec.secctx = (self.data_address + off) as u64; 710 writer.write_payload(&tr_sec)?; 711 } else { 712 writer.write_payload(&*tr)?; 713 } 714 715 let mut alloc = self.allocation.lock().take().ok_or(ESRCH)?; 716 717 // Dismiss the completion of transaction with a failure. No failure paths are allowed from 718 // here on out. 719 send_failed_reply.dismiss(); 720 721 // Commit files, and set FDs in FDA to be closed on buffer free. 722 let close_on_free = files.commit(); 723 alloc.set_info_close_on_free(close_on_free); 724 725 // It is now the user's responsibility to clear the allocation. 726 alloc.keep_alive(); 727 728 self.drop_outstanding_txn(); 729 730 crate::trace::trace_transaction_received(&self); 731 732 // When this is not a reply and not a oneway transaction, update `current_transaction`. If 733 // it's a reply, `current_transaction` has already been updated appropriately. 734 if self.target_node.is_some() && tr_sec.transaction_data.flags & TF_ONE_WAY == 0 { 735 thread.set_current_transaction(self); 736 } 737 738 Ok(false) 739 } 740 741 fn cancel(self: DArc<Self>) { 742 let allocation = self.allocation.lock().take(); 743 drop(allocation); 744 745 // If this is not a reply or oneway transaction, then send a dead reply. 746 if self.target_node.is_some() && !self.flags.is_oneway() { 747 let reply = Err(BR_DEAD_REPLY); 748 self.from.deliver_reply(reply, &self, None); 749 } else { 750 binder_debug!( 751 pid = self.to.task.pid(), 752 DeadTransaction, 753 "undelivered transaction {}, process died", 754 self.debug_id 755 ); 756 } 757 758 self.drop_outstanding_txn(); 759 } 760 761 fn should_sync_wakeup(&self) -> bool { 762 !self.flags.is_oneway() 763 } 764 765 fn debug_print(&self, m: &SeqFile, _prefix: &str, tprefix: &str) -> Result { 766 self.debug_print_inner(m, tprefix); 767 Ok(()) 768 } 769 } 770 771 #[pinned_drop] 772 impl PinnedDrop for Transaction { 773 fn drop(self: Pin<&mut Self>) { 774 self.drop_outstanding_txn(); 775 } 776 } 777