Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration Candidate 2020-08-26 #62

Merged
merged 9 commits into from
Sep 1, 2020
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ See README.txt for more information.

## Version History

### Development Build: v3.2.0+dev9
- Restricts destination file permissions
- Squash int comparison warning
- Replace ctime (which generates LGTM warning) with ctime_r
- Deconflicts global/local parameters
- See <https://github.com/nasa/elf2cfetbl/pull/62>

### Development Build: v3.1.0+dev39

- Adds a null to the end of SrcFilename and DstFilename when using strncpy.
Expand Down
115 changes: 65 additions & 50 deletions elf2cfetbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ void PrintSymbol32(union Elf_Sym *Symbol);
void PrintSymbol64(union Elf_Sym *Symbol);
void PrintSectionHeader32(union Elf_Shdr *SectionHeader);
void PrintSectionHeader64(union Elf_Shdr *SectionHeader);
void PrintElfHeader32(union Elf_Ehdr ElfHeader);
void PrintElfHeader64(union Elf_Ehdr ElfHeader);
void PrintElfHeader32(union Elf_Ehdr ElfHeaderLcl);
void PrintElfHeader64(union Elf_Ehdr ElfHeaderLcl);

/**
* Global Variables
Expand Down Expand Up @@ -357,75 +357,75 @@ ElfStrMap e_machine_Map[] = {
};

// Elf Header helper functions
uint8_t get_e_ident(const union Elf_Ehdr *ElfHeader, int index)
uint8_t get_e_ident(const union Elf_Ehdr *ElfHeaderLcl, int index)
{
if (TargetWordsizeIs32Bit)
{
return ElfHeader->Ehdr32.e_ident[index];
return ElfHeaderLcl->Ehdr32.e_ident[index];
}
else
{
return ElfHeader->Ehdr64.e_ident[index];
return ElfHeaderLcl->Ehdr64.e_ident[index];
}
}

uint16_t get_e_type(const union Elf_Ehdr *ElfHeader)
uint16_t get_e_type(const union Elf_Ehdr *ElfHeaderLcl)
{
if (TargetWordsizeIs32Bit)
{
return ElfHeader->Ehdr32.e_type;
return ElfHeaderLcl->Ehdr32.e_type;
}
else
{
return ElfHeader->Ehdr64.e_type;
return ElfHeaderLcl->Ehdr64.e_type;
}
}

uint16_t get_e_machine(const union Elf_Ehdr *ElfHeader)
uint16_t get_e_machine(const union Elf_Ehdr *ElfHeaderLcl)
{
if (TargetWordsizeIs32Bit)
{
return ElfHeader->Ehdr32.e_machine;
return ElfHeaderLcl->Ehdr32.e_machine;
}
else
{
return ElfHeader->Ehdr64.e_machine;
return ElfHeaderLcl->Ehdr64.e_machine;
}
}

uint32_t get_e_version(const union Elf_Ehdr *ElfHeader)
uint32_t get_e_version(const union Elf_Ehdr *ElfHeaderLcl)
{
if (TargetWordsizeIs32Bit)
{
return ElfHeader->Ehdr32.e_version;
return ElfHeaderLcl->Ehdr32.e_version;
}
else
{
return ElfHeader->Ehdr64.e_version;
return ElfHeaderLcl->Ehdr64.e_version;
}
}

uint16_t get_e_shstrndx(const union Elf_Ehdr *ElfHeader)
uint16_t get_e_shstrndx(const union Elf_Ehdr *ElfHeaderLcl)
{
if (TargetWordsizeIs32Bit)
{
return ElfHeader->Ehdr32.e_shstrndx;
return ElfHeaderLcl->Ehdr32.e_shstrndx;
}
else
{
return ElfHeader->Ehdr64.e_shstrndx;
return ElfHeaderLcl->Ehdr64.e_shstrndx;
}
}

uint16_t get_e_shnum(const union Elf_Ehdr *ElfHeader)
uint16_t get_e_shnum(const union Elf_Ehdr *ElfHeaderLcl)
{
if (TargetWordsizeIs32Bit)
{
return ElfHeader->Ehdr32.e_shnum;
return ElfHeaderLcl->Ehdr32.e_shnum;
}
else
{
return ElfHeader->Ehdr64.e_shnum;
return ElfHeaderLcl->Ehdr64.e_shnum;
}
}

Expand Down Expand Up @@ -929,7 +929,7 @@ int32 ProcessCmdLineOptions(int ArgumentCount, char *Arguments[])
int32 Status = SUCCESS;
bool InputFileSpecified = false;
bool OutputFileSpecified = false;
int16 i = 1;
int i = 1;
char * EndPtr;
uint32 MaxDay;
struct tm FileEpochTm;
Expand Down Expand Up @@ -1404,7 +1404,8 @@ int32 GetDstFilename(void)

int32 OpenSrcFile(void)
{
int RtnCode;
int RtnCode;
char TimeBuff[50];

// Check to see if input file can be found and opened
SrcFileDesc = fopen(SrcFilename, "r");
Expand All @@ -1422,10 +1423,11 @@ int32 OpenSrcFile(void)
SrcFileTimeInScEpoch = SrcFileStats.st_mtime + EpochDelta;

if (Verbose)
printf("Original Source File Modification Time: %s\n", ctime(&SrcFileStats.st_mtime));
if (Verbose)
{
printf("Original Source File Modification Time: %s\n", ctime_r(&SrcFileStats.st_mtime, TimeBuff));
printf("Source File Modification Time in Seconds since S/C Epoch: %ld (0x%08lX)\n", SrcFileTimeInScEpoch,
SrcFileTimeInScEpoch);
}
}
else
{
Expand All @@ -1439,6 +1441,8 @@ int32 OpenSrcFile(void)

int32 OpenDstFile(void)
{
struct stat dststat;

// Check to see if output file can be opened and written
DstFileDesc = fopen(DstFilename, "w");

Expand All @@ -1448,6 +1452,17 @@ int32 OpenDstFile(void)
return FAILED;
}

/* Fix file if too permissive (CWE-732) */
if (stat(DstFilename, &dststat) == 0)
{
if (Verbose)
printf("%s: Destination file permissions after open = 0x%X\n", DstFilename, dststat.st_mode);
chmod(DstFilename, dststat.st_mode & ~(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH));
stat(DstFilename, &dststat);
if (Verbose)
printf("%s: Destination file permissions after chmod = 0x%X\n", DstFilename, dststat.st_mode);
}

