From patchwork Tue Apr 26 19:30:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Seth Forshee X-Patchwork-Id: 8943601 Return-Path: X-Original-To: patchwork-selinux@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 9F14D9F1D3 for ; Tue, 26 Apr 2016 19:38:58 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B7A342015A for ; Tue, 26 Apr 2016 19:38:57 +0000 (UTC) Received: from emsm-gh1-uea11.nsa.gov (emsm-gh1-uea11.nsa.gov [8.44.101.9]) by mail.kernel.org (Postfix) with ESMTP id 8A43320166 for ; Tue, 26 Apr 2016 19:38:56 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.24,538,1454976000"; d="scan'208";a="15642644" IronPort-PHdr: =?us-ascii?q?9a23=3Ab0rimRG+arejFvrLx5aF4J1GYnF86YWxBRYc798d?= =?us-ascii?q?s5kLTJ79r8WwAkXT6L1XgUPTWs2DsrQf27qQ7PGrADdYqb+681k8M7V0Hycfjs?= =?us-ascii?q?sXmwFySOWkMmbcaMDQUiohAc5ZX0Vk9XzoeWJcGcL5ekGA6ibqtW1aJBzzOEJP?= =?us-ascii?q?K/jvHcaK1oLsh7D0pMyYOl4QzBOGIppMbzyO5T3LsccXhYYwYo0Q8TDu5kVyRu?= =?us-ascii?q?JN2GlzLkiSlRuvru25/Zpk7jgC86l5r50IeezAcq85Vb1VCig9eyBwvZWz9EqL?= =?us-ascii?q?cQzarFYGU25erVwAKQnI4BzgW573+GOuu+B81SWXJ8jeXbU1Qi+j6KEtQxjt3m?= =?us-ascii?q?NPFDM/7CnwlMFql75BrR+970h8zojIJoaLOeBlYrnQcM8ySm9IX8IXXCtEVMf0?= =?us-ascii?q?Ro8GFOYIPPwQjITnqFwI5U+3CgqEHu7jyjZUwHTx2Pt+m8EgEgyO5wohA8ADqj?= =?us-ascii?q?yAt9P0KLUfS8ivwaXIxCmFZPRTj3O15I2Nfh0/rPWkR798ccPNj0IoEkeNtVWZ?= =?us-ascii?q?roz4Nnux16xZqGif7sJjWPiphmphrBt+9Hzn6s4yi4WBuYUSxlDD6D50iNI5Ot?= =?us-ascii?q?inEmZ6Z9C+GZddvi3cMJF5FIdqaXtpsSBy7Loco5+gNHwBz5E/2gLSceeJdJKQ?= =?us-ascii?q?7xvoWc6eJD55gDRuf7fpw16b9UWww+vwW9P87ldQqWIRjNjWsVgf2hrS9I6DS/?= =?us-ascii?q?1g7gGmwznJygOFrqlmKEZ8tqPBItZ1wLc7lbIIrF/OWCTxnROyxI2beEts3uG5?= =?us-ascii?q?9+PhKuHkopaNK4hyohn5Na8um8GlDOgjdA4DQz7fsa6ekvXJ51DyTf1kj/Aynr?= =?us-ascii?q?KT+MTQK8UWoqKiKwFU34Iv8BukSTC834JLs2MAKQd+dQ6Hx6buIUrOLf2wWeyy?= =?us-ascii?q?n1TqnjBx3PDLM5XqC5PKKHnImbOndrF4vR0PgDEvxMxSssoHQooKJ+j+Dwqo7I?= =?us-ascii?q?TV?= X-IPAS-Result: =?us-ascii?q?A2EEBQBXwx9X/wHyM5BeHAGDG4FQu3cfhzpMAQEBAQEBAgJ?= =?us-ascii?q?iJ4ItghUBAQQBAiQTFCALAwMJAQEXKQgIAwEoBRURBgEHCwUYBIgJxDoBCwEdh?= =?us-ascii?q?iGIWhEBA4VxAQSNVHSJSIFVjESJT4VARY5rYoIFG4FpTodxCBeBHgEBAQ?= Received: from unknown (HELO tarius.tycho.ncsc.mil) ([144.51.242.1]) by emsm-gh1-uea11.nsa.gov with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 26 Apr 2016 19:38:25 +0000 Received: from prometheus.infosec.tycho.ncsc.mil (prometheus [192.168.25.40]) by tarius.tycho.ncsc.mil (8.14.4/8.14.4) with ESMTP id u3QJcPX2012163; Tue, 26 Apr 2016 15:38:25 -0400 Received: from tarius.tycho.ncsc.mil (tarius.infosec.tycho.ncsc.mil [144.51.242.1]) by prometheus.infosec.tycho.ncsc.mil (8.15.2/8.15.2) with ESMTP id u3QJUpF1172205 for ; Tue, 26 Apr 2016 15:30:51 -0400 Received: from goalie.tycho.ncsc.mil (goalie [144.51.242.250]) by tarius.tycho.ncsc.mil (8.14.4/8.14.4) with ESMTP id u3QJUoHI010214 for ; Tue, 26 Apr 2016 15:30:50 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0A+BQAQwR9X/yUp0ApeHYMbgVCCc7R9i2JMAQEBAQEBZieEQgEFJ1IQCEksKwYBEogqxGeGIYhvhXEFjVR0iUiBVYxEiU+FQEWOa2KBNgFOG4FpHjCHcYE9AQEB X-IPAS-Result: A0A+BQAQwR9X/yUp0ApeHYMbgVCCc7R9i2JMAQEBAQEBZieEQgEFJ1IQCEksKwYBEogqxGeGIYhvhXEFjVR0iUiBVYxEiU+FQEWOa2KBNgFOG4FpHjCHcYE9AQEB X-IronPort-AV: E=Sophos;i="5.24,537,1454994000"; d="scan'208";a="5410351" Received: from emsm-gh1-uea11.corp.nsa.gov (HELO emsm-gh1-uea11.nsa.gov) ([10.208.41.37]) by goalie.tycho.ncsc.mil with ESMTP; 26 Apr 2016 15:30:51 -0400 IronPort-PHdr: =?us-ascii?q?9a23=3AGA4JwR+oHQ0c9/9uRHKM819IXTAuvvDOBiVQ1KB5?= =?us-ascii?q?2+McTK2v8tzYMVDF4r011RmSDdWdtKMP0rGO+4nbGkU+or+5+EgYd5JNUxJXwe?= =?us-ascii?q?43pCcHRPC/NEvgMfTxZDY7FskRHHVs/nW8LFQHUJ2mPw6anHS+4HYoFwnlMkIt?= =?us-ascii?q?f6KuSt6U0JX8jrvqs7ToICx2xxOFKYtoKxu3qQiD/uI3uqBFbpgL9x3Sv3FTcP?= =?us-ascii?q?5Xz247bXianhL7+9vitMU7q3cYk7sb+sVBSaT3ebgjBfwdVWx+cjN92Mq+/zTZ?= =?us-ascii?q?TADH2T1UeGQbnhdSBgHDplmuU53wvyf3rO9VyCybJtb3SrZyUjOnueMjaxLulG?= =?us-ascii?q?8gKTMj633Nh8FsxPZYrRS94RthzpTPfJuYOeZWcabUfNdcTm1ECIIZfihIGY+1?= =?us-ascii?q?bpFHLOcbMuFe5930qFgmtxayBQCwQujoz2kMzlr33qBy6OIgDRPLxkR0BNQPrG?= =?us-ascii?q?bZtv3vOasSWPzzx67Nm3GLY/YT1Trj5YzgaB0tofiQG7l3dI6Z8kkmEAPYhx20?= =?us-ascii?q?qMSxJDOR18wOvnKd4u4mUvig3SpvhwhvozTn+cArhonOm5kYggTI6iBmmq40I9?= =?us-ascii?q?OiQUJ2aNLiF4Ff4WXSHZF3S8VqZWZyoichgukNvpujYTQH0o8pwATHZvWMfqCH?= =?us-ascii?q?5xviUKCaJjIuwDpMfL+vjhC+/FLo9OzmUYHgzldQqQJXn9/NqDYJ1hrO+o6AUP?= =?us-ascii?q?Q740T3nX6t3gabyeheKAhgl6PQKLY636U90JEUtBKHVg37lku+qKaHakIisryq?= =?us-ascii?q?7+32frLgjoebMIR0iwzkP6g13MqyHbJ8ekIsGSCx4/m32fXN/ED9TK4Axqk8nK?= =?us-ascii?q?jUtJzBDcoaq6q1GAJOlIE55EDsIS2h1YEjkGUHZHxCYgmHgoWhb0rDPPC+A/Cl?= =?us-ascii?q?mFShnR9gzvbBOLzqC5GLJX/GxuSyNY1h4lJRnVJghetU4IhZX/RYeKr+?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0FNBQDdwB9X/3/oP4heHYMbgVCCc7R9i?= =?us-ascii?q?2JMAQEBAQEBAgJiJ4ItghUBBSdSEAgQOSwrBgESiCrEZ4YhiG+FcQWNVHSJSIF?= =?us-ascii?q?VjESJT4VARY5rYoE2AU4bgWkeMIdxgT0BAQE?= X-IPAS-Result: =?us-ascii?q?A0FNBQDdwB9X/3/oP4heHYMbgVCCc7R9i2JMAQEBAQEBAgJ?= =?us-ascii?q?iJ4ItghUBBSdSEAgQOSwrBgESiCrEZ4YhiG+FcQWNVHSJSIFVjESJT4VARY5rY?= =?us-ascii?q?oE2AU4bgWkeMIdxgT0BAQE?= X-IronPort-AV: E=Sophos;i="5.24,537,1454976000"; d="scan'208";a="15642329" Received: from unknown (HELO ubuntu-hedt) ([136.63.232.127]) by emsm-gh1-uea11.nsa.gov with ESMTP; 26 Apr 2016 19:30:49 +0000 Received: by ubuntu-hedt (Postfix, from userid 1000) id 2E38D302390; Tue, 26 Apr 2016 14:30:49 -0500 (CDT) From: Seth Forshee To: "Eric W. Biederman" , Kent Overstreet , Alasdair Kergon , Mike Snitzer , dm-devel@redhat.com, Shaohua Li , David Woodhouse , Brian Norris , Alexander Viro , Jan Kara , Jeff Layton , "J. Bruce Fields" Subject: [PATCH v4 04/21] block_dev: Support checking inode permissions in lookup_bdev() Date: Tue, 26 Apr 2016 14:30:27 -0500 Message-Id: <1461699046-30485-5-git-send-email-seth.forshee@canonical.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1461699046-30485-1-git-send-email-seth.forshee@canonical.com> References: <1461699046-30485-1-git-send-email-seth.forshee@canonical.com> X-Mailman-Approved-At: Tue, 26 Apr 2016 15:34:29 -0400 X-BeenThere: selinux@tycho.nsa.gov X-Mailman-Version: 2.1.20 Precedence: list List-Id: "Security-Enhanced Linux \(SELinux\) mailing list" List-Post: List-Help: Cc: linux-bcache@vger.kernel.org, Serge Hallyn , Seth Forshee , Miklos Szeredi , Richard Weinberger , linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, linux-raid@vger.kernel.org, fuse-devel@lists.sourceforge.net, Austin S Hemmelgarn , linux-mtd@lists.infradead.org, selinux@tycho.nsa.gov, linux-fsdevel@vger.kernel.org, cgroups@vger.kernel.org, Pavel Tikhomirov MIME-Version: 1.0 Errors-To: selinux-bounces@tycho.nsa.gov Sender: "Selinux" X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When looking up a block device by path no permission check is done to verify that the user has access to the block device inode at the specified path. In some cases it may be necessary to check permissions towards the inode, such as allowing unprivileged users to mount block devices in user namespaces. Add an argument to lookup_bdev() to optionally perform this permission check. A value of 0 skips the permission check and behaves the same as before. A non-zero value specifies the mask of access rights required towards the inode at the specified path. The check is always skipped if the user has CAP_SYS_ADMIN. All callers of lookup_bdev() currently pass a mask of 0, so this patch results in no functional change. Subsequent patches will add permission checks where appropriate. Signed-off-by: Seth Forshee Acked-by: Serge Hallyn --- drivers/md/bcache/super.c | 2 +- drivers/md/dm-table.c | 2 +- drivers/mtd/mtdsuper.c | 2 +- fs/block_dev.c | 13 ++++++++++--- fs/quota/quota.c | 2 +- include/linux/fs.h | 2 +- 6 files changed, 15 insertions(+), 8 deletions(-) diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c index a296425a7270..e169739a0253 100644 --- a/drivers/md/bcache/super.c +++ b/drivers/md/bcache/super.c @@ -1950,7 +1950,7 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr, sb); if (IS_ERR(bdev)) { if (bdev == ERR_PTR(-EBUSY)) { - bdev = lookup_bdev(strim(path)); + bdev = lookup_bdev(strim(path), 0); mutex_lock(&bch_register_lock); if (!IS_ERR(bdev) && bch_is_open(bdev)) err = "device already registered"; diff --git a/drivers/md/dm-table.c b/drivers/md/dm-table.c index f9e8f0bef332..13f568d527b5 100644 --- a/drivers/md/dm-table.c +++ b/drivers/md/dm-table.c @@ -372,7 +372,7 @@ dev_t dm_get_dev_t(const char *path) dev_t uninitialized_var(dev); struct block_device *bdev; - bdev = lookup_bdev(path); + bdev = lookup_bdev(path, 0); if (IS_ERR(bdev)) dev = name_to_dev_t(path); else { diff --git a/drivers/mtd/mtdsuper.c b/drivers/mtd/mtdsuper.c index 20c02a3b7417..b5b60e1af31c 100644 --- a/drivers/mtd/mtdsuper.c +++ b/drivers/mtd/mtdsuper.c @@ -176,7 +176,7 @@ struct dentry *mount_mtd(struct file_system_type *fs_type, int flags, /* try the old way - the hack where we allowed users to mount * /dev/mtdblock$(n) but didn't actually _use_ the blockdev */ - bdev = lookup_bdev(dev_name); + bdev = lookup_bdev(dev_name, 0); if (IS_ERR(bdev)) { ret = PTR_ERR(bdev); pr_debug("MTDSB: lookup_bdev() returned %d\n", ret); diff --git a/fs/block_dev.c b/fs/block_dev.c index 3e84d62d0a25..e9b937845bdb 100644 --- a/fs/block_dev.c +++ b/fs/block_dev.c @@ -1431,7 +1431,7 @@ struct block_device *blkdev_get_by_path(const char *path, fmode_t mode, struct block_device *bdev; int err; - bdev = lookup_bdev(path); + bdev = lookup_bdev(path, 0); if (IS_ERR(bdev)) return bdev; @@ -1821,12 +1821,14 @@ EXPORT_SYMBOL(ioctl_by_bdev); /** * lookup_bdev - lookup a struct block_device by name * @pathname: special file representing the block device + * @mask: rights to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) * * Get a reference to the blockdevice at @pathname in the current * namespace if possible and return it. Return ERR_PTR(error) - * otherwise. + * otherwise. If @mask is non-zero, check for access rights to the + * inode at @pathname. */ -struct block_device *lookup_bdev(const char *pathname) +struct block_device *lookup_bdev(const char *pathname, int mask) { struct block_device *bdev; struct inode *inode; @@ -1841,6 +1843,11 @@ struct block_device *lookup_bdev(const char *pathname) return ERR_PTR(error); inode = d_backing_inode(path.dentry); + if (mask != 0 && !capable(CAP_SYS_ADMIN)) { + error = __inode_permission(inode, mask); + if (error) + goto fail; + } error = -ENOTBLK; if (!S_ISBLK(inode->i_mode)) goto fail; diff --git a/fs/quota/quota.c b/fs/quota/quota.c index 0f10ee9892ce..59223384b22e 100644 --- a/fs/quota/quota.c +++ b/fs/quota/quota.c @@ -799,7 +799,7 @@ static struct super_block *quotactl_block(const char __user *special, int cmd) if (IS_ERR(tmp)) return ERR_CAST(tmp); - bdev = lookup_bdev(tmp->name); + bdev = lookup_bdev(tmp->name, 0); putname(tmp); if (IS_ERR(bdev)) return ERR_CAST(bdev); diff --git a/include/linux/fs.h b/include/linux/fs.h index 66a639ec1bc4..173b8adc6131 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2438,7 +2438,7 @@ static inline void unregister_chrdev(unsigned int major, const char *name) #define BLKDEV_MAJOR_HASH_SIZE 255 extern const char *__bdevname(dev_t, char *buffer); extern const char *bdevname(struct block_device *bdev, char *buffer); -extern struct block_device *lookup_bdev(const char *); +extern struct block_device *lookup_bdev(const char *, int mask); extern void blkdev_show(struct seq_file *,off_t); #else