Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp819972lqo; Fri, 17 May 2024 02:33:03 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWjH8uHgLEUmI+TfqCsE5gd5H8FJbEfH49FIS6dyDnBy8oU7tY/JG+Q73zimAPvwPjTM+or7COVpiS3nqpzyRuPTBjsQmyfYC2YYMi4sw== X-Google-Smtp-Source: AGHT+IGGOBgz25TIEgKh+ztPXGGyRSXCW1eRcdMU0GxMVFjGhu2l1d9b0w3A+4pTtZ4WQDeCxl3g X-Received: by 2002:a17:906:594d:b0:a59:bb1f:6ffc with SMTP id a640c23a62f3a-a5a2d54c13fmr1360376166b.8.1715938383338; Fri, 17 May 2024 02:33:03 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715938383; cv=pass; d=google.com; s=arc-20160816; b=huaezpMHJG9scOxZv+C6gxSdwBfGB1S1lCZ3qc/wXWihnujsznmOd7aNCtc/cASg04 3/wzImpljR0lJe1mPpoaC0uKJ6PoOZ0qpVCgtmUMnpLsmUaZc6eypBEa6c7B56gxSkfH FsvznUyoMzX5s4JEpN6sRoAMJQk8/CM9vHpysvG3SuxAx8s8rx6xA9pU5f1gAjVvzfiT 4jUl5qJ1T3B10RxjsehmD31Q6L7yXiwmOk2c8oP8msDKgS5YFsS8BuKLQLGMeJinD+sY e0+gtFDSGPkHRwwyCQRUSSqUxLaJAMF+3vr6tXuMjgzcvKuAcIwNf+RYyAS2ghpSqif/ kacQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=OeUEDmczYkvZCSBa1m0ZBb+fLzryf361jdn5sgXxhlw=; fh=o2R9PeRhA/4Bhl0Iyk4YtP0Th6v0dW6HKVXMtBG7UvI=; b=lrfuUieQ8OkPgIi+BfvTkEimrYPAPy1gL3KBYLXvuA6uGaFZSugH8eDzJNBcKJr4B4 xUoSBfGBXmQtAxFll2GOiuIIJByNzBIPx+6+f8WnMGTMTkWTQnQoSwhtykGBvoAWq8Eu NUl5sALKyOv4sPVfDkB6fSeGuUiK96avMAVeu46JZnx1ImlMrkSDEPlTkqkZg5Mj03dA 9SX8B0sIEGTQbyHCTxiGahEnGZRGTJ0BtVcpWBowMkHzmlOR9YDrgNmUB6NFU34j5uZM pjK5XsHTmMe8o0cf4XBH6O154nsNXgH6GFnAaU5VYPvWdGhC3ACNRhajgh2Oyq2H2bAC GUCg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=4mbyBZhH; arc=pass (i=1 spf=pass spfdomain=flex--aliceryhl.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-181936-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-181936-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a1797d9easi924351466b.268.2024.05.17.02.33.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 May 2024 02:33:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-181936-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=4mbyBZhH; arc=pass (i=1 spf=pass spfdomain=flex--aliceryhl.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-181936-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-181936-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id DE6EA1F22323 for ; Fri, 17 May 2024 09:33:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 849553FE58; Fri, 17 May 2024 09:31:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4mbyBZhH" Received: from mail-lf1-f73.google.com (mail-lf1-f73.google.com [209.85.167.73]) (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 771833D969 for ; Fri, 17 May 2024 09:31:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715938303; cv=none; b=WxrNGt71cZpNDA9/UQVCxrRz3HpDHwEQmJz2e7oYlZohajoHM0KbOz3QWYT66Mmg+4QfhkG7hXnOi6lnAqN4jrwqyQ8sAG2cZe4Qz+ypxP/AwZ1grz5NH9ccaow5YzNonliECEqzEGOC8H9z3dbNMvuo4733wBqrj0K/h15vxBc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715938303; c=relaxed/simple; bh=O7eW32RK5FMclee3Nlw489v5va/erWNbHRKjhVU+QdI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=AqDZ32z+cKzBPqP5xOnkIDYx7/1MYLlvV3l87e+4vVbLvi01a4FmY0et3Yh7/Y3aZtrZ8iIFKBc0JM/VichNsKRZJsWtQh1A/4O8IMcOQSTAYRN5l/wAvgUuLn3EVtI+N/5QHcduixgpRsZgWpHBPD5m+pear5pw4qx+QKM+rbs= 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=4mbyBZhH; arc=none smtp.client-ip=209.85.167.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Received: by mail-lf1-f73.google.com with SMTP id 2adb3069b0e04-5220830a9c7so7315180e87.3 for ; Fri, 17 May 2024 02:31:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1715938300; x=1716543100; 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=OeUEDmczYkvZCSBa1m0ZBb+fLzryf361jdn5sgXxhlw=; b=4mbyBZhH+STVaDSg7xmgZUGnPflpBXSPE3v8WxKMIpxsGrveazkhGfaXFkVbGwmdBK Fm9uUsAAD5zRVgUt7O1sBxU2lBloA5kKqm2xw0zL2yjpy22cHBrHFi4KN/LLYzk8fMYS mTn6aiCyImdLxL4u5iqmq/vmSCm6Xma3KcuL0hLlv3nSCFes9Em2xXzau8ot8W7KNqnu ehP/mohu7zC3gL8ApmCkLwC4fUPZwJrY6x5Tg587IPIpaYXQdjHIPvahq/XEwQy3AvfO UBvTHJy6HsW7gdf59Oll/dvVe6l77RNd9HcOycHNOjCB3lpK68zHmKZS++mZh5zwsF6K /dLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715938300; x=1716543100; 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=OeUEDmczYkvZCSBa1m0ZBb+fLzryf361jdn5sgXxhlw=; b=O6Gbn2cdYI0E8m3WWyGWHPnEFsZf/N1Ya6Wz798yKS5z0p5AJ6csPtpIGoEM71L5oO v1HpuPSt92+88uPiPjt7mHBv4/cUpAXxu1zfTwVXp9tjeg++Zj6l4tj2cfkrJPd3IypU 9kuD6hqxcHeiq4i9bwNEPK32C0JFEtR6tmqECAli5Lhm6ztrmZHQp50c/vwSfsaHGg4Q glYBW8yDAcqKRsLTYN8vqsqY/ZKxC/IFcLHU75rRelA5IUqM2KW/CmW4EBQB+e+/NwSE aqmj4FvLc2/Xi8pQinf5wVZq3YQ/XZw1ZaDCeStthhtyHLCT6JN10LMgaSxP9uh72fK/ SlAA== X-Forwarded-Encrypted: i=1; AJvYcCXRxHT2rJ3dU+Lzvoy85eHDMwrhanieJiOZaVjR74LJdYzADKsl/4vF41AG8SEo8iJhpxFSMIzb5tDWH1VrrXzV2J2h/ph1poZvQaeZ X-Gm-Message-State: AOJu0YxgkLYbcpzkyBWMz5oz/eGzgX+7wtOhC2NcydykfWCc7nvCZunT 693wfCiCjt/m2nhuFpatkMuwmaLvkqU905x72pHBolHR04Kez+bll53VHncty/9WPQJTPxRyhrV xg/HXDeJ0XkCuzQ== X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6512:39d3:b0:519:2c84:2409 with SMTP id 2adb3069b0e04-52210069ba4mr24256e87.4.1715938299676; Fri, 17 May 2024 02:31:39 -0700 (PDT) Date: Fri, 17 May 2024 09:30:38 +0000 In-Reply-To: <20240517-alice-file-v6-0-b25bafdc9b97@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240517-alice-file-v6-0-b25bafdc9b97@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=6451; i=aliceryhl@google.com; h=from:subject:message-id; bh=O7eW32RK5FMclee3Nlw489v5va/erWNbHRKjhVU+QdI=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmRyPmBSHV+7HBh3J44fzcR8jOrXqHMBve2i4aU ehVsJvHdTGJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZkcj5gAKCRAEWL7uWMY5 RhcxEACq9InTBY73sPRRkpN/BM8RjQ/Gk4KPUb8pm2iehVHo/f82Z5jC4In4BUpuL3UqISOBljc I8Pyh9zmOkOUioFUW7z9kMiEG9yBFZrgPvIqstRdDhRZJ1ttpOYVcioZZ2xkDkXlgFX0I6DRnKQ nfwYW9GCfA3MBLEmtTZCckcjIxXZJuviII9dlwhyWNERMJHVwscuZiEr6L8WUFlb4Irn9bYpFC6 175paKUOL6SDm1xCIzLeLhkBWOAh4OiGXP83JLpmlO85mFV0jvEtwOtzHBWvqstC1IbRxjfWxO5 3h3J8G1sQuriW8xmkHlkrfrfgVwXOuXWdWhX4n29fDwxWqXxRWXM3dr+Tkj5+JLNRkeYxfIqH81 DGSTMxi4rKLCL1b70gCpXSWH8/FFK5/aHp55of+ltnabLBPUTjBLm+647EmhYONm+UJg7fHynOj GmkQvC1NiWLmXjRkyEc67wrfWTPlE3Ujd39u+qpoi9SUQ9nhA0gxENKivBhYO1QuGvqs75gaQLk Mq8tMpUQhQX2m1LJOGC3uuXVlyu8au73kA7cmk8MKLV8xrRUaZF9ZrZ+8AChAD652M6LnPmtyp5 6vg4UxOkvlBcK7KKDWezS+rCOgm5irziElLK+OCS8/BjBqimUp/k1wBH3Vu/BG8JFCbJEnSt6R1 zkO3qEhaDTCDN6Q== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240517-alice-file-v6-5-b25bafdc9b97@google.com> Subject: [PATCH v6 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=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan Cc: Dan Williams , Kees Cook , Matthew Wilcox , Thomas Gleixner , Daniel Xu , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl , Martin Rodriguez Reboredo , Trevor Gross Content-Type: text/plain; charset="utf-8" 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 --- rust/bindings/bindings_helper.h | 1 + rust/helpers.c | 21 ++++++++++++ rust/kernel/cred.rs | 8 +++++ rust/kernel/lib.rs | 1 + rust/kernel/security.rs | 72 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index 94091cb337e9..cd2aaaaf9214 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 56415bce8af0..766e368bd0d8 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -177,6 +178,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 360d6fdbe5e7..fdd899040098 100644 --- a/rust/kernel/cred.rs +++ b/rust/kernel/cred.rs @@ -50,6 +50,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 cc629a74137f..ade5889c76b4 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -39,6 +39,7 @@ pub mod net; 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..ee2ef0385bae --- /dev/null +++ b/rust/kernel/security.rs @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! 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) }; + } +} -- 2.45.0.rc1.225.g2a3ae87e7f-goog