From patchwork Thu Jan 25 03:27:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taras Kondratiuk X-Patchwork-Id: 10183601 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 94AD660383 for ; Thu, 25 Jan 2018 03:42:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 86924283BD for ; Thu, 25 Jan 2018 03:42:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7B4B728673; Thu, 25 Jan 2018 03:42:17 +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=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 23702283BD for ; Thu, 25 Jan 2018 03:42:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933236AbeAYDhU (ORCPT ); Wed, 24 Jan 2018 22:37:20 -0500 Received: from alln-iport-7.cisco.com ([173.37.142.94]:42689 "EHLO alln-iport-7.cisco.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932998AbeAYDhS (ORCPT ); Wed, 24 Jan 2018 22:37:18 -0500 X-Greylist: delayed 560 seconds by postgrey-1.27 at vger.kernel.org; Wed, 24 Jan 2018 22:37:18 EST DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=cisco.com; i=@cisco.com; l=2499; q=dns/txt; s=iport; t=1516851438; x=1518061038; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=nvpl0b5eWYplhP1sX/h4M5FhyqcWUI82Pb+U0GTb0WU=; b=RPW6eYAzDGxV6JRtw20Z3xaO1K0FiskDNpqkRT2ERLeP6A/DXWMSFla8 tb06Vyy6ua14qyWv+EUEj1SYHi5Q2ajlL83Kdvyr+9XndKbWLY0uznnDT 2URt2j4fk2QMLJ1Jgr61Ca/PAm8uYpEoiBjM2NlgRgjuZ6L8vAJqavsKi s=; X-IronPort-AV: E=Sophos;i="5.46,409,1511827200"; d="scan'208";a="60756978" Received: from rcdn-core-6.cisco.com ([173.37.93.157]) by alln-iport-7.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 w0P3Ruit007601 (version=TLSv1/SSLv3 cipher=AES128-SHA256 bits=128 verify=NO); Thu, 25 Jan 2018 03:28:00 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 04/15] initramfs: remove unnecessary symlinks processing shortcut Date: Thu, 25 Jan 2018 03:27:44 +0000 Message-Id: <1516850875-25066-5-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 Special handling of symlinks in do_header() assumes that name and body entries are sequential and reads them together. This shortcut has no real performance benefits, but it complicates changes to the state machine. Make handling of symlinks more similar to a regular files. Store name in name_buf and destination in symlink_buf. Signed-off-by: Taras Kondratiuk --- init/initramfs.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/init/initramfs.c b/init/initramfs.c index b6ee675e5cdb..d0ab7ad6ac05 100644 --- a/init/initramfs.c +++ b/init/initramfs.c @@ -266,16 +266,7 @@ static int __init do_header(void) state = do_skip; if (name_len <= 0 || name_len > PATH_MAX) return 0; - if (S_ISLNK(mode)) { - if (body_len > PATH_MAX) - return 0; - collect = collected = symlink_buf; - remains = N_ALIGN(name_len) + body_len; - next_state = do_symlink; - state = do_collect; - return 0; - } - if (S_ISREG(mode) || !body_len) + if (S_ISREG(mode) || S_ISLNK(mode) || !body_len) read_into(name_buf, N_ALIGN(name_len), do_name); return 0; } @@ -372,6 +363,11 @@ static int __init do_name(void) sys_chmod(collected, mode); do_utime(collected, mtime); } + } else if (S_ISLNK(mode)) { + if (body_len > PATH_MAX) + return 0; + memcpy_optional(name_buf, collected, N_ALIGN(name_len)); + read_into(symlink_buf, body_len, do_symlink); } return 0; } @@ -397,11 +393,12 @@ static int __init do_copy(void) static int __init do_symlink(void) { - collected[N_ALIGN(name_len) + body_len] = '\0'; - clean_path(collected, 0); - sys_symlink(collected + N_ALIGN(name_len), collected); - sys_lchown(collected, uid, gid); - do_utime(collected, mtime); + memcpy_optional(symlink_buf, collected, body_len); + symlink_buf[body_len] = '\0'; + clean_path(name_buf, 0); + sys_symlink(symlink_buf, name_buf); + sys_lchown(name_buf, uid, gid); + do_utime(name_buf, mtime); state = do_skip; next_state = do_reset; return 0; @@ -453,7 +450,7 @@ static char * __init unpack_to_rootfs(char *buf, unsigned long len) static __initdata char msg_buf[64]; header_buf = kmalloc(110, GFP_KERNEL); - symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL); + symlink_buf = kmalloc(PATH_MAX + 1, GFP_KERNEL); name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL); if (!header_buf || !symlink_buf || !name_buf)