Skip to content
This repository was archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
Avoid possible overflows from signed/unsigned comparisons
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Horman <[email protected]>
  • Loading branch information
horms committed Feb 2, 2010
1 parent 48f3ad6 commit 3d6c264
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 35 deletions.
3 changes: 2 additions & 1 deletion kdump/kdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ static void *generate_new_headers(

static void write_all(int fd, const void *buf, size_t count)
{
ssize_t result, written = 0;
ssize_t result;
size_t written = 0;
const char *ptr;
size_t left;
ptr = buf;
Expand Down
3 changes: 2 additions & 1 deletion kexec/arch/i386/crashdump-x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ static void ultoa(unsigned long i, char *str)
* memory regions the new kernel can use to boot into. */
static int cmdline_add_memmap(char *cmdline, struct memory_range *memmap_p)
{
int i, cmdlen, len, min_sizek = 100;
int i, cmdlen, len;
unsigned long min_sizek = 100;
char str_mmap[256], str_tmp[20];

/* Exact map */
Expand Down
5 changes: 3 additions & 2 deletions kexec/arch/i386/kexec-multiboot-x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ int multiboot_x86_load(int argc, char **argv, const char *buf, off_t len,
struct AddrRangeDesc *mmap;
int command_line_len;
int i;
uint32_t u;
int opt;
int modules, mod_command_line_space;
#define OPT_CL (OPT_ARCH_MAX+0)
Expand Down Expand Up @@ -375,8 +376,8 @@ int multiboot_x86_load(int argc, char **argv, const char *buf, off_t len,
/* Relocate offsets in the MBI to absolute addresses */
mbi_offset = mbi_base;
modp = ((void *)mbi) + mbi->mods_addr;
for (i=0; i<mbi->mods_count; i++) {
modp[i].cmdline += mbi_offset;
for (u = 0; u < mbi->mods_count; u++) {
modp[u].cmdline += mbi_offset;
}
mbi->mods_addr += mbi_offset;
mbi->cmdline += mbi_offset;
Expand Down
2 changes: 1 addition & 1 deletion kexec/arch/ia64/crashdump-ia64.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static int seg_comp(const void *a, const void *b)
*/
static void add_loaded_segments_info(struct mem_ehdr *ehdr)
{
int i;
unsigned i;
for(i = 0; i < ehdr->e_phnum; i++) {
struct mem_phdr *phdr;
phdr = &ehdr->e_phdr[i];
Expand Down
2 changes: 1 addition & 1 deletion kexec/arch/ia64/kexec-elf-ia64.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void elf_ia64_usage(void)
*/
void move_loaded_segments(struct mem_ehdr *ehdr, unsigned long addr)
{
int i;
unsigned i;
long offset;
struct mem_phdr *phdr;
for(i = 0; i < ehdr->e_phnum; i++) {
Expand Down
7 changes: 4 additions & 3 deletions kexec/arch/ia64/kexec-ia64.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ int get_memory_ranges(struct memory_range **range, int *ranges,
while(fgets(line, sizeof(line), fp) != 0) {
unsigned long start, end;
char *str;
int type;
unsigned type;
int consumed;
int count;
if (memory_ranges >= max_memory_ranges)
Expand Down Expand Up @@ -219,13 +219,14 @@ int arch_compat_trampoline(struct kexec_info *UNUSED(info))
int update_loaded_segments(struct mem_ehdr *ehdr)
{
int i;
unsigned u;
struct mem_phdr *phdr;
unsigned long start_addr = ULONG_MAX, end_addr = 0;
unsigned long align = 1UL<<26; /* 64M */
unsigned long start, end;

for (i = 0; i < ehdr->e_phnum; i++) {
phdr = &ehdr->e_phdr[i];
for (u = 0; u < ehdr->e_phnum; u++) {
phdr = &ehdr->e_phdr[u];
if (phdr->p_type != PT_LOAD)
continue;
if (phdr->p_paddr < start_addr)
Expand Down
5 changes: 3 additions & 2 deletions kexec/arch/ppc64/crashdump-ppc64.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ static int get_dyn_reconf_crash_memory_ranges(void)
uint64_t start, end;
char fname[128], buf[32];
FILE *file;
int i, n;
unsigned int i;
int n;
uint32_t flags;

strcpy(fname, "/proc/device-tree/");
Expand Down Expand Up @@ -450,7 +451,7 @@ int load_crashdump_segments(struct kexec_info *info, char* mod_cmdline,

void add_usable_mem_rgns(unsigned long long base, unsigned long long size)
{
int i;
unsigned int i;
unsigned long long end = base + size;
unsigned long long ustart, uend;

Expand Down
25 changes: 17 additions & 8 deletions kexec/arch/ppc64/fs2dt.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ static void add_dyn_reconf_usable_mem_property(int fd)
uint64_t buf[32];
uint64_t ranges[2*MAX_MEMORY_RANGES];
uint64_t base, end, loc_base, loc_end;
int range, rlen = 0, i;
int rngs_cnt, tmp_indx;
size_t i, rngs_cnt, range;
int rlen = 0;
int tmp_indx;

strcpy(fname, pathname);
bname = strrchr(fname, '/');
Expand Down Expand Up @@ -189,13 +190,15 @@ static void add_dyn_reconf_usable_mem_property(int fd)
dt += (rlen + 3)/4;
}

static void add_usable_mem_property(int fd, int len)
static void add_usable_mem_property(int fd, size_t len)
{
char fname[MAXPATH], *bname;
uint64_t buf[2];
uint64_t ranges[2*MAX_MEMORY_RANGES];
uint64_t base, end, loc_base, loc_end;
int range, rlen = 0;
size_t range;
int rlen = 0;
ssize_t slen;

strcpy(fname, pathname);
bname = strrchr(fname,'/');
Expand All @@ -206,12 +209,12 @@ static void add_usable_mem_property(int fd, int len)

if (len < 2 * sizeof(uint64_t))
die("unrecoverable error: not enough data for mem property\n");
len = 2 * sizeof(uint64_t);
slen = 2 * sizeof(uint64_t);

if (lseek(fd, 0, SEEK_SET) < 0)
die("unrecoverable error: error seeking in \"%s\": %s\n",
pathname, strerror(errno));
if (read(fd, buf, len) != len)
if (read(fd, buf, slen) != slen)
die("unrecoverable error: error reading \"%s\": %s\n",
pathname, strerror(errno));

Expand Down Expand Up @@ -263,7 +266,9 @@ static void add_usable_mem_property(int fd, int len)
static void putprops(char *fn, struct dirent **nlist, int numlist)
{
struct dirent *dp;
int i = 0, fd, len;
int i = 0, fd;
size_t len;
ssize_t slen;
struct stat statbuf;

for (i = 0; i < numlist; i++) {
Expand Down Expand Up @@ -324,9 +329,13 @@ static void putprops(char *fn, struct dirent **nlist, int numlist)
die("unrecoverable error: could not open \"%s\": %s\n",
pathname, strerror(errno));

if (read(fd, dt, len) != len)
slen = read(fd, dt, len);
if (slen < 0)
die("unrecoverable error: could not read \"%s\": %s\n",
pathname, strerror(errno));
if ((size_t)slen != len)
die("unrecoverable error: short read from\"%s\"\n",
pathname);

checkprop(fn, dt, len);

Expand Down
2 changes: 1 addition & 1 deletion kexec/arch/ppc64/kexec-elf-ppc64.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ int elf_ppc64_load(int argc, char **argv, const char *buf, off_t len,
size_t size;
uint64_t *rsvmap_ptr;
struct bootblock *bb_ptr;
unsigned int i;
int i;
int result, opt;
uint64_t my_kernel, my_dt_offset;
unsigned int my_panic_kernel;
Expand Down
3 changes: 2 additions & 1 deletion kexec/arch/ppc64/kexec-ppc64.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ static int get_dyn_reconf_base_ranges(void)
uint64_t start, end;
char fname[128], buf[32];
FILE *file;
int i, n;
unsigned int i;
int n;

strcpy(fname, "/proc/device-tree/");
strcat(fname, "ibm,dynamic-reconfiguration-memory/ibm,lmb-size");
Expand Down
4 changes: 2 additions & 2 deletions kexec/arch/sh/kexec-zImage-sh.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ int zImage_sh_load(int argc, char **argv, const char *buf, off_t len,
struct kexec_info *info)
{
char *command_line;
int opt, k;
unsigned long empty_zero, zero_page_base, zero_page_size;
int opt;
unsigned long empty_zero, zero_page_base, zero_page_size, k;
unsigned long image_base;
char *param;

Expand Down
3 changes: 2 additions & 1 deletion kexec/arch/x86_64/crashdump-x86_64.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,8 @@ static void ultoa(unsigned long i, char *str)
* memory regions the new kernel can use to boot into. */
static int cmdline_add_memmap(char *cmdline, struct memory_range *memmap_p)
{
int i, cmdlen, len, min_sizek = 100;
int i, cmdlen, len;
unsigned long min_sizek = 100;
char str_mmap[256], str_tmp[20];

/* Exact map */
Expand Down
2 changes: 1 addition & 1 deletion kexec/kexec-elf-exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int elf_exec_load(struct mem_ehdr *ehdr, struct kexec_info *info)
{
unsigned long base;
int result;
int i;
size_t i;

if (!ehdr->e_phdr) {
fprintf(stderr, "No program header?\n");
Expand Down
11 changes: 4 additions & 7 deletions kexec/kexec-elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,7 @@ static int build_mem_elf64_phdr(const char *buf, struct mem_ehdr *ehdr, int idx)
static int build_mem_phdrs(const char *buf, off_t len, struct mem_ehdr *ehdr,
uint32_t flags)
{
size_t phdr_size, mem_phdr_size;
int i;
size_t phdr_size, mem_phdr_size, i;

/* e_phnum is at most 65535 so calculating
* the size of the program header cannot overflow.
Expand Down Expand Up @@ -582,8 +581,7 @@ static int build_mem_elf64_shdr(const char *buf, struct mem_ehdr *ehdr, int idx)
static int build_mem_shdrs(const char *buf, off_t len, struct mem_ehdr *ehdr,
uint32_t flags)
{
size_t shdr_size, mem_shdr_size;
int i;
size_t shdr_size, mem_shdr_size, i;

/* e_shnum is at most 65536 so calculating
* the size of the section header cannot overflow.
Expand Down Expand Up @@ -636,7 +634,7 @@ static int build_mem_shdrs(const char *buf, off_t len, struct mem_ehdr *ehdr,
&& (shdr->sh_offset + shdr->sh_size) > len) {
/* The section does not fit in the buffer */
if (probe_debug) {
fprintf(stderr, "ELF section %d not in file\n",
fprintf(stderr, "ELF section %zd not in file\n",
i);
}
return -1;
Expand Down Expand Up @@ -666,8 +664,7 @@ static void read_nhdr(const struct mem_ehdr *ehdr,
static int build_mem_notes(struct mem_ehdr *ehdr)
{
const unsigned char *note_start, *note_end, *note;
size_t note_size;
int i;
size_t note_size, i;
/* First find the note segment or section */
note_start = note_end = NULL;
for(i = 0; !note_start && (i < ehdr->e_phnum); i++) {
Expand Down
2 changes: 1 addition & 1 deletion kexec/lzma.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ LZFILE *lzopen(const char *path, const char *mode)
int lzclose(LZFILE *lzfile)
{
lzma_ret ret;
int n;
size_t n;

if (!lzfile)
return -1;
Expand Down
2 changes: 1 addition & 1 deletion purgatory/arch/ia64/purgatory-ia64.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ patch_efi_memmap(struct kexec_boot_params *params,
uint64_t orig_type;
efi_memory_desc_t *src_md, *dst_md;
void *src_end = src + boot_param->efi_memmap_size;
int i;
unsigned long i;
for (; src < src_end; src += boot_param->efi_memdesc_size,
dest += boot_param->efi_memdesc_size) {
unsigned long mstart, mend;
Expand Down
2 changes: 1 addition & 1 deletion purgatory/purgatory.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void verify_sha256_digest(void)
{
struct sha256_region *ptr, *end;
sha256_digest_t digest;
int i;
size_t i;
sha256_context ctx;
sha256_starts(&ctx);
end = &sha256_regions[sizeof(sha256_regions)/sizeof(sha256_regions[0])];
Expand Down

0 comments on commit 3d6c264

Please sign in to comment.