Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp782638ybz; Wed, 22 Apr 2020 07:56:53 -0700 (PDT) X-Google-Smtp-Source: APiQypJ4Jz8isD1VqiB3G2z6C5khDA5rYwzWEmxkhH+k5D0WDxixFLw9qswzgfUpESmP3fOf4PoH X-Received: by 2002:a50:dac9:: with SMTP id s9mr23769297edj.313.1587567413442; Wed, 22 Apr 2020 07:56:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587567413; cv=none; d=google.com; s=arc-20160816; b=QRaTBwrn8VM+Jwl/tsuDxIOV4EXZDTZ1ih67923+xoXF5H3prSs+/NlJNpHamp9UgA JQIaw5LzRG7hJwkPOF1u7WPX7Pd6Ej3t9ZACHnzJVK9WmJh2Htz5Uvjyx7WSNS1x3B9u VDT3OkoinM8+Ih7b1if29BCb1sgrjIWBd/7dwlufWMupw7X3KodCsp6hsT1/NGRbJfWN k9wBEHXvlFs2fxBQ5LQV5o9x9hDkjU8coYL1p6dVsHft0xdqcpUvKqfJ7RFdun7r9Gzx 5n2Sb2tRRKFSIT2RuMJu47e36lcdDDLS871reAO90DazOMoi82/u0WXLQu+JQrQGaEkt 2Dhg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=2N6twM3ohdryTGyNJiMjndi+QjbAxfk1XVw5hf9m1G8=; b=0IWh/FPOz4US1d2D6biiv7Xf+3ymU9Bi60GjR/kAKK7wG4GwMPOnqK3RMQX0+3c3JM TtfET1kLuDIuE6H75ZA+dNNkS6PixyK5OZBTLxJtRmwtMR0ZeTRolFPlU0pNs/SOQyGY Km4lHFGpU31dN+9Qy4KNK0s94jFSAo7DN/6RW2MgE44r1euL3s4rkhUpX7COwxh2++Sx Ehs4Xg3gJph1ENjEoWlSVmz8qKE3dTVZb3DYt4f8bgIZ+52G0cLoRhMv4igUkd+f5b1p nP+SpmdODah+jXKuS2porY8tozYPj8y/n1DLNRL0fOGS3XzATfZfQbI3Hkww8sNBC94U 3Cxw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id mj2si3481264ejb.279.2020.04.22.07.56.29; Wed, 22 Apr 2020 07:56:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727933AbgDVOzR (ORCPT + 99 others); Wed, 22 Apr 2020 10:55:17 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:49617 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727059AbgDVOzO (ORCPT ); Wed, 22 Apr 2020 10:55:14 -0400 Received: from ip5f5af183.dynamic.kabel-deutschland.de ([95.90.241.131] helo=wittgenstein.fritz.box) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1jRGmM-0006CM-Ne; Wed, 22 Apr 2020 14:55:10 +0000 From: Christian Brauner To: Jens Axboe , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-api@vger.kernel.org Cc: Jonathan Corbet , Serge Hallyn , "Rafael J. Wysocki" , Tejun Heo , "David S. Miller" , Christian Brauner , Saravana Kannan , Jan Kara , David Howells , Seth Forshee , David Rheinsberg , Tom Gundersen , Christian Kellner , Dmitry Vyukov , =?UTF-8?q?St=C3=A9phane=20Graber?= , linux-doc@vger.kernel.org, netdev@vger.kernel.org, Steve Barber , Dylan Reid , Filipe Brandenburger , Kees Cook , Benjamin Elder , Akihiro Suda Subject: [PATCH v2 3/7] loop: use ns_capable for some loop operations Date: Wed, 22 Apr 2020 16:54:33 +0200 Message-Id: <20200422145437.176057-4-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.26.1 In-Reply-To: <20200422145437.176057-1-christian.brauner@ubuntu.com> References: <20200422145437.176057-1-christian.brauner@ubuntu.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following LOOP_GET_STATUS, LOOP_SET_STATUS, and LOOP_SET_BLOCK_SIZE operations are now allowed in non-initial namespaces. Most other operations were already possible before. Cc: Jens Axboe Cc: Seth Forshee Cc: Tom Gundersen Cc: Tejun Heo Cc: Christian Kellner Cc: Greg Kroah-Hartman Cc: "David S. Miller" Cc: David Rheinsberg Cc: Dmitry Vyukov Cc: "Rafael J. Wysocki" Signed-off-by: Christian Brauner --- /* v2 */ - Christian Brauner : - Adapated loop_capable() based on changes in the loopfs implementation patchset. Otherwise it is functionally equivalent to the v1 version. --- drivers/block/loop.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 52f7583dd17d..8e21d4b33e01 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1352,6 +1352,16 @@ void loopfs_evict_locked(struct loop_device *lo) } mutex_unlock(&loop_ctl_mutex); } + +static bool loop_capable(const struct loop_device *lo, int cap) +{ + return ns_capable(loopfs_ns(lo), cap); +} +#else /* !CONFIG_BLK_DEV_LOOPFS */ +static inline bool loop_capable(const struct loop_device *lo, int cap) +{ + return capable(cap); +} #endif /* CONFIG_BLK_DEV_LOOPFS */ static int @@ -1368,7 +1378,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) return err; if (lo->lo_encrypt_key_size && !uid_eq(lo->lo_key_owner, uid) && - !capable(CAP_SYS_ADMIN)) { + !loop_capable(lo, CAP_SYS_ADMIN)) { err = -EPERM; goto out_unlock; } @@ -1499,7 +1509,7 @@ loop_get_status(struct loop_device *lo, struct loop_info64 *info) memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); info->lo_encrypt_type = lo->lo_encryption ? lo->lo_encryption->number : 0; - if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { + if (lo->lo_encrypt_key_size && loop_capable(lo, CAP_SYS_ADMIN)) { info->lo_encrypt_key_size = lo->lo_encrypt_key_size; memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, lo->lo_encrypt_key_size); @@ -1723,7 +1733,7 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode, return loop_clr_fd(lo); case LOOP_SET_STATUS: err = -EPERM; - if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { + if ((mode & FMODE_WRITE) || loop_capable(lo, CAP_SYS_ADMIN)) { err = loop_set_status_old(lo, (struct loop_info __user *)arg); } @@ -1732,7 +1742,7 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode, return loop_get_status_old(lo, (struct loop_info __user *) arg); case LOOP_SET_STATUS64: err = -EPERM; - if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { + if ((mode & FMODE_WRITE) || loop_capable(lo, CAP_SYS_ADMIN)) { err = loop_set_status64(lo, (struct loop_info64 __user *) arg); } @@ -1742,7 +1752,7 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode, case LOOP_SET_CAPACITY: case LOOP_SET_DIRECT_IO: case LOOP_SET_BLOCK_SIZE: - if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN)) + if (!(mode & FMODE_WRITE) && !loop_capable(lo, CAP_SYS_ADMIN)) return -EPERM; /* Fall through */ default: -- 2.26.1