From nobody Sun Feb 8 09:11:53 2026 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.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 6403B253F23 for ; Mon, 5 May 2025 12:17:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746447469; cv=none; b=Hd4TE/GdBREBITqMDo6iuJRU5Z/JGaSX0qlWiqUUemRjTWvBByADgdLj0ztZlnhb4fjKFTVJE9VmwnjVbR/rXo8QAWWEpnFJb9uY2dVZsJZRwWwGZ8LP4rj8IXdGZzGaZYeOhdjDUhG1lJjoJWrmwSVVEiqaHYUu/00+acm8R+o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746447469; c=relaxed/simple; bh=8u/2C1HjCG7Oo6U0j0F+topZF+8Fd+oSzLpsbfJQZO0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SupXikCAKVUetWtBdBr0k4HLZcqMJpNoUsdQVclt4DHhdZC1j2jyy9wPFmlZpEkm9z7gybyL9f8MKYW9ydk62qZ5D4LgN3bAEkmX9yxeG1vGtURV/gAcVyrSztBsNhQ5xLhYHIG1uviROURHLK3YsGjjR+9g8JlH7hHQmpORFVY= 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=GQwo4koI; arc=none smtp.client-ip=209.85.128.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GQwo4koI" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43d0830c3f7so31459175e9.2 for ; Mon, 05 May 2025 05:17:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746447466; x=1747052266; 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=7fVelJcRG2V/ngzKa3XTWCRHf2jEKMazBViPO8hcSl0=; b=GQwo4koIkNAYI5IEgZPHXyi3dToO4CgvOfXMByjRmXlTA70TppchkLSuMIlhwfeZDS U4GcZKnKSRb9TcTzEbIwivzNOhYm/QEXKklqvM8XPveahCd9AggFSMZ8TT1bYS/7kaFr cKZ+uL/nXjU6zp9EThzh2r/1kHVxCt2MT6btjUvVBZAtwd/dzXIqNJJaMPIjyAnmdp/5 HELTZfmHamRJPOodxfTIN7g/0OY2NhmkKrzOd4zu+OTgbgZDB3yEOhAG80VGSNPlpR66 KASRODhaXalLWHm6Pf1ljgnYj5FCf6FbSW9Pabc6RPB5pRq75ZoPpAt2K3nrmtN9OxJX vikw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746447466; x=1747052266; 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=7fVelJcRG2V/ngzKa3XTWCRHf2jEKMazBViPO8hcSl0=; b=ZevRB5t4u/yDhRLoOthuPWgcchqnoE/nWsaZw9xuw3z3egxNg/YUdjBr+SnRZ8YxUM q6LperDscX5CQpeWhQpspMmUrjQ3CL/KGULjK5rwiMBNLEdSrwpB9qX9WRUqsP021ndO MkJMd5jOmNKmc5zY57hCF+8iasHPXUuiVI2fkUM87wMteytYbqPxe7HzMIc5r2nKoCoM Tnm2eutlbWjl4yC+HcFRs5OJ7fbLSG0iNasqhZexZSbRK3Q+zk2Y21VT+TEz0emW+x/W nwo2wHfJGcghvAstiAtwPStg0fgEcf0BhIU1KQD1f/iiO1Mxj3XZSzdQHRoAqHXJCtcg d0jQ== X-Forwarded-Encrypted: i=1; AJvYcCXQ+16MOty1jAcgUq2qgA0E7uqKFQcItqRvMbv17m9M9kWV3rSVI2AOg4YNTpbqg1Jyz7DZS6+oyNjfEI0=@vger.kernel.org X-Gm-Message-State: AOJu0YyHMRFKEMHdJk89/jpAdO4sXBbMFdoa++VUNkLtXs1a6ZsZSG4L dQwwJxcIZ8rmf1UxYgiMbwvcKZAmegV5PK4Q1C0VKGdw7i++Noht5/H3EMN3QjbmboytpGF1Y1F 5J066VoDsgWmgIw== X-Google-Smtp-Source: AGHT+IGkvf05eY5B/6AaWdydV0XrkJVTEQQFjoaEGwZ6A/Xamp8EAUBgQbx3YAm3ZvJ8xf7GFwZL7TXQHi43U04= X-Received: from wmbji5.prod.google.com ([2002:a05:600c:a345:b0:440:595d:fba9]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4e88:b0:441:b19c:96fe with SMTP id 5b1f17b1804b1-441c48bdfe5mr60583825e9.10.1746447465827; Mon, 05 May 2025 05:17:45 -0700 (PDT) Date: Mon, 05 May 2025 12:17:31 +0000 In-Reply-To: <20250505-strncpy-from-user-v3-0-85c677fd4f91@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250505-strncpy-from-user-v3-0-85c677fd4f91@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2737; i=aliceryhl@google.com; h=from:subject:message-id; bh=8u/2C1HjCG7Oo6U0j0F+topZF+8Fd+oSzLpsbfJQZO0=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBoGKxlFgZEs3Oh+QBj4OXOMyTEaavUaL+fS+to3 MfE0g0Bs1WJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaBisZQAKCRAEWL7uWMY5 RtTyEACdu6fQS60hMvPeUzEyFGVNQmJPug3scwpiOn+SxBRJ2zB8bTrLxZ2Y3OAIlf0j123nHJr KMT3/LYn5zH73ckH6NLT0jLQ9eU3cgQuUA0p3gkRh+Uvp2X2dywWyrWu5RdEqEfJRvTN/gRr3HR G835GMqGJ5ANPqx1DKVSdoawQZ626vbI+PKeILtKprF5/2ZAkKpX5+xCDIq3IsiWW89Sq8xWtbH Z3cqr9M+AWHidqRs+sPAqmsy40RwjPatK7tSyhu4hM1FlWEMQZ3ogFYNWyZVvI0dQnBocmuIzsY vTabftRpKGVJrOXALoSejEUf3LRlaLVGSGJpydNJbnHKtdMXK1JFmhPOzG7KgBw1ag2HNdwRlq7 ri+gsTt2mmODzfFklmcTWWfuVPzdK/nBViT1H+GYk4RDysrELnORXIaAutueyWK3PEso39Emgil TmlnDYWuYGq2fbBwFawOP3cql/59UerkFNtv49Q9EgLsU5RfzxMmE3qlwkS3mqK1cjhFo5soVid R3PmWBNy4P/naPTTUHDHm+jmve773OgAQcLIRpRYS+FBpnMADhfYBCUKKcvQLgLWiTPh7euOMUk 02QI/UyjhuQSGJRz5VHma7v3L6hHJlksHMRXTjRGYnFOjcD20JqLBlSOFptJRynFsbxGtiJTbnZ pbXr6bR7GSZ+chg== X-Mailer: b4 0.14.2 Message-ID: <20250505-strncpy-from-user-v3-1-85c677fd4f91@google.com> Subject: [PATCH v3 1/2] uaccess: rust: add strncpy_from_user From: Alice Ryhl To: Miguel Ojeda , Andrew Morton , Alexander Viro , Greg Kroah-Hartman Cc: Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This patch adds a direct wrapper around the C function of the same name. It's not really intended for direct use by Rust code since strncpy_from_user has a somewhat unfortunate API where it only nul-terminates the buffer if there's space for the nul-terminator. This means that a direct Rust wrapper around it could not return a &CStr since the buffer may not be a cstring. However, we still add the method to build more convenient APIs on top of it, which will happen in subsequent patches. Reviewed-by: Danilo Krummrich Reviewed-by: Greg Kroah-Hartman Reviewed-by: Boqun Feng Signed-off-by: Alice Ryhl --- rust/kernel/uaccess.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs index 80a9782b1c6e98ed6eae308ade8551afa7adc188..a7b123915e9aa2329f376d67cad= 93e2fc17ae017 100644 --- a/rust/kernel/uaccess.rs +++ b/rust/kernel/uaccess.rs @@ -8,7 +8,7 @@ alloc::{Allocator, Flags}, bindings, error::Result, - ffi::c_void, + ffi::{c_char, c_void}, prelude::*, transmute::{AsBytes, FromBytes}, }; @@ -369,3 +369,36 @@ pub fn write(&mut self, value: &T) -> Resu= lt { Ok(()) } } + +/// Reads a nul-terminated string into `buf` and returns the length. +/// +/// This reads from userspace until a NUL byte is encountered, or until `b= uf.len()` bytes have been +/// read. Fails with [`EFAULT`] if a read happens on a bad address (some d= ata may have been +/// copied). When the end of the buffer is encountered, no NUL byte is add= ed, so the string is +/// *not* guaranteed to be NUL-terminated when `Ok(buf.len())` is returned. +/// +/// # Guarantees +/// +/// When this function returns `Ok(len)`, it is guaranteed that the first = `len` of `buf` bytes are +/// initialized and non-zero. Furthermore, if `len < buf.len()`, then `buf= [len]` is a NUL byte. +/// Unsafe code may rely on these guarantees. +#[inline] +#[expect(dead_code)] +fn raw_strncpy_from_user(ptr: UserPtr, buf: &mut [MaybeUninit]) -> Res= ult { + // CAST: Slice lengths are guaranteed to be `<=3D isize::MAX`. + let len =3D buf.len() as isize; + + // SAFETY: `buf` is valid for writing `buf.len()` bytes. + let res =3D unsafe { + bindings::strncpy_from_user(buf.as_mut_ptr().cast::(), ptr= as *const c_char, len) + }; + + if res < 0 { + return Err(Error::from_errno(res as i32)); + } + + #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)] + assert!(res <=3D len); + + Ok(res as usize) +} --=20 2.49.0.967.g6a0df3ecc3-goog From nobody Sun Feb 8 09:11:53 2026 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.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 816E12550B0 for ; Mon, 5 May 2025 12:17:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746447471; cv=none; b=p1yvQyP5iZQV4ht7nHi7zdtBfXKTpFcE4L2T85k/ODgvMVL/fRieemx6FDRFqIvnivFpOmw2LTiBEQ4RUpPpkdRNdP4FK3Ihq9xrLmN1dgdpEbFNNo8PbaOPjm9MHh6M/G2G/XwAyLvOtkrWVIwANhRSdPrdavWSxBkWIWzefZY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746447471; c=relaxed/simple; bh=lA7Gkh9jGL4mrigHzmXfNszvqrkSyoDnALirNgDesXs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HPNPUsyZ4ytKNTJJPKjQCjACf9XmmKik1LS1l+yFPn0MJQS8iztRNHKs49ytsWMnS5AaHONr8suzOXyazbdv+fYkz5gJFlfjpqL1GzcGEb8WRIePU5H4HyBDZH+FxlBR5VqzBPMBaAn+QtFqPuwmTcI4MKjgYFuVGtjspKWCxsM= 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=cT3XxS5X; arc=none smtp.client-ip=209.85.128.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cT3XxS5X" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43e9a3d2977so31250955e9.1 for ; Mon, 05 May 2025 05:17:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746447468; x=1747052268; 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=aI7ws8iZPwHTtGYkxfXZJ4wq0gS6ufEYiqCRndIOAfc=; b=cT3XxS5XxL72p5/9RJ2CipVe7hu3mF7ilDu/ZFJA9NXW1/4MFGEJY9C3aybipnCXHu NXgCGLnEvp4HqnEDOBLui4tKdzCFG26wSA3npQe/CZoJ12xRqcoBbmaQtazFSSpNNUpA ls6FEBFNwlKUkgBIXhq26UY7hmDk8tPEItQIisj65gDvs8I5tdtsdqD2V/mHG1qEKppz zcTB1TaI9dLkRT2wnQKHGsZu2qiNSZGRM7dWYSll6KSVpd4nLsjTXuqAiGL+VeXlt1hh ZqiFs+fWgK7lduY8fHXz3olf5DFJK1yObKNwzIdUtyM1cBRjI42OBfDGb8d96oBnk3l7 8qWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746447468; x=1747052268; 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=aI7ws8iZPwHTtGYkxfXZJ4wq0gS6ufEYiqCRndIOAfc=; b=Em8RRLJoZGMDsYvTMD7n/5Bb1My27UMNhWWzHZLvI7zwEGnsd23ZlQ2+EfwJkgy9d7 FkrWrDqpmHRoLxeUaYvFDHc1vz5vd+QubVb0X4KowYHxVGlNpTlxeqP/Mgw1Qru817H/ +qqWfArHst/i4W3PWS9DbZsyqdOM3ubCsLu08otdf4wsHrjlIaWD1nmEhJSjnTj2Cy7J 0AfLlDnJldNcxZHgOJWWx9Hfg7tjsc+rWzBbD4vKsC5fOwZrLcwxj9L/3SV34feCdq4Y AIkFMmfR100BbjxokejXvHe3rZd5U7siyVCj2q9T26xFs1ILYbdKpR0cvFU7ggTlMGCG 1YMQ== X-Forwarded-Encrypted: i=1; AJvYcCX32S14OZK4YjT8MBCWdDDBgiYbGxA4/QTiIuyefBiZOduQ5tw6nAhLFNBNq//3IUWx+6x9dDKhgUYTzlM=@vger.kernel.org X-Gm-Message-State: AOJu0YyTeJp/y6BLfAQ/+IZXUkNR3p+sp3bVXs+DCCjNkSHiXOMVUAx6 m5CBFRYxnZy5FF00o4q5mTP3Ujp31X/v9bj5E9lRen3OQ+/eyot+olTtQKrBZMDs/+ypByqRp0/ iTBWheopHpAQL8g== X-Google-Smtp-Source: AGHT+IGRlviQWDMm0JA9tB+a5FlOvz3A/Z4hQKhAVSVd7A0c+V+17SbTcKuOwq2RUwtBR0ovkffnvqcRY7oC5LM= X-Received: from wmbjb17.prod.google.com ([2002:a05:600c:54f1:b0:441:bf4e:899c]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:b96:b0:43c:f64c:447f with SMTP id 5b1f17b1804b1-441c49483c0mr51904745e9.29.1746447467965; Mon, 05 May 2025 05:17:47 -0700 (PDT) Date: Mon, 05 May 2025 12:17:32 +0000 In-Reply-To: <20250505-strncpy-from-user-v3-0-85c677fd4f91@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250505-strncpy-from-user-v3-0-85c677fd4f91@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4080; i=aliceryhl@google.com; h=from:subject:message-id; bh=lA7Gkh9jGL4mrigHzmXfNszvqrkSyoDnALirNgDesXs=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBoGKxl2bSoTcsJCcnQIyuizzBKGVs20krozLvxf msQUXTjH0WJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaBisZQAKCRAEWL7uWMY5 RndXD/oChQWpR+z9zVjQaGvOxyzaRIeKJU038RRe/zOMeiMEDMezfjn4DLPiI3wz0RlKzJFvbCN FuPB+HB5GpkD94rp87Q0OOleKxkMtJHhD7IRC1Zdu3BoAUfTQc1nQDuOk84tFt7AOm0arDS3v+X efm3d62o4rNhBrp3ejEtc2UOLUDRyEFeTB7hhplhh80vhDYWP5kNksV61vUchG7k244PtsBJkUs RF7ryElwzZUrNfP1T4mpFgX64+rWLuSvYwVSnKmoL5+Iruk1QSVYdA3W6I7nKnQXXhIjT1VgGvO clQKmJqIs+19jvx8n+QCtEZfhKFAatQO2p0YNd8467Wx9iG754qmMxztHS9C1jfu0D3wYx/j9bj ME4FK8k5dDI13kKFl972C8+kxbxVj27A2UrV16jQoM7ZU2uaC0H8UvH2bzMNKfgxBzyUl8heSPm eOlemNhRWs0lw4kFBHPyLe4OaWVFEt345eBxdMDK9NmDf1jpTOGe0BtQfPhpdGIFDoPBD1fokX6 yfHORGu8rkDlEUMpA4Ix67XPK480KLqba4fJHgOkVW4jk40G2y1q821uKm5vubW7ktWKI2mzuVr 84jmLSMjSgopzqAB79nfewtQei2rGfbBw9HBB8+ZDnVeOAIAKcj6PtwfShdEKBSmMxodAPbBgOF k42O/qnrvf0ZUPg== X-Mailer: b4 0.14.2 Message-ID: <20250505-strncpy-from-user-v3-2-85c677fd4f91@google.com> Subject: [PATCH v3 2/2] uaccess: rust: add UserSliceReader::strcpy_into_buf From: Alice Ryhl To: Miguel Ojeda , Andrew Morton , Alexander Viro , Greg Kroah-Hartman Cc: Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This patch adds a more convenient method for reading C strings from userspace. Logic is added to NUL-terminate the buffer when necessary so that a &CStr can be returned. Note that we treat attempts to read past `self.length` as a fault, so this returns EFAULT if that limit is exceeded before `buf.len()` is reached. Signed-off-by: Alice Ryhl --- rust/kernel/uaccess.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs index a7b123915e9aa2329f376d67cad93e2fc17ae017..978205289d297a4001a51fa40ac= 29039bff73672 100644 --- a/rust/kernel/uaccess.rs +++ b/rust/kernel/uaccess.rs @@ -293,6 +293,58 @@ pub fn read_all(mut self, buf: &mut Vec<= u8, A>, flags: Flags) -> R unsafe { buf.set_len(buf.len() + len) }; Ok(()) } + + /// Read a NUL-terminated string from userspace and return it. + /// + /// The string is read into `buf` and a NUL-terminator is added if the= end of `buf` is reached. + /// Since there must be space to add a NUL-terminator, the buffer must= not be empty. The + /// returned `&CStr` points into `buf`. + /// + /// Fails with [`EFAULT`] if the read happens on a bad address (some d= ata may have been + /// copied). + #[doc(alias =3D "strncpy_from_user")] + pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'bu= f CStr> { + if buf.is_empty() { + return Err(EINVAL); + } + + // SAFETY: The types are compatible and `strncpy_from_user` doesn'= t write uninitialized + // bytes to `buf`. + let mut dst =3D unsafe { &mut *(buf as *mut [u8] as *mut [MaybeUni= nit]) }; + + // We never read more than `self.length` bytes. + if dst.len() > self.length { + dst =3D &mut dst[..self.length]; + } + + let mut len =3D raw_strncpy_from_user(self.ptr, dst)?; + if len < dst.len() { + // Add one to include the NUL-terminator. + len +=3D 1; + } else if len < buf.len() { + // This implies that len =3D=3D dst.len() < buf.len(). + // + // This means that we could not fill the entire buffer, but we= had to stop reading + // because we hit the `self.length` limit of this `UserSliceRe= ader`. Since we did not + // fill the buffer, we treat this case as if we tried to read = past the `self.length` + // limit and received a page fault, which is consistent with o= ther `UserSliceReader` + // methods that also return page faults when you exceed `self.= length`. + return Err(EFAULT); + } else { + // This implies that len =3D=3D buf.len(). + // + // This means that we filled the buffer exactly. In this case,= we add a NUL-terminator + // and return it. Unlike the `len < dst.len()` branch, don't m= odify `len` because it + // already represents the length including the NUL-terminator. + // + // SAFETY: Due to the check at the beginning, the buffer is no= t empty. + unsafe { *buf.last_mut().unwrap_unchecked() =3D 0 }; + } + + // SAFETY: `raw_strncpy_from_user` guarantees that this range of b= ytes represents a + // NUL-terminated string with the only NUL byte being at the end. + Ok(unsafe { CStr::from_bytes_with_nul_unchecked(&buf[..len]) }) + } } =20 /// A writer for [`UserSlice`]. @@ -383,7 +435,6 @@ pub fn write(&mut self, value: &T) -> Resul= t { /// initialized and non-zero. Furthermore, if `len < buf.len()`, then `buf= [len]` is a NUL byte. /// Unsafe code may rely on these guarantees. #[inline] -#[expect(dead_code)] fn raw_strncpy_from_user(ptr: UserPtr, buf: &mut [MaybeUninit]) -> Res= ult { // CAST: Slice lengths are guaranteed to be `<=3D isize::MAX`. let len =3D buf.len() as isize; --=20 2.49.0.967.g6a0df3ecc3-goog