From patchwork Thu Jan 25 03:27:43 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taras Kondratiuk X-Patchwork-Id: 10183591 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 26D2860383 for ; Thu, 25 Jan 2018 03:41:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1992F286CD for ; Thu, 25 Jan 2018 03:41:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0E5002877F; Thu, 25 Jan 2018 03:41:35 +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=-14.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham 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 AF70E28712 for ; Thu, 25 Jan 2018 03:41:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933689AbeAYDk7 (ORCPT ); Wed, 24 Jan 2018 22:40:59 -0500 Received: from alln-iport-2.cisco.com ([173.37.142.89]:23533 "EHLO alln-iport-2.cisco.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933369AbeAYDha (ORCPT ); Wed, 24 Jan 2018 22:37:30 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=cisco.com; i=@cisco.com; l=1686; q=dns/txt; s=iport; t=1516851450; x=1518061050; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=7ti1CmKalB2rOy6NHEJ+DZLJMKGu7wCBQHH/kiDtVEo=; b=RW6UqDFNg6js/uUWmxlUgtO4eWJT0r+HgENdSQaOGoHQ3GZLunOxdR24 axqVWD0LWWFuCfb6jrG2tQEaQXn2teEZN6yXU8QCJanTSX/qljsH/QUeH Fp+3yxF4MrZTUKrDz6wc/KTipADmAB/5xaBxLnDlYeMQqhyqOqvqsXF8w o=; X-IronPort-AV: E=Sophos;i="5.46,409,1511827200"; d="scan'208";a="61314487" Received: from rcdn-core-6.cisco.com ([173.37.93.157]) by alln-iport-2.cisco.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 25 Jan 2018 03:28:00 +0000 Received: from sjc-ads-7132.cisco.com (sjc-ads-7132.cisco.com [10.30.217.207]) (authenticated bits=0) by rcdn-core-6.cisco.com (8.14.5/8.14.5) with ESMTP id w0P3Ruis007601 (version=TLSv1/SSLv3 cipher=AES128-SHA256 bits=128 verify=NO); Thu, 25 Jan 2018 03:27:59 GMT From: Taras Kondratiuk To: "H. Peter Anvin" , Al Viro , Arnd Bergmann , Rob Landley , Mimi Zohar , Jonathan Corbet , James McMechan Cc: initramfs@vger.kernel.org, Victor Kamensky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, xe-linux-external@cisco.com Subject: [PATCH v2 03/15] initramfs: store file name in name_buf Date: Thu, 25 Jan 2018 03:27:43 +0000 Message-Id: <1516850875-25066-4-git-send-email-takondra@cisco.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1516850875-25066-1-git-send-email-takondra@cisco.com> References: <1516850875-25066-1-git-send-email-takondra@cisco.com> X-Auto-Response-Suppress: DR, OOF, AutoReply X-Authenticated-User: takondra@cisco.com Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP There is already name_buf buffer pre-allocated for a file name. No need to allocate vcollected for every file. More over a name can be already stored in name_buf by read_info() function. Add memcpy_optional() function to handle such case. Signed-off-by: Taras Kondratiuk --- init/initramfs.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/init/initramfs.c b/init/initramfs.c index 49cd2681a26f..b6ee675e5cdb 100644 --- a/init/initramfs.c +++ b/init/initramfs.c @@ -210,7 +210,6 @@ static inline void __init eat(unsigned n) byte_count -= n; } -static __initdata char *vcollected; static __initdata char *collected; static long remains __initdata; static __initdata char *collect; @@ -324,6 +323,13 @@ static void __init clean_path(char *path, umode_t fmode) } } +static void *memcpy_optional(void *dest, const void *src, size_t n) +{ + if (dest != src) + return memcpy(dest, src, n); + return dest; +} + static __initdata int wfd; static int __init do_name(void) @@ -348,7 +354,8 @@ static int __init do_name(void) sys_fchmod(wfd, mode); if (body_len) sys_ftruncate(wfd, body_len); - vcollected = kstrdup(collected, GFP_KERNEL); + memcpy_optional(name_buf, collected, + N_ALIGN(name_len)); state = do_copy; } } @@ -375,8 +382,7 @@ static int __init do_copy(void) if (xwrite(wfd, victim, body_len) != body_len) error("write error"); sys_close(wfd); - do_utime(vcollected, mtime); - kfree(vcollected); + do_utime(name_buf, mtime); eat(body_len); state = do_skip; return 0;