Environment variables

The environment block

After the argv array and its NULL terminator, the stack holds the environment: an array of pointers to strings of the form KEY=VALUE, also terminated by NULL:

pop rdi ; argc lea rdx, [rsp + rdi*8 + 8] ; envp: past argv[argc] and its NULL

Finding a variable

To find a variable, walk the environment pointers, and for each string compare the leading bytes against the key plus the equals sign. The value is the rest of the string after the equals sign:

mov rsi, [rdx] ; one environment string add rdx, 8 ; next pointer ; compare rsi against "HOME=" ; value = rsi + 5

The KEY=VALUE format

The strings are not arrays: they are C strings, so the value runs to the next NUL byte. Print it with sys_write after scanning for that NUL to learn its length. This complete program prints the value of MYVAR:

section .text global _start _start: pop rdi ; argc lea rbx, [rsp + rdi*8 + 8] ; envp next: mov rsi, [rbx] ; one environment string test rsi, rsi jz done ; NULL ends the block cmp dword [rsi], "MYVA" jne skip cmp byte [rsi + 5], "=" ; the full key is MYVAR= jne skip lea rsi, [rsi + 6] ; the value starts here len: cmp byte [rsi + rdx], 0 je print inc rdx jmp len print: mov rax, 1 ; sys_write mov rdi, 1 ; stdout syscall jmp done skip: add rbx, 8 ; next pointer jmp next done: mov rax, 60 ; sys_exit xor rdi, rdi syscall

The command sets the variable for this run, so the output does not depend on the shell:

nasm -f elf64 -o env.o env.asm
ld -o env env.o
MYVAR=assembler ./env
echo $?

assembler
0

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Where does the environment block start on the stack at program start?

    Answer

    Right after the NULL that ends the argv array

    The argv pointers, their NULL and then the environment pointers are pushed together.

  2. How does an environment string spell a variable?

    Answer

    KEY=VALUE

    The name, an equals sign and the value form one NUL-terminated string.

  3. What ends the list of environment pointers?

    Answer

    A NULL pointer

    The array is NULL-terminated, so a zero pointer marks the end.

  4. What does lea rbx, [rsp + rdi*8 + 8] compute when rdi holds argc?

    Answer

    The address of the first environment pointer

    Eight bytes per argv pointer, plus one more for the NULL, reach the environment array.

  5. What does this program print when it runs as MYVAR=assembler ./env? section .text global _start _start: pop rdi lea rbx, [rsp + rdi*8 + 8] next: mov rsi, [rbx] test rsi, rsi jz done cmp dword [rsi], "MYVA" jne skip cmp byte [rsi + 5], "=" jne skip lea rsi, [rsi + 6] xor rdx, rdx len: cmp byte [rsi + rdx], 0 je print inc rdx jmp len print: mov rax, 1 mov rdi, 1 syscall jmp done skip: add rbx, 8 jmp next done: mov rax, 60 xor rdi, rdi syscall

    Answer

    assembler

    The program skips the key and its equals sign and prints the value.

  6. Why does the program compare dword [rsi] with "MYVA" and then check byte 5?

    Answer

    To skip a variable whose name starts with MYVA but is not MYVAR

    The first comparison is only the start of the key; the equals sign proves the full name.

  7. What follows the value of an environment variable?

    Answer

    A NUL byte

    Each value is the rest of a C string, which ends at the first zero byte.

  8. What does test rsi, rsi test in the loop over the environment?

    Answer

    Whether the pointer is NULL

    test sets the zero flag when the pointer is zero, which ends the loop.

  9. In the program on this page, why does the command set MYVAR instead of relying on the shell?

    Answer

    Because the environment may not contain MYVAR

    A variable the shell does not export would make the output empty; setting it makes the run reproducible.

  10. What does lea rsi, [rsi + 6] point at?

    Answer

    The value after MYVAR=

    Six bytes are the five letters of MYVAR and the equals sign, so the pointer moves to the value.

The short snippets on this page are fragments: they show the environment block, not a whole program. The complete program was assembled with NASM 3.01, linked with GNU ld 2.45.1, and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev