Skip to content

Commit 0a68650

Browse files
Darksonnfbq
authored andcommitted
rust: file: add Kuid wrapper
Adds a wrapper around `kuid_t` called `Kuid`. This allows us to define various operations on kuids such as equality and current_euid. It also lets us provide conversions from kuid into userspace values. Rust Binder needs these operations because it needs to compare kuids for equality, and it needs to tell userspace about the pid and uid of incoming transactions. Signed-off-by: Alice Ryhl <aliceryhl@google.com> Link: https://lore.kernel.org/r/20231206-alice-file-v2-5-af617c0d9d94@google.com
1 parent 71d8d22 commit 0a68650

File tree

4 files changed

+116
-3
lines changed

4 files changed

+116
-3
lines changed

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/errname.h>
1212
#include <linux/file.h>
1313
#include <linux/fs.h>
14+
#include <linux/pid_namespace.h>
1415
#include <linux/security.h>
1516
#include <linux/slab.h>
1617
#include <linux/refcount.h>

rust/helpers.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,51 @@ void rust_helper_put_task_struct(struct task_struct *t)
142142
}
143143
EXPORT_SYMBOL_GPL(rust_helper_put_task_struct);
144144

145+
kuid_t rust_helper_task_uid(struct task_struct *task)
146+
{
147+
return task_uid(task);
148+
}
149+
EXPORT_SYMBOL_GPL(rust_helper_task_uid);
150+
151+
kuid_t rust_helper_task_euid(struct task_struct *task)
152+
{
153+
return task_euid(task);
154+
}
155+
EXPORT_SYMBOL_GPL(rust_helper_task_euid);
156+
157+
#ifndef CONFIG_USER_NS
158+
uid_t rust_helper_from_kuid(struct user_namespace *to, kuid_t uid)
159+
{
160+
return from_kuid(to, uid);
161+
}
162+
EXPORT_SYMBOL_GPL(rust_helper_from_kuid);
163+
#endif /* CONFIG_USER_NS */
164+
165+
bool rust_helper_uid_eq(kuid_t left, kuid_t right)
166+
{
167+
return uid_eq(left, right);
168+
}
169+
EXPORT_SYMBOL_GPL(rust_helper_uid_eq);
170+
171+
kuid_t rust_helper_current_euid(void)
172+
{
173+
return current_euid();
174+
}
175+
EXPORT_SYMBOL_GPL(rust_helper_current_euid);
176+
177+
struct user_namespace *rust_helper_current_user_ns(void)
178+
{
179+
return current_user_ns();
180+
}
181+
EXPORT_SYMBOL_GPL(rust_helper_current_user_ns);
182+
183+
pid_t rust_helper_task_tgid_nr_ns(struct task_struct *tsk,
184+
struct pid_namespace *ns)
185+
{
186+
return task_tgid_nr_ns(tsk, ns);
187+
}
188+
EXPORT_SYMBOL_GPL(rust_helper_task_tgid_nr_ns);
189+
145190
struct kunit *rust_helper_kunit_get_current_test(void)
146191
{
147192
return kunit_get_current_test();

rust/kernel/cred.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
99
use crate::{
1010
bindings,
11+
task::Kuid,
1112
types::{AlwaysRefCounted, Opaque},
1213
};
1314

@@ -52,9 +53,9 @@ impl Credential {
5253
}
5354

5455
/// Returns the effective UID of the given credential.
55-
pub fn euid(&self) -> bindings::kuid_t {
56+
pub fn euid(&self) -> Kuid {
5657
// SAFETY: By the type invariant, we know that `self.0` is valid.
57-
unsafe { (*self.0.get()).euid }
58+
Kuid::from_raw(unsafe { (*self.0.get()).euid })
5859
}
5960
}
6061

rust/kernel/task.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
//! C header: [`include/linux/sched.h`](../../../../include/linux/sched.h).
66
77
use crate::{bindings, types::Opaque};
8-
use core::{marker::PhantomData, ops::Deref, ptr};
8+
use core::{
9+
cmp::{Eq, PartialEq},
10+
marker::PhantomData,
11+
ops::Deref,
12+
ptr,
13+
};
914

1015
/// Returns the currently running task.
1116
#[macro_export]
@@ -78,6 +83,12 @@ unsafe impl Sync for Task {}
7883
/// The type of process identifiers (PIDs).
7984
type Pid = bindings::pid_t;
8085

86+
/// The type of user identifiers (UIDs).
87+
#[derive(Copy, Clone)]
88+
pub struct Kuid {
89+
kuid: bindings::kuid_t,
90+
}
91+
8192
impl Task {
8293
/// Returns a task reference for the currently executing task/thread.
8394
///
@@ -132,12 +143,32 @@ impl Task {
132143
unsafe { *ptr::addr_of!((*self.0.get()).pid) }
133144
}
134145

146+
/// Returns the UID of the given task.
147+
pub fn uid(&self) -> Kuid {
148+
// SAFETY: By the type invariant, we know that `self.0` is valid.
149+
Kuid::from_raw(unsafe { bindings::task_uid(self.0.get()) })
150+
}
151+
152+
/// Returns the effective UID of the given task.
153+
pub fn euid(&self) -> Kuid {
154+
// SAFETY: By the type invariant, we know that `self.0` is valid.
155+
Kuid::from_raw(unsafe { bindings::task_euid(self.0.get()) })
156+
}
157+
135158
/// Determines whether the given task has pending signals.
136159
pub fn signal_pending(&self) -> bool {
137160
// SAFETY: By the type invariant, we know that `self.0` is valid.
138161
unsafe { bindings::signal_pending(self.0.get()) != 0 }
139162
}
140163

164+
/// Returns the given task's pid in the current pid namespace.
165+
pub fn pid_in_current_ns(&self) -> Pid {
166+
// SAFETY: Calling `task_active_pid_ns` with the current task is always safe.
167+
let namespace = unsafe { bindings::task_active_pid_ns(bindings::get_current()) };
168+
// SAFETY: We know that `self.0.get()` is valid by the type invariant.
169+
unsafe { bindings::task_tgid_nr_ns(self.0.get(), namespace) }
170+
}
171+
141172
/// Wakes up the task.
142173
pub fn wake_up(&self) {
143174
// SAFETY: By the type invariant, we know that `self.0.get()` is non-null and valid.
@@ -147,6 +178,41 @@ impl Task {
147178
}
148179
}
149180

181+
impl Kuid {
182+
/// Get the current euid.
183+
pub fn current_euid() -> Kuid {
184+
// SAFETY: Just an FFI call.
185+
Self::from_raw(unsafe { bindings::current_euid() })
186+
}
187+
188+
/// Create a `Kuid` given the raw C type.
189+
pub fn from_raw(kuid: bindings::kuid_t) -> Self {
190+
Self { kuid }
191+
}
192+
193+
/// Turn this kuid into the raw C type.
194+
pub fn into_raw(self) -> bindings::kuid_t {
195+
self.kuid
196+
}
197+
198+
/// Converts this kernel UID into a userspace UID.
199+
///
200+
/// Uses the namespace of the current task.
201+
pub fn into_uid_in_current_ns(self) -> bindings::uid_t {
202+
// SAFETY: Just an FFI call.
203+
unsafe { bindings::from_kuid(bindings::current_user_ns(), self.kuid) }
204+
}
205+
}
206+
207+
impl PartialEq for Kuid {
208+
fn eq(&self, other: &Kuid) -> bool {
209+
// SAFETY: Just an FFI call.
210+
unsafe { bindings::uid_eq(self.kuid, other.kuid) }
211+
}
212+
}
213+
214+
impl Eq for Kuid {}
215+
150216
// SAFETY: The type invariants guarantee that `Task` is always ref-counted.
151217
unsafe impl crate::types::AlwaysRefCounted for Task {
152218
fn inc_ref(&self) {

0 commit comments

Comments
 (0)