From 63f61c5cf639953aa38e025485919b0aa1c49b59 Mon Sep 17 00:00:00 2001 From: "kay.sievers@vrfy.org" Date: Mon, 7 Mar 2005 04:29:43 +0100 Subject: [PATCH] [PATCH] replace strncpy()/strncat() by strlcpy()/strlcat() --- namedev.c | 92 +++++++++++++++++----------------- namedev.h | 30 +++++------- namedev_parse.c | 33 +++++++------ udev.c | 9 ++-- udev.h | 34 ++++++------- udev_add.c | 34 ++++++------- udev_config.c | 33 ++++++------- udev_db.c | 117 ++++++++++++++++++++------------------------ udev_libc_wrapper.c | 52 +++++++++++++++++--- udev_libc_wrapper.h | 20 ++++++++ udev_multiplex.c | 23 ++++----- udev_remove.c | 22 +++++---- udev_sysfs.c | 10 ++-- udev_utils.c | 27 +++++----- udev_utils.h | 36 +------------- udevd.c | 3 +- udevinfo.c | 29 ++++++----- udevstart.c | 53 ++++++++++---------- udevtest.c | 19 ++++--- 19 files changed, 346 insertions(+), 330 deletions(-) diff --git a/namedev.c b/namedev.c index 49f9e4b1..9c73c081 100644 --- a/namedev.c +++ b/namedev.c @@ -35,6 +35,7 @@ #include "libsysfs/sysfs/libsysfs.h" #include "list.h" +#include "udev_libc_wrapper.h" #include "udev.h" #include "udev_utils.h" #include "udev_version.h" @@ -144,14 +145,14 @@ static int get_format_len(char **str) */ static int find_free_number(struct udevice *udev, const char *name) { - char devpath[NAME_SIZE]; - char filename[NAME_SIZE]; + char devpath[PATH_SIZE]; + char filename[PATH_SIZE]; int num = 0; - strfieldcpy(filename, name); + strlcpy(filename, name, sizeof(filename)); while (1) { dbg("look for existing node '%s'", filename); - if (udev_db_search_name(devpath, DEVPATH_SIZE, filename) != 0) { + if (udev_db_search_name(devpath, sizeof(devpath), filename) != 0) { dbg("free num=%d", num); return num; } @@ -161,16 +162,16 @@ static int find_free_number(struct udevice *udev, const char *name) info("find_free_number gone crazy (num=%d), aborted", num); return -1; } - snprintf(filename, NAME_SIZE, "%s%d", name, num); - filename[NAME_SIZE-1] = '\0'; + snprintf(filename, sizeof(filename), "%s%d", name, num); + filename[sizeof(filename)-1] = '\0'; } } static void apply_format(struct udevice *udev, char *string, size_t maxsize, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device) { - char temp[NAME_SIZE]; - char temp2[NAME_SIZE]; + char temp[PATH_SIZE]; + char temp2[PATH_SIZE]; char *tail, *pos, *cpos, *attr, *rest; int len; int i; @@ -189,36 +190,36 @@ static void apply_format(struct udevice *udev, char *string, size_t maxsize, tail = pos+1; len = get_format_len(&tail); c = tail[0]; - strfieldcpy(temp, tail+1); + strlcpy(temp, tail+1, sizeof(temp)); tail = temp; dbg("format=%c, string='%s', tail='%s'",c , string, tail); attr = get_format_attribute(&tail); switch (c) { case 'p': - strfieldcatmax(string, udev->devpath, maxsize); + strlcat(string, udev->devpath, maxsize); dbg("substitute kernel name '%s'", udev->kernel_name); break; case 'b': - strfieldcatmax(string, udev->bus_id, maxsize); + strlcat(string, udev->bus_id, maxsize); dbg("substitute bus_id '%s'", udev->bus_id); break; case 'k': - strfieldcatmax(string, udev->kernel_name, maxsize); + strlcat(string, udev->kernel_name, maxsize); dbg("substitute kernel name '%s'", udev->kernel_name); break; case 'n': - strfieldcatmax(string, udev->kernel_number, maxsize); + strlcat(string, udev->kernel_number, maxsize); dbg("substitute kernel number '%s'", udev->kernel_number); break; case 'm': sprintf(temp2, "%d", minor(udev->devt)); - strfieldcatmax(string, temp2, maxsize); + strlcat(string, temp2, maxsize); dbg("substitute minor number '%s'", temp2); break; case 'M': sprintf(temp2, "%d", major(udev->devt)); - strfieldcatmax(string, temp2, maxsize); + strlcat(string, temp2, maxsize); dbg("substitute major number '%s'", temp2); break; case 'c': @@ -241,17 +242,17 @@ static void apply_format(struct udevice *udev, char *string, size_t maxsize, dbg("requested part of result string not found"); break; } - strfieldcpy(temp2, cpos); + strlcpy(temp2, cpos, sizeof(temp2)); /* %{2+}c copies the whole string from the second part on */ if (rest[0] != '+') { cpos = strchr(temp2, ' '); if (cpos) cpos[0] = '\0'; } - strfieldcatmax(string, temp2, maxsize); + strlcat(string, temp2, maxsize); dbg("substitute part of result string '%s'", temp2); } else { - strfieldcatmax(string, udev->program_result, maxsize); + strlcat(string, udev->program_result, maxsize); dbg("substitute result string '%s'", udev->program_result); } break; @@ -278,18 +279,18 @@ static void apply_format(struct udevice *udev, char *string, size_t maxsize, len - i, tmpattr->value); } } - strfieldcatmax(string, tmpattr->value, maxsize); + strlcat(string, tmpattr->value, maxsize); dbg("substitute sysfs value '%s'", tmpattr->value); break; case '%': - strfieldcatmax(string, "%", maxsize); + strlcat(string, "%", maxsize); pos++; break; case 'e': next_free_number = find_free_number(udev, string); if (next_free_number > 0) { sprintf(temp2, "%d", next_free_number); - strfieldcatmax(string, temp2, maxsize); + strlcat(string, temp2, maxsize); } break; case 'P': @@ -303,7 +304,7 @@ static void apply_format(struct udevice *udev, char *string, size_t maxsize, udev_init_device(&udev_parent, NULL, NULL); /* lookup the name in the udev_db with the DEVPATH of the parent */ if (udev_db_get_device(&udev_parent, &class_dev_parent->path[strlen(sysfs_path)]) == 0) { - strfieldcatmax(string, udev_parent.name, maxsize); + strlcat(string, udev_parent.name, maxsize); dbg("substitute parent node name'%s'", udev_parent.name); } else dbg("parent not found in database"); @@ -313,15 +314,16 @@ static void apply_format(struct udevice *udev, char *string, size_t maxsize, case 'N': if (udev->tmp_node[0] == '\0') { dbg("create temporary device node for callout"); - snprintf(udev->tmp_node, NAME_SIZE, "%s/.tmp-%u-%u", udev_root, major(udev->devt), minor(udev->devt)); - udev->tmp_node[NAME_SIZE] = '\0'; + snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u", + udev_root, major(udev->devt), minor(udev->devt)); + udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0'; udev_make_node(udev, udev->tmp_node, udev->devt, 0600, 0, 0); } - strfieldcatmax(string, udev->tmp_node, maxsize); + strlcat(string, udev->tmp_node, maxsize); dbg("substitute temporary device node name '%s'", udev->tmp_node); break; case 'r': - strfieldcatmax(string, udev_root, maxsize); + strlcat(string, udev_root, maxsize); dbg("substitute udev_root '%s'", udev_root); break; default: @@ -332,7 +334,7 @@ static void apply_format(struct udevice *udev, char *string, size_t maxsize, if (len > 0) pos[len] = '\0'; - strfieldcatmax(string, tail, maxsize); + strlcat(string, tail, maxsize); } } @@ -344,11 +346,11 @@ static int execute_program(struct udevice *udev, const char *path, char *value, int fds[2]; pid_t pid; char *pos; - char arg[PROGRAM_SIZE]; - char *argv[(PROGRAM_SIZE / 2) + 1]; + char arg[PATH_SIZE]; + char *argv[(sizeof(arg) / 2) + 1]; int i; - strfieldcpy(arg, path); + strlcpy(arg, path, sizeof(arg)); i = 0; if (strchr(path, ' ')) { pos = arg; @@ -516,14 +518,14 @@ static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_devic static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device) { - char path[SYSFS_PATH_MAX]; + char path[PATH_SIZE]; char *temp; /* we have to have a sysfs device for ID to work */ if (!sysfs_device) return -ENODEV; - strfieldcpy(path, sysfs_device->path); + strlcpy(path, sysfs_device->path, sizeof(path)); temp = strrchr(path, '/'); temp++; dbg("search '%s' in '%s', path='%s'", dev->id, temp, path); @@ -535,14 +537,14 @@ static int match_id(struct config_device *dev, struct sysfs_class_device *class_ static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device) { - char path[SYSFS_PATH_MAX]; + char path[PATH_SIZE]; char *temp; /* we have to have a sysfs device for PLACE to work */ if (!sysfs_device) return -ENODEV; - strfieldcpy(path, sysfs_device->path); + strlcpy(path, sysfs_device->path, sizeof(path)); temp = strrchr(path, '/'); dbg("search '%s' in '%s', path='%s'", dev->place, temp, path); if (strstr(temp, dev->place) != NULL) @@ -653,12 +655,12 @@ try_parent: /* execute external program */ if (dev->program[0] != '\0') { - char program[PROGRAM_SIZE]; + char program[PATH_SIZE]; dbg("check " FIELD_PROGRAM); - strfieldcpy(program, dev->program); + strlcpy(program, dev->program, sizeof(program)); apply_format(udev, program, sizeof(program), class_dev, sysfs_device); - if (execute_program(udev, program, udev->program_result, NAME_SIZE) != 0) { + if (execute_program(udev, program, udev->program_result, sizeof(udev->program_result)) != 0) { dbg(FIELD_PROGRAM " returned nonzero"); goto try_parent; } @@ -707,7 +709,7 @@ int namedev_name_device(struct udevice *udev, struct sysfs_class_device *class_d if (sysfs_device) { dbg("found devices device: path='%s', bus_id='%s', bus='%s'", sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus); - strfieldcpy(udev->bus_id, sysfs_device->bus_id); + strlcpy(udev->bus_id, sysfs_device->bus_id, sizeof(udev->bus_id)); } dbg("udev->kernel_name='%s'", udev->kernel_name); @@ -739,24 +741,24 @@ int namedev_name_device(struct udevice *udev, struct sysfs_class_device *class_d dbg("applied mode=%#o to '%s'", udev->mode, udev->kernel_name); } if (dev->owner[0] != '\0') { - strfieldcpy(udev->owner, dev->owner); + strlcpy(udev->owner, dev->owner, sizeof(udev->owner)); apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device); dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name); } if (dev->group[0] != '\0') { - strfieldcpy(udev->group, dev->group); + strlcpy(udev->group, dev->group, sizeof(udev->group)); apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device); dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name); } /* collect symlinks */ if (dev->symlink[0] != '\0') { - char temp[NAME_SIZE]; + char temp[PATH_SIZE]; char *pos, *next; info("configured rule in '%s[%i]' applied, added symlink '%s'", dev->config_file, dev->config_line, dev->symlink); - strfieldcpy(temp, dev->symlink); + strlcpy(temp, dev->symlink, sizeof(temp)); apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device); /* add multiple symlinks separated by spaces */ @@ -778,9 +780,9 @@ int namedev_name_device(struct udevice *udev, struct sysfs_class_device *class_d info("configured rule in '%s[%i]' applied, '%s' becomes '%s'", dev->config_file, dev->config_line, udev->kernel_name, dev->name); - strfieldcpy(udev->name, dev->name); + strlcpy(udev->name, dev->name, sizeof(udev->name)); apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device); - strfieldcpy(udev->config_file, dev->config_file); + strlcpy(udev->config_file, dev->config_file, sizeof(udev->config_file)); udev->config_line = dev->config_line; if (udev->type != NET) @@ -794,7 +796,7 @@ int namedev_name_device(struct udevice *udev, struct sysfs_class_device *class_d if (udev->name[0] == '\0') { /* no rule matched, so we use the kernel name */ - strfieldcpy(udev->name, udev->kernel_name); + strlcpy(udev->name, udev->kernel_name, sizeof(udev->name)); dbg("no rule found, use kernel name '%s'", udev->name); } diff --git a/namedev.h b/namedev.h index 1d6920cc..76caaaa9 100644 --- a/namedev.h +++ b/namedev.h @@ -28,14 +28,6 @@ struct sysfs_class_device; -#define BUS_SIZE 32 -#define FILE_SIZE 64 -#define VALUE_SIZE 128 -#define ID_SIZE 64 -#define PLACE_SIZE 64 -#define DRIVER_SIZE 64 -#define PROGRAM_SIZE 128 - #define FIELD_KERNEL "KERNEL" #define FIELD_SUBSYSTEM "SUBSYSTEM" #define FIELD_BUS "BUS" @@ -61,7 +53,7 @@ struct sysfs_class_device; #define RULEFILE_SUFFIX ".rules" struct sysfs_pair { - char file[FILE_SIZE]; + char file[PATH_SIZE]; char value[VALUE_SIZE]; }; @@ -69,16 +61,16 @@ struct config_device { struct list_head node; char kernel[NAME_SIZE]; - char subsystem[SUBSYSTEM_SIZE]; - char bus[BUS_SIZE]; - char id[ID_SIZE]; - char place[PLACE_SIZE]; + char subsystem[NAME_SIZE]; + char bus[NAME_SIZE]; + char id[NAME_SIZE]; + char place[NAME_SIZE]; struct sysfs_pair sysfs_pair[MAX_SYSFS_PAIRS]; - char program[PROGRAM_SIZE]; - char result[PROGRAM_SIZE]; - char driver[DRIVER_SIZE]; - char name[NAME_SIZE]; - char symlink[NAME_SIZE]; + char program[PATH_SIZE]; + char result[PATH_SIZE]; + char driver[NAME_SIZE]; + char name[PATH_SIZE]; + char symlink[PATH_SIZE]; char owner[USER_SIZE]; char group[USER_SIZE]; @@ -88,7 +80,7 @@ struct config_device { int ignore_device; int ignore_remove; - char config_file[NAME_SIZE]; + char config_file[PATH_SIZE]; int config_line; }; diff --git a/namedev_parse.c b/namedev_parse.c index e3605659..94259612 100644 --- a/namedev_parse.c +++ b/namedev_parse.c @@ -36,6 +36,7 @@ #include #include +#include "udev_libc_wrapper.h" #include "udev.h" #include "udev_utils.h" #include "logging.h" @@ -134,7 +135,7 @@ static int namedev_parse(struct udevice *udev, const char *filename) cur += count+1; lineno++; - if (count >= LINE_SIZE) { + if (count >= sizeof(line)) { info("line too long, rule skipped %s, line %d", filename, lineno); continue; } @@ -172,31 +173,31 @@ static int namedev_parse(struct udevice *udev, const char *filename) break; if (strcasecmp(temp2, FIELD_KERNEL) == 0) { - strfieldcpy(dev.kernel, temp3); + strlcpy(dev.kernel, temp3, sizeof(dev.kernel)); valid = 1; continue; } if (strcasecmp(temp2, FIELD_SUBSYSTEM) == 0) { - strfieldcpy(dev.subsystem, temp3); + strlcpy(dev.subsystem, temp3, sizeof(dev.subsystem)); valid = 1; continue; } if (strcasecmp(temp2, FIELD_BUS) == 0) { - strfieldcpy(dev.bus, temp3); + strlcpy(dev.bus, temp3, sizeof(dev.bus)); valid = 1; continue; } if (strcasecmp(temp2, FIELD_ID) == 0) { - strfieldcpy(dev.id, temp3); + strlcpy(dev.id, temp3, sizeof(dev.id)); valid = 1; continue; } if (strcasecmp(temp2, FIELD_PLACE) == 0) { - strfieldcpy(dev.place, temp3); + strlcpy(dev.place, temp3, sizeof(dev.place)); valid = 1; continue; } @@ -220,28 +221,28 @@ static int namedev_parse(struct udevice *udev, const char *filename) dbg("error parsing " FIELD_SYSFS " attribute"); continue; } - strfieldcpy(pair->file, attr); - strfieldcpy(pair->value, temp3); + strlcpy(pair->file, attr, sizeof(pair->file)); + strlcpy(pair->value, temp3, sizeof(pair->value)); valid = 1; } continue; } if (strcasecmp(temp2, FIELD_DRIVER) == 0) { - strfieldcpy(dev.driver, temp3); + strlcpy(dev.driver, temp3, sizeof(dev.driver)); valid = 1; continue; } if (strcasecmp(temp2, FIELD_PROGRAM) == 0) { program_given = 1; - strfieldcpy(dev.program, temp3); + strlcpy(dev.program, temp3, sizeof(dev.program)); valid = 1; continue; } if (strcasecmp(temp2, FIELD_RESULT) == 0) { - strfieldcpy(dev.result, temp3); + strlcpy(dev.result, temp3, sizeof(dev.result)); valid = 1; continue; } @@ -260,7 +261,7 @@ static int namedev_parse(struct udevice *udev, const char *filename) } } if (temp3[0] != '\0') - strfieldcpy(dev.name, temp3); + strlcpy(dev.name, temp3, sizeof(dev.name)); else dev.ignore_device = 1; valid = 1; @@ -268,19 +269,19 @@ static int namedev_parse(struct udevice *udev, const char *filename) } if (strcasecmp(temp2, FIELD_SYMLINK) == 0) { - strfieldcpy(dev.symlink, temp3); + strlcpy(dev.symlink, temp3, sizeof(dev.symlink)); valid = 1; continue; } if (strcasecmp(temp2, FIELD_OWNER) == 0) { - strfieldcpy(dev.owner, temp3); + strlcpy(dev.owner, temp3, sizeof(dev.owner)); valid = 1; continue; } if (strcasecmp(temp2, FIELD_GROUP) == 0) { - strfieldcpy(dev.group, temp3); + strlcpy(dev.group, temp3, sizeof(dev.group)); valid = 1; continue; } @@ -330,7 +331,7 @@ static int namedev_parse(struct udevice *udev, const char *filename) } dev.config_line = lineno; - strfieldcpy(dev.config_file, filename); + strlcpy(dev.config_file, filename, sizeof(dev.config_file)); retval = add_config_dev(&dev); if (retval) { dbg("add_config_dev returned with error %d", retval); diff --git a/udev.c b/udev.c index c9461469..bf2eb373 100644 --- a/udev.c +++ b/udev.c @@ -32,6 +32,7 @@ #include #include "libsysfs/sysfs/libsysfs.h" +#include "udev_libc_wrapper.h" #include "udev.h" #include "udev_utils.h" #include "udev_sysfs.h" @@ -99,7 +100,7 @@ int main(int argc, char *argv[], char *envp[]) struct sysfs_class_device *class_dev; struct sysfs_device *devices_dev; struct udevice udev; - char path[SYSFS_PATH_MAX]; + char path[PATH_SIZE]; const char *error; const char *action; const char *devpath; @@ -164,7 +165,8 @@ int main(int argc, char *argv[], char *envp[]) goto hotplug; } - snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath); + snprintf(path, sizeof(path), "%s%s", sysfs_path, udev.devpath); + path[sizeof(path)-1] = '\0'; class_dev = wait_class_device_open(path); if (class_dev == NULL) { dbg ("open class device failed"); @@ -206,7 +208,8 @@ int main(int argc, char *argv[], char *envp[]) /* wait for sysfs */ dbg("devices add"); - snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, devpath); + snprintf(path, sizeof(path), "%s%s", sysfs_path, devpath); + path[sizeof(path)-1] = '\0'; devices_dev = wait_devices_device_open(path); if (!devices_dev) { dbg("devices device unavailable (probably remove has beaten us)"); diff --git a/udev.h b/udev.h index 811439fa..7eb07bf1 100644 --- a/udev.h +++ b/udev.h @@ -33,13 +33,11 @@ #define COMMENT_CHARACTER '#' #define LINE_SIZE 512 -#define NAME_SIZE 256 +#define NAME_SIZE 128 +#define PATH_SIZE 256 #define USER_SIZE 32 - -#define ACTION_SIZE 32 -#define DEVPATH_SIZE 256 -#define SUBSYSTEM_SIZE 32 #define SEQNUM_SIZE 32 +#define VALUE_SIZE 128 #define DEVD_DIR "/etc/dev.d" #define DEVD_SUFFIX ".dev" @@ -58,11 +56,11 @@ enum device_type { }; struct udevice { - char devpath[DEVPATH_SIZE]; - char subsystem[SUBSYSTEM_SIZE]; + char devpath[PATH_SIZE]; + char subsystem[NAME_SIZE]; - char name[NAME_SIZE]; - char devname[NAME_SIZE]; + char name[PATH_SIZE]; + char devname[PATH_SIZE]; struct list_head symlink_list; char owner[USER_SIZE]; char group[USER_SIZE]; @@ -70,13 +68,13 @@ struct udevice { char type; dev_t devt; - char tmp_node[NAME_SIZE]; + char tmp_node[PATH_SIZE]; int partitions; int ignore_remove; int config_line; - char config_file[NAME_SIZE]; - char bus_id[SYSFS_NAME_LEN]; - char program_result[NAME_SIZE]; + char config_file[PATH_SIZE]; + char bus_id[NAME_SIZE]; + char program_result[PATH_SIZE]; char kernel_number[NAME_SIZE]; char kernel_name[NAME_SIZE]; int test_run; @@ -89,11 +87,11 @@ extern int udev_start(void); extern void udev_multiplex_directory(struct udevice *udev, const char *basedir, const char *suffix); extern int udev_make_node(struct udevice *udev, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid); -extern char sysfs_path[SYSFS_PATH_MAX]; -extern char udev_root[PATH_MAX]; -extern char udev_db_path[PATH_MAX+NAME_MAX]; -extern char udev_config_filename[PATH_MAX+NAME_MAX]; -extern char udev_rules_filename[PATH_MAX+NAME_MAX]; +extern char sysfs_path[PATH_SIZE]; +extern char udev_root[PATH_SIZE]; +extern char udev_db_path[PATH_SIZE]; +extern char udev_config_filename[PATH_SIZE]; +extern char udev_rules_filename[PATH_SIZE]; extern int udev_log; extern int udev_dev_d; extern int udev_hotplug_d; diff --git a/udev_add.c b/udev_add.c index e05710ce..f47ca25f 100644 --- a/udev_add.c +++ b/udev_add.c @@ -111,16 +111,16 @@ exit: static int create_node(struct udevice *udev, struct sysfs_class_device *class_dev) { - char filename[NAME_SIZE]; - char partitionname[NAME_SIZE]; + char filename[PATH_SIZE]; + char partitionname[PATH_SIZE]; struct name_entry *name_loop; uid_t uid = 0; gid_t gid = 0; int tail; int i; - snprintf(filename, NAME_SIZE, "%s/%s", udev_root, udev->name); - filename[NAME_SIZE-1] = '\0'; + snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name); + filename[sizeof(filename)-1] = '\0'; /* create parent directories if needed */ if (strchr(udev->name, '/')) @@ -173,8 +173,8 @@ static int create_node(struct udevice *udev, struct sysfs_class_device *class_de for (i = 1; i <= udev->partitions; i++) { dev_t part_devt; - snprintf(partitionname, NAME_SIZE, "%s%d", filename, i); - partitionname[NAME_SIZE-1] = '\0'; + snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i); + partitionname[sizeof(partitionname)-1] = '\0'; part_devt = makedev(major(udev->devt), minor(udev->devt)+1); udev_make_node(udev, partitionname, part_devt, udev->mode, uid, gid); } @@ -183,10 +183,10 @@ static int create_node(struct udevice *udev, struct sysfs_class_device *class_de /* create symlink(s) if requested */ list_for_each_entry(name_loop, &udev->symlink_list, node) { - char linktarget[NAME_SIZE]; + char linktarget[PATH_SIZE]; - snprintf(filename, NAME_SIZE, "%s/%s", udev_root, name_loop->name); - filename[NAME_SIZE-1] = '\0'; + snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name); + filename[sizeof(filename)-1] = '\0'; dbg("symlink '%s' to node '%s' requested", filename, udev->name); if (!udev->test_run) @@ -204,11 +204,11 @@ static int create_node(struct udevice *udev, struct sysfs_class_device *class_de } while (name_loop->name[i] != '\0') { if (name_loop->name[i] == '/') - strfieldcat(linktarget, "../"); + strlcat(linktarget, "../", sizeof(linktarget)); i++; } - strfieldcat(linktarget, &udev->name[tail]); + strlcat(linktarget, &udev->name[tail], sizeof(linktarget)); dbg("symlink(%s, %s)", linktarget, filename); if (!udev->test_run) { @@ -242,8 +242,8 @@ static int rename_net_if(struct udevice *udev) } memset(&ifr, 0x00, sizeof(struct ifreq)); - strfieldcpy(ifr.ifr_name, udev->kernel_name); - strfieldcpy(ifr.ifr_newname, udev->name); + strlcpy(ifr.ifr_name, udev->kernel_name, IFNAMSIZ); + strlcpy(ifr.ifr_newname, udev->name, IFNAMSIZ); retval = ioctl(sk, SIOCSIFNAME, &ifr); if (retval != 0) @@ -283,8 +283,8 @@ int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev) "remove might not work for custom names"); /* use full path to the environment */ - snprintf(udev->devname, NAME_SIZE, "%s/%s", udev_root, udev->name); - udev->devname[NAME_SIZE-1] = '\0'; + snprintf(udev->devname, sizeof(udev->devname), "%s/%s", udev_root, udev->name); + udev->devname[sizeof(udev->devname)-1] = '\0'; } else if (udev->type == NET) { /* look if we want to change the name of the netif */ @@ -300,13 +300,13 @@ int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev) pos = strrchr(udev->devpath, '/'); if (pos != NULL) { pos[1] = '\0'; - strfieldcat(udev->devpath, udev->name); + strlcat(udev->devpath, udev->name, sizeof(udev->devpath)); setenv("DEVPATH", udev->devpath, 1); setenv("INTERFACE", udev->name, 1); } /* use netif name for the environment */ - strfieldcpy(udev->devname, udev->name); + strlcpy(udev->devname, udev->name, sizeof(udev->devname)); } } diff --git a/udev_config.c b/udev_config.c index 5e204488..26ecf629 100644 --- a/udev_config.c +++ b/udev_config.c @@ -33,6 +33,7 @@ #include #include "libsysfs/sysfs/libsysfs.h" +#include "udev_libc_wrapper.h" #include "udev.h" #include "udev_utils.h" #include "udev_version.h" @@ -40,11 +41,11 @@ #include "namedev.h" /* global variables */ -char sysfs_path[SYSFS_PATH_MAX]; -char udev_root[PATH_MAX]; -char udev_db_path[PATH_MAX+NAME_MAX]; -char udev_rules_filename[PATH_MAX+NAME_MAX]; -char udev_config_filename[PATH_MAX+NAME_MAX]; +char sysfs_path[PATH_SIZE]; +char udev_root[PATH_SIZE]; +char udev_db_path[PATH_SIZE]; +char udev_rules_filename[PATH_SIZE]; +char udev_config_filename[PATH_SIZE]; int udev_log; int udev_dev_d; int udev_hotplug_d; @@ -98,12 +99,11 @@ static int parse_config_file(void) int lineno; int retval = 0; - if (file_map(udev_config_filename, &buf, &bufsize) == 0) { - dbg("reading '%s' as config file", udev_config_filename); - } else { + if (file_map(udev_config_filename, &buf, &bufsize) != 0) { dbg("can't open '%s' as config file", udev_config_filename); return -ENODEV; } + dbg("reading '%s' as config file", udev_config_filename); /* loop through the whole file */ lineno = 0; @@ -114,7 +114,7 @@ static int parse_config_file(void) cur += count+1; lineno++; - if (count >= LINE_SIZE) { + if (count >= sizeof(line)) { info("line too long, conf line skipped %s, line %d", udev_config_filename, lineno); continue; @@ -132,8 +132,7 @@ static int parse_config_file(void) if (bufline[0] == COMMENT_CHARACTER) continue; - strncpy(line, bufline, count); - line[count] = '\0'; + strlcpy(line, bufline, count); temp = line; dbg_parse("read '%s'", temp); @@ -145,19 +144,19 @@ static int parse_config_file(void) dbg_parse("variable='%s', value='%s'", variable, value); if (strcasecmp(variable, "udev_root") == 0) { - strfieldcpy(udev_root, value); + strlcpy(udev_root, value, sizeof(udev_root)); no_trailing_slash(udev_root); continue; } if (strcasecmp(variable, "udev_db") == 0) { - strfieldcpy(udev_db_path, value); + strlcpy(udev_db_path, value, sizeof(udev_db_path)); no_trailing_slash(udev_db_path); continue; } if (strcasecmp(variable, "udev_rules") == 0) { - strfieldcpy(udev_rules_filename, value); + strlcpy(udev_rules_filename, value, sizeof(udev_rules_filename)); no_trailing_slash(udev_rules_filename); continue; } @@ -177,7 +176,7 @@ static void get_dirs(void) char *temp; int retval; - retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX); + retval = sysfs_get_mnt_path(sysfs_path, sizeof(sysfs_path)); if (retval) dbg("sysfs_get_mnt_path failed"); @@ -185,13 +184,13 @@ static void get_dirs(void) if (getenv("UDEV_TEST") != NULL) { temp = getenv("SYSFS_PATH"); if (temp != NULL) { - strfieldcpy(sysfs_path, temp); + strlcpy(sysfs_path, temp, sizeof(sysfs_path)); no_trailing_slash(sysfs_path); } temp = getenv("UDEV_CONFIG_FILE"); if (temp != NULL) - strfieldcpy(udev_config_filename, temp); + strlcpy(udev_config_filename, temp, sizeof(udev_config_filename)); } parse_config_file(); diff --git a/udev_db.c b/udev_db.c index 653d7d08..806fc71b 100644 --- a/udev_db.c +++ b/udev_db.c @@ -33,6 +33,7 @@ #include #include "libsysfs/sysfs/libsysfs.h" +#include "udev_libc_wrapper.h" #include "udev.h" #include "udev_utils.h" #include "logging.h" @@ -42,11 +43,11 @@ static int get_db_filename(const char *devpath, char *filename, int len) { - char temp[SYSFS_PATH_MAX]; + char temp[PATH_SIZE]; char *pos; /* replace '/' to transform path into a filename */ - strfieldcpy(temp, devpath); + strlcpy(temp, devpath, sizeof(temp)); pos = strchr(&temp[1], '/'); while (pos) { pos[0] = PATH_TO_NAME_CHAR; @@ -60,14 +61,14 @@ static int get_db_filename(const char *devpath, char *filename, int len) int udev_db_add_device(struct udevice *udev) { - char filename[SYSFS_PATH_MAX]; + char filename[PATH_SIZE]; struct name_entry *name_loop; FILE *f; if (udev->test_run) return 0; - get_db_filename(udev->devpath, filename, SYSFS_PATH_MAX); + get_db_filename(udev->devpath, filename, sizeof(filename)); create_path(filename); @@ -93,8 +94,7 @@ int udev_db_add_device(struct udevice *udev) static int parse_db_file(struct udevice *udev, const char *filename) { - char line[NAME_SIZE]; - char temp[NAME_SIZE]; + char line[PATH_SIZE]; unsigned int major, minor; char *bufline; char *buf; @@ -115,44 +115,38 @@ static int parse_db_file(struct udevice *udev, const char *filename) switch(bufline[0]) { case 'P': - if (count > DEVPATH_SIZE) - count = DEVPATH_SIZE-1; - strncpy(udev->devpath, &bufline[2], count-2); - udev->devpath[count-2] = '\0'; + if (count > sizeof(udev->devpath)) + count = sizeof(udev->devpath)-1; + strlcpy(udev->devpath, &bufline[2], count-2); break; case 'N': - if (count > NAME_SIZE) - count = NAME_SIZE-1; - strncpy(udev->name, &bufline[2], count-2); - udev->name[count-2] = '\0'; + if (count > sizeof(udev->name)) + count = sizeof(udev->name)-1; + strlcpy(udev->name, &bufline[2], count-2); break; case 'M': - if (count > NAME_SIZE) - count = NAME_SIZE-1; - strncpy(temp, &bufline[2], count-2); - temp[count-2] = '\0'; - sscanf(temp, "%u:%u", &major, &minor); + if (count > sizeof(line)) + count = sizeof(line)-1; + strlcpy(line, &bufline[2], count-2); + sscanf(line, "%u:%u", &major, &minor); udev->devt = makedev(major, minor); break; case 'S': - if (count > NAME_SIZE) - count = NAME_SIZE-1; - strncpy(temp, &bufline[2], count-2); - temp[count-2] = '\0'; - name_list_add(&udev->symlink_list, temp, 0); + if (count > sizeof(line)) + count = sizeof(line)-1; + strlcpy(line, &bufline[2], count-2); + name_list_add(&udev->symlink_list, line, 0); break; case 'A': - if (count > NAME_SIZE) - count = NAME_SIZE-1; - strncpy(line, &bufline[2], count-2); - line[count-2] = '\0'; + if (count > sizeof(line)) + count = sizeof(line)-1; + strlcpy(line, &bufline[2], count-2); udev->partitions = atoi(line); break; case 'R': - if (count > NAME_SIZE) - count = NAME_SIZE-1; - strncpy(line, &bufline[2], count-2); - line[count-2] = '\0'; + if (count > sizeof(line)) + count = sizeof(line)-1; + strlcpy(line, &bufline[2], count-2); udev->ignore_remove = atoi(line); break; } @@ -167,9 +161,9 @@ static int parse_db_file(struct udevice *udev, const char *filename) int udev_db_delete_device(struct udevice *udev) { - char filename[SYSFS_PATH_MAX]; + char filename[PATH_SIZE]; - get_db_filename(udev->devpath, filename, SYSFS_PATH_MAX); + get_db_filename(udev->devpath, filename, sizeof(filename)); unlink(filename); return 0; @@ -177,9 +171,9 @@ int udev_db_delete_device(struct udevice *udev) int udev_db_get_device(struct udevice *udev, const char *devpath) { - char filename[SYSFS_PATH_MAX]; + char filename[PATH_SIZE]; - get_db_filename(devpath, filename, SYSFS_PATH_MAX); + get_db_filename(devpath, filename, sizeof(filename)); if (parse_db_file(udev, filename) != 0) return -1; @@ -199,9 +193,9 @@ int udev_db_search_name(char *devpath, size_t len, const char *name) while (1) { struct dirent *ent; - char filename[NAME_SIZE]; - char path[DEVPATH_SIZE]; - char nodename[NAME_SIZE]; + char filename[PATH_SIZE]; + char path[PATH_SIZE]; + char nodename[PATH_SIZE]; char *bufline; char *buf; size_t bufsize; @@ -215,8 +209,8 @@ int udev_db_search_name(char *devpath, size_t len, const char *name) if (ent->d_name[0] == '.') continue; - snprintf(filename, NAME_SIZE, "%s/%s", udev_db_path, ent->d_name); - filename[NAME_SIZE-1] = '\0'; + snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name); + filename[sizeof(filename)-1] = '\0'; dbg("looking at '%s'", filename); if (file_map(filename, &buf, &bufsize) != 0) { @@ -232,21 +226,18 @@ int udev_db_search_name(char *devpath, size_t len, const char *name) switch(bufline[0]) { case 'P': - if (count > DEVPATH_SIZE) - count = DEVPATH_SIZE-1; - strncpy(path, &bufline[2], count-2); - path[count-2] = '\0'; + if (count > sizeof(path)) + count = sizeof(path)-1; + strlcpy(path, &bufline[2], count-2); break; case 'N': case 'S': - if (count > NAME_SIZE) - count = NAME_SIZE-1; - strncpy(nodename, &bufline[2], count-2); - nodename[count-2] = '\0'; + if (count > sizeof(nodename)) + count = sizeof(nodename)-1; + strlcpy(nodename, &bufline[2], count-2); dbg("compare '%s' '%s'", nodename, name); if (strcmp(nodename, name) == 0) { - strncpy(devpath, path, len); - devpath[len] = '\0'; + strlcpy(devpath, path, len); file_unmap(buf, bufsize); closedir(dir); return 0; @@ -275,9 +266,9 @@ int udev_db_dump_names(int (*handler_function)(const char *path, const char *nam while (1) { struct dirent *ent; - char filename[NAME_SIZE]; - char path[DEVPATH_SIZE]; - char nodename[NAME_SIZE]; + char filename[PATH_SIZE]; + char path[PATH_SIZE]; + char nodename[PATH_SIZE]; char *bufline; char *buf; size_t bufsize; @@ -291,8 +282,8 @@ int udev_db_dump_names(int (*handler_function)(const char *path, const char *nam if (ent->d_name[0] == '.') continue; - snprintf(filename, NAME_SIZE, "%s/%s", udev_db_path, ent->d_name); - filename[NAME_SIZE-1] = '\0'; + snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name); + filename[sizeof(filename)-1] = '\0'; dbg("looking at '%s'", filename); if (file_map(filename, &buf, &bufsize) != 0) { @@ -310,16 +301,14 @@ int udev_db_dump_names(int (*handler_function)(const char *path, const char *nam switch(bufline[0]) { case 'P': - if (count > DEVPATH_SIZE) - count = DEVPATH_SIZE-1; - strncpy(path, &bufline[2], count-2); - path[count-2] = '\0'; + if (count > sizeof(path)) + count = sizeof(path)-1; + strlcpy(path, &bufline[2], count-2); break; case 'N': - if (count > NAME_SIZE) - count = NAME_SIZE-1; - strncpy(nodename, &bufline[2], count-2); - nodename[count-2] = '\0'; + if (count > sizeof(nodename)) + count = sizeof(nodename)-1; + strlcpy(nodename, &bufline[2], count-2); break; default: continue; diff --git a/udev_libc_wrapper.c b/udev_libc_wrapper.c index df545152..0dafe4d0 100644 --- a/udev_libc_wrapper.c +++ b/udev_libc_wrapper.c @@ -32,16 +32,58 @@ #include "../udev_utils.h" #include "../logging.h" - #ifdef __KLIBC__ #define __OWN_USERDB_PARSER__ #endif + #ifdef USE_STATIC #define __OWN_USERDB_PARSER__ #endif -#ifndef __OWN_USERDB_PARSER__ +#ifndef strlcpy +size_t strlcpy(char *dst, const char *src, size_t size) +{ + size_t bytes = 0; + char *q = dst; + const char *p = src; + char ch; + + while ((ch = *p++)) { + if (bytes < size) + *q++ = ch; + bytes++; + } + + *q = '\0'; + return bytes; +} +#endif + +#ifndef strlcat +size_t strlcat(char *dst, const char *src, size_t size) +{ + size_t bytes = 0; + char *q = dst; + const char *p = src; + char ch; + + while (bytes < size && *q) { + q++; + bytes++; + } + while ((ch = *p++)) { + if (bytes < size) + *q++ = ch; + bytes++; + } + + *q = '\0'; + return bytes; +} +#endif + +#ifndef __OWN_USERDB_PARSER__ #include #include #include @@ -107,11 +149,10 @@ static unsigned long get_id_by_name(const char *uname, const char *dbfile) bufline = &buf[cur]; cur += count+1; - if (count >= LINE_SIZE) + if (count >= sizeof(line)) continue; - strncpy(line, bufline, count); - line[count] = '\0'; + strlcpy(line, bufline, count); pos = line; /* get name */ @@ -158,5 +199,4 @@ gid_t lookup_group(const char *group) id = get_id_by_name(group, GROUP_FILE); return (gid_t) id; } - #endif /* __OWN_USERDB_PARSER__ */ diff --git a/udev_libc_wrapper.h b/udev_libc_wrapper.h index 2df2f442..65f4e01c 100644 --- a/udev_libc_wrapper.h +++ b/udev_libc_wrapper.h @@ -22,7 +22,27 @@ #ifndef _UDEV_LIBC_WRAPPER_H_ #define _UDEV_LIBC_WRAPPER_H_ +#ifdef asmlinkage +# undef asmlinkage +#endif +#ifdef __i386__ +# define asmlinkage __attribute__((regparm(0))) +#endif +#ifndef asmlinkage +# define asmlinkage +#endif + +#include + extern uid_t lookup_user(const char *user); extern gid_t lookup_group(const char *group); +#ifndef strlcat +extern size_t strlcpy(char *dst, const char *src, size_t size); +#endif + +#ifndef strlcat +extern size_t strlcat(char *dst, const char *src, size_t size); +#endif + #endif /* _UDEV_LIBC_WRAPPER_H_ */ diff --git a/udev_multiplex.c b/udev_multiplex.c index 6d7852db..3ef6e170 100644 --- a/udev_multiplex.c +++ b/udev_multiplex.c @@ -28,6 +28,7 @@ #include #include "udev.h" +#include "udev_libc_wrapper.h" #include "udev_utils.h" #include "logging.h" @@ -72,22 +73,22 @@ static int run_program(struct udevice *udev, const char *filename) */ void udev_multiplex_directory(struct udevice *udev, const char *basedir, const char *suffix) { - char dirname[PATH_MAX]; + char dirname[PATH_SIZE]; /* chop the device name up into pieces based on '/' */ if (udev->name[0] != '\0') { - char devname[NAME_SIZE]; + char devname[PATH_SIZE]; char *temp; - strfieldcpy(devname, udev->name); + strlcpy(devname, udev->name, sizeof(devname)); temp = strchr(devname, '/'); while (temp != NULL) { temp[0] = '\0'; /* don't call the subsystem directory here */ if (strcmp(devname, udev->subsystem) != 0) { - snprintf(dirname, PATH_MAX, "%s/%s", basedir, devname); - dirname[PATH_MAX-1] = '\0'; + snprintf(dirname, sizeof(dirname), "%s/%s", basedir, devname); + dirname[sizeof(dirname)-1] = '\0'; call_foreach_file(run_program, udev, dirname, suffix); } @@ -98,18 +99,18 @@ void udev_multiplex_directory(struct udevice *udev, const char *basedir, const c } if (udev->name[0] != '\0') { - snprintf(dirname, PATH_MAX, "%s/%s", basedir, udev->name); - dirname[PATH_MAX-1] = '\0'; + snprintf(dirname, sizeof(dirname), "%s/%s", basedir, udev->name); + dirname[sizeof(dirname)-1] = '\0'; call_foreach_file(run_program, udev, dirname, suffix); } if (udev->subsystem[0] != '\0') { - snprintf(dirname, PATH_MAX, "%s/%s", basedir, udev->subsystem); - dirname[PATH_MAX-1] = '\0'; + snprintf(dirname, sizeof(dirname), "%s/%s", basedir, udev->subsystem); + dirname[sizeof(dirname)-1] = '\0'; call_foreach_file(run_program, udev, dirname, suffix); } - snprintf(dirname, PATH_MAX, "%s/default", basedir); - dirname[PATH_MAX-1] = '\0'; + snprintf(dirname, sizeof(dirname), "%s/default", basedir); + dirname[sizeof(dirname)-1] = '\0'; call_foreach_file(run_program, udev, dirname, suffix); } diff --git a/udev_remove.c b/udev_remove.c index 7a6c032d..1fd36d24 100644 --- a/udev_remove.c +++ b/udev_remove.c @@ -30,6 +30,7 @@ #include #include +#include "udev_libc_wrapper.h" #include "udev.h" #include "udev_utils.h" #include "udev_version.h" @@ -69,16 +70,16 @@ static int delete_path(const char *path) static int delete_node(struct udevice *udev) { - char filename[NAME_SIZE]; - char partitionname[NAME_SIZE]; + char filename[PATH_SIZE]; + char partitionname[PATH_SIZE]; struct name_entry *name_loop; struct stat stats; int retval; int i; int num; - snprintf(filename, NAME_SIZE, "%s/%s", udev_root, udev->name); - filename[NAME_SIZE-1] = '\0'; + snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name); + filename[sizeof(filename)-1] = '\0'; dbg("checking major/minor of device node '%s'", filename); if (stat(filename, &stats) != 0) @@ -103,8 +104,8 @@ static int delete_node(struct udevice *udev) return -1; } for (i = 1; i <= num; i++) { - snprintf(partitionname, NAME_SIZE, "%s%d", filename, i); - partitionname[NAME_SIZE-1] = '\0'; + snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i); + partitionname[sizeof(partitionname)-1] = '\0'; unlink_secure(partitionname); } } @@ -114,8 +115,8 @@ static int delete_node(struct udevice *udev) delete_path(filename); list_for_each_entry(name_loop, &udev->symlink_list, node) { - snprintf(filename, NAME_SIZE, "%s/%s", udev_root, name_loop->name); - filename[NAME_SIZE-1] = '\0'; + snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name); + filename[sizeof(filename)-1] = '\0'; dbg("unlinking symlink '%s'", filename); retval = unlink(filename); @@ -157,12 +158,13 @@ int udev_remove_device(struct udevice *udev) temp = strrchr(udev->devpath, '/'); if (temp == NULL) return -ENODEV; - strfieldcpy(udev->name, &temp[1]); + strlcpy(udev->name, &temp[1], sizeof(udev->name)); dbg("'%s' not found in database, falling back on default name", udev->name); } /* use full path to the environment */ - snprintf(udev->devname, NAME_SIZE, "%s/%s", udev_root, udev->name); + snprintf(udev->devname, sizeof(udev->devname), "%s/%s", udev_root, udev->name); + udev->devname[sizeof(udev->devname)-1] = '\0'; return delete_node(udev); } diff --git a/udev_sysfs.c b/udev_sysfs.c index fc7ee885..f9ff1ed2 100644 --- a/udev_sysfs.c +++ b/udev_sysfs.c @@ -105,7 +105,7 @@ static int wait_for_class_device_attributes(struct sysfs_class_device *class_dev const char **error) { const char *file; - char filename[SYSFS_PATH_MAX]; + char filename[PATH_SIZE]; int loop; file = get_subsystem_specific_file(class_dev->classname); @@ -114,7 +114,8 @@ static int wait_for_class_device_attributes(struct sysfs_class_device *class_dev return 0; } - snprintf(filename, SYSFS_PATH_MAX-1, "%s/%s", class_dev->path, file); + snprintf(filename, sizeof(filename), "%s/%s", class_dev->path, file); + filename[sizeof(filename)-1] = '\0'; dbg("looking at class '%s' for specific file '%s'", class_dev->classname, filename); loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND; @@ -363,7 +364,7 @@ int wait_for_devices_device(struct sysfs_device *devices_dev, for (devicefile = device_files; devicefile->bus != NULL; devicefile++) { if (strcmp(devices_dev->bus, devicefile->bus) == 0) { - char filename[SYSFS_PATH_MAX]; + char filename[PATH_SIZE]; struct stat stats; if (devicefile->file == NULL) { @@ -372,7 +373,8 @@ int wait_for_devices_device(struct sysfs_device *devices_dev, } found_bus_type = 1; - snprintf(filename, SYSFS_PATH_MAX-1, "%s/%s", devices_dev->path, devicefile->file); + snprintf(filename, sizeof(filename), "%s/%s", devices_dev->path, devicefile->file); + filename[sizeof(filename)-1] = '\0'; dbg("looking at bus '%s' device for specific file '%s'", devices_dev->bus, filename); if (stat(filename, &stats) == 0) { diff --git a/udev_utils.c b/udev_utils.c index 145f02b5..d572581f 100644 --- a/udev_utils.c +++ b/udev_utils.c @@ -31,6 +31,7 @@ #include #include +#include "udev_libc_wrapper.h" #include "udev.h" #include "logging.h" #include "udev_utils.h" @@ -45,10 +46,10 @@ int udev_init_device(struct udevice *udev, const char* devpath, const char *subs INIT_LIST_HEAD(&udev->symlink_list); if (subsystem) - strfieldcpy(udev->subsystem, subsystem); + strlcpy(udev->subsystem, subsystem, sizeof(udev->subsystem)); if (devpath) { - strfieldcpy(udev->devpath, devpath); + strlcpy(udev->devpath, devpath, sizeof(udev->devpath)); no_trailing_slash(udev->devpath); if (strncmp(udev->devpath, "/block/", 7) == 0) @@ -63,7 +64,7 @@ int udev_init_device(struct udevice *udev, const char* devpath, const char *subs /* get kernel name */ pos = strrchr(udev->devpath, '/'); if (pos) { - strfieldcpy(udev->kernel_name, &pos[1]); + strlcpy(udev->kernel_name, &pos[1], sizeof(udev->kernel_name)); dbg("kernel_name='%s'", udev->kernel_name); /* Some block devices have '!' in their name, change that to '/' */ @@ -78,7 +79,7 @@ int udev_init_device(struct udevice *udev, const char* devpath, const char *subs pos = &udev->kernel_name[strlen(udev->kernel_name)]; while (isdigit(pos[-1])) pos--; - strfieldcpy(udev->kernel_number, pos); + strlcpy(udev->kernel_number, pos, sizeof(udev->kernel_number)); dbg("kernel_number='%s'", udev->kernel_number); } } @@ -126,7 +127,7 @@ int kernel_release_satisfactory(unsigned int version, unsigned int patchlevel, u int create_path(const char *path) { - char p[NAME_SIZE]; + char p[PATH_SIZE]; char *pos; struct stat stats; @@ -195,19 +196,19 @@ int parse_get_pair(char **orig_string, char **left, char **right) return -ENODEV; /* take the right side and strip off the '"' */ - while (isspace(*string)) + while (isspace(string[0])) ++string; - if (*string == '"') + if (string[0] == '"') ++string; else return -ENODEV; temp = strsep(&string, "\""); - if (!string || *temp == '\0') + if (!string || temp[0] == '\0') return -ENODEV; *right = temp; *orig_string = string; - + return 0; } @@ -292,7 +293,7 @@ int name_list_add(struct list_head *name_list, const char *name, int sort) return -ENOMEM; } - strfieldcpy(new_name->name, name); + strlcpy(new_name->name, name, sizeof(new_name->name)); list_add_tail(&new_name->node, &loop_name->node); return 0; @@ -338,10 +339,10 @@ int call_foreach_file(int (*handler_function)(struct udevice *udev, const char * /* call function for every file in the list */ list_for_each_entry_safe(loop_file, tmp_file, &file_list, node) { - char filename[NAME_SIZE]; + char filename[PATH_SIZE]; - snprintf(filename, NAME_SIZE, "%s/%s", dirname, loop_file->name); - filename[NAME_SIZE-1] = '\0'; + snprintf(filename, sizeof(filename), "%s/%s", dirname, loop_file->name); + filename[sizeof(filename)-1] = '\0'; handler_function(udev, filename); diff --git a/udev_utils.h b/udev_utils.h index 3bb1b253..c7a41aaa 100644 --- a/udev_utils.h +++ b/udev_utils.h @@ -24,43 +24,9 @@ #include "udev.h" -#define strfieldcpy(to, from) \ -do { \ - to[sizeof(to)-1] = '\0'; \ - strncpy(to, from, sizeof(to)-1); \ -} while (0) - -#define strfieldcat(to, from) \ -do { \ - to[sizeof(to)-1] = '\0'; \ - strncat(to, from, sizeof(to) - strlen(to)-1); \ -} while (0) - -#define strfieldcpymax(to, from, maxsize) \ -do { \ - to[maxsize-1] = '\0'; \ - strncpy(to, from, maxsize-1); \ -} while (0) - -#define strfieldcatmax(to, from, maxsize) \ -do { \ - to[maxsize-1] = '\0'; \ - strncat(to, from, maxsize - strlen(to)-1); \ -} while (0) - -#ifdef asmlinkage -# undef asmlinkage -#endif -#ifdef __i386__ -# define asmlinkage __attribute__((regparm(0))) -#endif -#ifndef asmlinkage -# define asmlinkage -#endif - struct name_entry { struct list_head node; - char name[NAME_SIZE]; + char name[PATH_SIZE]; }; extern int udev_init_device(struct udevice *udev, const char* devpath, const char *subsystem); diff --git a/udevd.c b/udevd.c index 1376cf05..c2f2dd04 100644 --- a/udevd.c +++ b/udevd.c @@ -39,6 +39,7 @@ #include #include "list.h" +#include "udev_libc_wrapper.h" #include "udev.h" #include "udev_version.h" #include "udev_utils.h" @@ -262,7 +263,7 @@ static int compare_devpath(const char *running, const char *waiting) { int i; - for (i = 0; i < DEVPATH_SIZE; i++) { + for (i = 0; i < PATH_SIZE; i++) { /* identical device event found */ if (running[i] == '\0' && waiting[i] == '\0') return 1; diff --git a/udevinfo.c b/udevinfo.c index e30a036a..4d13a5f6 100644 --- a/udevinfo.c +++ b/udevinfo.c @@ -28,6 +28,7 @@ #include "libsysfs/sysfs/libsysfs.h" #include "libsysfs/dlist.h" +#include "udev_libc_wrapper.h" #include "udev.h" #include "udev_utils.h" #include "udev_version.h" @@ -35,8 +36,6 @@ #include "logging.h" -#define SYSFS_VALUE_SIZE 256 - #ifdef USE_LOG void log_message (int level, const char *format, ...) { @@ -51,12 +50,12 @@ void log_message (int level, const char *format, ...) static void print_all_attributes(struct dlist *attr_list) { struct sysfs_attribute *attr; - char value[SYSFS_VALUE_SIZE]; + char value[VALUE_SIZE]; int len; dlist_for_each_data(attr_list, attr, struct sysfs_attribute) { if (attr->value != NULL) { - strfieldcpy(value, attr->value); + strlcpy(value, attr->value, sizeof(value)); len = strlen(value); if (len == 0) continue; @@ -193,9 +192,9 @@ int main(int argc, char *argv[], char *envp[]) int root = 0; int attributes = 0; enum query_type query = NONE; - char path[NAME_SIZE] = ""; - char name[NAME_SIZE] = ""; - char temp[NAME_SIZE]; + char path[PATH_SIZE] = ""; + char name[PATH_SIZE] = ""; + char temp[PATH_SIZE]; struct name_entry *name_loop; char *pos; int retval = 0; @@ -215,12 +214,12 @@ int main(int argc, char *argv[], char *envp[]) switch (option) { case 'n': dbg("udev name: %s\n", optarg); - strfieldcpy(name, optarg); + strlcpy(name, optarg, sizeof(name)); break; case 'p': dbg("udev path: %s\n", optarg); - strfieldcpy(path, optarg); + strlcpy(path, optarg, sizeof(path)); break; case 'q': @@ -284,7 +283,7 @@ int main(int argc, char *argv[], char *envp[]) if (path[0] != '/') { /* prepend '/' if missing */ strcpy(temp, "/"); - strfieldcat(temp, path); + strlcpy(temp, path, sizeof(temp)); pos = temp; } else { pos = path; @@ -299,7 +298,7 @@ int main(int argc, char *argv[], char *envp[]) } if (name[0] != '\0') { - char devpath[NAME_SIZE]; + char devpath[PATH_SIZE]; int len; /* remove udev_root if given */ @@ -309,7 +308,7 @@ int main(int argc, char *argv[], char *envp[]) } else pos = name; - retval = udev_db_search_name(devpath, DEVPATH_SIZE, pos); + retval = udev_db_search_name(devpath, sizeof(devpath), pos); if (retval != 0) { printf("device not found in database\n"); goto exit; @@ -360,9 +359,9 @@ print: } else { if (strncmp(path, sysfs_path, strlen(sysfs_path)) != 0) { /* prepend sysfs mountpoint if not given */ - strfieldcpy(temp, path); - strfieldcpy(path, sysfs_path); - strfieldcat(path, temp); + snprintf(temp, sizeof(temp), "%s%s", sysfs_path, path); + temp[sizeof(temp)-1] = '\0'; + strlcpy(path, temp, sizeof(temp)); } print_device_chain(path); goto exit; diff --git a/udevstart.c b/udevstart.c index 97d38490..6b032a19 100644 --- a/udevstart.c +++ b/udevstart.c @@ -36,11 +36,12 @@ #include #include "libsysfs/sysfs/libsysfs.h" +#include "udev_libc_wrapper.h" +#include "udev.h" #include "logging.h" #include "namedev.h" #include "udev_utils.h" #include "list.h" -#include "udev.h" #ifdef USE_LOG void log_message(int level, const char *format, ...) @@ -50,8 +51,8 @@ void log_message(int level, const char *format, ...) struct device { struct list_head list; - char path[DEVPATH_SIZE]; - char subsys[SUBSYSTEM_SIZE]; + char path[PATH_SIZE]; + char subsys[NAME_SIZE]; }; /* sort files in lexical order */ @@ -74,8 +75,8 @@ static int device_list_insert(const char *path, char *subsystem, struct list_hea return -ENOMEM; } - strfieldcpy(new_device->path, path); - strfieldcpy(new_device->subsys, subsystem); + strlcpy(new_device->path, path, sizeof(new_device->path)); + strlcpy(new_device->subsys, subsystem, sizeof(new_device->subsys)); list_add_tail(&new_device->list, &loop_device->list); dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys); return 0; @@ -173,11 +174,11 @@ static void exec_list(struct list_head *device_list) static int has_devt(const char *directory) { - char filename[NAME_SIZE]; + char filename[PATH_SIZE]; struct stat statbuf; - snprintf(filename, NAME_SIZE, "%s/dev", directory); - filename[NAME_SIZE-1] = '\0'; + snprintf(filename, sizeof(filename), "%s/dev", directory); + filename[sizeof(filename)-1] = '\0'; if (stat(filename, &statbuf) == 0) return 1; @@ -187,26 +188,26 @@ static int has_devt(const char *directory) static void udev_scan_block(void) { - char base[NAME_SIZE]; + char base[PATH_SIZE]; DIR *dir; struct dirent *dent; LIST_HEAD(device_list); - snprintf(base, DEVPATH_SIZE, "%s/block", sysfs_path); - base[DEVPATH_SIZE-1] = '\0'; + snprintf(base, sizeof(base), "%s/block", sysfs_path); + base[sizeof(base)-1] = '\0'; dir = opendir(base); if (dir != NULL) { for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { - char dirname[DEVPATH_SIZE]; + char dirname[PATH_SIZE]; DIR *dir2; struct dirent *dent2; if (dent->d_name[0] == '.') continue; - snprintf(dirname, NAME_SIZE, "%s/%s", base, dent->d_name); - dirname[NAME_SIZE-1] = '\0'; + snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name); + dirname[sizeof(dirname)-1] = '\0'; if (has_devt(dirname)) device_list_insert(dirname, "block", &device_list); else @@ -216,13 +217,13 @@ static void udev_scan_block(void) dir2 = opendir(dirname); if (dir2 != NULL) { for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) { - char dirname2[DEVPATH_SIZE]; + char dirname2[PATH_SIZE]; if (dent2->d_name[0] == '.') continue; - snprintf(dirname2, DEVPATH_SIZE, "%s/%s", dirname, dent2->d_name); - dirname2[DEVPATH_SIZE-1] = '\0'; + snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name); + dirname2[sizeof(dirname2)-1] = '\0'; if (has_devt(dirname2)) device_list_insert(dirname2, "block", &device_list); @@ -237,37 +238,37 @@ static void udev_scan_block(void) static void udev_scan_class(void) { - char base[DEVPATH_SIZE]; + char base[PATH_SIZE]; DIR *dir; struct dirent *dent; LIST_HEAD(device_list); - snprintf(base, DEVPATH_SIZE, "%s/class", sysfs_path); - base[DEVPATH_SIZE-1] = '\0'; + snprintf(base, sizeof(base), "%s/class", sysfs_path); + base[sizeof(base)-1] = '\0'; dir = opendir(base); if (dir != NULL) { for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { - char dirname[DEVPATH_SIZE]; + char dirname[PATH_SIZE]; DIR *dir2; struct dirent *dent2; if (dent->d_name[0] == '.') continue; - snprintf(dirname, DEVPATH_SIZE, "%s/%s", base, dent->d_name); - dirname[DEVPATH_SIZE-1] = '\0'; + snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name); + dirname[sizeof(dirname)-1] = '\0'; dir2 = opendir(dirname); if (dir2 != NULL) { for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) { - char dirname2[DEVPATH_SIZE]; + char dirname2[PATH_SIZE]; if (dent2->d_name[0] == '.') continue; - snprintf(dirname2, DEVPATH_SIZE, "%s/%s", dirname, dent2->d_name); - dirname2[DEVPATH_SIZE-1] = '\0'; + snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name); + dirname2[sizeof(dirname2)-1] = '\0'; /* pass the net class as it is */ if (strcmp(dent->d_name, "net") == 0) diff --git a/udevtest.c b/udevtest.c index 084fde07..5ce25700 100644 --- a/udevtest.c +++ b/udevtest.c @@ -1,5 +1,5 @@ /* - * udev.c + * udevtest.c * * Userspace devfs * @@ -53,8 +53,8 @@ int main(int argc, char *argv[], char *envp[]) { struct sysfs_class_device *class_dev; char *devpath; - char path[SYSFS_PATH_MAX]; - char temp[NAME_SIZE]; + char path[PATH_SIZE]; + char temp[PATH_SIZE]; struct udevice udev; char *subsystem = NULL; @@ -69,18 +69,16 @@ int main(int argc, char *argv[], char *envp[]) udev_init_config(); /* remove sysfs_path if given */ - if (strncmp(argv[1], sysfs_path, strlen(sysfs_path)) == 0) { + if (strncmp(argv[1], sysfs_path, strlen(sysfs_path)) == 0) devpath = &argv[1][strlen(sysfs_path)] ; - } else if (argv[1][0] != '/') { /* prepend '/' if missing */ - strfieldcpy(temp, "/"); - strfieldcat(temp, argv[1]); + snprintf(temp, sizeof(temp), "/%s", argv[1]); + temp[sizeof(temp)-1] = '\0'; devpath = temp; - } else { + } else devpath = argv[1]; - } info("looking at '%s'", devpath); @@ -100,7 +98,8 @@ int main(int argc, char *argv[], char *envp[]) } /* open the device */ - snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath); + snprintf(path, sizeof(path), "%s%s", sysfs_path, udev.devpath); + path[sizeof(path)-1] = '\0'; class_dev = sysfs_open_class_device_path(path); if (class_dev == NULL) { info("sysfs_open_class_device_path failed"); -- 2.39.5