From patchwork Thu Jan 2 23:06:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaac Manjarres X-Patchwork-Id: 13924999 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 637A4E77188 for ; Thu, 2 Jan 2025 23:07:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E7AA06B0083; Thu, 2 Jan 2025 18:07:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E03DF6B0085; Thu, 2 Jan 2025 18:07:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA5576B0088; Thu, 2 Jan 2025 18:07:11 -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 A97BC6B0083 for ; Thu, 2 Jan 2025 18:07:11 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2D3DBC04E1 for ; Thu, 2 Jan 2025 23:07:11 +0000 (UTC) X-FDA: 82964047866.10.DEFF72D Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf04.hostedemail.com (Postfix) with ESMTP id 9661140014 for ; Thu, 2 Jan 2025 23:06:16 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=1jj7CVer; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3Gxx3Zw4KCHIYiQQScQdZQhhUiWeeWbU.SecbYdkn-ccalQSa.ehW@flex--isaacmanjarres.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3Gxx3Zw4KCHIYiQQScQdZQhhUiWeeWbU.SecbYdkn-ccalQSa.ehW@flex--isaacmanjarres.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735859205; a=rsa-sha256; cv=none; b=RGY4hT4mPMS/LlrXVt2PzD8Wrzr0JsKMih5+ghzbB6oi10ElTAhBPiXCKbRXps/EufG3i4 IzyWO3wVG/r5G0w8adxVR9Z7LvXO4/EsIKqn2ouXg5tHv1xB9BvzFPxFinZStAgkPUsQoy dNoHv+2u95jSvtjfQt9lRoVzKzPSyKg= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=1jj7CVer; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3Gxx3Zw4KCHIYiQQScQdZQhhUiWeeWbU.SecbYdkn-ccalQSa.ehW@flex--isaacmanjarres.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3Gxx3Zw4KCHIYiQQScQdZQhhUiWeeWbU.SecbYdkn-ccalQSa.ehW@flex--isaacmanjarres.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1735859205; 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=oukJOp8a/TMok9WYKtZiYjZTJSlSkLa4mKjkY8cRWFM=; b=bAk529qEn8cJJXSRdeAWwfj39YGmDHjTrcKi5+gFTqM0kq6CB/h5sMPJtpprDdk2rW4ZuO 1jqHprkWm3J1muA6PqlAKeQpnrfQa4hztc8kiLa5i1z7HjkwZ5AZYOBp5BXizkD0luzv1x ZRldz98D4GdXLRtSpQURSnNXOjkepHs= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2162f80040aso148954975ad.1 for ; Thu, 02 Jan 2025 15:07:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1735859228; x=1736464028; 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=oukJOp8a/TMok9WYKtZiYjZTJSlSkLa4mKjkY8cRWFM=; b=1jj7CVern8aQqpC2ZF+MTI0gsDM2z67kOQtixWN5ieAiNbZzgWmRHcXNWXrtxxLPA2 Op54Yp2tPQI7ul1L4x74xHhc2N6fscdDKN6hR4S3XPB7RQLcSGCyNxLlELwo1uTjIWSL CJzMHNj8wq6+UX+0BBght01r9Uv/XaNHqfGfkm+TKJmvuo31v0Dph4O7jzLwtJrUmIGn g077LVfNhqFqjZXqhJCaUZYkQiJjdnRwx3tbsc2+nDwZFLUs+EF0J5x2S+t+/5xDw5dw /DmhvlAFdlYEWWvNrZrb4cPkPqeJjYgjCCzt9YehDbdw45OZAxuCo8KSSFQwH+ayAIFi 2sFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735859228; x=1736464028; 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=oukJOp8a/TMok9WYKtZiYjZTJSlSkLa4mKjkY8cRWFM=; b=IGWhhL5N3NLMOQ1g0GrVEZXJENBoEhiNDShHuwCj1EGqQu369Uh5LAMt/347OHk3gQ D/ZQr2WIptUf9iEmkMYdfgsNzZocANjZMGRTfRarwJnNA7ggWX/Lydv48hR9MhP48sGl IxzxINmtL9/2qwYS8ivcLgPavxEqn28ZK32eT9lVONlHFK6EIK5exvjfQuf2QTQQLHDK cEf1zPc6wDlu/2cJAeNRO59rR23wxeaJ8+Vo3bN5DaUN8u9ZJ8P1OzIhku/nXaExQWTE sKXkPjldzDlhlVCD/RoHaNsyMz1uxSAbdtzWd6d7D32BmtIkyBDn9lRSFfSZPf8OrPfp PsBA== X-Forwarded-Encrypted: i=1; AJvYcCX+5//tgjxVzcB/7Cq/caf/qPgrMykbmkxshFtJpAWdaIgmYq7A1fv1cp+5GhqmSsMuaTddKCxSaw==@kvack.org X-Gm-Message-State: AOJu0YxQqiBAGWQYfhWhG1gzitr8RaCieF1EgSouvbdLn7OeWWc5PazT cjmpU4yj/gPszImCZbW9SnK/G66xaYzWu6b+D25jBE37QJsM/mY6s+5TDDrBrNjEcpTufmQ0XGg 0HgCtiUaALtaebhi7DoR2Xa0dJnwtLsw/CQ== X-Google-Smtp-Source: AGHT+IHuF92dOUZi8JHInVoyGiPpTf/4WW1PFofvYbD/uxaxSwdrHBgggo7bixp9tF8fN4tBvaLn+HpwurCruzC8y+eL8g== X-Received: from pfbeg22.prod.google.com ([2002:a05:6a00:8016:b0:725:d033:af87]) (user=isaacmanjarres job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:a103:b0:1e1:ae68:d900 with SMTP id adf61e73a8af0-1e5e04a2e64mr88252525637.22.1735859227902; Thu, 02 Jan 2025 15:07:07 -0800 (PST) Date: Thu, 2 Jan 2025 15:06:54 -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-2-isaacmanjarres@google.com> Subject: [PATCH v1 1/2] mm/memfd: Refactor and cleanup the logic in memfd_create() 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-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 9661140014 X-Stat-Signature: y8s744riam8puhodyguxi3jkn88aggyg X-HE-Tag: 1735859176-698431 X-HE-Meta: U2FsdGVkX19K559qyGKwznW++Y1nFoND3trlUurUBLnCNSJhPNjv8bcv9C0VNwaiymTVtlVVBMXR6zsbZPOwmTar0KoIV5ltWhuxfPF8wSatCA2aViEXnoNf65dssvhYcLwZ5FAnq3CXukV8NuGgkJp01fXh/t++BxZeZReo/SZTgFCv9lMICHVxgsrDH2r+PaBt81pyXY9XjlluHt6pWI/0X6jw7HbfrmjeO1k5xRIYZpH+UQ9/wRo4XgSuQFjWj4S307Zbu0Ol1BXNoIICdFm/ABCGrCCP2EZCRJNbzl3VCvX7dCb24hO3ZHy6Ocv4ewkmluYS+NturjYxgnGsqC3+3Op+WHcEfjVJv3KMUWny9zm/UdZ2ZxeRCUPUjJy8LadtrYlW5Wh1PTaUkKQJ4pIgE22grpJ5daWu+SbzFBSIgA/8cZbeU/RAnaRFtsnb3jExw2RZEd622KmIY2pqfysZW9qW/l5o8JSyap/CzbASvKNZYtuJUSBxXhnI5gC7UiL8/dpNPF7mdeC/IKtW4Vq2o61UQ7SYHXyVEAUEU5/Tip5Man21nOucRMGkpmgd+SgqqLg/LOl8v/2wGbnp3k7IAYUdjVhyu2qfh++1B30ylt2F0PImTzxn7v22pFbwAzWq8/BfZktduzoNLKrheoX1q2HJzyY38zFA8tPUpfF1qOdhIw+X2OohOoGJwwnLL5ooPSvbzKYv9YZzFDxY/a5O95K4Ps8A6e2CN4LGvGdMF9PZAd7EPv5VsNH61NQinEntXhiKQ8jVV37nwcHvFM9PlVCSqEYmZGNkVh1+EixCycTS4Dh00ntH7UUiqHs+a7yCAcE9ajceDvTtYHXg1DECnm10xbmaEauQBEpe8bjPaqEd6DL5nv2oCikFAY89hrtiCy8qyKC71KFTt7hZwBtt6TOUviA+0djCOOKNhYt5czvqUT1ds325+EqFc+7PsXSIHs+nET/QoBQ58LH xpfBtLLQ Nw/LBVCCpgQIp8DwNsvAKfTrnVk0FyOvYLNLYVLD7y2gCCnmwXS7//CYZQhFUuL7TPwNeAO/FhpNwXHtLtrMFVHAMr9JDuOHtfXexyD+F7cBfmJIRBQpPqN96EYmyAiRUFhvMgj+n6gbSDGNR5Ulh22plM6+s2FoMjdBXc3Os+gLY2QzubqSfcTNwVofAIqw6mm7ljglWrp3fBTzZyPQ6R+Lr15XSA8q0Vnwb7QKEvHlZVzHrpu8DF4iOl9/2kRgVxiNTp+NaaExoK1qBmApTgYApX3paZ8poMXBERDq5hU/n4XPQFQAtd7wmWD5LiBR1Oft+vJxj6jW2MC0s5lEODEIdzxheusKi2Nlej8M6OukJaWE8vZv55PLxHTu4/UupAWGMVjqyx5UFjHenFNWXrjKwVPjlZPQBZof/22oeCd3ymkac3QgwlCbyvx7pXgUfDL5l8wSGOz7uo1c2fHhJEoRZtIPcg7dx6hTnJtHt8RqR8hEU7EfuLzHRskqol9pAjKXerPzOp+lLO41JNo2vaoML93h5WT8WOddhgupT154WSEYh/uoK3hrX0HacBSDSG/GhCxCJOlCYmYaNLEjsxzVWnjwcC7i2vswcCCG11Te/256qmVGKnEF8LJy1qWX10RmFTdccmW1x/nq5ZzBc0+Ed2g== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000089, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: memfd_create() is a pretty busy function that could be easier to read if some of the logic was split out into helper functions. Therefore, split the flags check, name creation, and file creation into their own helper functions, and create the file structure before creating the memfd. This allows for simplifying the error handling path in memfd_create(). No functional change. Signed-off-by: Isaac J. Manjarres --- mm/memfd.c | 87 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 31 deletions(-) diff --git a/mm/memfd.c b/mm/memfd.c index c17c3ea701a1..2372b9b1dc18 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -327,16 +327,8 @@ static int check_sysctl_memfd_noexec(unsigned int *flags) return 0; } -SYSCALL_DEFINE2(memfd_create, - const char __user *, uname, - unsigned int, flags) +static int memfd_validate_flags(unsigned int flags) { - unsigned int *file_seals; - struct file *file; - int fd, error; - char *name; - long len; - if (!(flags & MFD_HUGETLB)) { if (flags & ~(unsigned int)MFD_ALL_FLAGS) return -EINVAL; @@ -351,20 +343,25 @@ SYSCALL_DEFINE2(memfd_create, if ((flags & MFD_EXEC) && (flags & MFD_NOEXEC_SEAL)) return -EINVAL; - error = check_sysctl_memfd_noexec(&flags); - if (error < 0) - return error; + return check_sysctl_memfd_noexec(&flags); +} + +static char *memfd_create_name(const char __user *uname) +{ + int error; + char *name; + long len; /* length includes terminating zero */ len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1); if (len <= 0) - return -EFAULT; + return ERR_PTR(-EFAULT); if (len > MFD_NAME_MAX_LEN + 1) - return -EINVAL; + return ERR_PTR(-EINVAL); name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_KERNEL); if (!name) - return -ENOMEM; + return ERR_PTR(-ENOMEM); strcpy(name, MFD_NAME_PREFIX); if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) { @@ -378,11 +375,22 @@ SYSCALL_DEFINE2(memfd_create, goto err_name; } - fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0); - if (fd < 0) { - error = fd; - goto err_name; - } + return name; + +err_name: + kfree(name); + return ERR_PTR(error); +} + +static struct file *memfd_file_create(const char *name, unsigned int flags) +{ + unsigned int *file_seals; + struct file *file; + int error; + + error = memfd_validate_flags(flags); + if (error < 0) + return ERR_PTR(error); if (flags & MFD_HUGETLB) { file = hugetlb_file_setup(name, 0, VM_NORESERVE, @@ -391,10 +399,8 @@ SYSCALL_DEFINE2(memfd_create, MFD_HUGE_MASK); } else file = shmem_file_setup(name, 0, VM_NORESERVE); - if (IS_ERR(file)) { - error = PTR_ERR(file); - goto err_fd; - } + if (IS_ERR(file)) + return file; file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE; file->f_flags |= O_LARGEFILE; @@ -414,13 +420,32 @@ SYSCALL_DEFINE2(memfd_create, *file_seals &= ~F_SEAL_SEAL; } - fd_install(fd, file); - kfree(name); - return fd; + return file; +} -err_fd: - put_unused_fd(fd); -err_name: +SYSCALL_DEFINE2(memfd_create, + const char __user *, uname, + unsigned int, flags) +{ + struct file *file; + int fd; + char *name; + + name = memfd_create_name(uname); + if (IS_ERR(name)) + return PTR_ERR(name); + + file = memfd_file_create(name, flags); + /* name is not needed beyond this point. */ kfree(name); - return error; + if (IS_ERR(file)) + return PTR_ERR(file); + + fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0); + if (fd >= 0) + fd_install(fd, file); + else + fput(file); + + return fd; } 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: U2FsdGVkX19M9DrBM/siTLBhsKimX0rVb8IA1idvLR7A+56ER6T0c29l3ioVnUI9YYjGe5TmBRK8975HUvUxotPzmbYTnWVOi6yLAXgz/oQQJr+oaxMLtcMD+CIhDhbguTWnszQRlu84ts9o+Z8/3pR21uS3R2h4zLAnCU1pd7nC/4ppVCWk9HgAeECayoPmhb+gwlaBCBKR8YF4aT28/nBlblmEHlSsdwQQV0LlYLnODCXmvTo1n2kS646OeFJLNIJZNONU0yXzEjWaL5rHGo7rbjT3ZPhahhyAalpLMVmrLoWUwN8TOTa0Jz9SsR3/9fRXQg850HWonK2WcefhTlOOQAXMh5RcmVAxkQtnk2C3q6kIV1C9/XFJahffNl3zHevmA+9U0yrFy8KSbNAmstImQ7cSHKBkiFdoCeyqQYmJopPQ/juFu58RSMTdAjN9UbYw7V7lwBNDvEzFdxofvv+Zu8liZ/tljNRT13rShzkjGZjPVecKPDu9iEGj8Y7+hOnCXD58apPF2yO7zNe1sAir7HZiJ36ywWNj2Jdr4BAugCqWp1mfOkoppdLBTBBqOboBwmLy8T1HgzhARDF+sBSL9TT/0bZHZRhGx0GF01TDrEJzycf3nIZf7rIUQh7LQZhoQDl1ZRsaxD610k3uT+cuPFDOp4FlOcV0aU2V1RoXAJeqy+8ZCfoI6M9PeyA/C9+u9ESuDDhIdbCngvSQlB6jrV7VY8FCJNXgSvWjlwJOjJkqHVN4YaT4ALeuQcHrQDW5c1rVeTEvm02tl9agfVmNOai8FmtYRKGjA6owncLUnCmVKLXcSiUPI3SLUF6QJHxduQcCLIHrs+JuIf3w5cmTbiES8w5MUf4kKkn3o5cYTuDa3mrkDV7x/Pd0zeXPDrH4mfbrjLJC1lH8XNZgVkNVejTAgcopOaVzuvZnr3qy69mr0trYeBe82Hr/ZpYBoHxaez7i5AHgibD6L2S 8d7hAks2 hARv/a+RDQdv6sNSsBHhxkBWOuFwjVTs1Ascw38TaEpTCuNrOZwyrn6HbEMoFlKGdAvt2hnRbDbgec7NdVluU5OJ7uwtSeLYp4Oe/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; }