Author Topic: Flags for Linux sys_open in 64-bit NASM  (Read 737 times)

Offline paml27

  • Jr. Member
  • *
  • Posts: 27
Flags for Linux sys_open in 64-bit NASM
« on: December 05, 2019, 12:35:24 AM »
I created a file in 64-bit NASM on Linux.  Now I want to open it and write data into it.  The file is created, but no data are written.  My code follows:

File_Name: db '/opt/Test_Output_Files/Linux_File_Test',0
File_Mode: dq 754
Write_Buffer: db 'This is what I want to write',0

; Create file
mov rax,85 ; sys_creat
mov rdi,File_Name
mov rsi,File_Mode  ; mode (permissions)
syscall

mov rax,1 ; sys_open
mov rdi,File_Name
mov rsi,2 ;read-write
mov rdx,754
syscall

mov rax,1 ; sys_write
mov rdi,File_Name
mov rsi,Write_Buffer
mov rdx,29
syscall

mov rax,3 ; sys_close
mov rdi,File_Name
syscall

My questions are:
1.  When I create a file in 64-bit Linux is it open for reading and writing, or do I need to issue an open command as well? 

2.  When I open a file in 64-bit Linux, what are the flags?  According to the Linux man page at https://linux.die.net/man/3/open, there are three options: 

O_RDONLY - Open for reading only.
O_WRONLY - Open for writing only.
O_RDWR - Open for reading and writing. The result is undefined if this flag is applied to a FIFO.

I know that read-only is zero, so I assumed write only and read-write are 1 and 2, but I haven't found any listing of the numeric values that we would use in assembly language, unlike the mode which is based on chmod. 

3.  I am doing all of this as a superuser, not as root.  Any special considerations for file handling by a superuser? 

Thanks very much for any help on this. 


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2400
  • Country: us
Re: Flags for Linux sys_open in 64-bit NASM
« Reply #1 on: December 05, 2019, 04:41:43 AM »
Hi Pam127,
I don't know much 64 bit code, but a few observations...

I think mode is supposed to be octal. C takes a leading zero to be octal, Nasm does not. You probably want "754q".

Code: [Select]
mov rax, 1 ; sys_open
Code: [Select]
mov rax, 1 ; sys_write
These can't both be right.

sys_create and/or sys_open should return a file descriptor in rax. Use that instead of filename from then on.

1) Dunno. What happens?
2) Dunno. I think "2" is correct. What happens?
3) Is there a difference? I don't think so.

Don't forget to exit cleanly when you're done.

Good luck. Let us know how it comes out.

Best,
Frank


Offline paml27

  • Jr. Member
  • *
  • Posts: 27
Re: Flags for Linux sys_open in 64-bit NASM
« Reply #2 on: December 05, 2019, 06:26:10 PM »
Thanks for your reply, Frank.  Yes, sys_open is 2, not 1.  I've tried your other suggestions, but still no luck.  I'm still working on this, so I'll let you know what happens. 


Offline paml27

  • Jr. Member
  • *
  • Posts: 27
Re: Flags for Linux sys_open in 64-bit NASM
« Reply #3 on: December 05, 2019, 07:38:02 PM »
A little more info.  Sys_creat (as shown above) returns a positive value, and sys_write works with that.  But I'm still having problems with sys_open.  The flags may be the problem.  The Linux man pages at http://man7.org/linux/man-pages/man2/open.2.html don't help much, and I haven't found any definitive source yet.  The following code works, but sys_open still does not. 

; Create file
mov rax,85 ; sys_creat
mov rdi,File_Name
mov rsi,File_Mode  ; mode (permissions)
syscall

mov rdi,rax ; return code from sys_open
mov rax,1 ; sys_write
;mov rdi,File_Name
mov rsi,Write_Buffer
mov rdx,29
syscall


Offline paml27

  • Jr. Member
  • *
  • Posts: 27
Re: Flags for Linux sys_open in 64-bit NASM
« Reply #4 on: December 09, 2019, 10:28:27 PM »
The answer to this question is:

; Create file
mov rax,85 ; sys_creat
mov rdi,File_Name
mov rsi,[File_Mode]  ; mode (permissions)
syscall

The File_Mode variable must be passed by value (enclosed on brackets), not by reference, as it was before. 

Offline fredericopissarra

  • Jr. Member
  • *
  • Posts: 58
Re: Flags for Linux sys_open in 64-bit NASM
« Reply #5 on: December 09, 2019, 10:56:21 PM »