Skip to content

Commit f432c9d

Browse files
committed
CHB:ELF: add check for read-only data sections
1 parent ee49135 commit f432c9d

File tree

3 files changed

+53
-36
lines changed

3 files changed

+53
-36
lines changed

CodeHawk/CHB/bchlibelf/bCHELFHeader.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,15 @@ object(self)
545545
&& (va#lt (h#get_addr#add h#get_size))))
546546
false self#get_sections
547547

548+
method is_readonly_address (va: doubleword_int): bool =
549+
List.fold_left (fun found (_, h, _) ->
550+
found
551+
|| (h#is_program_section)
552+
&& (h#is_readonly || h#is_executable)
553+
&& (h#get_addr#le va)
554+
&& (va#lt (h#get_addr#add h#get_size)))
555+
false self#get_sections
556+
548557
method is_uninitialized_data_address (va: doubleword_int): bool =
549558
List.fold_left (fun found (_, h, _) ->
550559
found

CodeHawk/CHB/bchlibelf/bCHELFSectionHeader.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ object (self)
370370

371371
method is_executable = sh_flags#is_nth_bit_set 2
372372

373+
method is_readonly = not (sh_flags#is_nth_bit_set 0)
374+
373375
method is_string_table =
374376
match self#get_section_type with SHT_StrTab -> true | _ -> false
375377

CodeHawk/CHB/bchlibelf/bCHELFTypes.mli

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
(* =============================================================================
2-
CodeHawk Binary Analyzer
2+
CodeHawk Binary Analyzer
33
Author: A. Cody Schuffelen and Henny Sipma
44
------------------------------------------------------------------------------
55
The MIT License (MIT)
6-
6+
77
Copyright (c) 2005-2020 Kestrel Technology LLC
88
Copyright (c) 2020 Henny Sipma
99
Copyright (c) 2021-2024 Aarno Labs LLC
@@ -14,10 +14,10 @@
1414
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1515
copies of the Software, and to permit persons to whom the Software is
1616
furnished to do so, subject to the following conditions:
17-
17+
1818
The above copyright notice and this permission notice shall be included in all
1919
copies or substantial portions of the Software.
20-
20+
2121
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2222
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2323
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -31,11 +31,11 @@
3131
References used:
3232
3333
The standard /usr/include/elf.h in Arch Linux
34-
The latest draft of the System V Application Binary Interface:
34+
The latest draft of the System V Application Binary Interface:
3535
http://www.sco.com/developers/gabi/latest/contents.html
36-
March 19, 1997 draft copy of the Intel Supplement to the System V
36+
March 19, 1997 draft copy of the Intel Supplement to the System V
3737
Application Binary Interface
38-
----------------------------------------------------------------------------- *)
38+
----------------------------------------------------------------------------- *)
3939

4040
(* chlib *)
4141
open CHNumerical
@@ -51,47 +51,47 @@ open BCHLibTypes
5151
open BCHDwarfTypes
5252

5353

54-
type elf_section_header_type_t =
55-
| SHT_NullSection
54+
type elf_section_header_type_t =
55+
| SHT_NullSection
5656
| SHT_ProgBits
57-
| SHT_SymTab
58-
| SHT_StrTab
59-
| SHT_Rela
60-
| SHT_Hash
61-
| SHT_Dynamic
62-
| SHT_Note
63-
| SHT_NoBits
64-
| SHT_Rel
65-
| SHT_ShLib
66-
| SHT_DynSym
67-
| SHT_InitArray
57+
| SHT_SymTab
58+
| SHT_StrTab
59+
| SHT_Rela
60+
| SHT_Hash
61+
| SHT_Dynamic
62+
| SHT_Note
63+
| SHT_NoBits
64+
| SHT_Rel
65+
| SHT_ShLib
66+
| SHT_DynSym
67+
| SHT_InitArray
6868
| SHT_FiniArray
69-
| SHT_PreinitArray
70-
| SHT_Group
69+
| SHT_PreinitArray
70+
| SHT_Group
7171
| SHT_SymTabShndx
7272
| SHT_GNU_verdef
7373
| SHT_GNU_verneed
7474
| SHT_GNU_versym
7575
| SHT_MIPS_RegInfo
76-
| SHT_OSSection of doubleword_int
77-
| SHT_ProcSection of doubleword_int
78-
| SHT_UserSection of doubleword_int
76+
| SHT_OSSection of doubleword_int
77+
| SHT_ProcSection of doubleword_int
78+
| SHT_UserSection of doubleword_int
7979
| SHT_UnknownType of doubleword_int
80-
80+
8181
type elf_program_header_type_t =
82-
| PT_Null
83-
| PT_Load
84-
| PT_Dynamic
85-
| PT_Interpreter
86-
| PT_Note
87-
| PT_Reference
82+
| PT_Null
83+
| PT_Load
84+
| PT_Dynamic
85+
| PT_Interpreter
86+
| PT_Note
87+
| PT_Reference
8888
| PT_ThreadLocalStorage
8989
| PT_RegInfo
90-
| PT_OSSpecific of doubleword_int
90+
| PT_OSSpecific of doubleword_int
9191
| PT_ProcSpecific of doubleword_int
9292

9393
type elf_dynamic_tag_value_t = DTV_d_val | DTV_d_ptr | DTV_d_none
94-
94+
9595
type elf_dynamic_tag_t =
9696
| DT_Null
9797
| DT_Needed
@@ -205,7 +205,7 @@ class type elf_dictionary_int =
205205
method read_xml: xml_element_int -> unit
206206
method toPretty: pretty_t
207207
end
208-
208+
209209
class type elf_raw_section_int =
210210
object
211211
method get_size: int
@@ -248,7 +248,7 @@ class type elf_string_table_int =
248248
class type elf_symbol_table_entry_int =
249249
object
250250
method id: int
251-
method read: pushback_stream_int -> unit
251+
method read: pushback_stream_int -> unit
252252
method set_name: string -> unit
253253
method get_name: string
254254
method get_st_binding: int
@@ -761,6 +761,7 @@ object
761761

762762
(* predicates *)
763763
method is_executable: bool
764+
method is_readonly: bool
764765
method is_string_table: bool
765766
method is_symbol_table: bool
766767
method is_relocation_table: bool
@@ -816,6 +817,11 @@ object
816817
within a program section that is not an executable section. *)
817818
method is_data_address: doubleword_int -> bool
818819

820+
(** [read_readonly_address va] returns [true] if virtual address [va] is an
821+
address within a program section that is not writeable (it may be
822+
executable). *)
823+
method is_readonly_address: doubleword_int -> bool
824+
819825
method is_uninitialized_data_address: doubleword_int -> bool
820826
method is_global_offset_table_address: doubleword_int -> bool
821827
method has_xsubstring: doubleword_int -> int -> bool

0 commit comments

Comments
 (0)