From patchwork Mon Jul 29 02:37:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13744219 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 C3D7FC3DA70 for ; Mon, 29 Jul 2024 02:38:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3C8066B009B; Sun, 28 Jul 2024 22:38:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 377A86B009C; Sun, 28 Jul 2024 22:38:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 23E906B009D; Sun, 28 Jul 2024 22:38:27 -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 0594B6B009B for ; Sun, 28 Jul 2024 22:38:26 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 99E351602F4 for ; Mon, 29 Jul 2024 02:38:26 +0000 (UTC) X-FDA: 82391231412.19.E1497C5 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) by imf08.hostedemail.com (Postfix) with ESMTP id DFA76160019 for ; Mon, 29 Jul 2024 02:38:23 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=CabCGQ6h; spf=pass (imf08.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.46 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=1722220677; 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=BoXVnJDl1DBrc1Y4CS2cvJgm86xrff8Hli3dvn72knc=; b=P2G56bL0bMYlTkaWsJWB7wQ4ReHQLuzanQvK7Z4Yc5qpMMLa9dUCUHVXY4pdltCylA49UK fnSYlbAOQ1S5TCuFzxhBwLDDr4Dpxp6uxgWJFCW6IxoUfEcYDhdq+gze8kXcJUlFbuiW/6 b8mrWKrLDTpqnAYUqj0oXokpqscT8EU= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=CabCGQ6h; spf=pass (imf08.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.46 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722220677; a=rsa-sha256; cv=none; b=pVoSMErlfT9aZnyTc9rxnUCiJJ3WbIbnN4F6HFrzSNLExukcmzxt2bdDLOWtZ4QSeCvn/+ RWuqOcXONPTJqx/IPThyvF/PRU/wC5CVziRLyysLufVbhyydQ0+0el617J0jLwWj0lcJ6H qTn3bN8c3jKmfFsDfs9857ykh5J6EFo= Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-2cd48ad7f0dso2211882a91.0 for ; Sun, 28 Jul 2024 19:38:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722220703; x=1722825503; 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=BoXVnJDl1DBrc1Y4CS2cvJgm86xrff8Hli3dvn72knc=; b=CabCGQ6hQyoYLfAfTZXRJTvejAtupuE6LIUMLtFDc3W4s/pMVOCMIlzpySvx/J9Hfi kwC3iNdDP7lvvTWsFZ8fyqw+rJcGUXQpQh9j6ZcR2Y0mgQ+tXQOuKdw7PxJYP2u6AxOU O3T/P3DfwnnMd1YbdziWWeQlPRoMTHPclx+4fbzciT5rN3Ew8orNzPF9+tx3kvWgE3Sq 76GX7qNjt804FC+ElCiiKQjTkFHE3v0mCujyTp/hCrfQz2S9qO3wXXFsIxAqU/7RvF/o xyl1bc3eKqrRMc/h5szNPLHwWetdoOsgb6wFckuxbbsAY8h+nDg+/dZ5EPX+i5OVCB4u KPlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722220703; x=1722825503; 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=BoXVnJDl1DBrc1Y4CS2cvJgm86xrff8Hli3dvn72knc=; b=H9r4QhwbRoA/xzm3jmkHwpd2yTmh64TyeMF54WCw6ooWxsXkwwoFrDVSXJ45fopbr0 R2g87IMVXX2r3zlQEuFujNapPisuxT6MS8qb0j6VQ1t7zbxxUyJnzv49ZHOkiv+U6sF2 OIoj2t1Zec5DvRS8rcccL4yxeZzHq16slU+msSEFqVBMv3JI5uNEV37YLfLZEWY82fkj NCwTbnSxBm3hiaICcNeAlAfNHI0BDK3etTo54NT0+S7SvKbYt38/pAd8T0n93KePEuhu JyKy6z6yHL2abO+0JL5FSm9Xt2D1chMNSx2hpvZuqWyOgGLkIxksFMsQaFIUCw3FSeC+ Y4Hg== X-Forwarded-Encrypted: i=1; AJvYcCV+v2W1mmTymPLiVm3vkjmWIazAyF9qMPWEE4QlgppEa/F1YXKQXPV2nBaXjwYLIAyrt2I9t120+IG5vwR/hU7GnHI= X-Gm-Message-State: AOJu0YzbpGfQIPQ9YQvNSjTYguY7LyU6kFluyFqaECPliJEV6Hzh736N vv/Hk2bS85mVnN0GoGeAohKuQ27noSnaurZkVQ6dk6sr+mugfEUv X-Google-Smtp-Source: AGHT+IHeVJuzyb2EwGW+50qWRtjvsFUnfcwe8AV+hJIj0aUlJNPkNbWdN0SRhTofBR2ODO0UIaMdgg== X-Received: by 2002:a17:90b:384d:b0:2c9:7f3d:6aea with SMTP id 98e67ed59e1d1-2cf7e5f4b7cmr7481617a91.32.1722220702568; Sun, 28 Jul 2024 19:38:22 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.31]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cf28c55a38sm7332247a91.10.2024.07.28.19.37.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jul 2024 19:38:22 -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 resend v4 00/11] Improve the copy of task comm Date: Mon, 29 Jul 2024 10:37:08 +0800 Message-Id: <20240729023719.1933-1-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: DFA76160019 X-Stat-Signature: 41h8s1fyx36b7y7ajjc18qzd5fwfisx6 X-HE-Tag: 1722220703-241826 X-HE-Meta: U2FsdGVkX18k01zyK5ost1GLnY9xNV2NIHSSoIjcPXRXHxTEdrhQNOKCHDYy08ubb/Uj8NdAp4NWAX4megAKdzjh0vdv4Zdxp/0fSParS6JdYMRPrVOz+uM7SFfbNm+isE59XB0LfB9BhbufPEXHG5pzKkBcf5M5Soan6XIM51AjR3Y4szm6cdKxV9BF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kdqS3mP4sQpRcPQUdPZyoWRNW7RjeRkg+CKklNFLla3Z8GY5EcpeRh9kN4nLpKxlbgpeHpaT64ZInBJQ8pOc1z9GwoQGyxZ2SKKCbMqhQO8tpk8BQV8R5tzr69Ey4ZKhXJ8RUNdtC1niSYa0eRhVvbjqrTmP9rFdMexU13ioTIQAzXgWRvgN2cv4+JwRIHNG7HMHtd1Vx/ZtpVwsW46FlZ6x40xlZ882NXKk0qBwbO5Wxz0Hj/mjMYJVUYWoZM62ksZgc5Si8md5Z4I0yY 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: Hello Andrew, Is it appropriate for you to apply this to the mm tree? 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(-)