From patchwork Fri Feb 16 20:33:40 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taras Kondratiuk X-Patchwork-Id: 10225677 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 6849060231 for ; Fri, 16 Feb 2018 20:39:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5976328C99 for ; Fri, 16 Feb 2018 20:39:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4E6A32908E; Fri, 16 Feb 2018 20:39:12 +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 D2B5928C99 for ; Fri, 16 Feb 2018 20:39:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751102AbeBPUi7 (ORCPT ); Fri, 16 Feb 2018 15:38:59 -0500 Received: from rcdn-iport-4.cisco.com ([173.37.86.75]:15587 "EHLO rcdn-iport-4.cisco.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750907AbeBPUeA (ORCPT ); Fri, 16 Feb 2018 15:34:00 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=cisco.com; i=@cisco.com; l=2499; q=dns/txt; s=iport; t=1518813240; x=1520022840; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=nvpl0b5eWYplhP1sX/h4M5FhyqcWUI82Pb+U0GTb0WU=; b=BchP61rTYB6VhmWCnCTwakG/h5Xx6+BtifmPo5d9xn4tzgrSGh+l1ebl wEC2Xd/dVPZIJaD9UqeuMdswzTQlTKIhmHol+T4sXZQP7/yqnsej7jClz eDTtzaoJTFxt3Q+tPlwL8MflfcQNSSIRKlxTe6ROmtCmfTC+V+kLllg+c 0=; X-IronPort-AV: E=Sophos;i="5.46,520,1511827200"; d="scan'208";a="356800564" Received: from rcdn-core-6.cisco.com ([173.37.93.157]) by rcdn-iport-4.cisco.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 16 Feb 2018 20:33:59 +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 w1GKXsMT015412 (version=TLSv1/SSLv3 cipher=AES128-SHA256 bits=128 verify=NO); Fri, 16 Feb 2018 20:33:58 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 v3 04/15] initramfs: remove unnecessary symlinks processing shortcut Date: Fri, 16 Feb 2018 20:33:40 +0000 Message-Id: <1518813234-5874-5-git-send-email-takondra@cisco.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1518813234-5874-1-git-send-email-takondra@cisco.com> References: <1518813234-5874-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)