From patchwork Fri Jun 28 09:05:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13715776 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 D4B55C30659 for ; Fri, 28 Jun 2024 09:05:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 50D816B0092; Fri, 28 Jun 2024 05:05:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4BC486B0098; Fri, 28 Jun 2024 05:05:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 35CBC6B0099; Fri, 28 Jun 2024 05:05:33 -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 188A76B0092 for ; Fri, 28 Jun 2024 05:05:33 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 9F8E2A049A for ; Fri, 28 Jun 2024 09:05:32 +0000 (UTC) X-FDA: 82279714104.20.9CD5729 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf02.hostedemail.com (Postfix) with ESMTP id C5BD98001E for ; Fri, 28 Jun 2024 09:05:29 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Lbd+jj+l; spf=pass (imf02.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.182 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=1719565512; 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:in-reply-to:references:references:dkim-signature; bh=/dYktotwfBu6Roz80A5YCXaIPxsklnKpN0JVw9YLBJ4=; b=YmOM4JwbOvs2rroW1kQMkEWwphV+mSj9eHsO/TVyDMUtXyD34hT4+n1cC3eD1X5mg8EAbB 4yMWbPzuJwfDNfKEsXwO4rLlThu7fP/O0X9cKPZxf20Zw+cYfbmPfH4+toqFUB8NSvDQ4M OoVHQE8UpepYgdBd8/qkEornYfOGMeE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719565512; a=rsa-sha256; cv=none; b=p/ORoaUpFVLmgp7llUP29zQfyXg6OGTk/ONL3uhqh7dnPv04PiCajkuLcrKO4p+9mxCrm5 iWTsG++onzRENhdbqvoxnzt9qvvbq5/tjvMnQ8MI3urhF+SBkTS0G65pVsqWUj3xoIZD9e XuHwoK2o/LrBiWfnLh6LQ8xRUWj0UJI= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Lbd+jj+l; spf=pass (imf02.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1f480624d0fso2168415ad.1 for ; Fri, 28 Jun 2024 02:05:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565529; x=1720170329; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/dYktotwfBu6Roz80A5YCXaIPxsklnKpN0JVw9YLBJ4=; b=Lbd+jj+lZ5J7JFwhDZAI6qQhgeO7F2UyAwWdrMbg+R+/3oCJunNKC/U232TMMWeeM8 8n/0esqwHlHEyPSKEAIZ8YYELhO98LLBpzSaJ/jyVjYGM4UFSeKrd3embD/Sto0M35Ho PcLSGo0JUF4RhE0WD3/OQmdrRCSdtyWgi1p/6ZUHLyfiwjvKOmTGanrK6rSGndQcR29G xYXas7vny1E2rJkLsehHduYvp+s86P9SnAy8V9M6g6d2g5FwVvZ555eOBghqsNFiS2xs ZhsBEamuU73PjlFT81kfcR67zg/qH3jntJdm6NNlTOSBeCklNVXUNBWzPKWykhsNvs9L Q+ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565529; x=1720170329; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/dYktotwfBu6Roz80A5YCXaIPxsklnKpN0JVw9YLBJ4=; b=fMsLJJhq7x1Nk4w/wehM/ykr34zzZfl3CoS/z3xqHimFKuQNAE3piuZZfsfc7jRozl /DpYdDbiydWAvua12u34g34rumxQLn90Mu9e6iMZzwtZj5CL0QJjZDvvcCVRGHH1qe2I xwHEhQaVKtJvXVXeqOyo8wtILPZid0TkmZ+CuKK/1iYcLw3eGVv2QC+8MMPF5K57Avz+ 6sGzMIi2QqinvWtS9GqK6gy2IUO+L1xqhcnYrsJoDbEYD49Q8zH7caHwR0W9TIbROA07 EK6R45WzfOQqpaxB7cTRtjOYRc4KCLasTMirdGjEPq4di3qyAaBmn3lRKiV38X/m0rpd 5g8w== X-Forwarded-Encrypted: i=1; AJvYcCUGHF3G9EYfRlnVGYEZx+a77KYWVQT0kwezUjgv1tcIz4FlJOEeW/hpSYwV4EtTau4o7NOG7ITH/bPHUDomyRXB5BE= X-Gm-Message-State: AOJu0YwaRjvy4oniAWI3v+FWRGNYCIC4iKJnr8m+5gCDY9cGOpaFs6lt mVey+rBKPPYX6OZJyph3Q9zTquIbObnZ7LrYCXfduZh8MuBVJCYs X-Google-Smtp-Source: AGHT+IHq12/wr4f/48x7wfiSbegh8HPUA/2boo4oAyw8ihJMGtAi4AloztlVccxiTIt0WgGf0DVrww== X-Received: by 2002:a17:902:eccf:b0:1fa:3f05:20b2 with SMTP id d9443c01a7336-1fa3f05244emr164534885ad.65.1719565528645; Fri, 28 Jun 2024 02:05:28 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.05.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:05:27 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org, Alexander Viro , Christian Brauner , Jan Kara , Kees Cook , Matus Jokay Subject: [PATCH v4 01/11] fs/exec: Drop task_lock() inside __get_task_comm() Date: Fri, 28 Jun 2024 17:05:07 +0800 Message-Id: <20240628090517.17994-1-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628085750.17367-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: C5BD98001E X-Stat-Signature: hwaoqa1thpinetr6eijuz9ahaqx3cpnc X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1719565529-129665 X-HE-Meta: U2FsdGVkX199Bm3ovTBAjZQkDPL7UK4Ov37adMmU/p1wic5huuVsep759k8aP6AaxrcodCQHs15V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ixBQ2O7kmy6i/Rp3tQCllV7Wahzb5pDbPa5ype/HspPqB08p1kpzOiEgofeyG/8kJhbbs5R4QICM6SRHS1O+rldiOZAVflfmcThsNruS1Z00acJMetPTCU+kG7ltLbTezsnHE34Vv8UXgdkmHuJPXvQ084tcJq/KDo0DP39mI6c1eY7kMd81IngRUKurWdx5j8EcoAcFw/AxqHU41aWbx3W3cPZWKc55SbXgk/dfxeDRVX4pljqexfkHG+v58UW7NEvigQ49/lDPJv9NRH0sukHFXQoCBGF9hcbSYmWkHQTM9zjx3HLzrHPtvuH99AUvoRj5nckH3aGNFFnpABdwts58YZcdRUwOuIsbeLygOB9MTfmDEx5t9HJ8KHQGLiNmmDzFU9+sb/w9XmbqbHcofALyIRM4W5QzahU9SSHtM0NwwH4/U0UGZtg4Ezxob919cV0494K2tHWdR6x91HfED+sMnWC8rdxSKVp6IajwMGs6gS1eOC8bCe62iNFGA1nnML7qsseCtb24RnEyWnnlHZkGULKToN7Vv7Qd4f7RT8v5Ow9crPCfqMGBDLPtsP0IigXGkYLwWLpYRaxQTJi8T5GrIXbQJvxbKE+eKTzMEXszuwABDq5y/9BCnhUAVIZufLpVJ/7IztVtc= 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: Quoted from Linus [0]: Since user space can randomly change their names anyway, using locking was always wrong for readers (for writers it probably does make sense to have some lock - although practically speaking nobody cares there either, but at least for a writer some kind of race could have long-term mixed results Suggested-by: Linus Torvalds Link: https://lore.kernel.org/all/CAHk-=wivfrF0_zvf+oj6==Sh=-npJooP8chLPEfaFV0oNYTTBA@mail.gmail.com [0] Signed-off-by: Yafang Shao Cc: Alexander Viro Cc: Christian Brauner Cc: Jan Kara Cc: Eric Biederman Cc: Kees Cook Cc: Alexei Starovoitov Cc: Matus Jokay --- fs/exec.c | 10 ++++++++-- include/linux/sched.h | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 40073142288f..fa6b61c79df8 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1238,12 +1238,18 @@ static int unshare_sighand(struct task_struct *me) return 0; } +/* + * User space can randomly change their names anyway, so locking for readers + * doesn't make sense. For writers, locking is probably necessary, as a race + * condition could lead to long-term mixed results. + * The strscpy_pad() in __set_task_comm() can ensure that the task comm is + * always NUL-terminated. Therefore the race condition between reader and writer + * is not an issue. + */ char *__get_task_comm(char *buf, size_t buf_size, struct task_struct *tsk) { - task_lock(tsk); /* Always NUL terminated and zero-padded */ strscpy_pad(buf, tsk->comm, buf_size); - task_unlock(tsk); return buf; } EXPORT_SYMBOL_GPL(__get_task_comm); diff --git a/include/linux/sched.h b/include/linux/sched.h index 61591ac6eab6..95888d1da49e 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1086,9 +1086,9 @@ struct task_struct { /* * executable name, excluding path. * - * - normally initialized setup_new_exec() + * - normally initialized begin_new_exec() * - access it with [gs]et_task_comm() - * - lock it with task_lock() + * - lock it with task_lock() for writing */ char comm[TASK_COMM_LEN]; From patchwork Fri Jun 28 09:05: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: 13715777 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 C0DB9C2BBCA for ; Fri, 28 Jun 2024 09:05:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5A9096B0099; Fri, 28 Jun 2024 05:05:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 558956B009B; Fri, 28 Jun 2024 05:05:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4207F6B009C; Fri, 28 Jun 2024 05:05:40 -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 21C6E6B0099 for ; Fri, 28 Jun 2024 05:05:40 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C873812112F for ; Fri, 28 Jun 2024 09:05:39 +0000 (UTC) X-FDA: 82279714398.20.0553561 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by imf16.hostedemail.com (Postfix) with ESMTP id 71E5718001D for ; Fri, 28 Jun 2024 09:05:36 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=C9bjDiZN; spf=pass (imf16.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.181 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=1719565518; 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:in-reply-to:references:references:dkim-signature; bh=y+o2QcP3zzShPD5bpWdvqtRKk4SGVf/ncCjWPDHjjaM=; b=ycZuF1eIG4QWbwG6ncQK2IFt/AF+ua13JkEuH6DjFcydaAr+48wqv7sfxkOp3QBGB4+WOo JxNpBVO/dx+N9QLrtqmuMml5EcQynXIkbkjWaaJ7JFbbJJSuk/KRNy16cg+7s9ScQpocHm l1pcT6HqRfVXx0YkCW6fpoab8pO1Inc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719565518; a=rsa-sha256; cv=none; b=CipZfo2zUteOWMjNfkJMxdzxmPyQ6h/vrwt2xEE5IwICBRgu9Sa2U+bcyIidVLh65/7oyo 3oFxPAmrLdpmWqMzZjoIiBvBTsBnY7UEphNHxrG/hfF/uOOfbS1oj79P8bnr8Jrm9ujgvR 8Ogur+vAreoKnamds4vPY+pD3xRcW18= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=C9bjDiZN; spf=pass (imf16.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.181 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-72c03d2926eso222898a12.2 for ; Fri, 28 Jun 2024 02:05:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565535; x=1720170335; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=y+o2QcP3zzShPD5bpWdvqtRKk4SGVf/ncCjWPDHjjaM=; b=C9bjDiZNc+UigBFT8sob9tA6tWoNlaUVFgWhcV7EB8h9dgSX6T1KwoaFXELipzDQzJ jFs2pJix3JIicxrcQmLU2pKqp1eFZnSxeaJV0iBsYV07nDyaknORcenMSTZt85Pyfeu8 AfMMLqzskYuERX5RZXydvig4FdpHMUEgpR9W4rGAejwS2/w3zLDnf54eYQGu/KMceNbr ksHXHprU7oy2TFmfDYGLIJqexVJww8GCgfJM47cj+0PNbes3S4efGoql9C4R72mmxYXg ZwlJeJxmC0Y0ctkrdLiiDSW4oldDSW84X4HY06l+tXZnwMqQEP0MngZzMkJxNvIB/jts FnBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565535; x=1720170335; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=y+o2QcP3zzShPD5bpWdvqtRKk4SGVf/ncCjWPDHjjaM=; b=MJGC25PGU+petoAuYYAPtksw7ZnuD+1jj+QuQbu/0ioO69rd7+ZzA/mHviCJGI0bT5 1aZflC9/2RAuADNMc/wtKTNNEozD+4CU4E4DfGYyZa6+2RJOm41NtjE5f83kYIEuBzA+ jNjf90VByYGrSrr+Y18xRDrXWtc4Z0RA5qyHfDav6LrYxRdQW6IBmqKd1AbVk1uBadaC 7vHi6psQ3EcGSssOQtr3viH2PYo3I2I2tWBgIoxCpYrTHzO8CmMUzKmaUjLrukM2IqrV cDUChFe8DSalt3cEj/y6I0HzZ7fK+WeMKcCgmZsAGleFuZo+dvJEFt0OUQVIjqOHhWiS Opog== X-Forwarded-Encrypted: i=1; AJvYcCV7QKNMKQRaOqupQQKNkNc4X8RmBKUcQzuJBA8ZhEsp73KoaWguel8a0aRp9hFe6n8yahKxFN8Pu8LNLNh5N/3EyyU= X-Gm-Message-State: AOJu0YxJQBj+zSBlocQoz+UQxwRxf05JsjKhmV3D6PfnLs6t2tqvrUfY Bxvf2tEFkSh5st7BvzKbL3g6r4JIZEnknJgQcSqjbUcmUTGxHDi1 X-Google-Smtp-Source: AGHT+IGiL1nE/JPCYxh777n4qtTvEfwnEp1By9NjDz6oRHtLGf4s+XwALk3c6cvWzb1jiZh3cdfwuw== X-Received: by 2002:a05:6a20:c325:b0:1bd:2267:b45e with SMTP id adf61e73a8af0-1bd2267b4edmr8264153637.50.1719565535182; Fri, 28 Jun 2024 02:05:35 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.05.29 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:05:34 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org, Paul Moore , Eric Paris Subject: [PATCH v4 02/11] auditsc: Replace memcpy() with __get_task_comm() Date: Fri, 28 Jun 2024 17:05:08 +0800 Message-Id: <20240628090517.17994-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 71E5718001D X-Stat-Signature: 9gnaffq9xoq9oqcjjqhjp3ag3aamfi6r X-HE-Tag: 1719565536-167035 X-HE-Meta: U2FsdGVkX1/kz8+xDar7xqgJBkiMXT9NCz55AJAHQD9vJqI69bwhkT4Leswj8Yg8F7s/8jbFi2FnMxAZem+4JoqcPAV6/+mc0jlFbMetO2ToPSv79DF0Txu6AIwLt2gtuswvitX/OnUyvtLaJ4SoF3LiaAUUpXxZV1AMDdHhor/ahEGHLz2Cu1qIDx/tkl0jCb4sWWlvzJxD2sd+Zb4DyjavMtUGuXFipHftLPlXSVM1ZnhWhJMZ6q9Pb4B7mgsgxjOGudX8r9n05U/Bw5dBIG8DTjtse/0RCLzDDWf640CBoa+q1r98YKmxAmeP8KkjT5biEan55kOTdtVxY9WBlkf+ieMfc1n5FoIM7Ced4hKI9ceFnvRDjONgPMVyAYMfnGuCI3ZH+8B8DEpN5oJuLaiUc06kXZsg8mqpH7JJef6Mh8DnajZMicjZ8Pgufnlx5/oo+rOsqmZ9WtKcnq418srGRZRII8PK4SGhOU1s17naLHU2tTy89V2Ri4DDw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=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 __get_task_comm() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Acked-by: Paul Moore Cc: Eric Paris --- kernel/auditsc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/auditsc.c b/kernel/auditsc.c index 6f0d6fb6523f..0459a141dc86 100644 --- a/kernel/auditsc.c +++ b/kernel/auditsc.c @@ -2730,7 +2730,7 @@ void __audit_ptrace(struct task_struct *t) context->target_uid = task_uid(t); context->target_sessionid = audit_get_sessionid(t); security_task_getsecid_obj(t, &context->target_sid); - memcpy(context->target_comm, t->comm, TASK_COMM_LEN); + __get_task_comm(context->target_comm, TASK_COMM_LEN, t); } /** @@ -2757,7 +2757,7 @@ int audit_signal_info_syscall(struct task_struct *t) ctx->target_uid = t_uid; ctx->target_sessionid = audit_get_sessionid(t); security_task_getsecid_obj(t, &ctx->target_sid); - memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN); + __get_task_comm(ctx->target_comm, TASK_COMM_LEN, t); return 0; } @@ -2778,7 +2778,7 @@ int audit_signal_info_syscall(struct task_struct *t) axp->target_uid[axp->pid_count] = t_uid; axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t); security_task_getsecid_obj(t, &axp->target_sid[axp->pid_count]); - memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN); + __get_task_comm(axp->target_comm[axp->pid_count], TASK_COMM_LEN, t); axp->pid_count++; return 0; From patchwork Fri Jun 28 09:05:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13715778 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 1C6E4C30659 for ; Fri, 28 Jun 2024 09:05:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A3E0A6B009C; Fri, 28 Jun 2024 05:05:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9ED276B009E; Fri, 28 Jun 2024 05:05:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 866276B009F; Fri, 28 Jun 2024 05:05:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 654866B009C for ; Fri, 28 Jun 2024 05:05:46 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E9C21818A7 for ; Fri, 28 Jun 2024 09:05:45 +0000 (UTC) X-FDA: 82279714650.13.5244D5D Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf18.hostedemail.com (Postfix) with ESMTP id 1E35F1C0006 for ; Fri, 28 Jun 2024 09:05:42 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=X6f9GSKl; spf=pass (imf18.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=1719565519; 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:in-reply-to:references:references:dkim-signature; bh=J+uQG4RKAtUTljj+c6PuEzvl8DtV0722F0vMFtL07WU=; b=hKHUYTJ6piWqvwE9eLct8z5nJKSMkThG8RmMW+OHxwGFDAERLRcebbhLsraRKq25rL/Ych HY3qgAWKSiEL6nmUcUsf+qQd5asqXKKkAppEEwdwUca5V1W7K5kjSoXOYWPPd/pvJv/6ZV YPCdZb5w17PPpwvqKqFJ+SnmAEg2r3Q= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=X6f9GSKl; spf=pass (imf18.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719565519; a=rsa-sha256; cv=none; b=S8/rpFH4xrNbdwvkvlek813jt2Ml9QV9xMojB4e+6lfGYdoBAGhIJepc3su2cP0xIQwAmd d+XnEKhQ6yY6JF7+tjACXY9rnI16yAj8NlqNDFU6hGdv871+QBkt+AN/L3iptOCWrfT2Hp 2FNw2zRYD7mchrrT+LzgrTtbOFbu9Pk= Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1f9a78c6c5dso2160305ad.1 for ; Fri, 28 Jun 2024 02:05:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565542; x=1720170342; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=J+uQG4RKAtUTljj+c6PuEzvl8DtV0722F0vMFtL07WU=; b=X6f9GSKl2u6fZ8R0VIiXynI1nFuP11aULr91XmRktJBzObePMQenQw4PfKZgoBDzM1 TGfV8Uf16XvJlEXhCdEHxoRpo4ResbP9E8Bs3XeMKLnTjGWJ9vPl20YcclSwKufkJt9f vr663fsWFuW7uMp54bDgpnDUTmqOXHzJA03Lb2wokV8RLa2iFdFAVNZIKtCuJrarXVep EZqM2vHd0e9x+Hs0wNA0V+R7pwwc2T2r7uWePNtPv7RUJvSfcZqZtQwtSj61zSr7L45L sWnSCX35AC2HnSQW/bte+B00RPGTVR8Oky4te0OwiBJr7yozruRv8NBN7B01E9BJPmEh e6ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565542; x=1720170342; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=J+uQG4RKAtUTljj+c6PuEzvl8DtV0722F0vMFtL07WU=; b=QF4VfBPuiBzrw+fblFDyJnkU3rKM9KWk5kTcozM590VavGFlMCZA361FrzHSQVjJw+ gFD8Bsup5R1oa4shR/vh2xvjqz1Ds9zUb+PPHurbQWw9Rw0xv5uYh0DQHCe/5SzVijo7 jH8SjsaS5vx3n6OXGv1lBDMwnIrpdDjEBUQg5xtUhb3sBgdOpFtvLssOYMdka1tejOfR Xbd2u/B+zNtoDJUwB+TUbjS2dgw4Zy7YmVmwdthwTSuCGU11uGgb4lJ1+ggqm1NKoZNZ edGPHuLpWlN7gq0Ktw0TQqp2YlCk+akPqE1HHUXq8XJ5HSEZhgkx3mwUmHWPPCCfGmq8 pxxA== X-Forwarded-Encrypted: i=1; AJvYcCWB+QvxbfdrxXhOJ2umHgHYb4wtQSmROA6Pqw2NdcAkofbiNLHdQTGpgk6W3DbFzp85nlBwqtc3YAIvKPRXfBFU/4M= X-Gm-Message-State: AOJu0Yw3rGw37ATDzfz0TkV1p/rYJtEXJ52wfVN9AxbqSI4fTNjkaThQ b9O7Sz0VPMBb5QhUrbJG9mJRIQLfvWnY3JdU2I6R1XdoNpNG1FAv X-Google-Smtp-Source: AGHT+IEiCe46L4L+6u8pFkskIn0tSNW3yZf9XlVCW3W2rC8VIHwBiWsCsriXmS70kmQMnPpYjn9ORg== X-Received: by 2002:a17:902:a3cd:b0:1fa:a34e:8819 with SMTP id d9443c01a7336-1faa34e8cb9mr39730485ad.60.1719565541990; Fri, 28 Jun 2024 02:05:41 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.05.35 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:05:41 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org, Paul Moore , James Morris , "Serge E. Hallyn" , Stephen Smalley , Ondrej Mosnacek Subject: [PATCH v4 03/11] security: Replace memcpy() with __get_task_comm() Date: Fri, 28 Jun 2024 17:05:09 +0800 Message-Id: <20240628090517.17994-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 1E35F1C0006 X-Stat-Signature: fm88jexns9u4unezr9yqhzrdsfpdnce6 X-Rspam-User: X-HE-Tag: 1719565542-296551 X-HE-Meta: U2FsdGVkX1/mZcxfrZdPhM6mBxNjfihyqqgq9PA2YjTFWrCMCJl7o8r66tiwOBYZJLcWLvp0VKcNsz6wLpNu0OEG8h1bCrRT3WYj5JuFuTuKcgT7skQMy76RQ1zACy06dIgWSGUvUQn2DxnIBugWGCuiuwo70v7A7EOECyF207AqQzyG27NyeH5ts63QYPh+Gh3o1lB7oxSS/j1xxHPU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581Eufdf07i8dg8hq6J7G8kIVqpBLDh5d68/7YYW+x4fP2cJIfgMYSc84nbd7XcSVPxbtVrJX3SE9b4M/jhlk5wXxSno+16MVWVzrcIBAJQl9CPPp9MfFiyhIezeiacVClccphTn0C1aPAFP0gCDaxFJawHbKuxU02feNNmP0zb8ffawiJ383ySYjkLujguhxxdBr/PaKuUgTt0lTjVzWjWPBrKka1kVQ3P6wTcjX1qEo1MosFjlDabpC71xiGCPIHGU5m9vF0Y0w/Nu5VRj5a7dnT/p81SM2jdskZ/oA8aougZtf9ZnjjsVPcm54VRUGSKE4hXs6inVzkMjn6m6jH/Mb2gtqCkKrTfcoBArA1Jippv613yXQyUvH9oQw8WWnimZx0JnzLBTbStUmUa13kVYkCGvjQlU1vN7E3eUIuDhV2vZIGccCwLT8Ng1Ze+lZrKABW9oofTiSjxp915vyMij6mnCaYqs/iQzwJ2sUX3yZVbxisbiA1MyxZyWm9LiEOVBhnm6A3rZ3f1imf1fRNeECxpMB+zmO/CIG++wUfY= 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: Quoted from Linus [0]: selinux never wanted a lock, and never wanted any kind of *consistent* result, it just wanted a *stable* result. Using __get_task_comm() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao LINK: https://lore.kernel.org/all/CAHk-=wivfrF0_zvf+oj6==Sh=-npJooP8chLPEfaFV0oNYTTBA@mail.gmail.com/ [0] Acked-by: Paul Moore Cc: James Morris Cc: "Serge E. Hallyn" Cc: Stephen Smalley Cc: Ondrej Mosnacek --- security/lsm_audit.c | 4 ++-- security/selinux/selinuxfs.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/security/lsm_audit.c b/security/lsm_audit.c index 849e832719e2..a922e4339dd5 100644 --- a/security/lsm_audit.c +++ b/security/lsm_audit.c @@ -207,7 +207,7 @@ static void dump_common_audit_data(struct audit_buffer *ab, BUILD_BUG_ON(sizeof(a->u) > sizeof(void *)*2); audit_log_format(ab, " pid=%d comm=", task_tgid_nr(current)); - audit_log_untrustedstring(ab, memcpy(comm, current->comm, sizeof(comm))); + audit_log_untrustedstring(ab, __get_task_comm(comm, sizeof(comm), current)); switch (a->type) { case LSM_AUDIT_DATA_NONE: @@ -302,7 +302,7 @@ static void dump_common_audit_data(struct audit_buffer *ab, char comm[sizeof(tsk->comm)]; audit_log_format(ab, " opid=%d ocomm=", pid); audit_log_untrustedstring(ab, - memcpy(comm, tsk->comm, sizeof(comm))); + __get_task_comm(comm, sizeof(comm), tsk)); } } break; diff --git a/security/selinux/selinuxfs.c b/security/selinux/selinuxfs.c index e172f182b65c..a8a2ec742576 100644 --- a/security/selinux/selinuxfs.c +++ b/security/selinux/selinuxfs.c @@ -708,7 +708,7 @@ static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, if (new_value) { char comm[sizeof(current->comm)]; - memcpy(comm, current->comm, sizeof(comm)); + __get_task_comm(comm, sizeof(comm), current); pr_err("SELinux: %s (%d) set checkreqprot to 1. This is no longer supported.\n", comm, current->pid); } From patchwork Fri Jun 28 09:05:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13715779 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 8EB12C2BBCA for ; Fri, 28 Jun 2024 09:05:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 24DA06B009F; Fri, 28 Jun 2024 05:05:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1FE1D6B00A0; Fri, 28 Jun 2024 05:05:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 09F206B00A1; Fri, 28 Jun 2024 05:05:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id DF1C16B009F for ; Fri, 28 Jun 2024 05:05:51 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 68F01A0B89 for ; Fri, 28 Jun 2024 09:05:51 +0000 (UTC) X-FDA: 82279714902.20.597711A Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf30.hostedemail.com (Postfix) with ESMTP id 8C9EA80016 for ; Fri, 28 Jun 2024 09:05:49 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=I5FKL06+; spf=pass (imf30.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.174 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=1719565540; a=rsa-sha256; cv=none; b=dof0ygintnWQ25HhXfKAKGaH+xx36epUR2NN3XjTdBr3hQakCGNc6jJmzhSEhMCXpsgVHE maaibxXX1n0K9xUmwyGeAFjCtbO8GI+fMRdZMKE62jq2rc1EiMmwXj7DdJKWBLRq1Aa8HV IrNJPwQivXmMIrE6HpO02fsUMYmQu9k= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=I5FKL06+; spf=pass (imf30.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.174 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=1719565540; 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:in-reply-to:references:references:dkim-signature; bh=QQdkC9XISn7BWPlTE5/iE7aUj/L2jeLonKoaBsbNpS0=; b=PHZ91jLNLd3RRWdCERm+RtkP7fZ6OJhSoGlxT0WZL4LGHJ0+hp4JOBUXLib6G126r+T5eA x29ytu7a6m0NAm6vn6KbPGwwrvXnegUjrU50OFg+th/rThcA0MYQtp+QVR4Bg2DXwvRS8F E6AxIO/VJgT2RVIV6buZD+cO53F0C3w= Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-71910dfb8c0so214679a12.3 for ; Fri, 28 Jun 2024 02:05:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565548; x=1720170348; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QQdkC9XISn7BWPlTE5/iE7aUj/L2jeLonKoaBsbNpS0=; b=I5FKL06+9sfKMWFh107wZZr3lRPzG9/ziMDvvJRLjcvfiSRZ/IJIgwswfNpkUbtFiB UMAEvUeyLD/+OBydzuYVk25x/dcthflPhvWh4iXYBo5GDADLXkIuUtdWMods4GU1Sa4L AtYO1r7L3lMKtkRnaGl6YzI99H8JI3xp1xANARb8jc+JZDDHhT5MGIT1eh+f/eHApqTc 0rX/XGxtckyFxxK31tg0+h+7+d4gUUAE0Vfyjss0+TaU+San7qSTfLRAwMKbFileYn4Z +rd8zzxPzScZmqIUsCnJxPW6DHPZ4QWZ7XCI8rvE1qDT8ayEKNOXA8F4QzuT2uWy40O0 qH5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565548; x=1720170348; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QQdkC9XISn7BWPlTE5/iE7aUj/L2jeLonKoaBsbNpS0=; b=lZG3oBLN7E3uRtbMj5MS8l0exg41YjbwrU09AiHniMv7YQWzms7lQmSyI9tWl6MEXN POWYvhFlhii9mY1SOcOi2qcMcvDXbLI+KXOJsjIjnzr7poom+xSs4/2njca8ZN8ZmgcP MXW34KnDS0nzYykiltQqIW/mXgYbov5UAAzDEc2EoQZyU/1MT+rz8FQ8nnRnl1I6QQkH e/z/cU9akmVySeXPPa9IAe454HmN6EyLLLqgD7VzRjHmEG/pyg806V1uNHRZqQ4KzKBS 7sHR5M0PBkmDVVn4VSHgm5kjxbieFJLUdCVxiA2dTIlshW03uQwSsIXSE/C+w/AXIPrq Ap8A== X-Forwarded-Encrypted: i=1; AJvYcCUo0FEZbPJM+64irRFljp72F5sId9MgssK9tvci3NTYS81H/cohWYIb0vly3SWAfTZ4gTuD6BsUBna3tIcLHy74VuU= X-Gm-Message-State: AOJu0YyahhoAdHqzuGF93CJy1MGPnybtJQOt6Ribs+YOPhJxAKSrNlY/ 1kEmYaNo8xnMpIBiLZYrAjDqNdsH9BXaNn4NYo9s3ZCKRMp7ikv7NypPtIWKMLk= X-Google-Smtp-Source: AGHT+IFMoCwH/epxCGvG8TCVrNgBjmP2Ft1l1xXAwKN56cpOIayJsWQPU41hiw8mZOfUPZaXevc3Mg== X-Received: by 2002:a05:6a20:6a04:b0:1be:d9fc:7f03 with SMTP id adf61e73a8af0-1bed9fc824fmr4655522637.23.1719565548310; Fri, 28 Jun 2024 02:05:48 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.05.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:05:47 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org, Quentin Monnet Subject: [PATCH v4 04/11] bpftool: Ensure task comm is always NUL-terminated Date: Fri, 28 Jun 2024 17:05:10 +0800 Message-Id: <20240628090517.17994-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: m8mcwwwej8bcgubhnqsfaroc68q4jif1 X-Rspamd-Queue-Id: 8C9EA80016 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1719565549-183426 X-HE-Meta: U2FsdGVkX19yWDtlAhW5VuKKuhsN2gNfkrd6vmAy9IeZKHP2cvLg/6SYyJcpfKIukWhaZ9ICA71y5m9CVrB/MBLucXusO4cOR3I1otl8qGCcj9hDeF70vzFd/vEvPrLDau257YrIlcLI9be4xsEMZp5yrR2tsiXhVe8hJf58ILW8KTOWRC8o/ph7oMQ4Aiw2vz3T4y1gF+uJZxRP3GZdeDr4rC/c7F3jpIiKSBuWyx313y9ivDGLLrHfndtemgBKyi7qmSRPBwzCOrkvV7b3fYCiYj6l/+oYIqQsnqXNjzOrIHEmvXzrsBJ0QSNLV1BGjUI0LTX1TNGjRXmvEWbgX23q4p4+kofofqR17h+Lv7HVye6QE2dYqX2o0xv1qbfovdCqDs9C2W3QFDabc2up3HbELTzuaSzRqZIh4+hSLlaTAwR9M/YuTa6uTS6hfG60epwjXP1sUOR5Mv4w7OJG9qiemu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.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: Let's explicitly ensure the destination string is NUL-terminated. This way, it won't be affected by changes to the source string. Signed-off-by: Yafang Shao Reviewed-by: Quentin Monnet --- tools/bpf/bpftool/pids.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/bpf/bpftool/pids.c b/tools/bpf/bpftool/pids.c index 9b898571b49e..23f488cf1740 100644 --- a/tools/bpf/bpftool/pids.c +++ b/tools/bpf/bpftool/pids.c @@ -54,6 +54,7 @@ static void add_ref(struct hashmap *map, struct pid_iter_entry *e) ref = &refs->refs[refs->ref_cnt]; ref->pid = e->pid; memcpy(ref->comm, e->comm, sizeof(ref->comm)); + ref->comm[sizeof(ref->comm) - 1] = '\0'; refs->ref_cnt++; return; @@ -77,6 +78,7 @@ static void add_ref(struct hashmap *map, struct pid_iter_entry *e) ref = &refs->refs[0]; ref->pid = e->pid; memcpy(ref->comm, e->comm, sizeof(ref->comm)); + ref->comm[sizeof(ref->comm) - 1] = '\0'; refs->ref_cnt = 1; refs->has_bpf_cookie = e->has_bpf_cookie; refs->bpf_cookie = e->bpf_cookie; From patchwork Fri Jun 28 09:05:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13715780 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 C9809C30659 for ; Fri, 28 Jun 2024 09:05:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5A95C6B00A1; Fri, 28 Jun 2024 05:05:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 559C26B00A2; Fri, 28 Jun 2024 05:05:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3FB4C6B00A4; Fri, 28 Jun 2024 05:05:58 -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 2018A6B00A1 for ; Fri, 28 Jun 2024 05:05:58 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id CD6E9141800 for ; Fri, 28 Jun 2024 09:05:57 +0000 (UTC) X-FDA: 82279715154.24.3324EB7 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by imf23.hostedemail.com (Postfix) with ESMTP id DDE24140015 for ; Fri, 28 Jun 2024 09:05:55 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=K+199wWh; spf=pass (imf23.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.169 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=1719565538; 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:in-reply-to:references:references:dkim-signature; bh=8rSpj4+mjUH2uIm/J6/DitPwV6C8fJ6cAywoYkpJrFY=; b=n8OyDqEmB+KZyWTjWtgtVLQSlUHaFEXEXFAWXXoU3gyixtWJoZJzjna3kwn5jz31T9eLWJ USNp1LYc2T7e9CWVG8GkPTgYftU/38CusrXnYOSDr/YFK7U6fd0geO4IHPsgeoHZFIjxUJ f9cfpLZ0RYnhXcCzxY6baqenyt4uYfo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719565538; a=rsa-sha256; cv=none; b=rW/ovW759LQ8ingEEbOcr51vfrM1RnIb38HJCYddfj55MxdJdmf5ybeGVzCERxdpMMKaiU kpZvCSq2iwZZnZHsa7OInGZPuclqgiEs8hS469uZEsoRRfelqI4hEya79ZAupGGsNZ+i9j 5cpx0YodFawbLXiKHj+aXlH0Dn02aRc= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=K+199wWh; spf=pass (imf23.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.169 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-718354c17e4so261248a12.1 for ; Fri, 28 Jun 2024 02:05:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565555; x=1720170355; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8rSpj4+mjUH2uIm/J6/DitPwV6C8fJ6cAywoYkpJrFY=; b=K+199wWhBvQwChuByv94P+td6MaxfW4z1wl0NXwMQDDWFAZ+BlXHVh+5wm6CFdSZXl cVnuf270VymGzNeufUbt6W3yC04p9ej+0yU1TZM6OZWL88f6/z5mG+qsKW503RUsNulh XqB/CHtoazDcAf8u3YONjKyNRsQ9x3vF2wKTmKgYtAoWAzavX0pAd/K+n8eyaZ09X8OU VWF0gHzMXEFJdEhkzNR64ZdqZMthfQM3VVDgGTjiznh8hLPHKNMVL6myk2MLjVlz01uz akG3vXQgv2TZQpxB+/RD4oX5OonhZlnTvkfciDryBODhqmqwrWH6kjYfRvXq2NG7LiQ7 mkkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565555; x=1720170355; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8rSpj4+mjUH2uIm/J6/DitPwV6C8fJ6cAywoYkpJrFY=; b=nuAHG2ZjXSXXKmh4HPbNv6LsZMj/eZPToJSZbYkDiQzG8HjJjs8qm+K081TFkrMJP6 BrGc/jQgT0kHzjIhi5NmAws4z+ecKXqBp74Bj6HqtoVxMmSyCyKRTTIcthsPxFm92GZ3 c5rVlPr6/ncBI64Lp8IX4w6QArShybeV+TLD2G8JLTvqnm9UjGscfrMMMp32r6tmyc8z ODcn3+ApVAR6cn66JOIfUceuYacxoWzwBtfuc2wYc6R7M4LWfDkwyzP/AKlgduWURU03 xiIlEVrg7BwcVIh1KWie0qkaSA9RO8EdcbyZJb8dorIzTwpUKhMY9smhGbr/EGp0tbza BN9g== X-Forwarded-Encrypted: i=1; AJvYcCVMX1MbSGAoXQ/P9TvogahonjrSM1zaJubP8U9zUNF5s/S0rIxrA8965TV8tLo69OEhXaNam04mU0Tf19FauqFYfNY= X-Gm-Message-State: AOJu0YwjwpqQe0JxYQhCFyK2poEkWI1LkmBA6mDkBkyeBh/KTdKOdbCV yi3IbHDWbIOuhASqh5VFJDAB8xnEKrkkfY9/OmoDjZYsgHhxIVLF X-Google-Smtp-Source: AGHT+IFlYRl3xHVXsC7P9mtN40+ZKznkLN9wN0U9RRVR76hQVjzS9rrWerayfKdnpHLG4c4CVUtojQ== X-Received: by 2002:a05:6a20:4c16:b0:1bd:22fe:fcaa with SMTP id adf61e73a8af0-1bd22fefd3dmr9032557637.51.1719565554658; Fri, 28 Jun 2024 02:05:54 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.05.48 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:05:54 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org Subject: [PATCH v4 05/11] mm/util: Fix possible race condition in kstrdup() Date: Fri, 28 Jun 2024 17:05:11 +0800 Message-Id: <20240628090517.17994-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: DDE24140015 X-Stat-Signature: f3mdyoe6b68yakaj3pjw4khsxdcwu66h X-HE-Tag: 1719565555-149151 X-HE-Meta: U2FsdGVkX19Bt3K6Fk4zJwLsS7A+RUZSJr94rg1TZYkBM1M+FzkHUYOAjvvLvqRRxDbIlJkDUpI6YzRmKKN9c8fdTZnpZGHBThkb+lrC7rFuTlmnYpWb09W1fQbrs1w32OPkD3MjU1EtP1ytM2H76Lg+n+1o5MeB4yoOSR/mmHLR6KiIDn++xXQt2q+tW/1+RvD5fFXooCNH8nJUnbECTXn4suM36vot1fJBDZmPVO3HQuUYU7L3Go1gOyeNorzr+7Af0ukv/AupI2JRQfYHjzoLJyyFl32N/TJiQPXMGEe6kK9DTZX3Yn3af5bpkfCiAuiavLumEUxiwTnsHf6jQLM2nYi+S+CVcf5oerOLrxim03mU/mIGSNA3vVK27NwfVoVd8W7NhF90tQetrBSTRDepzBglPQLbGhY2WbAJqj3eeID4yqFUUZNBlxBDxyz41JKJjV9dkg2LQh8ZgWiZavHYn/bvlpxDQKfrAQVQqBjmR02cGLxIvHc2zAe0Q5MNUHkJg6GrQ5c2ANG1nKnvQF6o5sJnPYyJUC1QVBN1VMkMBCnQNGj7TvRtOW3Yi5iAfRxqkKwWkqx6kEFIIhRVa7c5qyokLM01wzjIRQuDY7O1JMXXjbeJK3CpLCXVpf6ASkLApsddi7xF3g5al0/3Meq+Hy3R/7KSH1UDTWsKvYSiW507XfKzmdNRNYtXYw7Ik2vvOevUXSlB+m+xAZgxF5yQeXRWItGMuufYkxdhuBs0VHmi2xqAGyKEeWkVq+H/GqwoAV6XhpJBkdzGK9mCBAoXJSjZRvfWQxVSs+hRFcHKn3orfyVaSAvaChH46L6I4nztTqeuxnLn+aU7CYeATK730tEC1Obl6rDb52EudQdF0iFpaKFddEgROx4cIeWvXimpg2iPWf8mUfy+6vCKLXprAuHZGB+SkkqyNtogp76OdY337rFdmuVTIpWEBrWlVdC2GbgETvg20R9CZ8X G+3dDKpH X1Tty3BIwzUyl1v9Wn8AqxNB3Kz1b4z/1XrX6pYu0tNlJTclj9SVQmYr4PG9vaSuanUywRs4d0yarhaprxoUS1OoRNcvcgYxrooLWcYn/apeX4OgHmD5KwMsD90A/+DEsNO0hy9s8K+YmOFUaFhGT8D5RJesaAZoS6OMuBxzuSNKjww4rZxAcXmdABlkSzN7lcQhahiDtsnrikYHLHi4PnGm+dXq7PuefqXh2QPuNhDgjPaM25a4lBFDJG7ktwk4eTSB9j9UJeUf3mlf9NgNTdn8wRSL8y+yjT10w0JQr64R/LsZX/gl8e/C1LB5brLunTpa8YoPpoG/2ZgHmsB7NY2GlCIg8uFuhi6iu96tZHmrRGbV1DUDubUiHd5B4Lq1oPFyOQPPCkusToq2oJGOb9zis1+P/fQ5/mxLF0gecfS7g/ZU7xTpm48Z+I/D27GjjgZ9AkSitmrGYLYpc4NVnqSZLTCztBABLt/9GhjpefJQb8I7cGDl8TTO2Q5yuRJfcwjwxk1voWXtXaPw= 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: In kstrdup(), it is critical to ensure that the dest string is always NUL-terminated. However, potential race condidtion can occur between a writer and a reader. Consider the following scenario involving task->comm: reader writer len = strlen(s) + 1; strlcpy(tsk->comm, buf, sizeof(tsk->comm)); memcpy(buf, s, len); In this case, there is a race condition between the reader and the writer. The reader calculate the length of the string `s` based on the old value of task->comm. However, during the memcpy(), the string `s` might be updated by the writer to a new value of task->comm. If the new task->comm is larger than the old one, the `buf` might not be NUL-terminated. This can lead to undefined behavior and potential security vulnerabilities. Let's fix it by explicitly adding a NUL-terminator. Signed-off-by: Yafang Shao Cc: Andrew Morton --- mm/util.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/util.c b/mm/util.c index c9e519e6811f..41c7875572ed 100644 --- a/mm/util.c +++ b/mm/util.c @@ -60,8 +60,14 @@ char *kstrdup(const char *s, gfp_t gfp) len = strlen(s) + 1; buf = kmalloc_track_caller(len, gfp); - if (buf) + if (buf) { memcpy(buf, s, len); + /* During memcpy(), the string might be updated to a new value, + * which could be longer than the string when strlen() is + * called. Therefore, we need to add a null termimator. + */ + buf[len - 1] = '\0'; + } return buf; } EXPORT_SYMBOL(kstrdup); From patchwork Fri Jun 28 09:05:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13715781 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 A7DEFC41513 for ; Fri, 28 Jun 2024 09:06:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 422DA6B00A4; Fri, 28 Jun 2024 05:06:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3D2E26B00A5; Fri, 28 Jun 2024 05:06:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 273666B00A6; Fri, 28 Jun 2024 05:06:05 -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 029696B00A4 for ; Fri, 28 Jun 2024 05:06:04 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B38CAC16EA for ; Fri, 28 Jun 2024 09:06:04 +0000 (UTC) X-FDA: 82279715448.13.1F756FF Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf24.hostedemail.com (Postfix) with ESMTP id BD856180007 for ; Fri, 28 Jun 2024 09:06:02 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=frX9xi9J; spf=pass (imf24.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.182 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=1719565539; 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:in-reply-to:references:references:dkim-signature; bh=hxss6iSMiIilMwCNJtHyPtBSL44EBghXiK4U5v9w5a8=; b=CwFCqiOnde+yMILbB/0A5gJ4DcnMztbiDMHTCTXoNxK7lQRzsjnpo3RIU+k74B0xH4hb1K ajyddZvAx89HfrbHd+7rdS8AePJ9mpUmvsfMnTBbxFrD4hGiZzzYSuIPo/mL2f2SxF5mYY iRFk0baG444XVTDbYgVp2lyCpboh+Sg= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=frX9xi9J; spf=pass (imf24.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.182 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=1719565539; a=rsa-sha256; cv=none; b=mQYxpehhf8qqoerRdoGNa0muP/d2s0FiNsc+Iv802vDzFZXd55CjRGep1vV0Axq1WzT6Gw eKCSDpnRRu6TThoCt4H8QE9HTnF1mldZvInmoXCFhGTmKhLiIHgq4K2f3neFYGItf69n2w 2G5uSCDBj/I7PbUCrzO00reb+SH3Xww= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1fa9ecfb321so2083705ad.0 for ; Fri, 28 Jun 2024 02:06:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565562; x=1720170362; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hxss6iSMiIilMwCNJtHyPtBSL44EBghXiK4U5v9w5a8=; b=frX9xi9J9mkTbky9Vg9+n98cJm4Nv2MHzd/g/oikuvCvnkU3NNQx//BSHzludExsGe H65fWsLJoYrS84dOHfc5iJWVgC+Ai5DL8R/qwO0SUlKx6Xn4a8fN+pUbXLv0RbQCEHRk nRv/Dx1mjj/8DJDTfHIepCPeWbYB+N5MqGguJbG9mZ3z6UxW5t95waJZ0m8jvKIBRVgK yHKtNYEVFEzo6tx/6sf8d5xdWpuy5af7wD7mA4yWPYPUrciea0c4BV7eFl5Ro94SypMK UYZ3e0tvy5VvtOZgJsXGLH52anYCKJnOgMBWPrs7+NZBMishFWnd+vensar2BIMw6Gcl UE+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565562; x=1720170362; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hxss6iSMiIilMwCNJtHyPtBSL44EBghXiK4U5v9w5a8=; b=A0hQSy3rSiKBDz3jPJJxz0ehg3FYiojR8AJ1ZoqPz6Nh65q+GJQY4ULGy8UWl2lPaE 5RNgAH03/stELWFV+WtFq94Fg605X8rBYR0IGSmGyk+RzZpL68uXoD/a7M54GTPbLV8R HYUGI2oJNyezTzNhrRRvMPg+DjVtxrX6hH/pvBP3qK3P/IJfDuM7QZyOcg16PJG125HK BRDN2m7WC2FKI9fHsy9seCTGYUCrfPCJJcSaFQkdxlqb9Nrhsnw9EA15OOzmRTLWbP0i vTFy0SwY1dDcOxN2uc1lTEiC99EKrNG/QauofBASvrAax9mWDkoY41dcjEAzyOKImUMf sHVw== X-Forwarded-Encrypted: i=1; AJvYcCV2MWIFIrth0rHPizzBal0wTCCBGTQsXfHlPKd1OwXq1UmFURiCdh2hO4bx+ezznke81XgioFo5Fye3kyU75ncSOk0= X-Gm-Message-State: AOJu0YxhMKrT07bTV3zdYUCBQ9JmRt6JHKQeha4vAS9/QfoVm38Pc3Ry poGFKk5V5+dDPexvTgCki/r6srr3ZYfjgxloGuvO3xmkts71SVFQ X-Google-Smtp-Source: AGHT+IF41G2qRww4DdF/h1fHsQyMDqy+U0zSGeUJQI71N0qwJDXNmrrmWplbD4mTRfu3C38uIRvv6A== X-Received: by 2002:a17:903:18d:b0:1fa:2001:d8ff with SMTP id d9443c01a7336-1fa2001dad7mr177652065ad.52.1719565561588; Fri, 28 Jun 2024 02:06:01 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.05.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:06:00 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org, Simon Horman , Matthew Wilcox Subject: [PATCH v4 06/11] mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul} Date: Fri, 28 Jun 2024 17:05:12 +0800 Message-Id: <20240628090517.17994-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: BD856180007 X-Stat-Signature: wewrtgdedkobae1ow8aiyjp7nniwcbp8 X-Rspam-User: X-HE-Tag: 1719565562-95911 X-HE-Meta: U2FsdGVkX19ZhoBqC7BC7AFI7N51e8l4bc7/pzxCuqrwEWDgCEhbJQjYKSwFv1EVg5MsvbyED53s5tEedfTLtwVO3D8UffcFOpKAW632+rqWYjIwxca5oOddfuebcxiKUHZcAvyf8jdAG0UUL4zQxEESae0udEVcBduw1A9Uj3fpl1bRd+NbbtGw9pAZNZ+5P20PVt0v+wa+EcL4gB9AyIX6iD6x4vVrOSzXySK0qJ0+ifoy6TZBIuU6eO3akoqjdLUVQVWSTPMfeFIbyWsXXWX4WKBMXrAZT4+fST3zzoxDBKvhKqFLNFZsSMyhppLIpyoNbvJojxmzw2SM7GHErsMZCR0FC92yImFGsJrrVBUyQY3u/P7Se5YOsUx7OHAaGXMfhKdeAPKVmif+ZDSaB76/7pJTz40fcH1OXm4DIMkqfN4hcJDbI3Upc6AA1a0/awGA3EDXQWor1b7XOd9+UUIdeckat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NYvpznjtr2VXnj1s34RsotvuO+wb2K2BjolCs391OwchEjYsp7i2EusuzpQlZs2QyrR6o87ZnXgIxgLnWCDKb4nXKu9B/H1/5axqvMKbq9AlKBWvv99QTBo= 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: These three functions follow the same pattern. To deduplicate the code, let's introduce a common helper __kmemdup_nul(). Suggested-by: Andrew Morton Signed-off-by: Yafang Shao Cc: Simon Horman Cc: Matthew Wilcox --- mm/util.c | 67 +++++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 41 deletions(-) diff --git a/mm/util.c b/mm/util.c index 41c7875572ed..62a4686352b9 100644 --- a/mm/util.c +++ b/mm/util.c @@ -43,33 +43,40 @@ void kfree_const(const void *x) EXPORT_SYMBOL(kfree_const); /** - * kstrdup - allocate space for and copy an existing string - * @s: the string to duplicate + * __kmemdup_nul - Create a NUL-terminated string from @s, which might be unterminated. + * @s: The data to copy + * @len: The size of the data, including the null terminator * @gfp: the GFP mask used in the kmalloc() call when allocating memory * - * Return: newly allocated copy of @s or %NULL in case of error + * Return: newly allocated copy of @s with NUL-termination or %NULL in + * case of error */ -noinline -char *kstrdup(const char *s, gfp_t gfp) +static __always_inline char *__kmemdup_nul(const char *s, size_t len, gfp_t gfp) { - size_t len; char *buf; - if (!s) + buf = kmalloc_track_caller(len, gfp); + if (!buf) return NULL; - len = strlen(s) + 1; - buf = kmalloc_track_caller(len, gfp); - if (buf) { - memcpy(buf, s, len); - /* During memcpy(), the string might be updated to a new value, - * which could be longer than the string when strlen() is - * called. Therefore, we need to add a null termimator. - */ - buf[len - 1] = '\0'; - } + memcpy(buf, s, len); + /* Ensure the buf is always NUL-terminated, regardless of @s. */ + buf[len - 1] = '\0'; return buf; } + +/** + * kstrdup - allocate space for and copy an existing string + * @s: the string to duplicate + * @gfp: the GFP mask used in the kmalloc() call when allocating memory + * + * Return: newly allocated copy of @s or %NULL in case of error + */ +noinline +char *kstrdup(const char *s, gfp_t gfp) +{ + return s ? __kmemdup_nul(s, strlen(s) + 1, gfp) : NULL; +} EXPORT_SYMBOL(kstrdup); /** @@ -104,19 +111,7 @@ EXPORT_SYMBOL(kstrdup_const); */ char *kstrndup(const char *s, size_t max, gfp_t gfp) { - size_t len; - char *buf; - - if (!s) - return NULL; - - len = strnlen(s, max); - buf = kmalloc_track_caller(len+1, gfp); - if (buf) { - memcpy(buf, s, len); - buf[len] = '\0'; - } - return buf; + return s ? __kmemdup_nul(s, strnlen(s, max) + 1, gfp) : NULL; } EXPORT_SYMBOL(kstrndup); @@ -190,17 +185,7 @@ EXPORT_SYMBOL(kvmemdup); */ char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) { - char *buf; - - if (!s) - return NULL; - - buf = kmalloc_track_caller(len + 1, gfp); - if (buf) { - memcpy(buf, s, len); - buf[len] = '\0'; - } - return buf; + return s ? __kmemdup_nul(s, len + 1, gfp) : NULL; } EXPORT_SYMBOL(kmemdup_nul); From patchwork Fri Jun 28 09:05:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13715782 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 C938AC30659 for ; Fri, 28 Jun 2024 09:06:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5D0F26B00A6; Fri, 28 Jun 2024 05:06:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 58BFB6B00A7; Fri, 28 Jun 2024 05:06:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3FA266B00A9; Fri, 28 Jun 2024 05:06:11 -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 1CF836B00A6 for ; Fri, 28 Jun 2024 05:06:11 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id BB2FEC0B21 for ; Fri, 28 Jun 2024 09:06:10 +0000 (UTC) X-FDA: 82279715700.22.A5A32F7 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf08.hostedemail.com (Postfix) with ESMTP id D5601160018 for ; Fri, 28 Jun 2024 09:06:08 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Ox/+b2Ef"; spf=pass (imf08.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.175 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=1719565545; 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:in-reply-to:references:references:dkim-signature; bh=K/nz1JHR4z8p+ajw1D912vGA5V6XqUcGmzMFKN4JA2M=; b=LAlsKUTpDm20M0ee1JmhaQBMRAxeoSEVBJ53jxF8wBjtn3BNgKdJ3krLlpQXJoNJtTepgk CJfCDYZjgnKU3OmOJ/Lzl2i5BOirfMfT3TPq521wKWdYN2qgXj9rbvfCdzmRpfydaAvbXX k1s+n/VYxMwCoSmF+KvW2u77DpW4/rk= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Ox/+b2Ef"; spf=pass (imf08.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.175 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=1719565545; a=rsa-sha256; cv=none; b=Tfe8/H1uqfC+9FqXFTyYX9GE8NN9hyjdJq8vyYxgtmDlxzBHXIJ4sjjhO6mx5YRGDAN1RQ JUsmEbbgTaaCuqXyRZ1FY39C5P+MdLM3Kf9WPzbyBlXdHYPmi7Mfqf0n/8+t14KAHduv0V Rjsy6K9sPx4Wh11PcaPFFNCMDp2pG1w= Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1fa07e4f44eso1913465ad.2 for ; Fri, 28 Jun 2024 02:06:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565568; x=1720170368; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=K/nz1JHR4z8p+ajw1D912vGA5V6XqUcGmzMFKN4JA2M=; b=Ox/+b2Effab4AFM+DKJYH0QjZWx6BOYoUUNKFMfFL3VUOgrSnMSBylcECdLSXpZu6e 3JufNu8BiybQPCoR/5QvdhGBLRkAjf4EZ5U7UyWkJ1s4cYmFRwLAHheolN+R5B5YnQfG /27u2FofDS2yAedWG3dzV8X7zTKERSpKRiW7jMXknik6IDwqzBPzrm1APvf/cYApER7+ rOsu2QRbUEw0MXLFIWK7Cg58ZFE+0aavnHB4fw7JQQ6n63OlY50oMqAK/hp/vfFFM+bw BIfIt5MPkUZVxVQM/1Y8MdOAJDvwjooGfNCA6tX+looWDYu2+ZcmG87Zq4O5UlEDhgpZ b/cA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565568; x=1720170368; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=K/nz1JHR4z8p+ajw1D912vGA5V6XqUcGmzMFKN4JA2M=; b=MevvBomu6my1EkdgJEHP3AQURpsRLqzqi+O/sYjSvR7cbOb3fAU1forqCPYUfbSpzA 2BxO+x3BwPNUZ0vX4Mj9/YgMIn+pJS0Pw7HXQGP/zFaMYafSZiy5DOLnS2EqWjZecJgE 26Xv5awcrsEqi4nduPznOd8rxJYzGQTbbPHeLgzC348J2dC1+pALLO9MQOD3XUpUoG/0 grTpolXDT6bKLLsOjErCMhFsqtYwm7Ypx+z6baZMYY8PBVCdd4A/Kt29ADY+EE7vY+iG xbaybXV+l3cDgdXPSxyUBZ39IjLj4LqfZ6LmGvUkNfQgzagaJ0Vol6O9QoJA6wVl8fBY 7RwA== X-Forwarded-Encrypted: i=1; AJvYcCUWfnLgljG2AXEq/I+XtaTcGsc1hxrSIeGbohbSkT6F1AsjDbdd5StMqBBX1yRXzw9eU1aceIbtBm9O3j7JoKP21iA= X-Gm-Message-State: AOJu0YwxPmGCWYbawVRBrttVJ9t2r+qBtXQUlnRZWfSFHgjEqL4p93aY ZAWkKbNq3tIbcxypWUu1GpFvMPhMjSZi+8e7vLCDnzOFVjllHRj1 X-Google-Smtp-Source: AGHT+IHdHlMsiSxDneCpl0Z+k+h/q/UIRrZYdoSU24sjr9+9ved5pPcYJylJrzwg3G+ZaraRZ/+k4g== X-Received: by 2002:a17:902:eccb:b0:1fa:ab7c:a483 with SMTP id d9443c01a7336-1faab7ca748mr37580565ad.5.1719565567694; Fri, 28 Jun 2024 02:06:07 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.06.01 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:06:07 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org Subject: [PATCH v4 07/11] mm/kmemleak: Replace strncpy() with __get_task_comm() Date: Fri, 28 Jun 2024 17:05:13 +0800 Message-Id: <20240628090517.17994-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: D5601160018 X-Stat-Signature: azfotyxffuzbbhfuamoshsq6q4n537g3 X-Rspam-User: X-HE-Tag: 1719565568-190708 X-HE-Meta: U2FsdGVkX1/+CHseOpEwQmI/3IS4wFVJfhkX0KYXeE5aF6JpamBuH/GiEaOC5c+4UPgppPLOHB7gXFpZ3JTAR9fMDiplQqOlsDYfU6rGnr9JTbyGLcZbReQmpqpyuCB7dUO2e0cl5eAhNnD9EYqwdGa9zIHS4BGg00LdhJJsHw3qNOV5qOQH6COdNSARVvi8YUUk24qOzQIwbVguhoosCOrAvvT1t6JMW+DVKNS4jl8G7+I9zgZGnnaurwtrntxn/x7YBE6mV60E/Z7djzV+OKWk2dI8yZOO8EJKuzQBLf3mZaXeoR36x6zrkGmK5MHr4uHX4AO6Y6PJv0j+Gk1OtSTz2/meUjCv556ruHQvRo568bfoST4aiB6uX/HIYi++fp5enUO0g1HTtLU7RLmtDrff0Q1GT8Lck1P0Q2vRMC4yOtn+D3Sa26phHQyb1LmseD2C36yVkEmfinF9btaUCpm1tSpQSzy8tLF03/v3dmjWf+vuOIvHr8I6oGbTVcWLqC+VjAgcg22Y+MTJ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qxTMPcadso2YKQg== 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: Since task lock was dropped from __get_task_comm(), it's safe to call it from kmemleak. Using __get_task_comm() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Acked-by: Catalin Marinas Cc: Andrew Morton --- mm/kmemleak.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index d5b6fba44fc9..ef29aaab88a0 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -663,13 +663,7 @@ static struct kmemleak_object *__alloc_object(gfp_t gfp) strncpy(object->comm, "softirq", sizeof(object->comm)); } else { object->pid = current->pid; - /* - * There is a small chance of a race with set_task_comm(), - * however using get_task_comm() here may cause locking - * dependency issues with current->alloc_lock. In the worst - * case, the command line is not correct. - */ - strncpy(object->comm, current->comm, sizeof(object->comm)); + __get_task_comm(object->comm, sizeof(object->comm), current); } /* kernel backtrace */ From patchwork Fri Jun 28 09:05:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13715783 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 39BC0C30659 for ; Fri, 28 Jun 2024 09:06:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B9E556B00A9; Fri, 28 Jun 2024 05:06:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B4D606B00AA; Fri, 28 Jun 2024 05:06:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9EF6B6B00AB; Fri, 28 Jun 2024 05:06:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 7E9526B00A9 for ; Fri, 28 Jun 2024 05:06:17 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0062B818A7 for ; Fri, 28 Jun 2024 09:06:16 +0000 (UTC) X-FDA: 82279715994.18.16DD4BB Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf03.hostedemail.com (Postfix) with ESMTP id 2019820003 for ; Fri, 28 Jun 2024 09:06:14 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=H9blIUFY; spf=pass (imf03.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.177 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=1719565557; 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:in-reply-to:references:references:dkim-signature; bh=aN2V+duoRLP5mjQclolxXRJl1PN7YZIwA2PLtFFVKQo=; b=GhAr/M3HmsseJoLbkxnfyD6iTCPCjUARYwO7VRa4J4RFlBRQXZ8RlDk7iGQf7MRGVfTs8i Eguk/f3kTvhkyaWe2d5jwv4oZ4gZPtEhdy1ONF67v6sOJf3cd3Tkg38Is5m8Z+XAHrOOsD Z73o92lCZ6fwufylqweMAHtFxNkixiQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719565557; a=rsa-sha256; cv=none; b=XScF54Ru8991o0Ktpsur9QWru1+BYZGqPaA3FQD2l5d86ESLVMftX+Eaf+CwtzmhCZL9on qq3FkRbS1DGelQQ4dHKbU9IoMaIKNtIsngwq14hMknK5lgNvWrSSA8L4sMTI01VboMM+6R mwgoL0OYJ3LWM6i1yFvWKC8fYJXjFIw= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=H9blIUFY; spf=pass (imf03.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1f480624d10so2276485ad.1 for ; Fri, 28 Jun 2024 02:06:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565574; x=1720170374; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=aN2V+duoRLP5mjQclolxXRJl1PN7YZIwA2PLtFFVKQo=; b=H9blIUFYQOSVrzys0ho+bjCU7ituZb0oWQ1/LJmaiqRBsujIdc+rq1V8W3JbsmaSmO iuc7pqOgRIq02mf+dtY2cNVGPuRS/ISjZ5+dm8Ojj/7UJjSKmlhOdcsK1T/FCpsQL4/B SjljcuKhkZ/xz4q80rBT9KNtmCeEWzQxvyzASshmJdmynvRFhQqDr2B5Y4m5AzYAt7xs 1lxXFTtDNt+jIY42sd0zXokxayKnomhNlwUezRHituL0zaQY4gVIKDZ49QyVlmB4eUfS L02nxgplC78GVvFKhOW7TackEiDoF1fYdX3OGao4D3koMyvwIAW+6sTZ0KU8o3pdiXSI 8OQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565574; x=1720170374; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aN2V+duoRLP5mjQclolxXRJl1PN7YZIwA2PLtFFVKQo=; b=V7sBSABop5ab9f7GzCW3SMr31aTJZy5MzyiJmH2SCJiUxvhnWajp1d+8Xn/7IgJx8m 1YxmDPEQU/zV+8JIbIi1cnDa5ZUtxveNlXHwNTei45qjtA/ruVcQESB2/48fk2xzPRgw Noxp7PqDEtodARAQ/4gEz5PBCvfebj4LAORduTf4q+tyfghClOH895zQMln17vqkvj12 l4V0GK+fLI45tPDSLXaOi4BSUMFIDcFEuaPMpbdiZ6fSmlXDHu4Qvqv4e5VjVK9TXQfB n/ugZSMSvvSzYxhUmGF3PE5aPyxKpht9tnmh1BeGG8sITq3ORxL4VcS6hjvlLVbab2B8 uZfA== X-Forwarded-Encrypted: i=1; AJvYcCX7bd/A4AohEuSHA041+hO7zbc7ZQBYwU0qUz6nYLwc8Lmcvsq4+eMYyeqdOHLk6vtIjrviXFOWgtWqre77S0jxb/4= X-Gm-Message-State: AOJu0YwEun38bGMtA1TeUPRftNX6RQTmGqLoXGhhhydXlUWEe4b5N4mp +CsdrFHYjs3X1THgwihAqm0XX7vgeaWjhnE3AkMYsTA3QTSnKCu9 X-Google-Smtp-Source: AGHT+IH8hbTH/mkdkSjyBT5baT0IFO1FQw7wMYLskc8y3Zcc6iSLCmGElb9qtOP7mNAm10MFemyYoQ== X-Received: by 2002:a17:902:d50b:b0:1f9:e7b4:5def with SMTP id d9443c01a7336-1fa1d3e0157mr181052415ad.3.1719565573956; Fri, 28 Jun 2024 02:06:13 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.06.08 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:06:13 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org Subject: [PATCH v4 08/11] tsacct: Replace strncpy() with __get_task_comm() Date: Fri, 28 Jun 2024 17:05:14 +0800 Message-Id: <20240628090517.17994-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 2019820003 X-Stat-Signature: x965zccc833yp187cs34z94zweehwaty X-HE-Tag: 1719565574-693078 X-HE-Meta: U2FsdGVkX18VBXZWE3RMSCb0g+HGfHqPW80TJi4k0qLkTc0Lwsx+jLH+gvmVmCoGKSp2ZPPW/EyVy8zO6nzcAsxZV1cp/ENy4z0weljROtGxGqKC3GKrmcUW7bS5/RgDGx3a0Da/stdRsX6PD3Ug0vx5l1lYkpgRpw3p+lGQP8ySDhOHcoGBueZ22uShouwJ/zgRJC9OOvdzFC1wt3vZcZiFkFIX7HDCoJgjI9uu+W8Bz84HBXwV8mhvD9P12DS4FAbV+J6CNzOPq+sq8HriSCNhBegrGdqskFu2Yoh9QB1ToYZSONeA5dDPvrXWYos3S8k5qa2OQS/BnRyt8DCmgN3J6EvX+nN1LSN7wU0n7sqqY72RDqm3bjylwyPbw0x9jL8s8xJAWxSBqLISDzvqP2z28ERdTZswPJ734ph28FUK5jr6zOqHchDYBbGgKnoQ7Z6XWgIWhvL51K2SCRAKhGk5ipVRwQPTCl0OEzh2IgXiFOTCQtE4Y3vx2Xa9VpqFlo+7Apvjj09bFWqRGX732ad4KTPcrQawRp1E0acthO6GGOLyHIawXP/wrNXX2qrk+5Rlj7tgZUPypBUWa/pjieSQqY0U8rdSdCsJ6+XV3zrQnxV3CM/yGPe+kqOXySnamotM00650sAaarTtwVMGry/CTYszmtT6OZb/wL8lhaDkbhFtN4atYyUBJmFf7myyU0DbJFi4SnZcss9nTCXrdZReRgvZ0T7Ty3/wero0PFZzp6sw3fvFiKktG+0wNte4qK9LO2eXq/6X7Em66kNRhRdUQt1xr1gqAVL2zizUzqS9sVPwk23/XRMYbdC56keD3h5iNmsjolKi99soh4C80HKmYm4JxfVPdZoHnYrMZTvPl8ajGq4yiJlw79Eeb7Y2GgMDPhMyjleW37h37q/UAiAQFSrQ0Ag4yZjEuJutlidDNUvw7wGPr12IrThSoqVg+BRwPCbp41hZFveOjFe XbUK3QOj 3sFDQ1+5Dubs1oCAi8jib/3SrlfF9/zTRX0pPGcMYnG2juwxIMqz23qq8mR66QVytJg/ipQ51MPIELSrCT+cZsjYava6qJ/QhHVdVJI8GW33yBDWQf+WNraC3aGZsqLLuZSQIgIfp6W6tG23o1z3+Eyy542gGX/ZP/TaIL/X9w+qOwelLptHxERpvVi6/Y/RPmGvONUupUsDBwwsaMJfqUbyNuCS3Nu+de/dTzdD9Ediyf7l/MrvQCnpJTLMO3PhvuhwtlHp6+tAnhRHdkcW08ftLEQhTlMBqkOt+71ybQLUAYnIg8GutW6uec9dbHWR8wI28L5JKcTpjABMRzG1xXM5KO8GALCdR5UoG86tEI7igS5Quu6bENA8fDkJc2vmVIcl7qbnmlB8Gl2F3X3LxB/0fNBCFA6vTe2mZ7YsgtnleoiqehBUsb/GyOIcJFyNPf+D/mDkck0ZPc9A5gu6qfr3EamOF7hfY0U0C8K2FTXUsSsSVZGSMDpmCxA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000036, 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 __get_task_comm() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao --- kernel/tsacct.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/tsacct.c b/kernel/tsacct.c index 4252f0645b9e..6b094d5d9135 100644 --- a/kernel/tsacct.c +++ b/kernel/tsacct.c @@ -76,7 +76,7 @@ void bacct_add_tsk(struct user_namespace *user_ns, stats->ac_minflt = tsk->min_flt; stats->ac_majflt = tsk->maj_flt; - strncpy(stats->ac_comm, tsk->comm, sizeof(stats->ac_comm)); + __get_task_comm(stats->ac_comm, sizeof(stats->ac_comm), tsk); } From patchwork Fri Jun 28 09:05:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13715784 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 A7C70C2BBCA for ; Fri, 28 Jun 2024 09:06:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1B23B6B00AB; Fri, 28 Jun 2024 05:06:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1627F6B00AC; Fri, 28 Jun 2024 05:06:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 003C46B00AD; Fri, 28 Jun 2024 05:06:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id CE6A96B00AB for ; Fri, 28 Jun 2024 05:06:23 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 858D8120A02 for ; Fri, 28 Jun 2024 09:06:23 +0000 (UTC) X-FDA: 82279716246.08.E23BDAD Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf06.hostedemail.com (Postfix) with ESMTP id AF521180006 for ; Fri, 28 Jun 2024 09:06:21 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=eXZFNb01; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.173 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=1719565561; 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:in-reply-to:references:references:dkim-signature; bh=/Cv3tjdGG97YCwgOlXgEBsrvlHrrgiTFYRDjMKYVYxc=; b=c4JKYwN0qh6pTTQkl09ZZEWjQojE49Qnlzn+SAtMpE5KzY5gPiMzSwJU5bLaeinefaOIII MBPfsLnucwpbgHfv/AoeuSfsIIEMhhAtxAYGy+ALXMRiehC+FOw+umOO0Z5ZyTR4Q21z1C iPygKKQQn/3rjaNFOjxlk/7cHBO+lgg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719565561; a=rsa-sha256; cv=none; b=E0NIBE1GwIJXcSY5hzzSBi0C8V9pznJ/SqgO/844Ds1tF8oJqzUBXu7Ao9z+jwZxlTm7E+ 41rtf73nFe3S3VCJ7Aa1MWxFkjJ/L9Ff1pnzgY5Q8vrFqKRaH9N6y5zgzZpT+PhLOqCOgP GaBn2JWlH5n93Az1/K0czLkUlu8ySi4= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=eXZFNb01; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1f480624d10so2276955ad.1 for ; Fri, 28 Jun 2024 02:06:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565580; x=1720170380; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/Cv3tjdGG97YCwgOlXgEBsrvlHrrgiTFYRDjMKYVYxc=; b=eXZFNb01aQRyAOq7ObimDtGBqXYJvLCcpMuEafGMJXld0G44eV3ovKG+450HWub2Bb 6e3S4uEq3D6bcxNbphR4mmzor8IvSZMZZ8CwnhkQEaOI1frGjJXyBbhGL8EqFEQVxyrc IK/cS9bwe4cbA0tuXR8djfOg0Ktr/LSeOTkpRjYQ6Z4y6sxTCYpdUspUflacvEShRjaI D1F6H8gFUFTK+WQwIoB6KceeNAN2iAApZHsj7wiDVv0jdqNBHti56BI3V5M98fDwBYVV avzUFmnyRV54+micvAigLzW0VyN/4Comtgp0Ke+PFIeRNJHKoQJU6GvXcVijHGI7rHsQ bWcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565580; x=1720170380; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/Cv3tjdGG97YCwgOlXgEBsrvlHrrgiTFYRDjMKYVYxc=; b=O3ZI36pNVXAxtA5Mz49HCMhTTgjY9zo63P+/EfS1JoCLbCMSubhNNPutlcTNhW789Y 8zY2e+pU1faH0cnE9Z7eIOTbqPtiYrmkhD8LiBXTuApVP0YU/n7MoIJeBqH1IrkqG5Uj 5BZbEpM+CBkdtkAl42I4hh/F5/PaaaFQa4b80DBRdf+ATPkDzJDdXJa90k2Av4jECTTH gny6wGLPypsM8CTQcwHiuPAnIFuZb1Enkc7KiwtsnQEbw1yyfUR3K+5vtyUDMr+fPj4x tdbhE0jzBdDvG3IlgfObnnBZK8Fh9U9fNd1bXNyPSE1HBALdTmeg/Wc26rjR4v+N6K/3 NCnA== X-Forwarded-Encrypted: i=1; AJvYcCVGhUELPcpZQwYE+eq509SnZxts/N6v/XGmDdOEEflzE/GybKRPP/fPDTtlDhY3Z/0mlmzjiGL1dUcBKO4hhKk4wXY= X-Gm-Message-State: AOJu0YxyHy+P+08kolX6SsYKEMWAN1rFZ0mPRh76DymjZ8gB4MLeKX0b VRb8iG4Jo3yPMRXsFRL+P7wJyXAlus2HkwaFajHuIBQxUuz1m73C X-Google-Smtp-Source: AGHT+IHxho2IY/KPtiPSOQ+4V3CqX1HJNyhpaejcbnnpb1PpTk/59mlf95gQnvz1ll6XSDBM+JJ4SA== X-Received: by 2002:a17:903:1c3:b0:1f9:9cdd:dc76 with SMTP id d9443c01a7336-1fa1d667d19mr200814765ad.47.1719565580537; Fri, 28 Jun 2024 02:06:20 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.06.14 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:06:19 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org, Masami Hiramatsu , Mathieu Desnoyers Subject: [PATCH v4 09/11] tracing: Replace strncpy() with __get_task_comm() Date: Fri, 28 Jun 2024 17:05:15 +0800 Message-Id: <20240628090517.17994-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: AF521180006 X-Stat-Signature: 4wmjdqahi87qmxag7pe5ri1k8p5uh1y9 X-Rspam-User: X-HE-Tag: 1719565581-64370 X-HE-Meta: U2FsdGVkX1/G01Eae4lsxRZZtGbZAH+s8a2M05wJeHBISk2Jxo32lSNekbsQD7KWvkD/xxxyxCpU/KdxZDpBAiuPlLqM1vDVf/dw/DoyrVHsCniRuDZQN9H2bqaLzfbR7lM0mPOuwnfJSysGmyKaG4l00mb/Iqdl5KfN7uc6dBQurhqaZ9WBWfZ00ORC2TeoAAcuSRlc1J0suKgxWZUWHcpn3doRauDAf3Cc+O8JUbRA8DnvYheVOx94+RN8oGXzW6+RtU0fNyZj16WM1g9DTyxfT0DiPSAcM30Hza/+BbZAVigf1NFivn13DeCWqXKXkoJrAnz9JDbDZoS1LF6fKDhPgYZNk9oW03URT3HNImzcRYK3BPs9vt4U7BQA3vEzXmOZROyDYFIZRTnqb7yc4qYY6Ci/5a1B16RTNF9OQNkYHsGcVgJCgg3b+XOYPSTpbxo6fNohqGuo/c2bpm2oA1Tlp0yxPNd7wLEV1HVXdqjXfM+5tmdrpK+X7WK6KPI5njnXRCqWTx05Gu7ipjXp0gJsPZmzVjboDYYE7cv8aoTakkh1IwJf8VX59s+WCE4xm3VIYnnFeZ+0LUAxDTEg5TVjBPbnPKZlxqoD9tKB0tnHms0TRRkCIJtYYtlhwOEihOVu2psb9cdMfTK0JYUACVNwJMufPeY7rGBItYaPJ42iI3X6l3GsBHUU3XN1eHB0iz/ZcGnc7LEwgoMmPr5r2wo6y1Xad4CkoZg9j5F0jo3gpCWzyqKgdG2vRdmuSYtYFn7WGmV1mgZua8a/+FOVRShtVXYG274kn7KIvF6Br9CwgUXezVXK4YxPAuU6ec1/i32LFz2M1BwBsZe/t/kfbAkxxgZ/MCMTcfRQTMo1Zdhv5aUBHmyvPlt0KV6ueoseNvI2d+FYaoyKJa66/DqI1WC/eQqP3gt4MEMGl+H5QboU3eUqxXOCRkLf1alHcyNfc1trr5FBz2Po/Tco3l7 dfUiLXSH B8bHMAExhWGPkWz0LUgBuy0p64ipBWgn8ybP6bg/XeXQQSamHFZK6qUGjOI7gPchs3V/nK+rtuC7qGBww2To5VoF/NOkkzNx1Zav5r+jcdZLV2h4IHLS9Nirv1oKliT13OqbwW0ludgTwqHcxFnF46tsTFnQowdjEjUvuSfwVjEttPvyX1oJcmIu3CHB/cm4Y2Tf4BALu8wlTI6s8SMzNkwsx9HLNcU2FYVwvRp/aC8760oqeHV7eif8gE08A0lp4KcdMzlYRZrVVdHoOe5JUOSEh51tdAKEAqcUdI51C9fatGWitkcu7CKFWZ0WWZOU4/d5/JdJv4TXycjWtXNBeKf6F/Qc9DCwPqH2ip+2tnbMMSGXq4jceqaciieDREv0mvf/CC4uOqcI/92k9RGEcKRP3yBT54HIESTduk9I37yl03FkT77PZ33ze8ujbN9qic0WDzYajqOeJW55+xchGNbY4M1fHaGlRPl1cpxRsKSVB4kyElwrNq6MhxUXfTQ0XEPJ4jx3HoLqnh5jknzahv3O9HXWgbRKrNBMoraqbLqVjW1XExFZb8YBMAgYIYc3zOft8CDxUyLJpjaoEDPFmg88R5stlwsZC7qXs77JuATKqkz3XFLXCD1/iTbMpZSR9F4sUscUd9MM7zy7pv8I7elQRE8nc4r2ksZq7OHK81e171wUDRhu1pLlwYCGn/aVckJFcFx8739HkT09bvMAELxcsQ0GlVjn5w3TIB57O3kuTXoWDvB3F46sTwnPeuiTTvht7 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000002, 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 __get_task_comm() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Acked-by: Masami Hiramatsu (Google) Cc: Steven Rostedt Cc: Mathieu Desnoyers --- kernel/trace/trace.c | 2 +- kernel/trace/trace_events_hist.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 578a49ff5c32..ce94a86154a2 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -1907,7 +1907,7 @@ __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) max_data->critical_start = data->critical_start; max_data->critical_end = data->critical_end; - strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN); + __get_task_comm(max_data->comm, TASK_COMM_LEN, tsk); max_data->pid = tsk->pid; /* * If tsk == current, then use current_uid(), as that does not use diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c index 6ece1308d36a..721d4758a79f 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -1599,7 +1599,7 @@ static inline void save_comm(char *comm, struct task_struct *task) return; } - strncpy(comm, task->comm, TASK_COMM_LEN); + __get_task_comm(comm, TASK_COMM_LEN, task); } static void hist_elt_data_free(struct hist_elt_data *elt_data) From patchwork Fri Jun 28 09:05: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: 13715785 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 42BA3C2BBCA for ; Fri, 28 Jun 2024 09:06:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C89436B0096; Fri, 28 Jun 2024 05:06:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C10706B00AD; Fri, 28 Jun 2024 05:06:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A8B746B00AE; Fri, 28 Jun 2024 05:06:32 -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 8AF706B0096 for ; Fri, 28 Jun 2024 05:06:32 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 3F56F1C2B73 for ; Fri, 28 Jun 2024 09:06:32 +0000 (UTC) X-FDA: 82279716624.28.94BA17C Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) by imf03.hostedemail.com (Postfix) with ESMTP id 6BCFC2000E for ; Fri, 28 Jun 2024 09:06:30 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=S28Rg9Kf; spf=pass (imf03.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.176 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=1719565582; 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:in-reply-to:references:references:dkim-signature; bh=YS5JkKLpH7rBBlezu3RsuBopuhXkHEJwrCmaySM0BwQ=; b=nlIsjcAaX6HZCZRY3Qk00JX/CHaQ9tqgdtDgylxwF4OVNKs8BAs4ycQmAKpWATVHwZEzGu zlMpBn55AdkmzaavctmZsOHAf40j/oaw8J8aCHUNqb1a8RLlASOrhVv/O+HJEN05SQnyTI BHz715c7jR6Hvlebsi0wUwizBO/rmik= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=S28Rg9Kf; spf=pass (imf03.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.176 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=1719565582; a=rsa-sha256; cv=none; b=XI1rTQ7haTmS3Lxz8oKV0vt/s2oyMEqr0AW4MZj6eEz3e5gcHtRR3zvvM16G3FucliuRQd pL3EFIXsiQk0E9y7MSit1bO75unBGcdI7kaNGahug7YBXewZPj6zPwvctd5JMan5fP8JT/ oe+0zlRAfL2pRmY1XEnMaJ6q15ogvTE= Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-718354c17e4so261579a12.1 for ; Fri, 28 Jun 2024 02:06:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565589; x=1720170389; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YS5JkKLpH7rBBlezu3RsuBopuhXkHEJwrCmaySM0BwQ=; b=S28Rg9Kfi8hVi3mSFd5TUOyftUHyd8o9BlU1aJJ6PTN01Sg4eR0BixtNl1RolnDz2q EiMhCG+x/qcioIWHUHH4n5OTNLizDLH42zlfPzhrn6VYm4cVkOYfvbNyjz7OIs/4BPAz U7qLWn090Sz9Q3uBEMHAq9s5jenIurDpCKN7eN6EOSutVaxXJb3drRXqQFqb3Ti4mT2j KQW8IJBFmIAhA0JN/TOH8UWBWrDwxOG6CCk29O4c1/MQJQUQHavobaxeTeAdojqfvaq/ kt+bCqLcKVKsouoIAlm8ZB8A4/YN9DKTUivX6EuOnWnAL1k31z2pyHK/t0NqnZlS9rwF Hteg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565589; x=1720170389; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YS5JkKLpH7rBBlezu3RsuBopuhXkHEJwrCmaySM0BwQ=; b=XHRPY1732nHGL5cln0xg9HceCAUiAvLg4vqCf8jyu2E60XskkUVyIUvhEUIO5+eAhS +D0jl4pNK3wmJvu9nIf6l3U300uIRFg0F6f37KtqOH1Pe3CroA9SSdHoZ1VDQIcxW1A/ 4yqYNLS+LlR98JoUtawrNRju58ah+caKDR8m7Qd5G4Ep42oymBIgt0qMNhE/Cx91s7nD wXJZ2ucEdF5VUfNJT/0S6OE8bbb0TE0frySihlD1IkIqwGR3cjjsKzT1VT1cw2Y4y/Te +JnhuxsdqLyD3c+9XPbJ7WYWNXVnZX1NCYHQj+3YPvOp4zFWYTfW4d5SbxwOGEVELeXo Z+aA== X-Forwarded-Encrypted: i=1; AJvYcCWWInEOPg7meJJwEI9iXQXt6YuJTrp3Yb+H/maE0PdMLaODUbubQ9XyqXNd+FyN4p3/Hifnx6xVfYEQ+U8TW3f+23Q= X-Gm-Message-State: AOJu0Yzb5CNA6Yb43XlUn5nLzVV0ETkoIrpPVIYYa4I/NsXzofklvQjz KviKMJSiZ/Z8/6lAyo92tC9Ta1c7HW7VSnw8wjHuS0RaGasffr5w X-Google-Smtp-Source: AGHT+IGOKlpBVO+d+BglzrA1U38WRrINDjYkTk3SrZCioD4n2rLQ44T0idrxtclcQs0Zl0I35xoQFQ== X-Received: by 2002:a05:6a20:b391:b0:1bd:2200:23f5 with SMTP id adf61e73a8af0-1bd2200244amr8943793637.39.1719565589157; Fri, 28 Jun 2024 02:06:29 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.06.20 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:06:28 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org, "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , Paolo Abeni Subject: [PATCH v4 10/11] net: Replace strcpy() with __get_task_comm() Date: Fri, 28 Jun 2024 17:05:16 +0800 Message-Id: <20240628090517.17994-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 6BCFC2000E X-Stat-Signature: 7q6wzt89xwp1imkc15onj5quptzpwa7r X-HE-Tag: 1719565590-964892 X-HE-Meta: U2FsdGVkX18vw6ePuwfIEAi28wHlcvGAHR7SEn52XROT5s4rBboV0QaMegeC4siwmcB739PNvh8LoTi+HcUsdctQv/4V8Q6jRSZYr4SJ4u49CO9ih8z1FVTuEJR1mw8bn1NPSYUZs0xMQBExuEiuToZ3tMkP56QW3U1re4UjDBI1ZHPExqH1WZlUFHY1QAaqtp2I+LwooikgxlXMxaEq0Pn2lakDEiarqb6FsFzsk1qF3YVURivBlj7aY38LQHoD8tgNYhQFjAwqMPBYhqbB+cSdLq/5HGOIN6e/IzbzGmp9Xu/6uubM0nhAmfzjYcbunhy5e5U1+a8BKpc0B16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7Y2AAoVXMTSAecTCSbZixn2oMKS62tq9yEIuIA5AjzTlqBbLc10ZOMBBKwyEMH5GRZpe4Q+I2XtOWNHdCNCKPZpG9bz/APf0FYANV0T4zkBIuH1/0ikWcRqvHa2reNArEAmdMdRN+WgnyrMNdpmx+vLv2imynHAZE1hxhmi7sYbazV4ZLmf8ZzCESmRLjObm7Q3Yen1c1g/a2O/yIv2/yQz6rIYfpOUV7i/ZuxCGdh8J 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: To prevent errors from occurring when the src string is longer than the dst string in strcpy(), we should use __get_task_comm() instead. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Cc: "David S. Miller" Cc: David Ahern Cc: Eric Dumazet Cc: Jakub Kicinski Cc: Paolo Abeni --- net/ipv6/ndisc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/ipv6/ndisc.c b/net/ipv6/ndisc.c index 254b192c5705..10d8e8c6ca02 100644 --- a/net/ipv6/ndisc.c +++ b/net/ipv6/ndisc.c @@ -1942,7 +1942,7 @@ static void ndisc_warn_deprecated_sysctl(const struct ctl_table *ctl, static char warncomm[TASK_COMM_LEN]; static int warned; if (strcmp(warncomm, current->comm) && warned < 5) { - strcpy(warncomm, current->comm); + __get_task_comm(warncomm, TASK_COMM_LEN, current); pr_warn("process `%s' is using deprecated sysctl (%s) net.ipv6.neigh.%s.%s - use net.ipv6.neigh.%s.%s_ms instead\n", warncomm, func, dev_name, ctl->procname, From patchwork Fri Jun 28 09:05:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13715786 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 7C082C2BBCA for ; Fri, 28 Jun 2024 09:06:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 116BD6B00AE; Fri, 28 Jun 2024 05:06:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0C6FC6B00AF; Fri, 28 Jun 2024 05:06:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EA9A56B00B0; Fri, 28 Jun 2024 05:06:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id CCB316B00AE for ; Fri, 28 Jun 2024 05:06:40 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 8D163C10DD for ; Fri, 28 Jun 2024 09:06:40 +0000 (UTC) X-FDA: 82279716960.10.7E177E2 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by imf19.hostedemail.com (Postfix) with ESMTP id B2EB61A0004 for ; Fri, 28 Jun 2024 09:06:37 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="NMv/nfRZ"; spf=pass (imf19.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.178 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=1719565579; 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:in-reply-to:references:references:dkim-signature; bh=uws88M38Us6mZwthahlWUK7fM6+CMa7f0lHdNpc5/ec=; b=XMEnxwp1NwTLoCnwW+6eG8hAcM2IfrhUw9F+x6RJ2CdrxbpykHATTiKNHPBTmlUpu1yIwd F+zGitlOL8HCmBi008H1LH0VH1+BPXAH9xBBMA/6Qc9jdKTifSbzRH7XeeJvmqrXv926cY Bsqa5vrem3RBF+4zkxhdt+og/AjoRlI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719565579; a=rsa-sha256; cv=none; b=E5oLUEkgpjdiQgZ16qYXC4Ej5TlX0ayN6idP+eOXI1dr/8dy6ctXkqGS9PHJEp5NWd85JJ Avi0iMpKo9TTGQNVQEvO9PS0KMdY0htkB1bisZhlOBIKIDUHHbMvWgzzaulcflWgT+YJq0 Tn+V0twsh59Vu408EfUKLMuMMrP/GyI= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="NMv/nfRZ"; spf=pass (imf19.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1faad2f1967so11843895ad.0 for ; Fri, 28 Jun 2024 02:06:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565596; x=1720170396; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=uws88M38Us6mZwthahlWUK7fM6+CMa7f0lHdNpc5/ec=; b=NMv/nfRZ6aU7o+FeWr9svZnFWUHvL4b+W/rv1DX2Y5BjnbGabn6HMKV8na47R6Dsny FSm0ruTCw3cRq1lrubo7Sb8+mwSswK3PK7mTGaU9Sjdv2Aq3RzvH77fSSeKHSYrcHGCq C+I2vS3utY1i1Cv/uLr38ak+meBI5QzDrhHtq4O5siLlWJ/KUmFmPIZb+iYvTMdT5Eyh k5jpcHLjyxk99zGJAcy0puNkk0VS5yFIVEIHwgof/sEk9mcR1FEsc5SP5LLCLNmzn8S6 jfv4cjU+mGckjp7NQNfJaiQDm0L759F7IPqpWavGhPWH9TWW3AgL/v9yBIwpQ25O/8il jtaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565596; x=1720170396; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uws88M38Us6mZwthahlWUK7fM6+CMa7f0lHdNpc5/ec=; b=JhXmSj3AnskEiKLBloFJvYgllkB9wGkMPCtGq//yEC1qm4s2z8ye0vEUsp8ifP87M5 bgGTq23Os5uIDEtmA+Ij4fxl7VvJ5gM76ybXIp6PrK84wmOcmlTqGXBJ1ytnc8n20p6e xp71LJFWuRk5wIjWg/y/xSEqLtpPlt8jYQAAGxNu31KpoqNzwgjtqLpVSibevddRfw+4 kgP+b6z/8Y1NRltb8CjE8R7xloqToCQ0NqHeLh5ZOATC2kAtZ5FKXTRpbqPEhdOxpOFc 4pS4YDbHc1qjRynD/wIWI2jLdDrPCosOWCzfi76G7jlpB70YzP+yfRXkA3j3OOGZBDl1 jcaQ== X-Forwarded-Encrypted: i=1; AJvYcCXmKWTLY9bfi/8VY8zefV4Dvg/IrnDvAFPxVo/Gb3CRShXQDFSchp4raadWmMd46QZNhBuoeLqRcWdFj1rE72F9St4= X-Gm-Message-State: AOJu0Yzg+S9uOh0J8PtOdQs+GhCMepjLqOFpeDm/KhksE9l1wHh65lx0 hQE/Qz0ZKjnSC+df3w5IqrYa9qD6pevhaYlU9YJ7FyJrME4lz5Ha X-Google-Smtp-Source: AGHT+IFavdIVLnysCQZL38ewD7/CaBekji3/EpakpMEZSkmUhyVQ9iiqTWsp/YgBhmvk5fMqdYz2hg== X-Received: by 2002:a17:903:244a:b0:1f9:e3fa:d932 with SMTP id d9443c01a7336-1fac7e975ffmr17035345ad.9.1719565596545; Fri, 28 Jun 2024 02:06:36 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.06.29 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:06:36 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org, Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie Subject: [PATCH v4 11/11] drm: Replace strcpy() with __get_task_comm() Date: Fri, 28 Jun 2024 17:05:17 +0800 Message-Id: <20240628090517.17994-11-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: B2EB61A0004 X-Stat-Signature: 14qeyxdfwdsyi6fgrn3cp5dw9mc1jd3q X-HE-Tag: 1719565597-399934 X-HE-Meta: U2FsdGVkX1/PxTErwcyhSsIPhv/8q+Pr+imEq7q0HQtg7UEp+j/7jwz0DOgVsTOC3yYeET5yyFpSbGQ6m0Cfw9/9dxRWq8/l9jcpEeEo9PCdxsm1gzySs869XpYG3qXAqDAM1ZHZd1KMbJTajgV8e+xlIXuDuJKZK/779n7boX012gVjdJo+tehRNJfJ/uoe9fQERcTeM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sJJJHSUvZNwDW1axw/ZJeYlMWh78V9viBEyYo5SNbnWnmAs+c9YU9NR07nsiXmtjaa1DmvGViZeoq9MTzRTZE9xRTYwAuJvhfWs6Zkki1GFhpQ0yE6c2QVwemP6MLWN/JndB7RfjTyfL3rKWjoEMylEEGLk+WZuR0hsmskeoUzknSnQq7YvlWSONSVnYXI67LOAeBlGYLbxtpRNEjV960JL9QQBdxmvmV0VvXODvH1bJe9haLIC02+XaucP0X9SY8UtqmQbBJUQEHUy/5Vrqe5Jva0Qda7j+Vs2vmDw= 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: To prevent erros from occurring when the src string is longer than the dst string in strcpy(), we should use __get_task_comm() instead. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Acked-by: Daniel Vetter Cc: Maarten Lankhorst Cc: Maxime Ripard Cc: Thomas Zimmermann Cc: David Airlie --- drivers/gpu/drm/drm_framebuffer.c | 2 +- drivers/gpu/drm/i915/i915_gpu_error.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/drm_framebuffer.c b/drivers/gpu/drm/drm_framebuffer.c index 888aadb6a4ac..25262b07ffaf 100644 --- a/drivers/gpu/drm/drm_framebuffer.c +++ b/drivers/gpu/drm/drm_framebuffer.c @@ -868,7 +868,7 @@ int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb, INIT_LIST_HEAD(&fb->filp_head); fb->funcs = funcs; - strcpy(fb->comm, current->comm); + __get_task_comm(fb->comm, sizeof(fb->comm), current); ret = __drm_mode_object_add(dev, &fb->base, DRM_MODE_OBJECT_FB, false, drm_framebuffer_free); diff --git a/drivers/gpu/drm/i915/i915_gpu_error.c b/drivers/gpu/drm/i915/i915_gpu_error.c index 625b3c024540..b2c16a53bd24 100644 --- a/drivers/gpu/drm/i915/i915_gpu_error.c +++ b/drivers/gpu/drm/i915/i915_gpu_error.c @@ -1411,7 +1411,7 @@ static bool record_context(struct i915_gem_context_coredump *e, rcu_read_lock(); task = pid_task(ctx->pid, PIDTYPE_PID); if (task) { - strcpy(e->comm, task->comm); + __get_task_comm(e->comm, sizeof(e->comm), task); e->pid = task->pid; } rcu_read_unlock();