From patchwork Fri Jun 21 02:29:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13706691 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 E2AF4C2BD05 for ; Fri, 21 Jun 2024 02:30:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 15D238D011D; Thu, 20 Jun 2024 22:30:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0C0818D0111; Thu, 20 Jun 2024 22:30:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E2FF68D011D; Thu, 20 Jun 2024 22:30:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id B6D948D0111 for ; Thu, 20 Jun 2024 22:30:37 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 208EDC0786 for ; Fri, 21 Jun 2024 02:30:37 +0000 (UTC) X-FDA: 82253317314.20.42618D9 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf25.hostedemail.com (Postfix) with ESMTP id 5A643A0003 for ; Fri, 21 Jun 2024 02:30:35 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=YOqceFvJ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf25.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718937024; 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=GwlwwP2ml9p21cPlAav/YvSh18oChNGHXpTCNRHeKlc=; b=ZTeSvwtApOaYwvBJ1oKQ6fNrRDczzHtET8AeeNMcyE8jxB9uIIK7ltPdmUBGboV7N296ut GECWxmmsynUzT4voPQ90oEE5gCOvV5SO7uIgVmN1cKvaKgflMv1Eh2e3Re55dcJbq2q9Sg nWfaHTkpFr7RcuLJFekVyEhAURKRuWU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718937024; a=rsa-sha256; cv=none; b=RXRVUqENYhCchGU9rJ1W0EMh6BlktsMcbBvqlQdfckLWgi2cHUTwOmIkZCq9URq++F5Hd1 QA7NRxBGDn+x/RXcX1uUP/OBZ0RJ9fEU0aLKhRClgFykG9GlhrdqlkwlStaySGfcD7EXwn 6Ciz1/8ZjtjvAXAX4QMd0L78e9jtIq4= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=YOqceFvJ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf25.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-7163489149eso378960a12.1 for ; Thu, 20 Jun 2024 19:30:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718937034; x=1719541834; 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=GwlwwP2ml9p21cPlAav/YvSh18oChNGHXpTCNRHeKlc=; b=YOqceFvJc7iot4GjGuw1XOpgBVWNDsscLilXukA9YGPCc1O8xWmb7YP5JnCrDlESG/ DP0HaOWv8lGuLOYWJttFQeKhqM6gDQ8mAiJyceOjIfCBh9VD/3WRfHpg0s3dfVajBmiu bdKasHTDYr+fUVh1Qv43UcVhxyrofNosit0zbRgmu29auybuon1B9ppaZAvWZmG2e42/ nAvU1z1jbHLE3RbA6YurVfkQCzuc9iEkcYu9GhRojhXTLF1G1wcUHPSxYdtm3092YfqT sBoIC4bKvV7BGHkSnsgnjRG5/DaAyCIJVg3p0cxYlsDwylB0WNK2NvHLxbSjLajixfDm 0upQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718937034; x=1719541834; 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=GwlwwP2ml9p21cPlAav/YvSh18oChNGHXpTCNRHeKlc=; b=akwaHePdnJDwMk69E1bfrLVVR2JITgrbtV5JOlY0Zv5g/cmMZZCqW86HXQ0YnC8XPW 6b7IUjsBtvYhigAeSvZkjoygrb3tayA+P7x0Lts4p+VtdWCKIKR1m+Z/WedMy6MFupQT PwEmcvoBuWcGGJRkc/XFoJ7qnVjTrdJuUPZBVBxSM++lQAmKzNfekFQF8NWB7984Y/lb Eu8HmXSHsdGg3zYDo1lRkaZ18dY5xoRQDNxnV5EpvLJh1pH26ZIimBf467BLMQ6BTIEx l4mKIw4TWH4YghF6v18UuveXc5w5dL3BZhuEe3l7GLu0FMmBzwZGjJaMFBCroWFhuJji 5lkQ== X-Forwarded-Encrypted: i=1; AJvYcCWiV1Kpo+AapIrHIPukzJuNqky8lFDhDRYd1aA7CeHAyKKBUaa0ILVQGzgjSzVXhamDe19bhcZpBqx5pvagI+J8Buc= X-Gm-Message-State: AOJu0Yy7vxwhcA0KCQqTuSXlALEgK0p7ZjtBG6S5CjVjX7h8LR161Nmz skjVFhY/fRNYAVDHesZg0JQAnmLquWQ1lYFA5BhTUiy922198nlZ X-Google-Smtp-Source: AGHT+IHCunKAxv47P844vphuNTxBO9sCB83yDLseIyT9/+IsrkCTE3Wi2jgqKchbD0S9to2P9PKntQ== X-Received: by 2002:a05:6a20:9145:b0:1b7:edea:e32 with SMTP id adf61e73a8af0-1bcbb46b981mr9429646637.32.1718937033951; Thu, 20 Jun 2024 19:30:33 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.24]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706511944d2sm332488b3a.70.2024.06.20.19.30.23 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Jun 2024 19:30:32 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, akpm@linux-foundation.org, 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 v3 00/11] Improve the copy of task comm Date: Fri, 21 Jun 2024 10:29:48 +0800 Message-Id: <20240621022959.9124-1-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 5A643A0003 X-Stat-Signature: dhghmook5ssqd47jch9ywyr3m9sqfn8r X-Rspam-User: X-HE-Tag: 1718937035-678434 X-HE-Meta: U2FsdGVkX18Ar+tpzLqUDhKbkxEDJrGQcuLhAu50JaC/qkTL3ILrc/hVTb7IFMoYb/jBZ7YxuaVaXqjIXJUvxp7FXF97jJj0cLzUdM08Bii/BL1TgpBZWYxHcgOTRz8fWBJjdtdSNg08VPm6QWi12VjXur9LTOfqdQ0MgpsJE70KdQHXo8V06ZHVbyxNIJ+ggJbxut4znfzCYB7NRqHR/tUv++oP3GudADQ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TrqSX7WgnTJvslswrtyglnM1kimoBj04xoMTH7e/ehiFVxoNxLVjJqlNLaa6/KxUsutITOE7hbrfXjaEnC6b34WTAFBW5BnF3f8QkdZvzR3GRFy0KxJvAoe+tm+Nj/z99bDUkBybTen5Ss4ys37S1jCoAfli9tdhDjHPW1vo4GZyxyp2BaJ7Q0fLAZryf85JC8ywnFMVcHlcPfpQ3FDGOSbPT+s/j2vhMkrFiQJAofmxTfyBxAnbprIFHB7lZp/WZIry/2hcypmVUXvNxqs7E= 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~#9: strncpy PATCH #10~#11: strcpy Suggested-by: Linus Torvalds Link: https://lore.kernel.org/all/CAHk-=wjAmmHUg6vho1KjzQi2=psR30+CogFd4aXrThr2gsiS4g@mail.gmail.com/ [0] Changes: v2->v3: - 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 (11): 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} mm/kmemleak: Replace strncpy() with __get_task_comm() tsacct: Replace strncpy() with __get_task_comm() 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 +- kernel/tsacct.c | 2 +- mm/internal.h | 24 ++++++++++++++++++++++++ mm/kmemleak.c | 8 +------- mm/util.c | 21 ++++----------------- net/ipv6/ndisc.c | 2 +- security/lsm_audit.c | 4 ++-- security/selinux/selinuxfs.c | 2 +- tools/bpf/bpftool/pids.c | 2 ++ 15 files changed, 53 insertions(+), 40 deletions(-)