Received: by 2002:a25:86ce:0:0:0:0:0 with SMTP id y14csp346402ybm; Mon, 20 May 2019 17:40:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqzWKrBMFaB3R7aSkO3cMxBfvdeePs7P5S2NfcC14OdTOz68FJ8i/3M/GEQ7UM4NmbaMwqmq X-Received: by 2002:aa7:99dd:: with SMTP id v29mr85120612pfi.252.1558399242429; Mon, 20 May 2019 17:40:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558399242; cv=none; d=google.com; s=arc-20160816; b=Op8KZJVT+Tz9bYNJTzK+sSwt3V6/01mp8Iu9whNjJZ2lEMJGdd6BSnf60el/MaRu4A rvIkQrNG6RsfFWHFBXCIy/f26qp3XIPUaMPyt844/31fKl1mlhf3EClNu9JfCBsJbdoq hQyH/v2Uzl5+WjwTDYtynhvQeE8sP87afuKREYCamm7nUOSqq6g3Ua1N8BbQzKOS1cu5 67HpkCtFD1QjXUw6ybJ28V8vn5cwxyovOJzlY5o7mvEsWd/wQ+lbRJfPSp0cBFh7BK8o +L+WfMBv25sA+vFm/v5j+p/j/0V+9NGjqbqKIl0H00GervxCQYkLcS6V1LmvYTtnkh2U KqpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=MR44WtSwJe/zFDuWc/zOmzIMbmANGkp5pwKA03lAjbw=; b=YuVMGJJZnqk7PVyZaJvf0UamE7EMFfWJ4imJZwljrC2tU0YLhFUzrpsCetDCxRVLTT vXSBshkWtYvED10XK/WJJaE0sQyLNRK2QZboDuuzVFlNZjtZRDd6xMffk+XHadpTp2T2 4DClTIRWZWNgzGyofwKbn7qSmdw4J3PEb/7/dETDBMMmRMI6Q5uV3lphzVPsxIDujSgC V5dKc58H3hIXdHRmsgnXWh0XDNlmE1mNxL3jDswxR6tfv2NNrucp1p16as3XAHZ4qqjR u+CkA8pnk91WT4mBStmsG4M4eV1VPXiyNfBY7qp++k7jRJyrXC7yiyvOQyR3KIkmk0K9 7KWQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z123si20201204pfz.168.2019.05.20.17.40.27; Mon, 20 May 2019 17:40:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727424AbfEUAif (ORCPT + 99 others); Mon, 20 May 2019 20:38:35 -0400 Received: from master.debian.org ([82.195.75.110]:54046 "EHLO master.debian.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726586AbfEUAif (ORCPT ); Mon, 20 May 2019 20:38:35 -0400 Received: from pabs by master.debian.org with local (Exim 4.89) (envelope-from ) id 1hSsnC-0001uo-8h; Tue, 21 May 2019 00:38:10 +0000 From: Paul Wise To: Neil Horman , Alexander Viro , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Wise , Jakub Wilk Subject: [PATCH] coredump: Split pipe command whitespace before expanding template Date: Tue, 21 May 2019 08:37:56 +0800 Message-Id: <20190521003756.5236-1-pabs3@bonedaddy.net> X-Mailer: git-send-email 2.20.1 In-Reply-To: <201905202342.COVmaSLI%lkp@intel.com> References: <201905202342.COVmaSLI%lkp@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Save the offsets of the start of each argument to avoid having to update pointers to each argument after every corename krealloc and to avoid having to duplicate the memory for the dump command. Executable names containing spaces were previously being expanded from %e or %E and then split in the middle of the filename. This is incorrect behaviour since an argument list can represent arguments with spaces. The splitting could lead to extra arguments being passed to the core dump handler that it might have interpreted as options or ignored completely. Core dump handlers that are not aware of this Linux kernel issue will be using %e or %E without considering that it may be split and so they will be vulnerable to processes with spaces in their names breaking their argument list. If their internals are otherwise well written, such as if they are written in shell but quote arguments, they will work better after this change than before. If they are not well written, then there is a slight chance of breakage depending on the details of the code but they will already be fairly broken by the split filenames. Core dump handlers that are aware of this Linux kernel issue will be placing %e or %E as the last item in their core_pattern and then aggregating all of the remaining arguments into one, separated by spaces. Alternatively they will be obtaining the filename via other methods. Both of these will be compatible with the new arrangement. A side effect from this change is that unknown template types (for example %z) result in an empty argument to the dump handler instead of the argument being dropped. This is a desired change as: It is easier for dump handlers to process empty arguments than dropped ones, especially if they are written in shell or don't pass each template item with a preceding command-line option in order to differentiate between individual template types. Most core_patterns in the wild do not use options so they can confuse different template types (especially numeric ones) if an earlier one gets dropped in old kernels. If the kernel introduces a new template type and a core_pattern uses it, the core dump handler might not expect that the argument can be dropped in old kernels. For example, this can result in security issues when %d is dropped in old kernels. This happened with the corekeeper package in Debian and resulted in the interface between corekeeper and Linux having to be rewritten to use command-line options to differentiate between template types. The core_pattern for most core dump handlers is written by the handler author who would generally not insert unknown template types so this change should be compatible with all the core dump handlers that exist. Reported-by: Jakub Wilk Reported-in: <20190312145043.jxjoj66kqssptolr@jwilk.net> Reported-by: Paul Wise Reported-in: Suggestions-from: Jakub Wilk Signed-off-by: Paul Wise See-also: https://bugs.debian.org/924398 Fixes: commit 74aadce986052f20088c2678f589ea0e8d3a4b59 --- fs/coredump.c | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/fs/coredump.c b/fs/coredump.c index e42e17e55bfd..c45582b5856c 100644 --- a/fs/coredump.c +++ b/fs/coredump.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -187,11 +188,13 @@ static int cn_print_exe_file(struct core_name *cn) * name into corename, which must have space for at least * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator. */ -static int format_corename(struct core_name *cn, struct coredump_params *cprm) +static int format_corename(struct core_name *cn, struct coredump_params *cprm, + size_t **argv, int *argc) { const struct cred *cred = current_cred(); const char *pat_ptr = core_pattern; int ispipe = (*pat_ptr == '|'); + bool was_space = false; int pid_in_pattern = 0; int err = 0; @@ -201,12 +204,36 @@ static int format_corename(struct core_name *cn, struct coredump_params *cprm) return -ENOMEM; cn->corename[0] = '\0'; - if (ispipe) + if (ispipe) { + /* sizeof(core_pattern) / 2 is the maximum number of args. */ + int argvs = sizeof(core_pattern) / 2; + (*argv) = kmalloc_array(argvs, sizeof(**argv), GFP_KERNEL); + if (!(*argv)) + return -ENOMEM; + (*argv)[(*argc)++] = 0; ++pat_ptr; + } /* Repeat as long as we have more pattern to process and more output space */ while (*pat_ptr) { + /* + * Split on spaces before doing template expansion so that + * %e and %E don't get split if they have spaces in them + */ + if (ispipe) { + if (isspace(*pat_ptr)) { + was_space = true; + pat_ptr++; + continue; + } else if (was_space) { + was_space = false; + err = cn_printf(cn, "%c", '\0'); + if (err) + return err; + (*argv)[(*argc)++] = cn->used; + } + } if (*pat_ptr != '%') { err = cn_printf(cn, "%c", *pat_ptr++); } else { @@ -546,6 +573,8 @@ void do_coredump(const kernel_siginfo_t *siginfo) struct cred *cred; int retval = 0; int ispipe; + size_t *argv = NULL; + int argc = 0; struct files_struct *displaced; /* require nonrelative corefile path and be extra careful */ bool need_suid_safe = false; @@ -592,9 +621,10 @@ void do_coredump(const kernel_siginfo_t *siginfo) old_cred = override_creds(cred); - ispipe = format_corename(&cn, &cprm); + ispipe = format_corename(&cn, &cprm, &argv, &argc); if (ispipe) { + int argi; int dump_count; char **helper_argv; struct subprocess_info *sub_info; @@ -637,12 +667,16 @@ void do_coredump(const kernel_siginfo_t *siginfo) goto fail_dropcount; } - helper_argv = argv_split(GFP_KERNEL, cn.corename, NULL); + helper_argv = kmalloc_array(argc + 1, sizeof(*helper_argv), + GFP_KERNEL); if (!helper_argv) { printk(KERN_WARNING "%s failed to allocate memory\n", __func__); goto fail_dropcount; } + for (argi = 0; argi < argc; argi++) + helper_argv[argi] = cn.corename + argv[argi]; + helper_argv[argi] = NULL; retval = -ENOMEM; sub_info = call_usermodehelper_setup(helper_argv[0], @@ -652,7 +686,7 @@ void do_coredump(const kernel_siginfo_t *siginfo) retval = call_usermodehelper_exec(sub_info, UMH_WAIT_EXEC); - argv_free(helper_argv); + kfree(helper_argv); if (retval) { printk(KERN_INFO "Core dump to |%s pipe failed\n", cn.corename); @@ -766,6 +800,7 @@ void do_coredump(const kernel_siginfo_t *siginfo) if (ispipe) atomic_dec(&core_dump_count); fail_unlock: + kfree(argv); kfree(cn.corename); coredump_finish(mm, core_dumped); revert_creds(old_cred); -- 2.20.1