From a9a40e7f82a541556ecd23b0ba016dd620b8e368 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Sat, 4 Jan 2014 09:21:57 +0400 Subject: [PATCH] move devname validation to util.c --- Makefile | 3 ++- bus.c | 4 ++++ endpoint.c | 4 ++++ handle.c | 36 ------------------------------------ namespace.c | 4 ++++ util.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ util.h | 2 ++ 7 files changed, 64 insertions(+), 37 deletions(-) create mode 100644 util.c diff --git a/Makefile b/Makefile index 3d53b73..e1a655e 100644 --- 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 492976f..872ddbd 100644 --- 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; diff --git a/endpoint.c b/endpoint.c index ea7d95d..7b2a413 100644 --- a/endpoint.c +++ b/endpoint.c @@ -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; diff --git a/handle.c b/handle.c index b2da93f..d003912 100644 --- a/handle.c +++ b/handle.c @@ -22,7 +22,6 @@ #include #include #include -#include #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; diff --git a/namespace.c b/namespace.c index 836e96f..ac3a947 100644 --- a/namespace.c +++ b/namespace.c @@ -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 index 0000000..68aa630 --- /dev/null +++ b/util.c @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2013 Kay Sievers + * Copyright (C) 2013 Greg Kroah-Hartman + * Copyright (C) 2013 Daniel Mack + * 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 +#include +#include +#include +#include + +#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 bee27ab..ade7841 100644 --- 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 -- 2.34.1