From patchwork Mon Aug 12 02:29:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760009 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 053A1C52D7F for ; Mon, 12 Aug 2024 02:31:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 850E06B00A3; Sun, 11 Aug 2024 22:31:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7DB706B00A4; Sun, 11 Aug 2024 22:31:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 653D96B00A5; Sun, 11 Aug 2024 22:31:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 44F926B00A3 for ; Sun, 11 Aug 2024 22:31:09 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 079D4A0739 for ; Mon, 12 Aug 2024 02:31:09 +0000 (UTC) X-FDA: 82442016258.02.1DAE848 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf26.hostedemail.com (Postfix) with ESMTP id 32700140005 for ; Mon, 12 Aug 2024 02:31:06 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cnARXHBB; spf=pass (imf26.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723429813; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Dg8K6fIsZQLFiS7Xpg/1YZlm81yjN4oHJWyrWmInMGw=; b=2DUzkMhyHLf5+JrhC+6JLsx2vdnwT2+QMr4WrA5Sk5iJuQcgUfmhzrz73DSmJPwZF7XzSP VDlz2ruG1++66QJ+r6GTXWv7l29Jr+bg2cMaqHLMKgr9nxf87hi1LpS8ZAnMDolRBbOPgx nWQQvOsJFqN+rVcrlyZT/I7f+wle4a0= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cnARXHBB; spf=pass (imf26.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723429813; a=rsa-sha256; cv=none; b=r7gfngwQYnsOxYjtZIN9UokOQB0ZxzaeRRpIfSgvhrmIU3loF5idtfodhqodcq/K5FPxMA mrL9XE53aNlaKMdZ0FcPz61fJytQg40uR/6Z40A7m4HQbiq7/soCvxW5F+uGKL05eTRDm2 g7oNcPaJ2dPs7NgpbZ/qN4HgphewSeM= Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1fc60c3ead4so24420115ad.0 for ; Sun, 11 Aug 2024 19:31:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429866; x=1724034666; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Dg8K6fIsZQLFiS7Xpg/1YZlm81yjN4oHJWyrWmInMGw=; b=cnARXHBBe2yTSF0G5pH50C0c9gCmwGxjOLkGvmaKQm01lMr8YWrTArZGlSXMohwO3p vv7+rUuYs49AYsij+L3l+Jf4XnsETu7dSzEglPfRmNSb8QWoQgqPRxpemxCVjoUBB6E3 s3vLjdL/UDUdpqI0YSzRj7NlpUjBwC05e7bsF+8G+HeFSttHjWxmfyNOBgaZkU09cFxU b2dkWVhYzcTjlh7wTb4oxc6SfJdf7iyaWWbIAtFZ0sQZOMFYmB6sgCi8Jt9PT0fWza4M ZwLli1HzC2qoY25HqWnkwxj/PK5IOwJsotsPpkX7xb+QHwvDrv1r5s0bo2wMp3WXXqoG K53g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429866; x=1724034666; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Dg8K6fIsZQLFiS7Xpg/1YZlm81yjN4oHJWyrWmInMGw=; b=AxH419R+8SZRmENUsIE1zRS8+C399QLG2h28l424GZVuIdOhN37Qs43keZbqLm1OKu z14APKJ4KXLf38W016nxKejHuDF+94VBml0RANQCPmMoAVrw4h4fv4CdOyFI9E0n/NZu pJ3SRB7wQleXEWqetr/knzFhYAiq57IGtgzPL7w4Y1uMOEhT+ZoY9yS/f0EKAKqFOhwI oZwRfADHzaDsmTlQjEVKpCoUHjncekgoi2T88+ax0U7EDW8I2rp/be5Z4UhkXnwqWYmC OzBMTxQi+J/stKP0eXifyl09xqOZr1tnGJ4nPVFIM12SOFtm+Kr5JwVjSHTJPSzJPvQ1 pJRg== X-Forwarded-Encrypted: i=1; AJvYcCUVPF+7UxrExkytqOK/gasMhfGkR3ctu126jeqWH3n0Yy3NLIQjwLIEh69zfomQjGml7MPV79CSo2plHCY1UIEPF18= X-Gm-Message-State: AOJu0Yw2X7u1T98ey8ohZrH0xfEjfOgUuCN3hCm2Q5Y1YY5VF4YPNxr0 AVoct9jElJ5dCryFYPXstDtricTKNkSYhQE2ihgDusiXNivIY2gF X-Google-Smtp-Source: AGHT+IGpTCkxya5k7tDP0eeWIg02IZrGkBb+Xs7WqcLI3e7hWnKKPF02puCEAAGLT4aj9e0Erpx/iw== X-Received: by 2002:a17:902:cccf:b0:1f7:1655:825c with SMTP id d9443c01a7336-200ae5a8934mr50792845ad.36.1723429865812; Sun, 11 Aug 2024 19:31:05 -0700 (PDT) Received: from localhost.localdomain ([39.144.39.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bb9fed69sm27884765ad.188.2024.08.11.19.30.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:31:05 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao Subject: [PATCH v6 5/9] mm/util: Fix possible race condition in kstrdup() Date: Mon, 12 Aug 2024 10:29:29 +0800 Message-Id: <20240812022933.69850-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240812022933.69850-1-laoar.shao@gmail.com> References: <20240812022933.69850-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: ryezu8md51bhci3c4x86jdp5hipz415o X-Rspam-User: X-Rspamd-Queue-Id: 32700140005 X-Rspamd-Server: rspam02 X-HE-Tag: 1723429866-646697 X-HE-Meta: U2FsdGVkX1/mbfdTnC5iIKUeDMHxnPWk88Dn96571ZrzzhAIe02A1Nlny0+6RSvA/zyx2WO2j6YosS/q8DsM6ozm9wP6DvtAsbdLbFH+k5GBl8iJ4FHLXWGIjn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G4tXJmj/ybiu0nbH9BkU7BN/cYAknOOyQSpU+N9y1nvZqiVqvxWpaEoGDfyGRJ9hwtDROrXkEWFCgfisF62Pfmn+Vst3eoWJ3Sj9AeYjJ1oD3RrABsupAi5UtgBYrygFg+8pVVz26NZOdKBPpTzCaieQC8WZcGVnLRlUyQbQHGNjBoCYPt5XQ09siEuNA+FySB1RvJd0HZcQ0oaiWHzTE9LFmulvQud/Bt2Zw4+2C9M0lMK/J6FQiF00rDe7juaGhFeL8sJoi6zy8TX1jMCHmm3eeYgGtvY2a0prOtVURssFSsqM+YKms2QKUUP8PPALGEi9xdrsM7DNbVV+JQ9ay/+t6PWViE9sSzHpiNZ2Lzk2juMCK1Zk7KVCQmzCD3nwtJL3dgp8856Hwwa3DSzBKWjcA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: In kstrdup(), it is critical to ensure that the dest string is always NUL-terminated. However, potential race condidtion can occur between a writer and a reader. Consider the following scenario involving task->comm: reader writer len = strlen(s) + 1; strlcpy(tsk->comm, buf, sizeof(tsk->comm)); memcpy(buf, s, len); In this case, there is a race condition between the reader and the writer. The reader calculate the length of the string `s` based on the old value of task->comm. However, during the memcpy(), the string `s` might be updated by the writer to a new value of task->comm. If the new task->comm is larger than the old one, the `buf` might not be NUL-terminated. This can lead to undefined behavior and potential security vulnerabilities. Let's fix it by explicitly adding a NUL-terminator. Signed-off-by: Yafang Shao Cc: Andrew Morton --- mm/util.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/util.c b/mm/util.c index 983baf2bd675..4542d8a800d9 100644 --- a/mm/util.c +++ b/mm/util.c @@ -62,8 +62,14 @@ char *kstrdup(const char *s, gfp_t gfp) len = strlen(s) + 1; buf = kmalloc_track_caller(len, gfp); - if (buf) + if (buf) { memcpy(buf, s, len); + /* During memcpy(), the string might be updated to a new value, + * which could be longer than the string when strlen() is + * called. Therefore, we need to add a null termimator. + */ + buf[len - 1] = '\0'; + } return buf; } EXPORT_SYMBOL(kstrdup);