From patchwork Sat Nov 20 11:27:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12630339 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 5BEFBC433EF for ; Sat, 20 Nov 2021 11:30:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 768E06B0075; Sat, 20 Nov 2021 06:28:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 718056B0078; Sat, 20 Nov 2021 06:28:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5B95B6B007B; Sat, 20 Nov 2021 06:28:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0045.hostedemail.com [216.40.44.45]) by kanga.kvack.org (Postfix) with ESMTP id 4E1A26B0075 for ; Sat, 20 Nov 2021 06:28:16 -0500 (EST) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 116961839A10A for ; Sat, 20 Nov 2021 11:28:06 +0000 (UTC) X-FDA: 78829084572.09.C39DE8A Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf14.hostedemail.com (Postfix) with ESMTP id E988260019AB for ; Sat, 20 Nov 2021 11:28:04 +0000 (UTC) Received: by mail-pl1-f171.google.com with SMTP id y8so10154184plg.1 for ; Sat, 20 Nov 2021 03:28:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Lt7iv6uOXlDKXawjf9ET+/GYW6XX3mUcHJCliRc171M=; b=kLCK+zjYkFrsRjI3TaLGvzv3dnaA97yKMIg42nSBAyE1CrZ1nNquqZklW0bBKpTZq7 pNmcot6EjZ9TWOZAABMubJPR5Kyan1K7kPxVdoomExOt05GYyGlv7AFj4Fr55pVPzg+o +XV8stHIt7WeJLXpo2UTuUMPz9ODHe6+c6eW5reRPjwArvPFK2a80PqrcNaeOD67hog4 NnqvdZZ/XZQpUoCXrgoZP4o2TVm+LoC3v/isOJg7qXtWFBbY6sFwjA9wPRRkXB/dcBFr +c0dso/sFQeMrtKScg4mVtk1QmQlKD1r3mxPSbfYJUngA0ElfoaTlYERHu6+ygIQpnyk kWmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Lt7iv6uOXlDKXawjf9ET+/GYW6XX3mUcHJCliRc171M=; b=dubZiBWrWy+kIGW/Ka89/ExvndmpHeLGpcAamaNphg0dVG3AmBBQiszrD/GTG6dEMw g7nUmHyOHFomuhkxCBiIWeQoznga40PMYjQ/0NP5wWkXyUvb0tt5/nwTmGyDtro0HB2K o+9WbhSnLsY4j9QWX95jYkCuW6QaLrwwyXlHyDCgV2b+DpFttPSxKMJLu6/ShwonEvL1 bBAt4oCdp7BTYnEa45O4tAxelIRZOM9N6n5hHQNHblIbpRzsjU/GuQouLlradsx2YRSI fG0gNT9CaUmrWsl1ULPCjEHNAbWpnOngUq63jdEnenQ4681QXr2mNP9yrAJDAMWP8oqt 9E0w== X-Gm-Message-State: AOAM5324cEI71+zx+9wc0vN7DJQUdGTwSrGz9317DFBeD+1Ocouq1Go2 Y8LqDT5ctlouggEBa1eG+Cw= X-Google-Smtp-Source: ABdhPJz9GZRYzuPNdb4pW+nAfRhUojoMVAgV/BEbkEffPAsNBPEqLyyAkiUpEHZPYz68GAmQ/wSOOg== X-Received: by 2002:a17:90b:3b8c:: with SMTP id pc12mr9369718pjb.9.1637407684839; Sat, 20 Nov 2021 03:28:04 -0800 (PST) Received: from vultr.guest ([66.42.104.82]) by smtp.gmail.com with ESMTPSA id q17sm2835490pfu.117.2021.11.20.03.28.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Nov 2021 03:28:04 -0800 (PST) From: Yafang Shao To: akpm@linux-foundation.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Kees Cook , David Hildenbrand , Mathieu Desnoyers , Arnaldo Carvalho de Melo , Andrii Nakryiko , Michal Miroslaw , Peter Zijlstra , Steven Rostedt , Matthew Wilcox , Al Viro , Petr Mladek Subject: [PATCH v2 4/7] fs/binfmt_elf: replace open-coded string copy with get_task_comm Date: Sat, 20 Nov 2021 11:27:35 +0000 Message-Id: <20211120112738.45980-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211120112738.45980-1-laoar.shao@gmail.com> References: <20211120112738.45980-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: E988260019AB X-Stat-Signature: agb3u5cws7yjj1yr6cb7mmp18rzu6tg8 Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=kLCK+zjY; spf=pass (imf14.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-HE-Tag: 1637407684-93487 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: It is better to use get_task_comm() instead of the open coded string copy as we do in other places. struct elf_prpsinfo is used to dump the task information in userspace coredump or kernel vmcore. Below is the verification of vmcore, crash> ps PID PPID CPU TASK ST %MEM VSZ RSS COMM 0 0 0 ffffffff9d21a940 RU 0.0 0 0 [swapper/0] > 0 0 1 ffffa09e40f85e80 RU 0.0 0 0 [swapper/1] > 0 0 2 ffffa09e40f81f80 RU 0.0 0 0 [swapper/2] > 0 0 3 ffffa09e40f83f00 RU 0.0 0 0 [swapper/3] > 0 0 4 ffffa09e40f80000 RU 0.0 0 0 [swapper/4] > 0 0 5 ffffa09e40f89f80 RU 0.0 0 0 [swapper/5] 0 0 6 ffffa09e40f8bf00 RU 0.0 0 0 [swapper/6] > 0 0 7 ffffa09e40f88000 RU 0.0 0 0 [swapper/7] > 0 0 8 ffffa09e40f8de80 RU 0.0 0 0 [swapper/8] > 0 0 9 ffffa09e40f95e80 RU 0.0 0 0 [swapper/9] > 0 0 10 ffffa09e40f91f80 RU 0.0 0 0 [swapper/10] > 0 0 11 ffffa09e40f93f00 RU 0.0 0 0 [swapper/11] > 0 0 12 ffffa09e40f90000 RU 0.0 0 0 [swapper/12] > 0 0 13 ffffa09e40f9bf00 RU 0.0 0 0 [swapper/13] > 0 0 14 ffffa09e40f98000 RU 0.0 0 0 [swapper/14] > 0 0 15 ffffa09e40f9de80 RU 0.0 0 0 [swapper/15] It works well as expected. Some comments are added to explain why we use the hard-coded 16. Suggested-by: Kees Cook Signed-off-by: Yafang Shao Reviewed-by: David Hildenbrand Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Andrii Nakryiko Cc: Michal Miroslaw Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Matthew Wilcox Cc: David Hildenbrand Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek --- fs/binfmt_elf.c | 2 +- include/linux/elfcore-compat.h | 5 +++++ include/linux/elfcore.h | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index f8c7f26f1fbb..b9a33cc34d6b 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -1585,7 +1585,7 @@ static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p, SET_UID(psinfo->pr_uid, from_kuid_munged(cred->user_ns, cred->uid)); SET_GID(psinfo->pr_gid, from_kgid_munged(cred->user_ns, cred->gid)); rcu_read_unlock(); - strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname)); + get_task_comm(psinfo->pr_fname, p); return 0; } diff --git a/include/linux/elfcore-compat.h b/include/linux/elfcore-compat.h index e272c3d452ce..54feb64e9b5d 100644 --- a/include/linux/elfcore-compat.h +++ b/include/linux/elfcore-compat.h @@ -43,6 +43,11 @@ struct compat_elf_prpsinfo __compat_uid_t pr_uid; __compat_gid_t pr_gid; compat_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid; + /* + * The hard-coded 16 is derived from TASK_COMM_LEN, but it can't be + * changed as it is exposed to userspace. We'd better make it hard-coded + * here. + */ char pr_fname[16]; char pr_psargs[ELF_PRARGSZ]; }; diff --git a/include/linux/elfcore.h b/include/linux/elfcore.h index 957ebec35aad..746e081879a5 100644 --- a/include/linux/elfcore.h +++ b/include/linux/elfcore.h @@ -65,6 +65,11 @@ struct elf_prpsinfo __kernel_gid_t pr_gid; pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid; /* Lots missing */ + /* + * The hard-coded 16 is derived from TASK_COMM_LEN, but it can't be + * changed as it is exposed to userspace. We'd better make it hard-coded + * here. + */ char pr_fname[16]; /* filename of executable */ char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */ };