Message ID | 20240828030321.20688-6-laoar.shao@gmail.com (mailing list archive) |
---|---|
State | New |
Headers | show
Return-Path: <owner-linux-mm@kvack.org> 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 1D696C54749 for <linux-mm@archiver.kernel.org>; Wed, 28 Aug 2024 03:04:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AA2E96B0096; Tue, 27 Aug 2024 23:04:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A53436B0098; Tue, 27 Aug 2024 23:04:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8F3F06B0099; Tue, 27 Aug 2024 23:04:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 6DA016B0096 for <linux-mm@kvack.org>; Tue, 27 Aug 2024 23:04:17 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 162EC417C3 for <linux-mm@kvack.org>; Wed, 28 Aug 2024 03:04:17 +0000 (UTC) X-FDA: 82500160554.29.293A06F Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by imf28.hostedemail.com (Postfix) with ESMTP id 31350C000F for <linux-mm@kvack.org>; Wed, 28 Aug 2024 03:04:14 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=nWJijizF; spf=pass (imf28.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.52 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=1724814211; 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=N9R7Mhbk+Se4PIDhpm0/p6nrldMPUtb+oC6vNd91DMo=; b=a+KCTX1OgYxOyNTyuUH0dxGF379lFRAK+W30T6dx66zw0p3AqMqlC3FyRpcMr0kiCDOrXE dxrd1jmt1yn7EMNpOySS/ZmJpq+qywR60TsPo9CEoMer8i7Qiq68nF3mNKcGDrppivJPB1 3SaHNGltNXvBjw8SQWVLNE2j981gB+U= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=nWJijizF; spf=pass (imf28.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.52 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=1724814211; a=rsa-sha256; cv=none; b=zXJVAD+z4g2IOb5LN5cf3aDtcFUXDmqU4ktwuJbqYLn7zY4I7vTWpkC08KmLsrn8SS8YOL pnVLZc4EjB40vEwqRkr0UwkaTZGmsS3Ecy9JSmyC7ZZLiEgwaXIIeskihOC+/4g+BlPb/C +JVo06Mm02QI2m5DJ1nnqqmc69RH2QY= Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2d439572aeaso125417a91.1 for <linux-mm@kvack.org>; Tue, 27 Aug 2024 20:04:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724814254; x=1725419054; 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=N9R7Mhbk+Se4PIDhpm0/p6nrldMPUtb+oC6vNd91DMo=; b=nWJijizFIpeNW6buGLZ4BTUitwruLES2cpnYe7PNgpysZAJBaUtO8knFCHL19KNP9S N3gCZ36GWHWguIx7ZSehQJ9niqaL0qzjhmJJmZSqJ/gU7+5bm5ydNDgy+lcUYerWK3a5 ZqUeyJOJTXAXAU5X/co/QPQ3sEfwNSNUCpsavOYdV0GXilt456Ism8+Nh7kL5j5AO8LK BOTgf9B007CExhRJ6jzyipqqrbntIyXVS11tiXEp+XhnO+1snDhebay6VletHffUcUZg fB7ng9OSvqM23ZyBOBrn8gjn440Mt8fd1sJHdK/ttSwoFdLiW4gPdqz3VoVp0Szz12+p S5tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724814254; x=1725419054; 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=N9R7Mhbk+Se4PIDhpm0/p6nrldMPUtb+oC6vNd91DMo=; b=EfZ3bR8cYT8LxehVcv2JO1Fj5eWZQBgH+V9xAjqxUBMiUdZFwkWcByCgXZC1ZorNdj wRNItoRp2u/YCKUbODy56MtmBKpybSSRX+a9cCAEiKCKiNXSZFACWM4/tSjr/jpKEUMF 5JZCoQaObT7TLTmYRrGBU/CrYG+5BMYNiPqge+ZVFbBuLWlGG/CZCiQBb4d4sQr+8J/4 5DK+LUsOulXIPFy3aHDUUBJClq4f3b81fujs7Tv1L53vxOxRunSImTH3hAzdhbq67q/R dCC8XHqPOL4SDqxersI8EANTGdno1j0GFBeWlwXaHRQhLB4tAAUWf+tRV3k6fxNCkBbv QJ2A== X-Forwarded-Encrypted: i=1; AJvYcCWZ6LwhjONa66giTTdCMkLHUWSXQ8BJy6IdAA8Br8tUH426roovKHS73UZdyyFJ2owG0Q8DzesWow==@kvack.org X-Gm-Message-State: AOJu0YytVMz7K5HHH7A6r4/QYOe71LfayPjOumf3jQJ4eR5qzOXU27vz GOWJUrJmz/jYUJ4u1FkWkj/DEfRU111+SdXN8ZsJDQ7VfNSQtfNb X-Google-Smtp-Source: AGHT+IGCtw+zPOY+Vs3gCNaMxterxPo1VEjCCML55T5/9brSfwlu9kiROVQ6nSIbi2XhsLWhhe27AA== X-Received: by 2002:a17:90b:3b49:b0:2d3:d79f:e8b7 with SMTP id 98e67ed59e1d1-2d843c16e68mr1209703a91.5.1724814253885; Tue, 27 Aug 2024 20:04:13 -0700 (PDT) Received: from localhost.localdomain ([39.144.104.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d8445db8f6sm317977a91.1.2024.08.27.20.04.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2024 20:04:13 -0700 (PDT) From: Yafang Shao <laoar.shao@gmail.com> To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, 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 <laoar.shao@gmail.com> Subject: [PATCH v8 5/8] mm/util: Fix possible race condition in kstrdup() Date: Wed, 28 Aug 2024 11:03:18 +0800 Message-Id: <20240828030321.20688-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240828030321.20688-1-laoar.shao@gmail.com> References: <20240828030321.20688-1-laoar.shao@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 31350C000F X-Stat-Signature: sbby79ruiujn1t4a1yqqcm6k51iykdqe X-HE-Tag: 1724814254-311345 X-HE-Meta: U2FsdGVkX18NPBjYVaNwBb9C9bXgrKY64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eo3S0mC+dAzaxFbueZZ+fAhqR8Xp+pOnNkhxLrrMVV8VtF8uqP/EiW/Y4AxElfTU7lAvORp1TwxB6YcoQVJ5RUicJAn4o9LjXF8Jnn5Ie19n/4SmsE6l/EGRe51KnUCtpz081LDDt53xRWOba2qcmsWjVA4bu9NLr7e5ehfeumi5UgGQMbL1kUhmRQ1Jei/wm+WDRPtTGXAjd+ONb0P/Yuy0nBhsUilIISK5XgNHW5mIdpx3kFNtprKt2OJsZPNBsg1QAIWJ6XB3Vdn3NSH/vCMUf5sy9VZqiFLpGPPsYSIKu6Jx+bdPZZp789VdymBcEFQWiyBSVTnpSIWDA0IKlg6zz6BtW7IrqE8abnJI1nrbrviy3jId//GH54ti77ahfzPLDeE23SqKqfmcDIhCu1VFEEH4iWFO/8NpWyBVOYgB01heLPdrzOCo2Wys6If1AdFabNwrc88tf/HNWaiRTgFHs36YTQAUXd9q+JMpB 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: <linux-mm.kvack.org> List-Subscribe: <mailto:majordomo@kvack.org> List-Unsubscribe: <mailto:majordomo@kvack.org> |
Series |
Improve the copy of task comm
|
expand
|
diff --git a/mm/util.c b/mm/util.c index bd283e2132e0..9a77a347c385 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 NUL termimator. + */ + buf[len - 1] = '\0'; + } return buf; } EXPORT_SYMBOL(kstrdup);
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 <laoar.shao@gmail.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Alejandro Colomar <alx@kernel.org> --- mm/util.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)