Class: Elf::Policy::Policy
- Inherits:
-
Object
- Object
- Elf::Policy::Policy
- Defined in:
- lib/mithril/policy/dsl.rb
Instance Attribute Summary collapse
-
#calls ⇒ Object
Returns the value of attribute calls.
-
#data ⇒ Object
Returns the value of attribute data.
-
#imported_symbols ⇒ Object
Returns the value of attribute imported_symbols.
-
#start ⇒ Object
Returns the value of attribute start.
-
#tags ⇒ Object
Returns the value of attribute tags.
Instance Method Summary collapse
- #<<(*transitions) ⇒ Object
-
#initialize ⇒ Policy
constructor
A new instance of Policy.
- #inject(file) ⇒ Object
- #resolve_reference(elffile, relocations, offset, ref) ⇒ Object
- #resolve_size(elffile, relocations, offset, ref) ⇒ Object
- #states ⇒ Object
- #write_amd64(elffile) ⇒ Object
Constructor Details
#initialize ⇒ Policy
Returns a new instance of Policy.
64 65 66 67 68 69 70 |
# File 'lib/mithril/policy/dsl.rb', line 64 def initialize @data={} @calls=[] @states ={} = {} @imported_symbols = {} end |
Instance Attribute Details
#calls ⇒ Object
Returns the value of attribute calls.
36 37 38 |
# File 'lib/mithril/policy/dsl.rb', line 36 def calls @calls end |
#data ⇒ Object
Returns the value of attribute data.
36 37 38 |
# File 'lib/mithril/policy/dsl.rb', line 36 def data @data end |
#imported_symbols ⇒ Object
Returns the value of attribute imported_symbols.
37 38 39 |
# File 'lib/mithril/policy/dsl.rb', line 37 def imported_symbols @imported_symbols end |
#start ⇒ Object
Returns the value of attribute start.
36 37 38 |
# File 'lib/mithril/policy/dsl.rb', line 36 def start @start end |
#tags ⇒ Object
Returns the value of attribute tags.
36 37 38 |
# File 'lib/mithril/policy/dsl.rb', line 36 def end |
Instance Method Details
#<<(*transitions) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/mithril/policy/dsl.rb', line 43 def <<(*transitions) transitions.each do |t| if t.is_a? Data x= data x[t.from] ||= {} x[t.from][t.tag] ||= t if(x[t.from][t.tag].to != t.to) raise ArgumentError "Invalid Policy, different to states on same tag" #TODO: allow #different accesses to go to different tags. at the moment, this is moot, because data #access is unconditional end x[t.from][t.tag].read ||= t.read x[t.from][t.tag].write ||= t.write x[t.from][t.tag].exec ||= t.exec elsif t.is_a? Call calls << t else raise ArgumentError.new "#{t.class} is not a valid transition" end end end |
#inject(file) ⇒ Object
202 203 204 205 206 207 208 209 |
# File 'lib/mithril/policy/dsl.rb', line 202 def inject(file) case file.machine when ElfFlags::Machine::EM_X86_64 write_amd64(file) else raise RuntimeError.new "Wrong architecture for ARM64" end end |
#resolve_reference(elffile, relocations, offset, ref) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/mithril/policy/dsl.rb', line 71 def resolve_reference(elffile, relocations,offset, ref) if(ref.is_a? Integer) ref.to_i elsif(ref == "_dl_runtime_resolve") #HACK:HACK:HACK: I couldn't hack ld.so to fix this, so #here comes a nasty hack #note that the address of _dl_runtime_resolve is 16 bytes into PLT.GOT if !elffile.symbols.include? "_elfp_hidden_trampolineaddr" pltgot = elffile.dynamic.pltgot or raise RuntimeError.new "No plt.got for _dl_runtime_resolve hack" elffile.symbols << Elf::Symbol.new("_elfp_hidden_trampolineaddr", pltgot,STT::STT_OBJECT,16, STB::STB_LOCAL,8) end symb = elffile.symbols["_elfp_hidden_trampolineaddr"] relocations << Elf::Relocation.new.tap{|x| x.type = R::R_X86_64_COPY x.offset = offset x.symbol = symb x.is_dynamic = true x.addend = 0 } 0xDEADBEEF else raise RuntimeError.new "Symbol #{ref} not found" unless elffile.symbols.include? ref relocations << Elf::Relocation.new.tap{|x| x.type = R::R_X86_64_64 x.offset = offset x.symbol = elffile.symbols[ref] x.is_dynamic = true x.addend = 0 } 2**64-1 end end |
#resolve_size(elffile, relocations, offset, ref) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/mithril/policy/dsl.rb', line 102 def resolve_size(elffile,relocations, offset, ref) if(ref.is_a? Integer) ref.to_i else raise RuntimeError.new "Symbol #{ref} not found" unless elffile.symbols.include? ref relocations << Elf::Relocation.new.tap{|x| x.type = R::R_X86_64_SIZE64 x.offset = offset x.symbol = elffile.symbols[ref] x.is_dynamic = true x.addend = 0 } 2**64-1 end end |
#states ⇒ Object
38 39 40 41 42 |
# File 'lib/mithril/policy/dsl.rb', line 38 def states t = data.keys + data.values.map(&:keys).flatten + calls.map(&:from) + calls.map(&:to) t.uniq end |
#write_amd64(elffile) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/mithril/policy/dsl.rb', line 117 def write_amd64(elffile) factory = ElfStructFactory.instance(:little,64) @imported_symbols.each_key {|symbol| if elffile.symbols.include?(symbol) elffile.symbols[symbol].is_dynamic = true else elffile.symbols << Elf::Symbol.new(symbol,nil,Elf::STT::STT_OBJECT, 0, Elf::STB::STB_GLOBAL, 0).tap {|x| x.semantics = Elf::SHN::SHN_UNDEF } end } out = factory.elfp_header.new() state_ids = {} tag_ids = {} relocations = [] states = states() @start = states.first unless states.include? @start #These have to be filled in the order in which they are written #FIXME: Make these aware of double transitions to the same range/ state states.each_with_index do |state,index| id = index + 2 id = 1 if @start == state out.states << factory.elfp_state.new.tap {|x| x.id = id x.stackid = 0 } state_ids[state] = id print "State #{state} #{id}\n" end tag_ids[:default] = 0 .each_with_index do |(name,ranges),index| tag_ids[name] = index+1 ranges.each do |data| out. << factory.elfp_tag.new.tap {|x| x.tag = index + 1 x.addr = 0 x.siz = 0 } out..last.tap {|x| x.addr = resolve_reference(elffile,relocations,x.addr.offset,data.low) if data.high.nil? x.siz = resolve_size(elffile,relocations,x.siz.offset,data.low) else pp "Warning, emitting SIZE symbol with value #{ data.high.to_i rescue data.high.name}" x.siz = resolve_reference(elffile,relocations,x.siz.offset,data.high) end } end print "Tag #{name} #{index + 1} \n" end self.calls.each do |call| out.calls << factory.elfp_call.new.tap {|x| x.from = state_ids[call.from] x.to = state_ids[call.to] x.parambytes = call.parambytes x.returnbytes = call.parambytes } out.calls.last.off = resolve_reference(elffile,relocations,out.calls.last.off.offset, call.symbol) end self.data.values.map(&:values).flatten.each do |data| out.data << factory.elfp_data.new.tap {|x| x.from = state_ids[data.from] x.to = state_ids[data.to] x.type = 0 x.type |= ELFP::ELFP_RW_READ if data.read x.type |= ELFP::ELFP_RW_WRITE if data.write x.type |= ELFP::ELFP_RW_EXEC if data.exec raise RuntimeError.new "Unknown tag #{data.tag}" unless tag_ids.include? data.tag x.tag = tag_ids[data.tag] print "#{x.from} to #{x.to} data #{data.tag} = #{x.tag} #{data.read ? "r":" "}#{data.write ? "w":" "}#{data.exec ? "x":" "}\n" } end out = Elf::ProgBits.new(".elfbac",nil,out.to_binary_s) out.align = 8 out.flags = SHF::SHF_ALLOC | SHF::SHF_WRITE out.sect_type = SHT::SHT_PROGBITS out.phdr = ElfFlags::PhdrType::PT_ELFBAC out.phdr_flags = ElfFlags::PhdrFlags::PF_R relocations.each { |rel| rel.section = out elffile.relocations << rel } elffile.progbits << out end |