Skip to content

Commit 86f4285

Browse files
authored
Better randomization of stats filenames. (GH-30145)
1 parent 30322c4 commit 86f4285

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Python/specialize.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,20 @@ _Py_PrintSpecializationStats(int to_file)
167167
# else
168168
const char *dirname = "/tmp/py_stats/";
169169
# endif
170-
char buf[48];
171-
sprintf(buf, "%s%u_%u.txt", dirname, (unsigned)clock(), (unsigned)rand());
170+
/* Use random 160 bit number as file name,
171+
* to avoid both accidental collisions and
172+
* symlink attacks. */
173+
unsigned char rand[20];
174+
char hex_name[41];
175+
_PyOS_URandomNonblock(rand, 20);
176+
for (int i = 0; i < 20; i++) {
177+
hex_name[2*i] = "0123456789abcdef"[rand[i]&15];
178+
hex_name[2*i+1] = "0123456789abcdef"[(rand[i]>>4)&15];
179+
}
180+
hex_name[40] = '\0';
181+
char buf[64];
182+
assert(strlen(dirname) + 40 + strlen(".txt") < 64);
183+
sprintf(buf, "%s%s.txt", dirname, hex_name);
172184
FILE *fout = fopen(buf, "w");
173185
if (fout) {
174186
out = fout;

0 commit comments

Comments
 (0)