2

I am following this tutorial, Lesson 22.
I am doing so on Ubuntu 19.10 x86_64 with NASM version 2.14.02.
It is suppose to create a file, readme.txt and set 777 permissions on it (-rwxrwxrwx).

    mov     ecx, 0777           ; set all permissions to read, write, execute
    mov     ebx, filename       ; filename we will create
    mov     eax, 8              ; invoke SYS_CREAT (kernel opcode 8)
    int     80h                 ; call the kernel

I am using the compile/link command as indicated in the above tutorial :

; Compile with: nasm -f elf create.asm
; Link with (64 bit systems require elf_i386 option): ld -m elf_i386 create.o -o create
; Run with: ./create

When running the create program, the file is created but permissions are not 777 but :

-r----x--t 1 cptam cptam    0 Apr  5 21:41 readme.txt

I can't figure out why the 0777 becomes, if I am not mistaken, 1510. Could anyone explain me how to, correctly set the permissions on a file?

Thanks!

Hunkerjr
  • 83
  • 1
  • 6
  • 2
    The tutorial is wrong: you need `0o777` instead of `0777`. NASM doesn't recognise numbers as octal just because they begin with a zero. You must use `0o` to introduce an octal number. – fuz Apr 06 '20 at 02:16
  • Or `777o` or `777q` or `0q777` in addition. – David C. Rankin Apr 06 '20 at 02:26
  • 1
    Does this answer your question? [How to represent octal numbers in Assembly?](https://stackoverflow.com/questions/39564402/how-to-represent-octal-numbers-in-assembly) – David C. Rankin Apr 06 '20 at 02:26
  • @DavidC.Rankin That question doesn't specify an assembler; it's not really suitable as a duplicate. – fuz Apr 06 '20 at 02:31
  • Ok, I guess you are correct on the question itself, the 2nd answer is NASM specific. (I retracted close vote, but left the link) – David C. Rankin Apr 06 '20 at 02:46
  • You can use `strace ./creat` to decode the args to the system call you make. You could have then used GDB to see that `mov ecx, 0777` wasn't putting the number you expected into a register. – Peter Cordes Apr 06 '20 at 07:03
  • @fuz: Both answers on that question are NASM-compatible, and mention NASM explicitly. I think it works as a duplicate. I'll hold off on closing it unilaterally in case you don't find that convincing. – Peter Cordes Apr 06 '20 at 07:06
  • 1
    @DavidC.Rankin You are right, strace ./creat is really helpful! I can cleary see the ecx was not having the expected value. The same with gdb. 2 new tools added in my assembly journey. Thanks a lot! – Hunkerjr Apr 06 '20 at 10:20
  • @Hunkerjr That was Peter Cordes that deserves the thanks there. (there are questions on whether he is human or an advanced AI bot with knowledge of all things assembly) Either way, he is the one to thank. – David C. Rankin Apr 06 '20 at 14:53

1 Answers1

3

Unlike C, a 0-prefix alone doesn't imply an octal constant in NASM

Consequently, ecx is set to 0o1411 (777 in decimal) before the creat syscall. You can address this with an appropriate octal specifier on the mode constant:

    mov     ecx, 0o777          ; set all permissions to read, write, execute
zetavolt
  • 2,989
  • 1
  • 23
  • 33
  • It would be great if you could expand your answer a little. Answers should be able to stand on their own, even if external links go down. – fuz Apr 06 '20 at 02:30
  • 1
    Perhaps also worth mentioning that `creat` and `open(O_CREAT)` respect `umask`, so you wouldn't actually get `-rwxrwxrwx` unless your system / shell environment is configured very strangely. – Peter Cordes Apr 06 '20 at 07:08
  • Hmm strange. I did read that permissions are in octal and tried ```777q``` and ```0q777``` but was getting some strange permissions. That was from the [Nasm Doc](https://www.nasm.us/doc/nasmdoc3.html#section-3.4.1). It does work correcly with ```0o777 ``` which creates a file as 755, as indicated by @PeterCordes – Hunkerjr Apr 06 '20 at 10:00
  • @Hunkerjr: I tried `mov ecx, 777q` and `mov ecx, 0q777` in a file by itself with NASM, and they did both assemble to `mov reg, 0x1ff` aka `511`, which is octal 0o777. If you tried those and they didn't work, you were doing something else wrong as well. – Peter Cordes Apr 06 '20 at 10:09