Le 06/07/2013 17:42, Kenneth R Westerback a écrit :
> On Sat, Jul 06, 2013 at 05:21:31PM +0200, Maxime Villard wrote:
>> Hi,
>> - - - - sys/kern/exec_elf.c l.236 ~> 251&252
>> Are my code scanner and me wrong, or 'bdiff' may not be
>> initialized ?
>>
>
> Codewise it does look possible that bdiff will be used uninitialized.
> Whether it can happen in reality depends on whether ph->p_align can ever
> be < 1. Next question -- what would the correct value for bdiff be in that
<= 1
> case? 0? i.e. should the line be 'diff = bdiff = 0;'.
>
> .... Ken
>
Hum hum, after investigating a bit, and from what I understood - if I'm
not mistaken, I think it would make sense if bdiff was set to
bdiff = ph->p_vaddr - trunc_page(ph->p_vaddr);
Since we are supposed to align only on 2^n boundaries, if we get 0 or 1
we do not align on p_align. But p_vaddr still has to be aligned on
PAGE_MASK (with trunc_page()); I read somewhere that ELF loaders are
smart enough to adjust the address when it does not exactly fit a page
boundary. So bdiff should be the difference with the original p_vaddr.
Actually, bdiff is already set to this value in the other conditions.
There's another problem, 'base' should also be initialized here. I would
say that is should be set to the truncated p_vaddr plus the address at
which we want to load:
base = *addr + trunc_page(ph->p_vaddr);
but I'm not sure at all.
Anyway, here is a patch. With these changes, 'uaddr' becomes useless,
so I removed it.
...Ok?
Index: exec_elf.c
===================================================================
RCS file: /cvs/src/sys/kern/exec_elf.c,v
retrieving revision 1.93
diff -u -r1.93 exec_elf.c
--- exec_elf.c 4 Jul 2013 17:37:05 -0000 1.93
+++ exec_elf.c 14 Aug 2013 18:31:37 -0000
@@ -215,7 +215,7 @@
ELFNAME(load_psection)(struct exec_vmcmd_set *vcset, struct vnode *vp,
Elf_Phdr *ph, Elf_Addr *addr, Elf_Addr *size, int *prot, int flags)
{
- u_long uaddr, msize, lsize, psize, rm, rf;
+ u_long msize, lsize, psize, rm, rf;
long diff, offset, bdiff;
Elf_Addr base;
@@ -229,19 +229,16 @@
/* page align vaddr */
base = *addr + trunc_page(ph->p_vaddr)
- ELF_TRUNC(ph->p_vaddr, ph->p_align);
-
- bdiff = ph->p_vaddr - trunc_page(ph->p_vaddr);
-
} else
diff = 0;
} else {
- *addr = uaddr = ph->p_vaddr;
+ *addr = ph->p_vaddr;
if (ph->p_align > 1)
- *addr = ELF_TRUNC(uaddr, ph->p_align);
- base = trunc_page(uaddr);
- bdiff = uaddr - base;
- diff = uaddr - *addr;
+ *addr = ELF_TRUNC(*addr, ph->p_align);
+ base = trunc_page(ph->p_vaddr);
+ diff = ph->p_vaddr - *addr;
}
+ bdiff = ph->p_vaddr - trunc_page(ph->p_vaddr);
*prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
*prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;