udev: always use last 11 chars for hash string

This makes the last 11 chars are always preserved for hashed string.
So, it is hard to generate a path which conflicts to another path.

Fixes an issue demonstrated in the previous commit.
This commit is contained in:
Yu Watanabe
2021-06-04 22:28:09 +09:00
parent 0192864da7
commit 2bb0227165
2 changed files with 13 additions and 6 deletions

View File

@@ -35,8 +35,16 @@ static void test_udev_node_escape_path(void) {
test_udev_node_escape_path_one(a, b);
strcpy(a + sizeof(a) - 12 - 9, "N3YhcCqFeID");
strcpy(b + sizeof(b) - 12, "L1oK9iKWdmi");
test_udev_node_escape_path_one(a, b);
test_udev_node_escape_path_one(a, b); /* <-- Ouch. This will pass. Needs to be fixed. */
strcpy(a + sizeof(a) - 12 - 9, "a");
strcpy(b + sizeof(b) - 12, "A7oaHBRuuZq");
test_udev_node_escape_path_one(a, b);
a[sizeof(a) - 12 - 9] = '\0';
b[sizeof(a) - 12] = '\0';
test_udev_node_escape_path_one(a, b);
}
int main(int argc, char *argv[]) {

View File

@@ -219,20 +219,21 @@ size_t udev_node_escape_path(const char *src, char *dest, size_t size) {
assert(src);
assert(dest);
assert(size >= 12);
for (i = 0, j = 0; src[i] != '\0'; i++) {
if (src[i] == '/') {
if (j+4 >= size)
if (j+4 >= size - 12 + 1)
goto toolong;
memcpy(&dest[j], "\\x2f", 4);
j += 4;
} else if (src[i] == '\\') {
if (j+4 >= size)
if (j+4 >= size - 12 + 1)
goto toolong;
memcpy(&dest[j], "\\x5c", 4);
j += 4;
} else {
if (j+1 >= size)
if (j+1 >= size - 12 + 1)
goto toolong;
dest[j] = src[i];
j++;
@@ -247,8 +248,6 @@ toolong:
h = siphash24_string(src, UDEV_NODE_HASH_KEY.bytes);
assert(size >= 12);
for (unsigned k = 0; k <= 10; k++)
dest[size - k - 2] = urlsafe_base64char((h >> (k * 6)) & 63);