chiark / gitweb /
udev-rules.c: parse_file() - fix possible buffer overflow
[elogind.git] / udev / udev-rules.c
index 174edfd9c4924b7ee33d119bc2552cd9aa2f958b..da08bc11f69668a807e675743757da033fc76dee 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2009 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2003-2009 Kay Sievers <kay.sievers@vrfy.org>
  * Copyright (C) 2008 Alan Jenkins <alan-jenkins@tuffmail.co.uk>
  *
  * This program is free software: you can redistribute it and/or modify
@@ -27,6 +27,7 @@
 #include <errno.h>
 #include <dirent.h>
 #include <fnmatch.h>
+#include <time.h>
 
 #include "udev.h"
 
@@ -270,7 +271,7 @@ static const char *token_str(enum token_type type)
                [TK_M_PROGRAM] =                "M PROGRAM",
                [TK_M_IMPORT_FILE] =            "M IMPORT_FILE",
                [TK_M_IMPORT_PROG] =            "M IMPORT_PROG",
-               [TK_M_IMPORT_PARENT] =          "M MPORT_PARENT",
+               [TK_M_IMPORT_PARENT] =          "M IMPORT_PARENT",
                [TK_M_RESULT] =                 "M RESULT",
                [TK_M_MAX] =                    "M MAX",
 
@@ -718,8 +719,9 @@ static int import_property_from_string(struct udev_device *dev, char *line)
                struct udev_list_entry *entry;
 
                entry = udev_device_add_property(dev, key, val);
-               /* store in db */
-               udev_list_entry_set_flag(entry, 1);
+               /* store in db, skip private keys */
+               if (key[0] != '.')
+                       udev_list_entry_set_flag(entry, 1);
        }
        return 0;
 }
@@ -785,8 +787,9 @@ static int import_parent_into_properties(struct udev_device *dev, const char *fi
 
                        dbg(udev, "import key '%s=%s'\n", key, val);
                        entry = udev_device_add_property(dev, key, val);
-                       /* store in db */
-                       udev_list_entry_set_flag(entry, 1);
+                       /* store in db, skip private keys */
+                       if (key[0] != '.')
+                               udev_list_entry_set_flag(entry, 1);
                }
        }
        return 0;
@@ -812,6 +815,8 @@ static int wait_for_file(struct udev_device *dev, const char *file, int timeout)
 
        dbg(udev, "will wait %i sec for '%s'\n", timeout, file);
        while (--loop) {
+               const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND };
+
                /* lookup file */
                if (stat(file, &stats) == 0) {
                        info(udev, "file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
@@ -823,7 +828,7 @@ static int wait_for_file(struct udev_device *dev, const char *file, int timeout)
                        return -2;
                }
                info(udev, "wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
-               usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
+               nanosleep(&duration, NULL);
        }
        info(udev, "waiting for '%s' failed\n", file);
        return -1;
@@ -873,7 +878,7 @@ static int get_key(struct udev *udev, char **line, char **key, enum operation_ty
        char *temp;
 
        linepos = *line;
-       if (linepos == NULL && linepos[0] == '\0')
+       if (linepos == NULL || linepos[0] == '\0')
                return -1;
 
        /* skip whitespace */
@@ -1377,7 +1382,7 @@ static int add_rule(struct udev_rules *rules, char *line,
                        int flag = 0;
 
                        attr = get_key_attribute(rules->udev, key + sizeof("RUN")-1);
-                       if (attr != NULL && strstr(attr, "ignore_error"))
+                       if (attr != NULL && strstr(attr, "fail_event_on_error"))
                                flag = 1;
                        rule_add_key(&rule_tmp, TK_A_RUN, op, value, &flag);
                        continue;
@@ -1404,6 +1409,9 @@ static int add_rule(struct udev_rules *rules, char *line,
                        } else {
                                if (value[0] == '\0')
                                        info(rules->udev, "name empty, node creation suppressed\n");
+                               else if (strcmp(value, "%k") == 0)
+                                       err(rules->udev, "NAME=\"%%k\" is superfluous and breaks "
+                                           "kernel supplied names, please remove it from %s:%u\n", filename, lineno);
                                rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
                                attr = get_key_attribute(rules->udev, key + sizeof("NAME")-1);
                                if (attr != NULL) {
@@ -1506,7 +1514,7 @@ static int add_rule(struct udev_rules *rules, char *line,
                                int tout = atoi(&pos[strlen("event_timeout=")]);
 
                                rule_add_key(&rule_tmp, TK_A_EVENT_TIMEOUT, 0, NULL, &tout);
-                               dbg(rules->udev, "event timout=%i\n", tout);
+                               dbg(rules->udev, "event timeout=%i\n", tout);
                        }
                        pos = strstr(value, "string_escape=");
                        if (pos != NULL) {
@@ -1560,7 +1568,6 @@ invalid:
 static int parse_file(struct udev_rules *rules, const char *filename, unsigned short filename_off)
 {
        FILE *f;
-       unsigned int first_token;
        char line[UTIL_LINE_SIZE];
        int line_nr = 0;
        unsigned int i;
@@ -1570,10 +1577,7 @@ static int parse_file(struct udev_rules *rules, const char *filename, unsigned s
        f = fopen(filename, "r");
        if (f == NULL)
                return -1;
-
-       first_token = rules->token_cur;
-
-       while(fgets(line, sizeof(line), f) != NULL) {
+       while (fgets(line, sizeof(line), f) != NULL) {
                char *key;
                size_t len;
 
@@ -1595,6 +1599,8 @@ static int parse_file(struct udev_rules *rules, const char *filename, unsigned s
                while (line[len-2] == '\\') {
                        if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
                                break;
+                       if (strlen(&line[len-2]) < 2)
+                               break;
                        line_nr++;
                        len = strlen(line);
                }
@@ -1608,7 +1614,7 @@ static int parse_file(struct udev_rules *rules, const char *filename, unsigned s
        fclose(f);
 
        /* link GOTOs to LABEL rules in this file to be able to fast-forward */
-       for (i = first_token+1; i < rules->token_cur; i++) {
+       for (i = rules->token_cur+1; i < rules->token_cur; i++) {
                if (rules->tokens[i].type == TK_A_GOTO) {
                        char *label = &rules->buf[rules->tokens[i].key.value_off];
                        unsigned int j;
@@ -1793,7 +1799,7 @@ struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
                if (stat(filename, &statbuf) == 0 && statbuf.st_size > 0)
                        parse_file(rules, filename, filename_off);
                else
-                       info(udev, "can not read '%s'\n", filename);
+                       err(udev, "can not read '%s'\n", filename);
                udev_list_entry_delete(file_loop);
        }
 
@@ -2374,8 +2380,9 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event
 
                                        udev_event_apply_format(event, value, temp_value, sizeof(temp_value));
                                        entry = udev_device_add_property(event->dev, name, temp_value);
-                                       /* store in db */
-                                       udev_list_entry_set_flag(entry, 1);
+                                       /* store in db, skip private keys */
+                                       if (name[0] != '.')
+                                               udev_list_entry_set_flag(entry, 1);
                                } else {
                                        udev_device_add_property(event->dev, name, NULL);
                                }