Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3476312imu; Sun, 11 Nov 2018 15:59:54 -0800 (PST) X-Google-Smtp-Source: AJdET5dftxFgncRhbHNQidNnPOKzdNcNbqQSu1+saJ4nk4vU2BjiHR5n/mR5PoAdmg3RsddjPjzc X-Received: by 2002:a63:2f86:: with SMTP id v128mr14998987pgv.407.1541980794577; Sun, 11 Nov 2018 15:59:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541980794; cv=none; d=google.com; s=arc-20160816; b=KHnPGnc4/rIpkQbu7BO4GIMaOkR+1xYiKE8sEr9qrPFT2IIZJkbVyYM6pLnFEldv1a XUMBWBZ6YZCo9Pyn40bJb+SkDx4AhIaMLKOC+/BfImRaYCd+CLD4eDMFw5mMm+unUIS+ PeDz/1v7sdoxWPnDhC9w/QslPHKG8rz0K46Ir6ADRoo6mot6SjxQvfVO01acwC6JHKlC qAAD2SIPOjFeM5LL1nmtJVA4kUJhFzO+WShjb6bO+3GKEQra4/tXE9Zkdd2+gfB/cc/U UJHhwd5FOkSjbr/gTihTYXYP+7XK2RoAVPgyln+yrBxKVEDKWNYCpk7TVtnkNgXVC7Sn s40g== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=tcT6XGpHdOZGsnJV1RGRD7blzrO8Dy6Pyj18vHLvJbY=; b=tnCDNRopqQHJJnh5MvatQ7W5t0jV5ZcbSV7c3siYcfckQeYz70zNf/Zm/O77cnDNnf IHZk0+SUp+WP8B2TMzu9+pYcuftZk3dBCbICjdDHsYHn9r7C9wRz7bbNjbGC/AwtPQc5 UzmOjHxvNF5Q6t4hApQ4nq7qzFO9x6Nw0lBaRODZ7RpA2F5M/gjvKDV/y/KV0fHSnBsi oECWS+h/uhkghKZ69o7YPoBMIT8s0Jq3smayGdqgr3B25EStYejYJjhqfRU3reQT8LlA 54Pah0g7td3++R4GYhvTrkFmYp80aGNlYM2MWmL9NV/u/vA67u+AymFGiwVD5XjhAmjG /ZNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=lrb2+SjI; 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 t64-v6si17154564pfd.58.2018.11.11.15.59.39; Sun, 11 Nov 2018 15:59:54 -0800 (PST) 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; dkim=pass header.i=@kernel.org header.s=default header.b=lrb2+SjI; 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 S1732388AbeKLISF (ORCPT + 99 others); Mon, 12 Nov 2018 03:18:05 -0500 Received: from mail.kernel.org ([198.145.29.99]:35336 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732375AbeKLISE (ORCPT ); Mon, 12 Nov 2018 03:18:04 -0500 Received: from localhost (unknown [206.108.79.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B60F1223D0; Sun, 11 Nov 2018 22:28:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541975288; bh=vXVjGoar1jJGjJ5wVEmOd00Dqxu2arR3UStjlA9+UHQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lrb2+SjIiT9/KaFOWBFNOuA15Kj97Y0dhc4K7ohfMk23O4vqHb/zUTui0IY5Yv+Kf v4AEI08qkrZrdtNzXilU0mEbMCm7ur/VyPdTuvhH8ev0XOevzu/UkuO/ZEPd95gPl3 82LZz7MuwijfroBKGHmKqLJ4cfpGr4RC5xBPtj+4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Theodore Tso , stable@kernel.org, syzbot+e81ccd4744c6c4f71354@syzkaller.appspotmail.com Subject: [PATCH 4.19 226/361] ext4: fix EXT4_IOC_SWAP_BOOT Date: Sun, 11 Nov 2018 14:19:33 -0800 Message-Id: <20181111221651.118847765@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181111221619.915519183@linuxfoundation.org> References: <20181111221619.915519183@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Theodore Ts'o commit 18aded17492088962ef43f00825179598b3e8c58 upstream. The code EXT4_IOC_SWAP_BOOT ioctl hasn't been updated in a while, and it's a bit broken with respect to more modern ext4 kernels, especially metadata checksums. Other problems fixed with this commit: * Don't allow installing a DAX, swap file, or an encrypted file as a boot loader. * Respect the immutable and append-only flags. * Wait until any DIO operations are finished *before* calling truncate_inode_pages(). * Don't swap inode->i_flags, since these flags have nothing to do with the inode blocks --- and it will give the IMA/audit code heartburn when the inode is evicted. Signed-off-by: Theodore Ts'o Cc: stable@kernel.org Reported-by: syzbot+e81ccd4744c6c4f71354@syzkaller.appspotmail.com Signed-off-by: Greg Kroah-Hartman --- fs/ext4/ioctl.c | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) --- a/fs/ext4/ioctl.c +++ b/fs/ext4/ioctl.c @@ -67,7 +67,6 @@ static void swap_inode_data(struct inode ei1 = EXT4_I(inode1); ei2 = EXT4_I(inode2); - swap(inode1->i_flags, inode2->i_flags); swap(inode1->i_version, inode2->i_version); swap(inode1->i_blocks, inode2->i_blocks); swap(inode1->i_bytes, inode2->i_bytes); @@ -85,6 +84,21 @@ static void swap_inode_data(struct inode i_size_write(inode2, isize); } +static void reset_inode_seed(struct inode *inode) +{ + struct ext4_inode_info *ei = EXT4_I(inode); + struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); + __le32 inum = cpu_to_le32(inode->i_ino); + __le32 gen = cpu_to_le32(inode->i_generation); + __u32 csum; + + if (!ext4_has_metadata_csum(inode->i_sb)) + return; + + csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, sizeof(inum)); + ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, sizeof(gen)); +} + /** * Swap the information from the given @inode and the inode * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other @@ -102,10 +116,13 @@ static long swap_inode_boot_loader(struc struct inode *inode_bl; struct ext4_inode_info *ei_bl; - if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode)) + if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode) || + IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) || + ext4_has_inline_data(inode)) return -EINVAL; - if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN)) + if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) || + !inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN)) return -EPERM; inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO); @@ -120,13 +137,13 @@ static long swap_inode_boot_loader(struc * that only 1 swap_inode_boot_loader is running. */ lock_two_nondirectories(inode, inode_bl); - truncate_inode_pages(&inode->i_data, 0); - truncate_inode_pages(&inode_bl->i_data, 0); - /* Wait for all existing dio workers */ inode_dio_wait(inode); inode_dio_wait(inode_bl); + truncate_inode_pages(&inode->i_data, 0); + truncate_inode_pages(&inode_bl->i_data, 0); + handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2); if (IS_ERR(handle)) { err = -EINVAL; @@ -159,6 +176,8 @@ static long swap_inode_boot_loader(struc inode->i_generation = prandom_u32(); inode_bl->i_generation = prandom_u32(); + reset_inode_seed(inode); + reset_inode_seed(inode_bl); ext4_discard_preallocations(inode); @@ -169,6 +188,7 @@ static long swap_inode_boot_loader(struc inode->i_ino, err); /* Revert all changes: */ swap_inode_data(inode, inode_bl); + ext4_mark_inode_dirty(handle, inode); } else { err = ext4_mark_inode_dirty(handle, inode_bl); if (err < 0) { @@ -178,6 +198,7 @@ static long swap_inode_boot_loader(struc /* Revert all changes: */ swap_inode_data(inode, inode_bl); ext4_mark_inode_dirty(handle, inode); + ext4_mark_inode_dirty(handle, inode_bl); } } ext4_journal_stop(handle);