Debian Codegraph @

bugxml/readbugs.py

raw
1 #!/usr/bin/python3
2 # Copyright (c) 2016-2023 David Anderson.
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12 # * Neither the name of the example nor the
13 # names of its contributors may be used to endorse or promote products
14 # derived from this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY David Anderson ''AS IS'' AND ANY
17 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 # DISCLAIMED. IN NO EVENT SHALL David Anderson BE LIABLE FOR ANY
20 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
26 # OF SUCH DAMAGE.
27
28 import os
29 import sys
30 from datetime import datetime
31
32 # Happens to be the usual location of bugxml
33 # so the import works.
34 path="/home/davea/dwarf/code/bugxml"
35 mytz="PST"
36
37 sys.path.append(os.path.abspath(path))
38 import bugrecord
39
40
41 def ignore_this_line(d, inrecord):
42 if len(d) < 1:
43 if inrecord == "y":
44 return "n"
45 else:
46 return "y"
47 s = str(d)
48 if s[0] == "#":
49 return "y"
50 return "n"
51
52
53 def closeouttext(bugrec, intext, text, linecount):
54 if intext == "d":
55 bugrec.setdescription(text)
56 return
57 elif intext == "v":
58 bugrec.setvulnerability(text)
59 return
60 elif intext == "r":
61 bugrec.setreferences(text)
62 return
63 if intext == "":
64 return
65 print("bogus closeout line at line ", linecount)
66 sys.exit(1)
67
68
69 def readbugs(iname):
70 name = iname
71 if len(name) == 0:
72 name = "/home/davea/dwarf/code/bugxml/data.txt"
73 try:
74 file = open(name, "r")
75 except IOError as message:
76 print("failed to open ", name, message)
77
78 inrecord = "n"
79 linecount = 0
80 text = []
81 usedid = {}
82 intext = ""
83 bugrec = ""
84 buglist = []
85 while 1:
86 try:
87 rec = file.readline()
88 except EOFError:
89 break
90 if len(rec) < 1:
91 # eof
92 break
93 linecount += 1
94 if ignore_this_line(rec, inrecord) == "y":
95 continue
96 rec = rec.rstrip()
97 if inrecord == "n":
98 if len(rec) == 0:
99 continue
100 if rec.find(":") == -1:
101 print("bogus non-blank line at line ", linecount)
102 sys.exit(1)
103 if inrecord == "y" and len(rec) > 0:
104 # A multi line entry may have ":" in it.
105 if intext != "" and rec[0] == " ":
106 s3 = "".join(rec)
107 text += [s3]
108 continue
109 low = rec.find(":")
110 fldname = rec[0 : low + 1]
111 fldval = rec[low + 1 :]
112 if fldname == "id:":
113 if inrecord == "y":
114 print("bogus id: at line ", linecount)
115 sys.exit(1)
116 inrecord = "y"
117 f = fldval.strip()
118 if f in usedid:
119 print("Duplicate Key:", f, "Giving up.")
120 sys.exit(1)
121 usedid[f] = 1
122 s4 = "".join(fldval)
123 bugrec = bugrecord.bugrecord(s4)
124 elif fldname == "cve:":
125 closeouttext(bugrec, intext, text, linecount),
126 intext = ""
127 text = []
128 s4 = "".join(fldval)
129 bugrec.setcve(s4)
130 elif fldname == "fuzzer:":
131 closeouttext(bugrec, intext, text, linecount),
132 intext = ""
133 text = []
134 s4 = "".join(fldval)
135 bugrec.setfuzzer(s4)
136 elif fldname == "datereported:":
137 closeouttext(bugrec, intext, text, linecount),
138 intext = ""
139 text = []
140 s4 = "".join(fldval)
141 bugrec.setdatereported(s4)
142 elif fldname == "reportedby:":
143 closeouttext(bugrec, intext, text, linecount),
144 intext = ""
145 text = []
146 s4 = "".join(fldval)
147 bugrec.setreportedby(s4)
148 elif fldname == "vulnerability:":
149 closeouttext(bugrec, intext, text, linecount),
150 intext = "v"
151 text = []
152 if len(fldval) > 0:
153 s4 = "".join(fldval)
154 text = [s4]
155
156 elif fldname == "product:":
157 closeouttext(bugrec, intext, text, linecount),
158 intext = ""
159 text = []
160 s4 = "".join(fldval)
161 bugrec.setproduct(s4)
162 elif fldname == "description:":
163 closeouttext(bugrec, intext, text, linecount),
164 text = []
165 intext = "d"
166 if len(fldval) > 0:
167 s4 = "".join(fldval)
168 text = [s4]
169
170 elif fldname == "datefixed:":
171 closeouttext(bugrec, intext, text, linecount),
172 text = []
173 intext = ""
174 s4 = "".join(fldval)
175 bugrec.setdatefixed(s4)
176 elif fldname == "references:":
177 closeouttext(bugrec, intext, text, linecount),
178 text = []
179 intext = "r"
180 if len(fldval) > 0:
181 s4 = "".join(fldval)
182 text = [s4]
183 elif fldname == "gitfixid:":
184 closeouttext(bugrec, intext, text, linecount),
185 text = []
186 intext = ""
187 s4 = "".join(fldval)
188 bugrec.setgitfixid(s4)
189 elif fldname == "tarrelease:":
190 closeouttext(bugrec, intext, text, linecount),
191 text = []
192 intext = ""
193 s4 = "".join(fldval)
194 bugrec.settarrelease(s4)
195 elif fldname == "endrec:":
196 closeouttext(bugrec, intext, text, linecount),
197 text = []
198 if inrecord == "n":
199 print("bogus endrec: at line ", linecount)
200 sys.exit(1)
201 buglist += [bugrec]
202 inrecord = "n"
203 text = []
204 intext = ""
205 inrecord = "n"
206 file.close()
207 return buglist
208
209
210 def sort_by_id(myl):
211 """Sort the list of objects by name."""
212 auxiliary = [(x._id, x) for x in myl]
213 auxiliary.sort()
214 return [x[1] for x in auxiliary]
215
216
217 def write_line(file, l):
218 file.write(l + "\n")
219
220
221 def write_all_lines(file, txt):
222 for t in txt:
223 write_line(file, t)
224
225 headtext = [
226 '<!DOCTYPE html>',
227 '<html lang="en">',
228 '<head>',
229 ' <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" >',
230 '<title>Libdwarf Vulnerabilities</title>',
231 ' <META NAME="description" CONTENT="Libdwarf Vulnerabilities">',
232 ' <META NAME="keywords" CONTENT="DWARF, DWARF2, DWARF3, DWARF4,DWARF5">',
233 '</head>',
234 '</body>',
235 '<h1 id=top>Libdwarf Vulnerabilities</h1>',
236 '<p>',
237 'This page provides',
238 'documentation of known vulnerabilities',
239 'in libdwarf.',
240 'We are concerned here with',
241 'cases where corrupt (by accident or intention)',
242 'DWARF can cause the library to get',
243 'a fault (crash) which',
244 'could expose the calling program',
245 'to interception by malefactors.',
246 'Dates (where known) are in ISO extended date format.',
247 '</p>',
248 '<p>',
249 'Some of the bugs reported here have a CVE assigned,',
250 'for example CVE-2017-9052.',
251 'These are reported on cve.org (or possibly the earlier',
252 'cve.mitre.org).',
253 'Search with "libdwarf" on cve.org for a list.',
254 '</p>',
255 '<p>',
256 'Git reference path names refer to object files',
257 'in the libdwarf regression test base.',
258 'The test files can be retrieved via anonymous access:',
259 '<br>',
260 '"git clone https://github.org/davea42/libdwarf-regressiontests"',
261 '</p>',
262 '<p>',
263 'A few bugs refer to https://bugzilla.redhat.com',
264 'bug system entries',
265 'and/or https://bugs.chromium.org',
266 'in addition to showing the names of test files in',
267 'the regression test base.',
268 '</p>',
269 '<H2 id=vulnerabilities>Vulnerabilities</H2>',
270 '<p>',
271 'Vulnerabilities <a href="./dwarfbug.html">listed newest-first</a>.',
272 '<br>',
273 'Vulnerabilities <a href="./dwarfbuglohi.html">listed oldest-first</a>.',
274 '</p>',
275 ]
276
277 def writeheader(file,headname):
278 dt = datetime.today()
279 for l in headtext:
280 write_line(file,l)
281 hl='<H2>%s</H2>'%(headname)
282 write_line(file,hl)
283 #tm='</p>as of %s %s</p>'%(dt.strftime("%Y-%m-%d %H:%M "),mytz)
284 tm='</p>as of %s</p>'%(dt.strftime("%B %Y"))
285 write_line(file,tm)
286
287 def generatehtml(list2,headname, name):
288 try:
289 file = open(name, "w")
290 except IOError as message:
291 print("failed to open ", name, message)
292 sys.exit(1)
293 writeheader(file,headname)
294 write_line(file,"<p> Record count: %d </p>"%(len(list2)))
295 for i,b in enumerate(list2):
296 num = int(i) +1
297 txt = b.generate_html(num)
298 write_all_lines(file, txt)
299 write_line(file,'<p> <a href="#top">[top]</a> </p>')
300 write_line(file, "</body>")
301 write_line(file, "</html>")
302 file.close()
303
304
305 def generatexml(list2, name):
306 try:
307 file = open(name, "w")
308 except IOError as message:
309 print("failed to open ", name, message)
310 sys.exit(1)
311 t = '<?xml version="1.0" encoding="us-ascii"?>'
312 write_line(file, t)
313 write_line(file, "<dwarfbug>")
314 for b in list2:
315 txt = b.generate_xml()
316 write_all_lines(file, txt)
317 write_line(file, "</dwarfbug>")
318 file.close()
319
320
321 if __name__ == "__main__":
322 list = readbugs("")
323 list2 = sort_by_id(list)
324 list2.reverse()
325 #for b in list2:
326 # b.printbug()
327
328 targlo="LibDwarf Vulnerabilities Newest First"
329 generatehtml(list2,targlo,"dwarfbug.html")
330 generatexml(list2,"dwarfbug.xml")
331
332 list2.reverse()
333 targhi="LibDwarf Vulnerabilities Oldest First"
334 generatehtml(list2,targhi,"dwarfbuglohi.html")