[PATCH] udev - safer string handling all over the place

On Tue, Feb 24, 2004 at 11:50:52PM +0100, Kay Sievers wrote:
> Here is the first step towards a safer string handling.
> More will follow, but for now only the easy ones :)
>
> Thanks to all who pointed this out. strncat() isn't a nice function. We
> all should remember that the destination string is not terminated if the
> given lenght is shorter than the strlen of the source string.
>
> And shame on the various implementers of strfieldcat() I found in the
> unapplied patches on this list, it's not really better than strncpy()
> and hides the real problem.

Hmm, bk didn't checked in one file, maybe I edited it again as root.
Nevermind, here is the more complete version.
This commit is contained in:
kay.sievers@vrfy.org 2004-02-26 19:37:47 -08:00 committed by Greg KH
parent 167a27e70f
commit c472e3c89b
9 changed files with 48 additions and 42 deletions

View file

@ -157,7 +157,7 @@ static mode_t get_default_mode(void)
static char *get_default_owner(void) static char *get_default_owner(void)
{ {
if (strlen(default_owner_str) == 0) if (strlen(default_owner_str) == 0)
strncpy(default_owner_str, "root", OWNER_SIZE); strfieldcpy(default_owner_str, "root");
return default_owner_str; return default_owner_str;
} }
@ -165,7 +165,7 @@ static char *get_default_owner(void)
static char *get_default_group(void) static char *get_default_group(void)
{ {
if (strlen(default_group_str) == 0) if (strlen(default_group_str) == 0)
strncpy(default_group_str, "root", GROUP_SIZE); strfieldcpy(default_group_str, "root");
return default_group_str; return default_group_str;
} }
@ -276,7 +276,7 @@ static void apply_format(struct udevice *udev, unsigned char *string, struct sys
if (attr != NULL) if (attr != NULL)
i = atoi(attr); i = atoi(attr);
if (i > 0) { if (i > 0) {
strncpy(temp1, udev->program_result, sizeof(temp1)); strfieldcpy(temp1, udev->program_result);
pos2 = temp1; pos2 = temp1;
while (i) { while (i) {
i--; i--;
@ -837,8 +837,8 @@ done:
} else { } else {
/* no matching perms found :( */ /* no matching perms found :( */
udev->mode = get_default_mode(); udev->mode = get_default_mode();
strncpy(udev->owner, get_default_owner(), OWNER_SIZE); strfieldcpy(udev->owner, get_default_owner());
strncpy(udev->group, get_default_group(), GROUP_SIZE); strfieldcpy(udev->group, get_default_group());
} }
dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o", dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
udev->name, udev->owner, udev->group, udev->mode); udev->name, udev->owner, udev->group, udev->mode);

View file

@ -319,21 +319,21 @@ static int namedev_parse_permissions(char *filename)
dbg("cannot parse line '%s'", line); dbg("cannot parse line '%s'", line);
continue; continue;
} }
strncpy(dev.name, temp2, sizeof(dev.name)); strfieldcpy(dev.name, temp2);
temp2 = strsep(&temp, ":"); temp2 = strsep(&temp, ":");
if (!temp2) { if (!temp2) {
dbg("cannot parse line '%s'", line); dbg("cannot parse line '%s'", line);
continue; continue;
} }
strncpy(dev.owner, temp2, sizeof(dev.owner)); strfieldcpy(dev.owner, temp2);
temp2 = strsep(&temp, ":"); temp2 = strsep(&temp, ":");
if (!temp2) { if (!temp2) {
dbg("cannot parse line '%s'", line); dbg("cannot parse line '%s'", line);
continue; continue;
} }
strncpy(dev.group, temp2, sizeof(dev.group)); strfieldcpy(dev.group, temp2);
if (!temp) { if (!temp) {
dbg("cannot parse line: %s", line); dbg("cannot parse line: %s", line);
@ -422,7 +422,7 @@ static int call_foreach_file(int parser (char *f) , char *filename, char *extens
/* parse every file in the list */ /* parse every file in the list */
list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) { list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) {
strfieldcpy(file, filename); strfieldcpy(file, filename);
strcat(file, loop_file->name); strfieldcat(file, loop_file->name);
parser(file); parser(file);
list_del(&loop_file->list); list_del(&loop_file->list);
free(loop_file); free(loop_file);

View file

@ -78,7 +78,7 @@ static int create_path(char *file)
int retval; int retval;
struct stat stats; struct stat stats;
strncpy(p, file, sizeof(p)); strfieldcpy(p, file);
pos = strchr(p+1, '/'); pos = strchr(p+1, '/');
while (1) { while (1) {
pos = strchr(pos+1, '/'); pos = strchr(pos+1, '/');
@ -145,8 +145,8 @@ static int create_node(struct udevice *dev, int fake)
int i; int i;
int tail; int tail;
strncpy(filename, udev_root, sizeof(filename)); strfieldcpy(filename, udev_root);
strncat(filename, dev->name, sizeof(filename)); strfieldcat(filename, dev->name);
switch (dev->type) { switch (dev->type) {
case 'b': case 'b':
@ -225,8 +225,8 @@ static int create_node(struct udevice *dev, int fake)
if (linkname == NULL || linkname[0] == '\0') if (linkname == NULL || linkname[0] == '\0')
break; break;
strncpy(filename, udev_root, sizeof(filename)); strfieldcpy(filename, udev_root);
strncat(filename, linkname, sizeof(filename)); strfieldcat(filename, linkname);
dbg("symlink '%s' to node '%s' requested", filename, dev->name); dbg("symlink '%s' to node '%s' requested", filename, dev->name);
if (!fake) if (!fake)
if (strrchr(linkname, '/')) if (strrchr(linkname, '/'))
@ -243,13 +243,13 @@ static int create_node(struct udevice *dev, int fake)
} }
while (linkname[i] != '\0') { while (linkname[i] != '\0') {
if (linkname[i] == '/') if (linkname[i] == '/')
strcat(linktarget, "../"); strfieldcat(linktarget, "../");
i++; i++;
} }
if (linktarget[0] == '\0') if (linktarget[0] == '\0')
strcpy(linktarget, "./"); strfieldcpy(linktarget, "./");
strcat(linktarget, &dev->name[tail]); strfieldcat(linktarget, &dev->name[tail]);
/* unlink existing files to ensure that our symlink is created */ /* unlink existing files to ensure that our symlink is created */
if (!fake && (lstat(filename, &stats) == 0)) { if (!fake && (lstat(filename, &stats) == 0)) {
@ -278,8 +278,8 @@ static struct sysfs_class_device *get_class_dev(char *device_name)
char dev_path[SYSFS_PATH_MAX]; char dev_path[SYSFS_PATH_MAX];
struct sysfs_class_device *class_dev = NULL; struct sysfs_class_device *class_dev = NULL;
strcpy(dev_path, sysfs_path); strfieldcpy(dev_path, sysfs_path);
strcat(dev_path, device_name); strfieldcat(dev_path, device_name);
dbg("looking at '%s'", dev_path); dbg("looking at '%s'", dev_path);
/* open up the sysfs class device for this thing... */ /* open up the sysfs class device for this thing... */
@ -304,9 +304,9 @@ static int sleep_for_dev(char *path)
int loop = SECONDS_TO_WAIT_FOR_DEV; int loop = SECONDS_TO_WAIT_FOR_DEV;
int retval; int retval;
strcpy(filename, sysfs_path); strfieldcpy(filename, sysfs_path);
strcat(filename, path); strfieldcat(filename, path);
strcat(filename, "/dev"); strfieldcat(filename, "/dev");
while (loop--) { while (loop--) {
struct stat buf; struct stat buf;

View file

@ -72,8 +72,8 @@ static int delete_node(struct udevice *dev)
int retval; int retval;
int i; int i;
strncpy(filename, udev_root, sizeof(filename)); strfieldcpy(filename, udev_root);
strncat(filename, dev->name, sizeof(filename)); strfieldcat(filename, dev->name);
info("removing device node '%s'", filename); info("removing device node '%s'", filename);
retval = unlink(filename); retval = unlink(filename);
@ -103,8 +103,8 @@ static int delete_node(struct udevice *dev)
if (linkname == NULL) if (linkname == NULL)
break; break;
strncpy(filename, udev_root, sizeof(filename)); strfieldcpy(filename, udev_root);
strncat(filename, linkname, sizeof(filename)); strfieldcat(filename, linkname);
dbg("unlinking symlink '%s'", filename); dbg("unlinking symlink '%s'", filename);
retval = unlink(filename); retval = unlink(filename);
@ -141,7 +141,7 @@ int udev_remove_device(char *path, char *subsystem)
temp = strrchr(path, '/'); temp = strrchr(path, '/');
if (temp == NULL) if (temp == NULL)
return -ENODEV; return -ENODEV;
strncpy(dev.name, &temp[1], sizeof(dev.name)); strfieldcpy(dev.name, &temp[1]);
} }
dbg("name is '%s'", dev.name); dbg("name is '%s'", dev.name);

6
udev.h
View file

@ -61,6 +61,12 @@ do { \
strncpy(to, from, sizeof(to)-1); \ strncpy(to, from, sizeof(to)-1); \
} while (0) } while (0)
#define strfieldcat(to, from) \
do { \
to[sizeof(to)-1] = '\0'; \
strncat(to, from, sizeof(to) - strlen(to) -1); \
} while (0)
extern int udev_add_device(char *path, char *subsystem, int fake); extern int udev_add_device(char *path, char *subsystem, int fake);
extern int udev_remove_device(char *path, char *subsystem); extern int udev_remove_device(char *path, char *subsystem);
extern void udev_init_config(void); extern void udev_init_config(void);

View file

@ -80,8 +80,8 @@ void sysbus_send_create(struct udevice *dev, const char *path)
if (sysbus_connection == NULL) if (sysbus_connection == NULL)
return; return;
strncpy(filename, udev_root, sizeof(filename)); strfieldcpy(filename, udev_root);
strncat(filename, dev->name, sizeof(filename)); strfieldcat(filename, dev->name);
/* object, interface, member */ /* object, interface, member */
message = dbus_message_new_signal("/org/kernel/udev/NodeMonitor", message = dbus_message_new_signal("/org/kernel/udev/NodeMonitor",
@ -114,8 +114,8 @@ void sysbus_send_remove(const char* name, const char *path)
if (sysbus_connection == NULL) if (sysbus_connection == NULL)
return; return;
strncpy(filename, udev_root, sizeof(filename)); strfieldcpy(filename, udev_root);
strncat(filename, name, sizeof(filename)); strfieldcat(filename, name);
/* object, interface, member */ /* object, interface, member */
message = dbus_message_new_signal("/org/kernel/udev/NodeMonitor", message = dbus_message_new_signal("/org/kernel/udev/NodeMonitor",

View file

@ -53,7 +53,7 @@ int udevdb_add_dev(const char *path, const struct udevice *dev)
return -ENODEV; return -ENODEV;
memset(keystr, 0, NAME_SIZE); memset(keystr, 0, NAME_SIZE);
strcpy(keystr, path); strfieldcpy(keystr, path);
key.dptr = keystr; key.dptr = keystr;
key.dsize = strlen(keystr) + 1; key.dsize = strlen(keystr) + 1;
@ -91,7 +91,7 @@ int udevdb_delete_dev(const char *path)
return -EINVAL; return -EINVAL;
memset(keystr, 0, sizeof(keystr)); memset(keystr, 0, sizeof(keystr));
strcpy(keystr, path); strfieldcpy(keystr, path);
key.dptr = keystr; key.dptr = keystr;
key.dsize = strlen(keystr) + 1; key.dsize = strlen(keystr) + 1;
@ -180,7 +180,7 @@ static int find_device_by_name(char *path, struct udevice *dev)
{ {
if (strncmp(dev->name, find_name, sizeof(dev->name)) == 0) { if (strncmp(dev->name, find_name, sizeof(dev->name)) == 0) {
memcpy(find_dev, dev, sizeof(*find_dev)); memcpy(find_dev, dev, sizeof(*find_dev));
strncpy(find_path, path, NAME_SIZE); strfieldcpy(find_path, path);
find_found = 1; find_found = 1;
/* stop search */ /* stop search */
return 1; return 1;

View file

@ -73,7 +73,7 @@ static int print_all_attributes(const char *path)
dlist_for_each_data(attributes, attr, struct sysfs_attribute) { dlist_for_each_data(attributes, attr, struct sysfs_attribute) {
if (attr->value != NULL) { if (attr->value != NULL) {
strncpy(value, attr->value, SYSFS_VALUE_MAX); strfieldcpy(value, attr->value);
len = strlen(value); len = strlen(value);
if (len == 0) if (len == 0)
continue; continue;
@ -306,8 +306,8 @@ static int process_options(void)
} else { } else {
if (path[0] != '/') { if (path[0] != '/') {
/* prepend '/' if missing */ /* prepend '/' if missing */
strcat(temp, "/"); strfieldcat(temp, "/");
strncat(temp, path, sizeof(path)); strfieldcat(temp, path);
pos = temp; pos = temp;
} else { } else {
pos = path; pos = path;
@ -343,7 +343,7 @@ print:
case NAME: case NAME:
if (root) if (root)
strfieldcpy(result, udev_root); strfieldcpy(result, udev_root);
strncat(result, dev.name, sizeof(result)); strfieldcat(result, dev.name);
break; break;
case SYMLINK: case SYMLINK:
@ -385,7 +385,7 @@ exit:
/* prepend sysfs mountpoint if not given */ /* prepend sysfs mountpoint if not given */
strfieldcpy(temp, path); strfieldcpy(temp, path);
strfieldcpy(path, sysfs_path); strfieldcpy(path, sysfs_path);
strncat(path, temp, sizeof(path)); strfieldcat(path, temp);
} }
print_device_chain(path); print_device_chain(path);
return 0; return 0;

View file

@ -82,9 +82,9 @@ static int build_hotplugmsg(struct hotplug_msg *msg, char *action,
memset(msg, 0x00, sizeof(*msg)); memset(msg, 0x00, sizeof(*msg));
strfieldcpy(msg->magic, UDEV_MAGIC); strfieldcpy(msg->magic, UDEV_MAGIC);
msg->seqnum = seqnum; msg->seqnum = seqnum;
strncpy(msg->action, action, 8); strfieldcpy(msg->action, action);
strncpy(msg->devpath, devpath, 128); strfieldcpy(msg->devpath, devpath);
strncpy(msg->subsystem, subsystem, 16); strfieldcpy(msg->subsystem, subsystem);
return sizeof(struct hotplug_msg); return sizeof(struct hotplug_msg);
} }