sys/dev/disk/dm: Remove read/write support for /dev/mapper/control
authorTomohiro Kusumi <kusumi.tomohiro@gmail.com>
Sun, 1 May 2016 00:12:10 +0000 (09:12 +0900)
committerTomohiro Kusumi <kusumi.tomohiro@gmail.com>
Sun, 1 May 2016 03:12:34 +0000 (12:12 +0900)
It's obvious that /dev/mapper/control has nothing to read/write.
Running below results in kernel panic as the code requires properly
initialized a per-table lock while this chrdev has no tables mapped.
(control chrdev and dm blkdevs use the same dm device structures)

-----
 [root@]~# cat ./dm1.c
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <fcntl.h>
 int main(void)
 {
         int fd;
         char buf[1024];
         fd = open("/dev/mapper/control", O_RDWR);
         if (fd == -1) {
                 perror("open");
                 exit(1);
         }
         if (read(fd, buf, sizeof(buf)) == -1)
                 perror("read");
         if (write(fd, buf, sizeof(buf)) == -1)
                 perror("write");
         close(fd);
         return 0;
 }

With this commit.
-----
 [root@]~# uname
 DragonFly
 [root@]~# kldload dm
 [root@]~# ls -l /dev/mapper/control
 crw-r-----  1 root  operator   65, 0x00000000 May  1 09:25 /dev/mapper/control
 [root@]~# gcc -Wall -g ./dm1.c -o ./dm1
 [root@]~# ./dm1
 read: Operation not supported by device
 write: Operation not supported by device

With Linux kernel.
-----
 [root@localhost]~# uname
 Linux
 [root@localhost]~# ls -l /dev/mapper/control
 crw-rw---- 1 root root 10, 236 Apr 25 12:44 /dev/mapper/control
 [root@localhost]~# gcc -Wall -g ./dm1.c -o ./dm1
 [root@localhost]~# ./dm1
 read: Invalid argument
 write: Invalid argument

sys/dev/disk/dm/device-mapper.c

index bbf5a6b..1d2d7c1 100644 (file)
@@ -66,6 +66,13 @@ static int dm_ioctl_switch(u_long);
 static void dmminphys(struct buf *);
 #endif
 
+static struct dev_ops dmctl_ops = {
+       { "dm", 0, D_MPSAFE },
+       .d_open         = dmopen,
+       .d_close        = dmclose,
+       .d_ioctl        = dmioctl,
+};
+
 struct dev_ops dm_ops = {
        { "dm", 0, D_DISK | D_MPSAFE },
        .d_open         = dmopen,
@@ -178,7 +185,7 @@ dm_doinit(void)
        dm_target_init();
        dm_dev_init();
        dm_pdev_init();
-       dmcdev = make_dev(&dm_ops, 0, UID_ROOT, GID_OPERATOR, 0640, "mapper/control");
+       dmcdev = make_dev(&dmctl_ops, 0, UID_ROOT, GID_OPERATOR, 0640, "mapper/control");
 }
 
 /*