From nobody Fri Dec 19 22:01:17 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 E80C5269CF6 for ; Tue, 27 May 2025 12:34:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748349280; cv=none; b=EjUdz+jjUSQ2wof0/pDB/DcX5caLQXh4NJhThq/oTX9B016x2/HiQdytlij6O1ueAn7hCndkiO3A8JBWb4mGedKEtAkmAFCqvYN9Rk1akr4pKbz/vGQJzfrPR/Uw+jUQ/kEUmAQqnpY/KdkwAyTw1+pNcrxn+CR4VqraaCjpGeQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748349280; c=relaxed/simple; bh=qSzSccNwHm/EbOY1HUt+rDd/6Vn+Dn1mSUc44tIJgVU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uzqFCNBqwk1qqqDHrW6S8j40y4wyBcNF3SxlXvDxKMQHGB2xER26zV1vLKiEFDdk+0wmrXXKSiZ3M62ZtM47A7sWn+QGaFQLNGR1XAYBmI5ZoEIdYBkM0qnZ0Me2wYMZWR+skpu4gf71JcD/erzmhvUSp6kcywMRdn/HV+7uoyA= 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=zeZRoO2I; arc=none smtp.client-ip=209.85.128.74 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="zeZRoO2I" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-44b1f5b91c1so25338715e9.1 for ; Tue, 27 May 2025 05:34:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1748349277; x=1748954077; 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=tgiTcbnODmN7NBNzZHsAa4v7DHZcix2AOinDeZo93mI=; b=zeZRoO2I9rMxuKyR5KIRQOaQBqLUT4EfhMHMvR10hKbBlDcIieTtKBUvzzhWPSrsil xmweIShljtmpYkYTNEujqfu+vczQdQHC9iePH7xpRNFK+Zo++R3H4ZDv3gfs8Dhvm9YQ yMcx5kgBj96w/aHzalkp5cDuoIpgZWF7NpRwM2N5neSjRp8CYkUlC1bluXt+gNNwfvDg 9X489+TU/sBWXeF+E/otkvXEdP4fb3DvZehsQjEFE1xaHl1+MrXgvVmZUasq4Cu/CJff RrmE44q+e6R6KHQMrLHVobTVg9oAR38ES/5FFRbHJ5dive5cWCfiK2M0izwFswXvHSgu fV2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748349277; x=1748954077; 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=tgiTcbnODmN7NBNzZHsAa4v7DHZcix2AOinDeZo93mI=; b=bDXhg+r4M5IaBVV3YAK1pVP2GpoA8x9F/VJUG929hw5nKGxc6kDI78WLmxCPzvyesT EDY3tWSr7tEkGlALqDaaZH5/wA4+Mcinje+/lc+Cs69wg5hgLTN93QvILp92nYcMOGoS L6Dl5x9TwqoGKn6Qf95q8V1HsnvzPjhPDAr9IMtc6msNBhA2TzKnxtZ6z+igs9C+NswF ZZ5b628ivvaQ2mn2DCD/m+BPOWPKSsjfvDCR/s5PYRRRu5+vZly5pf1dqw1FQrHPM6eT JjljzeohrWgCybE/h4YyYZEwR9uXcMBrUZZJDfAREj1FZzDEA+xuGQpbi1cLYiqo5vBh 1FOg== X-Forwarded-Encrypted: i=1; AJvYcCUqWJXM14YA9gXCBOw9QF5lB8gTnIk+ndajY6YQLusEHJUBWT7v3nVlGOZi1WmHF5rrK5jHBwZL858sFHw=@vger.kernel.org X-Gm-Message-State: AOJu0Yxp7Nh2+fiLLilUjsSuCbjVGhi6VleukDp+6Hj3+L4d6SMOqaz8 rPpBriKhkiiFt1UNIg9XmvnDB270QDT7sFs+bVHpCMGNtiHuu9P7KrT3xU4C3mXU/HEdn4feJa3 33uNKN70+Q995Ezcptw== X-Google-Smtp-Source: AGHT+IGUdGoc9kWEo4dBuCw+VIQGeYKUwY+zpACgrutqHlc2rqm6n1Ssrc8EZOTz7NSsdxPPD68l3PWXaMzI7jc= X-Received: from wmbji3.prod.google.com ([2002:a05:600c:a343:b0:44a:bc4e:e9a9]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4fcd:b0:441:d2d8:bd8b with SMTP id 5b1f17b1804b1-44c91ad6329mr107575295e9.8.1748349277376; Tue, 27 May 2025 05:34:37 -0700 (PDT) Date: Tue, 27 May 2025 12:34:22 +0000 In-Reply-To: <20250527-strncpy-from-user-v4-0-82168470d472@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250527-strncpy-from-user-v4-0-82168470d472@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2738; i=aliceryhl@google.com; h=from:subject:message-id; bh=qSzSccNwHm/EbOY1HUt+rDd/6Vn+Dn1mSUc44tIJgVU=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBoNbFZAnmeyWKbI8/QBJ6CUOZ2bBb/QB8Bp9NiX NP0QcLLmUGJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaDWxWQAKCRAEWL7uWMY5 RlnRD/4tKhcp5FVbA8ub8EK/siY4DNNs29ckv82qj+LAthILy0di7qU/qwPGGmHvIF1B5C5E2aU jcaZQXGLTO5zFUM8Z4hzk0CD7r3AVZpIlpNL+R/slhOqUQVs9fDM6Q4ImBtsKujuzFoxDz8kgX6 rtay0mcK9PRJKGt9aueXEnWS6tjXF7msWCtiIMvUIBKrR6f3EpXzB6PTf+hWICwRdyxqrrktaPx 54MhAhoZVjRDzIHUkEc1WHfIdOTjF2ZpaOuia3gJQ8CmSpeKlfvtamPmWRUAIjnDOcIK2y0mMXJ SN9UF6MyL9Hy5Vbj/WU20riL1S30Oz7B8G5MT1J/5SPSIbOvecIyfxC8LqvH8Mu/dJIJxAS88J8 c0PXKGuNWlb+JQY70SgYoTV23+cSQTqJ4dwxgpPSMUPYyS+NmTxUna8/E7eK4DPRrcY8qyEVOl8 zwI8IDnUCttiyuixkM/VfTv4XsyVMhDGxo27OZJFdWRniEHLB3FjS3LIGiDRVOKOvm2OLiGX85U X1ku4JxecJsmYHzMVQqwfA87h7awackxG3oi+vUVz3CB9C9uPCXSVvHWo6n9ojIzXot1ntE6ZHK MorKGBPxFsK44AzQQ+rhPo8PfIQ4VXV/dqj8swG/XMM7g3MrgIImgFCkcNSE6EkEPe13TNa0cSr 6h01ds1kMQjFxnQ== X-Mailer: b4 0.14.2 Message-ID: <20250527-strncpy-from-user-v4-1-82168470d472@google.com> Subject: [PATCH v4 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 Reviewed-by: Benno Lossin --- 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..9b1e4016fca2c25a44a8417c7e3= 5e0fcf08aa959 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 `dst` and returns the length. +/// +/// This reads from userspace until a NUL byte is encountered, or until `d= st.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(dst.len())` is returned. +/// +/// # Guarantees +/// +/// When this function returns `Ok(len)`, it is guaranteed that the first = `len` bytes of `dst` are +/// initialized and non-zero. Furthermore, if `len < dst.len()`, then `dst= [len]` is a NUL byte. +/// Unsafe code may rely on these guarantees. +#[inline] +#[expect(dead_code)] +fn raw_strncpy_from_user(dst: &mut [MaybeUninit], src: UserPtr) -> Res= ult { + // CAST: Slice lengths are guaranteed to be `<=3D isize::MAX`. + let len =3D dst.len() as isize; + + // SAFETY: `dst` is valid for writing `dst.len()` bytes. + let res =3D unsafe { + bindings::strncpy_from_user(dst.as_mut_ptr().cast::(), src= 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.1151.ga128411c76-goog From nobody Fri Dec 19 22:01:17 2025 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 1AA5926FA46 for ; Tue, 27 May 2025 12:34:40 +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=1748349282; cv=none; b=TBh/NUzCoIHCZHiiqTN6D7M5cx0h6giZsiACOagjw0yBv+/8NB7MGkcjpdUT5Ki33LsKDeyW3kpJfa+eUhUqRs6toVOdHJnlYoFVbtOaNQbwAFzeEU8iTIsvBJ/oi+lFDqudEs6Qdym8JApOC/QWH9R9LXfyh+3M0tXPhIpYrCM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748349282; c=relaxed/simple; bh=5+gHeZnaQYgZutQTtX+EknJ3CS9JtqVgxYuIsi38bhM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=g2iTI/FI/CGQSLujAUbdZ+jzqqgXrbF3xx9KlqKk8USvT6A5aO2OUWWf4RE0p3nwshO2lAYkd0a1s4UCEnRZ2qAAb7d7VasVkR148Gk8zeIfbCCvz0XOgA0nrpDn7MvBM6ekmhqd8j5b7dy+yllgRorWpFSpHcRQuU1yecsbip0= 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=e0twGWY+; 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="e0twGWY+" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-442dc702850so21268935e9.1 for ; Tue, 27 May 2025 05:34:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1748349279; x=1748954079; 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=eKsBs7x+60dOtIZ1nxd8ao5TOBopIAlwZSl9Z1b0iQQ=; b=e0twGWY+5bgupR+r/q1FdB1f4tXIWvWNabFG4Kh1Mzkzx/ESXvoaocxLvElC/HZJsY zXF8M9doFmwCZ8TX8YrmUjWWyUIc0ymBb7mOT00dQmfm9DSNL5xGU+K5PZoCuMwBq8c2 hOcNwrWEUNzjnWSs/5T6wI/w7DFKFpghF/uRqwonSyFFAyqPsGluREPZ0YybOwi0rile UB3VUcU1oC51C0I0FE8D5wbLFNebZej0ii23VRUYqeX8embE1yCZXL6sjp13C3Oo4IoV y6d5TFVrbx9pd2iVDg4r/sskllbeF7KFwIpi/vjyGmPvuACgiyE2/peOpjzsNiIVRx+/ DAXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748349279; x=1748954079; 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=eKsBs7x+60dOtIZ1nxd8ao5TOBopIAlwZSl9Z1b0iQQ=; b=Bo8EdsElEOdFsuqrmjvAaS2gc8yIpZJbCtHeyIvh7ffEzBULJYOfjuJdUlG9O7OdSh PaJFv3I9mIzMe84cJGrWVjklsfewCMQ7H1mp46t1auvbNLeVFJkCTxytrGXURjpDV5Kf lmijf8eBb7rEXycKEcxCSxPqLQ/6BgTTThN1YKYqQ8vYc6gO5Jywr/6mjbEiY7Z2rh4w Z1HcJRlbltUagrqjfhOBOaaOSoRiJCqFivoxIM3xioR+6M4O9V0ABFg+VOCRw2WuWAov 05ZiZv7wichBzPZjs7FjAVWgFwXoZlVeaOQEZXstey37x5/Le83wUl9A9j/V5NWO6rnH 6YSA== X-Forwarded-Encrypted: i=1; AJvYcCUsTMq1WokIPVK/D5q86KLVmrssJ/efvgYZcPC4B38bL0fXddea9YKWWErhrlbdTXDqT7o7m4jTIcSUr/s=@vger.kernel.org X-Gm-Message-State: AOJu0Yx6Puf5Kxn0CWnWc/PVf6UuStJ7cbPc7mZhb7ja6LFiLlftSatI RmyLj+0XTjoOlWiZrtcTO/BqYZ6UrIBUESndcPAfc/2p9Y5XeoCMvMRB9FF0gkee58Ps0FaZxOC cIU6cEH/DDO3IBYYOrg== X-Google-Smtp-Source: AGHT+IGnRHVSmtxKdrmGRlE3xDGcS+8P/Z4lRJJObbM07yTpLPawvzcWIixbwi4EVPbUM3Zpb6y++7QHKHZeG+c= X-Received: from wmbbd24.prod.google.com ([2002:a05:600c:1f18:b0:442:dc6e:b907]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:154f:b0:441:d438:505c with SMTP id 5b1f17b1804b1-44c935dd818mr88577295e9.32.1748349279399; Tue, 27 May 2025 05:34:39 -0700 (PDT) Date: Tue, 27 May 2025 12:34:23 +0000 In-Reply-To: <20250527-strncpy-from-user-v4-0-82168470d472@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250527-strncpy-from-user-v4-0-82168470d472@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4347; i=aliceryhl@google.com; h=from:subject:message-id; bh=5+gHeZnaQYgZutQTtX+EknJ3CS9JtqVgxYuIsi38bhM=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBoNbFZq8+H1drpShhcgu1FngxGyqqZO9Thkhg1b 7JRmMj1EQqJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaDWxWQAKCRAEWL7uWMY5 RvAPEACrquIC8lth9gbCh0iIxp9HpAna/Eo0e/qu5zVLnLC5FbHncx0JUfVyVY4AMS9GQf+KqLl 6ksFsGPzOhv+Dn5dZWpSkM5QRfpTkkXRMYrkn4vBcMZVPtViaEhU8J1mEJWeMkj25XI2haNSwjs Iv4imAgno8X9MiVAz9zosKxzqKD9aw8H0DdKyBxYpHu5wN8pEPpenx8EJTiPkas7OldeV3wm085 720TLayH2SOUv7hQTPteT7tN7q+jDw1sfg2YnLdS21AHsGlgNJqDhNEuvrWa8JXx97EJelDCbWx XX9I1+BrTazrvnl1e4YOzb6ksgp5YFNVHG84GX/FJArgyM4VEzRRbCljBWFX5rj4HlMZJwLiET+ OSGOKYTyy/wCFS2LwJDFFcvAdvWPR8QyfxHEQDOcQy3zFXBqQygAzcIDQz4NEiNPbKgjPLUCM+P /NeqSo9qqZZgnNOaC88eblTbAEQBynFFYDFo/0djY+5JCt/HaFOXuW/gf81rtXjBTCZl1IWhLU2 oPtmoko7px7PLm6TcaJAHBKfnnJrq7tuoqSbqil21KFF5+aGk5PROYOMyZDoHMhmp3siyVt9XLY diqZyHx6oJsXlc2tlFM5woDbpDZ1XsGgF0HF0AkcQDblzTEbkqiHAvJZ4MSy77rOG3xP7X7kSG8 xFcvIrouFd626PA== X-Mailer: b4 0.14.2 Message-ID: <20250527-strncpy-from-user-v4-2-82168470d472@google.com> Subject: [PATCH v4 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. Reviewed-by: Danilo Krummrich Signed-off-by: Alice Ryhl --- rust/kernel/uaccess.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs index 9b1e4016fca2c25a44a8417c7e35e0fcf08aa959..e6534b52a1920254d61f8349426= d4cdb38286089 100644 --- a/rust/kernel/uaccess.rs +++ b/rust/kernel/uaccess.rs @@ -293,6 +293,61 @@ 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(dst, self.ptr)?; + 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: There are two cases: + // * If we hit the `len < dst.len()` case, then `raw_strncpy_from_= user` guarantees that + // this slice contains exactly one NUL byte at the end of the st= ring. + // * Otherwise, `raw_strncpy_from_user` guarantees that the string= contained no NUL bytes, + // and we have since added a NUL byte at the end. + Ok(unsafe { CStr::from_bytes_with_nul_unchecked(&buf[..len]) }) + } } =20 /// A writer for [`UserSlice`]. @@ -383,7 +438,6 @@ pub fn write(&mut self, value: &T) -> Resul= t { /// initialized and non-zero. Furthermore, if `len < dst.len()`, then `dst= [len]` is a NUL byte. /// Unsafe code may rely on these guarantees. #[inline] -#[expect(dead_code)] fn raw_strncpy_from_user(dst: &mut [MaybeUninit], src: UserPtr) -> Res= ult { // CAST: Slice lengths are guaranteed to be `<=3D isize::MAX`. let len =3D dst.len() as isize; --=20 2.49.0.1151.ga128411c76-goog