Skip to content

Commit a57afab

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 e6c6972 commit a57afab

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

trust/token.c

+25-1
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,28 @@ 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+
const char **p1 = (const char **)one;
260+
const char **p2 = (const 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;
264275
int ret;
276+
int i;
265277
DIR *dir;
266278

267279
/* First we load all the modules */
@@ -272,10 +284,22 @@ loader_load_directory (p11_token *token,
272284
return 0;
273285
}
274286

287+
paths = p11_array_new (NULL);
288+
return_val_if_fail (paths != NULL, -1);
289+
275290
while ((dp = readdir (dir)) != NULL) {
276291
path = p11_path_build (directory, dp->d_name, NULL);
277292
return_val_if_fail (path != NULL, -1);
293+
294+
return_val_if_fail (p11_array_push (paths, path), -1);
295+
}
296+
297+
closedir (dir);
298+
299+
qsort (paths->elem, paths->num, sizeof (char *), compar_strings);
278300

301+
for (i = 0; i < paths->num; i++) {
302+
path = paths->elem[i];
279303
ret = loader_load_if_file (token, path);
280304
if (ret >= 0) {
281305
if (ret <= INT_MAX - total) {
@@ -291,7 +315,7 @@ loader_load_directory (p11_token *token,
291315
free (path);
292316
}
293317

294-
closedir (dir);
318+
p11_array_free (paths);
295319

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

0 commit comments

Comments
 (0)