From patchwork Fri Feb 9 11:18:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 198822 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp780772dyd; Fri, 9 Feb 2024 03:22:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IGoclYphKjr9JJzxGIi276lcIfn+ufqnTHsd8Jz6M2iYFFBq5y4wTGcj2Mss4UPspC0NQh/ X-Received: by 2002:a17:906:6806:b0:a38:9ff:571d with SMTP id k6-20020a170906680600b00a3809ff571dmr1055559ejr.34.1707477764098; Fri, 09 Feb 2024 03:22:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707477764; cv=pass; d=google.com; s=arc-20160816; b=khbykcdwYFQ6mNZOe9QpigTzU7QrZYbgN0CaymXL9/sbkxlYrpcx+gjC1ozrQ6YNq2 f6d9idtXeZqCSPyZcRpdike9t+PjcJAOOm2yPe6TOVhm+UpXY2pB/8tOo/Oeo4QWddMf oU0m50iVSc5ZIuqyFywx3eswh5BdqOsgaoyEu5fk1xDhSNpjyEGqBzB6nw14+DKnQ9gT O2UaJRBGOhJlIJ+DMWcJPKFeaxaebdm2bKs+IRaMvD9JR/JCjijMHslgcz9eiBwqnp4+ 66pq7FCP4s9zyeba+RjUQiEcMIxG+tZ2IRNWiIIdJnJzVZQ+N55oz+tMk9fK1lVYkGe3 uI0Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=6FCYANgF4lGfBb0oow1nW4bsB/Fdu+fIcf3p3BOVfZo=; fh=Uz6qLJiDSoTYlriPEgxhi2tOpmevOJKWZHXE1Jond8U=; b=MwAUknC/Lwt0Cifb21bJibw5jNGt8zYVG0OG7Bn2lmAbza6bdLZkZ57flz2HFxRFdc IMwSaZb/k7q4zpYOqRXcdZYTVC3c8XSe66BDCVfL0xLfLOIwwman5Hci1uZ8w0syyHWj F/O0CZ5KegCKFuH1iZAurMVM9xnQgtd507x+j08UD/b+InCe7GMtS7nDqIzG/Lvd30X4 vp4o1ny2bpOFWXKkIrHb+fKDBP30Bwu0OxhzAQ10j3B1O5thHHVmg1Fb0+9fs67zaTzR xDbeM9sER0KTY1iz5n8UFW7K7xj7zeUQjPrVhiXEXW08VXB4hx62aqLFAwENyFsWM+bq G6dw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="bSLV/VQ2"; arc=pass (i=1 spf=pass spfdomain=flex--aliceryhl.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-59282-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59282-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCWNrDoRBJm8REmXHRR12nHMHJ+fIrZ7VNkd+sms5tt+EuMvvN3R1hrAjhqNCTfKeN3rA6zvWeYIiEpb5XXDRS5mg3puRg== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id jj16-20020a170907985000b00a385414b18asi744003ejc.83.2024.02.09.03.22.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 03:22:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59282-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="bSLV/VQ2"; arc=pass (i=1 spf=pass spfdomain=flex--aliceryhl.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-59282-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59282-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id A978F1F256FB for ; Fri, 9 Feb 2024 11:21:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4BAF738382; Fri, 9 Feb 2024 11:19:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bSLV/VQ2" Received: from mail-lj1-f202.google.com (mail-lj1-f202.google.com [209.85.208.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 73518374D9 for ; Fri, 9 Feb 2024 11:19:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707477544; cv=none; b=XpIU+bsWCvAEAsw/o65FNBwDMW8lk5TIBUgSt3q35FbyT8PiTbdV+huuEshAbFkeBnM5DoGrKcDlQRWVvNh+AkZ1LscIuwYtU9n4DGGV1rhECPFyQEeBeD0/VjXNkKcJYlVPnXEwoQZafAgUxrCdmFgz5kP87oN0PFsCed0apS0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707477544; c=relaxed/simple; bh=6s2gn0oE8oc/3LtJyrV+uh/JSqxQjhps1bnBqWxXGbI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Cxes9w6aqylvQ0Fx/FQe8eT+mo0CPa0tD+YyX7nvHnklIsCeA9vJYAg+eaD6CckDp5U0nhvBEYl7aseBurBCQxwaCMb9Ywg1GmmxcdrmWgol7kIM2xbS3igHQ2rUVwCAwZVGaTIZO9SqdTDLLrwulLW76uXjKBsd35+6n+6TESs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bSLV/VQ2; arc=none smtp.client-ip=209.85.208.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Received: by mail-lj1-f202.google.com with SMTP id 38308e7fff4ca-2d0a329e86cso8129211fa.3 for ; Fri, 09 Feb 2024 03:19:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707477540; x=1708082340; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=6FCYANgF4lGfBb0oow1nW4bsB/Fdu+fIcf3p3BOVfZo=; b=bSLV/VQ2HR6K2ekMF7QjV/PvAWgoLu8FwVtsInnaYPKSwpjw3UR8iHksj8RHklnlTq iteB3pZtdO24Dw4b3F4Gm27DAgmmGcbY9Kap3eYqiGBWmlkWxH+myH4P87sa/mqeZIQM RScRbzNLwvYNC0TGeliS3lXfJes6C0SOx/S5444/mbv4V0A1DW3Jlp6dMjFgl0MsZcdC /9X8h8vXO3phvhJD+bRUNowTpK5bAGCdmgKL6cdEjrJGzK4yBeUBKLRdEyxX29skUKE7 p7rN9JCdk3TkmJ4L6l5Q051eDaF77ht8aLdcP9NtnuH1BSQMFFFOOVnu+enSX7nEwtmU 42kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707477540; x=1708082340; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6FCYANgF4lGfBb0oow1nW4bsB/Fdu+fIcf3p3BOVfZo=; b=G5IXPE+ccdbGvYayt1qWB1cT9t/fHfbBIgcx1MyM4dMJ0rwr85j3hvP+8rcbS2Uo+x HR4dd+oMl64Gd4+MPGiJ776SwyH1/bTPkH0RJa/yEvnU1sWjMqi0wvNkxNt1aMyhJ1o2 BJiQ9X1uRnwBJKhJJoiJvnOLJfLgxRtYJg/JeX64h1oWlLPcyK44QzDpCMmVEWjn2bwQ lQrhwu/36/mEKCw2dEGKsHg6EYaHRpnrnj0naZ65x63hqvPP7lFlHdwkSzRgWS2HHGQ5 crkiQY5G07wCrHti9svS7QUHeTf2WIM70XU8+lwhNo5GfcJUp5VsP1fNIvJKjcYNC9Xq camQ== X-Gm-Message-State: AOJu0YxIzWTkPKHD/ffGaTLM/yu9nRlt9ptWXbXNxrhcoUUwugRld6+F anj5WaqJGw6KAoOtn56YH8omE0uKCRRVx60hzd06Vtltg+ljDWckgnmFb9+o9DzlH2CywxfUGWz OOfJsd5qY9mnm5g== X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:651c:b20:b0:2d0:dd94:4ca9 with SMTP id b32-20020a05651c0b2000b002d0dd944ca9mr1166ljr.0.1707477540357; Fri, 09 Feb 2024 03:19:00 -0800 (PST) Date: Fri, 09 Feb 2024 11:18:17 +0000 In-Reply-To: <20240209-alice-file-v5-0-a37886783025@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240209-alice-file-v5-0-a37886783025@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=7117; i=aliceryhl@google.com; h=from:subject:message-id; bh=E3RpArjiH4nioml1kPKUKUnKZduDlkajGIRmbXxIUMA=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBlxgoR52G40En1ErRk4ewFgUO8fX4mp3h9lJumG l6RPs7WbD6JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZcYKEQAKCRAEWL7uWMY5 Rp5kEACcB92TIqUG+z0wBikO2hBIPI7gjDYF3SHW0x5qjB5WvxXic9+h71rZXkkrtlC/zD6x/Lu djaZpWwQUHHw1uJLA+7wmn0+GXGBjC97rhwZXktQIH5a+6rypSwJQ5e4QkFuvU623gZd7/mM7EQ N7uE56h/3UxdTFaEG1T8n6syqdmDWE1RqJkVU3CqU2CbrhjlZD1DVBomBv30NN2/FkO2/dMPCev C1O3Eeb7XXtD679sNvHggESeASeXsy+fSuaCtoL7dAoIBNsnS1htn8NvZ12obEzaxIWlav9Csll 6RZKO+x4psyQNuWqdaJnkPbfrWwmj1A3nPDN+myeVrN68orgcbIuYUbrACh37J4qel3WrWKHrla 9PPFaVhFPppwU+DnCvrZ7+nW/D+RqdVklU/DvK6+QE/dk42M5Kd0NhvgTBYfTUtITZG2yiK7F/n TiriCyvgW9hf73sZO8/kmkS1qa0Qg+HAjpqBa+ErKZiDigrABYAOso2bUPv3vhzVGMl1RyLH2xG aRSFY0rXlw1u3+s2mukvotlUWn727P1tSZMBHgIzAyiXZx7ImEXYeCgv2fUy1CQ68/+UQET3v1H 6xBPL+rrZLa9rPSu564U2gSmpBWhReIrdmCxnqSCpeK0UV1fhA3wpZsuFU558GuwbzrZ0t85Xxw B4dqH8Rwrzg329w== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240209-alice-file-v5-4-a37886783025@google.com> Subject: [PATCH v5 4/9] rust: cred: add Rust abstraction for `struct cred` From: Alice Ryhl To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Peter Zijlstra , Alexander Viro , Christian Brauner , Greg Kroah-Hartman , " =?utf-8?q?Arve_Hj=C3=B8n?= =?utf-8?q?nev=C3=A5g?= " , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan Cc: Dan Williams , Kees Cook , Matthew Wilcox , Thomas Gleixner , Daniel Xu , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl , Trevor Gross , Martin Rodriguez Reboredo X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790420203940080525 X-GMAIL-MSGID: 1790420203940080525 From: Wedson Almeida Filho 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. Signed-off-by: Wedson Almeida Filho Co-developed-by: Alice Ryhl Reviewed-by: Trevor Gross Reviewed-by: Benno Lossin Reviewed-by: Martin Rodriguez Reboredo Signed-off-by: Alice Ryhl --- rust/bindings/bindings_helper.h | 1 + rust/helpers.c | 13 ++++++++ rust/kernel/cred.rs | 74 +++++++++++++++++++++++++++++++++++++++++ rust/kernel/file.rs | 13 ++++++++ rust/kernel/lib.rs | 1 + 5 files changed, 102 insertions(+) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index 41fcd2905ed4..84a56e8b6b67 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -7,6 +7,7 @@ */ #include +#include #include #include #include diff --git a/rust/helpers.c b/rust/helpers.c index 03141a3608a4..10ed69f76424 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -164,6 +165,18 @@ struct file *rust_helper_get_file(struct file *f) } EXPORT_SYMBOL_GPL(rust_helper_get_file); +const struct cred *rust_helper_get_cred(const struct cred *cred) +{ + return get_cred(cred); +} +EXPORT_SYMBOL_GPL(rust_helper_get_cred); + +void rust_helper_put_cred(const struct cred *cred) +{ + put_cred(cred); +} +EXPORT_SYMBOL_GPL(rust_helper_put_cred); + /* * `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can * use it in contexts where Rust expects a `usize` like slice (array) indices. diff --git a/rust/kernel/cred.rs b/rust/kernel/cred.rs new file mode 100644 index 000000000000..360d6fdbe5e7 --- /dev/null +++ b/rust/kernel/cred.rs @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Credentials management. +//! +//! C header: [`include/linux/cred.h`](srctree/include/linux/cred.h). +//! +//! Reference: + +use crate::{ + bindings, + types::{AlwaysRefCounted, Opaque}, +}; + +/// Wraps the kernel's `struct cred`. +/// +/// Credentials are used for various security checks in the kernel. +/// +/// Most fields of credentials are immutable. When things have their credentials changed, that +/// happens by replacing the credential instead of changing an existing credential. See the [kernel +/// documentation][ref] for more info on this. +/// +/// # Invariants +/// +/// Instances of this type are always ref-counted, that is, a call to `get_cred` ensures that the +/// allocation remains valid at least until the matching call to `put_cred`. +/// +/// [ref]: https://www.kernel.org/doc/html/latest/security/credentials.html +#[repr(transparent)] +pub struct Credential(Opaque); + +// SAFETY: +// - `Credential::dec_ref` can be called from any thread. +// - It is okay to send ownership of `Credential` across thread boundaries. +unsafe impl Send for Credential {} + +// SAFETY: It's OK to access `Credential` through shared references from other threads because +// we're either accessing properties that don't change or that are properly synchronised by C code. +unsafe impl Sync for Credential {} + +impl Credential { + /// Creates a reference to a [`Credential`] from a valid pointer. + /// + /// # Safety + /// + /// The caller must ensure that `ptr` is valid and remains valid for the lifetime of the + /// returned [`Credential`] reference. + pub unsafe fn from_ptr<'a>(ptr: *const bindings::cred) -> &'a Credential { + // SAFETY: The safety requirements guarantee the validity of the dereference, while the + // `Credential` type being transparent makes the cast ok. + unsafe { &*ptr.cast() } + } + + /// Returns the effective UID of the given credential. + pub fn euid(&self) -> bindings::kuid_t { + // SAFETY: By the type invariant, we know that `self.0` is valid. Furthermore, the `euid` + // field of a credential is never changed after initialization, so there is no potential + // for data races. + unsafe { (*self.0.get()).euid } + } +} + +// SAFETY: The type invariants guarantee that `Credential` is always ref-counted. +unsafe impl AlwaysRefCounted for Credential { + fn inc_ref(&self) { + // SAFETY: The existence of a shared reference means that the refcount is nonzero. + unsafe { bindings::get_cred(self.0.get()) }; + } + + unsafe fn dec_ref(obj: core::ptr::NonNull) { + // SAFETY: The safety requirements guarantee that the refcount is nonzero. The cast is okay + // because `Credential` has the same representation as `struct cred`. + unsafe { bindings::put_cred(obj.cast().as_ptr()) }; + } +} diff --git a/rust/kernel/file.rs b/rust/kernel/file.rs index cf8ebf619379..3a64c5022941 100644 --- a/rust/kernel/file.rs +++ b/rust/kernel/file.rs @@ -7,6 +7,7 @@ use crate::{ bindings, + cred::Credential, error::{code::*, Error, Result}, types::{ARef, AlwaysRefCounted, Opaque}, }; @@ -207,6 +208,18 @@ pub fn as_ptr(&self) -> *mut bindings::file { self.0.get() } + /// Returns the credentials of the task that originally opened the file. + pub fn cred(&self) -> &Credential { + // SAFETY: It's okay to read the `f_cred` field without synchronization because `f_cred` is + // never changed after initialization of the file. + let ptr = unsafe { (*self.as_ptr()).f_cred }; + + // SAFETY: The signature of this function ensures that the caller will only access the + // returned credential while the file is still valid, and the C side ensures that the + // credential stays valid at least as long as the file. + unsafe { Credential::from_ptr(ptr) } + } + /// Returns the flags associated with the file. /// /// The flags are a combination of the constants in [`flags`]. diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 9353dd713a20..f65e5978f807 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -33,6 +33,7 @@ #[cfg(not(testlib))] mod allocator; mod build_assert; +pub mod cred; pub mod error; pub mod file; pub mod init;