// libelf
#include <gelf.h>
-#include <elfutils/libdwelf.h>
#include <pakfire/ctx.h>
#include <pakfire/elf.h>
struct pakfire_file* file;
// ELF Object
- struct pakfire_elf* _elf;
+ struct pakfire_elf* elf;
// File Descriptor
int fd;
// Path
const char* path;
-
- // ELF
- Elf* elf;
};
#define pakfire_linter_file_info(lfile, format, ...) \
#define pakfire_linter_file_error(lfile, format, ...) \
pakfire_linter_result(lfile->linter, lfile->file, PAKFIRE_LINTER_ERROR, format, ## __VA_ARGS__)
-static int pakfire_linter_file_init_libelf(struct pakfire_linter_file* lfile) {
- // Initialize libelf
- if (elf_version(EV_CURRENT) == EV_NONE) {
- ERROR(lfile->ctx, "Could not initialize libelf: %s\n", elf_errmsg(-1));
- return -EINVAL;
- }
-
- return 0;
-}
-
-static int pakfire_linter_file_open_elf(struct pakfire_linter_file* lfile) {
- int r;
-
- // Initialize libelf
- r = pakfire_linter_file_init_libelf(lfile);
- if (r < 0)
- return r;
-
- // Parse the ELF header
- lfile->elf = elf_memory(lfile->data, lfile->length);
- if (!lfile->elf)
- return -errno;
-
- return 0;
-}
-
/*
Maps the file into memory
*/
}
// Open an ELF object
- r = pakfire_elf_open(&l->_elf, l->ctx, l->path, l->fd);
+ r = pakfire_elf_open(&l->elf, l->ctx, l->path, l->fd);
if (r < 0) {
switch (-r) {
// This does not seem to be an ELF file
}
}
- // Initialize libelf
- r = pakfire_linter_file_open_elf(l);
- if (r < 0) {
- ERROR(l->ctx, "Could not initialize libelf: %s\n", strerror(-r));
- goto ERROR;
- }
-
// Return the pointer
*lfile = pakfire_linter_file_ref(l);
static void pakfire_linter_file_free(struct pakfire_linter_file* lfile) {
int r;
- if (lfile->_elf)
- pakfire_elf_unref(lfile->_elf);
-
if (lfile->elf)
- elf_end(lfile->elf);
+ pakfire_elf_unref(lfile->elf);
if (lfile->data) {
r = munmap(lfile->data, lfile->length);
}
static int pakfire_linter_file_check_pie(struct pakfire_linter_file* lfile) {
- if (!pakfire_elf_is_pie(lfile->_elf))
+ if (!pakfire_elf_is_pie(lfile->elf))
return pakfire_linter_file_error(lfile, "Missing PIE");
return 0;
}
// Report an error if there is not SSP
- if (!pakfire_elf_has_ssp(lfile->_elf))
+ if (!pakfire_elf_has_ssp(lfile->elf))
return pakfire_linter_file_error(lfile, "Missing Stack Smashing Protection");
return 0;
}
static int pakfire_linter_file_check_execstack(struct pakfire_linter_file* lfile) {
- if (pakfire_elf_has_execstack(lfile->_elf))
+ if (pakfire_elf_has_execstack(lfile->elf))
return pakfire_linter_file_error(lfile, "Executable Stack");
return 0;
static int pakfire_linter_file_check_relro(struct pakfire_linter_file* lfile) {
// If the file is fully RELRO, everything is good
- if (pakfire_elf_is_fully_relro(lfile->_elf))
+ if (pakfire_elf_is_fully_relro(lfile->elf))
return 0;
// Show a warning if the file is only partially RELRO
- else if (pakfire_elf_is_partially_relro(lfile->_elf))
+ else if (pakfire_elf_is_partially_relro(lfile->elf))
return pakfire_linter_file_warning(lfile, "Is partially RELRO");
// Return an error if this file is not RELRO at all
int r;
// Fetch any runpaths
- r = pakfire_elf_has_runpaths(lfile->_elf, &runpaths);
+ r = pakfire_elf_has_runpaths(lfile->elf, &runpaths);
if (r < 0)
goto ERROR;
int r;
// Fetch if CF Protection has been enabled
- int flags = pakfire_elf_has_cf_protection(lfile->_elf);
+ int flags = pakfire_elf_has_cf_protection(lfile->elf);
// aarch64: Branch Target Identification
if (flags & PAKFIRE_ELF_MISSING_BTI) {
}
static int pakfire_linter_file_is_stripped(struct pakfire_linter_file* lfile) {
- switch (pakfire_elf_type(lfile->_elf)) {
+ switch (pakfire_elf_type(lfile->elf)) {
// Do not check Relocatable Objects
case ET_REL:
return 0;
break;
}
- if (!pakfire_elf_is_stripped(lfile->_elf))
+ if (!pakfire_elf_is_stripped(lfile->elf))
return pakfire_linter_file_error(lfile, "Not Stripped");
return 0;
const char* debuglink = NULL;
// Fetch the debug link
- debuglink = pakfire_elf_debuglink(lfile->_elf);
+ debuglink = pakfire_elf_debuglink(lfile->elf);
if (!debuglink)
return pakfire_linter_file_error(lfile, "Missing Debug Link");
const char* build_id = NULL;
// Fetch the build ID
- build_id = pakfire_elf_build_id(lfile->_elf);
+ build_id = pakfire_elf_build_id(lfile->elf);
if (!build_id)
return pakfire_linter_file_error(lfile, "Missing Build ID");
// Checks if files in /usr/lib/debug are correct
static int pakfire_linter_file_check_debug(struct pakfire_linter_file* lfile) {
// Fail if this file is not an ELF file
- if (!lfile->_elf)
+ if (!lfile->elf)
return pakfire_linter_file_error(lfile, "File is not in ELF format");
// Fail if there is no debugging information in the file
- if (pakfire_elf_is_stripped(lfile->_elf))
+ if (pakfire_elf_is_stripped(lfile->elf))
return pakfire_linter_file_error(lfile, "Has no debug information");
return 0;
return pakfire_linter_file_check_debug(lfile);
// ELF Checks
- if (lfile->_elf) {
+ if (lfile->elf) {
// Check if stripped
r = pakfire_linter_file_is_stripped(lfile);
if (r < 0)