From mboxrd@z Thu Jan 1 00:00:00 1970 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 9CDAFCDE024 for ; Thu, 26 Sep 2024 17:35:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2069F6B00A0; Thu, 26 Sep 2024 13:35:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1B6D36B00A2; Thu, 26 Sep 2024 13:35:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0A51B6B00A3; Thu, 26 Sep 2024 13:35:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E25086B00A0 for ; Thu, 26 Sep 2024 13:35:47 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 944A31A019E for ; Thu, 26 Sep 2024 17:35:47 +0000 (UTC) X-FDA: 82607591934.25.3EDE2CB Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) by imf28.hostedemail.com (Postfix) with ESMTP id AE9EBC0019 for ; Thu, 26 Sep 2024 17:35:45 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="ITvXJm5/"; spf=pass (imf28.hostedemail.com: domain of andy.shevchenko@gmail.com designates 209.85.167.43 as permitted sender) smtp.mailfrom=andy.shevchenko@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=1727371984; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xtR89bH5Z//239xnT5kCrki/umqFGhowZSxev9N5vn4=; b=Y400pVfM7E1+FrJk9QaMlg8pd5HYR3Ef+EhX2cmoRxQ1N1EJIi0XJ5+xYWPA3UTxWA/RsS a9gB5BmRzyidvWaqxCA0lsut9u0nqYlUxIOSoUQQZg0Uxec2PBNJRVqPIkd51+eaEDdlKt dUTJ273R++wQztq2Q7XN6L09IxadNvY= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="ITvXJm5/"; spf=pass (imf28.hostedemail.com: domain of andy.shevchenko@gmail.com designates 209.85.167.43 as permitted sender) smtp.mailfrom=andy.shevchenko@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727371984; a=rsa-sha256; cv=none; b=BtmSDyUhJQkzUt16noP9HGg0viI1Tq75mzaKa7Q17uM1OSgo7KhLBaB4qpxbwFFK4B59U4 Sggc7T07huS4AmGYwyNTNrE2WpSDJeovPejx3sUsp1QzUei2TDafrVkwLUN6D6ujo0rQpd BMmO4FvLDVpSbf+W0Yb3hHnqY7znpXk= Received: by mail-lf1-f43.google.com with SMTP id 2adb3069b0e04-5369f1c7cb8so1744137e87.1 for ; Thu, 26 Sep 2024 10:35:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727372144; x=1727976944; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=xtR89bH5Z//239xnT5kCrki/umqFGhowZSxev9N5vn4=; b=ITvXJm5/3bRdJ/M2uqPGJ8HsNNh2F8A6N2yFqMZTWicQ+Ma6iWdWeics+H1McVGY91 wBdLI6m7RuMDOG8U2EWRlddhsKtnWDNFEblViyubzY8X2irGf8K7KkxvowhYU4IMALn4 6JgbDXaiAMFBnt7fA8U1NUpsN2AJ3psNiOiWmIDBSEFeky0ccyiC6rQvKD2UcXVcxk+V 5ddhJjMcHkAxzITllXT4kl9LaPYxlRIhj+8KfqT5UQV1BAPgYerrvFVF/yqSHJAokGQE 0NJ3LmmGjLF6TJhkPE7n/mzu7NjKYu1NKREIddYVw8wjOm8ZoWcI2zLN/IZpB6UzYUVq HtpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727372144; x=1727976944; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xtR89bH5Z//239xnT5kCrki/umqFGhowZSxev9N5vn4=; b=oj3kP3jjPaS++iLjhasrqXyR6P3eerwzAn2ZyFiDKw8OW980i9V5dWhC1/kd+/2DxS u9HGVUHzwuzC41D3eaShXkrlrLm1d3cFek+02Bq6PUA3HSTZKNOSXJgiPDbRGoBvy+jD sbJqtXVOApJUwBYhmbVf5JNAiyJCbkhzwe7frK9xJJ61IwLs4Axr0reuvkPlVI9B2Xc7 VugOtpWYChANil+MplGE1ieWJr4vfewnj/uB4cn+tUbNsioU+UVoPTJNTwlQXAJ5AMZU imk0maTAzIxksJ5RGiD9bnnsiPSOaPD5c/h3qCzlSmABl5OaYHd+askSXDDZbakQCp5V X76g== X-Forwarded-Encrypted: i=1; AJvYcCUXHsSelmlhuBWjqGm5EdyDEuWz0HkPpX2IhNzoy9UHl5MxyDeFFoxG+p8boIiszP8jUEaZH1kMxg==@kvack.org X-Gm-Message-State: AOJu0YxToUmy5HCWHx1/j0ySBzer9fkOoH2f2BBkvEx2TxPmuRDyGmyg 5AeSaHrj14wWCJwYhRLdE76XWwzyXowPak5EkXDB9OJ4IWM2nkebylNRI7UaMUbAeLPgRau13RX Y6XDpzf66iDF08cj52/MmHCwtxgs= X-Google-Smtp-Source: AGHT+IHf75DxkntTvBkZGDev4x95K+giFUe8FZhnZLvsyGMJj8v524+ofSRAoRSBODfKCXI8yelsxdzzM7LxitSB/UQ= X-Received: by 2002:a05:6512:2310:b0:536:536a:3854 with SMTP id 2adb3069b0e04-5389fc70f56mr217205e87.60.1727372143545; Thu, 26 Sep 2024 10:35:43 -0700 (PDT) MIME-Version: 1.0 References: <20240817025624.13157-1-laoar.shao@gmail.com> <20240817025624.13157-6-laoar.shao@gmail.com> In-Reply-To: <20240817025624.13157-6-laoar.shao@gmail.com> From: Andy Shevchenko Date: Thu, 26 Sep 2024 20:35:06 +0300 Message-ID: Subject: Re: [PATCH v7 5/8] mm/util: Fix possible race condition in kstrdup() To: Yafang Shao Cc: akpm@linux-foundation.org, 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: AE9EBC0019 X-Stat-Signature: 78goeroktogtj8t4mwfftd86548aoc67 X-Rspam-User: X-HE-Tag: 1727372145-998080 X-HE-Meta: U2FsdGVkX1/jwSR7bAoPaGVStVdw2aYqkRRfJkgPem1MweyJOt1M25LZ/xZylQs0rNinNKAM2CtPez7ZXs9cBZ5PIDQQVqtfwJK38sszkkfLH4SvkQNju/5EsVugfpC0Yn+p8qiKs1u737i9ZfLD0SfQpmA9I/Ez0QeqV4KTwJOKnPzwRhWiqCOJTRliT5uNtwEpBoW0zK817rleF+gWnZZQ1PeIbsnbzvZCRuuPQERRG+5z673XQs+/2USh51c6OJuvZjVYPYU3Bts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CTA7jGdS4dCftMvJ+Rk/BwFrcdHQQCQLZbtWF3HDR1P09bh4UDW9L1nXNB6JumzlH/jy09pK4Z6SThaaed3lm81q6N4FJE4HxHb X-Bogosity: Ham, tests=bogofilter, spamicity=0.000005, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Thu, Sep 26, 2024 at 7:44=E2=80=AFPM Yafang Shao = wrote: > > In kstrdup(), it is critical to ensure that the dest string is always > NUL-terminated. However, potential race condidtion can occur between a condition > writer and a reader. > > Consider the following scenario involving task->comm: > > reader writer > > len =3D 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 calculates > 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. memcpy() is not atomic AFAIK, meaning that the new string can be also shorter and when memcpy() already copied past the new NUL. I would amend the explanation to include this as well. ... > + /* 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. /* * The wrong comment style. Besides that a typo * in the word 'terminator'. Please, run codespell on your changes. * Also use the same form: NUL-terminator when you are talking * about '\0' and not NULL. */ > + */ --=20 With Best Regards, Andy Shevchenko