2

Recently I realized that you can do this in 64-bit code:

  const size_t kLowStackSize = 1024UL * 1024UL * 4UL;
  void *low_stack = mmap(NULL, kLowStackSize, PROT_READ | PROT_WRITE,
      MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
  struct __attribute__((packed, aligned(16))) {
    int32_t address;
    int16_t segment;
  } target = {(uint32_t) (uint64_t) code, 0x23};
  asm volatile(
      "mov %%rsp, %%r8\n"
      "mov %[stack], %%rsp\n"
      "push %%r8\n"
      "lcall *(%[target])\n"
      "pop %%rsp"
      :
      : [stack] "r" (low_stack + kLowStackSize), [target] "r" (&target)
      : "r8");

where code points to a piece of 32-bit code located on an executable page in the lower 4GiB of the address space, and 0x23 is the value of the __USER32_CS segment selector in Linux's x86 headers. I don't know whether the attributes are necessary for the jump target, but I added the for good measure. Of course, to make the far return possible this calling code itself must be located somewhere in the lower 4 GiB of the virtual address space. I found that placing it into main is sufficient.

I understand this is mostly useless (there are no 32-bit libraries loaded, the calling conventions are different, etc.) and prone to breakage (the value of __USER32_CS is not part of Linux's userspace-facing API).

My question: Is there a simple way to demonstrate the the target of the call is indeed executed in 32-bit mode? Are the any practical uses (existing software of libraries leveraging it, or at least not-so-impractical possibilites) for this kind of call?

2
  • 1
    Simply put some code into your target that executes differently in 64bit and 32bit mode. I'd use the binary code for the 64bit sequence XOR EAX, EAX; DEC RAX; RAX; LRET because that, due to the REX prefix being interpreted as DEC EAX in 32bit mode, evaluates there to XOR EAX, EAX; DEC EAX; DEC EAX; LRET (first DEC from the prefix, second DEC the actual two-byte-opcode) and therefore gives a different result.
    – FrankH.
    Aug 16, 2013 at 12:09
  • That's a really great idea (and actually something I can assemble by hand). Maybe you should post it as an answer. If no one comes up with a reference to some actual software which does a far call between __USER_CS and __USER32_CS in userspace, I think I will accept yours. Aug 16, 2013 at 12:46

1 Answer 1

2

In x86, the 32bit and 64bit instruction encodings are mostly identical.

The big exception to that are the 16 single-byte INC and DEC instruction opcodes. These 16 bytes, in 64bit mode, have been repurposed into the REX prefix family, which allows to specify 64bit operand size as well as the usage of the new registers in 64bit mode.

This means 64bit code like:

    xorl %eax, %eax
    .byte 0x48, 0xff, 0xc8
; this is the same as:
;   decq %rax         ; opcode: 0x48 0xff 0xc8
    lret $0

is valid 32bit code but will there be executed as:

    xorl %eax, %eax
    decl %eax         ; opcode: 0x48
    decl %eax         ; opcode: 0xff 0xc8
    lret $0

So you can ljmp to this piece of code, and test the (32bit) return value; it'll be -1 if executed in 64bit mode but -2 if executed in 32bit mode.

I do not know what the preconditions for far returns from 32bit to 64bit mode are. I suspect you might have to set up both a "low mem" 64bit stack pointer to start with as well as a low-mem 64bit code address "trampoline" (so that both the return EIP and return ESP in the far call frame are 32bit values).

4
  • Thank you for the detailed explanation! I got to mostly the same conclusion after reading your comment. One thing to note -- for anyone else who would like to experiment with this in the future -- is that while setting up a "low mem" stack is neccessary, at least on my machine, there is no need for a separate "trampoline". I believe that if need be, it would be possible to change the load address of .code to somewhere "low mem" with a linker script. However, at least on my setup, it loads there by default. Aug 19, 2013 at 9:40
  • 1
    In 64bit mode, the main executable is mapped at addresses 0x400000 (check the default linker script) and upwards, while shared libraries are mapped at addresses >>2GB. If the function you make an lcall from is in your main executable's text segment, it's pretty much guaranteed < 2GB, but if you move the function to a shared lib, that won't be the case anymore. It depends ...
    – FrankH.
    Aug 19, 2013 at 10:27
  • Oh, I see now. (Altough in any "serious" use, one would need generated "thunks" to convert between calling conventions, e.g. by writing a version of dlsym that expects the function signature and turns in into a thunk located in low mem. Nevertheless, such "serious" use would also need a special dynamic linker and libc, so "impractical" is probably an understatement.) Aug 19, 2013 at 10:34
  • @KristófMarussy: "impractical" depends on your needs ;-) but agreed not at all simple to use, particularly not if you want to make this work together with dynamic linking. Easier interoperability (with no direct need for thunking would be the use of the x32 ABI (sites.google.com/site/x32abi) but that'd need recompiling, and it's still a work in progress. My gut feeling is that a multi-process, shared memory IPC/RPC-based "thunking" solution would be easier to do (yes you can shmat() the same SHM segment to both a 64bit and 32bit app).
    – FrankH.
    Aug 19, 2013 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.