From 4e84f18db19589074cbc0b8bda353eb2314a1a01 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 23 Jun 2008 21:56:36 -0400 Subject: [PATCH] Fix various off-by-one comparison bugs. Signed-off-by: Anders Kaseorg --- kmodsrc/ksplice.c | 15 +++++++++------ kmodsrc/ksplice.h | 2 +- objcommon.h | 7 +++++-- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/kmodsrc/ksplice.c b/kmodsrc/ksplice.c index b414957..7b4194b 100644 --- a/kmodsrc/ksplice.c +++ b/kmodsrc/ksplice.c @@ -324,16 +324,19 @@ int check_address_for_conflict(struct module_pack *pack, long addr) struct ksplice_size *s = pack->primary_sizes; struct safety_record *rec; + /* It is safe for addr to point to the beginning of a patched + function, because that location will be overwritten with a + trampoline. */ list_for_each_entry(rec, pack->safety_records, list) { if (rec->care == 1 && addr > rec->addr - && addr <= (rec->addr + rec->size)) { + && addr < rec->addr + rec->size) { ksplice_debug(2, "[<-- CONFLICT] "); return -EAGAIN; } } for (; s->name != NULL; s++) { - if (addr > s->thismod_addr - && addr <= (s->thismod_addr + s->size)) { + if (addr >= s->thismod_addr + && addr < s->thismod_addr + s->size) { ksplice_debug(2, "[<-- CONFLICT] "); return -EAGAIN; } @@ -1077,13 +1080,13 @@ void set_temp_myst_relocs(struct module_pack *pack, int status_val) int starts_with(const char *str, const char *prefix) { - return !strncmp(str, prefix, strlen(prefix)); + return strncmp(str, prefix, strlen(prefix)) == 0; } int ends_with(const char *str, const char *suffix) { - return strlen(str) > strlen(suffix) && - !strcmp(&str[strlen(str) - strlen(suffix)], suffix); + return strlen(str) >= strlen(suffix) && + strcmp(&str[strlen(str) - strlen(suffix)], suffix) == 0; } int label_offset(const char *sym_name) diff --git a/kmodsrc/ksplice.h b/kmodsrc/ksplice.h index 863e833..258e47e 100644 --- a/kmodsrc/ksplice.h +++ b/kmodsrc/ksplice.h @@ -81,7 +81,7 @@ static inline int virtual_address_mapped(long addr) pmd_t *pmd; pte_t *ptep; - if (addr > init_mm.start_code && addr < init_mm.end_code) + if (addr >= init_mm.start_code && addr < init_mm.end_code) return 1; pgd = pgd_offset_k(addr); diff --git a/objcommon.h b/objcommon.h index b7575a9..7785024 100644 --- a/objcommon.h +++ b/objcommon.h @@ -24,8 +24,11 @@ struct supersect { long get_syms(bfd *abfd, asymbol ***syms_ptr); struct supersect *fetch_supersect(bfd *abfd, asection *sect, asymbol **sympp); -#define starts_with(str, prefix) (!strncmp(str, prefix, strlen(prefix))) -#define ends_with(str, suffix) (strlen(str) > strlen(suffix) && !strcmp(&str[strlen(str)-strlen(suffix)], suffix)) +#define starts_with(str, prefix) \ + (strncmp(str, prefix, strlen(prefix)) == 0) +#define ends_with(str, suffix) \ + (strlen(str) >= strlen(suffix) && \ + strcmp(&str[strlen(str) - strlen(suffix)], suffix) == 0) int label_offset(const char *sym_name); const char *only_label(const char *sym_name); -- 2.11.4.GIT