return SUCCESS;
}

Expand Down Expand Up @@ -2007,58 +2022,58 @@ void PrintSectionHeader64(union Elf_Shdr *SectionHeader)
printf(" sh_entsize = 0x%08lx\n", (long unsigned int)SectionHeader->Shdr64.sh_entsize);
}

void PrintElfHeader32(union Elf_Ehdr ElfHeader)
void PrintElfHeader32(union Elf_Ehdr ElfHeaderLcl)
{
if (Verbose)
printf(" e_version = %d\n", get_e_version(&ElfHeader));
printf(" e_version = %d\n", get_e_version(&ElfHeaderLcl));
if (Verbose)
printf(" e_entry = 0x%x\n", ElfHeader.Ehdr32.e_entry);
printf(" e_entry = 0x%x\n", ElfHeaderLcl.Ehdr32.e_entry);
if (Verbose)
printf(" e_phoff = 0x%08x (%u)\n", ElfHeader.Ehdr32.e_phoff, ElfHeader.Ehdr32.e_phoff);
printf(" e_phoff = 0x%08x (%u)\n", ElfHeaderLcl.Ehdr32.e_phoff, ElfHeaderLcl.Ehdr32.e_phoff);
if (Verbose)
printf(" e_shoff = 0x%08x (%u)\n", ElfHeader.Ehdr32.e_shoff, ElfHeader.Ehdr32.e_shoff);
printf(" e_shoff = 0x%08x (%u)\n", ElfHeaderLcl.Ehdr32.e_shoff, ElfHeaderLcl.Ehdr32.e_shoff);
if (Verbose)
printf(" e_flags = 0x%08x\n", ElfHeader.Ehdr32.e_flags);
printf(" e_flags = 0x%08x\n", ElfHeaderLcl.Ehdr32.e_flags);
if (Verbose)
printf(" e_ehsize = %d\n", ElfHeader.Ehdr32.e_ehsize);
printf(" e_ehsize = %d\n", ElfHeaderLcl.Ehdr32.e_ehsize);
if (Verbose)
printf(" e_phentsize = %d\n", ElfHeader.Ehdr32.e_phentsize);
printf(" e_phentsize = %d\n", ElfHeaderLcl.Ehdr32.e_phentsize);
if (Verbose)
printf(" e_phnum = %d\n", ElfHeader.Ehdr32.e_phnum);
printf(" e_phnum = %d\n", ElfHeaderLcl.Ehdr32.e_phnum);
if (Verbose)
printf(" e_shentsize = %d\n", ElfHeader.Ehdr32.e_shentsize);
printf(" e_shentsize = %d\n", ElfHeaderLcl.Ehdr32.e_shentsize);
if (Verbose)
printf(" e_shnum = %d\n", get_e_shnum(&ElfHeader));
printf(" e_shnum = %d\n", get_e_shnum(&ElfHeaderLcl));
if (Verbose)
printf(" e_shstrndx = %d\n", get_e_shstrndx(&ElfHeader));
printf(" e_shstrndx = %d\n", get_e_shstrndx(&ElfHeaderLcl));
}

