Skip to content

Commit

Permalink
fixing a few bugs discovered by Coverity
Browse files Browse the repository at this point in the history
Files: common/acpi.c, include/compiler.h, arch/x86/pagetables.c
       include/arch/x86/pagetable.h

These bugs were discovered and resolved using Coverity Static Analysis
Security Testing (SAST) by Synopsys, Inc.

Signed-off-by: Pawel Wieczorkiewicz <[email protected]>
  • Loading branch information
wipawel committed Aug 27, 2020
1 parent 9bff053 commit c83fc4e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions arch/x86/pagetables.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ static void *init_map_mfn(mfn_t mfn) {
static uint8_t _tmp[PAGE_SIZE] __aligned(PAGE_SIZE);
pgentry_t *e;

if (mfn_invalid(mfn))
return NULL;
BUG_ON(mfn_invalid(mfn));

e = (pgentry_t *) l1_table_entry(get_l1_table(_tmp), _tmp);
BUG_ON(!e);
set_pgentry(e, mfn, L1_PROT);

return _tmp;
Expand Down
19 changes: 17 additions & 2 deletions common/acpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ static inline void *acpi_map_table(paddr_t pa) {
static inline rsdt_t *acpi_find_rsdt(const rsdp_rev1_t *rsdp) {
rsdt_t *rsdt = acpi_map_table(rsdp->rsdt_paddr);

if (!rsdt)
return NULL;

if (RSDT_SIGNATURE != rsdt->header.signature)
return NULL;

Expand All @@ -132,6 +135,9 @@ static inline rsdt_t *acpi_find_rsdt(const rsdp_rev1_t *rsdp) {
static inline xsdt_t *acpi_find_xsdt(const rsdp_rev2_t *rsdp) {
xsdt_t *xsdt = acpi_map_table(rsdp->xsdt_paddr);

if (!xsdt)
return NULL;

if (XSDT_SIGNATURE != xsdt->header.signature)
return NULL;

Expand All @@ -158,6 +164,9 @@ static unsigned process_madt_entries(void) {
acpi_madt_t *madt = (acpi_madt_t *) acpi_find_table(MADT_SIGNATURE);
acpi_madt_entry_t *entry;

if (!madt)
return 0;

printk("ACPI: [MADT] LAPIC Addr: %p, Flags: %08x\n", _ptr(madt->lapic_addr),
madt->flags);

Expand Down Expand Up @@ -219,21 +228,27 @@ void init_acpi(void) {
if (rsdp->rev < 2) {
rsdt_t *rsdt = acpi_find_rsdt(rsdp);

if (!rsdt)
return;

for (unsigned int i = 0; i < ACPI_NR_TABLES(rsdt); i++) {
acpi_table_t *tab = acpi_map_table(rsdt->entry[i]);

if (get_checksum(tab, tab->header.length) == 0x0)
if (tab && get_checksum(tab, tab->header.length) == 0x0)
acpi_tables[max_acpi_tables++] = tab;
}
}
else {
xsdt_t *xsdt = acpi_find_xsdt((rsdp_rev2_t *) rsdp);

if (!xsdt)
return;

for (unsigned int i = 0; i < ACPI_NR_TABLES(xsdt); i++) {
paddr_t tab_pa = _ul(xsdt->entry[i].high) << 32 | xsdt->entry[i].low;
acpi_table_t *tab = acpi_map_table(tab_pa);

if (get_checksum(tab, tab->header.length) == 0x0)
if (tab && get_checksum(tab, tab->header.length) == 0x0)
acpi_tables[max_acpi_tables++] = tab;
}
}
Expand Down
8 changes: 5 additions & 3 deletions include/arch/x86/pagetable.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,15 @@ static inline pgentry_t pgentry_from_virt(const void *va, unsigned long flags) {
return pgentry_from_paddr(virt_to_paddr(va), flags);
}

#define INVALID_PGENTRY(e) (!(e) || mfn_invalid((e)->mfn))

#if defined(__x86_64__)
static inline pml4_t *get_l4_table(void) { return paddr_to_virt_kern(read_cr3()); }

static inline pdpe_t *get_l3_table(const void *va) {
pml4_t *l3e = l4_table_entry(get_l4_table(), va);

return mfn_invalid(l3e->mfn) ? NULL : mfn_to_virt_kern(l3e->mfn);
return INVALID_PGENTRY(l3e) ? NULL : mfn_to_virt_kern(l3e->mfn);
}
#elif defined(__i386__)
static inline pdpe_t *get_l3_table(void) { return paddr_to_virt_kern(read_cr3()); }
Expand All @@ -227,13 +229,13 @@ static inline pdpe_t *get_l3_table(void) { return paddr_to_virt_kern(read_cr3())
static inline pde_t *get_l2_table(const void *va) {
pdpe_t *l2e = l3_table_entry(get_l3_table(va), va);

return mfn_invalid(l2e->mfn) ? NULL : mfn_to_virt_kern(l2e->mfn);
return INVALID_PGENTRY(l2e) ? NULL : mfn_to_virt_kern(l2e->mfn);
}

static inline pte_t *get_l1_table(const void *va) {
pde_t *l1e = l2_table_entry(get_l2_table(va), va);

return mfn_invalid(l1e->mfn) ? NULL : mfn_to_virt_kern(l1e->mfn);
return INVALID_PGENTRY(l1e) ? NULL : mfn_to_virt_kern(l1e->mfn);
}

static inline void set_pgentry(pgentry_t *e, mfn_t mfn, unsigned long flags) {
Expand Down
4 changes: 2 additions & 2 deletions include/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
#define __bss_init __section(".bss.init")

#define IS_INIT_SECTION(name) \
(!strcmp(name, ".text.init") || !strcmp(name, ".data.init") || \
!strcmp(name, ".bss.init"))
((name) && (!strcmp(name, ".text.init") || !strcmp(name, ".data.init") || \
!strcmp(name, ".bss.init")))

#define __user_text __section(".text.user")
#define __user_data __section(".data.user")
Expand Down

0 comments on commit c83fc4e

Please sign in to comment.