Skip to content

chmod

Intro

chmod - change the permissions of a file

Description

The chmod() system call is used to change the permission of a given file path, determined by the parameter pathname. The permission bits of a file are set using the second parameter of the call, mode.

The permission are set base on the user or group that own the file, setuid and setgid bit and matrix of user, group and other. For more detail the man page of chmod should be consulted.

Arguments

  • pathname:const char*[U,TOCTOU] - Path to the file whose permission bits are to be changed.
  • mode:mode_t[K] - A bitmask of permission bits that will be used to set the new permission.
* `S_ISUID (04000)`: Set-user-ID (sets process effective user ID on `execve(2)`)
* `S_ISGID (02000)`: Set-group-ID (sets process effective group ID on `execve(2)`; mandatory locking as described in `fcntl(2)`; takes a new file's group from parent directory as described in `chown(2)` and `mkdir(2)`)
* `S_ISVTX (01000)`: Sticky bit (restricted deletion flag as described in `unlink(2)`)
* `S_IRUSR (00400)`: Read by owner
* `S_IWUSR (00200)`: Write by owner
* `S_IXUSR (00100)`: Execute/search by owner ("search" applies for directories, allowing access to entries within)
* `S_IRGRP (00040)`: Read by group
* `S_IWGRP (00020)`: Write by group
* `S_IXGRP (00010)`: Execute/search by group
* `S_IROTH (00004)`: Read by others
* `S_IWOTH (00002)`: Write by others
* `S_IXOTH (00001)`: Execute/search by others

Available Tags

  • K - Originated from kernel-space.
  • U - Originated from user space.
  • TOCTOU - Vulnerable to TOCTOU (time of check, time of use).
  • OPT - Optional argument - might not always be available (passed with null value).

Hooks

sys_chmod

Type

Tracepoint (through sys_enter).

Purpose

To get a context of the bits which are being used to change the mode and who initiated the call.

Example Use Case

In order to collect events about every time files permissions inside of a directory are modified, you can use this event.

Issues

The chmod() system call can be subjected to TOCTOU issues because because a program may check the permissions of a file using the stat system call and, before the program can call chmod, another process changes the permissions of the file OR replaces it with a symlink to a different file.

Using fchmodat() deals with the symlink situation, removing the TOCTOU issue in that case.

  • openat()
  • fchmodat()
  • chown()

This document was automatically generated by OpenAI and reviewed by a Human.