From patchwork Mon Mar 6 21:55:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9607979 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id CAAB7601D2 for ; Tue, 7 Mar 2017 02:15:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B855128477 for ; Tue, 7 Mar 2017 02:15:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ACBA328491; Tue, 7 Mar 2017 02:15:43 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 565FC28477 for ; Tue, 7 Mar 2017 02:15:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750815AbdCGCPa (ORCPT ); Mon, 6 Mar 2017 21:15:30 -0500 Received: from mail-pf0-f179.google.com ([209.85.192.179]:35168 "EHLO mail-pf0-f179.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754332AbdCGCPL (ORCPT ); Mon, 6 Mar 2017 21:15:11 -0500 Received: by mail-pf0-f179.google.com with SMTP id j5so68128269pfb.2 for ; Mon, 06 Mar 2017 18:15:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=vCrPpiBVBR2jJxoXP7hk6cOkP0Xxe4XbwqfDf88j0Dw=; b=NkM7PS3FzX3u4JIcUHorf2ltj2tmt4CWf7l5B+XIgP0Q94tLCimUbKMU3xpiNGlMPj CmplCMj6PMcGBkMK43DpIWcQgliftYnmV25YZxfC5DPNfk/zhmIpp/YSYdHFXPoRQ1Ki m+RYqwX45oY/JyUC5T9OAgUSN132LEoqoHGzs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=vCrPpiBVBR2jJxoXP7hk6cOkP0Xxe4XbwqfDf88j0Dw=; b=e+0sR4f1dE7zk6VvEg1jCYgsubAgueGCIq8ZU9wF4VQwqcF6tyNN4aV0WoPQdy3peL 6wRDYuiE+vpUdZDHKRcJ9x/zSoJHlSxd3L0XUWrYz6QINTcb0xj/Lb77aX2lnBAt3MRZ zWGOwG0ddXIWMlwgHqq3YTS/445c5s7WOYX5uJF0nTMMBmkyDkwTVwJPaUj+efWZWg1q Q1kegrSYBaSKRi7hg02DZi4hKXHqGCY3sofEyI++mzmfp466nwEmCrTJPwQhmyT79WnA TvRX3arLPOgr420MKdlFEVV+e2ELtPsoo7wTsK1l+16f2Sx7y9FFeBq7WOAAeZoPugQu ci8w== X-Gm-Message-State: AMke39mYehzpqQPDNTE4fWgwsjP7kXKmmrIM7kJTv9/ATOBsqswViye3ZsUoXEbtwXjO9CNf X-Received: by 10.98.97.68 with SMTP id v65mr23715891pfb.124.1488837341188; Mon, 06 Mar 2017 13:55:41 -0800 (PST) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id 90sm29006638pfl.24.2017.03.06.13.55.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Mar 2017 13:55:38 -0800 (PST) From: Kees Cook To: linux-kernel@vger.kernel.org Cc: Kees Cook , Nobuhiro Iwamatsu , Qiuxu Zhuo , Ard Biesheuvel , Anton Vorontsov , Colin Cross , Tony Luck , Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , "Rafael J. Wysocki" , Len Brown , Matt Fleming , Nathan Fontenot , Pan Xinhui , Daniel Axtens , Paul Gortmaker , Geliang Tang , linuxppc-dev@lists.ozlabs.org, linux-acpi@vger.kernel.org, linux-efi@vger.kernel.org, linux-doc@vger.kernel.org Subject: [PATCH 11/18] pstore: Always allocate buffer for decompression Date: Mon, 6 Mar 2017 13:55:25 -0800 Message-Id: <1488837332-71582-12-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1488837332-71582-1-git-send-email-keescook@chromium.org> References: <1488837332-71582-1-git-send-email-keescook@chromium.org> Sender: linux-acpi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently, pstore_mkfile() performs a memcpy() of the record contents, so it can live anywhere. However, this is needlessly wasteful. In preparation of pstore_mkfile() keeping the record contents, always allocate a buffer for the contents. Signed-off-by: Kees Cook --- fs/pstore/platform.c | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/fs/pstore/platform.c b/fs/pstore/platform.c index 879658b4c679..c0d401e732e6 100644 --- a/fs/pstore/platform.c +++ b/fs/pstore/platform.c @@ -768,6 +768,7 @@ EXPORT_SYMBOL_GPL(pstore_unregister); static void decompress_record(struct pstore_record *record) { int unzipped_len; + char *decompressed; /* Only PSTORE_TYPE_DMESG support compression. */ if (!record->compressed || record->type != PSTORE_TYPE_DMESG) { @@ -783,17 +784,29 @@ static void decompress_record(struct pstore_record *record) unzipped_len = pstore_decompress(record->buf, big_oops_buf, record->size, big_oops_buf_sz); - if (unzipped_len > 0) { - if (record->ecc_notice_size) - memcpy(big_oops_buf + unzipped_len, - record->buf + record->size, - record->ecc_notice_size); - kfree(record->buf); - record->buf = big_oops_buf; - record->size = unzipped_len; - record->compressed = false; - } else + if (unzipped_len <= 0) { pr_err("decompression failed: %d\n", unzipped_len); + return; + } + + /* Build new buffer for decompressed contents. */ + decompressed = kmalloc(unzipped_len + record->ecc_notice_size, + GFP_KERNEL); + if (!decompressed) { + pr_err("decompression ran out of memory\n"); + return; + } + memcpy(decompressed, big_oops_buf, unzipped_len); + + /* Append ECC notice to decompressed buffer. */ + memcpy(decompressed + unzipped_len, record->buf + record->size, + record->ecc_notice_size); + + /* Swap out compresed contents with decompressed contents. */ + kfree(record->buf); + record->buf = decompressed; + record->size = unzipped_len; + record->compressed = false; } /* @@ -819,13 +832,10 @@ void pstore_get_records(int quiet) decompress_record(&record); rc = pstore_mkfile(&record); - /* Free buffer other than big oops */ - if (record.buf != big_oops_buf) - kfree(record.buf); - if (rc && (rc != -EEXIST || !quiet)) failed++; + kfree(record.buf); memset(&record, 0, sizeof(record)); record.psi = psi; }