]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/commitdiff
UBUNTU: SAUCE: (namespace) block_dev: Support checking inode permissions in lookup_bdev()
authorSeth Forshee <seth.forshee@canonical.com>
Fri, 31 Jul 2015 17:58:34 +0000 (12:58 -0500)
committerTim Gardner <tim.gardner@canonical.com>
Mon, 20 Feb 2017 03:57:58 +0000 (20:57 -0700)
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.

Acked-by: Serge Hallyn <serge.hallyn@canonical.com>
Signed-off-by: Seth Forshee <seth.forshee@canonical.com>
drivers/md/bcache/super.c
drivers/md/dm-table.c
drivers/mtd/mtdsuper.c
fs/block_dev.c
fs/quota/quota.c
include/linux/fs.h

index 3a19cbc8b230e5a7b77cafa89427b8e282c251dc..fd66c7d964089acc3620f73ac950fd11088c8557 100644 (file)
@@ -1957,7 +1957,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";
index 0a427de23ed2c6a01fa183738705b8c8f6e3e099..53014d20aaca4f8754e63826ac328039d13c853f 100644 (file)
@@ -375,7 +375,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 {
index 20c02a3b7417cd3025a6c17142f86a3479e2e3a8..b5b60e1af31ce96ba94bd5247688444ca0b1d214 100644 (file)
@@ -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);
index 3c47614a4b32c75c4d5cf75d436389305c145288..6c00227303d90655343da77cda2e74a459387a58 100644 (file)
@@ -1744,7 +1744,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;
 
@@ -2140,12 +2140,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;
@@ -2160,6 +2162,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;
index 07e08c7d05cae23d92ab891cb384e5dda90509df..3a725f2b69700f6747afea6603446e4924f89c25 100644 (file)
@@ -806,7 +806,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);
index 111cb2c9458b939e08b5858e2e7978c99932383f..81b0eb8b7231726cb4ef0d520d6b0af98d7a893f 100644 (file)
@@ -2454,7 +2454,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