From patchwork Mon Aug 12 02:29:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760004 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 D696BC3DA4A for ; Mon, 12 Aug 2024 02:30:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0D5896B0092; Sun, 11 Aug 2024 22:30:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 05E296B0098; Sun, 11 Aug 2024 22:30:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E41EB6B009A; Sun, 11 Aug 2024 22:30:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C3D236B0092 for ; Sun, 11 Aug 2024 22:30:08 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id EA29912054D for ; Mon, 12 Aug 2024 02:30:07 +0000 (UTC) X-FDA: 82442013654.24.8DBE805 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf13.hostedemail.com (Postfix) with ESMTP id 2D0762000F for ; Mon, 12 Aug 2024 02:30:04 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KewOtWte; spf=pass (imf13.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.172 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=1723429736; 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=CPcfP+fHyMLz6sCkXjXj/fN1cAsrryKdxQYaf/+RRmQ=; b=NNpYgY1Ag4uc9E9ZTblBdkqm97tCYnk03uBWJBkja3v6FyUVDKsjYynwFJxYpXOq2vHiXJ pQ5m+L9dhTQgLs3GLnQGV2MtE+S4yqviVEi4YUI4yuSkeWednUzlNHWnk69CUyqvNCGdqN 0gs64sr7kVWDXd2GWlwJzLiwZIWnaCc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723429736; a=rsa-sha256; cv=none; b=jAi7GYspI6kCtNvcudI682p/8qQlIp0EJr2p1lLWpOCu2cL5bwCzYNvoRr0hz+Fd8GsO5l Ig5xp8rVgOmVwBcmNcy9X+d4KxxSOSxiG5EalIERizKmXAPl1MCy4MFCmWh8AL1zp5eh1H JsS1EJX5MQg/WiKnwA5T7/0AHbSmYRs= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KewOtWte; spf=pass (imf13.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1fc52394c92so36124225ad.1 for ; Sun, 11 Aug 2024 19:30:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429804; x=1724034604; 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=CPcfP+fHyMLz6sCkXjXj/fN1cAsrryKdxQYaf/+RRmQ=; b=KewOtWteDLDuNx6SlXzszP2mdgcLYUVBAnqNZiJdSyCFK9rASWVcooAkRiYiHKtRew 9db0p5zjI5gkbfUYI0k3G41SFlnGuWMKB6CQ6x+j50BKxF6hly4Fw+vlr3jYSA85NCt/ ZeeQHTIrU7CaiH3IDDlcLg25OdNV1ApAZF2kGIwGjYQYtqJqayUbhaDfB4J6oPk7G3Za 7kSbvp31aPDyhzb4OHZqV1OHZCw0JXb46WKHU8nnfH94YaLDYzsvMcZ+4wHqEylwstGv 97Cy8IPZLZeaRyJQjQF1gxRKpy39w7LoQv7Hb5LUHevuYtqcEthrqYkpW74/Bn2kSqk2 fh0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429804; x=1724034604; 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=CPcfP+fHyMLz6sCkXjXj/fN1cAsrryKdxQYaf/+RRmQ=; b=ZiHkbCwG2+4MRVKURANjYvfPwfKGfPrKhY5twD5c0hx2B3/R2CmNCmLgdJs2PxzxQp 7341zeGsyk3CpyYPwwKPvrEQRUZLbNvQ0kIF2FTujwYHp2PzxO+2wiGxQLcAgeSm0gHh LUuiTz7GDhSq8vkPFPCUkIRjyRyKALPGbwSkje/q4W8Ok3QxKRwLpTD/z0AyNrvUERyT gVaylx0M2Y+asgB+WQTT83YGKjNVbPWIYODYWp2HWAKAjPzqR5dQk6gCK5xxUgRt/G8k Y2pYqelk+wPU+L1Oe1zOsl1JribNNdi26ZZSOs0yQ+CPT7dCEo6CYX8cLhlKQdDKxTBT Ey9A== X-Forwarded-Encrypted: i=1; AJvYcCUubLuun+E8Nv2UvlC8f41HZyJs8xpYcgDMsM4v8mO3Hkr9r29lHIRIWEp4bLSsnoc5gz7v/9weAHnsnxdzyM7gU6E= X-Gm-Message-State: AOJu0YzteRV4wPMRFcwtpe7SD7GgtCPKedc8q8rPHT8IJsr6wZqEY0Tk nJmgK3UB/lOfQHB7YoMy3TAWdSonFrosztZlHL0ojmN2jz98JyIR X-Google-Smtp-Source: AGHT+IHWjfWk9Yvp8Xz47EmfqTOyYIqZTpLYHww0hZawPQZt6bgoSLmdtP7yW5PiZUmHJmuoGirzew== X-Received: by 2002:a17:902:ce03:b0:1fb:7b01:7980 with SMTP id d9443c01a7336-200ae250234mr122466755ad.0.1723429803589; Sun, 11 Aug 2024 19:30:03 -0700 (PDT) Received: from localhost.localdomain ([39.144.39.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bb9fed69sm27884765ad.188.2024.08.11.19.29.54 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:30:02 -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 v6 0/9] Improve the copy of task comm Date: Mon, 12 Aug 2024 10:29:24 +0800 Message-Id: <20240812022933.69850-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: 2D0762000F X-Stat-Signature: hi6iqmcrok8wg5boyhf8beapb33y1b1u X-HE-Tag: 1723429804-898576 X-HE-Meta: U2FsdGVkX181a8wUsZsAEOScbyYMjXD7LM4uLhqVON0QoFPZzPxr+ApBDQkdGbO652lR8w6gvpLakxe+8abmYQNKcNBhA8L0CUPMn0HPaaRa0k5qxlg97ugW76Qp+4qglIKp1BE8DI6V3Ke7zT77/BngXLFDi4rnrRGjbWspNk4CtW3ifgwJc/CX50f31fnzKeNEloRemPuTwEfeNW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d2HY6/C5pFbrEK8ZUynNak191Vn6SGDJ4GHdSAqJgZlfYG/PTOHWlEOMT8IE2O+zmHYTOJp16Y6sobK7EKxbga2cpVpCJXrLRwu+a3fxKTKxFuVuYqwZbXWulLprWFy8aeySw837RHyIPyOk7kSpezTshIGN7WJzDnrp+T29cZPRDbCf9S1xmBHPZO8VlJCuf7Ag4vk3qpsxf8N1NbZysPzz8HwS44BHVsgzQtYE1 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 In this series, we have removed __get_task_comm() because the task_lock() and BUILD_BUG_ON() within it are unnecessary. Suggested-by: Linus Torvalds Link: https://lore.kernel.org/all/CAHk-=wjAmmHUg6vho1KjzQi2=psR30+CogFd4aXrThr2gsiS4g@mail.gmail.com/ [0] Changes: v5->v6: - Get rid of __get_task_comm() (Linus) - Use ARRAY_SIZE() in get_task_comm() (Alejandro) v4->v5: https://lore.kernel.org/all/20240804075619.20804-1-laoar.shao@gmail.com/ - 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) Yafang Shao (9): Get rid of __get_task_comm() auditsc: Replace memcpy() with strscpy() 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 strscpy() net: Replace strcpy() with strscpy() drm: Replace strcpy() with strscpy() drivers/gpu/drm/drm_framebuffer.c | 2 +- drivers/gpu/drm/i915/i915_gpu_error.c | 2 +- fs/exec.c | 10 ----- fs/proc/array.c | 2 +- include/linux/sched.h | 31 +++++++++++--- kernel/auditsc.c | 6 +-- kernel/kthread.c | 2 +- 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 + 14 files changed, 66 insertions(+), 64 deletions(-)