From patchwork Thu Jan 2 23:06:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaac Manjarres X-Patchwork-Id: 13925000 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 54403E77188 for ; Thu, 2 Jan 2025 23:07:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF9966B0088; Thu, 2 Jan 2025 18:07:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AA87C6B0089; Thu, 2 Jan 2025 18:07:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 949A06B008A; Thu, 2 Jan 2025 18:07:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 6C4146B0088 for ; Thu, 2 Jan 2025 18:07:15 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 34A8FAEE31 for ; Thu, 2 Jan 2025 23:07:15 +0000 (UTC) X-FDA: 82964047068.01.7EEF155 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf09.hostedemail.com (Postfix) with ESMTP id 8E8FF14000A for ; Thu, 2 Jan 2025 23:06:38 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=zmd7xZgW; spf=pass (imf09.hostedemail.com: domain of 3IBx3Zw4KCHcdnVVXhVieVmmZnbjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--isaacmanjarres.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3IBx3Zw4KCHcdnVVXhVieVmmZnbjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--isaacmanjarres.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735859185; a=rsa-sha256; cv=none; b=me6BHr9II9Vgw4ij++5CDVFZrG9z2xbTyo327lCGTqzPZ7cCHP6B2+wH09kfoJ0mA9YOvz d3ugRQo4ieWaUO9r4Q+p7Qub/hHddKq1FrnB2HveF7MnBj+Y0YcnFNYBS6MFjdqdTGVjjl PQX8VEv729qHl+zI5fAtjt1R9fyZ5RM= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=zmd7xZgW; spf=pass (imf09.hostedemail.com: domain of 3IBx3Zw4KCHcdnVVXhVieVmmZnbjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--isaacmanjarres.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3IBx3Zw4KCHcdnVVXhVieVmmZnbjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--isaacmanjarres.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1735859185; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Z1ymDQY9g5+AxfT3gggorOe2sQUyIfM9uTTESRagjDg=; b=2afucRgXtkR4cQjRoRF1ttFh3Kp/1BBM872uWCuA/KxK7w0lEc7bGV7TLe1uHMlwLHXTaC 3f9hQJh/BEjTK2bW/Kn2ixYPmaVSrEJjJgiEFXeUA3owT3kr/Nph7HvEE6DcbxkiOrQCQ7 4QUFpCflyugQClbzN/6BTllGx01qRCY= Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2eedd15c29eso15675248a91.3 for ; Thu, 02 Jan 2025 15:07:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1735859232; x=1736464032; darn=kvack.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=zmd7xZgWqQHjN/KjzzXfn/+qI4Jou9e51ua3I/fv9jz96tSeY+9wo1cIwRydOt+LQT 9ehX06k9AnMi4mVGKiUDduoh81SX325UsOhr7KMLynvNDQg5WpQ2zsfTSy/v57Pzod+S rCIf67uuZa55Ie4LkEeI2dzkWCxv3U4dhzj67nmHpFlpr5LaLPwOJnhxyh/ExE1G1pyf XUMAmpi6h4ZG/I7VYK6lv9OVYWLB/opt7a4hHe9o9oZkV9KCg8bK0eh01wRAj3WDWjR3 vk5eZwP013hqwepR/qUPVx9KHQXFXQlw3CS6D6HQBRwF2fLtSkW4yD5Ek6tqWRZsbSda BkJw== 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=UI8zNCVC6Dr5CFV1X4LSBCJc8Cqkp5TTZS+5QEQ9DaOH02dvMxXTcyuYXlmDNz6wGe bLUOFR1wf2SVg/BW25M11oLORPiuJMgwJXJy3SlzWOcomSV+L+bwHct8GUZjB6VY0bjA S7Y4PHNw3ldqd6BnC9hqehGSwflS2GF4Dkdpb8XOdANhb2DBg898PXgwykDLhrG6Feea 5T/4CSqLRnUxFImw0t/zBBXXihUGt226LXCyzXbrgeOF6KlqulN2u39Q0y2wTvI2oNTB 2PnLQJXzVZTpk5DGQ4P8WSvr6m44Y6H1kILJm8zDihPxnEa171gc52CRHATS8HpGleZ5 xZ0g== X-Forwarded-Encrypted: i=1; AJvYcCUgVMOUp+kjEcroNURrJm14Q1dHlA1xvjUK53mPJXVQGDhdgAPaJJoBRcRoSwmW4Zvh3kZu2jbfIg==@kvack.org X-Gm-Message-State: AOJu0YxVbFeqq/jBnN19+AGQh59+vtUhS9j0mAuo33WsXq0o7XF1mlVM WkOpmFJ7Lli0PAvUvxTvpP779F9rl2404adaFK4sAtm7Eb6WluNENvPATcnhb0tlAz91wTAJQyJ Z6UL8F1t27lmmdohe85/lRBve02o/D0a/Gw== 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> 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 X-Rspamd-Queue-Id: 8E8FF14000A X-Stat-Signature: mm4bukawr7e5ahd59cc3khc1xruer6d4 X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1735859198-285569 X-HE-Meta: U2FsdGVkX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/rCzLZOT3naJV3/fSLwFhFvq5s0VFZsCq7aMTautU0zhbR7zzu7WsbdYpcxcivtbp3ZWWbEpam1srAwI5davDrSMQQaQ6V+wwb4wn5xtLdnKt2aHvEcG5zinglU7/Ng2BK53xjLy2RBQzdJ9rVxKUDjbogzl690v4yhWubpjyynr+wUr/6ykWhsHIVzR9fnDs+jrCMg9O7g/hbtuz0cQHXxdLHAnC9P2o+RAEkwRmD14OyTzNTfo7m1WYAtKx7c5kk1QseQXPtwX7wxsPA9wJGKHOxUi6+lw89cHgIqnH24JemV6bDrcYasxXneUe+Dvcf7hgjxwDlEko8+Spk3leDQWUhda0FI1hEgrH/GFrpfzBihSbjU3QiOzlxDaZeCaMNEAQqPUgHJr1XcgkFMqnp6XdDfDo2metL/WeWYHv7OWdcJmm26rAPvXD440yw5rhU6iwtBuP/HGAjRtvjbGjTnyxL8e5uFvBAaPwgVASJyZqLBidtuLzrm/F5oK3+1vZ/1NyOr90Od6CNkKADPxFDhQlOZ6vpscf6HJCWf17ho9Ov9aX+Dk1nLcsoLrTblvAtTI9N4zGbSdYhMsu1DFEAg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.160158, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 *uname) char *name; long len; - /* length includes terminating zero */ - len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1); - if (len <= 0) - return ERR_PTR(-EFAULT); - if (len > MFD_NAME_MAX_LEN + 1) - return ERR_PTR(-EINVAL); - - name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_KERNEL); + name = kmalloc(MFD_NAME_PREFIX_LEN + MFD_NAME_MAX_LEN + 1, GFP_KERNEL); if (!name) return ERR_PTR(-ENOMEM); strcpy(name, MFD_NAME_PREFIX); - if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) { + /* length does not include terminating zero */ + len = strncpy_from_user(name + MFD_NAME_PREFIX_LEN, uname, MFD_NAME_MAX_LEN + 1); + if (len < 0) { error = -EFAULT; goto err_name; - } - - /* terminating-zero may have changed after strnlen_user() returned */ - if (name[len + MFD_NAME_PREFIX_LEN - 1]) { - error = -EFAULT; + } else if (len > MFD_NAME_MAX_LEN) { + error = -EINVAL; goto err_name; }