0

I'm learning file I/O in MacOS [Monterey] x86-64 assembly. [Assembling with nasm, using Mach-O 64 output.] The example program I've been adapting for Mac from this textbook is producing strange output. The root of the problem seems to be that this function fragment, in the source file "file.asm":

global createFile
createFile:                       ; fileName testfile.txt already in rdi
    mov     rax,      0x02000005
    mov     rsi,      0x0202
    mov     rdx,      0777
    syscall
    

can create the expected file "testfile.txt", and permit the first expected Hello World line to be written, but, unlike the rest of the program expects, it cannot give me or any subsequent functions write permissions to "testfile.txt".

If I run this program as myself, and then try to edit the file it created [testfile.txt], I will get a Permission denied and be told the file is read-only. Likewise, later functions in "file.asm" that are supposed to be able to write to "testfile.txt" will fail, although they don't throw any errors.

But, if I run the program once as myself, and then a second time as sudo, then and only then will a subsequent function which is supposed to be able to overwrite "testfile.txt", succeed.

I've tried appending

    mov     rdi,       fileName
    mov     rax,       0x0200000f  ;[BSD/MacOS syscall for chmod]
    mov     rsi,       0777
    syscall

to createFile, but it did nothing. Do I need to be working with entire directories when chmod-ing? Is there any way to give myself, and the rest of "file.asm", write access to this file right upon creating it?

zeroclaim
  • 29
  • 1
  • 5
  • `0777` isn't octal in NASM. It's still decimal. MacOS has a way to trace system calls; use it to have it decode your args. (Also use `ls -l` to check the permissions on the file.) – Peter Cordes Jan 21 '22 at 18:04

0 Answers0