From nobody Sat Jun 13 15:13:23 2026 Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) (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 3F6453B27F1 for ; Wed, 6 May 2026 20:49:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778100573; cv=none; b=N73HdcNM01TCvEVYTyQfaOSUi0wmAuJyOfDObkhMJyR+WfH921F3lVT+M361q7b7S8ivV98mDiFQIZcbbSrx1aaT/Xu7QPjPI5Qc/cGWUXvEmOhsnQ+TN5otbVNoH2NgkYSKXXf2QmAuQq/XuFJ7LBF3Hn8Ku0mYHKpQgVbrJD0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778100573; c=relaxed/simple; bh=99V9//V/GQNqh/x63qJ9youBs3GJq8wBzyRhottcyC4=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=k2yyMW3XV7oVtWUmDJnYOoORLY8x8+LnReKLUZmaJRQZpT0Tq8uH0wYq91lewklbSObUOOQO/gq7/jFMIquKSDkx97UxO9Z7g4kQYMXtz2E6kZE4P919MfCm5bO/l9Eb6KXzgQfB7H/4Pt5/6w+SfNvN5e9/Tyry3ebls9+0h0U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=oX831dYL; arc=none smtp.client-ip=209.85.216.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="oX831dYL" Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-364c5317d67so100408a91.2 for ; Wed, 06 May 2026 13:49:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778100572; x=1778705372; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=TZjYOQ1IR/s8XAAiI8t2bP/5mFaUmDBU9B5ZjAyEcWw=; b=oX831dYLvDoHMVeIf7qku7TeuEIKYcKqBKNCJ4e4eEZ9F+S6+lFUZccdb/1Jk7Cy/k y17jqgqoChpKop1ROEKh5DRaO6xSHSFkWKN6IGTqI+b0QRoSdaQ84tf6gW6sVhkElCG1 4YCR04RjAHTSCIkmmUEdQX190xy6nP6p+czQZzZRgdwv0FKwHo+bEIStFfFTUsNr2awQ S0MmIJxajd2GnoLT9kz/V1tpZ0JaeEt6ZHMPxgb9h0fCZo7y8MEw5zaerKfO6qp6QNf/ /CRiyTt1u2eHpfshD9yDQz4vedHJ/dIC9of0acQwlcV+ThDJxGTFBglaMjKaUqbHr+TM bgzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778100572; x=1778705372; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=TZjYOQ1IR/s8XAAiI8t2bP/5mFaUmDBU9B5ZjAyEcWw=; b=iRVcUzYVhCP9S1uWhXGK/2zzLiZYpci7IRUm7lOelXuhLnl7FUrv1uDYjdak8A1mts 80CvTk2eeGi3X5jDOKyroZrSvzyQjt8N011C7qFpczV2HqksEiDh2mqzACikXUjBHHkO roPtK7mbU6xU9KXdT3FSOOQ5KH+sjfd/3KbU+4lrFe/4iUYYO2Z1w3r7nqTY9yDWpoB2 UKhamfJrmDoaKGF/P1fmoSCkNuAZC/hq6qbVQpVgdSXc0Yf8z7zn1O4JYWIJLtViR1z0 cocqIX5BhQP1atGQO20b9pXDY4PeWHPmV1zAm2Rw06lyYDC2bKQCFTxQ1up1HvFaAofE 8D8A== X-Forwarded-Encrypted: i=1; AFNElJ8jexr2U/QFvOWBSubq8a4sSZPv0ziAfTEzumqHSTazsQZLovtg0LrSMRxOdX9RjWzOQxjivXQmEjOA0AY=@vger.kernel.org X-Gm-Message-State: AOJu0Yx9yFm8+sc0aUjr7YGYdu86Ny0irdP0yiUjwv6U2vA6jV91XSrO 07OdXE43XHvCwOSJsWnHa5mPiPZDdtgA5VuJNZMy0x1WC36HOu6blz/9 X-Gm-Gg: AeBDiesBrAK7H18WhFegMpT9Qd4ihRsaQSinb82JQOrAH4ohzhORP04qDkAgcDUFkTk x0AEE/7nuMtx5773j4xiY7vrIl9dhGkVMaIY3C5Vz97zyOvmFi4/WNU9R+IXwCR+S+zaGzwZff/ Tgf90e+kuCo4MWbQJnpkmuEYroaNdi3VcAsp5nKCm8bsnS0EVODm7cL4cI+X28tNwzuY24kGW16 r+rBqHdBkd957GTiZQE6v0uJ9c2OMrk0foTI9lum6DgRBHfl9Esyi2eikie973ezubxCnLywu7z POUu/RBCYfIsgKLOWO+uvPtg53VCrIFrWEymZOUiJhR/8upFpeEyPgH3Htfqpd1N0CqGklRRDkO H+qOtSW0APaNnf+Ws1yI715XWVJrR8IDNd/KlQr6C06P66dPFBmr9QpuNQ4F1yz/1b6w33fo3EW TCMpVZVsWvhcDhP85Sn22FwdCA6sa837YCNO5w7MjSHGar7ADv1Jo= X-Received: by 2002:a17:90a:d886:b0:35b:8d89:719b with SMTP id 98e67ed59e1d1-365ab3e592amr5283506a91.1.1778100571511; Wed, 06 May 2026 13:49:31 -0700 (PDT) Received: from crewmobile-Dell-G15-5520.. ([2409:40d7:e:c40c:b89c:2d1:e053:9c37]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-365b10d3e04sm1585128a91.9.2026.05.06.13.49.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 May 2026 13:49:31 -0700 (PDT) From: Arnav Sharma To: ojeda@kernel.org, paul@paul-moore.com Cc: Arnav Sharma , Serge Hallyn , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , linux-security-module@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] rust: cred: add safe abstractions for capable() and ns_capable() Date: Thu, 7 May 2026 02:19:13 +0530 Message-ID: <20260506204913.26022-1-arnav4324@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The capable() function is the primary privilege gate in the Linux kernel, used to check if the current task possesses a specific POSIX capability. While bindings for capable() and ns_capable() exist, there are currently no safe Rust abstractions for them. Introduce safe Rust wrappers for capable() and ns_capable() in the kernel::cred module. These functions validate that the requested capability is within the valid [0, CAP_LAST_CAP] bounds before calling into the C side, ensuring that safe Rust code cannot inadvertently trigger a kernel BUG() on invalid inputs. The abstractions take a `u32` parameter to ergonomically match the generated `bindings::CAP_*` constants without requiring explicit caller casts. Signed-off-by: Arnav Sharma --- rust/kernel/cred.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/rust/kernel/cred.rs b/rust/kernel/cred.rs index ffa156b9df37..6525b52b81ae 100644 --- a/rust/kernel/cred.rs +++ b/rust/kernel/cred.rs @@ -90,3 +90,82 @@ unsafe fn dec_ref(obj: core::ptr::NonNull) { unsafe { bindings::put_cred(obj.cast().as_ptr()) }; } } + +/// Checks whether the current task has the given capability in the init u= ser namespace. +/// +/// This function tests whether the current task has the specified POSIX c= apability available for +/// use. The check is performed against the initial user namespace (`init_= user_ns`). +/// +/// When the check succeeds, the kernel sets the `PF_SUPERPRIV` flag on th= e current task. This +/// marks the task as having used superuser privileges, which is visible i= n process accounting +/// and auditing. +/// +/// The capability constants are available as `bindings::CAP_*` values (fo= r example, +/// [`bindings::CAP_NET_ADMIN`], [`bindings::CAP_SYS_ADMIN`]). These const= ants are defined in +/// `include/uapi/linux/capability.h`. +/// +/// This function must be called from task (process) context only. Calling= it from a context where +/// there is no valid `current` task (such as hard interrupt context) is n= ot permitted. +/// +/// # Preconditions +/// +/// `cap` must be a valid capability constant in the range `[0, CAP_LAST_C= AP]`. +/// Passing a value outside this range is a programming error and will tri= gger +/// a kernel `BUG()`. +/// +/// C header: [`include/linux/capability.h`](srctree/include/linux/capabil= ity.h) +/// +/// # Examples +/// +/// ``` +/// use kernel::bindings; +/// use kernel::cred::capable; +/// +/// if !capable(bindings::CAP_SYS_ADMIN) { +/// return Err(EPERM); +/// } +/// # Ok::<(), Error>(()) +/// ``` +#[inline] +pub fn capable(cap: u32) -> bool { + // SAFETY: `capable()` is safe to call from task context. It checks `c= urrent_cred()` against + // the init user namespace and returns whether the specified capabilit= y is granted. + unsafe { bindings::capable(cap as i32) } +} + +/// Checks whether the current task has the given capability in the specif= ied user namespace. +/// +/// This is the namespace-aware variant of [`capable`]. It tests whether t= he current task has the +/// specified capability in the given user namespace, rather than in the i= nit user namespace. +/// +/// This function is relevant for code that must respect user namespace bo= undaries (for example, +/// operations inside unprivileged containers). For most driver code that = is not namespace-aware, +/// [`capable`] is the correct function to use instead. +/// +/// Like [`capable`], this function sets `PF_SUPERPRIV` on the current tas= k when the check +/// succeeds, and it must be called from task context only. +/// +/// # Preconditions +/// +/// `cap` must be a valid capability constant in the range `[0, CAP_LAST_C= AP]`. +/// Passing a value outside this range is a programming error and will tri= gger +/// a kernel `BUG()`. +/// +/// C header: [`include/linux/capability.h`](srctree/include/linux/capabil= ity.h) +/// +/// # Safety +/// +/// The caller must ensure that: +/// +/// - `ns` is a non-null pointer to a fully initialized `struct user_names= pace`. +/// - The `user_namespace` pointed to by `ns` remains valid and is not fre= ed for +/// the duration of this call. +#[inline] +pub unsafe fn ns_capable(ns: *mut bindings::user_namespace, cap: u32) -> b= ool { + // SAFETY: The caller guarantees that `ns` is a non-null, valid pointe= r to a fully initialized + // `struct user_namespace` that remains valid for the duration of this= call. + // `ns_capable()` checks `current_cred()` against the provided namespa= ce and returns whether + // the specified capability is granted. + unsafe { bindings::ns_capable(ns, cap as i32) } +} + --=20 2.43.0