From patchwork Thu Jul 25 14:27:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13742012 Received: from mail-lj1-f201.google.com (mail-lj1-f201.google.com [209.85.208.201]) (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 44D6519D087 for ; Thu, 25 Jul 2024 14:27:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721917669; cv=none; b=oo6Fy38rfVsx648ymiTusL7/7J3bjqVqWKyRCxHsZAUmQHzR64bkpVMktJZE683WOV/npPvagRKyJrkyr4bGx10oWf0E9pmhz8zkaSA4t62RtTltywuDpSCJ9OmLCB7ifY7ps+brNuSNmt6nOt9fi36MW+oKnApXNpNeqjXM+XU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721917669; c=relaxed/simple; bh=AebHlmQT/v8d6mOy63DVdfJexRKdHwwdHK1NUyWuM+Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sYX83ELvlSP2nENLcjnP8bnBQHh9tEkeRaqxU1FjMlwjX71c7h8J0h1HQprH7zcjL7CIme/n17J6qgVZKi0V4lqWwTHniQ+Di/5LH+ecN2/oEaTJPweqSEiN4qXE77RphlzPokfhaQhsvtttig5iNjX1s3QQ2dnzP++nQ+kBbM8= 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=iS3Eawpu; arc=none smtp.client-ip=209.85.208.201 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iS3Eawpu" Received: by mail-lj1-f201.google.com with SMTP id 38308e7fff4ca-2ef315c6990so1591951fa.1 for ; Thu, 25 Jul 2024 07:27:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721917665; x=1722522465; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=eQAwdRqXZq1hIaipuqKSk20IQQ5K8HeQcKqN8Vibh8A=; b=iS3Eawpuup7pEVmHO6bAYAA76QFREFowWjvD8f6PhkRAgeAirfPLbtelFIrDty01S6 DhGCT7yHLMt0LpB4OsVsYIhWc3utpyjKI3otLN5J1p//3jbEvEb1C5oB8+pVhMqCfd8P oe2lVbuVopxyAIMxph3TCza6w5wfUo/Dxzmi1fiVfrbJYVmeXxeMpx2VtfKpxKeZm1/w n3HqS7tVg6XR3/hj0ujc8FeBc7T51m4R2ubQCrna+lS/DEdMZby5bXfUMH0OIcGr6th+ J9Do8HHDU53Ni++o1t9BfmRC1C0mnuzdICL8tOrWvVudcbyyBJIfIkMw8kCrfsSuKrEJ HzyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721917665; x=1722522465; h=content-transfer-encoding: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=eQAwdRqXZq1hIaipuqKSk20IQQ5K8HeQcKqN8Vibh8A=; b=LTe/J2a2qDiGMs6vNcfMC1x4+WqB3moVVl8Oy9j+/tHk0JshcyoR4Qb9Hkum+EsJS4 N+nhYVfxWQq7HzSwJ29kqWmabvlCf9MLvH6c3owfzylPfluxmKOZRZIarQ7PwXLgMSZ5 zK91PySGCVkJGxkM/d1stVKNuAW/Ufnm3cu3wIFLQeDhy5oN8jYXlad1BnjYYDbygWGu SufxSjCT0fGMMpmcwlc1YUH3xXFcECuwmSrt9gdoZgr4mL7upv7idO29Rbo6fMvJ6SLt TxMxzERABGuS50b+dmSdIPX1wQMpLy8elfof+aP+zZsFJeFom3lSb+u3dB7o/a9H+6H+ wZGA== X-Forwarded-Encrypted: i=1; AJvYcCXqwnwTEJS+PuzmUo36kDEg1Pjt3jf5HlBXaHL309Ci0BfEZeSPOzSqoDjDBUVJ3v80ZZ6JY1jUgsWoD0SEfGOkVAe6KDmlRJi53E/Yiw== X-Gm-Message-State: AOJu0Yx57TQbAzoRIQxPZpjLV5eU/t5XnOI1MZNzCKNGt7bOCP4e3jq3 drC0z80fBw1aokxjUGxIcaKHpHSXPKi/ys8qi2B7KVUM3Gx9P/c0iAl79wgSluQF+O9Yz8GM/eP N6PyNsV9YWuZxHw== X-Google-Smtp-Source: AGHT+IHcegB6zdY+CFU4sJisI0TvHXhaoETjFS+27nPoBR1lwOb7c/de/PS4CNsB1A2WVLhGKZO+t4pp1FszUq8= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a2e:b535:0:b0:2ef:2c1d:1b32 with SMTP id 38308e7fff4ca-2f032eed05bmr78971fa.4.1721917665334; Thu, 25 Jul 2024 07:27:45 -0700 (PDT) Date: Thu, 25 Jul 2024 14:27:34 +0000 In-Reply-To: <20240725-alice-file-v8-0-55a2e80deaa8@google.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240725-alice-file-v8-0-55a2e80deaa8@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=5576; i=aliceryhl@google.com; h=from:subject:message-id; bh=h/k9dOkL3ny8wfocUav2zrVmFCywHPfnevq6opaBUgc=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmomDWw+7WfvAi24ysM5mXXEgGb7TOp01nzbdWk k3MmTzI9jiJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZqJg1gAKCRAEWL7uWMY5 RrOZD/0RgCQelMFscVSijYh4+gwKpLVFjRMC5YsH70Eu1xv5s4w9omgqbaAoTyEl35dDDdWTrim eTWANNP8nBaJCe8GNOKW1TJdHM6OfWbMYvWPQBBd3gxLi2xB2DYPIQlhhSJt3Kjotj+hWHYCLCz W7olIyS1X393PeIOrqntfjDuQVs+daJjCTq+pnMj42VdqjGg9y3fvS4+oOfpcSIoejTnME4zV6D 0uDErnJPBKMNZgIYQft7/GVf+Bg4TR73catNWWFBbOfvwcs7FukkLJ74oG9x7cl5NSVc5FdGxcO WsS+8Nn2haYJ044fSlRhJpv/Z/pFura23zVZ2b6gw2v2VX91LZmEkk1cP1Wt98r6qPBL/TGQ8FZ a6tE0mSu7MdYllqK082/RxgkGVxa6b+mFiF5iUxEYPL8KHvkv50MhoJ2N/sa2k1z/ZsUkHaDE84 MjNCMwi7Ek5IfjtGUF3RRWq1LCVs+KprUVouw2qiHJi0GHlo/oY1XLMEScuLbIZi55B0EVeaC4s Y5BKjbHwKUJhtjFI8pb63/l8pdO61yheSDZoDzL7fO+mJvozvYhmu6gXRDpVhhyc0NRAn8ZTvm7 NIUTgLdAe0as5pJp7q0sQiwvrVxUDc3dNlMw1XPAOULbwF3/eFWc7pqeR+1fVv3lbx3vgFvg2OR knwtmDsAwRabY4A== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240725-alice-file-v8-1-55a2e80deaa8@google.com> Subject: [PATCH v8 1/8] rust: types: add `NotThreadSafe` 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?= =?utf-8?q?=C3=B8nnev=C3=A5g?= " , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan Cc: Dan Williams , Matthew Wilcox , Thomas Gleixner , Daniel Xu , Martin Rodriguez Reboredo , Trevor Gross , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl , Kees Cook This introduces a new marker type for types that shouldn't be thread safe. By adding a field of this type to a struct, it becomes non-Send and non-Sync, which means that it cannot be accessed in any way from threads other than the one it was created on. This is useful for APIs that require globals such as `current` to remain constant while the value exists. We update two existing users in the Kernel to use this helper: * `Task::current()` - moving the return type of this value to a different thread would not be safe as you can no longer be guaranteed that the `current` pointer remains valid. * Lock guards. Mutexes and spinlocks should be unlocked on the same thread as where they were locked, so we enforce this using the Send trait. There are also additional users in later patches of this patchset. See [1] and [2] for the discussion that led to the introduction of this patch. Link: https://lore.kernel.org/all/nFDPJFnzE9Q5cqY7FwSMByRH2OAn_BpI4H53NQfWIlN6I2qfmAqnkp2wRqn0XjMO65OyZY4h6P4K2nAGKJpAOSzksYXaiAK_FoH_8QbgBI4=@proton.me/ [1] Link: https://lore.kernel.org/all/nFDPJFnzE9Q5cqY7FwSMByRH2OAn_BpI4H53NQfWIlN6I2qfmAqnkp2wRqn0XjMO65OyZY4h6P4K2nAGKJpAOSzksYXaiAK_FoH_8QbgBI4=@proton.me/ [2] Suggested-by: Benno Lossin Reviewed-by: Benno Lossin Reviewed-by: Trevor Gross Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Björn Roy Baron Signed-off-by: Alice Ryhl Reviewed-by: Gary Guo --- rust/kernel/sync/lock.rs | 13 +++++++++---- rust/kernel/task.rs | 10 ++++++---- rust/kernel/types.rs | 21 +++++++++++++++++++++ 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index f6c34ca4d819..d6e9bab114b8 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -6,8 +6,13 @@ //! spinlocks, raw spinlocks) to be provided with minimal effort. use super::LockClassKey; -use crate::{init::PinInit, pin_init, str::CStr, types::Opaque, types::ScopeGuard}; -use core::{cell::UnsafeCell, marker::PhantomData, marker::PhantomPinned}; +use crate::{ + init::PinInit, + pin_init, + str::CStr, + types::{NotThreadSafe, Opaque, ScopeGuard}, +}; +use core::{cell::UnsafeCell, marker::PhantomPinned}; use macros::pin_data; pub mod mutex; @@ -139,7 +144,7 @@ pub fn lock(&self) -> Guard<'_, T, B> { pub struct Guard<'a, T: ?Sized, B: Backend> { pub(crate) lock: &'a Lock, pub(crate) state: B::GuardState, - _not_send: PhantomData<*mut ()>, + _not_send: NotThreadSafe, } // SAFETY: `Guard` is sync when the data protected by the lock is also sync. @@ -191,7 +196,7 @@ pub(crate) unsafe fn new(lock: &'a Lock, state: B::GuardState) -> Self { Self { lock, state, - _not_send: PhantomData, + _not_send: NotThreadSafe, } } } diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs index 55dff7e088bf..278c623de0c6 100644 --- a/rust/kernel/task.rs +++ b/rust/kernel/task.rs @@ -4,10 +4,12 @@ //! //! C header: [`include/linux/sched.h`](srctree/include/linux/sched.h). -use crate::types::Opaque; +use crate::{ + bindings, + types::{NotThreadSafe, Opaque}, +}; use core::{ ffi::{c_int, c_long, c_uint}, - marker::PhantomData, ops::Deref, ptr, }; @@ -106,7 +108,7 @@ impl Task { pub unsafe fn current() -> impl Deref { struct TaskRef<'a> { task: &'a Task, - _not_send: PhantomData<*mut ()>, + _not_send: NotThreadSafe, } impl Deref for TaskRef<'_> { @@ -125,7 +127,7 @@ fn deref(&self) -> &Self::Target { // that `TaskRef` is not `Send`, we know it cannot be transferred to another thread // (where it could potentially outlive the caller). task: unsafe { &*ptr.cast() }, - _not_send: PhantomData, + _not_send: NotThreadSafe, } } diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index bd189d646adb..bb115d730ebb 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -473,3 +473,24 @@ unsafe impl AsBytes for str {} // does not have any uninitialized portions either. unsafe impl AsBytes for [T] {} unsafe impl AsBytes for [T; N] {} + +/// Zero-sized type to mark types not [`Send`]. +/// +/// Add this type as a field to your struct if your type should not be sent to a different task. +/// Since [`Send`] is an auto trait, adding a single field that is `!Send` will ensure that the +/// whole type is `!Send`. +/// +/// If a type is `!Send` it is impossible to give control over an instance of the type to another +/// task. This is useful to include in types that store or reference task-local information. A file +/// descriptor is an example of such task-local information. +/// +/// This type also makes the type `!Sync`, which prevents immutable access to the value from +/// several threads in parallel. +pub type NotThreadSafe = PhantomData<*mut ()>; + +/// Used to construct instances of type [`NotThreadSafe`] similar to how `PhantomData` is +/// constructed. +/// +/// [`NotThreadSafe`]: type@NotThreadSafe +#[allow(non_upper_case_globals)] +pub const NotThreadSafe: NotThreadSafe = PhantomData; From patchwork Thu Jul 25 14:27:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13742013 Received: from mail-lj1-f201.google.com (mail-lj1-f201.google.com [209.85.208.201]) (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 EF63F19D893 for ; Thu, 25 Jul 2024 14:27:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721917671; cv=none; b=aKrpvCMWe4+4yjvDHhWsgS5Et8q+qlRCxcvEddRGkLM3rDP/qt+PCqM3dK91yAzvH0rI2lBvRsWsIUhosdATzbUAW1pBGAUZc8eRI+DRrxtIewwaHnScKHJi/zjJFraRZ02bopLXw+YodwjsJ2CtIhucuZGf3ZBJscM8XC+0Tow= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721917671; c=relaxed/simple; bh=Y7EveGwaFZVeZYYvVcVUkcGhuSESwjRgqqnewAo3pxw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TC44a80YK93AOlPIl6yVOVv8cOqLb2NypC6yWoKMJFkQIMEaZpU6hnDc/UzTzXZpv8vTOI05pw6ilLEWmYrA8XbMludiJck5i9FOyi3Wyf/wW95bALYut6IX/6xCxjpahNf3A1lHL7cq9Zra9k/14sU/RVCxe/YiNa7ynGojCGE= 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=IIGrO0HS; arc=none smtp.client-ip=209.85.208.201 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="IIGrO0HS" Received: by mail-lj1-f201.google.com with SMTP id 38308e7fff4ca-2ef1ba2a5e6so2361871fa.1 for ; Thu, 25 Jul 2024 07:27:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721917668; x=1722522468; 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=rqu9hbcZjiCiJxMWwW1DRQvdcpwKODwZoAJXE9dNBuQ=; b=IIGrO0HSUwM4JAC1W6HTDEVta8UwvgIgaq88d/TfulzvvsxkQfw3uS6r3JcQfoC0Ql +0LdZO3H5fjctx2dZ1S470carvP6nQiUFf7mlS50fwJOxkgzmfe6pO3F4FZTCH0FfRo2 MeIiuA/NSRuc+AhdcV0HohR6k/iAWpXOp8qXwQzGqCTJ3I4clDWkLeg7kFim+v++p6MB 8d2sNOURRwOOvhyqa2foU/J8cGtTeleb3B/qqp6pyrtYBtzkiKwNa6dd/e/Qm5N8c4gl 66mzTpUn0uN+yav+VEfvKrtmPmrkRRboLdrOaXpJoU21MIifqyWIamjX3p18YPJZYKxY Jp/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721917668; x=1722522468; 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=rqu9hbcZjiCiJxMWwW1DRQvdcpwKODwZoAJXE9dNBuQ=; b=JgcpCn+DonsocnMAJnfAL/zZnh5ydyj6hJGscMyeMTGUfi9eVFVtccxQvg3JgiFXb6 CfYOuW7dV2oiqj1HOvpOb0e6uLT5F3bveQaNBtT8B4nCqwL8Ul3xnflJzcOyaZd55Qgb rZXCENfSiG6xxEfdq+AtAtkomtGIgjEdWCNdNn+sxDjZEGtcZYq4ASE4Qi767KxipfNt N2TvQ4BC+ijAwyABV6TFHgu7sCcazX1cUJUWgtu45LUb9rh9fTULCwetM56DMgLL1Y28 Tj7jiYGzb23sarL5PrrGOwpzp0WhQlRdoQJOMachsSyT4QRDI7WcTKschYkuVknL4MNJ WpZw== X-Forwarded-Encrypted: i=1; AJvYcCXj4K36nqWe3FeN0lgZgFH9X9cBzzP24lhBY7m2AU61BfVRBhPP+H7jEHQUT+Yls+waRx7Z1XcnxhlOEHKvwZJVbPNLaSzb7fumr1mGHQ== X-Gm-Message-State: AOJu0Yxg9K/TUJtgS1fzDMy9MZ25zFx+IMMYPJZtyNC9QJ/JezmHPpq7 1x9LWza9gpxJDaBiGsNmOMTreknaHRj0vbW2Ohcq6KX7Rcasi9pnbhVGwJDRMPweihFL/dK+2Sg vOZTZ2p/m6fBlCw== X-Google-Smtp-Source: AGHT+IFnXRADlnUEbxl0mtBvfwQuGp+zFQ+sJ+3vf/k0s7kKBVZQ+Ew6ChUoyD+TuMMrhdtMCgU3QmSJ6KPBxEQ= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a2e:97c4:0:b0:2ef:1bc3:3fa with SMTP id 38308e7fff4ca-2f039b3999emr48501fa.0.1721917668172; Thu, 25 Jul 2024 07:27:48 -0700 (PDT) Date: Thu, 25 Jul 2024 14:27:35 +0000 In-Reply-To: <20240725-alice-file-v8-0-55a2e80deaa8@google.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240725-alice-file-v8-0-55a2e80deaa8@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2383; i=aliceryhl@google.com; h=from:subject:message-id; bh=Y7EveGwaFZVeZYYvVcVUkcGhuSESwjRgqqnewAo3pxw=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmomDXe7TQovfSy+bcM+80aWs50pz7TKA1NVt2F dKLuFFnsZ2JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZqJg1wAKCRAEWL7uWMY5 RpJIEAC1c3zbht1aNR9FY2+6fNMS99tzB2GUHPJaEdtuUJ4ILd1Eg60NRfZuOdJcp58SdE75C4R DxcTruQkGWaQ6GUOHi9LcnOfO3YgxzHKdjnFeZ2XRdB4o6kP+V4oaT4h9GI0TmllzHOl0iAcP7y iCJe5J+1zEo2PUPeR5sq1dJhzJ1gisMMlqu/kdCZ0hZhpHI3OEF+RPnb2CfZrSGm6ywXyLtrRGT X9idZihV/78sRUhhwXb1OU5xaJgrd2ltriARLf66c4dI/jkqurNSSWMgYZhoAZq4o4BjXuQoVXK kqjrAK06BgJLj2sKp8GR4QqRoZha6rndpSyDkUIfOi1MqFgJLpMUW0Z/SBLyap9Ya56+zsemE2J LzWfzOAT76t8xTPDvN9pNG1jKHgsNGYBFSRyraWlUtVZD7YD7o8Zk6LSP3cF4bDdc7DIJOlKFWh U6IDSZ575oMK4ltRD7KwcKnioadJrujgwOJCobcqzSkRVs5cQ5KNOW4LG9OyBEkhdj6bj32CLHh Ry7preBUES11aSl79B8RNZnVgoD8PRXx0RkXS6lsY914s60hjcYDMDi6PdjCVYhOFGWg1BimRf/ O48jMkZXjXyM2HA8rSAFAwwAhFfyzQSIOKc50Q/E0Go2O+gM2Nd4pzF2WvZxI+Mmq+i3d8zcOLg F/mNAgZB1cBXWOA== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240725-alice-file-v8-2-55a2e80deaa8@google.com> Subject: [PATCH v8 2/8] rust: task: add `Task::current_raw` 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?= =?utf-8?q?=C3=B8nnev=C3=A5g?= " , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan Cc: Dan Williams , Matthew Wilcox , Thomas Gleixner , Daniel Xu , Martin Rodriguez Reboredo , Trevor Gross , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl , Kees Cook Introduces a safe function for getting a raw pointer to the current task. When writing bindings that need to access the current task, it is often more convenient to call a method that directly returns a raw pointer than to use the existing `Task::current` method. However, the only way to do that is `bindings::get_current()` which is unsafe since it calls into C. By introducing `Task::current_raw()`, it becomes possible to obtain a pointer to the current task without using unsafe. Link: https://lore.kernel.org/all/CAH5fLgjT48X-zYtidv31mox3C4_Ogoo_2cBOCmX0Ang3tAgGHA@mail.gmail.com/ Reviewed-by: Benno Lossin Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Trevor Gross Signed-off-by: Alice Ryhl Reviewed-by: Gary Guo --- rust/kernel/task.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs index 278c623de0c6..367b4bbddd9f 100644 --- a/rust/kernel/task.rs +++ b/rust/kernel/task.rs @@ -97,6 +97,15 @@ unsafe impl Sync for Task {} type Pid = bindings::pid_t; impl Task { + /// Returns a raw pointer to the current task. + /// + /// It is up to the user to use the pointer correctly. + #[inline] + pub fn current_raw() -> *mut bindings::task_struct { + // SAFETY: Getting the current pointer is always safe. + unsafe { bindings::get_current() } + } + /// Returns a task reference for the currently executing task/thread. /// /// The recommended way to get the current task/thread is to use the @@ -119,14 +128,12 @@ fn deref(&self) -> &Self::Target { } } - // SAFETY: Just an FFI call with no additional safety requirements. - let ptr = unsafe { bindings::get_current() }; - + let current = Task::current_raw(); TaskRef { // SAFETY: If the current thread is still running, the current task is valid. Given // that `TaskRef` is not `Send`, we know it cannot be transferred to another thread // (where it could potentially outlive the caller). - task: unsafe { &*ptr.cast() }, + task: unsafe { &*current.cast() }, _not_send: NotThreadSafe, } } From patchwork Thu Jul 25 14:27:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13742014 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 DB61419DF75 for ; Thu, 25 Jul 2024 14:27:52 +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=1721917676; cv=none; b=YAcnsUpyOxYx2J8mO+WBLUyWJBFUZrWssRFgXWAX6RWYkhFj3+8xtbbeBeKDTLObS2jVy+9JrV0TxzKOf6cK1nqviSCTB8/m+tI5EeT9HxQ148uRZSDbTaRFsPhAXlCy56RFAlBc++NwEaOTh1VT1S2T9aooZATz3ZQEp5YWAdU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721917676; c=relaxed/simple; bh=tZmP/exOJqVMU3pbXZPV0jYvSiyzlcGQQjEC7T9Sw+4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fkxp8b90x15NUUHGEhHaqG9iYuPpcQ+/fhtgOtFzEAFN0wiV6XWSWMu3QfBbuVMt/eUexH8uN2AqcUCXFjLSL4X1WUObytGr5RQTv8Qw6TewAgcBkBqCPHvzD8FMcaxOpky1FKE7C28bsUtitsFKkx9YK7cxl2a51N6rS+prNr0= 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=J24szGg9; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="J24szGg9" Received: by mail-lj1-f202.google.com with SMTP id 38308e7fff4ca-2ef315c6990so1592441fa.1 for ; Thu, 25 Jul 2024 07:27:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721917671; x=1722522471; 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=+PtKhdfCpcsJ3ZgZBrn1gqMPcNc/frbhz/ZePW+P05s=; b=J24szGg9YvYMqZzctw8BZOyO1QrGjghHD4QXdNuB5JTXsFJxnZ74TwZIR4mZ+GZPdh CfXGJfebLlpdgywDVTbJ0M8ph8EmakRKzV1Bf++CEYApdiYb0dI2AMdNvID+lErEdUIM RQUPydoIWbKhotLzZkWDyZQD0SCiAZ+5T42AbmCszPBhmqRhWXJJ5rlGeRb6q8yId9Hw 7yeu4HuGuQ9LXvNJSIIPG3VxL0YOuEEVitjUDtdJxgzvcVkCAOiy+RBsz8KNduPcGop5 pQpKBZq1rppKPnb51kZpyHy866UmawDCHo9IPpzWgLXcgW4y2fQLP3X3Pd/dSREZR3XK EGaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721917671; x=1722522471; 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=+PtKhdfCpcsJ3ZgZBrn1gqMPcNc/frbhz/ZePW+P05s=; b=LEI9qawniCKPDrYVPhxsevNtFQd1M8x578WDSQ8Eni5ITDHU3AjSdKJqr6EeLUAjaM X6shnxnVxyh9UvU1Nj9xL/ktaekIQ3DqSRD8vhTwynDpMD3cAUbHFQp0fLl2Q9qh/yxg hd68/442xjlJwh71VApLzFe3aangiiTIrxZFE9ffFJT6I6fgkDFdyegVHhyuwFDaDPie HwoePcUINT+O1MC/r2IlqKsQV8KhpKwGYKZ5qalQt09frM+gT3B/rF/+AOqrE6JsDyvI ztG1QWhx6nLOhcBAvHXkuO1tdmDMmybl9Cjc4AAKmsq+kvpw4rmM5izpEDbtuqB+8lnI Q+qg== X-Forwarded-Encrypted: i=1; AJvYcCUjBQlD73Pw8xKjLKPZ9TL97wyaQkSnNdgpRV7u/uKFrPvGMMMckINIPIDpuv/KSQItBEfD/CydDGTp6uLstudaWavcBGWKI32SLPEzmw== X-Gm-Message-State: AOJu0YyE+kkvcci1UlL5Fm9Sw71cygOHiYpIniQ4I02xqc9liGV5My3Q UDqkYKH3AYHtVM/CDbWjekA0pEuQGinQzzkCXRakM+MU2i5OW7lmYXvBIXEPA3c/nD/mc5kUHhT ZzxHxnCNfFkCBsg== X-Google-Smtp-Source: AGHT+IGhT+ddWmjsOetwaChqygUw9x0ZlLJk0+533sVWyFxzVDHmQgvQg5WpdkgxP6YMakF/7X6W0GNaGMtPW/Y= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a2e:3318:0:b0:2ef:1c03:73ec with SMTP id 38308e7fff4ca-2f03a3fa37fmr43881fa.0.1721917671216; Thu, 25 Jul 2024 07:27:51 -0700 (PDT) Date: Thu, 25 Jul 2024 14:27:36 +0000 In-Reply-To: <20240725-alice-file-v8-0-55a2e80deaa8@google.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240725-alice-file-v8-0-55a2e80deaa8@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=22113; i=aliceryhl@google.com; h=from:subject:message-id; bh=wtEVBL3hhHW8DgSuHsZi6E0p/49A2G8Ru/LhkgmYw0U=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmomDYvL/mRgqVHHGN2b4y1BxYsrJLirrVUSX9g /fUf9N+RwWJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZqJg2AAKCRAEWL7uWMY5 RnqZD/98SbRs6khkB2NUmHwn17mx5ONXCpBpXbpEiswDgpJkTPTJuYuFB0mY7nKu/Xh+tKY2VQk whpmPMOKXUOu0oG73UjnvrTpTjtxdWxcKY3iOY0Do99CTz/30yzFWxTWuh7Zl8e8+2MLoBPIQJy KP8UjdIq2791nVuF0lgoDbxSGunmjsiXxI+NUuNPiVFk0cyPFdBhYzbe6NgjWpyxrvp+mUwVBjv HG+oU51lWGLMRRAIyWUMwD+j0XWMPE2N3rD6nGLDZFtMVO/N/AvVp5tfG1EnZHAqS6BG75QVu4a 7OnYeCO08aFmxbvMzolyMHZfku+eG2slSCQSK9qCnAJ23gvzXGxxUp7DbNPXVg5lio001gjoyFe E1K22BbowaD4QhR9v82lQJLbfuYDXkOvgnnEVxD0KRLqHyRLdyKeIWQ39RMlz4+6L+3CYPDvPwt jmrongn6FQ7OJLzbT9mGMKf4y8JY+EHZdr5w/n7HbJhJzTCoYe1heyy3wPoUt+H/yfy/Ii8z5no WNFfpXMEE2nKmW+KJ7Ggv+sn3kZm5UgF/kgPF7jLhbV69Kd0oTNBEvmh943QN2QXo5i4BG787ow xA4KoFwoDgHYK2wBesd7sr63PwzPmx6+gr9cOcGs/z2JBIJIiZiklLwGi2QN8Q/VVSvXAF0jdJY nKL6XWJW1DHtT7g== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240725-alice-file-v8-3-55a2e80deaa8@google.com> Subject: [PATCH v8 3/8] rust: file: add Rust abstraction for `struct file` 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?= =?utf-8?q?=C3=B8nnev=C3=A5g?= " , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan Cc: Dan Williams , Matthew Wilcox , Thomas Gleixner , Daniel Xu , Martin Rodriguez Reboredo , Trevor Gross , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl , Kees Cook From: Wedson Almeida Filho This abstraction makes it possible to manipulate the open files for a process. The new `File` struct wraps the C `struct file`. When accessing it using the smart pointer `ARef`, the pointer will own a reference count to the file. When accessing it as `&File`, then the reference does not own a refcount, but the borrow checker will ensure that the reference count does not hit zero while the `&File` is live. Since this is intended to manipulate the open files of a process, we introduce an `fget` constructor that corresponds to the C `fget` method. In future patches, it will become possible to create a new fd in a process and bind it to a `File`. Rust Binder will use these to send fds from one process to another. We also provide a method for accessing the file's flags. Rust Binder will use this to access the flags of the Binder fd to check whether the non-blocking flag is set, which affects what the Binder ioctl does. This introduces a struct for the EBADF error type, rather than just using the Error type directly. This has two advantages: * `File::fget` returns a `Result, BadFdError>`, which the compiler will represent as a single pointer, with null being an error. This is possible because the compiler understands that `BadFdError` has only one possible value, and it also understands that the `ARef` smart pointer is guaranteed non-null. * Additionally, we promise to users of the method that the method can only fail with EBADF, which means that they can rely on this promise without having to inspect its implementation. That said, there are also two disadvantages: * Defining additional error types involves boilerplate. * The question mark operator will only utilize the `From` trait once, which prevents you from using the question mark operator on `BadFdError` in methods that return some third error type that the kernel `Error` is convertible into. (However, it works fine in methods that return `Error`.) Signed-off-by: Wedson Almeida Filho Co-developed-by: Daniel Xu Signed-off-by: Daniel Xu Co-developed-by: Alice Ryhl Signed-off-by: Alice Ryhl Reviewed-by: Benno Lossin --- fs/file.c | 7 + rust/bindings/bindings_helper.h | 2 + rust/helpers.c | 7 + rust/kernel/fs.rs | 8 + rust/kernel/fs/file.rs | 375 ++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + rust/kernel/types.rs | 8 + 7 files changed, 408 insertions(+) diff --git a/fs/file.c b/fs/file.c index a3b72aa64f11..1d910f96ec2e 100644 --- a/fs/file.c +++ b/fs/file.c @@ -1127,6 +1127,13 @@ EXPORT_SYMBOL(task_lookup_next_fdget_rcu); * * The fput_needed flag returned by fget_light should be passed to the * corresponding fput_light. + * + * (As an exception to rule 2, you can call filp_close between fget_light and + * fput_light provided that you capture a real refcount with get_file before + * the call to filp_close, and ensure that this real refcount is fput *after* + * the fput_light call.) + * + * See also the documentation in rust/kernel/file.rs. */ static unsigned long __fget_light(unsigned int fd, fmode_t mask) { diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index f2bafb10f181..e2d22f151ec9 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include #include #include diff --git a/rust/helpers.c b/rust/helpers.c index 305f0577fae9..5ba1f6de0251 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -199,6 +200,12 @@ rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags) } EXPORT_SYMBOL_GPL(rust_helper_krealloc); +struct file *rust_helper_get_file(struct file *f) +{ + return get_file(f); +} +EXPORT_SYMBOL_GPL(rust_helper_get_file); + /* * `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/fs.rs b/rust/kernel/fs.rs new file mode 100644 index 000000000000..0121b38c59e6 --- /dev/null +++ b/rust/kernel/fs.rs @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Kernel file systems. +//! +//! C headers: [`include/linux/fs.h`](srctree/include/linux/fs.h) + +pub mod file; +pub use self::file::{File, LocalFile}; diff --git a/rust/kernel/fs/file.rs b/rust/kernel/fs/file.rs new file mode 100644 index 000000000000..f1a52814b2da --- /dev/null +++ b/rust/kernel/fs/file.rs @@ -0,0 +1,375 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Files and file descriptors. +//! +//! C headers: [`include/linux/fs.h`](srctree/include/linux/fs.h) and +//! [`include/linux/file.h`](srctree/include/linux/file.h) + +use crate::{ + bindings, + error::{code::*, Error, Result}, + types::{ARef, AlwaysRefCounted, Opaque}, +}; +use core::ptr; + +/// Flags associated with a [`File`]. +pub mod flags { + /// File is opened in append mode. + pub const O_APPEND: u32 = bindings::O_APPEND; + + /// Signal-driven I/O is enabled. + pub const O_ASYNC: u32 = bindings::FASYNC; + + /// Close-on-exec flag is set. + pub const O_CLOEXEC: u32 = bindings::O_CLOEXEC; + + /// File was created if it didn't already exist. + pub const O_CREAT: u32 = bindings::O_CREAT; + + /// Direct I/O is enabled for this file. + pub const O_DIRECT: u32 = bindings::O_DIRECT; + + /// File must be a directory. + pub const O_DIRECTORY: u32 = bindings::O_DIRECTORY; + + /// Like [`O_SYNC`] except metadata is not synced. + pub const O_DSYNC: u32 = bindings::O_DSYNC; + + /// Ensure that this file is created with the `open(2)` call. + pub const O_EXCL: u32 = bindings::O_EXCL; + + /// Large file size enabled (`off64_t` over `off_t`). + pub const O_LARGEFILE: u32 = bindings::O_LARGEFILE; + + /// Do not update the file last access time. + pub const O_NOATIME: u32 = bindings::O_NOATIME; + + /// File should not be used as process's controlling terminal. + pub const O_NOCTTY: u32 = bindings::O_NOCTTY; + + /// If basename of path is a symbolic link, fail open. + pub const O_NOFOLLOW: u32 = bindings::O_NOFOLLOW; + + /// File is using nonblocking I/O. + pub const O_NONBLOCK: u32 = bindings::O_NONBLOCK; + + /// File is using nonblocking I/O. + /// + /// This is effectively the same flag as [`O_NONBLOCK`] on all architectures + /// except SPARC64. + pub const O_NDELAY: u32 = bindings::O_NDELAY; + + /// Used to obtain a path file descriptor. + pub const O_PATH: u32 = bindings::O_PATH; + + /// Write operations on this file will flush data and metadata. + pub const O_SYNC: u32 = bindings::O_SYNC; + + /// This file is an unnamed temporary regular file. + pub const O_TMPFILE: u32 = bindings::O_TMPFILE; + + /// File should be truncated to length 0. + pub const O_TRUNC: u32 = bindings::O_TRUNC; + + /// Bitmask for access mode flags. + /// + /// # Examples + /// + /// ``` + /// use kernel::fs::file; + /// # fn do_something() {} + /// # let flags = 0; + /// if (flags & file::flags::O_ACCMODE) == file::flags::O_RDONLY { + /// do_something(); + /// } + /// ``` + pub const O_ACCMODE: u32 = bindings::O_ACCMODE; + + /// File is read only. + pub const O_RDONLY: u32 = bindings::O_RDONLY; + + /// File is write only. + pub const O_WRONLY: u32 = bindings::O_WRONLY; + + /// File can be both read and written. + pub const O_RDWR: u32 = bindings::O_RDWR; +} + +/// Wraps the kernel's `struct file`. Thread safe. +/// +/// This represents an open file rather than a file on a filesystem. Processes generally reference +/// open files using file descriptors. However, file descriptors are not the same as files. A file +/// descriptor is just an integer that corresponds to a file, and a single file may be referenced +/// by multiple file descriptors. +/// +/// # Refcounting +/// +/// Instances of this type are reference-counted. The reference count is incremented by the +/// `fget`/`get_file` functions and decremented by `fput`. The Rust type `ARef` represents a +/// pointer that owns a reference count on the file. +/// +/// Whenever a process opens a file descriptor (fd), it stores a pointer to the file in its fd +/// table (`struct files_struct`). This pointer owns a reference count to the file, ensuring the +/// file isn't prematurely deleted while the file descriptor is open. In Rust terminology, the +/// pointers in `struct files_struct` are `ARef` pointers. +/// +/// ## Light refcounts +/// +/// Whenever a process has an fd to a file, it may use something called a "light refcount" as a +/// performance optimization. Light refcounts are acquired by calling `fdget` and released with +/// `fdput`. The idea behind light refcounts is that if the fd is not closed between the calls to +/// `fdget` and `fdput`, then the refcount cannot hit zero during that time, as the `struct +/// files_struct` holds a reference until the fd is closed. This means that it's safe to access the +/// file even if `fdget` does not increment the refcount. +/// +/// The requirement that the fd is not closed during a light refcount applies globally across all +/// threads - not just on the thread using the light refcount. For this reason, light refcounts are +/// only used when the `struct files_struct` is not shared with other threads, since this ensures +/// that other unrelated threads cannot suddenly start using the fd and close it. Therefore, +/// calling `fdget` on a shared `struct files_struct` creates a normal refcount instead of a light +/// refcount. +/// +/// Light reference counts must be released with `fdput` before the system call returns to +/// userspace. This means that if you wait until the current system call returns to userspace, then +/// all light refcounts that existed at the time have gone away. +/// +/// ### The file position +/// +/// Each `struct file` has a position integer, which is protected by the `f_pos_lock` mutex. +/// However, if the `struct file` is not shared, then the kernel may avoid taking the lock as a +/// performance optimization. +/// +/// The condition for avoiding the `f_pos_lock` mutex is different from the condition for using +/// `fdget`. With `fdget`, you may avoid incrementing the refcount as long as the current fd table +/// is not shared; it is okay if there are other fd tables that also reference the same `struct +/// file`. However, `fdget_pos` can only avoid taking the `f_pos_lock` if the entire `struct file` +/// is not shared, as different processes with an fd to the same `struct file` share the same +/// position. +/// +/// To represent files that are not thread safe due to this optimization, the [`LocalFile`] type is +/// used. +/// +/// ## Rust references +/// +/// The reference type `&File` is similar to light refcounts: +/// +/// * `&File` references don't own a reference count. They can only exist as long as the reference +/// count stays positive, and can only be created when there is some mechanism in place to ensure +/// this. +/// +/// * The Rust borrow-checker normally ensures this by enforcing that the `ARef` from which +/// a `&File` is created outlives the `&File`. +/// +/// * Using the unsafe [`File::from_raw_file`] means that it is up to the caller to ensure that the +/// `&File` only exists while the reference count is positive. +/// +/// * You can think of `fdget` as using an fd to look up an `ARef` in the `struct +/// files_struct` and create an `&File` from it. The "fd cannot be closed" rule is like the Rust +/// rule "the `ARef` must outlive the `&File`". +/// +/// # Invariants +/// +/// * All instances of this type are refcounted using the `f_count` field. +/// * There must not be any active calls to `fdget_pos` on this file that did not take the +/// `f_pos_lock` mutex. +#[repr(transparent)] +pub struct File { + inner: Opaque, +} + +/// Wraps the kernel's `struct file`. Not thread safe. +/// +/// This type represents a file that is not known to be safe to transfer across thread boundaries. +/// To obtain a thread-safe [`File`], use the [`assume_no_fdget_pos`] conversion. +/// +/// See the documentation for [`File`] for more information. +/// +/// # Invariants +/// +/// * All instances of this type are refcounted using the `f_count` field. +/// * If there is an active call to `fdget_pos` that did not take the `f_pos_lock` mutex, then it +/// must be on the same thread as this `File`. +/// +/// [`assume_no_fdget_pos`]: LocalFile::assume_no_fdget_pos +pub struct LocalFile { + inner: Opaque, +} + +// SAFETY: This file is known to not have any active `fdget_pos` calls that did not take the +// `f_pos_lock` mutex, so it is safe to transfer it between threads. +unsafe impl Send for File {} + +// SAFETY: This file is known to not have any active `fdget_pos` calls that did not take the +// `f_pos_lock` mutex, so it is safe to access its methods from several threads in parallel. +unsafe impl Sync for File {} + +impl LocalFile { + /// Constructs a new `struct file` wrapper from a file descriptor. + /// + /// The file descriptor belongs to the current process, and there might be active local calls + /// to `fdget_pos` on the same file. + /// + /// To obtain an `ARef`, use the [`assume_no_fdget_pos`] function to convert. + /// + /// [`assume_no_fdget_pos`]: LocalFile::assume_no_fdget_pos + #[inline] + pub fn fget(fd: u32) -> Result, BadFdError> { + // SAFETY: FFI call, there are no requirements on `fd`. + let ptr = ptr::NonNull::new(unsafe { bindings::fget(fd) }).ok_or(BadFdError)?; + + // SAFETY: `bindings::fget` created a refcount, and we pass ownership of it to the `ARef`. + // + // INVARIANT: This file is in the fd table on this thread, so either all `fdget_pos` calls + // are on this thread, or the file is shared, in which case `fdget_pos` calls took the + // `f_pos_lock` mutex. + Ok(unsafe { ARef::from_raw(ptr.cast()) }) + } + + /// Creates a reference to a [`LocalFile`] from a valid pointer. + /// + /// # Safety + /// + /// * The caller must ensure that `ptr` points at a valid file and that the file's refcount is + /// positive for the duration of 'a. + /// * The caller must ensure that if there is an active call to `fdget_pos` that did not take + /// the `f_pos_lock` mutex, then that call is on the current thread. + #[inline] + pub unsafe fn from_raw_file<'a>(ptr: *const bindings::file) -> &'a LocalFile { + // SAFETY: The caller guarantees that the pointer is not dangling and stays valid for the + // duration of 'a. The cast is okay because `File` is `repr(transparent)`. + // + // INVARIANT: The caller guarantees that there are no problematic `fdget_pos` calls. + unsafe { &*ptr.cast() } + } + + /// Assume that there are no active `fdget_pos` calls that prevent us from sharing this file. + /// + /// This makes it safe to transfer this file to other threads. No checks are performed, and + /// using it incorrectly may lead to a data race on the file position if the file is shared + /// with another thread. + /// + /// This method is intended to be used together with [`LocalFile::fget`] when the caller knows + /// statically that there are no `fdget_pos` calls on the current thread. For example, you + /// might use it when calling `fget` from an ioctl, since ioctls usually do not touch the file + /// position. + /// + /// # Safety + /// + /// There must not be any active `fdget_pos` calls on the current thread. + #[inline] + pub unsafe fn assume_no_fdget_pos(me: ARef) -> ARef { + // INVARIANT: There are no `fdget_pos` calls on the current thread, and by the type + // invariants, if there is a `fdget_pos` call on another thread, then it took the + // `f_pos_lock` mutex. + // + // SAFETY: `LocalFile` and `File` have the same layout. + unsafe { ARef::from_raw(ARef::into_raw(me).cast()) } + } + + /// Returns a raw pointer to the inner C struct. + #[inline] + pub fn as_ptr(&self) -> *mut bindings::file { + self.inner.get() + } + + /// Returns the flags associated with the file. + /// + /// The flags are a combination of the constants in [`flags`]. + #[inline] + pub fn flags(&self) -> u32 { + // This `read_volatile` is intended to correspond to a READ_ONCE call. + // + // SAFETY: The file is valid because the shared reference guarantees a nonzero refcount. + // + // FIXME(read_once): Replace with `read_once` when available on the Rust side. + unsafe { core::ptr::addr_of!((*self.as_ptr()).f_flags).read_volatile() } + } +} + +impl File { + /// Creates a reference to a [`File`] from a valid pointer. + /// + /// # Safety + /// + /// * The caller must ensure that `ptr` points at a valid file and that the file's refcount is + /// positive for the duration of 'a. + /// * The caller must ensure that if there are active `fdget_pos` calls on this file, then they + /// took the `f_pos_lock` mutex. + #[inline] + pub unsafe fn from_raw_file<'a>(ptr: *const bindings::file) -> &'a File { + // SAFETY: The caller guarantees that the pointer is not dangling and stays valid for the + // duration of 'a. The cast is okay because `File` is `repr(transparent)`. + // + // INVARIANT: The caller guarantees that there are no problematic `fdget_pos` calls. + unsafe { &*ptr.cast() } + } +} + +// Make LocalFile methods available on File. +impl core::ops::Deref for File { + type Target = LocalFile; + #[inline] + fn deref(&self) -> &LocalFile { + // SAFETY: The caller provides a `&File`, and since it is a reference, it must point at a + // valid file for the desired duration. + // + // By the type invariants, there are no `fdget_pos` calls that did not take the + // `f_pos_lock` mutex. + unsafe { LocalFile::from_raw_file(self as *const File as *const bindings::file) } + } +} + +// SAFETY: The type invariants guarantee that `LocalFile` is always ref-counted. This implementation +// makes `ARef` own a normal refcount. +unsafe impl AlwaysRefCounted for LocalFile { + #[inline] + fn inc_ref(&self) { + // SAFETY: The existence of a shared reference means that the refcount is nonzero. + unsafe { bindings::get_file(self.as_ptr()) }; + } + + #[inline] + unsafe fn dec_ref(obj: ptr::NonNull) { + // SAFETY: To call this method, the caller passes us ownership of a normal refcount, so we + // may drop it. The cast is okay since `File` has the same representation as `struct file`. + unsafe { bindings::fput(obj.cast().as_ptr()) } + } +} + +// SAFETY: The type invariants guarantee that `File` is always ref-counted. This implementation +// makes `ARef` own a normal refcount. +unsafe impl AlwaysRefCounted for File { + #[inline] + fn inc_ref(&self) { + // SAFETY: The existence of a shared reference means that the refcount is nonzero. + unsafe { bindings::get_file(self.as_ptr()) }; + } + + #[inline] + unsafe fn dec_ref(obj: ptr::NonNull) { + // SAFETY: To call this method, the caller passes us ownership of a normal refcount, so we + // may drop it. The cast is okay since `File` has the same representation as `struct file`. + unsafe { bindings::fput(obj.cast().as_ptr()) } + } +} + +/// Represents the `EBADF` error code. +/// +/// Used for methods that can only fail with `EBADF`. +#[derive(Copy, Clone, Eq, PartialEq)] +pub struct BadFdError; + +impl From for Error { + #[inline] + fn from(_: BadFdError) -> Error { + EBADF + } +} + +impl core::fmt::Debug for BadFdError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.pad("EBADF") + } +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 5d310e79485f..c364893e13a2 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -29,6 +29,7 @@ pub mod alloc; mod build_assert; pub mod error; +pub mod fs; pub mod init; pub mod ioctl; #[cfg(CONFIG_KUNIT)] diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index bb115d730ebb..cb3e178b2000 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -366,6 +366,14 @@ pub unsafe fn from_raw(ptr: NonNull) -> Self { _p: PhantomData, } } + + /// Convert this [`ARef`] into a raw pointer. + /// + /// The caller retains ownership of the refcount that this `ARef` used to own. + pub fn into_raw(me: Self) -> NonNull { + let me = core::mem::ManuallyDrop::new(me); + me.ptr + } } impl Clone for ARef { From patchwork Thu Jul 25 14:27:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13742015 Received: from mail-lj1-f201.google.com (mail-lj1-f201.google.com [209.85.208.201]) (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 D722319DF9C for ; Thu, 25 Jul 2024 14:27:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721917677; cv=none; b=oH8zH3XJsd6Riw6Yxt6hzmJt3jrtfsLhCGHFbKaHstUD0fdF7UpIZmCU7lHsRrDDowM85E6qUy2BQ+WXmujEVHdQHpcsc0wyb8Z7h3GRuHpTmstIbD+3iOYLbaGITNKDIj+diNIz9i5IrcQFN9J4pA0uokq1BXff2G9BGxVrb1Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721917677; c=relaxed/simple; bh=RXfaT/DBCQsWj/QAf6Xvjcb3hoSnauK9QOWBOYATpsw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NZTbb0NogAnNQeHlz2yECdx3qUpGh/0U+Ryjpc+f8dPR/DYDGtBdAPt9ASIl77rnHge4aFv/fDBOmlfuoHMXgEeLOHCO4DYZzpl8H0/SmictEr0ryXNXtvbttm9vs+KoxNvEGXTtARvkXOt2NrNUGGpmVxA29Z/5MvYxez/Bue8= 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=bNNGFK+C; arc=none smtp.client-ip=209.85.208.201 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bNNGFK+C" Received: by mail-lj1-f201.google.com with SMTP id 38308e7fff4ca-2ef286cf0e8so2521901fa.0 for ; Thu, 25 Jul 2024 07:27:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721917674; x=1722522474; 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=OiCmBwg2/y546/4rMjJfsysS+CelPZa1uCxDdubqWiE=; b=bNNGFK+ChHShPCggqe/e5P8vPmHpGOkPWAPxLRQT/CmkNDuvKKqtVK0QCAsuAK9nKq HwUz6BFhb/zVWB3yq+a3wJIqgDu2RSF2oPKTPK9aJaT8Fl0l92kR0zi62n7PrWAxH4OJ RjfCZECHXPPZuOqZOZMQ3QRzViK5nKGKZMPfbXqGNKElSzXKaXfiuHLzpivMaju3EouT rClpYGZ1Zb5j5Ke2q/RmI6BJBShy9p7xp9TOZgBqio3i+ldGt/JZViAdCanfenjo9bdb bRjxjLluGzjHGVn0246/gjceBkc7lBqbN464Y/IsZD2YBf3LzQtnfB2T5omdb8oAlvRe TpmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721917674; x=1722522474; 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=OiCmBwg2/y546/4rMjJfsysS+CelPZa1uCxDdubqWiE=; b=vbxBAB5QBXNSD5UJly3+mCriGgqW5YuA0vi4fEO4TBkIPUEqBglQ3AYdUJ/W7O/nqX Dt7r5Zup9Py/4rRCzTDn1TguOI3piO2SuCZqyTkA65hn3Fpb7tFQ2ftj5dero/SW9QZL mrK7e4oGEYL5ugr6AFkUDW9vl1nFeo9wk7zMJt/FxbzWOVgt0kCHQxcsoiqD0T7mFRZm wvFPiCUIe8q5wmeBR50qidbu83PWJXnh+Ez3yaA03W/DRNzKZ5VKWvDlq2Z9dHvpOwyp bYWpxcwKpOvCInmReqXqXEd/rS4Ria18ztPM+OgMrh8mskeBdX9gqopwB4bo3x4IvDhu /g6g== X-Forwarded-Encrypted: i=1; AJvYcCWUKePZoUnQskHKFHKEuvKorzyVPJuj8pfR1P4M1HGtiNUg/v8QIKTNFl4kBCkK4PKdtH4xhvZNnx1hRroKIYYC1dF1QajQGXReSHWWcA== X-Gm-Message-State: AOJu0YwSier0K/Qjzkv/65RchnMAFP4+P4pw3CRXlAlyTLcpdrUIvpGG uLGMtH9Zzv/rvN7LBA4Nyd3jfKk0GZXDc3dKk33FqYPsKEo7cSjcvDQeFh1q9TbxcgNwShB7If/ zBgoU9CR3eiGEiQ== X-Google-Smtp-Source: AGHT+IEsxwGIyCMRt8S3kpoTLeGHaoR+N+Zq9q7sZejFEWj8cm4nkIYcecRhbfp4fTAHMgfUv1T4/+xh2KIwHVw= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a2e:7d15:0:b0:2ef:1c03:73e7 with SMTP id 38308e7fff4ca-2f03dbe1817mr25471fa.7.1721917673884; Thu, 25 Jul 2024 07:27:53 -0700 (PDT) Date: Thu, 25 Jul 2024 14:27:37 +0000 In-Reply-To: <20240725-alice-file-v8-0-55a2e80deaa8@google.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240725-alice-file-v8-0-55a2e80deaa8@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=7149; i=aliceryhl@google.com; h=from:subject:message-id; bh=NIY5e2gNiq/wYldTRcMtyFezUNO6yObaGdnkQ//UV7A=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmomDYIR9taxDiZOFpXkHw3q9vFtwkHgafSI2Nc DDub73/NhyJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZqJg2AAKCRAEWL7uWMY5 RtxsD/4jHCh1BSJwTSRAr6sLQW7s2DSG9vj+kS5q1TBhSyIa7huvROeUgZ5Wr2yYtrfbHxjH8I1 0rF4a6Mf1Hs/L2NG0jrwueVYg2OfqPY7XLYCkD5gLDmtMFb9XtkqdDhPmuQB7+RHj1DrGhCWWQc wvVGvj2PtdvnxvitJOXAmjeL/8f0sUt542sEqc1uPtXFFjlRvGRuwxr1oxEM7A0N2svk1LroQZr rvaYhtRDEDYSj4PyN51uCZT8D6uawAbaaiWZ3CSGfZrNYZSIxgDJtxjOlqkouVN9kCudjyOVkL1 f3SMq7VuPfIJJBXA9A44vJ6MTD07rYAEsOb78k7zUEcz9ziCNjU+6s3PYMGqpdunp/ARq37XV+2 g5Cgn1XPkgzoyrMh4vsJWWWivNEi/nEeXTmfvpy87HexdM8k7S/53qcgKClS/cXrE3G4zeHPcFk NbR9YrOmD6N4QIlV7jSrA07z4TeH5EqLCWpnjR5ZaWDkSunYD5s4u137POu+3jYi3aT8Wlp2PiZ 8qHeFxFmXCioe1fXY9pM4/R1I5GbiybeuEJ9VtPJ4oshZjuIDwsnWA5JN7lu+AlPSr5wKo5/alh cqj5taZ3eQL2P1su6nVHyjQ+QUq0GbF02U7M9A6etaUi4vLMBpDMBjCKFLxNf+kJn8vCEm4onsh A415/TH2NIDszqA== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240725-alice-file-v8-4-55a2e80deaa8@google.com> Subject: [PATCH v8 4/8] 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?= =?utf-8?q?=C3=B8nnev=C3=A5g?= " , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan Cc: Dan Williams , Matthew Wilcox , Thomas Gleixner , Daniel Xu , Martin Rodriguez Reboredo , Trevor Gross , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl , Kees Cook 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 Reviewed-by: Gary Guo --- rust/bindings/bindings_helper.h | 1 + rust/helpers.c | 13 +++++++ rust/kernel/cred.rs | 76 +++++++++++++++++++++++++++++++++++++++++ rust/kernel/fs/file.rs | 13 +++++++ rust/kernel/lib.rs | 1 + 5 files changed, 104 insertions(+) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index e2d22f151ec9..22e62b3b34b0 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 5ba1f6de0251..4831abb5a438 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -206,6 +207,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..acee04768927 --- /dev/null +++ b/rust/kernel/cred.rs @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! 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/fs/file.rs b/rust/kernel/fs/file.rs index f1a52814b2da..8bed7bebcc43 100644 --- a/rust/kernel/fs/file.rs +++ b/rust/kernel/fs/file.rs @@ -9,6 +9,7 @@ use crate::{ bindings, + cred::Credential, error::{code::*, Error, Result}, types::{ARef, AlwaysRefCounted, Opaque}, }; @@ -274,6 +275,18 @@ pub fn as_ptr(&self) -> *mut bindings::file { self.inner.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 c364893e13a2..8fb57ec20867 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -28,6 +28,7 @@ pub mod alloc; mod build_assert; +pub mod cred; pub mod error; pub mod fs; pub mod init; From patchwork Thu Jul 25 14:27:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13742016 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 0C52519E7D3 for ; Thu, 25 Jul 2024 14:27:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721917679; cv=none; b=j8lCs9MkP91eiiNjxIg6VvprCoNDgPVqcXscj89gTKhIPUykgaPI/z+uA2ZhNO6iPRo90bqIvORjWqKiuDxOIvo3y3hb7KrtUAxwwClucPZ3jf1gWaopuD0tUYqcL38j8ljeTuJJV1Ls+1WbNejF8yaEa+cWJekliMRilzs7VJY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721917679; c=relaxed/simple; bh=HmtNCDQtZJEcY5US8BaBhrhnETHRhDvmtsa9lhcqgio=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FZ5AiTulwf0oJCR/XUH4lBge72Pe/2Ijm9q7ggdhCzQLVivHtgGMoaFD61KRV1j79ozC1xCL+1rYWIleFvozb2YpSxaQCaSW3lS51jQ7i8cAwCDoakwqzRM62dd3+ijvBJoxB4YNh6d4J90WWYYmoXD7T4hYeLj3cq13lj/0v0g= 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=iKkQ1BBm; arc=none smtp.client-ip=209.85.219.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iKkQ1BBm" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e0b365efb6cso685531276.3 for ; Thu, 25 Jul 2024 07:27:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721917677; x=1722522477; 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=Lo+BlZEzCOkRMbq6aMOskHCZf8MM+d8fsOOsjAvEhzc=; b=iKkQ1BBmd1/XPKSY+0GAlkgcQMPq01pc+fLQERJASUpwYBIKHOeGdpPm1ZMqw13nHN REyZwbFC7H+8axFXacKuUksqn1B85W+TNw9Afh5K/HEcWqaSCUmae3I8tkKS/wHgR6FV EaHIWFsD+gvTqwem9XtYLI29o3AxVjYC0T6jmssqqEgJtLO9AqvaivsrO/QOm1FwjKT1 nT1mEL+3GFFXOEHorH09JU5oxIV/ExvDPRGfWPwKuwB4taNCqvBbwiICyXfP8mRn3FNs UyfJu0G1WRFSw6AdueTo4aomifM0nt5O/G0IuXfv21FHEhQ+/nPJWdLf9tpoxXttUG53 NwFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721917677; x=1722522477; 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=Lo+BlZEzCOkRMbq6aMOskHCZf8MM+d8fsOOsjAvEhzc=; b=r5VbffyWzV/G1Zw2G6L87WSzWqPLCjjoH2xeXXxIcN90Zq898oTUaG0+5HRPIepl33 qQ1DSC+4vEs3NFBTEWMpHdiOzPfFX+yn07BQ4wKwrpSb26rGFxSn/cynafgR710ZI4oQ DeeciUBrAp1jMlehZVU1RTIWQnqXDtKWfYhE6qvGENdLBEoypeRp46ztNz40Sxw/5/kd 2HWzp1u3bKssDgAHNuIKMHzYnrjYt67Lkb1h5F/FFzE7phoEtA7N4U8pHmULqc+vf9XP Fbhvb87L09Ch861Hc7Z9BsXHOB9ewnxxeJKXX/PCz1vPOmhrWFA7thIDVw0s1QD2afR/ VayA== X-Forwarded-Encrypted: i=1; AJvYcCUL4wYsktKdiH2QlV3ELg+ppu84m3W3daKLBYS0yjznRxDg1EZWPw3aJeHILnnNBeMh6UIHk1VN0+1NcvgfhkPD5foufYEchxWFvSJ/pQ== X-Gm-Message-State: AOJu0YxVWDu1IRZOqxJjqJNlMCmVKI7WXD+QPOMqTvVpqPjPa2is9O2t ihzyG8gFe3ftUNmwcAdw5Ry7dPYOmqufqv5nhRd7em090yj8zvBC+WK9nVXlTbHjXTfTn2RiGOc /faZ0u4IaEJxf4w== X-Google-Smtp-Source: AGHT+IGZJ4LPfR/4zii7HUdx6c8AE09ty9AGdsgFOnmdLyZETBPRFMPmSfNYP2nDjKJVg57F2KbFo4WZCpUAF94= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a5b:4d1:0:b0:dfa:6ea5:c8d5 with SMTP id 3f1490d57ef6-e0b2cd5cb98mr4535276.10.1721917676930; Thu, 25 Jul 2024 07:27:56 -0700 (PDT) Date: Thu, 25 Jul 2024 14:27:38 +0000 In-Reply-To: <20240725-alice-file-v8-0-55a2e80deaa8@google.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240725-alice-file-v8-0-55a2e80deaa8@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=6483; i=aliceryhl@google.com; h=from:subject:message-id; bh=HmtNCDQtZJEcY5US8BaBhrhnETHRhDvmtsa9lhcqgio=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmomDZzwmAITfS7QqPxR40tSgrI9rFbAc+K3j8s 5VoYaUV0OqJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZqJg2QAKCRAEWL7uWMY5 Rh2REACKQNXwkZJxO/6/h2no6vzVb+zwAI+WAu9Hi/Afyyu0lAt9/iA3zbKFqdPilwV/iFcW+eu JGB7+Z88s27dKPkCvZovWKB9XJip209O7t77E74MVG8R/vbe67zeD2iq6ClnyhunvRT0oidVlwP IYsYe0Ne86md0nVX94MevPeeuxvlkZMX5qJ0yAx5wjmiUmnNlRq7sIHNXayaq1aetVgna4jYxa6 NXFMNHNx2XUbv91MSUrOgnDiVJGvRKraO3/TpFhhTHUVO8MXrauujF8ILA0YR3OoaCxL67KrGZw EnUfAfpiaM40wfvwvEf2SARlMmuNMEXqlSTJNpSfTsoJQj9Ks5eYiaPY5uIBD7rgkho5Rgq/vap Xo/volWUhp6wA1qbeKWQvaK2YXz6BNpVIr59JXOnMJyhWHWwSrTP1XXJFvb+YRG4xtrfs7QWBdb HGhiKA9fi66iv2LHH5aHHyWGiImSRxcHf4QLot+Qgou+2myO9w2HaKxBMeJ13ZG4cTUAyPno73e sP1CSkdfOVLOKQ02yjcA2J5O0dcQvsWDuZGlYlOfcEVI/06xzsC3U1I5vgzbDGRGwpi6tz2LCg+ Hz5Qc2El9EyM+T7JfSFNM5NNnD5KQ9NpwaS7fYE7aAdYQ1X0jqbuPgkin5rbxAUSpK9pOia0BoR gSdOk7rTwMUoI8w== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240725-alice-file-v8-5-55a2e80deaa8@google.com> Subject: [PATCH v8 5/8] rust: security: add abstraction for secctx 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?= =?utf-8?q?=C3=B8nnev=C3=A5g?= " , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan Cc: Dan Williams , Matthew Wilcox , Thomas Gleixner , Daniel Xu , Martin Rodriguez Reboredo , Trevor Gross , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl , Kees Cook Add an abstraction for viewing the string representation of a security context. This is needed by Rust Binder because it has a feature where a process can view the string representation of the security context for incoming transactions. The process can use that to authenticate incoming transactions, and since the feature is provided by the kernel, the process can trust that the security context is legitimate. Reviewed-by: Benno Lossin Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Trevor Gross Signed-off-by: Alice Ryhl Reviewed-by: Gary Guo --- rust/bindings/bindings_helper.h | 1 + rust/helpers.c | 21 ++++++++++++ rust/kernel/cred.rs | 8 +++++ rust/kernel/lib.rs | 1 + rust/kernel/security.rs | 74 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 105 insertions(+) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index 22e62b3b34b0..afa24d54c1a0 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/helpers.c b/rust/helpers.c index 4831abb5a438..b61f5a8ce1da 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -219,6 +220,26 @@ void rust_helper_put_cred(const struct cred *cred) } EXPORT_SYMBOL_GPL(rust_helper_put_cred); +#ifndef CONFIG_SECURITY +void rust_helper_security_cred_getsecid(const struct cred *c, u32 *secid) +{ + security_cred_getsecid(c, secid); +} +EXPORT_SYMBOL_GPL(rust_helper_security_cred_getsecid); + +int rust_helper_security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) +{ + return security_secid_to_secctx(secid, secdata, seclen); +} +EXPORT_SYMBOL_GPL(rust_helper_security_secid_to_secctx); + +void rust_helper_security_release_secctx(char *secdata, u32 seclen) +{ + security_release_secctx(secdata, seclen); +} +EXPORT_SYMBOL_GPL(rust_helper_security_release_secctx); +#endif + /* * `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 index acee04768927..92659649e932 100644 --- a/rust/kernel/cred.rs +++ b/rust/kernel/cred.rs @@ -52,6 +52,14 @@ pub unsafe fn from_ptr<'a>(ptr: *const bindings::cred) -> &'a Credential { unsafe { &*ptr.cast() } } + /// Get the id for this security context. + pub fn get_secid(&self) -> u32 { + let mut secid = 0; + // SAFETY: The invariants of this type ensures that the pointer is valid. + unsafe { bindings::security_cred_getsecid(self.0.get(), &mut secid) }; + secid + } + /// 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` diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 8fb57ec20867..a8de8293376e 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -40,6 +40,7 @@ pub mod page; pub mod prelude; pub mod print; +pub mod security; mod static_assert; #[doc(hidden)] pub mod std_vendor; diff --git a/rust/kernel/security.rs b/rust/kernel/security.rs new file mode 100644 index 000000000000..2522868862a1 --- /dev/null +++ b/rust/kernel/security.rs @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Linux Security Modules (LSM). +//! +//! C header: [`include/linux/security.h`](srctree/include/linux/security.h). + +use crate::{ + bindings, + error::{to_result, Result}, +}; + +/// A security context string. +/// +/// # Invariants +/// +/// The `secdata` and `seclen` fields correspond to a valid security context as returned by a +/// successful call to `security_secid_to_secctx`, that has not yet been destroyed by calling +/// `security_release_secctx`. +pub struct SecurityCtx { + secdata: *mut core::ffi::c_char, + seclen: usize, +} + +impl SecurityCtx { + /// Get the security context given its id. + pub fn from_secid(secid: u32) -> Result { + let mut secdata = core::ptr::null_mut(); + let mut seclen = 0u32; + // SAFETY: Just a C FFI call. The pointers are valid for writes. + to_result(unsafe { bindings::security_secid_to_secctx(secid, &mut secdata, &mut seclen) })?; + + // INVARIANT: If the above call did not fail, then we have a valid security context. + Ok(Self { + secdata, + seclen: seclen as usize, + }) + } + + /// Returns whether the security context is empty. + pub fn is_empty(&self) -> bool { + self.seclen == 0 + } + + /// Returns the length of this security context. + pub fn len(&self) -> usize { + self.seclen + } + + /// Returns the bytes for this security context. + pub fn as_bytes(&self) -> &[u8] { + let ptr = self.secdata; + if ptr.is_null() { + debug_assert_eq!(self.seclen, 0); + // We can't pass a null pointer to `slice::from_raw_parts` even if the length is zero. + return &[]; + } + + // SAFETY: The call to `security_secid_to_secctx` guarantees that the pointer is valid for + // `seclen` bytes. Furthermore, if the length is zero, then we have ensured that the + // pointer is not null. + unsafe { core::slice::from_raw_parts(ptr.cast(), self.seclen) } + } +} + +impl Drop for SecurityCtx { + fn drop(&mut self) { + // SAFETY: By the invariant of `Self`, this frees a pointer that came from a successful + // call to `security_secid_to_secctx` and has not yet been destroyed by + // `security_release_secctx`. + unsafe { bindings::security_release_secctx(self.secdata, self.seclen as u32) }; + } +} From patchwork Thu Jul 25 14:27:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13742017 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 D80C119E819 for ; Thu, 25 Jul 2024 14:28:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721917682; cv=none; b=SfOBkp26U3J1TLcYI3wRzAwIUNtBut3ky8LGAFHe1VToIoduOhzJBk7Lyahx4rRRCKZNPzpMmSZOIfoH69t1Ky7oeB19lxK8lg2tGvGLziMYAB5oqM+jBTm/MO9Is5FhJxDw1D3mJU0CaznRYNuGs3mRCUcGwnxzMEcjwv+qIOc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721917682; c=relaxed/simple; bh=J91Gx6MIvC8NeTYkxxFTLjHgsB7/VaJDgCuCqcQNtJs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HRPs09UuXc6YB6JrfV95KaDNYoPSmxcCZ1lcmS0bL+yZ6aVsMxs8yCTwRmyFgnAhVcsknV5POGAPjXd3WtCDaqUHEu3ANUFfFvN7S6OCDFdzXRbcE5JfccgEwtGxEVy/VBNhOfO2j3q623jnhW/wABXtZIoar+pHQzx2G9siVfw= 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=bPrXwkLJ; arc=none smtp.client-ip=209.85.219.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bPrXwkLJ" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e0b2fb62a07so1060828276.0 for ; Thu, 25 Jul 2024 07:28:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721917680; x=1722522480; 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=WCadNj0kmUPw3EC6sSlLJ1QlRyEDEjrL02Pe/kDxaDs=; b=bPrXwkLJoIrLh1rvK/CCkiLssEQ/o9ItzZnJwwNt5wZWM479fSyWibRYVZBP0RNIoJ 2cJFiD3JZDtEjYRvls0wlyoCe3Y2oJbx4Hj1yTp5pMWgePb+YwjZBP5ZopFG/UaUR0qp nlIkHA6xVlGtftOGl136E04WF3WfGJGlmU67kd/j/OYg1EUTeI3C/wfSmTi1KRIekyI2 I/ev7QN+gc/drD7Cr25YhwfcCoTf0XULYY6b+bj3VzgYoMr6wW5Vnv7xdRYTgupFlfy8 FUe1A6QxPFCTOCqrvcVU/j8oJW7x656filQiBtob6RYKGUXlsgLtjkHM/UrydXq1SO61 qEIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721917680; x=1722522480; 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=WCadNj0kmUPw3EC6sSlLJ1QlRyEDEjrL02Pe/kDxaDs=; b=PBOVr/creM96ocmuBiiQqAsAfRFJf6pAiJk+fWS3UipwkP29iBjattnkqyeiUnpsnW AiKGk1d/I4YPiDyxofT/rXa91f5wcdePFoeXRtAUiYMyUioQ8MrLm3UG8VMl1D/9Txu+ k6nVGItzjhUJUsdUUT6waG5X4Ba6va5061PEfZYmF71kF45aoxkrQaxg7s6+3UnNtVFR Y8gWJMOJmhs+qudRMuGwQic1tX3iFXJiqJcHSthzzIz1p50MfQyAqnf6eYGRHLgRxR+P cXgQL64xEBGlQBd4B/NClPWwc3tMNXsh5d46ugByX8thCPdBqiU68mRTixnWcEKiqzVA aOaw== X-Forwarded-Encrypted: i=1; AJvYcCV2zpGZd846QlBOlLZjT4vPj12O2w1Xc+HoKTgB92pFWkaq12JOiHceb79GFBmPpHJHrHa0aXnL7CWMNBearPLXm5UDuIuLkN1sW0U4Rw== X-Gm-Message-State: AOJu0Yw8R1ovGzDOscoA9RTwTAXcPcxEsOUUmIRPJ9w5a4FcMocodNRp Gw/NHCf918xmmB0du+BKlbhAZfSEKgzuUALBYvl+HfU05smyflhsvR6YNJMwWSv/lQJioj+RFXK ClSPVIM2U0u9xyw== X-Google-Smtp-Source: AGHT+IFKYihlbOoX4iJsmGs9EfswZcG8G7huPM/Kp+v9+uVgvno5FkEt+tRzpYzNQMus2OCOcCPmt1f5OwY66Ig= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6902:2b0f:b0:e05:65b7:32d9 with SMTP id 3f1490d57ef6-e0b2ca7734cmr22335276.6.1721917679919; Thu, 25 Jul 2024 07:27:59 -0700 (PDT) Date: Thu, 25 Jul 2024 14:27:39 +0000 In-Reply-To: <20240725-alice-file-v8-0-55a2e80deaa8@google.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240725-alice-file-v8-0-55a2e80deaa8@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=5384; i=aliceryhl@google.com; h=from:subject:message-id; bh=Dwps4ysAMwtCKIr0qbQaLdxg6EG+tQNeQs65V1osc9I=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmomDa+P7LzNE+jmX+/kQHXyXhbz5uo1UajGv8c 3SDDziX8GaJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZqJg2gAKCRAEWL7uWMY5 Rv1wD/9yIpYyp+1FzfVlXK5fro/+UN5sDKFyuVNC75IP8CLT6XcV3QknwbTAR23Jh05FQpF7+Pf jrz1iMF6MGxEu7dHpd+Cl+DYCvXcj0lLCya+ACthV9pGo7t8njodU8iIWPX5iWBDv8LqTNX+kft mKhg5iHuvAmi8RwVkEUQfiS1OspLzSSX6D3sO86RygZNpbKq8GhAd7b3J341plPPzDRq1N0mE9a ZK80uLJEYIdmdQisnranp/36sHgVvxreXbZyUx0DDVQktnaPU70yRHqgylgJcTQ25YpfvGPSdIw 68XWOIct7/yqFDfkFO9IbqWMZjWjRIyZkwmDrQ+J1aJ2gty5eHQfCSDjzsVy2xpdvMJZMBXqyoh zcOm7ohj2DqwATIzS82N0tXEYt5X5egNQ49Oy9uiWFrmdw6NEXNBXwCC0JzHQ1O+RrRPpbEHAfc UFrD9y4QhyV4axUSVWfyFEmShVg4naOomjnaZKHj9WoTqxroCvhPtXbMpIEoZL1RtXXx3s28m5g TQGTrj68xvKhQERJ0mrtk9mzDlV/K4v1m7FrikEh3Ltr8MkyfBdTaBcKcwIz29uNVG262QRQHfr FKq9qPioWU5Kx+f5qgLUrHkipdtPhEiGiOO1awCA6Q/9vdRSIhrHPft8exKcqcJdHqMHKaZoGNU pj6yiYSwGLvsqtA== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240725-alice-file-v8-6-55a2e80deaa8@google.com> Subject: [PATCH v8 6/8] rust: file: add `FileDescriptorReservation` 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?= =?utf-8?q?=C3=B8nnev=C3=A5g?= " , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan Cc: Dan Williams , Matthew Wilcox , Thomas Gleixner , Daniel Xu , Martin Rodriguez Reboredo , Trevor Gross , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl , Kees Cook From: Wedson Almeida Filho Allow for the creation of a file descriptor in two steps: first, we reserve a slot for it, then we commit or drop the reservation. The first step may fail (e.g., the current process ran out of available slots), but commit and drop never fail (and are mutually exclusive). This is needed by Rust Binder when fds are sent from one process to another. It has to be a two-step process to properly handle the case where multiple fds are sent: The operation must fail or succeed atomically, which we achieve by first reserving the fds we need, and only installing the files once we have reserved enough fds to send the files. Fd reservations assume that the value of `current` does not change between the call to get_unused_fd_flags and the call to fd_install (or put_unused_fd). By not implementing the Send trait, this abstraction ensures that the `FileDescriptorReservation` cannot be moved into a different process. Signed-off-by: Wedson Almeida Filho Co-developed-by: Alice Ryhl Reviewed-by: Benno Lossin Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Trevor Gross Signed-off-by: Alice Ryhl Reviewed-by: Gary Guo --- rust/kernel/fs/file.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/rust/kernel/fs/file.rs b/rust/kernel/fs/file.rs index 8bed7bebcc43..af98513bb2ee 100644 --- a/rust/kernel/fs/file.rs +++ b/rust/kernel/fs/file.rs @@ -11,7 +11,7 @@ bindings, cred::Credential, error::{code::*, Error, Result}, - types::{ARef, AlwaysRefCounted, Opaque}, + types::{ARef, AlwaysRefCounted, NotThreadSafe, Opaque}, }; use core::ptr; @@ -368,6 +368,79 @@ unsafe fn dec_ref(obj: ptr::NonNull) { } } +/// A file descriptor reservation. +/// +/// This allows the creation of a file descriptor in two steps: first, we reserve a slot for it, +/// then we commit or drop the reservation. The first step may fail (e.g., the current process ran +/// out of available slots), but commit and drop never fail (and are mutually exclusive). +/// +/// Dropping the reservation happens in the destructor of this type. +/// +/// # Invariants +/// +/// The fd stored in this struct must correspond to a reserved file descriptor of the current task. +pub struct FileDescriptorReservation { + fd: u32, + /// Prevent values of this type from being moved to a different task. + /// + /// The `fd_install` and `put_unused_fd` functions assume that the value of `current` is + /// unchanged since the call to `get_unused_fd_flags`. By adding this marker to this type, we + /// prevent it from being moved across task boundaries, which ensures that `current` does not + /// change while this value exists. + _not_send: NotThreadSafe, +} + +impl FileDescriptorReservation { + /// Creates a new file descriptor reservation. + pub fn get_unused_fd_flags(flags: u32) -> Result { + // SAFETY: FFI call, there are no safety requirements on `flags`. + let fd: i32 = unsafe { bindings::get_unused_fd_flags(flags) }; + if fd < 0 { + return Err(Error::from_errno(fd)); + } + Ok(Self { + fd: fd as u32, + _not_send: NotThreadSafe, + }) + } + + /// Returns the file descriptor number that was reserved. + pub fn reserved_fd(&self) -> u32 { + self.fd + } + + /// Commits the reservation. + /// + /// The previously reserved file descriptor is bound to `file`. This method consumes the + /// [`FileDescriptorReservation`], so it will not be usable after this call. + pub fn fd_install(self, file: ARef) { + // SAFETY: `self.fd` was previously returned by `get_unused_fd_flags`. We have not yet used + // the fd, so it is still valid, and `current` still refers to the same task, as this type + // cannot be moved across task boundaries. + // + // Furthermore, the file pointer is guaranteed to own a refcount by its type invariants, + // and we take ownership of that refcount by not running the destructor below. + // Additionally, the file is known to not have any non-shared `fdget_pos` calls, so even if + // this process starts using the file position, this will not result in a data race on the + // file position. + unsafe { bindings::fd_install(self.fd, file.as_ptr()) }; + + // `fd_install` consumes both the file descriptor and the file reference, so we cannot run + // the destructors. + core::mem::forget(self); + core::mem::forget(file); + } +} + +impl Drop for FileDescriptorReservation { + fn drop(&mut self) { + // SAFETY: By the type invariants of this type, `self.fd` was previously returned by + // `get_unused_fd_flags`. We have not yet used the fd, so it is still valid, and `current` + // still refers to the same task, as this type cannot be moved across task boundaries. + unsafe { bindings::put_unused_fd(self.fd) }; + } +} + /// Represents the `EBADF` error code. /// /// Used for methods that can only fail with `EBADF`. From patchwork Thu Jul 25 14:27:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13742018 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 1627D19EEBE for ; Thu, 25 Jul 2024 14:28:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721917685; cv=none; b=Nmv60TmUXam1jGylKPEtJnr3pL6GuSb+OxJ4+ChdQJI862nxBf127F1U5GNM5SpwXSbHyxQsFgl/+6gcIHhSKQ0xI02zfqM/HetygAkb9ToxbVi42dEZQHL4TzCqRnSlpvxeRi/Hvw6gdptAktbt0Q64f0/HacVhPMNMafjyYxk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721917685; c=relaxed/simple; bh=RB5fUBvZmSdvq9L2nU1k9qW9nGCScm08ZZQbKJ0RktQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MAdf45s59HNrV4APCrzLSlJjDFh04f+gT3ODedkGxKkBZG63iqFql2xUeilIGSlOFSWAKMJGaCN/wpT/aS+EN5fjA5g7wGXogUFkLY5kTYvtMFQ4OO7VWEpYfq3DZ1CmgYvOCgJ5EAoONwQQrnBiqmgyheeHFHD0uO4CfDalPcE= 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=CKX2ZRj3; arc=none smtp.client-ip=209.85.219.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="CKX2ZRj3" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e087ed145caso1673126276.3 for ; Thu, 25 Jul 2024 07:28:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721917683; x=1722522483; 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=Csn7tQLa+mrAk7iBu1tCNWUYbx+LRw9b+4+GLxKr0hU=; b=CKX2ZRj3byGEUc+a0wgYwyGRnh6NEyLZ0stzN5BrIxRK1dLN2nKNfqUyIjW1Z0+Cn9 ZM+VZoxBcXAS4MYMOP4BvBiFro+sN5XGNp6IEII0sZcBxrO8INFdDkf5ZM00GU9DUXS+ HypMUQvpwwSixq99oIZxBhXA5MAg5D8OjykuzKMovHPFeJsBarh/YBRa2NtAP8MkBCaO I2Hvw4spa3gllV+IAXDhdRrl6OGHHuacuMG2mMDX4ddDXnD1w1Y3vpIxzCtcK8RSMVZ5 Vw4Lf4iFjJpbtcT2PCBQNk9oJbkAjeGwDjvc415qAGik3t2lPXCE4Fi6TvJdGTAlzs/q 5bhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721917683; x=1722522483; 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=Csn7tQLa+mrAk7iBu1tCNWUYbx+LRw9b+4+GLxKr0hU=; b=vbYtgDzOIGu22vGdK/j5uasu/SYzOfXzg8eg5v8h29DZYpPIEVELKKguh7QLVmrVcE y9sQEUO9VAHJwuqfMnEvyf2cGI59G4bFCEezFdSE5gMVtjeLH5L0oZ9w5WUPCMQPNVw7 QLPm8JoAsbH4dlul9cDyAwyIUJyd+ZgETVNkGnm0yCPbAkV4fXpwVktXB2n9OBgIrsH0 me3k/iiN7Fl5ojI3TMakMaTuXIRTOwOI1ywYCln2THx/8fIRus6HSzG+Xs48qUXYkbVH kAD5kOs3BQt+cMOgotWmzYwbbX3kEcH1Zyg8RKXJu1SUBYNT3GR5Y6mfr2hoT2wncq3a ghVw== X-Forwarded-Encrypted: i=1; AJvYcCXehtDGSfXB39mJpbGRzajs2WTuOXcAqlfqJiyMjt42IHR6K8f/Wu7a+FCBxVJrb1aJz4nZXlsGGX6HtC2JHFnN5QPbvPF4j0YqoaBUdg== X-Gm-Message-State: AOJu0YzfsZUHScgwe4L8+eJDmFXlEPNpU5BPsN9Y/n8sMQbMS3EHUw+7 5ugejdQwxnqEd/Q1A/YuB3k+EIYA1gU5G6mLo0gqY3l5E67d7iJFQERCWnICu8I5ahChpuBANYy Kig7c0a6L6AYhdw== X-Google-Smtp-Source: AGHT+IHjNiBkce9Y7Rv3UU9G6zAWhyh0d+Nn04+di2Sll0COttEUFIpU+KZRv/R01wGbE+M/hI3INLySw+7m21I= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6902:102a:b0:e0b:f93:fe8c with SMTP id 3f1490d57ef6-e0b2c6be3b5mr15445276.0.1721917683034; Thu, 25 Jul 2024 07:28:03 -0700 (PDT) Date: Thu, 25 Jul 2024 14:27:40 +0000 In-Reply-To: <20240725-alice-file-v8-0-55a2e80deaa8@google.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240725-alice-file-v8-0-55a2e80deaa8@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=7801; i=aliceryhl@google.com; h=from:subject:message-id; bh=RB5fUBvZmSdvq9L2nU1k9qW9nGCScm08ZZQbKJ0RktQ=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmomDaOQy9H0VLTzKJjZ/FuWud6hd1PQ7N6BO8z 8pE96uPnGeJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZqJg2gAKCRAEWL7uWMY5 RuxHD/4zpGDFqB27kn/0o5xChNX4DpxWRUQBKkUD+mcWn0oGEB9ZqACT8c11EtSJwlvzs8XFEnN FqW8PiQtH0BphOw0sXgxHTPN17gQbqyOWT6fZmUSNCqRtEyZcsIdVxlnZgFyK3812vvB9UDrgOx RRhk9jx3p1FW8QtfMwB/wlnmprjGRTt+OqwtulWiwkz9lg3R+Mnl7jjLQXBXcrGSWuaG5xb///S Ggb7RiRyhlFa5Vp3K9PffT2LZVphAN0ZiRYGTmPiQFfKygfAGqVUUXljthkEbilJpWuBQQ4Xeuy gy8SgZ1Hrtl0Q2IP9cEA6YoLXlHGgCZJXjGq2P0SSV9SSLYwnrYKAOOqhcRRb/5lKez+1lvgCsW 0biBJNCiF5sEjPtSQgKWA6THJNWhnzJ5W8jyq4n915qIWOC49PSqsJmC8mx/Gpy+kKxv8PY6RmC KuBkwfqbliqWSa5nAmsPy/KEfqvbbBiRkKIRbBD7mff3IiXeghgLI69i+z1jC4l0xjBKIyh0kwX yTA3i7hnLaCJcx33bfn2UKQTGDoWWYIkIf2Bl+7FcuekeDavI/eN1yRbbKJr+m0APNiOn+fUK7F q7J7dPZHygsc7575XFynm3oVIbgcYUCpQ28S2g2NSMEzZzt3enU6fmmu2D88xGjBuid/5AbYdhX JMNwhfXM4wqwBVg== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240725-alice-file-v8-7-55a2e80deaa8@google.com> Subject: [PATCH v8 7/8] rust: file: add `Kuid` wrapper 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?= =?utf-8?q?=C3=B8nnev=C3=A5g?= " , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan Cc: Dan Williams , Matthew Wilcox , Thomas Gleixner , Daniel Xu , Martin Rodriguez Reboredo , Trevor Gross , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl , Kees Cook 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 Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Trevor Gross Signed-off-by: Alice Ryhl --- rust/bindings/bindings_helper.h | 1 + rust/helpers.c | 45 ++++++++++++++++++++++++++++ rust/kernel/cred.rs | 5 ++-- rust/kernel/task.rs | 66 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 2 deletions(-) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index afa24d54c1a0..d7f7ae109e6f 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/helpers.c b/rust/helpers.c index b61f5a8ce1da..dda7d26ccf8f 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -177,6 +177,51 @@ void rust_helper_put_task_struct(struct task_struct *t) } EXPORT_SYMBOL_GPL(rust_helper_put_task_struct); +kuid_t rust_helper_task_uid(struct task_struct *task) +{ + return task_uid(task); +} +EXPORT_SYMBOL_GPL(rust_helper_task_uid); + +kuid_t rust_helper_task_euid(struct task_struct *task) +{ + return task_euid(task); +} +EXPORT_SYMBOL_GPL(rust_helper_task_euid); + +#ifndef CONFIG_USER_NS +uid_t rust_helper_from_kuid(struct user_namespace *to, kuid_t uid) +{ + return from_kuid(to, uid); +} +EXPORT_SYMBOL_GPL(rust_helper_from_kuid); +#endif /* CONFIG_USER_NS */ + +bool rust_helper_uid_eq(kuid_t left, kuid_t right) +{ + return uid_eq(left, right); +} +EXPORT_SYMBOL_GPL(rust_helper_uid_eq); + +kuid_t rust_helper_current_euid(void) +{ + return current_euid(); +} +EXPORT_SYMBOL_GPL(rust_helper_current_euid); + +struct user_namespace *rust_helper_current_user_ns(void) +{ + return current_user_ns(); +} +EXPORT_SYMBOL_GPL(rust_helper_current_user_ns); + +pid_t rust_helper_task_tgid_nr_ns(struct task_struct *tsk, + struct pid_namespace *ns) +{ + return task_tgid_nr_ns(tsk, ns); +} +EXPORT_SYMBOL_GPL(rust_helper_task_tgid_nr_ns); + struct kunit *rust_helper_kunit_get_current_test(void) { return kunit_get_current_test(); diff --git a/rust/kernel/cred.rs b/rust/kernel/cred.rs index 92659649e932..81d67789b16f 100644 --- a/rust/kernel/cred.rs +++ b/rust/kernel/cred.rs @@ -10,6 +10,7 @@ use crate::{ bindings, + task::Kuid, types::{AlwaysRefCounted, Opaque}, }; @@ -61,11 +62,11 @@ pub fn get_secid(&self) -> u32 { } /// Returns the effective UID of the given credential. - pub fn euid(&self) -> bindings::kuid_t { + pub fn euid(&self) -> Kuid { // 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 } + Kuid::from_raw(unsafe { (*self.0.get()).euid }) } } diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs index 367b4bbddd9f..1a36a9f19368 100644 --- a/rust/kernel/task.rs +++ b/rust/kernel/task.rs @@ -9,6 +9,7 @@ types::{NotThreadSafe, Opaque}, }; use core::{ + cmp::{Eq, PartialEq}, ffi::{c_int, c_long, c_uint}, ops::Deref, ptr, @@ -96,6 +97,12 @@ unsafe impl Sync for Task {} /// The type of process identifiers (PIDs). type Pid = bindings::pid_t; +/// The type of user identifiers (UIDs). +#[derive(Copy, Clone)] +pub struct Kuid { + kuid: bindings::kuid_t, +} + impl Task { /// Returns a raw pointer to the current task. /// @@ -157,12 +164,31 @@ pub fn pid(&self) -> Pid { unsafe { *ptr::addr_of!((*self.0.get()).pid) } } + /// Returns the UID of the given task. + pub fn uid(&self) -> Kuid { + // SAFETY: By the type invariant, we know that `self.0` is valid. + Kuid::from_raw(unsafe { bindings::task_uid(self.0.get()) }) + } + + /// Returns the effective UID of the given task. + pub fn euid(&self) -> Kuid { + // SAFETY: By the type invariant, we know that `self.0` is valid. + Kuid::from_raw(unsafe { bindings::task_euid(self.0.get()) }) + } + /// Determines whether the given task has pending signals. pub fn signal_pending(&self) -> bool { // SAFETY: By the type invariant, we know that `self.0` is valid. unsafe { bindings::signal_pending(self.0.get()) != 0 } } + /// Returns the given task's pid in the current pid namespace. + pub fn pid_in_current_ns(&self) -> Pid { + // SAFETY: We know that `self.0.get()` is valid by the type invariant, and passing a null + // pointer as the namespace is correct for using the current namespace. + unsafe { bindings::task_tgid_nr_ns(self.0.get(), ptr::null_mut()) } + } + /// Wakes up the task. pub fn wake_up(&self) { // SAFETY: By the type invariant, we know that `self.0.get()` is non-null and valid. @@ -184,3 +210,43 @@ unsafe fn dec_ref(obj: ptr::NonNull) { unsafe { bindings::put_task_struct(obj.cast().as_ptr()) } } } + +impl Kuid { + /// Get the current euid. + #[inline] + pub fn current_euid() -> Kuid { + // SAFETY: Just an FFI call. + Self::from_raw(unsafe { bindings::current_euid() }) + } + + /// Create a `Kuid` given the raw C type. + #[inline] + pub fn from_raw(kuid: bindings::kuid_t) -> Self { + Self { kuid } + } + + /// Turn this kuid into the raw C type. + #[inline] + pub fn into_raw(self) -> bindings::kuid_t { + self.kuid + } + + /// Converts this kernel UID into a userspace UID. + /// + /// Uses the namespace of the current task. + #[inline] + pub fn into_uid_in_current_ns(self) -> bindings::uid_t { + // SAFETY: Just an FFI call. + unsafe { bindings::from_kuid(bindings::current_user_ns(), self.kuid) } + } +} + +impl PartialEq for Kuid { + #[inline] + fn eq(&self, other: &Kuid) -> bool { + // SAFETY: Just an FFI call. + unsafe { bindings::uid_eq(self.kuid, other.kuid) } + } +} + +impl Eq for Kuid {} From patchwork Thu Jul 25 14:27:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13742019 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 00AE519F477 for ; Thu, 25 Jul 2024 14:28:08 +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=1721917691; cv=none; b=dgfAs6MlSt03K+rpIMz+bTmzBLJUtGgQxb19ZmDNCILA1tTpCdwlhcwvpVUTH/ZHPA/tl/AbzAHTXeLFUB8Yc9q6fU+FDaeNIk1nJ1diz/KQJXwuhnMXgNdNyY+i1aiQqqh9oXFyz+SWPzpbNIFzBXvNLwMvqApJaMzqDUt2URY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721917691; c=relaxed/simple; bh=hxLniq6yNyNznDCTiHHngqdhzx2YJRtVONgjfGpwTUY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=acsvWcJP1XGEtJvK50+tjfX3FaZscC1UF6kC22rRGGAKm0ZZ5o8OOWoklhbYfHeMLPMAPMV3g21dVLUb4gdvfRzHXWpOlj98p+Tc8rM7LrysXuC8hsZNry8Ehkm9B/HJpwckeyam58RCPQS5wCXVizwhf3vVsBxDFNQWwnwuFrE= 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=uNJYFXcE; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="uNJYFXcE" Received: by mail-lj1-f202.google.com with SMTP id 38308e7fff4ca-2ef205d48ebso1970971fa.3 for ; Thu, 25 Jul 2024 07:28:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721917687; x=1722522487; 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=uzfkxy0T49pCsBww7qtAhWzqmcnTx5hmWNC+BQlP7as=; b=uNJYFXcEqi+9YtcviH6OfCATs8IRavf6Otl3I4+jNeJPWpB2mtSwLwBK6fY9KRIU4M yvl0LllVU4wC0VNdBzTEmhHNWN4XazqoGDCPyPhILB+U0SK3wr0EyAhf4tBPXiCmFdrP nql2LVHNR/C1Kzg9kho242k20sx01/K5ZQDck5TAJ+nKvYR4Twg6yIfjOhBWdzJ5FPHl hXGT0he1KsOnfnZU8ny8zSs6XMrMUJsGnU1tAn//N/XZoulLak35HLogcfwb/GyxEY8r 0ICZrFJTNLt58mhSIkKi5/SFE8CE0i+DCR4wqiQekQXO2wZm/CCNwJT4EGzr/614GxDK d/Tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721917687; x=1722522487; 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=uzfkxy0T49pCsBww7qtAhWzqmcnTx5hmWNC+BQlP7as=; b=dupuiLmKKl1Z/N2UGhjmMbsc234WBqA/eScsRp6NGUTaYngUxu+6GkL7UGxuownIpn lz97b0wLqxhB11sO55aCihONRVJd5bJ0y6ifbt+LPvgYyf4oEODF17UIHq0nL57wK9/x rfztxLXNy/Nkk6lvp3fL9avVlKiUs/ctHy7HAR3vVly1QK6gfA1uHHzleNIBTBilT/VG urdBVT1RtmSYVGz+tClcxG+yz+aRY+dHtaztQBSXc9JCjYEC6B8Mx8txwPKMOaiStI98 8184eBl+JrL8FSQ9GADdxrOa2F4NiFpfM6BETdQHh7m2NsCnS3L/6jSVPtvtBJDuUON/ NTCQ== X-Forwarded-Encrypted: i=1; AJvYcCUxWq5TfTOuGoEQ9k2d4pWIGxHCNsg2XZ3OLMHnJr5z+LURl6ZMaSUSi5Dmsp4Vmp/1T6lYpeECxokj6zPyZ7kBlofqNjfzhES4ucRXmA== X-Gm-Message-State: AOJu0YxB9v5TOTXVNilHqHEVk4oL/XYGb4nRRkvXpq5bcx2vUXvk9KIH x9lkPL9EYcMoBxRetC2jDcgyc8uHZpmqXgH1MEAkxObvpSskxobBcyYsHyX6jWpa7rRAn2ii5Tx pr2KFDJh+ax16YQ== X-Google-Smtp-Source: AGHT+IGMbYL743+8/f8aXkoRFyolwZ5ePIvxRon1wwAiXAJH8YagdZ9h6BmGjEVzAWoim1DdrpXGZy73wicRty0= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a2e:960c:0:b0:2ef:1fc0:4d76 with SMTP id 38308e7fff4ca-2f03dbb3977mr26511fa.4.1721917687296; Thu, 25 Jul 2024 07:28:07 -0700 (PDT) Date: Thu, 25 Jul 2024 14:27:41 +0000 In-Reply-To: <20240725-alice-file-v8-0-55a2e80deaa8@google.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240725-alice-file-v8-0-55a2e80deaa8@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=7281; i=aliceryhl@google.com; h=from:subject:message-id; bh=hxLniq6yNyNznDCTiHHngqdhzx2YJRtVONgjfGpwTUY=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmomDby/cEnK7Sc3xSnl9qOhF0JYGj7Cgmie2Dv V9lfMsFii+JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZqJg2wAKCRAEWL7uWMY5 RvzxD/0Treg9TICf5sVlaSfn8QrEHJeWCwWsFmDjsPoF7MrG23AgH/jscw9UdOOttRCNorzjSrd MwTqJdrviwyGPOkuQRW6YpSoBPYO7mdwsO/DesiKHAYuxIkHP5L8+ANkA4ykcaw14jvvJU92Dkf L7zUAM3G306HHXe8NOzWBg2GaQVGe82TmGC/SOGkgBCd7BKw0eEm8y1gXhZEGtWO8zdPse5s+EM fvlYYGSQ+49IOCRaqBxGaRivQErUXEDzW09H80iOlFU7STISVGswP6yMFTkd8l0pox8RCb9scsd dINXdl2UWPEley+IJ9pjKVydctoguOaePp8aI2uSH3P3L9stE6pik+3m0Si+2f4z4OLKm2osUeA iKqfNlaZbSjWyGxwPYyOhCaD8YI7ukvNLwPRkygv58tt/M2sB4dfxzmEVSxkvRymbCziSL+auJA nGHxHKHBreL28etKawqkFXp8/aH7Pu4faYWoq0wuCuT1pby0hYDfrDYUitH0wbn+5S1BQeuFyBN vjOEV/88jtyTgJUTCxa4HTT0nskGmNwk9x5XcsVuC8Oo2f04FSV4sNKCfZDr6vApHvsPH6xakIP R/9o/E707c+ItugXiZQGxr6PjAhC+JONpMlSHuFBStJXDas62cIHFQ05i+kRhm6zTqgIeEL+pa/ iVkjEr1mf/FLliQ== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240725-alice-file-v8-8-55a2e80deaa8@google.com> Subject: [PATCH v8 8/8] rust: file: add abstraction for `poll_table` 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?= =?utf-8?q?=C3=B8nnev=C3=A5g?= " , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan Cc: Dan Williams , Matthew Wilcox , Thomas Gleixner , Daniel Xu , Martin Rodriguez Reboredo , Trevor Gross , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl , Kees Cook The existing `CondVar` abstraction is a wrapper around `wait_queue_head`, but it does not support all use-cases of the C `wait_queue_head` type. To be specific, a `CondVar` cannot be registered with a `struct poll_table`. This limitation has the advantage that you do not need to call `synchronize_rcu` when destroying a `CondVar`. However, we need the ability to register a `poll_table` with a `wait_queue_head` in Rust Binder. To enable this, introduce a type called `PollCondVar`, which is like `CondVar` except that you can register a `poll_table`. We also introduce `PollTable`, which is a safe wrapper around `poll_table` that is intended to be used with `PollCondVar`. The destructor of `PollCondVar` unconditionally calls `synchronize_rcu` to ensure that the removal of epoll waiters has fully completed before the `wait_queue_head` is destroyed. That said, `synchronize_rcu` is rather expensive and is not needed in all cases: If we have never registered a `poll_table` with the `wait_queue_head`, then we don't need to call `synchronize_rcu`. (And this is a common case in Binder - not all processes use Binder with epoll.) The current implementation does not account for this, but if we find that it is necessary to improve this, a future patch could store a boolean next to the `wait_queue_head` to keep track of whether a `poll_table` has ever been registered. Reviewed-by: Benno Lossin Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Trevor Gross Signed-off-by: Alice Ryhl --- rust/bindings/bindings_helper.h | 1 + rust/kernel/sync.rs | 1 + rust/kernel/sync/poll.rs | 121 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index d7f7ae109e6f..809112fd73d3 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs index 0ab20975a3b5..bae4a5179c72 100644 --- a/rust/kernel/sync.rs +++ b/rust/kernel/sync.rs @@ -11,6 +11,7 @@ mod condvar; pub mod lock; mod locked_by; +pub mod poll; pub use arc::{Arc, ArcBorrow, UniqueArc}; pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult}; diff --git a/rust/kernel/sync/poll.rs b/rust/kernel/sync/poll.rs new file mode 100644 index 000000000000..d5f17153b424 --- /dev/null +++ b/rust/kernel/sync/poll.rs @@ -0,0 +1,121 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Utilities for working with `struct poll_table`. + +use crate::{ + bindings, + fs::File, + prelude::*, + sync::{CondVar, LockClassKey}, + types::Opaque, +}; +use core::ops::Deref; + +/// Creates a [`PollCondVar`] initialiser with the given name and a newly-created lock class. +#[macro_export] +macro_rules! new_poll_condvar { + ($($name:literal)?) => { + $crate::sync::poll::PollCondVar::new( + $crate::optional_name!($($name)?), $crate::static_lock_class!() + ) + }; +} + +/// Wraps the kernel's `struct poll_table`. +/// +/// # Invariants +/// +/// This struct contains a valid `struct poll_table`. +/// +/// For a `struct poll_table` to be valid, its `_qproc` function must follow the safety +/// requirements of `_qproc` functions: +/// +/// * The `_qproc` function is given permission to enqueue a waiter to the provided `poll_table` +/// during the call. Once the waiter is removed and an rcu grace period has passed, it must no +/// longer access the `wait_queue_head`. +#[repr(transparent)] +pub struct PollTable(Opaque); + +impl PollTable { + /// Creates a reference to a [`PollTable`] from a valid pointer. + /// + /// # Safety + /// + /// The caller must ensure that for the duration of 'a, the pointer will point at a valid poll + /// table (as defined in the type invariants). + /// + /// The caller must also ensure that the `poll_table` is only accessed via the returned + /// reference for the duration of 'a. + pub unsafe fn from_ptr<'a>(ptr: *mut bindings::poll_table) -> &'a mut PollTable { + // SAFETY: The safety requirements guarantee the validity of the dereference, while the + // `PollTable` type being transparent makes the cast ok. + unsafe { &mut *ptr.cast() } + } + + fn get_qproc(&self) -> bindings::poll_queue_proc { + let ptr = self.0.get(); + // SAFETY: The `ptr` is valid because it originates from a reference, and the `_qproc` + // field is not modified concurrently with this call since we have an immutable reference. + unsafe { (*ptr)._qproc } + } + + /// Register this [`PollTable`] with the provided [`PollCondVar`], so that it can be notified + /// using the condition variable. + pub fn register_wait(&mut self, file: &File, cv: &PollCondVar) { + if let Some(qproc) = self.get_qproc() { + // SAFETY: The pointers to `file` and `self` need to be valid for the duration of this + // call to `qproc`, which they are because they are references. + // + // The `cv.wait_queue_head` pointer must be valid until an rcu grace period after the + // waiter is removed. The `PollCondVar` is pinned, so before `cv.wait_queue_head` can + // be destroyed, the destructor must run. That destructor first removes all waiters, + // and then waits for an rcu grace period. Therefore, `cv.wait_queue_head` is valid for + // long enough. + unsafe { qproc(file.as_ptr() as _, cv.wait_queue_head.get(), self.0.get()) }; + } + } +} + +/// A wrapper around [`CondVar`] that makes it usable with [`PollTable`]. +/// +/// [`CondVar`]: crate::sync::CondVar +#[pin_data(PinnedDrop)] +pub struct PollCondVar { + #[pin] + inner: CondVar, +} + +impl PollCondVar { + /// Constructs a new condvar initialiser. + pub fn new(name: &'static CStr, key: &'static LockClassKey) -> impl PinInit { + pin_init!(Self { + inner <- CondVar::new(name, key), + }) + } +} + +// Make the `CondVar` methods callable on `PollCondVar`. +impl Deref for PollCondVar { + type Target = CondVar; + + fn deref(&self) -> &CondVar { + &self.inner + } +} + +#[pinned_drop] +impl PinnedDrop for PollCondVar { + fn drop(self: Pin<&mut Self>) { + // Clear anything registered using `register_wait`. + // + // SAFETY: The pointer points at a valid `wait_queue_head`. + unsafe { bindings::__wake_up_pollfree(self.inner.wait_queue_head.get()) }; + + // Wait for epoll items to be properly removed. + // + // SAFETY: Just an FFI call. + unsafe { bindings::synchronize_rcu() }; + } +}