From patchwork Sun Aug 4 07:56:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13752523 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 44BB3C3DA7F for ; Sun, 4 Aug 2024 07:56:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4911D6B007B; Sun, 4 Aug 2024 03:56:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 441366B0082; Sun, 4 Aug 2024 03:56:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E1C56B0085; Sun, 4 Aug 2024 03:56:52 -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 0FF666B007B for ; Sun, 4 Aug 2024 03:56:52 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 3495CC187C for ; Sun, 4 Aug 2024 07:56:51 +0000 (UTC) X-FDA: 82413806622.15.3B2ADEE Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by imf09.hostedemail.com (Postfix) with ESMTP id 6CBBE14000A for ; Sun, 4 Aug 2024 07:56:48 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=H6qfFAEp; spf=pass (imf09.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.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=1722758148; 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:references:dkim-signature; bh=jAUiIbA9M914l7SbVcGMcRn0qL18BQ5kTGC/RMGMPl4=; b=S3yofMt7V1UfuKcdGuYdZS4JxuoNnpGNboh0L6rspvFGxF/1AZG5uwiKTanq/EKLXfv3yF 1IskchTeg4jtqw0mLWtWp70J5rqiKLCcr0HtVT/70alRH4VHonHEGw4dr1nR95mlfTvWQy vVgoZwG+6ybkMsYhJ4AkSBa3X0ODkBQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722758148; a=rsa-sha256; cv=none; b=RwP4tE50pbYMiMjZnOMMmpaWeDSMXWaB4LiYbIHH0AQvzG8AWActv+8AbC/jKs7yv5LVtx l6KzE7CNHmeoue2xOKvhHSEDj5QI1rQjsXO2rgrqx9RdHA01k7yNiRGLDUq/QlxhdUlwGe AaSMiMLeb1oYSsYLOki29npm/wnm4yQ= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=H6qfFAEp; spf=pass (imf09.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-70d1fb6c108so6806582b3a.3 for ; Sun, 04 Aug 2024 00:56:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758207; x=1723363007; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=jAUiIbA9M914l7SbVcGMcRn0qL18BQ5kTGC/RMGMPl4=; b=H6qfFAEpFSxlLNCe1hNNJ9m+09iWD7Vcq46YxLYok/NaoNZHqosX4C3GKrri5w8qby 8H6wIRWQig3HTBI/c4HVH2uFVgrbu3YwPGnSL7DH/+4B/M/tEpnut5L5bH/OLQEu6fzP 6ytE/fBT/U2haaRUkjcFcT1QN6RuavhC/KBTKSnP2y/Xbr5f+Hjg6RoRcxrMVIH3rVxN /JnKTf+Oyg2TMozYb0EdobxSEpoLQcl/s3tqKqDdRP9pdzSOY58IBa00cVGh7YwgFckM cydIiWnX9WigK9CKzgLn46L0lYRjMgdhM8kV3sWwrR9TQXgDE7RpVy2fJTZIB2qXLARz 6Y+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758207; x=1723363007; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=jAUiIbA9M914l7SbVcGMcRn0qL18BQ5kTGC/RMGMPl4=; b=IavZMYhB6+KayXAgBEk1pOb6mleed5RpxPBxNm513xW4Exry+YyjV5V6i3yVTLFCW/ puBctmbXeKOsEK9WpKrponK0YUPL4ul82bm7n22pOOITUVJIKKnb5cjGF6ilEXS+6Oir 4/6KB16KiuN7l9AemZppVXHGXQOoFUnHdT4K+qgG/j7PmZrPtHICD+3urh4P+ddc5CjA oxeZ74VXb7O95rb8qeKFD74joSyt+8de84R77CEMPwQihneC1spM7CoM62cm5i6EvNhu B+SDB2JAsgslTb08TEkHhEW3AHxAcFjZg72wqzDp1QRzyznFI8EGd0xwCJXTrfUvP3UK vcJA== X-Forwarded-Encrypted: i=1; AJvYcCV6DT4sgmdZQlfuvapFtCU7hfX5mNWjyqBLMlzcpYY62wCk1j7ZO9GD65llcyGVZgHRckvtmHnQBuHVi6Dei5+W7Ok= X-Gm-Message-State: AOJu0YzWf6ZwDNPShjdc4467vzEtiJSjpShFosJwadVfANNYmzOTt5Ac H6Izg4mYwJNZCs3eSqPOOtwjoV8LGSjZAoK+HqdEktMYkNjFVplm X-Google-Smtp-Source: AGHT+IGr87hL+CR9BQ4tkSVsGq67qKnz8TEMjj1/AcYNViYHS42eo8GA1J34i40o5jKuxGL8nismgw== X-Received: by 2002:a17:902:d08a:b0:1fd:9e6e:7c10 with SMTP id d9443c01a7336-1ff5748d30cmr59016425ad.41.1722758206776; Sun, 04 Aug 2024 00:56:46 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.56.31 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:56:45 -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 v5 0/9] Improve the copy of task comm Date: Sun, 4 Aug 2024 15:56:10 +0800 Message-Id: <20240804075619.20804-1-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 6CBBE14000A X-Stat-Signature: 96itoom4t8dawj9tt1aga71n4sipyfwt X-HE-Tag: 1722758208-873004 X-HE-Meta: U2FsdGVkX19AWkC+8ZeqMYDWGzwLvIrZDz8tnJi6/rGn6inVMJyhYCfObQLNt/6K2HLZIxjsShlgH6xSEG6XHI+N545W+j8ayLaksXHHerp+W5aENXfpDGboCS/87Fyskw+76QIvs2TCcleVDzJx3SIkWwKD/PPe+WvfKONfKrw9H779A4PoW8jlPy+7Lo9ee8sesTMJuQcrSsP4DaHvFXNNkrc7swLsD6MVOQj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r1DZ4ALjamsl2turhOkZbxQRkEsGKZ9GNVZy5oCVwa3Cu7Tt9nBd4rI2l7fqOdcniVpCUUyuc9QRvsYL7UfgtTj9QlSoVbByO8VtJjC18VkJWDnxMKqwzVs/lql3e6INOtv6J60Oeg7TPZXSomSb/I4L29I2c2GEV2rPtYdeHrrv/52Kdh3LtEEEvUyCUO+cV3TT8uN5uVQbphCT4nG18Gh79EqO 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: Using {memcpy,strncpy,strcpy,kstrdup} to copy the task comm relies on the length of task comm. Changes in the task comm could result in a destination string that is overflow. Therefore, we should explicitly ensure the destination string is always NUL-terminated, regardless of the task comm. This approach will facilitate future extensions to the task comm. As suggested by Linus [0], we can identify all relevant code with the following git grep command: git grep 'memcpy.*->comm\>' git grep 'kstrdup.*->comm\>' git grep 'strncpy.*->comm\>' git grep 'strcpy.*->comm\>' PATCH #2~#4: memcpy PATCH #5~#6: kstrdup PATCH #7: strncpy PATCH #8~#9: strcpy There is a BUILD_BUG_ON() inside get_task_comm(), so when you use get_task_comm(), it implies that the BUILD_BUG_ON() is necessary. However, we don't want to impose this restriction on code where the length can be changed, so we use __get_task_comm(), rather than the get_task_comm(). One use case of get_task_comm() is in code that has already exposed the length to userspace. In such cases, we specifically add the BUILD_BUG_ON() to prevent developers from changing it. For more information, see commit 95af469c4f60 ("fs/binfmt_elf: replace open-coded string copy with get_task_comm"). Suggested-by: Linus Torvalds Link: https://lore.kernel.org/all/CAHk-=wjAmmHUg6vho1KjzQi2=psR30+CogFd4aXrThr2gsiS4g@mail.gmail.com/ [0] Changes: v4->v5: - Drop changes in the mm/kmemleak.c as it was fixed by commit 0b84780134fb ("mm/kmemleak: replace strncpy() with strscpy()") - Drop changes in kernel/tsacct.c as it was fixed by commmit 0fe2356434e ("tsacct: replace strncpy() with strscpy()") v3->v4: https://lore.kernel.org/linux-mm/20240729023719.1933-1-laoar.shao@gmail.com/ - Rename __kstrndup() to __kmemdup_nul() and define it inside mm/util.c (Matthew) - Remove unused local varaible (Simon) v2->v3: https://lore.kernel.org/all/20240621022959.9124-1-laoar.shao@gmail.com/ - Deduplicate code around kstrdup (Andrew) - Add commit log for dropping task_lock (Catalin) v1->v2: https://lore.kernel.org/bpf/20240613023044.45873-1-laoar.shao@gmail.com/ - Add comment for dropping task_lock() in __get_task_comm() (Alexei) - Drop changes in trace event (Steven) - Fix comment on task comm (Matus) v1: https://lore.kernel.org/all/20240602023754.25443-1-laoar.shao@gmail.com/ Yafang Shao (9): fs/exec: Drop task_lock() inside __get_task_comm() auditsc: Replace memcpy() with __get_task_comm() security: Replace memcpy() with __get_task_comm() bpftool: Ensure task comm is always NUL-terminated mm/util: Fix possible race condition in kstrdup() mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul} tracing: Replace strncpy() with __get_task_comm() net: Replace strcpy() with __get_task_comm() drm: Replace strcpy() with __get_task_comm() drivers/gpu/drm/drm_framebuffer.c | 2 +- drivers/gpu/drm/i915/i915_gpu_error.c | 2 +- fs/exec.c | 10 ++++- include/linux/sched.h | 4 +- kernel/auditsc.c | 6 +-- kernel/trace/trace.c | 2 +- kernel/trace/trace_events_hist.c | 2 +- mm/util.c | 61 ++++++++++++--------------- net/ipv6/ndisc.c | 2 +- security/lsm_audit.c | 4 +- security/selinux/selinuxfs.c | 2 +- tools/bpf/bpftool/pids.c | 2 + 12 files changed, 49 insertions(+), 50 deletions(-)