[PATCH] Chardev checking of overlapping ranges
authorAmos Waterland <apw@us.ibm.com>
Fri, 29 Sep 2006 09:00:08 +0000 (02:00 -0700)
committerLinus Torvalds <torvalds@g5.osdl.org>
Fri, 29 Sep 2006 16:18:12 +0000 (09:18 -0700)
The code in __register_chrdev_region checks that if the driver wishing to
register has the same major as an existing driver the new minor range is
strictly less than the existing minor range.  However, it does not also
check that the new minor range is strictly greater than the existing minor
range.  That is, if driver X has registered with major=x and minor=0-3,
__register_chrdev_region will allow driver Y to register with major=x and
minor=1-4.

Signed-off-by: Amos Waterland <apw@us.ibm.com>
Cc: Linas Vepstas <linas@austin.ibm.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
fs/char_dev.c

index 0009346..33b95af 100644 (file)
@@ -128,13 +128,31 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
 
        for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
                if ((*cp)->major > major ||
-                   ((*cp)->major == major && (*cp)->baseminor >= baseminor))
+                   ((*cp)->major == major &&
+                    (((*cp)->baseminor >= baseminor) ||
+                     ((*cp)->baseminor + (*cp)->minorct > baseminor))))
                        break;
-       if (*cp && (*cp)->major == major &&
-           (*cp)->baseminor < baseminor + minorct) {
-               ret = -EBUSY;
-               goto out;
+
+       /* Check for overlapping minor ranges.  */
+       if (*cp && (*cp)->major == major) {
+               int old_min = (*cp)->baseminor;
+               int old_max = (*cp)->baseminor + (*cp)->minorct - 1;
+               int new_min = baseminor;
+               int new_max = baseminor + minorct - 1;
+
+               /* New driver overlaps from the left.  */
+               if (new_max >= old_min && new_max <= old_max) {
+                       ret = -EBUSY;
+                       goto out;
+               }
+
+               /* New driver overlaps from the right.  */
+               if (new_min <= old_max && new_min >= old_min) {
+                       ret = -EBUSY;
+                       goto out;
+               }
        }
+
        cd->next = *cp;
        *cp = cd;
        mutex_unlock(&chrdevs_lock);