Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp176533img; Tue, 19 Mar 2019 21:20:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqwatQ/IVxjgygCKmXZsxu479YXI6By+veN2auX5GR7KD8O22okfTXLKHGRK1qvQQF/Et3+8 X-Received: by 2002:a17:902:d894:: with SMTP id b20mr5781463plz.318.1553055610796; Tue, 19 Mar 2019 21:20:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553055610; cv=none; d=google.com; s=arc-20160816; b=ZyOyVrfLPvpx6KHkaKrpIy2/+l1wpCd0ievrbljBWBPwH7Kz31f5pe5YeoFBiuBR86 2Q4ItVppdwDzfS5POdDmnbZyMYJiv38oXZ95xlTo09JevvcOnHH8GNgEUZRJwUEwJhUQ Bgl8E0FWAQC74T68SEZ8JInd80ZvX9iwN/1F0zjEDxDWu5kOgAuxwdSE0AhHlNS7vmiR NbZUMbt53ghaU+02HlzBr9W+29zGs+xoMEiovj2EaYLJtNXUIkUvTjr9k0XWp4ceailA s0fibVIK0NqezP9SW6h7SzLhuj+VCDdpvzQJ58I/V3u548xe2aZZUIezOkOrw8bHv1kO hkFg== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=LpDdTveu9Pyz7AEhASEdQ2wlPx0GffNcfh/cp7mr9PQ=; b=FnXoEhHkaOsPyw23+5rzTRcyx5KH//yb0A8UKurQ7y1w/oSWBz14porKnLTWmdIOp5 aFnRijhWRtkkweu0hJlPCC9H6IMOwmdVDoAAOSFyQm7k14igwwWxZhdZVtc0iEngj8rQ HRNqqRIBl3ccdW+AmygUf/xozAZc37bbo+x9GxSHzoWLXQYfP9StlEWCtV3KJY+phtTa 55wXQoywmu41HUEj2ZWdq49gcDTN92LupleMt+mMQF5z/o39GGbUZYjDiataePVuXSQi GLPQaDHsYrK/IxSX+cGU7wzaJX6CIauve3Xwd7/yjcaK1j4JkEPXZPJVQuzth64Flsrg De3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=36qL7z0C; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w1si721781pgr.92.2019.03.19.21.19.55; Tue, 19 Mar 2019 21:20:10 -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; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=36qL7z0C; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726529AbfCTERq (ORCPT + 99 others); Wed, 20 Mar 2019 00:17:46 -0400 Received: from userp2130.oracle.com ([156.151.31.86]:41628 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726282AbfCTERq (ORCPT ); Wed, 20 Mar 2019 00:17:46 -0400 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x2K4EO0X118415; Wed, 20 Mar 2019 04:17:36 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : to : cc : references : from : message-id : date : mime-version : in-reply-to : content-type : content-transfer-encoding; s=corp-2018-07-02; bh=LpDdTveu9Pyz7AEhASEdQ2wlPx0GffNcfh/cp7mr9PQ=; b=36qL7z0CZAvapdr1fU9bsnQAzAN7MQhJ/vj05/JLz3UqFApkXJhRS/q972NrUu43h8og nHrupLrPf/hqjST3AFUzKinEmoykfSaEaTZnh2XH/jidnkEdckXP2zvqWTaUMAJP3+cu g1pcMFUjybyZi+w3wxy/6EOclcKU6q6azeh2jDa1EJGiLF3Mx7udDLhQpVA+NfkLUlGc P/S86n0ImxltncHLnCJCSvUJuTqHX+Ka+HYBKm4gcXdVtTFMdbatw8dqk8LHkrdm38+E y8C/AS3pTsfpBcvmkmJQT+4uAc5VvLVK/xZo8S86yvK8ygdnwexurllzOPXiow8A7Blw zA== Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by userp2130.oracle.com with ESMTP id 2r8rjure65-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Mar 2019 04:17:36 +0000 Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by aserv0021.oracle.com (8.14.4/8.14.4) with ESMTP id x2K4HYmK012343 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Mar 2019 04:17:35 GMT Received: from abhmp0013.oracle.com (abhmp0013.oracle.com [141.146.116.19]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id x2K4HXlD003651; Wed, 20 Mar 2019 04:17:33 GMT Received: from [10.182.69.106] (/10.182.69.106) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 19 Mar 2019 21:17:33 -0700 Subject: Re: general protection fault in loop_validate_file (2) To: Jan Kara Cc: syzbot , axboe@kernel.dk, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, syzkaller-bugs@googlegroups.com, penguin-kernel@i-love.sakura.ne.jp References: <00000000000098bf7d05845616d7@google.com> <20190319094136.GE17334@quack2.suse.cz> From: Dongli Zhang Message-ID: Date: Wed, 20 Mar 2019 12:21:29 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: <20190319094136.GE17334@quack2.suse.cz> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9200 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1903200037 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 3/19/19 5:41 PM, Jan Kara wrote: > On Mon 18-03-19 20:27:02, Dongli Zhang wrote: >> Hi Jan, >> >> Indeed there is another issue implicitly reported by below console output from >> syzkaller: >> >> [ 245.524424][ T9455] loop_reread_partitions: partition scan of loop0 () failed >> (rc=-13) >> [ 245.563340][ T9499] kasan: CONFIG_KASAN_INLINE enabled >> [ 245.576412][ T9489] __loop_clr_fd: partition scan of loop0 failed (rc=-13) >> [ 245.581275][ T9499] kasan: GPF could be caused by NULL-ptr deref or user >> memory access >> [ 245.602596][ T9499] general protection fault: 0000 [#1] PREEMPT SMP KASAN >> >> I think rc=-13 is because of below code at line 168: >> >> 162 int __blkdev_reread_part(struct block_device *bdev) >> 163 { >> 164 struct gendisk *disk = bdev->bd_disk; >> 165 >> 166 if (!disk_part_scan_enabled(disk) || bdev != bdev->bd_contains) >> 167 return -EINVAL; >> 168 if (!capable(CAP_SYS_ADMIN)) >> 169 return -EACCES; >> 170 >> 171 lockdep_assert_held(&bdev->bd_mutex); >> 172 >> 173 return rescan_partitions(disk, bdev); >> 174 } >> 175 EXPORT_SYMBOL(__blkdev_reread_part); >> >> I can reproduce this by 'chown username /dev/loop0' on my test machine. >> >> Taking 'losetup -d /dev/loop0' as sample, as /dev/loop0 belongs to my username, >> I am able to detach the loop without 'su'. >> >> However, because of above line 168, the partition scan would fail. >> >> Should we always assume the user should have admin privilege to detach >> the loop and this is not a bug? > > Firstly, __blkdev_reread_part() is used for all devices so we have to be > *very* careful when we relax the permission checks there. > > Secondly, I'm not convinced it is always safe to allow non-priviledged user > to force repartitioning of a device. That exposes the whole partition table > parsing code to non-priviledged user and thus increases possible attack > surface. > > But in this specific case we call __blkdev_reread_part() only to tear down > partitions. So in this specific case calling it by unpriviledged user is > fine plus leaving stale partitions behind is certainly not nice. What we > could do is: > > Export drop_partitions() functionality as a function like > __blkdev_drop_part() that would call drop_partitions(bdev->bd_disk, bdev). > It would expect (and assert) that &bdev->bd_mutex is already locked. It > should probably also replicate the check: > > if (!disk_part_scan_enabled(disk) || bdev != bdev->bd_contains) > return -EINVAL; > > from __blkdev_reread_part(). And we can call this from __loop_clr_fd(). > > Then we can just unexport __blkdev_reread_part() (since only loop.c is > using it) and fold it inside blkdev_reread_part(). > > What do you think? > > Honza > The idea is to duplicate a new caller of drop_partitions() which is specifically used by loop. I think it works. It is safe as well as the functionality is limited within loop. However, perhaps we should not regard this as a bug? Below is the sample to reproduce this issue: $ dd if=/dev/zero of=tmp.raw bs=1M count=100 oflag=direct $ parted tmp.raw --script mklabel msdos mkpart primary 0% 50% mkpart primary 50% 100% $ sudo chown zhang /dev/loop0 $ losetup -P /dev/loop0 tmp.raw $ ls -l /dev | grep loop0 brw-rw---- 1 zhang disk 7, 0 Mar 20 11:42 loop0 --> user (zhang) brw-rw---- 1 root disk 259, 0 Mar 20 11:42 loop0p1 --> root brw-rw---- 1 root disk 259, 1 Mar 20 11:42 loop0p2 --> root $ losetup -d /dev/loop From above case, /dev/loop0 is owned by user (zhang), while the partitions are owned by root. Should the detach of loop owned by user also unmaps all related partitions owned by root? Perhaps we should assume the '-P' option is always used by root? This is similar to the fact that the administrator should always use '-P' in order to enforce partscan when the loop is detached. Otherwise, the partitions belong to the loop would remain after 'losetup -d'. Another example is from kpartx as below. If the partitions are mapped via 'kpartx -av', the user should always run 'kpartx -d' to remove all partition mappings. Otherwise, there would be dm-0 and dm-1 left. $ sudo kpartx -av tmp.raw add map loop0p1 (253:0): 0 102400 linear 7:0 1 add map loop0p2 (253:1): 0 102399 linear 7:0 102401 $ ls -l /dev/mapper/ total 0 crw------- 1 root root 10, 236 3月 20 11:16 control lrwxrwxrwx 1 root root 7 3月 20 11:21 loop0p1 -> ../dm-0 lrwxrwxrwx 1 root root 7 3月 20 11:21 loop0p2 -> ../dm-1 $ sudo losetup -d /dev/loop0 $ ls -l /dev/mapper/ total 0 crw------- 1 root root 10, 236 3月 20 11:16 control lrwxrwxrwx 1 root root 7 3月 20 11:21 loop0p1 -> ../dm-0 lrwxrwxrwx 1 root root 7 3月 20 11:21 loop0p2 -> ../dm-1 Dongli Zhang