Skip to content

Commit d436ff5

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. To read kuids from a `struct task_struct`, you must currently use various #defines that perform the appropriate field access under an RCU read lock. Currently, we do not have a Rust wrapper for rcu_read_lock, which means that for this patch, there are two ways forward: 1. Inline the methods into Rust code, and use __rcu_read_lock directly rather than the rcu_read_lock wrapper. This gives up lockdep for these usages of RCU. 2. Wrap the various #defines in helpers and call the helpers from Rust. This patch uses the second option. One possible disadvantage of the second option is the possible introduction of speculation gadgets, but as discussed in [1], the risk appears to be acceptable. Of course, once a wrapper for rcu_read_lock is available, it is preferable to use that over either of the two above approaches. Link: https://lore.kernel.org/all/202312080947.674CD2DC7@keescook/ [1] Reviewed-by: Benno Lossin <[email protected]> Reviewed-by: Martin Rodriguez Reboredo <[email protected]> Reviewed-by: Trevor Gross <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 6a000c7 commit d436ff5

File tree

4 files changed

+115
-2
lines changed

4 files changed

+115
-2
lines changed

rust/bindings/bindings_helper.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/jiffies.h>
2020
#include <linux/mdio.h>
2121
#include <linux/phy.h>
22+
#include <linux/pid_namespace.h>
2223
#include <linux/refcount.h>
2324
#include <linux/sched.h>
2425
#include <linux/security.h>

rust/helpers.c

+45
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,51 @@ void rust_helper_put_task_struct(struct task_struct *t)
178178
}
179179
EXPORT_SYMBOL_GPL(rust_helper_put_task_struct);
180180

