Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2039315imm; Mon, 16 Jul 2018 00:40:13 -0700 (PDT) X-Google-Smtp-Source: AAOMgpe+b53OXV0tzERyhsC2gRCoENXMU8YfdlYPHKfUL9+RZGVwpuSZdWWTW7EuGcd0gMQBnHfU X-Received: by 2002:a65:5c03:: with SMTP id u3-v6mr13950908pgr.402.1531726813436; Mon, 16 Jul 2018 00:40:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531726813; cv=none; d=google.com; s=arc-20160816; b=P064GZwxHj7qJbAAGidUIdwlxvBle/qE1H92I9pNUwMXQLRzw6A0YkaRl84/Bu+Qf8 p50TRyIdR4RzocilD7nPRo6lcwakK+j5MEUmGJchK98v26ozy12U5qByqeojmcURTHxe ajYL/rT0o1CI8udm+E1LWWvZyQ0MrX9B5X5O+MXXzwEMMv+yqg46YS2XLW4Mvnd8CQ3g X/ALsu9L8uYWuqIuCt+ACcG4s3b2Yw/hMDHos9Mi9R6UwCGNDJhEkQ2/2D8An7Pi12CJ zhhV42LFhgihKQmgTA5YVqrWJKMgU5EtUI8kp+jg2q7FAknMkAk+xcFecqf8j8PsAf+0 Qy3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=IGTtEjrMUlZOxVgPGD7UDntn65RGhgAcXP27QgTKKeQ=; b=AuanireoIPlc+61Cx1mdQWrQ4J6eIxk+ikWcRqPjLUtgYbjzD3W0wrc4XVSiTQGaUU 4RDIj5TikO3u7XxzQS8At1IkxoAPYCHlzlPOWAhYTkNi1Og22gwCpYcLyJHewDCGwjuD SQobk+mYVy56K2x2QDTK46MyimlSe+pZhVsrOio4jKO9z2LvBTFmP7+4CKmN8Hz6B7JO 2pCyZaT3qvy3elmITlkBJkteQBB+tW5SBQAsLr36Jc0ikSoBFmOnWBvmqZxBVAUdLzVj kDo1QjYQ8ZU2IBlfxd6xRIbrGZbo3sHYtTuceQmg6oXT7jsBtL+zaQ9Qu5x6o7P/6Nln 46RA== ARC-Authentication-Results: i=1; mx.google.com; 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 v11-v6si15060148pgo.278.2018.07.16.00.39.58; Mon, 16 Jul 2018 00:40:13 -0700 (PDT) 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; 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 S2387751AbeGPIEv (ORCPT + 99 others); Mon, 16 Jul 2018 04:04:51 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:47002 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726898AbeGPIEu (ORCPT ); Mon, 16 Jul 2018 04:04:50 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 6CCD2C03; Mon, 16 Jul 2018 07:38:48 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+4349872271ece473a7c91190b68b4bac7c5dbc87@syzkaller.appspotmail.com, syzbot+40bd32c4d9a3cc12a339@syzkaller.appspotmail.com, syzbot+769c54e66f994b041be7@syzkaller.appspotmail.com, syzbot+0a89a9ce473936c57065@syzkaller.appspotmail.com, Theodore Tso , Jens Axboe Subject: [PATCH 4.17 57/67] loop: add recursion validation to LOOP_CHANGE_FD Date: Mon, 16 Jul 2018 09:35:26 +0200 Message-Id: <20180716073452.796619568@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180716073443.294323458@linuxfoundation.org> References: <20180716073443.294323458@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.17-stable review patch. If anyone has any objections, please let me know. ------------------ From: Theodore Ts'o commit d2ac838e4cd7e5e9891ecc094d626734b0245c99 upstream. Refactor the validation code used in LOOP_SET_FD so it is also used in LOOP_CHANGE_FD. Otherwise it is possible to construct a set of loop devices that all refer to each other. This can lead to a infinite loop in starting with "while (is_loop_device(f)) .." in loop_set_fd(). Fix this by refactoring out the validation code and using it for LOOP_CHANGE_FD as well as LOOP_SET_FD. Reported-by: syzbot+4349872271ece473a7c91190b68b4bac7c5dbc87@syzkaller.appspotmail.com Reported-by: syzbot+40bd32c4d9a3cc12a339@syzkaller.appspotmail.com Reported-by: syzbot+769c54e66f994b041be7@syzkaller.appspotmail.com Reported-by: syzbot+0a89a9ce473936c57065@syzkaller.appspotmail.com Signed-off-by: Theodore Ts'o Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- drivers/block/loop.c | 68 ++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 30 deletions(-) --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -644,6 +644,36 @@ static void loop_reread_partitions(struc __func__, lo->lo_number, lo->lo_file_name, rc); } +static inline int is_loop_device(struct file *file) +{ + struct inode *i = file->f_mapping->host; + + return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; +} + +static int loop_validate_file(struct file *file, struct block_device *bdev) +{ + struct inode *inode = file->f_mapping->host; + struct file *f = file; + + /* Avoid recursion */ + while (is_loop_device(f)) { + struct loop_device *l; + + if (f->f_mapping->host->i_bdev == bdev) + return -EBADF; + + l = f->f_mapping->host->i_bdev->bd_disk->private_data; + if (l->lo_state == Lo_unbound) { + return -EINVAL; + } + f = l->lo_backing_file; + } + if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) + return -EINVAL; + return 0; +} + /* * loop_change_fd switched the backing store of a loopback device to * a new file. This is useful for operating system installers to free up @@ -673,14 +703,15 @@ static int loop_change_fd(struct loop_de if (!file) goto out; + error = loop_validate_file(file, bdev); + if (error) + goto out_putf; + inode = file->f_mapping->host; old_file = lo->lo_backing_file; error = -EINVAL; - if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) - goto out_putf; - /* size of the new backing store needs to be the same */ if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) goto out_putf; @@ -706,13 +737,6 @@ static int loop_change_fd(struct loop_de return error; } -static inline int is_loop_device(struct file *file) -{ - struct inode *i = file->f_mapping->host; - - return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; -} - /* loop sysfs attributes */ static ssize_t loop_attr_show(struct device *dev, char *page, @@ -877,7 +901,7 @@ static int loop_prepare_queue(struct loo static int loop_set_fd(struct loop_device *lo, fmode_t mode, struct block_device *bdev, unsigned int arg) { - struct file *file, *f; + struct file *file; struct inode *inode; struct address_space *mapping; int lo_flags = 0; @@ -896,29 +920,13 @@ static int loop_set_fd(struct loop_devic if (lo->lo_state != Lo_unbound) goto out_putf; - /* Avoid recursion */ - f = file; - while (is_loop_device(f)) { - struct loop_device *l; - - if (f->f_mapping->host->i_bdev == bdev) - goto out_putf; - - l = f->f_mapping->host->i_bdev->bd_disk->private_data; - if (l->lo_state == Lo_unbound) { - error = -EINVAL; - goto out_putf; - } - f = l->lo_backing_file; - } + error = loop_validate_file(file, bdev); + if (error) + goto out_putf; mapping = file->f_mapping; inode = mapping->host; - error = -EINVAL; - if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) - goto out_putf; - if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || !file->f_op->write_iter) lo_flags |= LO_FLAGS_READ_ONLY;