Message ID | 20240729023719.1933-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 37D50C3DA49 for <linux-mm@archiver.kernel.org>; Mon, 29 Jul 2024 02:40:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C7B466B00A3; Sun, 28 Jul 2024 22:40:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C2AF26B00A4; Sun, 28 Jul 2024 22:40:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AF2B86B00A5; Sun, 28 Jul 2024 22:40:04 -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 926C36B00A3 for <linux-mm@kvack.org>; Sun, 28 Jul 2024 22:40:04 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 42D791401C6 for <linux-mm@kvack.org>; Mon, 29 Jul 2024 02:40:04 +0000 (UTC) X-FDA: 82391235528.24.8E38E8A Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) by imf22.hostedemail.com (Postfix) with ESMTP id 685D2C0003 for <linux-mm@kvack.org>; Mon, 29 Jul 2024 02:40:02 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=IywBYpxe; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.177 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=1722220732; 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=8rSpj4+mjUH2uIm/J6/DitPwV6C8fJ6cAywoYkpJrFY=; b=KY5xgkv5RK0LmASaUGK4k5JqKF1Qsvw2vhq9E7HENsuS9Fb7A07Huq49YzpyPfLKlk3csm RJUjlGRwx5SvZGX5/GsjEMZoVxV02I9ri7g4yfG27ben6BiX3mWMoZLo5iYEZ0AapkVWPs zqLimBYaZ+SnMnGx8w7XPsajZwXjY7I= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=IywBYpxe; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.177 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=1722220732; a=rsa-sha256; cv=none; b=ycYPEii/5WVIoz14B7H8PHbqjUXfvlTBfFTjj2/bt7KV10OT5rnii4AwDMYH63dG83wG5N gKHuna8OOvWLR5XsNlzH1rHm8BAzikEtQcqeJs3xg0srgvVw7YjDB8ySMBVW8p+PiZGW+b BURr3mHCw3KuclfdYhxRs3T9/mi5xX0= Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-7a103ac7be3so1445404a12.3 for <linux-mm@kvack.org>; Sun, 28 Jul 2024 19:40:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722220801; x=1722825601; 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=8rSpj4+mjUH2uIm/J6/DitPwV6C8fJ6cAywoYkpJrFY=; b=IywBYpxewk39XDOR+KqqJAlknasCulNgi11gogPy92fFoJMtR9OEV6zybIcDaMgsZ7 HyAI4+3AXCSJd9BHfyuODgZtkPU6cGKYuGK4aSXsoEZ6a4vXK7TpEPs+Lzhk1zwSrMZE eOqU3ZLw20EssFBxBi1lEckfzQI/EvlMPropSzH8dbbkXbS8zHQF3Y6ydszfKVp89SxX wO6DqNv5UGUHpq/yCdwxBkcO8poLrQSZjU/wEUL9kpJtLKP7RXfwrMvxozfa8WmH7KrG Zv7vzq8/PuqT+UctFqCCrO1Bz2SmcmYKfjdwiLE5sO3Rw4p8zRi8Ry+npeK4JwOWiukH TIQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722220801; x=1722825601; 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=8rSpj4+mjUH2uIm/J6/DitPwV6C8fJ6cAywoYkpJrFY=; b=J3iU78JMeby/+vm7HRV9M7Yt2EHHpvv9n+a/+FpZIof0r1Q6fR0/+6UTtfNkx1FSZm 7ZuL2GvzIdWntOpWXnAmuyAKvoqte3+oWZmnCaVJfRpDUz5W6TcigjQuhLepPDv/FoT9 W/oNkXHmvGWVmUld+TeHdlOKEMj/rB+Fav0fecbhsDa3YfS53ZPueAoxC6LwoYO0TPcv zW4ft2vRyT2F4d4wrjZA4t65kmfxuH1Zg9sSLWFMwlW7EpUf7s5qHHBAhjm00JuJjVxe fRPHBfUhIcSu49YCVSW9g6G/VHQ9kqLgaM/N1ZPPymvmKMa+VKzOmj+2KxEHt4emLn4n VrRg== X-Forwarded-Encrypted: i=1; AJvYcCWIPh7P+WuE4KPR1hRbeQARznmFN//C38VZC0Xs6yfw87Rq51E9uTlpRyP94MLe7C0zNbAxpQ5xhJ27zdqP8G/B+7U= X-Gm-Message-State: AOJu0YxpDFjzsSsWHlWLHor2wfoekq77BXTwUn971jLU3rpH9Zqwnv1S Hx3andM2Ka1+41/RDnwIYnAKK1N9vXvGJ72gY2ijkPePdoAzVWah X-Google-Smtp-Source: AGHT+IGTYP+13dgbaXUTcOaiyJPHukRZsfD9pPSgLtBCe0th4rvzpLqZDHRAsUFno3V6NU5AaOlIiQ== X-Received: by 2002:a05:6a20:a105:b0:1c0:f2d9:a452 with SMTP id adf61e73a8af0-1c4a12a2e61mr4874235637.13.1722220801130; Sun, 28 Jul 2024 19:40:01 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.31]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cf28c55a38sm7332247a91.10.2024.07.28.19.39.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jul 2024 19:40:00 -0700 (PDT) From: Yafang Shao <laoar.shao@gmail.com> 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 <laoar.shao@gmail.com> Subject: [PATCH v4 05/11] mm/util: Fix possible race condition in kstrdup() Date: Mon, 29 Jul 2024 10:37:13 +0800 Message-Id: <20240729023719.1933-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240729023719.1933-1-laoar.shao@gmail.com> References: <20240729023719.1933-1-laoar.shao@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 685D2C0003 X-Stat-Signature: 7xauf8t6farby7awrq5koc3mq18bsprq X-Rspam-User: X-HE-Tag: 1722220802-855844 X-HE-Meta: U2FsdGVkX1/kTWqJD/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BjMvlYUbMQvcyyujZk3/0GwmkSxkuyNUncXObJrhZQ7WMPB81DpzRHyFhMdg7SpXKqcriaSRYX2N5jnDguU4DiBKIiyD2axLW2BwFtz+kqTdeL2Q9FjXdAO3LgKbnlLtDl2MNRpOqytb+ZzjvRbMioXoxLvhePhOPvWmDuFXEq8qnve+lqWF2znZqUEx3W2QFSAUlUOb/NNH3Bkz/2EOVwL8nQydHPRZ0DypIRcUwOEpxKypOPIcKFB52V1bjgqvUs0ThHxykvRHSdwC+IynPGqsK4iEB5D8Gmo3DE79iRuySKtaZsgxrS4iCbDKP2OwMmE2da4vT2zSmpo+1br3B+brKSCBHpeCe82TW104HzBp6Kia5TVDcCU3cBlFnva9fTJOHHKk1+O4lVAAN8= 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 c9e519e6811f..41c7875572ed 100644 --- a/mm/util.c +++ b/mm/util.c @@ -60,8 +60,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);
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> --- mm/util.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)