|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright (C) 2011-2020 Intel Corporation. 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 |
| 7 | +# are met: |
| 8 | +# |
| 9 | +# * Redistributions of source code must retain the above copyright |
| 10 | +# notice, this list of conditions and the following disclaimer. |
| 11 | +# * Redistributions in binary form must reproduce the above copyright |
| 12 | +# notice, this list of conditions and the following disclaimer in |
| 13 | +# the documentation and/or other materials provided with the |
| 14 | +# distribution. |
| 15 | +# * Neither the name of Intel Corporation nor the names of its |
| 16 | +# contributors may be used to endorse or promote products derived |
| 17 | +# from this software without specific prior written permission. |
| 18 | +# |
| 19 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | +# |
| 31 | +# |
| 32 | + |
| 33 | +__version__ = '1.0.1' |
| 34 | +import sys |
| 35 | +import os |
| 36 | +import re |
| 37 | +import shutil |
| 38 | +import argparse |
| 39 | + |
| 40 | +LOCK = 'lock' |
| 41 | +REP = 'rep[a-z]*' |
| 42 | +REX = 'rex(?:\.[a-zA-Z]+)?' |
| 43 | +SCALAR = '(?:(?:[+-]\s*)?(?:[0-9][0-9a-fA-F]*|0x[0-9a-fA-F]+))' |
| 44 | +IMMEDIATE = '(?:%s[hb]?)' %(SCALAR) |
| 45 | +REG = '(?:[a-zA-Z][a-zA-Z0-9]*)' |
| 46 | +SYM = '(?:[_a-zA-Z][_a-zA-Z0-9]*(?:@[0-9a-zA-Z]+)?)' |
| 47 | +LABEL = '(?:[._a-zA-Z0-9]+)' |
| 48 | +SEP = '(?:(?:^|:)\s*)' |
| 49 | +PFX = '(?:%s\s+)?' %(REX) |
| 50 | +CONST = '(?:(?:%s|%s|%s)(?:\s*[/*+-]\s*(?:%s|%s|%s))*)' %(SYM, SCALAR, LABEL, SYM, SCALAR, LABEL) |
| 51 | +OFFSET = '(?:%s|%s|%s\s*:\s*(?:%s|%s|))' %(CONST, SYM, REG, CONST, SYM) |
| 52 | +MEMORYOP = '(?:\[*(?:[a-zA-Z]+\s+)*(?:%s\s*:\s*%s?|(?:%s\s*)?\[[^]]+\]\]*))' %(REG, CONST, OFFSET) |
| 53 | +ANYOP = '(?:%s|%s|%s|%s|%s)' %(MEMORYOP, IMMEDIATE, REG, SYM, LABEL) |
| 54 | +MEMORYOP = '(?:%s|(?:[a-zA-Z]+\s+(?:ptr|PTR)\s+%s))' %(MEMORYOP, ANYOP) |
| 55 | +MEMORYSRC = '(?:%s\s*,\s*)+%s(?:\s*,\s*%s)*' %(ANYOP, MEMORYOP, ANYOP) |
| 56 | +MEMORYANY = '(?:%s\s*,\s*)*%s(?:\s*,\s*%s)*' %(ANYOP, MEMORYOP, ANYOP) |
| 57 | +ATTSTAR = '' |
| 58 | +GPR = '(?:rax|rcx|rdx|rbx|rdi|rsi|rbp|rsp|r8|r9|r10|r11|r12|r13|r14|r15|RAX|RCX|RDX|RBX|RDI|RSI|RBP|RSP|R8|R9|R10|R11|R12|R13|R14|R15)' |
| 59 | + |
| 60 | +LFENCE = [ |
| 61 | + '(?:%s%smov(?:[a-rt-z][a-z0-9]*)?\s+%s)' %(SEP, PFX, MEMORYSRC), |
| 62 | + '(?:%s%s(?:vpmask|vmask|mask|c|v|p|vp)mov[a-z0-9]*\s+%s)' %(SEP, PFX, MEMORYSRC), |
| 63 | + '(?:%s%spop[bswlqt]?\s+(?:%s|%s))' %(SEP, PFX, MEMORYOP, REG), |
| 64 | + '(?:%s%spopad?\s+%s\s*)' %(SEP, PFX, REG), |
| 65 | + '(?:%s%s(?:%s\s+)?xchg[a-z]*\s+%s)' %(SEP, PFX, LOCK, MEMORYANY), |
| 66 | + '(?:%s%s(?:%s\s+)?(?:x|p|vp|ph|h|pm|vpm|)add[a-z]*\s+%s)' %(SEP, PFX, LOCK, MEMORYANY), |
| 67 | + '(?:%s%s(?:%s\s+)?(?:p|vp|ph|h|)sub[a-z]*\s+%s)' %(SEP, PFX, LOCK, MEMORYANY), |
| 68 | + '(?:%s%s(?:%s\s+)?ad[co]x?[a-z]*\s+%s)' %(SEP, PFX, LOCK, MEMORYANY), |
| 69 | + '(?:%s%s(?:%s\s+)?sbb[a-z]*\s+%s)' %(SEP, PFX, LOCK, MEMORYANY), |
| 70 | + '(?:%s%s(?:%s\s+)?v?p?cmp(?:[a-rt-z][a-z0-9]*)?\s+%s)' %(SEP, PFX, LOCK, MEMORYANY), |
| 71 | + '(?:%s%s(?:%s\s+)?inc[a-z]*\s+%s)' %(SEP, PFX, LOCK, MEMORYANY), |
| 72 | + '(?:%s%s(?:%s\s+)?dec[a-z]*\s+%s)' %(SEP, PFX, LOCK, MEMORYANY), |
| 73 | + '(?:%s%s(?:%s\s+)?not[a-z]*\s+%s)' %(SEP, PFX, LOCK, MEMORYANY), |
| 74 | + '(?:%s%s(?:%s\s+)?neg[a-z]*\s+%s)' %(SEP, PFX, LOCK, MEMORYANY), |
| 75 | + '(?:%s%s(?:i|v|p|vp|)mul[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 76 | + '(?:%s%s(?:i|v|p|vp|)div[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 77 | + '(?:%s%spopcnt[a-z]*\s+%s)' %(SEP, PFX, MEMORYSRC), |
| 78 | + '(?:%s%scrc32[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 79 | + '(?:%s%s(?:%s\s+)?v?p?and[a-z]*\s+%s)' %(SEP, PFX, LOCK, MEMORYANY), |
| 80 | + '(?:%s%s(?:%s\s+)?v?p?or[a-z]*\s+%s)' %(SEP, PFX, LOCK, MEMORYANY), |
| 81 | + '(?:%s%s(?:%s\s+)?v?p?xor[a-z]*\s+%s)' %(SEP, PFX, LOCK, MEMORYANY), |
| 82 | + '(?:%s%sv?p?test[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 83 | + '(?:%s%ss[ah][lr][a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 84 | + '(?:%s%ssar[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 85 | + '(?:%s%s(?:vp|)ro(?:r|l)[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 86 | + '(?:%s%src(?:r|l)[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 87 | + '(?:%s%s(?:%s\s+)?bt[a-z]*\s+%s)' %(SEP, PFX, LOCK, MEMORYANY), |
| 88 | + '(?:%s%sbs[fr][a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 89 | + '(?:%s%s(?:vp|)[lt]zcnt[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 90 | + '(?:%s%sblsi[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 91 | + '(?:%s%sblsmsk[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 92 | + '(?:%s%sblsr[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 93 | + '(?:%s%sbextr[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 94 | + '(?:%s%sbzhi[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 95 | + '(?:%s%spdep[a-z]*\s+%s)' %(SEP, PFX, MEMORYSRC), |
| 96 | + '(?:%s%spext[a-z]*\s+%s)' %(SEP, PFX, MEMORYSRC), |
| 97 | + '(?:%s%s(?:%s\s+)?lods[a-z]*(?:\s+%s|\s*(?:#|$)))' %(SEP, PFX, REP, MEMORYSRC), |
| 98 | + '(?:%s%s(?:%s\s+)?scas[a-z]*(?:\s+%s|\s*(?:#|$)))' %(SEP, PFX, REP, MEMORYSRC), |
| 99 | + '(?:%s%s(?:%s\s+)?outs[a-z]*(?:\s+%s|\s*(?:#|$)))' %(SEP, PFX, REP, MEMORYSRC), |
| 100 | + '(?:%s%s(?:%s\s+)?cmps[a-z]*(?:\s+%s|\s*(?:#|$)))' %(SEP, PFX, REP, MEMORYSRC), |
| 101 | + '(?:%s%s(?:%s\s+)?movs[a-z]*(?:\s+%s|\s*(?:#|$)))' %(SEP, PFX, REP, MEMORYSRC), |
| 102 | + '(?:%s%slddqu\s+%s)' %(SEP, PFX, MEMORYSRC), |
| 103 | + '(?:%s%sv?pack[a-z]*\s+%s)' %(SEP, PFX, MEMORYSRC), |
| 104 | + '(?:%s%sv?p?unpck[a-z]*\s+%s)' %(SEP, PFX, MEMORYSRC), |
| 105 | + '(?:%s%sv?p?shuf[a-z0-9]*\s+%s)' %(SEP, PFX, MEMORYSRC), |
| 106 | + '(?:%s%sv?p?align[a-z]*\s+%s)' %(SEP, PFX, MEMORYSRC), |
| 107 | + '(?:%s%sv?pblend[a-z]*\s+%s)' %(SEP, PFX, MEMORYSRC), |
| 108 | + '(?:%s%svperm[a-z0-9]*\s+%s)' %(SEP, PFX, MEMORYSRC), |
| 109 | + '(?:%s%sv?p?insr[a-z]*\s+%s)' %(SEP, PFX, MEMORYSRC), |
| 110 | + '(?:%s%sv?insert[a-z0-9]*\s+%s)' %(SEP, PFX, MEMORYSRC), |
| 111 | + '(?:%s%sv?p?expand[a-z]*\s+%s)' %(SEP, PFX, MEMORYSRC), |
| 112 | + '(?:%s%svp?broadcast[a-z0-9]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 113 | + '(?:%s%svp?gather[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 114 | + '(?:%s%sv?pavg[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 115 | + '(?:%s%sv?p?min[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 116 | + '(?:%s%sv?p?max[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 117 | + '(?:%s%sv?phminpos[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 118 | + '(?:%s%sv?pabs[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 119 | + '(?:%s%sv?psign[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 120 | + '(?:%s%sv?(?:m|db|)psad[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 121 | + '(?:%s%sv?psll[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 122 | + '(?:%s%sv?psrl[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 123 | + '(?:%s%sv?psra[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 124 | + '(?:%s%sv?pclmulqdq\s+%s)' %(SEP, PFX, MEMORYANY), |
| 125 | + '(?:%s%sv?aesdec(?:last)?\s+%s)' %(SEP, PFX, MEMORYANY), |
| 126 | + '(?:%s%sv?aesenc(?:last)?\s+%s)' %(SEP, PFX, MEMORYANY), |
| 127 | + '(?:%s%sv?aesimc\s+%s)' %(SEP, PFX, MEMORYANY), |
| 128 | + '(?:%s%sv?aeskeygenassist\s+%s)' %(SEP, PFX, MEMORYANY), |
| 129 | + '(?:%s%sv?sha(?:1|256)(?:nexte|rnds4|msg1|msg2)\s+%s)' %(SEP, PFX, MEMORYSRC), |
| 130 | + '(?:%s%sv?cvt[a-z0-9]*\s+%s)' %(SEP, PFX, MEMORYSRC), |
| 131 | + '(?:%s%sv?rcp(?:ss|ps)\s+%s)' %(SEP, PFX, MEMORYANY), |
| 132 | + '(?:%s%sv?u?comis[sd]\s+%s)' %(SEP, PFX, MEMORYANY), |
| 133 | + '(?:%s%sv?round[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 134 | + '(?:%s%sv?dpp[sd]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 135 | + '(?:%s%sv?r?sqrt[a-z0-9]*\s+%s)' %(SEP, PFX, MEMORYSRC), |
| 136 | + '(?:%s%sv?ldmxcsr\s+%s)' %(SEP, PFX, MEMORYOP), |
| 137 | + '(?:%s%sf?x?rstors?\s+%s)' %(SEP, PFX, MEMORYOP), |
| 138 | + '(?:%s%sl[gi]dt\s+%s)' %(SEP, PFX, MEMORYOP), |
| 139 | + '(?:%s%slmsw\s+%s)' %(SEP, PFX, MEMORYANY), |
| 140 | + '(?:%s%svmptrld\s+%s)' %(SEP, PFX, MEMORYOP), |
| 141 | + '(?:%s%sf(?:b|i|)ld[a-z0-9]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 142 | + '(?:%s%sfi?add[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 143 | + '(?:%s%sfi?sub[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 144 | + '(?:%s%sfi?mul[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 145 | + '(?:%s%sfi?div[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 146 | + '(?:%s%sf(?:i|u|)com[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 147 | + '(?:%s%sleave[bswlqt]?)' %(SEP, PFX), |
| 148 | + '(?:%s%spopf[bswlqt]?)' %(SEP, PFX), |
| 149 | + '(?:%s%svfixupimm[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 150 | + '(?:%s%svf[m|n]add[a-z0-9]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 151 | + '(?:%s%svfpclass[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 152 | + '(?:%s%svget[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 153 | + '(?:%s%svpconflict[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 154 | + '(?:%s%svpternlog[d|q]\s+%s)' %(SEP, PFX, MEMORYANY), |
| 155 | + '(?:%s%svrange[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 156 | + '(?:%s%svreduce[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 157 | + '(?:%s%svrndscale[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 158 | + '(?:%s%svscalef[a-z]*\s+%s)' %(SEP, PFX, MEMORYANY), |
| 159 | + '(?:%s%sxlat\s+%s)' %(SEP, PFX, MEMORYANY), |
| 160 | + '(?:%s%sxlatb?)' %(SEP, PFX), |
| 161 | +] |
| 162 | + |
| 163 | +RET = '(?:%s%sret[a-z]*(?:\s+%s)?(?:#|$))' %(SEP, PFX, IMMEDIATE) |
| 164 | +MEM_INDBR = '(?:%s%s(?:call|jmp)[a-z]*\s+%s%s)' %(SEP, PFX, ATTSTAR, MEMORYOP) |
| 165 | +REG_INDBR = '(?:%s%s(?:call|jmp)[a-z]*\s+%s)' %(SEP, PFX, GPR) |
| 166 | + |
| 167 | +# |
| 168 | +# File Operations - read/write |
| 169 | +# |
| 170 | +def read_file(sfile): |
| 171 | + f = open(sfile, 'r') |
| 172 | + lines = f.readlines() |
| 173 | + f.close() |
| 174 | + return lines |
| 175 | + |
| 176 | +def write_file(tfile, lines): |
| 177 | + f = open(tfile, 'w') |
| 178 | + for line in lines: |
| 179 | + f.write(line) |
| 180 | + f.close() |
| 181 | + return |
| 182 | + |
| 183 | +def check_code_line(line): |
| 184 | + line = line.strip() |
| 185 | + if line.startswith(';') or line.startswith('%') or line.startswith('['): |
| 186 | + return False |
| 187 | + |
| 188 | + return True |
| 189 | + |
| 190 | +ASSEMBLERS = ['ml64', 'ml', 'nasm'] |
| 191 | +MITIGATIONS = ['NONE', 'CF', 'LOAD'] |
| 192 | + |
| 193 | +def insert_lfence(compiler, mitigation_level, infile, outfile): |
| 194 | + if compiler.startswith('ml'): |
| 195 | + PTR_KEYWORD = 'PTR' |
| 196 | + else: # compiler == 'nasm.exe' |
| 197 | + PTR_KEYWORD = '' |
| 198 | + |
| 199 | + pattern = '|'.join('(?:%s)' % l for l in LFENCE) |
| 200 | + lines = read_file(infile) |
| 201 | + outputs = lines |
| 202 | + for i in range(0, len(lines)): |
| 203 | + if lines[i].strip().startswith(';') or lines[i].strip().startswith('%') or lines[i].strip().startswith('['): |
| 204 | + continue |
| 205 | + if mitigation_level == 'LOAD': |
| 206 | + m = re.search(pattern, lines[i]) |
| 207 | + if m: |
| 208 | + j = i |
| 209 | + while j > 0: |
| 210 | + j = j + 1 |
| 211 | + if outputs[j].strip() != '' and not outputs[j].strip().startswith(';'): |
| 212 | + break |
| 213 | + if not outputs[j].strip().startswith('lfence'): |
| 214 | + load_mitigation = ' lfence\n' |
| 215 | + outputs[i] = outputs[i] + load_mitigation |
| 216 | + if mitigation_level == 'CF': |
| 217 | + m = re.search(REG_INDBR, lines[i]) |
| 218 | + if m: |
| 219 | + j = i |
| 220 | + while j > 0: |
| 221 | + j = j - 1 |
| 222 | + if outputs[j].strip() != '' and not outputs[j].strip().startswith(';'): |
| 223 | + break |
| 224 | + if not outputs[j].split('\n')[-2].strip().startswith('lfence'): |
| 225 | + outputs[i] = ' lfence\n' + outputs[i] |
| 226 | + if mitigation_level != 'NONE': |
| 227 | + m = re.search(RET, lines[i]) |
| 228 | + if m: |
| 229 | + ret_mitigation = ' shl QWORD %s [rsp], 0\n lfence\n' %(PTR_KEYWORD) |
| 230 | + outputs[i] = ret_mitigation + outputs[i] |
| 231 | + m = re.search(MEM_INDBR, lines[i]) |
| 232 | + if m: |
| 233 | + print ('Warning: indirect branch with memory operand, line %d' %(i)) |
| 234 | + |
| 235 | + write_file(outfile, outputs) |
| 236 | + |
| 237 | +def parse_options(): |
| 238 | + options = [] |
| 239 | + usage = "%(prog)s [options] [assembler arguments]" |
| 240 | + parser = argparse.ArgumentParser(usage=usage) |
| 241 | + parser.add_argument('--version', action='version', version=__version__) |
| 242 | + parser.add_argument('--assembler', type=str, help='specify assembler type: nasm|ml64|ml', required=True) |
| 243 | + parser.add_argument('--MITIGATION-CVE-2020-0551', type=str, dest='mitigation', default='NONE', help='specify CVE-2020-0551 mitigation level: NONE|LOAD|CF, [default: %(default)s]') |
| 244 | + (opts, args) = parser.parse_known_args() |
| 245 | + # check compiler |
| 246 | + if opts.assembler is None: |
| 247 | + print ('Error: assembler is not set') |
| 248 | + sys.exit(1) |
| 249 | + if not opts.assembler.lower() in ASSEMBLERS: |
| 250 | + print ('Error: assembler %s is not recogonized' %(opts.assembler)) |
| 251 | + sys.exit(1) |
| 252 | + compiler = opts.assembler# + '.exe' |
| 253 | + # mitigation level |
| 254 | + if not opts.mitigation.upper() in MITIGATIONS: |
| 255 | + print ('Error: MITIGATION-CVE-2020-0551 %s is not recogonized' %(opts.mitigation)) |
| 256 | + sys.exit(1) |
| 257 | + # process the arguments, add space and quote if needed |
| 258 | + for arg in args: |
| 259 | + if arg.startswith('/Ta') and len(arg) > 3: |
| 260 | + options.append('/Ta') |
| 261 | + arg = arg[3:] |
| 262 | + if arg.find(' ') > 0: |
| 263 | + arg = '\"' + arg + '\"' |
| 264 | + options.append(arg) |
| 265 | + return (compiler, opts.mitigation.upper(), options) |
| 266 | + |
| 267 | + |
| 268 | +def get_mitigated_file(src): |
| 269 | + return src + '.mitigated' |
| 270 | +def get_preprocess_file(src): |
| 271 | + return src + '.preprocess' |
| 272 | + |
| 273 | +def get_src_index(options): |
| 274 | + src_index = -1 |
| 275 | + for i in range(0,len(options)): |
| 276 | + if options[i].endswith('.asm'): |
| 277 | + if(src_index != -1): |
| 278 | + print ('source files conflict') |
| 279 | + sys.exit(-1) |
| 280 | + src_index = i |
| 281 | + if src_index == -1: |
| 282 | + print ('cannot find the source file') |
| 283 | + sys.exit(-1) |
| 284 | + return src_index |
| 285 | + |
| 286 | +def get_dst_index(options): |
| 287 | + dst_index = -1 |
| 288 | + for i in range(0,len(options)): |
| 289 | + if options[i] == '-o': |
| 290 | + if(dst_index != -1): |
| 291 | + print ('target files conflict') |
| 292 | + sys.exit(-1) |
| 293 | + dst_index = i+1 |
| 294 | + if dst_index == -1: |
| 295 | + print ('cannot find the target file') |
| 296 | + sys.exit(-1) |
| 297 | + return dst_index |
| 298 | + |
| 299 | +def get_preprocess_cmd(compiler, options, src_index): |
| 300 | + pre_file = get_preprocess_file(src_file) |
| 301 | + if compiler.startswith('ml'): |
| 302 | + pre_cmd = compiler + ' /EP ' + ' '.join(options) + ' > ' + pre_file |
| 303 | + else: # compiler == 'nasm.exe' |
| 304 | + ops = options |
| 305 | + dst_index = get_dst_index(options) |
| 306 | + tmp_file = ops[dst_index] |
| 307 | + ops[dst_index] = get_preprocess_file(src_file) |
| 308 | + pre_cmd = compiler + ' -E ' + ' '.join(ops) |
| 309 | + ops[dst_index] = tmp_file |
| 310 | + return pre_cmd |
| 311 | + |
| 312 | +if __name__ == '__main__': |
| 313 | + (compiler, mitigation, options) = parse_options() |
| 314 | + ops = options |
| 315 | + |
| 316 | + if mitigation != 'NONE': |
| 317 | + src_index = get_src_index(options) |
| 318 | + src_file = options[src_index] |
| 319 | + |
| 320 | + # preprocess the source file |
| 321 | + pre_cmd = get_preprocess_cmd(compiler, options, src_index) |
| 322 | + errno = os.system(pre_cmd) |
| 323 | + if errno != 0: |
| 324 | + print ('preprocess the assembly failed, see %s for the preprocess output' %(get_preprocess_file(src_file))) |
| 325 | + sys.exit(errno) |
| 326 | + # insert lfence |
| 327 | + insert_lfence(compiler, mitigation, get_preprocess_file(src_file), get_mitigated_file(src_file)) |
| 328 | + # compile use the mitigated file |
| 329 | + ops[src_index] = get_mitigated_file(src_file) |
| 330 | + |
| 331 | + as_cmd = compiler + ' ' + ' '.join(ops) |
| 332 | + errno = os.system(as_cmd) |
| 333 | + if errno != 0: |
| 334 | + print ('compile failed, file %s' %(get_mitigated_file(src_file))) |
| 335 | + sys.exit(errno) |
| 336 | + sys.exit(0) |
0 commit comments