Skip to content

Commit 621ea49

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] Signed-off-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent aa7531b commit 621ea49

File tree

4 files changed

+118
-2
lines changed

4 files changed

+118
-2
lines changed

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/ethtool.h>
1313
#include <linux/file.h>
1414
#include <linux/fs.h>
15+
#include <linux/pid_namespace.h>
1516
#include <linux/jiffies.h>
1617
#include <linux/mdio.h>
1718
#include <linux/phy.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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010
};
1111

1212
use core::{
13+
cmp::{Eq, PartialEq},
1314
ffi::{c_int, c_long, c_uint},
1415
ops::Deref,
1516
ptr,
@@ -97,6 +98,12 @@ unsafe impl Sync for Task {}
9798
/// The type of process identifiers (PIDs).
9899
type Pid = bindings::pid_t;
99100

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

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

186+
/// Returns the given task's pid in the current pid namespace.
187+
pub fn pid_in_current_ns(&self) -> Pid {
188+
let current = Task::current_raw();
189+
// SAFETY: Calling `task_active_pid_ns` with the current task is always safe.
190+
let namespace = unsafe { bindings::task_active_pid_ns(current) };
191+
// SAFETY: We know that `self.0.get()` is valid by the type invariant, and the namespace
192+
// pointer is not dangling since it points at this task's namespace.
193+
unsafe { bindings::task_tgid_nr_ns(self.0.get(), namespace) }
194+
}
195+
167196
/// Wakes up the task.
168197
pub fn wake_up(&self) {
169198
// SAFETY: By the type invariant, we know that `self.0.get()` is non-null and valid.
@@ -173,6 +202,46 @@ impl Task {
173202
}
174203
}
175204

205+
impl Kuid {
206+
/// Get the current euid.
207+
#[inline]
208+
pub fn current_euid() -> Kuid {
209+
// SAFETY: Just an FFI call.
210+
Self::from_raw(unsafe { bindings::current_euid() })
211+
}
212+
213+
/// Create a `Kuid` given the raw C type.
214+
#[inline]
215+
pub fn from_raw(kuid: bindings::kuid_t) -> Self {
216+
Self { kuid }
217+
}
218+
219+
/// Turn this kuid into the raw C type.
220+
#[inline]
221+
pub fn into_raw(self) -> bindings::kuid_t {
222+
self.kuid
223+
}
224+
225+
/// Converts this kernel UID into a userspace UID.
226+
///
227+
/// Uses the namespace of the current task.
228+
#[inline]
229+
pub fn into_uid_in_current_ns(self) -> bindings::uid_t {
230+
// SAFETY: Just an FFI call.
231+
unsafe { bindings::from_kuid(bindings::current_user_ns(), self.kuid) }
232+
}
233+
}
234+
235+
impl PartialEq for Kuid {
236+
#[inline]
237+
fn eq(&self, other: &Kuid) -> bool {
238+
// SAFETY: Just an FFI call.
239+
unsafe { bindings::uid_eq(self.kuid, other.kuid) }
240+
}
241+
}
242+
243+
impl Eq for Kuid {}
244+
176245
// SAFETY: The type invariants guarantee that `Task` is always ref-counted.
177246
unsafe impl crate::types::AlwaysRefCounted for Task {
178247
fn inc_ref(&self) {

0 commit comments

Comments
 (0)