void PrintElfHeader64(union Elf_Ehdr ElfHeader)
void PrintElfHeader64(union Elf_Ehdr ElfHeaderLcl)
{
if (Verbose)
printf(" e_version = %d\n", get_e_version(&ElfHeader));
printf(" e_version = %d\n", get_e_version(&ElfHeaderLcl));
if (Verbose)
printf(" e_entry = 0x%lx\n", (long unsigned int)ElfHeader.Ehdr64.e_entry);
printf(" e_entry = 0x%lx\n", (long unsigned int)ElfHeaderLcl.Ehdr64.e_entry);
if (Verbose)
printf(" e_phoff = 0x%08lx (%lu)\n", (long unsigned int)ElfHeader.Ehdr64.e_phoff,
(long unsigned int)ElfHeader.Ehdr64.e_phoff);
printf(" e_phoff = 0x%08lx (%lu)\n", (long unsigned int)ElfHeaderLcl.Ehdr64.e_phoff,
(long unsigned int)ElfHeaderLcl.Ehdr64.e_phoff);
if (Verbose)
printf(" e_shoff = 0x%08lx (%lu)\n", (long unsigned int)ElfHeader.Ehdr64.e_shoff,
(long unsigned int)ElfHeader.Ehdr64.e_shoff);
printf(" e_shoff = 0x%08lx (%lu)\n", (long unsigned int)ElfHeaderLcl.Ehdr64.e_shoff,
(long unsigned int)ElfHeaderLcl.Ehdr64.e_shoff);
if (Verbose)
printf(" e_flags = 0x%08x\n", ElfHeader.Ehdr64.e_flags);
printf(" e_flags = 0x%08x\n", ElfHeaderLcl.Ehdr64.e_flags);
if (Verbose)
printf(" e_ehsize = %d\n", ElfHeader.Ehdr64.e_ehsize);
printf(" e_ehsize = %d\n", ElfHeaderLcl.Ehdr64.e_ehsize);
if (Verbose)
printf(" e_phentsize = %d\n", ElfHeader.Ehdr64.e_phentsize);
printf(" e_phentsize = %d\n", ElfHeaderLcl.Ehdr64.e_phentsize);
if (Verbose)
printf(" e_phnum = %d\n", ElfHeader.Ehdr64.e_phnum);
printf(" e_phnum = %d\n", ElfHeaderLcl.Ehdr64.e_phnum);
if (Verbose)
printf(" e_shentsize = %d\n", ElfHeader.Ehdr64.e_shentsize);
printf(" e_shentsize = %d\n", ElfHeaderLcl.Ehdr64.e_shentsize);
if (Verbose)
printf(" e_shnum = %d\n", get_e_shnum(&ElfHeader));
printf(" e_shnum = %d\n", get_e_shnum(&ElfHeaderLcl));
if (Verbose)
printf(" e_shstrndx = %d\n", get_e_shstrndx(&ElfHeader));
printf(" e_shstrndx = %d\n", get_e_shstrndx(&ElfHeaderLcl));
}

void SwapElfHeader(void)
Expand Down
4 changes: 2 additions & 2 deletions elf2cfetbl_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
/*
* Development Build Macro Definitions
*/
#define ELF2CFETBL_BUILD_NUMBER 39 /*!< @brief Number of commits since baseline */
#define ELF2CFETBL_BUILD_BASELINE "v3.1.0+dev" /*!< @brief Development Build: git tag that is the base for the current */
#define ELF2CFETBL_BUILD_NUMBER 9 /*!< @brief Number of commits since baseline */
#define ELF2CFETBL_BUILD_BASELINE "v3.2.0-rc1" /*!< @brief Development Build: git tag that is the base for the current */

/*
* Version Macro Definitions
Expand Down