From patchwork Sat Aug 17 02:56:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13766922 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 98A86C531DE for ; Sat, 17 Aug 2024 02:56:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2FB546B03EC; Fri, 16 Aug 2024 22:56:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2AC206B03ED; Fri, 16 Aug 2024 22:56:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 19AB76B03EE; Fri, 16 Aug 2024 22:56:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id EEBC26B03EC for ; Fri, 16 Aug 2024 22:56:50 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 639CBC03DE for ; Sat, 17 Aug 2024 02:56:50 +0000 (UTC) X-FDA: 82460224980.08.4416D9C Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf17.hostedemail.com (Postfix) with ESMTP id 8D98F40003 for ; Sat, 17 Aug 2024 02:56:48 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Cc3XfaYh; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf17.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723863394; a=rsa-sha256; cv=none; b=sWJyu0CuA/SA1th8MKuMSS4nHlBixgl5uAtC/ij3ECWiSwi12ig6s/lgyk+3tMMPXtGHd/ JZSkJLb7kM+c6tZhGOCnASYruP7RntTVe6kJB3wHB1gA3Zi8UorWDYclBVP1BbsrPqI2Lv SCcjEKaSXd3hnco30hcf61eVp6IE0yM= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Cc3XfaYh; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf17.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.182 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=1723863394; 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=rKgZtG9uOqD3DFxRKcQkjZTQLILEzoQhivhfaU9Nqo4=; b=ffwoANRYF34Kj2MOaygsBK7KTAwcyZ6DylhEV6l0sesrHEOlmWyW1GDahKlrxEkqVk/xK9 E/w0FVT1SAKm52uYfQ3mxxpEw5IFwq7JhdEhMDNMJ3fq8DwQKZ1mc/XHVCvHjzIhnp3zI3 oapFlE1J8otnyqihvXIdvUTnPFBMUqA= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-201e64607a5so18742265ad.2 for ; Fri, 16 Aug 2024 19:56:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723863407; x=1724468207; 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=rKgZtG9uOqD3DFxRKcQkjZTQLILEzoQhivhfaU9Nqo4=; b=Cc3XfaYhxMFo26ikQj5LuWOiV1YwbIpBxFTpgZcMsU1MPNq2T54NQPvVVLdYiWeIWX s30fn0FF74vm/130AFz4wV/ENc2B20jH3zMqBB6HwIqwN0wtno9A5wHcjNMlCP6vW0GR f8vXKijrMVwLQbZREwl/kzmAnwJTFKcZvqyonFqq3xiaJYkNGWAgUTr41heilaQdcQf4 G6wgrsAxElMDV9XuulI1BTcfmETXwrtCs2IUHcHqE/9IVU4m5JD0YGqFWpP+EPhSfdz5 n/AhCs/dMU+DKO9ovgzxz/yNB7jldk88a4lFXaq/uip/9cHcZ2zHpr9mMg6R6Mirkm1G f8Ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723863407; x=1724468207; 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=rKgZtG9uOqD3DFxRKcQkjZTQLILEzoQhivhfaU9Nqo4=; b=lWZK/jlQMbWSUA+ffWoOQeYcullmtytUtfaM9VtbU3oJSpa3ez+zp23q/dflYX4CMy m7ZkP97KLRCzt4HTv1nZ2f5VJIBAbVNVT6gGE/sSGnjhPpPQk6OSim2VngYuqZPXhPPU nV+xKjoGD4E05aNfDg8R0NGN299Oq2iwhj3n0VIt9qnxQWg0t4+ON7eNtGzSz5zQ01Sy t8PHOLljI1SrpktR/TH1NVwKOWMyPDJlruttvOIgdWP8leSvcNkV7+A3pvoEVOL+Q/mg TCVV+KPN6VRydC1fYv8iu2KKT0S37vqeFklzHEJ2w6s/M4osAAqHEwN8RSYOTEKXMwO9 UPPg== X-Forwarded-Encrypted: i=1; AJvYcCUw4ZAph1+AW8QogcBncWiQm8FNKQ1G3esBoOc9ytitTp5Rq9XfzLGB3X5p7rIr5CH9Roo2AsiOddKx3+u7eMIXI0c= X-Gm-Message-State: AOJu0YyyeA/zi7IvNaMhj3+PvCS7frOsjlkp3eQ6hQUvMLcQKKmHI6sT dXfSRquW7qzUMqkn2iKAnx+xfhyaYaVJGA0QLsmfTnWCkLvjoZDu X-Google-Smtp-Source: AGHT+IEbPGpqoRSYF8jBgPK+pTBiTQRphrHbJ0rUSgdEvQgjtWO3Wff4DwqW4cJKQwtvK2mv0b1kNQ== X-Received: by 2002:a17:903:124e:b0:1f7:1b08:dda9 with SMTP id d9443c01a7336-20203e50e27mr63144585ad.8.1723863407171; Fri, 16 Aug 2024 19:56:47 -0700 (PDT) Received: from localhost.localdomain ([183.193.177.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f031c5e1sm31801785ad.94.2024.08.16.19.56.39 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 19:56:46 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, 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 v7 0/8] Improve the copy of task comm Date: Sat, 17 Aug 2024 10:56:16 +0800 Message-Id: <20240817025624.13157-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-Queue-Id: 8D98F40003 X-Rspamd-Server: rspam01 X-Stat-Signature: ihpdobf77449yezqt1rm9skqfiyg4ms5 X-HE-Tag: 1723863408-807522 X-HE-Meta: U2FsdGVkX1+2aY08TanSgcLs7akUQxaPHZ0GMC7wn2w+e5blpvCLPPn9M2WB8NWMMCa+aDggneApul5BlXGDwclYaW3z4pUmW6Ry2roKMFCroy3OoPPV5GiSKlAGrmLp/4sZSzFjTFl26vHGgIvs3ltfypGZqWVgs7QVy36r/J7lELpHVUwrL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3dLkDh2RVYaE0E/MBQPtfOyGxJ0Fh4QGb958+fQDXcyB9l2X1RSxuG8tZ7uZ/M4d6hFFWXLhkBn2jft3neP5cltVYWgBcmpPXwnU6hEhqe6DFsaYIPkkGu/6BG9NIfPucfeyf8d2iRh0yke/OF4FoLWFtvuWHnCz32Sd9R7Jy/T+p7zo1lgO2MXqcmGUvnzS8W2Pf/7a7GIzN1Q4Fjr6xcKdo+8pbJoKv8xzo/Ua48/F3uyaz88oNJRh4qs1eH4CwGbnbMZ+V+6Dy/akwwJVX1p2tuICHFBLLsKJ7Q== 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~#8: strcpy Please note that strncpy() is not included in this series as it is being tracked by another effort. [1] 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: v6->v7: - Improve the comment (Alejandro) - Drop strncpy as it is being tracked by another effort (Justin) https://github.com/KSPP/linux/issues/90 [1] v5->v6: https://lore.kernel.org/linux-mm/20240812022933.69850-1-laoar.shao@gmail.com/ - 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) v1: https://lore.kernel.org/all/20240602023754.25443-1-laoar.shao@gmail.com/ Yafang Shao (8): 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} 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 | 32 +++++++++++--- kernel/auditsc.c | 6 +-- kernel/kthread.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, 65 insertions(+), 62 deletions(-)