From patchwork Fri Jun 28 08:57:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13715733 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 13FB4C2BBCA for ; Fri, 28 Jun 2024 08:58:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A0E776B008C; Fri, 28 Jun 2024 04:58:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 996DC6B0092; Fri, 28 Jun 2024 04:58:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8106E6B0096; Fri, 28 Jun 2024 04:58:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 604026B008C for ; Fri, 28 Jun 2024 04:58:13 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id DC0A581AD3 for ; Fri, 28 Jun 2024 08:58:12 +0000 (UTC) X-FDA: 82279695624.17.D8B3AC1 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf29.hostedemail.com (Postfix) with ESMTP id 0B794120019 for ; Fri, 28 Jun 2024 08:58:10 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=BUy7dfjI; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719565076; a=rsa-sha256; cv=none; b=F7WnfNxj4KP+YPJS5KxAQnLdqDyQnQHhBPYiryZlzgJGuz4v7Uu0OqFy7ae7VLkDlKUYfu oDqj3YiH0e5KnOYD4o+5iu0eQ1iSnj9w+Cb6IgXBXiYY/lialZ8jMioo1GrqiZTYEBBO/0 +5W2YynbSMhFxZieWL83m8fOtUFavl4= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=BUy7dfjI; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.170 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=1719565076; 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=p+oKp38c6s8xaT2ZLZhMjXUeTO03kiDRVYzSyaWPaE0=; b=c7tczX1yrU1YcEGc2LzMNe+W+XTHbHk1Xkoknv8ySwzrBs//zb1KvzCl0LAUGJm8DY1Lea W9O7MN+yv8El2RFcIL3AewVk4s0nRCik4w4p8yF+1tCiuPw+NRdz6nsrxNLV4313Ap2o83 pFfL2MG8g2CQXZcfePovpAS8pPSRNfc= Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1faad2f1967so11788235ad.0 for ; Fri, 28 Jun 2024 01:58:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565090; x=1720169890; 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=p+oKp38c6s8xaT2ZLZhMjXUeTO03kiDRVYzSyaWPaE0=; b=BUy7dfjIJSxlwU0QN3iMAAbPZh9IQ118DXr2HPZ7tDZwiHdJx1/9eSnYcS0WDfla74 xQiwFEX4edGUI2qJjoE/WWcfBvm3iDCkvpu8gwsMdxANmaj9ZsqBjDV32Myczt4LCtCV RlTZbWcKAjgAfsKrsSPJPfSKkitsBgBmIU0uFL09xzAuGBtr29YsCdwpG5rITrjvTgaw ZpJDeXhIblCxJbvR305Cwr5n3FGRB3xgecsMPbw+ObN3ixo7YxBWbdmxZEmsCIWx7uRP phAANrvy7qjU2SeieUUKBbZPvukApInr7FvtzAi+6j/DkpISoU2OYAI7cXvrWKKRGzZt SKyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565090; x=1720169890; 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=p+oKp38c6s8xaT2ZLZhMjXUeTO03kiDRVYzSyaWPaE0=; b=aaBmAkcVbWjaIWl1ZrJiKq4qj5QKiAFRtWiNh4CBFO1Ph5jO4dp4fTmP9DWPN5qV+Z 9Jz77Tk/1K0DghkDYIU9ZmB/tEZ8FqixUVLrwJGOErXzR16XXQlK2ZKSW8NcpgefVqkr 9mDVdsVlpIf2+yHHFLRAJEWgqeb8Svm83mISysfciaWOWmdZmBXCN+NX6Z+K4mJbEAzO RsH6u9SqSSFfJRMRiSCf8tJ2oOFPQB9WPcUC2AjE/LmU/stLrAFjI3gw8GZcOW/dq9mx +ZhvZ6dscptT8c2ZmnOs+qS4q5mViWNvWnkF2gWDuIq7cM1phel0/ByBhNvD/t/LzB/7 lvQQ== X-Forwarded-Encrypted: i=1; AJvYcCVbGf3KbbQQ52Oi97PE7IAYqmaYQKHF2NNg+qcwqtSo5WhBDdYkIPK8TPM1e8uOON8AIH/6ZTsP3QzU06G95722DXM= X-Gm-Message-State: AOJu0Yxl0c4QYcrOGkRNjx2ByqbndJTsLILxc6w5VRgGEkDYf3Aes+H5 DOGi3xYas3YJHm9beRuz+Ud69P/zdKq2cFBTjUtiM91kl19VCKjM X-Google-Smtp-Source: AGHT+IHW5n7W+Hsi/3tpD3rJbrf7D7rBby69hy4/8oAP7PWGB8JzPIs+Xaa9S3prTCBl6bkFGCqEaA== X-Received: by 2002:a17:902:daca:b0:1f9:f6c5:b483 with SMTP id d9443c01a7336-1fac7f0478bmr14804095ad.27.1719565089631; Fri, 28 Jun 2024 01:58:09 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac1596728sm10270615ad.256.2024.06.28.01.57.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 01:58:09 -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 v4 00/11] Improve the copy of task comm Date: Fri, 28 Jun 2024 16:57:39 +0800 Message-Id: <20240628085750.17367-1-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 0B794120019 X-Stat-Signature: xj34gm7nexgam671bqzb67chzs1ee6ug X-Rspam-User: X-HE-Tag: 1719565090-547149 X-HE-Meta: U2FsdGVkX1//27fVoy4oOcjrPiPe7xgbNscitayKlJouUUMKLi6zoIEgzPIVDpF41+POzSDUOTYnK+ESh04QVli9YXo8hqbl92uIi9FeS1sea6EdRBOnjCQr3cPH3BPxd6sdv/N+r/y8xfHdUiMhy9cPn7/oLIJBGZmDjut3rvu2hy6vh+IjemaQue5TMoqnY23g2CFyg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Ms0HgFnS7FTk2Y44rXyS53owBf3q0oYq0zW8/hAN9xGIlfvK5vU1fIw07Qz/wIiUCbmI+jG071JuVyvPsCfCCpwUBwhunw5j5YabU45XchNZc3aGH5G0AS01n0LZdIi+m+5A17OaiGxewqhOUiBLnZbSLNBkx1Ncc/9EXNTHq7svyG4W9b9UIcX+LUzKjela4zqo7kaTHO7ZoPmpzyftBXOKyW2Xje1weeY89HndZ10MtTiT+KkT 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: v3->v4: - 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 (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/kmemleak.c | 8 +--- 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, 51 insertions(+), 58 deletions(-)