181+
kuid_t rust_helper_task_uid(struct task_struct *task)
182+
{
183+
return task_uid(task);
184+
}
185+
EXPORT_SYMBOL_GPL(rust_helper_task_uid);
186+
187+
kuid_t rust_helper_task_euid(struct task_struct *task)
188+
{
189+
return task_euid(task);
190+
}
191+
EXPORT_SYMBOL_GPL(rust_helper_task_euid);
192+
193+
#ifndef CONFIG_USER_NS
194+
uid_t rust_helper_from_kuid(struct user_namespace *to, kuid_t uid)
195+
{
196+
return from_kuid(to, uid);
197+
}
198+
EXPORT_SYMBOL_GPL(rust_helper_from_kuid);
199+
#endif /* CONFIG_USER_NS */
200+
201+
bool rust_helper_uid_eq(kuid_t left, kuid_t right)
202+
{
203+
return uid_eq(left, right);
204+
}
205+
EXPORT_SYMBOL_GPL(rust_helper_uid_eq);
206+
207+
kuid_t rust_helper_current_euid(void)
208+
{
209+
return current_euid();
210+
}
211+
EXPORT_SYMBOL_GPL(rust_helper_current_euid);
212+
213+
struct user_namespace *rust_helper_current_user_ns(void)
214+
{
215+
return current_user_ns();
216+
}
217+
EXPORT_SYMBOL_GPL(rust_helper_current_user_ns);
218+
219+
pid_t rust_helper_task_tgid_nr_ns(struct task_struct *tsk,
220+
struct pid_namespace *ns)
221+
{
222+
return task_tgid_nr_ns(tsk, ns);
223+
}
224+
EXPORT_SYMBOL_GPL(rust_helper_task_tgid_nr_ns);
225+
181226
struct kunit *rust_helper_kunit_get_current_test(void)
182227
{
183228
return kunit_get_current_test();

rust/kernel/cred.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1111
use crate::{
1212
bindings,
13+
task::Kuid,
1314
types::{AlwaysRefCounted, Opaque},
1415
};
1516

@@ -61,11 +62,11 @@ impl Credential {
6162
}
6263

6364
/// Returns the effective UID of the given credential.
64-
pub fn euid(&self) -> bindings::kuid_t {
65+
pub fn euid(&self) -> Kuid {
6566
// SAFETY: By the type invariant, we know that `self.0` is valid. Furthermore, the `euid`
6667
// field of a credential is never changed after initialization, so there is no potential
6768
// for data races.
68-
unsafe { (*self.0.get()).euid }
69+
Kuid::from_raw(unsafe { (*self.0.get()).euid })
6970
}
7071
}
7172

rust/kernel/task.rs

+66
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
types::{NotThreadSafe, Opaque},
1010
};
1111
use core::{
12+
cmp::{Eq, PartialEq},
1213
ffi::{c_int, c_long, c_uint},
1314
ops::Deref,
1415
ptr,
@@ -96,6 +97,12 @@ unsafe impl Sync for Task {}
9697
/// The type of process identifiers (PIDs).
9798
type Pid = bindings::pid_t;
9899

100+
/// The type of user identifiers (UIDs).
101+
#[derive(Copy, Clone)]
102+
pub struct Kuid {
103+
kuid: bindings::kuid_t,
104+
}
105+
99106
impl Task {
100107
/// Returns a raw pointer to the current task.
101108
///
@@ -157,12 +164,31 @@ impl Task {
157164
unsafe { *ptr::addr_of!((*self.0.get()).pid) }
158165
}
159166

167+
/// Returns the UID of the given task.
168+
pub fn uid(&self) -> Kuid {
169+
// SAFETY: By the type invariant, we know that `self.0` is valid.
170+
Kuid::from_raw(unsafe { bindings::task_uid(self.0.get()) })
171+
}
172+
173+
/// Returns the effective UID of the given task.
174+
pub fn euid(&self) -> Kuid {
175+
// SAFETY: By the type invariant, we know that `self.0` is valid.
176+
Kuid::from_raw(unsafe { bindings::task_euid(self.0.get()) })
177+
}
178+
160179
/// Determines whether the given task has pending signals.
161180
pub fn signal_pending(&self) -> bool {
162181
// SAFETY: By the type invariant, we know that `self.0` is valid.
163182
unsafe { bindings::signal_pending(self.0.get()) != 0 }
164183
}
165184

185+
/// Returns the given task's pid in the current pid namespace.
186+
pub fn pid_in_current_ns(&self) -> Pid {
187+
// SAFETY: We know that `self.0.get()` is valid by the type invariant, and passing a null
188+
// pointer as the namespace is correct for using the current namespace.
189+
unsafe { bindings::task_tgid_nr_ns(self.0.get(), ptr::null_mut()) }
190+
}
191+
166192
/// Wakes up the task.
167193
pub fn wake_up(&self) {
168194
// SAFETY: By the type invariant, we know that `self.0.get()` is non-null and valid.
@@ -184,3 +210,43 @@ unsafe impl crate::types::AlwaysRefCounted for Task {
184210
unsafe { bindings::put_task_struct(obj.cast().as_ptr()) }
185211
}
186212
}
213+
214+
impl Kuid {
215+
/// Get the current euid.
216+
#[inline]
217+
pub fn current_euid() -> Kuid {
218+
// SAFETY: Just an FFI call.
219+
Self::from_raw(unsafe { bindings::current_euid() })
220+
}
221+
222+
/// Create a `Kuid` given the raw C type.
223+
#[inline]
224+
pub fn from_raw(kuid: bindings::kuid_t) -> Self {
225+
Self { kuid }
226+
}
227+
228+
/// Turn this kuid into the raw C type.
229+
#[inline]
230+
pub fn into_raw(self) -> bindings::kuid_t {
231+
self.kuid
232+
}
233+
234+
/// Converts this kernel UID into a userspace UID.
235+
///
236+
/// Uses the namespace of the current task.
237+
#[inline]
238+
pub fn into_uid_in_current_ns(self) -> bindings::uid_t {
239+
// SAFETY: Just an FFI call.
240+
unsafe { bindings::from_kuid(bindings::current_user_ns(), self.kuid) }
241+
}
242+
}
243+
244+
impl PartialEq for Kuid {
245+
#[inline]
246+
fn eq(&self, other: &Kuid) -> bool {
247+
// SAFETY: Just an FFI call.
248+
unsafe { bindings::uid_eq(self.kuid, other.kuid) }
249+
}
250+
}
251+
252+
impl Eq for Kuid {}

0 commit comments

Comments
 (0)