Skip to content

Commit fc9bbcd

Browse files
committed
The parser cache is now always handled in a per-user fashion.
This avoids issues with people providing malciious caches in a multi-user system. Originally reported as: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=735263 A CVE number has been requested
1 parent 0a51c25 commit fc9bbcd

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

rply/parsergenerator.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import hashlib
33
import json
44
import random
5+
import stat
56
import string
67
import sys
78
import tempfile
@@ -124,17 +125,23 @@ def build(self):
124125

125126
cache_file = os.path.join(
126127
tempfile.gettempdir(),
127-
"rply-%s-%s-%s.json" % (self.VERSION, self.cache_id, self.compute_grammar_hash(g))
128+
"rply-%s-%s-%s-%s.json" % (self.VERSION, os.getuid(), self.cache_id, self.compute_grammar_hash(g))
128129
)
129130
table = None
130131
if os.path.exists(cache_file):
131132
with open(cache_file) as f:
132133
data = json.load(f)
133-
if self.data_is_valid(g, data):
134-
table = LRTable.from_cache(g, data)
134+
stat_result = os.fstat(f.fileno())
135+
if (
136+
stat_result.st_uid == os.getuid() and
137+
stat.S_IMODE(stat_result.st_mode) == 0o0600
138+
):
139+
if self.data_is_valid(g, data):
140+
table = LRTable.from_cache(g, data)
135141
if table is None:
136142
table = LRTable.from_grammar(g)
137-
with open(cache_file, "w") as f:
143+
fd = os.open(cache_file, os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o0600)
144+
with os.fdopen(fd, "w") as f:
138145
json.dump(self.serialize_table(table), f)
139146
if table.sr_conflicts:
140147
warnings.warn(

0 commit comments

Comments
 (0)