Re: NX bit versus standard permissions?

From: Matt (spamtrap_at_crayne.org)
Date: 09/15/04

  • Next message: Matt: "Re: piplining principles (and confusion!)"
    Date: Wed, 15 Sep 2004 19:29:43 +0000 (UTC)
    
    

    "Bob Masta" <NoSpam@daqarta.com> wrote in message
    news:4146dc6a.540273@news.itd.umich.edu...
    > On Mon, 13 Sep 2004 15:33:15 +0000 (UTC), Ivan Korotkov
    > <spamtrap@crayne.org> wrote:
    >
    >>> I'm getting really confused here! What are those
    >>> IMAGE_SCN_MEM_EXECUTE,
    >>> IMAGE_SCN_MEM_READ, and IMAGE_SCN_MEM_WRITE
    >>> flags in the PE header 'Characteristics' section for? If they don't
    >>> control access to the sections, then why do I need to
    >>> set them properly in order to include data and code
    >>> together in one section?
    >>
    >>No, you seem to miss the idea. Data and code are not included in one
    >>section. Section is not a segment neither a page. It is a COFF term that
    >>stands for a series of pages with similar protection and content. .data
    >>has MEM_READ | MEM_WRITE flags, i.e. all pages which this section is
    >>mapped to will have RW(+NX if any) flags. .text (.code) has MEM_READ |
    >>MEM_EXEC, i.e. it's pages will be given RX permission. The same flags are
    >>available for VirtualProtect(). When CPU has no NX bit, MEM_EXEC flag is
    >>useless - it's just ignored. But it should always be set for pages in code
    >>section for future compatibility.
    >
    > Sorry if I'm being dense here, but I still don't get it!
    [...]

    Since Windows 2000 and possibly prior, Windows has defined 8 page
    permissions for VirtualProtect (not counting modifies like PAGE_GUARD):
    PAGE_NOACCESS, PAGE_READONLY, PAGE_READWRITE, PAGE_WRITECOPY, PAGE_EXECUTE,
    PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, and PAGE_EXECUTE_WRITECOPY.
    They're fairly self-explanitory. You can simplify it and think of it as an
    orthogonal RWX model of permission bits even though notably W/WX are not
    supported combinations of bits. Also, you can ignore the write-copy flags;
    they're used only with shared memory -- on forked processes, and on certain
    file mapping objects (NT section objects).

    Now, if the CPU has no way to enforce NX, the execute permission is
    effectively ignored. As others have stated, x86 CPUs could *only* enforce
    execution permissions through the cs segment, but since cs is flat on most
    systems, there is no way to prevent execution of arbitrary code. That
    changes on CPUs with NX.

    The flags on the COFF sections determine the bits set on the pages in that
    section at runtime. If the COFF section is marked as RW, then the page gets
    PAGE_WRITECOPY. If it is marked as RX, it gets PAGE_EXECUTE_READ.
    Fortunately tools have been good about setting the execute bit in the code
    section, so binaries are generally forward-compatible.

    For you, you can continue leaving the code section as RWX and it will work
    just fine on NX CPUs. The memory will be marked PAGE_EXECUTE_WRITECOPY. You
    can write into it and it will become PAGE_EXECUTE_READWRITE. It will still
    be executable. If, however, you did not mark it as RWX and had instead used
    RW, then it would fault as soon as the first instruction executed.

    -Matt


  • Next message: Matt: "Re: piplining principles (and confusion!)"

    Relevant Pages

    • Re: Where do the gains of OOO architecture actually take effect?
      ... I recall seeing a dramatic performance increase on EV6. ... CPUs' hardware and allow the fairest comparison, ... probably execute very rarely. ... the scheduling of the binary's instruction stream onto one of the CPUs. ...
      (comp.arch)
    • Re: Buffer overflow prevention
      ... >> for something that has both write and execute permission. ... > addresses in either the code or data/stack segment. ... reach into the stack area at the end of memory. ... unless other protection techologies such as ProPolice are used to ...
      (Bugtraq)
    • Re: xp_cmdShell
      ... checkbox under SQL Server Agent properties --> Job ... System) and specify a Windows account for the SQL Agent proxy with the ... > I thought if he has execute permission for my sp, which owner is DBO, that ... If I set the execute permission for 'xp_cmdshell' to my read user I get the ...
      (microsoft.public.sqlserver.programming)
    • Re: Hi folks, I wrote a program for fun and learn some concepts. I suppose it is able to execute bin
      ... >>protection hardware. ... > a flat memory model. ... segment selectors have strong types. ... to DS before I try to execute that piece of code? ...
      (comp.os.linux.development.system)
    • Re: [PATCH] kprobes for s390 architecture
      ... Since all of these are relative branches, and they don't save the psw, the ... memory on multiple CPUs is the answer. ... If the instruction is brought into the pipeline while it is being changed ... on another cpu it will either execute the original instruction and that probe ...
      (Linux-Kernel)

    Loading