Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1 | By default qemu builds a complete list of directories within the user |
| 2 | emulation sysroot (-L option). The OE sysroot directory is large and |
| 3 | this is confusing, for example it indexes all pkgdata. In particular this |
| 4 | confuses strace of qemu binaries with tons of irrelevant paths. |
| 5 | |
| 6 | This patch stops the code indexing up front and instead only indexes |
| 7 | things if/as/when it needs to. This drastically reduces the files it |
| 8 | reads and reduces memory usage and cleans up strace. |
| 9 | |
| 10 | It would also avoid the infinite directory traversal bug in [YOCTO #6996] |
| 11 | although the code could still be vulnerable if it parsed those specific |
| 12 | paths. |
| 13 | |
| 14 | RP |
| 15 | 2016/3/9 |
| 16 | Upstream-Status: Pending |
| 17 | |
| 18 | Index: qemu-2.5.0/util/path.c |
| 19 | =================================================================== |
| 20 | --- qemu-2.5.0.orig/util/path.c |
| 21 | +++ qemu-2.5.0/util/path.c |
| 22 | @@ -19,6 +19,7 @@ struct pathelem |
| 23 | char *name; |
| 24 | /* Full path name, eg. /usr/gnemul/x86-linux/lib. */ |
| 25 | char *pathname; |
| 26 | + int populated_entries; |
| 27 | struct pathelem *parent; |
| 28 | /* Children */ |
| 29 | unsigned int num_entries; |
| 30 | @@ -49,6 +50,7 @@ static struct pathelem *new_entry(const |
| 31 | new->name = g_strdup(name); |
| 32 | new->pathname = g_strdup_printf("%s/%s", root, name); |
| 33 | new->num_entries = 0; |
| 34 | + new->populated_entries = 0; |
| 35 | return new; |
| 36 | } |
| 37 | |
| 38 | @@ -57,15 +59,16 @@ static struct pathelem *new_entry(const |
| 39 | /* Not all systems provide this feature */ |
| 40 | #if defined(DT_DIR) && defined(DT_UNKNOWN) && defined(DT_LNK) |
| 41 | # define dirent_type(dirent) ((dirent)->d_type) |
| 42 | -# define is_dir_maybe(type) \ |
| 43 | - ((type) == DT_DIR || (type) == DT_UNKNOWN || (type) == DT_LNK) |
| 44 | +# define is_not_dir(type) \ |
| 45 | + ((type) != DT_DIR && (type) != DT_UNKNOWN && (type) != DT_LNK) |
| 46 | #else |
| 47 | # define dirent_type(dirent) (1) |
| 48 | -# define is_dir_maybe(type) (type) |
| 49 | +# define is_not_dir(type) (0) |
| 50 | #endif |
| 51 | |
| 52 | static struct pathelem *add_dir_maybe(struct pathelem *path) |
| 53 | { |
| 54 | + unsigned int i; |
| 55 | DIR *dir; |
| 56 | |
| 57 | if ((dir = opendir(path->pathname)) != NULL) { |
| 58 | @@ -78,6 +81,11 @@ static struct pathelem *add_dir_maybe(st |
| 59 | } |
| 60 | closedir(dir); |
| 61 | } |
| 62 | + |
| 63 | + for (i = 0; i < path->num_entries; i++) |
| 64 | + (path->entries[i])->parent = path; |
| 65 | + |
| 66 | + path->populated_entries = 1; |
| 67 | return path; |
| 68 | } |
| 69 | |
| 70 | @@ -93,26 +101,16 @@ static struct pathelem *add_entry(struct |
| 71 | e = &root->entries[root->num_entries-1]; |
| 72 | |
| 73 | *e = new_entry(root->pathname, root, name); |
| 74 | - if (is_dir_maybe(type)) { |
| 75 | - *e = add_dir_maybe(*e); |
| 76 | + if (is_not_dir(type)) { |
| 77 | + (*e)->populated_entries = 1; |
| 78 | } |
| 79 | |
| 80 | return root; |
| 81 | } |
| 82 | |
| 83 | -/* This needs to be done after tree is stabilized (ie. no more reallocs!). */ |
| 84 | -static void set_parents(struct pathelem *child, struct pathelem *parent) |
| 85 | -{ |
| 86 | - unsigned int i; |
| 87 | - |
| 88 | - child->parent = parent; |
| 89 | - for (i = 0; i < child->num_entries; i++) |
| 90 | - set_parents(child->entries[i], child); |
| 91 | -} |
| 92 | - |
| 93 | /* FIXME: Doesn't handle DIR/.. where DIR is not in emulated dir. */ |
| 94 | static const char * |
| 95 | -follow_path(const struct pathelem *cursor, const char *name) |
| 96 | +follow_path(struct pathelem *cursor, struct pathelem **source, const char *name) |
| 97 | { |
| 98 | unsigned int i, namelen; |
| 99 | |
| 100 | @@ -123,14 +121,18 @@ follow_path(const struct pathelem *curso |
| 101 | return cursor->pathname; |
| 102 | |
| 103 | if (strneq(name, namelen, "..")) |
| 104 | - return follow_path(cursor->parent, name + namelen); |
| 105 | + return follow_path(cursor->parent, &cursor->parent, name + namelen); |
| 106 | |
| 107 | if (strneq(name, namelen, ".")) |
| 108 | - return follow_path(cursor, name + namelen); |
| 109 | + return follow_path(cursor, source, name + namelen); |
| 110 | + |
| 111 | + if (!cursor->populated_entries) |
| 112 | + *source = add_dir_maybe(cursor); |
| 113 | + cursor = *source; |
| 114 | |
| 115 | for (i = 0; i < cursor->num_entries; i++) |
| 116 | if (strneq(name, namelen, cursor->entries[i]->name)) |
| 117 | - return follow_path(cursor->entries[i], name + namelen); |
| 118 | + return follow_path(cursor->entries[i], &cursor->entries[i], name + namelen); |
| 119 | |
| 120 | /* Not found */ |
| 121 | return NULL; |
| 122 | @@ -164,8 +166,6 @@ void init_paths(const char *prefix) |
| 123 | g_free(base->name); |
| 124 | g_free(base); |
| 125 | base = NULL; |
| 126 | - } else { |
| 127 | - set_parents(base, base); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | @@ -177,5 +177,5 @@ const char *path(const char *name) |
| 132 | if (!base || !name || name[0] != '/') |
| 133 | return name; |
| 134 | |
| 135 | - return follow_path(base, name) ?: name; |
| 136 | + return follow_path(base, &base, name) ?: name; |
| 137 | } |