r/Assembly_language • • Apr 11 '26

Question Experimentation with capability flags and control flow validation

Is this prototype good?

section .data:

secret db 5, 4

global _start

section .text:

msg db "You have exited successfully", 0

equ length $ - 29

global success

_start:

test rbx, rbx

jz fail

cmp rbx, 10

jl fail

mov ebx, rbx

rol ebx, 5

mov secret, ebx

jmp success

success:

cmp ebx, secret

jne fail

mov rax, 1

mov rdi, 1

mov rsi, msg

mov rdx, length

syscall

jmp end

fail:

mov rax, 60

mov rdi, 0

syscall

end:

mov rax, 60

mov rdi, 0

syscall

0 Upvotes

11 comments sorted by

1

u/vintagecomputernerd Apr 11 '26

You first have to better explain what you're trying to do

-1

u/themagicalfire Apr 11 '26

Basically I’m treating capability systems and control flow integrity as the same thing from a low-level point of view, so I’m trying to perform capabilities and control flow integrity by checking variables or registers.

2

u/Plane_Dust2555 Apr 11 '26

What, exactly, this "capability systems" mean?

2

u/Plane_Dust2555 Apr 11 '26

Ok... maybe this is what you wanted:

; test.asm
;
;   nasm -felf64 -o test.o test.asm
;   ld -s -znoexecstack -o test test.o
;
  bits  64        ; always nice to tell nasm the ISA.
  default rel     ; offset only addresses must be always rip relative.

; To use 64 bits capabilities (2 entries of cap_data structure).
LINUX_CAPS_VER3 equ 0x20080522

CAP_SYS_ADMIN equ (1 << 21)   ; bit 21 from the lower 32 permission bits.

SYS_WRITE   equ 1
SYS_GETPID  equ 39
SYS_EXIT    equ 60
SYS_CAPSGET equ 125

  struc cap_hdr
    .version: resd  1
    .pid:     resd  1
  endstruc

  ; For version 3.
  struc cap_data
    .effective_lo:   resd 1
    .permitted_lo:   resd 1
    .inheritable_lo: resd 1

    .effective_hi:   resd 1
    .permitted_hi:   resd 1
    .inheritable_hi: resd 1
  endstruc

  section .rodata

permitted_msg:
  db  `System admin priviledges granted.\n`
permitted_msg_length equ $ - permitted_msg

not_permitted_msg:
  db  `No system admin priviledges for this user.\n`
not_permitted_msg_length equ $ - not_permitted_msg

caps_fail:
  db  `Failed to get capabilities.\n`
caps_fail_length equ $ - caps_fail

  section .bss

caps:
  resb  cap_data_size

  section .data

hdr:
  istruc cap_hdr
    at cap_hdr.version, dd LINUX_CAPS_VER3
    ; PID can be set to zero to get current process capabilities.
  iend

  section .text

  global _start

_start:
  ; Get the capabilities into data structure.
  mov   eax,SYS_CAPSGET
  lea   rdi,[hdr]
  lea   rsi,[caps]
  syscall
  ; test if fails...
  test  eax,eax
  jnz   .fail

  ; test specific capabilities bit.
  test  dword [caps + cap_data.effective_lo],CAP_SYS_ADMIN
  jz    .nosysadmin

  mov   eax,SYS_WRITE
  mov   edi,eax
  lea   rsi,[permitted_msg]
  mov   edx,permitted_msg_length
  syscall

  xor edi, edi
.exit:
  mov eax, SYS_EXIT
  syscall

  ; Show failure and exit with 1.
  align 4
.nosysadmin:
  mov   eax, SYS_WRITE
  mov   edi, eax          ; 1 = stdout.
  lea   rsi, [not_permitted_msg]
  mov   edx, not_permitted_msg_length
  syscall

  mov   edi,1
  jmp   .exit

  align 4
.fail:
  mov   eax,SYS_WRITE
  mov   edi,eax
  lea   rsi,[caps_fail]
  mov   edx,caps_fail_length
  syscall

  mov   edi,1
  jmp   .exit 

0

u/themagicalfire Apr 11 '26

Oh, I’ll try to explain how I think. There are permissions in operating systems and to perform some operations you need to have permissions as a prerequisite. Capabilities are a form of permissions, but context-specific 🙂. This is what I understand.

2

u/Plane_Dust2555 Apr 11 '26

Yep... ok... what the code above has to do with it?

0

u/themagicalfire Apr 11 '26

Can you see the conditional success message? That’s only if my security conditions work

1

u/Plane_Dust2555 Apr 11 '26

I think this is probably you were aiming at:

; test.asm
;
;   nasm -felf64 -o test.o test.asm
;   ld -s -znoexecstack -o test test.o
;
  bits  64        ; always nice to tell nasm the ISA.
  default rel     ; offset only addresses must be always rip relative.

SYS_WRITE equ 1
SYS_EXIT  equ 60

  ; Data that won't be changed should go to .rodata section.
  section .rodata

msg:
  db  `You have exited successfully.\n`
msg_length equ $ - msg

error_msg:
  db  `Error!\n`
error_msg_length equ $ - error_msg

  ; Data that will be changed (but are not zero initially) should go to .data section.
  section .data

secret:
  db  4, 5, 0, 0    ; or dd 0x0504
secret_length equ $ - secret

  section .text

  ; We only need to export _start.
  global _start

_start:
  ; Test if any of the bytes are less than 10 (unsigned).
  lea   rsi,[secret]                ; load pointer to 'secret'.
  lea   rcx,[rsi + secret_length]   ; points just after 'secret'.

  align 4                           ; doesn't hurt to align code to avoid cache misses.
.loop:                              ; all the other labels are local to _start.
  lodsb
  cmp   al,10
  jae   .fail
  cmp   rsi,rcx
  jne   .loop

  ; Do your magic (why?)
  mov   eax,[secret]
  rol   eax,5
  mov   [secret],eax

  ; Show success and exit with 0.
  ; We don't need to initialize entire R?? registers
  ; but the RSI pointer.
  mov eax, SYS_WRITE
  mov edi, eax            ; 1 = stdout.
  lea rsi, [msg]
  mov edx, msg_length
  syscall

  xor edi, edi
.exit:
  mov eax, SYS_EXIT
  syscall

  ; Show failure and exit with 1.
  align 4
.fail:
  mov   eax, SYS_WRITE
  mov   edi, eax          ; 1 = stdout.
  lea   rsi, [error_msg]
  mov   edx, error_msg_length
  syscall

  mov edi,1
  jmp .exit 

1

u/themagicalfire Apr 11 '26

Thank you for the help but I don’t understand what you updated in terms of security or syntax