lkml.org 
[lkml]   [2005]   [Jan]   [27]   [last100]   RSS Feed
Views: [more markup]  [less markup]  [headers]  [forward] 
 
Messages in this thread
/
From(H. Peter Anvin)
SubjectRe: Patch 4/6 randomize the stack pointer
DateFri, 28 Jan 2005 00:10:23 +0000 (UTC)
Followup to:  <41F94462.7080108@francetelecom.REMOVE.com>
By author:    Julien TINNES <julien.tinnes.NOSPAM@francetelecom.REMOVE.com>
In newsgroup: linux.dev.kernel
>
> Not very important but ((get_random_int() % 4096) << 4) could be 
> optimized into get_random_int() & 0xFFF0. Because 4096 is a power of 2 
> you won't loose any entropy by doing  & 0xFFF instead of %4096
> 

.. and gcc knows that.

: tazenda 8 ; cat testme.c
extern unsigned int get_random_int(void);

unsigned int test(void)
{
  return (get_random_int() % 4096) << 4;
}
: tazenda 9 ; objdump -dr testme.o
testme.o:     file format elf64-x86-64

Disassembly of section .text:

0000000000000000 <test>:
   0:   48 83 ec 08             sub    $0x8,%rsp
   4:   e8 00 00 00 00          callq  9 <test+0x9>
                        5: R_X86_64_PC32
   get_random_int+0xfffffffffffffffc
   9:   25 ff 0f 00 00          and    $0xfff,%eax
   e:   48 83 c4 08             add    $0x8,%rsp
  12:   c1 e0 04                shl    $0x4,%eax
  15:   c3                      retq
: tazenda 10 ; gcc -m32 -O2 -fomit-frame-pointer -g -c testme.c
: tazenda 11 ; objdump -dr testme.o
testme.o:     file format elf32-i386

Disassembly of section .text:

00000000 <test>:
   0:   83 ec 0c                sub    $0xc,%esp
   3:   e8 fc ff ff ff          call   4 <test+0x4>
                        4: R_386_PC32   get_random_int
   8:   25 ff 0f 00 00          and    $0xfff,%eax
   d:   83 c4 0c                add    $0xc,%esp
  10:   c1 e0 04                shl    $0x4,%eax
  13:   c3                      ret
: tazenda 12 ;
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
\
 
 \ /
  Last update: 2005-03-22 14:09    [from the cache]
©2003-2008