Skip to content

Commit cd1202f

Browse files
katexochenFreax13
andcommitted
token: sort paths for reproducible extract
There is no defined order in which readdir will return the entries of a directory. In practice, order can depend on inode number or similar. If we run p11-kit on different files systems with similar directory structure but different inode order the output of extract can change. To get a stable and reproducible output, sort the paths returned by readdir before extracting. Co-authored-by: Tom Dohrmann <erbse.13@gmx.de> Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
1 parent 5332245 commit cd1202f

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

trust/token.c

+27-2
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,23 @@ loader_load_if_file (p11_token *token,
252252
return 0;
253253
}
254254

255+
static int
256+
compar_strings (const void *one,
257+
const void *two)
258+
{
259+
char **p1 = (char **)one;
260+
char **p2 = (char **)two;
261+
return strcmp (*p1, *p2);
262+
}
263+
264+
255265
static int
256266
loader_load_directory (p11_token *token,
257267
const char *directory,
258268
p11_dict *present)
259269
{
260270
p11_dictiter iter;
271+
p11_array *paths;
261272
struct dirent *dp;
262273
char *path;
263274
int total = 0;
@@ -272,11 +283,25 @@ loader_load_directory (p11_token *token,
272283
return 0;
273284
}
274285

286+
paths = p11_array_new (NULL);
287+
275288
while ((dp = readdir (dir)) != NULL) {
276289
path = p11_path_build (directory, dp->d_name, NULL);
277290
return_val_if_fail (path != NULL, -1);
278291

279-
ret = loader_load_if_file (token, path);
292+
if (!p11_array_push (paths, path)) {
293+
free (path);
294+
return -1;
295+
}
296+
}
297+
298+
closedir(dir);
299+
300+
qsort(paths->elem, paths->num, sizeof(char *), compar_strings);
301+
302+
for (int i = 0; i < paths->num; i++) {
303+
path = paths->elem[i];
304+
ret = loader_load_if_file(token, path);
280305
if (ret >= 0) {
281306
if (ret <= INT_MAX - total) {
282307
total += ret;
@@ -291,7 +316,7 @@ loader_load_directory (p11_token *token,
291316
free (path);
292317
}
293318

294-
closedir (dir);
319+
p11_array_free (paths);
295320

296321
/* All other files that were present, not here now */
297322
p11_dict_iterate (present, &iter);

0 commit comments

Comments
 (0)