From patchwork Thu Jul 18 18:27:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roman Kisel X-Patchwork-Id: 13736636 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 22136C3DA61 for ; Thu, 18 Jul 2024 18:27:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A0C756B0085; Thu, 18 Jul 2024 14:27:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 995D66B0089; Thu, 18 Jul 2024 14:27:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 81CDA6B008C; Thu, 18 Jul 2024 14:27:58 -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 68C6F6B0085 for ; Thu, 18 Jul 2024 14:27:58 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0A89E401AF for ; Thu, 18 Jul 2024 18:27:58 +0000 (UTC) X-FDA: 82353707436.03.403BF76 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf14.hostedemail.com (Postfix) with ESMTP id 4A9BA100012 for ; Thu, 18 Jul 2024 18:27:55 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=SXd9PvAG; dmarc=pass (policy=none) header.from=linux.microsoft.com; spf=pass (imf14.hostedemail.com: domain of romank@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=romank@linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721327229; 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=GnOQJH6coKfJJds1hTuGDPjeAO6p0g0oj+N+wSCFGtg=; b=2sC5/qjeCMs99AlZEO2gVLY2LvoXwweHZUMBn5dL/kEcEYTJCrewjDPOnv5mulZ7TGBGOx E1zHvZlGw9f60c1TP3KqKVw/7IOdvgVp3SxGb6ZiZobVuTJrycxlVOhN3cErT1LwW1Y2qE PlgFs0nsi6S7p6+T4t+GRLot8cw4mqU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721327229; a=rsa-sha256; cv=none; b=4ktfvZQwTUlFpvOiaLo65VauJKmC8dUpIl5ZyGXwXLnJNFUrRshleGdkv9qQmrc0uJwPaC 98NRiF/DTVHl00Vs4W930mRpWPSJEeimjCdXBk3ivnORf/4S0FmmtvpnzVtNjzeq1W1EMt LthcgzrOqFtcqGiqFBn1/JGq3TD6kv0= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=SXd9PvAG; dmarc=pass (policy=none) header.from=linux.microsoft.com; spf=pass (imf14.hostedemail.com: domain of romank@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=romank@linux.microsoft.com Received: from xps-8930.corp.microsoft.com (unknown [131.107.160.48]) by linux.microsoft.com (Postfix) with ESMTPSA id CA41520B7177; Thu, 18 Jul 2024 11:27:53 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com CA41520B7177 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1721327273; bh=GnOQJH6coKfJJds1hTuGDPjeAO6p0g0oj+N+wSCFGtg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SXd9PvAGvsqXeqsEtaFnGlNW3LLr2x2Avdeqb1hqf8LG+RUfsgWYbdhtQ/xUkEQ7W VvKfpeAMZiTZ0PbjElW3ytMtBT6dyKkilUkNwJb8XgxAiSILm92KagJ2OayHQ7/WnH +2pJtCwKuJ9IxBoo+MAivQN2RIDd5y4qrhNjtcDM= From: Roman Kisel To: akpm@linux-foundation.org, apais@linux.microsoft.com, ardb@kernel.org, bigeasy@linutronix.de, brauner@kernel.org, ebiederm@xmission.com, jack@suse.cz, keescook@chromium.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, nagvijay@microsoft.com, oleg@redhat.com, tandersen@netflix.com, vincent.whitchurch@axis.com, viro@zeniv.linux.org.uk Cc: apais@microsoft.com, benhill@microsoft.com, ssengar@microsoft.com, sunilmut@microsoft.com, vdso@hexbites.dev Subject: [PATCH v3 1/2] coredump: Standartize and fix logging Date: Thu, 18 Jul 2024 11:27:24 -0700 Message-ID: <20240718182743.1959160-2-romank@linux.microsoft.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240718182743.1959160-1-romank@linux.microsoft.com> References: <20240718182743.1959160-1-romank@linux.microsoft.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 4A9BA100012 X-Stat-Signature: oq6pkgprud9nmn6izztxu476aca4hgx8 X-Rspam-User: X-HE-Tag: 1721327275-475544 X-HE-Meta: U2FsdGVkX19Dn+97gwMztwx5XrVtCWjA6OBm2xX9ngi+U8xRsq3oflbZgnwkD3EHZFA4ot2vk50c/sDO/zvcverm3qa5MF1PqVpFY1AQz2LX1l1Pp/S5aYFZMeeD+OjoMY+tELSrj5FSwLhI4PBFcU2tORlWR5GbtXuWBRLz5rvhIsuV3UB2IY82yPRrizelsd+TjuvW6otI/IlPDsdgQgHUR0KPxlOg5XXsoXREssadUYYeLU2mzR/yYEY0ANCyG43/kSwUdtfXyKOEoWM9L0xdtCIhBtgocYIG93Y+F/aO6pvvWZ3JBrj0Ol8yn673u0VYxW5CrmCx/KDgvYnjgQ0lcQp+segMQew5N/rME4ItQV8iNgsyijBDl20FrFZ7ruGcluWsr9pcdaHhdrIIgEvwszlhaWiRFlarS5+nLRUZmTHPOymanZsHA9oEL8EHNzDq+PEspCCesdbOxViyBcQuupYhnDKuM7ldzp9d81fHDQb+uTIt/5L3HWu03m44RNqx7m7bhN0xUSTBfxf53sNWI+621loiYYLrdH6yq/O7Rh7hQw53+AM7m6rYV8EDY33NM2xbn22F1p21nFb1GhRiGZf0ZJC5as6OgBkXTi2OQZLn5ocDdnqb12OQOnqV5anapMLnt0TrhLAf3EABXUZLQvJzzDLAIEbDS7gciZgtPq8WHGzCB6OjcWfJPDxhXPrj0zNaPUFnLKgyFCejIFlyKROqYUzdJQFCOCTef5pReQOf1f9AFGkY9ZLG/8PkmSFY1zFvZPlTxspXzqc+xGf1j/71KqzM9aOQMYdxLNTm+TVz+15wYl1PwchDbGUJuzmIk+uFP03dYMReQPgIkdREkLhadD/4LjoPZ4KCDY0zFBi8gk9L9+j3AXeFiLHN5m2PLMdJKxT/e+HslvN9mbCPyXrTg5vGvOiA5g51pv3KbGNxzqh960kuOtg0ZezkXmJXitPEP2MAn9pxJ8V LZo4S/lU bIKzSJyH6HvLOXxB/qDtS/Dq7WP+xmJR61oogLV8Trr0E1ss5H+kdRJPPjskIWo497WHCJnPwWobDFtUEhZ+4ibkYUjEP8rFrxAWV8lLm+RzgC2C/wAgP1zI+1HfMWVDEnC0Hgfm/HQRwBVQ4d+odwaMdjYWKftHT4GyMBuXFk2hEWtO60IJSqsPtFy45kB48NQEzijs70Veqw4qjcC6Ve5Eg5VQw96dWix/vPmxI9ooD8bWsh4t8O5H+jQAUDsPrLwwMKHzOfTOuMc8= 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: The coredump code does not log the process ID and the comm consistently, logs unescaped comm when it does log it, and does not always use the ratelimited logging. That makes it harder to analyze logs and puts the system at the risk of spamming the system log incase something crashes many times over and over again. Fix that by logging TGID and comm (escaped) consistently and using the ratelimited logging always. Signed-off-by: Roman Kisel Tested-by: Allen Pais > --- fs/coredump.c | 43 +++++++++++++++------------------------- include/linux/coredump.h | 22 ++++++++++++++++++++ 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/fs/coredump.c b/fs/coredump.c index a57a06b80f57..19d3343b93c6 100644 --- a/fs/coredump.c +++ b/fs/coredump.c @@ -586,8 +586,7 @@ void do_coredump(const kernel_siginfo_t *siginfo) struct subprocess_info *sub_info; if (ispipe < 0) { - printk(KERN_WARNING "format_corename failed\n"); - printk(KERN_WARNING "Aborting core\n"); + coredump_report_failure("format_corename failed, aborting core"); goto fail_unlock; } @@ -607,27 +606,21 @@ void do_coredump(const kernel_siginfo_t *siginfo) * right pid if a thread in a multi-threaded * core_pattern process dies. */ - printk(KERN_WARNING - "Process %d(%s) has RLIMIT_CORE set to 1\n", - task_tgid_vnr(current), current->comm); - printk(KERN_WARNING "Aborting core\n"); + coredump_report_failure("RLIMIT_CORE is set to 1, aborting core"); goto fail_unlock; } cprm.limit = RLIM_INFINITY; dump_count = atomic_inc_return(&core_dump_count); if (core_pipe_limit && (core_pipe_limit < dump_count)) { - printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n", - task_tgid_vnr(current), current->comm); - printk(KERN_WARNING "Skipping core dump\n"); + coredump_report_failure("over core_pipe_limit, skipping core dump"); goto fail_dropcount; } helper_argv = kmalloc_array(argc + 1, sizeof(*helper_argv), GFP_KERNEL); if (!helper_argv) { - printk(KERN_WARNING "%s failed to allocate memory\n", - __func__); + coredump_report_failure("%s failed to allocate memory", __func__); goto fail_dropcount; } for (argi = 0; argi < argc; argi++) @@ -644,8 +637,7 @@ void do_coredump(const kernel_siginfo_t *siginfo) kfree(helper_argv); if (retval) { - printk(KERN_INFO "Core dump to |%s pipe failed\n", - cn.corename); + coredump_report_failure("|%s pipe failed", cn.corename); goto close_fail; } } else { @@ -658,10 +650,8 @@ void do_coredump(const kernel_siginfo_t *siginfo) goto fail_unlock; if (need_suid_safe && cn.corename[0] != '/') { - printk(KERN_WARNING "Pid %d(%s) can only dump core "\ - "to fully qualified path!\n", - task_tgid_vnr(current), current->comm); - printk(KERN_WARNING "Skipping core dump\n"); + coredump_report_failure( + "this process can only dump core to a fully qualified path, skipping core dump"); goto fail_unlock; } @@ -730,13 +720,13 @@ void do_coredump(const kernel_siginfo_t *siginfo) idmap = file_mnt_idmap(cprm.file); if (!vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), current_fsuid())) { - pr_info_ratelimited("Core dump to %s aborted: cannot preserve file owner\n", - cn.corename); + coredump_report_failure("Core dump to %s aborted: " + "cannot preserve file owner", cn.corename); goto close_fail; } if ((inode->i_mode & 0677) != 0600) { - pr_info_ratelimited("Core dump to %s aborted: cannot preserve file permissions\n", - cn.corename); + coredump_report_failure("Core dump to %s aborted: " + "cannot preserve file permissions", cn.corename); goto close_fail; } if (!(cprm.file->f_mode & FMODE_CAN_WRITE)) @@ -757,7 +747,7 @@ void do_coredump(const kernel_siginfo_t *siginfo) * have this set to NULL. */ if (!cprm.file) { - pr_info("Core dump to |%s disabled\n", cn.corename); + coredump_report_failure("Core dump to |%s disabled", cn.corename); goto close_fail; } if (!dump_vma_snapshot(&cprm)) @@ -983,11 +973,10 @@ void validate_coredump_safety(void) { if (suid_dumpable == SUID_DUMP_ROOT && core_pattern[0] != '/' && core_pattern[0] != '|') { - pr_warn( -"Unsafe core_pattern used with fs.suid_dumpable=2.\n" -"Pipe handler or fully qualified core dump path required.\n" -"Set kernel.core_pattern before fs.suid_dumpable.\n" - ); + + coredump_report_failure("Unsafe core_pattern used with fs.suid_dumpable=2: " + "pipe handler or fully qualified core dump path required. " + "Set kernel.core_pattern before fs.suid_dumpable."); } } diff --git a/include/linux/coredump.h b/include/linux/coredump.h index 0904ba010341..45e598fe3476 100644 --- a/include/linux/coredump.h +++ b/include/linux/coredump.h @@ -43,8 +43,30 @@ extern int dump_align(struct coredump_params *cprm, int align); int dump_user_range(struct coredump_params *cprm, unsigned long start, unsigned long len); extern void do_coredump(const kernel_siginfo_t *siginfo); + +/* + * Logging for the coredump code, ratelimited. + * The TGID and comm fields are added to the message. + */ + +#define __COREDUMP_PRINTK(Level, Format, ...) \ + do { \ + char comm[TASK_COMM_LEN]; \ + \ + get_task_comm(comm, current); \ + printk_ratelimited(Level "coredump: %d(%*pE): " Format "\n", \ + task_tgid_vnr(current), (int)strlen(comm), comm, ##__VA_ARGS__); \ + } while (0) \ + +#define coredump_report(fmt, ...) __COREDUMP_PRINTK(KERN_INFO, fmt, ##__VA_ARGS__) +#define coredump_report_failure(fmt, ...) __COREDUMP_PRINTK(KERN_WARNING, fmt, ##__VA_ARGS__) + #else static inline void do_coredump(const kernel_siginfo_t *siginfo) {} + +#define coredump_report(...) +#define coredump_report_failure(...) + #endif #if defined(CONFIG_COREDUMP) && defined(CONFIG_SYSCTL)