move devname validation to util.c
authorKay Sievers <kay@vrfy.org>
Sat, 4 Jan 2014 05:21:57 +0000 (09:21 +0400)
committerKay Sievers <kay@vrfy.org>
Sat, 4 Jan 2014 05:26:43 +0000 (09:26 +0400)
Makefile
bus.c
endpoint.c
handle.c
namespace.c
util.c [new file with mode: 0644]
util.h

index 3d53b73f383279d52f0d7561b70eff73859bd275..e1a655ea600321dbac594eea312ba52be07e320d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -12,7 +12,8 @@ kdbus$(EXT)-y := \
        notify.o \
        namespace.o \
        policy.o \
-       pool.o
+       pool.o \
+       util.o
 
 obj-m += kdbus$(EXT).o
 
diff --git a/bus.c b/bus.c
index 492976f0a8f084224de471bf92f7a5e93fd7ec55..872ddbd7f8358ab994d183a9768db300628aa598 100644 (file)
--- a/bus.c
+++ b/bus.c
@@ -314,6 +314,10 @@ int kdbus_bus_make_user(void __user *buf, struct kdbus_cmd_make **make,
                                goto exit;
                        }
 
+                       ret = kdbus_devname_valid(item->str);
+                       if (ret < 0)
+                               goto exit;
+
                        n = item->str;
                        break;
 
index ea7d95d3868cb6d05afef2c384231da95afbcf44..7b2a413aa434c9cb6c6c54384d79426b032b7b25 100644 (file)
@@ -288,6 +288,10 @@ int kdbus_ep_make_user(void __user *buf,
                                goto exit;
                        }
 
+                       ret = kdbus_devname_valid(item->str);
+                       if (ret < 0)
+                               goto exit;
+
                        n = item->str;
                        continue;
 
index b2da93f4e74d7b71aff838a479a56d1282317187..d0039125820f20c9fa4030ad85c06b5bbd836995 100644 (file)
--- a/handle.c
+++ b/handle.c
@@ -22,7 +22,6 @@
 #include <linux/sizes.h>
 #include <linux/slab.h>
 #include <linux/uaccess.h>
-#include <linux/ctype.h>
 
 #include "bus.h"
 #include "connection.h"
@@ -203,29 +202,6 @@ static bool kdbus_check_flags(u64 kernel_flags)
        return kernel_flags <= 0xFFFFFFFFULL;
 }
 
-static int kdbus_handle_name_valid(const char *name)
-{
-       unsigned int i;
-       size_t len;
-
-       len = strlen(name);
-       if (len == 0)
-               return -EINVAL;
-
-       for (i = 0; i < len; i++) {
-               if (isalpha(name[i]))
-                       continue;
-               if (isdigit(name[i]))
-                       continue;
-               if (i > 0 && i + 1 < len && strchr("-.", name[i]))
-                       continue;
-
-               return -EINVAL;
-       }
-
-       return 0;
-}
-
 /* kdbus control device commands */
 static long kdbus_handle_ioctl_control(struct file *file, unsigned int cmd,
                                       void __user *buf)
@@ -252,10 +228,6 @@ static long kdbus_handle_ioctl_control(struct file *file, unsigned int cmd,
                if (ret < 0)
                        break;
 
-               ret = kdbus_handle_name_valid(name);
-               if (ret < 0)
-                       break;
-
                if (!kdbus_check_flags(make->flags)) {
                        ret = -ENOTSUPP;
                        break;
@@ -296,10 +268,6 @@ static long kdbus_handle_ioctl_control(struct file *file, unsigned int cmd,
                if (ret < 0)
                        break;
 
-               ret = kdbus_handle_name_valid(name);
-               if (ret < 0)
-                       break;
-
                if (!kdbus_check_flags(make->flags)) {
                        ret = -ENOTSUPP;
                        break;
@@ -364,10 +332,6 @@ static long kdbus_handle_ioctl_ep(struct file *file, unsigned int cmd,
                if (ret < 0)
                        break;
 
-               ret = kdbus_handle_name_valid(name);
-               if (ret < 0)
-                       break;
-
                if (!kdbus_check_flags(make->flags)) {
                        ret = -ENOTSUPP;
                        break;
index 836e96f76ae0749889f90b6c96e8e4bb5ca0c1b2..ac3a947b0ab29babaa7eace361c7a539d93c48ff 100644 (file)
@@ -366,6 +366,10 @@ int kdbus_ns_make_user(void __user *buf,
                                goto exit;
                        }
 
+                       ret = kdbus_devname_valid(item->str);
+                       if (ret < 0)
+                               goto exit;
+
                        n = item->str;
                        continue;
 
diff --git a/util.c b/util.c
new file mode 100644 (file)
index 0000000..68aa630
--- /dev/null
+++ b/util.c
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2013 Kay Sievers
+ * Copyright (C) 2013 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+ * Copyright (C) 2013 Daniel Mack <daniel@zonque.org>
+ * Copyright (C) 2013 Linux Foundation
+ *
+ * kdbus is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the
+ * Free Software Foundation; either version 2.1 of the License, or (at
+ * your option) any later version.
+ */
+
+#include <linux/sizes.h>
+#include <linux/slab.h>
+#include <linux/uaccess.h>
+#include <linux/ctype.h>
+#include <linux/fs.h>
+
+#include "util.h"
+
+/**
+ * kdbus_devname_valid - validate names showing up in /dev
+ * @name:              Name of namepspace, bus, endpoint
+ *
+ * Returns: 0 if the given name is valid, otherwise negative errno
+ */
+int kdbus_devname_valid(const char *name)
+{
+       unsigned int i;
+       size_t len;
+
+       len = strlen(name);
+       if (len == 0)
+               return -EINVAL;
+
+       for (i = 0; i < len; i++) {
+               if (isalpha(name[i]))
+                       continue;
+               if (isdigit(name[i]))
+                       continue;
+               if (i > 0 && i + 1 < len && strchr("-.", name[i]))
+                       continue;
+
+               return -EINVAL;
+       }
+
+       return 0;
+}
diff --git a/util.h b/util.h
index bee27ab1eba81dcf7fb241bf0afb5d3e87e2fb2f..ade7841e9baf899a61bd467c0aaf96ac9669a8ae 100644 (file)
--- a/util.h
+++ b/util.h
@@ -104,4 +104,6 @@ static inline unsigned int kdbus_str_hash(const char *str)
 {
        return full_name_hash(str, strlen(str));
 }
+
+int kdbus_devname_valid(const char *name);
 #endif