【问题标题】:File permissions in Linux assemblyLinux 程序集中的文件权限
【发布时间】:2015-05-07 00:00:09
【问题描述】:

我正在尝试获取有关文件权限的信息。我正在使用 sys_access 系统调用。这是我的代码 sn-p:

mov eax, 33 
mov ebx, fileName 
mov ecx, 1 
int 80h 

cmp eax, 0 
jl .error 

如果eax 为-1,则表示错误,我没有得到错误,但我需要检查文件的所有权限(所有者、组、其他)。我该怎么做?

【问题讨论】:

  • access(2) 不返回文件权限。它测试当前进程是否具有第二个参数中指定的权限。
  • 要获得权限,请使用stat(2)。它返回一个包含文件信息的结构,包括权限模式。
  • 谢谢,它解决了我的问题。现在我正在尝试使用 chmod() 更改这些权限,但是当我使用我在这里找到的标签设置 mod 时:[link] (tutorialspoint.com/unix_system_calls/chmod.htm),它们有点不像我想要的广告。当我将 ecx 设置为 00400 | 00200 它将所有者设置为可以读写,并且(不知道为什么)将访客设置为可以写入。虽然当我使用 00400 | 00200 | 00040 其集既读又写。你能帮我解决这个问题吗?
  • 尝试为此提出一个新问题。
  • 嗯,基本上所有内容都是一样的(几乎 - 即标题),所以我认为这不是一个好的选择。

标签: linux assembly permissions system-calls


【解决方案1】:

你可以使用内核函数sys_newstat(第106号-看this table)来获取文件权限。结构 stat 是一个永无止境的恐怖,但以下示例至少适用于我的 Debian Wheezy 64 位(NASM、32 位和 64 位模式):

SECTION .data

    filename        db '/root'          ; Just an example, can be replaced with any name
    filename_len    equ $ - filename    ; Length of filename
                    db 0                ; Terminator for `Int 80h / EAX = 106`
    perm_out        db 'Permissions: '
    perm            db 'drwxrwxrwx'
    perm_len        equ $ - perm        ; Index of last character in `perm`
    lf              db 10
    perm_out_len    equ $ - perm_out    ; Length of `Permissions: ...\n`

SECTION .bss
    stat resb 256               ; Way too much, but size is variable depending on OS

SECTION .text
global _start

_start:

    mov eax,4                   ; sys-out
    mov edx,filename_len        ; length of string to print
    mov ecx,filename            ; Pointer to string
    mov ebx,1                   ; StdOut
    int 0x80                    ; Call kernel

    mov eax,4                   ; sys-out
    mov edx,1                   ; Length of string to print
    mov ecx, lf                 ; Pointer to string
    mov ebx,1                   ; StdOut
    int 0x80                    ; Call kernel

    mov eax, 106                ; sys_newstat
    mov ebx, filename           ; Pointer to ASCIIZ file-name
    mov ecx, stat               ; Pointer to structure stat
    int 80h

    test eax, eax
    jz .noerr
    mov eax,1                   ; sys_exit
    mov ebx,1                   ; Exit code, 1=not normal
    int 0x80                    ; Call kernel
    .noerr:

    movzx eax, word [stat + 8]  ; st_mode (/usr/include/asm/stat.h)
    mov ebx, perm_len

    ; rwx bits
    mov ecx, 9
    .L1:
    sub ebx, 1
    shr eax, 1
    jc .J1
    mov byte [perm + ebx], '-'
    .J1:
    loop .L1

    ; directory bit
    sub ebx, 1
    shr eax, 6
    jc .J2
    mov byte [perm + ebx], '-'
    .J2:

    mov eax,4                   ; sys-out
    mov edx,perm_out_len        ; Length of string to print
    mov ecx,perm_out            ; Pointer to string
    mov ebx,1                   ; StdOut
    int 0x80                    ; Call kernel

    mov eax,1                   ; sys_exit
    mov ebx,0                   ; Exit code, 0=normal
    int 0x80                    ; Call kernel

【讨论】:

  • 感谢您的回答,我已经设法让 stat() 正常工作。现在我遇到了上面评论中提到的问题。现在我想更改该文件的权限(请在 cmets 中查找)。
  • @TreeZ:问一个新问题,告诉我们汇编器、链接器和 Linux 版本,并显示一个MCVE - 也许我明天会处理它;-)
猜你喜欢
  • 2015-11-04
  • 2012-08-06
  • 1970-01-01
  • 1970-01-01
  • 2016-01-28
  • 1970-01-01
  • 2012-12-09
  • 2020-12-02
相关资源
最近更新 更多