@@ -111,28 +111,49 @@ const T* get_section_raw_data(const ElfW(Ehdr) *elf_hdr, ElfW(Addr) start_addr)
111111bool validate_elf_header (const ElfW (Ehdr) *elf_hdr)
112112{
113113 // validate magic number
114- if (memcmp (&elf_hdr->e_ident , ELFMAG, SELFMAG))
114+ if (memcmp (&elf_hdr->e_ident , ELFMAG, SELFMAG)) {
115+ SE_TRACE (SE_TRACE_ERROR, " Incorrect magic number\n " );
115116 return false ;
117+ }
116118
117119#if RTS_SYSTEM_WORDSIZE == 64
118- if (ELFCLASS64 != elf_hdr->e_ident [EI_CLASS])
120+ if (ELFCLASS64 != elf_hdr->e_ident [EI_CLASS]) {
121+ SE_TRACE (SE_TRACE_ERROR, " Expected ELFCLASS64: 0x%x\n " ,
122+ elf_hdr->e_ident [EI_CLASS]);
119123 return false ;
124+ }
120125#else
121- if (ELFCLASS32 != elf_hdr->e_ident [EI_CLASS])
126+ if (ELFCLASS32 != elf_hdr->e_ident [EI_CLASS]) {
127+ SE_TRACE (SE_TRACE_ERROR, " Expected ELFCLASS32: 0x%x\n " ,
128+ elf_hdr->e_ident [EI_CLASS]);
122129 return false ;
130+ }
123131#endif
124132
125- if (ELFDATA2LSB!= elf_hdr->e_ident [EI_DATA])
133+ if (ELFDATA2LSB!= elf_hdr->e_ident [EI_DATA]) {
134+ SE_TRACE (SE_TRACE_ERROR, " Expected ELFDATA2LSB: 0x%x\n " ,
135+ elf_hdr->e_ident [EI_DATA]);
126136 return false ;
137+ }
127138
128- if (EV_CURRENT != elf_hdr->e_ident [EI_VERSION])
139+ if (EV_CURRENT != elf_hdr->e_ident [EI_VERSION]) {
140+ SE_TRACE (SE_TRACE_ERROR, " Expected EV_CURRENT: 0x%x\n " ,
141+ elf_hdr->e_ident [EI_VERSION]);
129142 return false ;
143+ }
130144
131- if (ET_DYN != elf_hdr->e_type )
145+ if (ET_DYN != elf_hdr->e_type ) {
146+ SE_TRACE (SE_TRACE_ERROR, " Expected ET_DYN: 0x%x\n " ,
147+ elf_hdr->e_type );
132148 return false ;
149+ }
133150
134- if (sizeof (ElfW (Phdr)) != elf_hdr->e_phentsize )
151+ if (sizeof (ElfW (Phdr)) != elf_hdr->e_phentsize ) {
152+ SE_TRACE (SE_TRACE_ERROR, " Expected phentsize == %d, got %d\n " ,
153+ sizeof (ElfW (Phdr)),
154+ elf_hdr->e_phentsize );
135155 return false ;
156+ }
136157
137158 return true ;
138159}
@@ -608,45 +629,60 @@ sgx_status_t ElfParser::run_parser()
608629 if (m_sections.size () != 0 ) return SGX_SUCCESS;
609630
610631 const ElfW (Ehdr) *elf_hdr = (const ElfW (Ehdr) *)m_start_addr;
611- if (elf_hdr == NULL || m_len < sizeof (ElfW (Ehdr)))
632+ if (elf_hdr == NULL || m_len < sizeof (ElfW (Ehdr))) {
633+ SE_TRACE_ERROR (" Header invalid size\n " );
612634 return SGX_ERROR_INVALID_ENCLAVE;
613-
635+ }
614636 /* Check elf header*/
615- if (!validate_elf_header (elf_hdr))
637+ if (!validate_elf_header (elf_hdr)) {
638+ SE_TRACE_ERROR (" Header invalid\n " );
616639 return SGX_ERROR_INVALID_ENCLAVE;
617-
640+ }
618641 /* Get and check machine mode */
619- if (!get_bin_fmt (elf_hdr, m_bin_fmt))
642+ if (!get_bin_fmt (elf_hdr, m_bin_fmt)) {
643+ SE_TRACE_ERROR (" Bin fmt incorrect\n " );
620644 return SGX_ERROR_MODE_INCOMPATIBLE;
645+ }
621646
622647 /* Check if there is any overlap segment, and make sure the segment is 1 page aligned;
623648 * TLS segment must exist.
624649 */
625- if (!validate_segment (elf_hdr, m_len))
650+ if (!validate_segment (elf_hdr, m_len)) {
651+ SE_TRACE_ERROR (" Segment incorrect\n " );
626652 return SGX_ERROR_INVALID_ENCLAVE;
653+ }
627654
628- if (!parse_dyn (elf_hdr, &m_dyn_info[0 ]))
655+ if (!parse_dyn (elf_hdr, &m_dyn_info[0 ])) {
656+ SE_TRACE_ERROR (" Dyn incorrect\n " );
629657 return SGX_ERROR_INVALID_ENCLAVE;
658+ }
630659
631660 /* Check if there is any undefined symbol */
632661 if (!check_symbol_table (elf_hdr, m_dyn_info, m_sym_table))
633662 {
663+ SE_TRACE_ERROR (" Symbol table incorrect\n " );
634664 return SGX_ERROR_UNDEFINED_SYMBOL;
635665 }
636666
637667 /* Check if there is unexpected relocation type */
638- if (!validate_reltabs (elf_hdr, m_dyn_info))
668+ if (!validate_reltabs (elf_hdr, m_dyn_info)) {
669+ SE_TRACE_ERROR (" Reltabs incorrect\n " );
639670 return SGX_ERROR_INVALID_ENCLAVE;
671+ }
640672
641673 /* Check if there is .ctor section */
642- if (has_ctor_section (elf_hdr))
674+ if (has_ctor_section (elf_hdr)) {
675+ SE_TRACE_ERROR (" ctor section incorrect\n " );
643676 return SGX_ERROR_INVALID_ENCLAVE;
677+ }
644678
645679 /* build regular sections */
646680 if (build_regular_sections (m_start_addr, m_sections, m_tls_section, m_metadata_offset, m_metadata_block_size))
647681 return SGX_SUCCESS;
648- else
682+ else {
683+ SE_TRACE_ERROR (" Regular sections incorrect\n " );
649684 return SGX_ERROR_INVALID_ENCLAVE;
685+ }
650686}
651687
652688ElfParser::~ElfParser ()
0 commit comments