Re: [PATCH v10 4/8] rust: cred: add Rust abstraction for `struct cred`

From: Alice Ryhl
Date: Sun Sep 15 2024 - 16:55:46 EST


On Sun, Sep 15, 2024 at 10:24 PM Kees Cook <kees@xxxxxxxxxx> wrote:
>
> On Sun, Sep 15, 2024 at 02:31:30PM +0000, Alice Ryhl wrote:
> > From: Wedson Almeida Filho <wedsonaf@xxxxxxxxx>
> >
> > Add a wrapper around `struct cred` called `Credential`, and provide
> > functionality to get the `Credential` associated with a `File`.
> >
> > Rust Binder must check the credentials of processes when they attempt to
> > perform various operations, and these checks usually take a
> > `&Credential` as parameter. The security_binder_set_context_mgr function
> > would be one example. This patch is necessary to access these security_*
> > methods from Rust.
> >
> > This Rust abstraction makes the following assumptions about the C side:
> > * `struct cred` is refcounted with `get_cred`/`put_cred`.
>
> Yes
>
> > * It's okay to transfer a `struct cred` across threads, that is, you do
> > not need to call `put_cred` on the same thread as where you called
> > `get_cred`.
>
> Yes
>
> > * The `euid` field of a `struct cred` never changes after
> > initialization.
>
> "after initialization", yes. The bprm cred during exec is special in
> that it gets updated (bprm_fill_uid) before it is installed into current
> via commit_creds() in begin_new_exec() (the point of no return for
> exec).

I think it will be pretty normal to need different Rust types for pre-
and post-initialization of a C value. When a value is not yet fully
initialized, you usually get some extra powers (modify otherwise
immutable fields), but probably also lose some powers (you can't share
it with other threads yet). I can document that this type should not
be used with the bprm cred during exec.

> > * The `f_cred` field of a `struct file` never changes after
> > initialization.
>
> Yes.
>
> >
> > Signed-off-by: Wedson Almeida Filho <wedsonaf@xxxxxxxxx>
> > Co-developed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> > Reviewed-by: Trevor Gross <tmgross@xxxxxxxxx>
> > Reviewed-by: Benno Lossin <benno.lossin@xxxxxxxxx>
> > Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@xxxxxxxxx>
> > Reviewed-by: Gary Guo <gary@xxxxxxxxxxx>
> > Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
>
> Reviewed-by: Kees Cook <kees@xxxxxxxxxx>

Thanks for the review!

Alice