Skip to content

Commit 8a4fed2

Browse files
authored
Merge pull request #577 from wedsonaf/security
rust: binder: use credentials in binder security callbacks.
2 parents e0734ef + ddbe5d2 commit 8a4fed2

File tree

4 files changed

+29
-60
lines changed

4 files changed

+29
-60
lines changed

drivers/android/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl Context {
4848
if manager.node.is_some() {
4949
return Err(Error::EBUSY);
5050
}
51-
security::binder_set_context_mgr(&node_ref.node.owner.task)?;
51+
security::binder_set_context_mgr(&node_ref.node.owner.cred)?;
5252

5353
// TODO: Get the actual caller id.
5454
let caller_uid = bindings::kuid_t::default();

drivers/android/process.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use core::{convert::TryFrom, mem::take, ops::Range};
44
use kernel::{
55
bindings, c_types,
6+
cred::Credential,
67
file::File,
78
file_operations::{FileOpener, FileOperations, IoctlCommand, IoctlHandler, PollTable},
89
io_buffer::{IoBufferReader, IoBufferWriter},
@@ -245,6 +246,9 @@ pub(crate) struct Process {
245246
// The task leader (process).
246247
pub(crate) task: Task,
247248

249+
// Credential associated with file when `Process` is created.
250+
pub(crate) cred: Credential,
251+
248252
// TODO: For now this a mutex because we have allocations in RangeAllocator while holding the
249253
// lock. We may want to split up the process state at some point to use a spin lock for the
250254
// other fields.
@@ -260,9 +264,10 @@ unsafe impl Send for Process {}
260264
unsafe impl Sync for Process {}
261265

262266
impl Process {
263-
fn new(ctx: Ref<Context>) -> Result<Ref<Self>> {
267+
fn new(ctx: Ref<Context>, cred: Credential) -> Result<Ref<Self>> {
264268
let mut process = Pin::from(UniqueRef::try_new(Self {
265269
ctx,
270+
cred,
266271
task: Task::current().group_leader().clone(),
267272
// SAFETY: `inner` is initialised in the call to `mutex_init` below.
268273
inner: unsafe { Mutex::new(ProcessInner::new()) },
@@ -802,8 +807,8 @@ impl IoctlHandler for Process {
802807
}
803808

804809
impl FileOpener<Ref<Context>> for Process {
805-
fn open(ctx: &Ref<Context>, _file: &File) -> Result<Self::Wrapper> {
806-
Self::new(ctx.clone())
810+
fn open(ctx: &Ref<Context>, file: &File) -> Result<Self::Wrapper> {
811+
Self::new(ctx.clone(), file.cred().clone())
807812
}
808813
}
809814

drivers/android/thread.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ impl Thread {
402402
strong,
403403
Some(self),
404404
)?;
405-
security::binder_transfer_binder(&self.process.task, &view.alloc.process.task)?;
405+
security::binder_transfer_binder(&self.process.cred, &view.alloc.process.cred)?;
406406
Ok(node)
407407
})?;
408408
}
@@ -412,7 +412,7 @@ impl Thread {
412412
// SAFETY: `handle` is a `u32`; any bit pattern is a valid representation.
413413
let handle = unsafe { obj.__bindgen_anon_1.handle } as _;
414414
let node = self.process.get_node_from_handle(handle, strong)?;
415-
security::binder_transfer_binder(&self.process.task, &view.alloc.process.task)?;
415+
security::binder_transfer_binder(&self.process.cred, &view.alloc.process.cred)?;
416416
Ok(node)
417417
})?;
418418
}
@@ -426,8 +426,8 @@ impl Thread {
426426
let fd = unsafe { obj.__bindgen_anon_1.fd };
427427
let file = File::from_fd(fd)?;
428428
security::binder_transfer_file(
429-
&self.process.task,
430-
&view.alloc.process.task,
429+
&self.process.cred,
430+
&view.alloc.process.cred,
431431
&file,
432432
)?;
433433
let field_offset =
@@ -618,7 +618,7 @@ impl Thread {
618618
fn oneway_transaction_inner(self: &Ref<Self>, tr: &BinderTransactionData) -> BinderResult {
619619
let handle = unsafe { tr.target.handle };
620620
let node_ref = self.process.get_transaction_node(handle)?;
621-
security::binder_transaction(&self.process.task, &node_ref.node.owner.task)?;
621+
security::binder_transaction(&self.process.cred, &node_ref.node.owner.cred)?;
622622
let completion = Ref::try_new(DeliverCode::new(BR_TRANSACTION_COMPLETE))?;
623623
let transaction = Transaction::new(node_ref, None, self, tr)?;
624624
self.inner.lock().push_work(completion);
@@ -630,7 +630,7 @@ impl Thread {
630630
fn transaction_inner(self: &Ref<Self>, tr: &BinderTransactionData) -> BinderResult {
631631
let handle = unsafe { tr.target.handle };
632632
let node_ref = self.process.get_transaction_node(handle)?;
633-
security::binder_transaction(&self.process.task, &node_ref.node.owner.task)?;
633+
security::binder_transaction(&self.process.cred, &node_ref.node.owner.cred)?;
634634
// TODO: We need to ensure that there isn't a pending transaction in the work queue. How
635635
// could this happen?
636636
let top = self.top_of_transaction_stack()?;

rust/kernel/security.rs

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,33 @@
44
//!
55
//! C header: [`include/linux/security.h`](../../../../include/linux/security.h).
66
7-
use crate::{file::File, task::Task, Result};
7+
use crate::{bindings, cred::Credential, file::File, to_result, Result};
88

99
/// Calls the security modules to determine if the given task can become the manager of a binder
1010
/// context.
11-
pub fn binder_set_context_mgr(_mgr: &Task) -> Result {
12-
// TODO
13-
//
14-
// // SAFETY: By the `Task` invariants, `mgr.ptr` is valid.
15-
// let ret = unsafe { bindings::security_binder_set_context_mgr(mgr.ptr) };
16-
// if ret != 0 {
17-
// Err(Error::from_kernel_errno(ret))
18-
// } else {
19-
// Ok(())
20-
// }
21-
22-
Ok(())
11+
pub fn binder_set_context_mgr(mgr: &Credential) -> Result {
12+
// SAFETY: By the `Credential` invariants, `mgr.ptr` is valid.
13+
to_result(|| unsafe { bindings::security_binder_set_context_mgr(mgr.ptr) })
2314
}
2415

2516
/// Calls the security modules to determine if binder transactions are allowed from task `from` to
2617
/// task `to`.
27-
pub fn binder_transaction(_from: &Task, _to: &Task) -> Result {
28-
// TODO
29-
//
30-
// // SAFETY: By the `Task` invariants, `from.ptr` and `to.ptr` are valid.
31-
// let ret = unsafe { bindings::security_binder_transaction(from.ptr, to.ptr) };
32-
// if ret != 0 {
33-
// Err(Error::from_kernel_errno(ret))
34-
// } else {
35-
// Ok(())
36-
// }
37-
38-
Ok(())
18+
pub fn binder_transaction(from: &Credential, to: &Credential) -> Result {
19+
// SAFETY: By the `Credential` invariants, `from.ptr` and `to.ptr` are valid.
20+
to_result(|| unsafe { bindings::security_binder_transaction(from.ptr, to.ptr) })
3921
}
4022

4123
/// Calls the security modules to determine if task `from` is allowed to send binder objects
4224
/// (owned by itself or other processes) to task `to` through a binder transaction.
43-
pub fn binder_transfer_binder(_from: &Task, _to: &Task) -> Result {
44-
// TODO
45-
//
46-
// // SAFETY: By the `Task` invariants, `from.ptr` and `to.ptr` are valid.
47-
// let ret = unsafe { bindings::security_binder_transfer_binder(from.ptr, to.ptr) };
48-
// if ret != 0 {
49-
// Err(Error::from_kernel_errno(ret))
50-
// } else {
51-
// Ok(())
52-
// }
53-
54-
Ok(())
25+
pub fn binder_transfer_binder(from: &Credential, to: &Credential) -> Result {
26+
// SAFETY: By the `Credential` invariants, `from.ptr` and `to.ptr` are valid.
27+
to_result(|| unsafe { bindings::security_binder_transfer_binder(from.ptr, to.ptr) })
5528
}
5629

5730
/// Calls the security modules to determine if task `from` is allowed to send the given file to
5831
/// task `to` (which would get its own file descriptor) through a binder transaction.
59-
pub fn binder_transfer_file(_from: &Task, _to: &Task, _file: &File) -> Result {
60-
// TODO
61-
//
62-
// // SAFETY: By the `Task` invariants, `from.ptr` and `to.ptr` are valid. Similarly, by the
63-
// // `File` invariants, `file.ptr` is also valid.
64-
// let ret = unsafe { bindings::security_binder_transfer_file(from.ptr, to.ptr, file.ptr) };
65-
// if ret != 0 {
66-
// Err(Error::from_kernel_errno(ret))
67-
// } else {
68-
// Ok(())
69-
// }
70-
71-
Ok(())
32+
pub fn binder_transfer_file(from: &Credential, to: &Credential, file: &File) -> Result {
33+
// SAFETY: By the `Credential` invariants, `from.ptr` and `to.ptr` are valid. Similarly, by the
34+
// `File` invariants, `file.ptr` is also valid.
35+
to_result(|| unsafe { bindings::security_binder_transfer_file(from.ptr, to.ptr, file.ptr) })
7236
}

0 commit comments

Comments
 (0)