Skip to content

Commit 7884043

Browse files
authored
Merge pull request #318 from wedsonaf/fail-fd
binder: Reject transactions containig FDs when they're not allowed.
2 parents 934f970 + b42fcef commit 7884043

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

drivers/android/thread.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ impl Thread {
378378
index_offset: usize,
379379
alloc: &Allocation,
380380
view: &AllocationView,
381+
allow_fds: bool,
381382
) -> BinderResult {
382383
let offset = alloc.read(index_offset)?;
383384
let header = view.read::<bindings::binder_object_header>(offset)?;
@@ -403,15 +404,26 @@ impl Thread {
403404
self.process.get_node_from_handle(handle, strong)
404405
})?;
405406
}
407+
BINDER_TYPE_FD => {
408+
if !allow_fds {
409+
return Err(BinderError::new_failed());
410+
}
411+
}
406412
_ => pr_warn!("Unsupported binder object type: {:x}\n", header.type_),
407413
}
408414
Ok(())
409415
}
410416

411-
fn translate_objects(&self, alloc: &mut Allocation, start: usize, end: usize) -> BinderResult {
417+
fn translate_objects(
418+
&self,
419+
alloc: &mut Allocation,
420+
start: usize,
421+
end: usize,
422+
allow_fds: bool,
423+
) -> BinderResult {
412424
let view = AllocationView::new(&alloc, start);
413425
for i in (start..end).step_by(size_of::<usize>()) {
414-
if let Err(err) = self.translate_object(i, alloc, &view) {
426+
if let Err(err) = self.translate_object(i, alloc, &view, allow_fds) {
415427
alloc.set_info(AllocationInfo { offsets: start..i });
416428
return Err(err);
417429
}
@@ -426,6 +438,7 @@ impl Thread {
426438
&self,
427439
to_process: &'a Process,
428440
tr: &BinderTransactionData,
441+
allow_fds: bool,
429442
) -> BinderResult<Allocation<'a>> {
430443
let data_size = tr.data_size as _;
431444
let adata_size = ptr_align(data_size);
@@ -450,7 +463,12 @@ impl Thread {
450463
alloc.copy_into(&mut reader, adata_size, offsets_size)?;
451464

452465
// Traverse the objects specified.
453-
self.translate_objects(&mut alloc, adata_size, adata_size + aoffsets_size)?;
466+
self.translate_objects(
467+
&mut alloc,
468+
adata_size,
469+
adata_size + aoffsets_size,
470+
allow_fds,
471+
)?;
454472
}
455473

456474
Ok(alloc)
@@ -540,7 +558,8 @@ impl Thread {
540558
(|| -> BinderResult<_> {
541559
let completion = Arc::try_new(DeliverCode::new(BR_TRANSACTION_COMPLETE))?;
542560
let process = orig.from.process.clone();
543-
let reply = Arc::try_new(Transaction::new_reply(self, process, tr)?)?;
561+
let allow_fds = orig.flags & TF_ACCEPT_FDS != 0;
562+
let reply = Arc::try_new(Transaction::new_reply(self, process, tr, allow_fds)?)?;
544563
self.inner.lock().push_work(completion);
545564
orig.from.deliver_reply(Either::Left(reply), &orig);
546565
Ok(())

drivers/android/transaction.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub(crate) struct Transaction {
2424
to: Ref<Process>,
2525
free_allocation: AtomicBool,
2626
code: u32,
27-
flags: u32,
27+
pub(crate) flags: u32,
2828
data_size: usize,
2929
offsets_size: usize,
3030
data_address: usize,
@@ -38,8 +38,9 @@ impl Transaction {
3838
from: &Arc<Thread>,
3939
tr: &BinderTransactionData,
4040
) -> BinderResult<Self> {
41+
let allow_fds = node_ref.node.flags & FLAT_BINDER_FLAG_ACCEPTS_FDS != 0;
4142
let to = node_ref.node.owner.clone();
42-
let alloc = from.copy_transaction_data(&to, tr)?;
43+
let alloc = from.copy_transaction_data(&to, tr, allow_fds)?;
4344
let data_address = alloc.ptr;
4445
alloc.keep_alive();
4546
Ok(Self {
@@ -61,8 +62,9 @@ impl Transaction {
6162
from: &Arc<Thread>,
6263
to: Ref<Process>,
6364
tr: &BinderTransactionData,
65+
allow_fds: bool,
6466
) -> BinderResult<Self> {
65-
let alloc = from.copy_transaction_data(&to, tr)?;
67+
let alloc = from.copy_transaction_data(&to, tr, allow_fds)?;
6668
let data_address = alloc.ptr;
6769
alloc.keep_alive();
6870
Ok(Self {

0 commit comments

Comments
 (0)