From nobody Fri Feb 13 00:17:24 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.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 D324E1BC099 for ; Thu, 2 Jan 2025 23:07:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735859234; cv=none; b=YHneN4h0st92CqcSRyC3KxJ4fGNmHFeQEyuEcdY9Yca7+zHiCyh3BXblwGQNmrKum8kuMUjpNOF/YW8F9dRpCTH8EWaf/DHuUmpMe7VrAmrRo1PjoieMozktrK2y2aXb+egOg40TXaU0D4yCYfSTD+lh1HV707+2c0uHWdhEW4E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735859234; c=relaxed/simple; bh=lVwz6EQc+GmUnf6SKzkPimN8I8gP1WT+3pKfoCYwtuU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dl85gJH64LHY7s2HFNDjowaOljvld5JuqqbSeGMfptE/mD9W1uVppvEenqmBZiAsQh4kyZbImJn3XIAOy2Wnh3FRA4HJXtXV5LCsyvAzUPVIFkjbvQHcAYx1R2m3Z6NFheWUf2JdT7b7OVCwHPY/NYMNNluFXmResv4UIJgAqhk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--isaacmanjarres.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=wumlUs/f; arc=none smtp.client-ip=209.85.216.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--isaacmanjarres.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="wumlUs/f" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ee86953aeaso15676322a91.2 for ; Thu, 02 Jan 2025 15:07:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1735859232; x=1736464032; 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=Z1ymDQY9g5+AxfT3gggorOe2sQUyIfM9uTTESRagjDg=; b=wumlUs/fKDuyl9c8vcUTRH9hEMrCWhyzsKml/hEkfhs2fh+aLzKZOdyIkip+NsvNFp q/c2FNcUoR8Q6JTLWw3xi6sQoonvfPw6Y3LuRxLiF1kT2s0rLj0m/PEUPUZoFEGXCiDN OV90Y2Njx0MwftjLhbf3wO9jaoNB5DuSDJk9HScqt+HE9uacXZS0c/BAZsG9ua/PsB3k OvyVcS0Ncf7j8Y+OFcv4VxIvhWkwMoRHIM7qoEZn9m9eBAYnhPVWC3pr2tkA9CI4YnR/ TJ75djeXnFMbRTwBzxXITk/zLrIvCq+xIGw8dY+QzAIdsPRdu4uWLhSQpTVEaBSYzuCs zXWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735859232; x=1736464032; 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=Z1ymDQY9g5+AxfT3gggorOe2sQUyIfM9uTTESRagjDg=; b=rgtnFLzr0qBtG/SIHu6f4xuMArSUVVXGX2mXlWcVdlyHNRav54FzdNjV4H8r23hI1F NZdv/FZkIvQ+nJPebuf2t4I1FpVNg0dy2QUDqG4WnBOEZsO7UQlrVac0CkS7hGwIW0FP lrw+klLfsmfqFOdM5+XDBl3mPviNqQizhaR/prOQioHMNcLuzEfS1a6bI8UDm7zP0hqr TbCP0GzxJy082X4Wo4Q52qI3+NkSTvW2dDc0101SLGu+r9nLvFZ8pyufLHG1fSImiwPE nHznekkoC8aHwIQ93JmFC2X4QqKPWmi3c35vnNfvxrq1VjTGkT5kpLdfNAxli/WQh+jH LUHw== X-Forwarded-Encrypted: i=1; AJvYcCUZvbx7Kwyo+khedPdBFx723D4fAVGD3BZQj3z0g+MVz255BhIMlcl+rbrGE1L6vDPvzVqfTnucuhEpyVI=@vger.kernel.org X-Gm-Message-State: AOJu0YyTIQVfmJ0upcq2AZw8jlZiMGEL4KK6rlsPPRtO08KM5CX/YfOb UGedUldfi55IVA07dmvtf99L6gznVYOgx2XNgcFcWOqNMhp9jLXd6H3CmynRLRqO0GrhnrR58+U 9RjbVHlA4DuPG2f5623MoTdV7NmwMnv6UAw== X-Google-Smtp-Source: AGHT+IGlO2YmLhygfiPAPycfbxYv+TTIibWfRCkG5aTHVjFfoRW/AJ/XcsZdOJEoQFgwv2UPlUl4ZPMk+b1NBVbvV+0+bw== X-Received: from pjc5.prod.google.com ([2002:a17:90b:2f45:b0:2eb:12d7:fedd]) (user=isaacmanjarres job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:c2c8:b0:2ee:ead6:6213 with SMTP id 98e67ed59e1d1-2f452e4c536mr70393518a91.19.1735859232037; Thu, 02 Jan 2025 15:07:12 -0800 (PST) Date: Thu, 2 Jan 2025 15:06:55 -0800 In-Reply-To: <20250102230658.1112261-1-isaacmanjarres@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250102230658.1112261-1-isaacmanjarres@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250102230658.1112261-3-isaacmanjarres@google.com> Subject: [PATCH v1 2/2] mm/memfd: Use strncpy_from_user() to read memfd name From: "Isaac J. Manjarres" To: lorenzo.stoakes@oracle.com, Andrew Morton Cc: kaleshsingh@google.com, jstultz@google.com, aliceryhl@google.com, surenb@google.com, "Isaac J. Manjarres" , kernel-team@android.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The existing logic uses strnlen_user() to calculate the length of the memfd name from userspace and then copies the string into a buffer using copy_from_user(). This is error-prone, as the string length could have changed between the time when it was calculated and when the string was copied. The existing logic handles this by ensuring that the last byte in the buffer is the terminating zero. This handling is contrived and can better be handled by using strncpy_from_user(), which gets the length of the string and copies it in one shot. Therefore, simplify the logic for copying the memfd name by using strncpy_from_user(). No functional change. Signed-off-by: Isaac J. Manjarres --- mm/memfd.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/mm/memfd.c b/mm/memfd.c index 2372b9b1dc18..e14ac2c41fbb 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -352,26 +352,18 @@ static char *memfd_create_name(const char __user *una= me) char *name; long len; =20 - /* length includes terminating zero */ - len =3D strnlen_user(uname, MFD_NAME_MAX_LEN + 1); - if (len <=3D 0) - return ERR_PTR(-EFAULT); - if (len > MFD_NAME_MAX_LEN + 1) - return ERR_PTR(-EINVAL); - - name =3D kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_KERNEL); + name =3D kmalloc(MFD_NAME_PREFIX_LEN + MFD_NAME_MAX_LEN + 1, GFP_KERNEL); if (!name) return ERR_PTR(-ENOMEM); =20 strcpy(name, MFD_NAME_PREFIX); - if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) { + /* length does not include terminating zero */ + len =3D strncpy_from_user(name + MFD_NAME_PREFIX_LEN, uname, MFD_NAME_MAX= _LEN + 1); + if (len < 0) { error =3D -EFAULT; goto err_name; - } - - /* terminating-zero may have changed after strnlen_user() returned */ - if (name[len + MFD_NAME_PREFIX_LEN - 1]) { - error =3D -EFAULT; + } else if (len > MFD_NAME_MAX_LEN) { + error =3D -EINVAL; goto err_name; } =20 --=20 2.47.1.613.gc27f4b7a9f-goog