From patchwork Thu Jan 9 18:59:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Isaac J. Manjarres" X-Patchwork-Id: 13933155 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 BBF88E77197 for ; Thu, 9 Jan 2025 18:59:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4DA626B00BB; Thu, 9 Jan 2025 13:59:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 48C606B00BD; Thu, 9 Jan 2025 13:59:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3034E6B00BE; Thu, 9 Jan 2025 13:59:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 0DF026B00BB for ; Thu, 9 Jan 2025 13:59:25 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id A5B74C03EC for ; Thu, 9 Jan 2025 18:59:23 +0000 (UTC) X-FDA: 82988826606.03.C71DC07 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by imf08.hostedemail.com (Postfix) with ESMTP id D5537160018 for ; Thu, 9 Jan 2025 18:59:21 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=4OZFR2aa; spf=pass (imf08.hostedemail.com: domain of 3iByAZw4KCAUnxffhrfsofwwjxlttlqj.htrqnsz2-rrp0fhp.twl@flex--isaacmanjarres.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3iByAZw4KCAUnxffhrfsofwwjxlttlqj.htrqnsz2-rrp0fhp.twl@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=1736449161; 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=vVkytyGroLwxKpu6w8PxqqBpmuTg7vt8SUP4RbxWhNI=; b=1YO4FNAyYSkqb/8+H3ZnNY4n81Z+irwPY7jr12feR5h2uC20OGtrhjQxx0ZZBByB4wDXgR ktDVifGHwK8y8O7lknQLqykrATgyo/Ls8p2KKyIsRZi80upCvlwgbvrqcTZPKOerJ3OTkj j4IQRfqQ8KRniqk3gafLd52m4P1rG3o= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736449161; a=rsa-sha256; cv=none; b=7939Aeq29LiL4F0IwfwwcbWF0/dTn1sgqYnrLhXY8+YJ+8xVpQ7DKDYno8LAEI2Dn2nUh/ GQwSZEKZECU1OUGTTI89fZ2QSAGRPFWza7TA5+2JW598YX9rOgoOBacuqxYZnyQ7ZfhhEQ LVPJMGGV7eOaE49qkoxZagzdDjPzPnE= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=4OZFR2aa; spf=pass (imf08.hostedemail.com: domain of 3iByAZw4KCAUnxffhrfsofwwjxlttlqj.htrqnsz2-rrp0fhp.twl@flex--isaacmanjarres.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3iByAZw4KCAUnxffhrfsofwwjxlttlqj.htrqnsz2-rrp0fhp.twl@flex--isaacmanjarres.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2efc3292021so3185385a91.1 for ; Thu, 09 Jan 2025 10:59:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736449160; x=1737053960; 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=vVkytyGroLwxKpu6w8PxqqBpmuTg7vt8SUP4RbxWhNI=; b=4OZFR2aaBUhzZVSx1xxUSJLFVJQRJJa1AnJthnm7s78yiZq79mmufj41i1szcf4PaZ U4xt9aryGmM2Ce00IoXgWXqzXXbiXCeluZb/gDcqKwkyw0Zd+c36H2B83zejx2dfYZIW +wAIPYt0/sZRfYau8t9PmhwdLBAHQNlU/42PAH2KHbJEw0C2U2hV4HOvRPJ/GWuB42My woiMs6iCv3SJ0cwupef+MOXmLc4Va9w6grk3QD0jPbyVAbLYnXSCiR1tP4CIhQooQIxr 7x2SdCqijJJPEk4+6GqlLFuXCWaugQ5JxUiQmouMiiFgIaCFHA0EBb5FGxIuyD8g4MHo hZqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736449160; x=1737053960; 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=vVkytyGroLwxKpu6w8PxqqBpmuTg7vt8SUP4RbxWhNI=; b=JckZqIiRi8Bo43ms5mdbVYnEA8P7G+mpcAvQCwTpUP8cfRMC3zunhiL+3Y+rEljlbD JutWw4bbYOgwicD/y8MDxfke9IFW4+beu0o5GttUopt0DJ8XjJhEByED0s4GdURJIQq9 Pdu3jDdXnD09U8iXY7JNGy/cbFOvmzLpC6o8EkFCMPd49FLMOcnb+p/w4/PVKjUqguTr 7/vq7djZrWWl2A9ccNQrgYNj9Q2lpU5WtrRBLXBJesJJYaEYbVbS67xDiQrDNSYa8KOj 7wq0n9JTbRbahkmexDEC6HZxd45qwmmXrDPMDR5S70rsFFGB3J6uoVYdAv0id2r1odaA IbBg== X-Forwarded-Encrypted: i=1; AJvYcCUBSUluL7jkRxVK0aq4mSPkx8K7OBTamXqXgOsFvn/b1mbkEjq5pnRNHcreqJAOHF/pn27ZUQvnXA==@kvack.org X-Gm-Message-State: AOJu0YzSfQ3b4DHZ+3lf0w8GXNvJEgV8c9i3Ao5vjM/DpLQLOeFKhvq1 kTXB9pjkrmh7yV5SvrbeXVb195yruTR5kV5EAIU3Ki6iWH8zs1U1ws1w5ODEteWI0WXrvK0+/Y1 w6EvhIxdO5FMjBmq5CPJqzoz6xXGkCtTiSQ== X-Google-Smtp-Source: AGHT+IFcAdbj5ipXPPT8S6r/j/AGAC2HdwtO6q6OnIuCZs5nz6p0jTJfcCgfKaTqsW7r6Riz5MD0Oxb+Hl+AaSAao9LhCw== X-Received: from pjbqa5.prod.google.com ([2002:a17:90b:4fc5:b0:2ef:d283:5089]) (user=isaacmanjarres job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:280a:b0:2ee:d63f:d77 with SMTP id 98e67ed59e1d1-2f548f2ecb6mr12096434a91.9.1736449160589; Thu, 09 Jan 2025 10:59:20 -0800 (PST) Date: Thu, 9 Jan 2025 10:59:05 -0800 In-Reply-To: <20250109185908.1006310-1-isaacmanjarres@google.com> Mime-Version: 1.0 References: <20250109185908.1006310-1-isaacmanjarres@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109185908.1006310-3-isaacmanjarres@google.com> Subject: [PATCH v3 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: D5537160018 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: ez37a4bufjb8t8xffp3b13unxuh8yxs3 X-HE-Tag: 1736449161-859045 X-HE-Meta: U2FsdGVkX18MyFnTPMxrAD+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ev+OSw8QuNaJOZdktHszKTxlnXtUhjodeZJkU4odUJhfqCQtOqjLxZls0TsoIWr5cL9V+EkguN2aBkhIKFHRvOuD2W5+zbdqHDqQsDo2qYz/Li1RClYo/vIPIAZ4kfm+o+INskrFk1uRwy7RxuyHdt+lLLQqL0gJ/FKpzGmTdlFf0p7BwRh7m5lOyahtr6v6ellDR7mWt0CvUksYg/mH4RJGLJFQP/U6G7EcDz792sKxmR+UYjMxE/H26VnrNfO1YC0Ebc3LJKm5qAVNYI1RVuUaQMKGpDTyfW4s9n4vtNOYsCS+b2Dm67Vv8+n/iXVGWCSI8GY9JYT8xrHZx21quMv6Nz9MAAq7iU4llfxsqG3/NhM0hTOZc+M5DWWoE6359/c2h8SLQcTfP3UJIVfEc8GYH1ga5UGdbL7DJn0PzsiKKjpZOpqgM55sZ8tTVb/eVUApFq7fU6mP76SUjymjIDp0VzIWOsJegnYsolkZB4M5wCSwubB2lxj/hTMibS3QsFFjOpgHytriZyD0L8yAkxrJDkrho8Eyg8ITTnfincSmxSj4/RihgSV6/PA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.015266, 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. Reviewed-by: Alice Ryhl 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 bf0c2d97b940..5b7c5892ba64 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -396,26 +396,18 @@ static char *alloc_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(NAME_MAX + 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)) { + /* returned 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; }