setcap

The setcap command in Linux is used to set capabilities on executable files. Capabilities allow you to assign specific privileges to an executable without giving it full root privileges. This enhances security by restricting the capabilities of the executable to only those it requires to function.

Overview of setcap

Purpose

The primary purpose of setcap is to grant specific capabilities to executables. This helps in minimizing the potential impact of vulnerabilities in privileged executables by limiting their access to only the required system capabilities.

Basic Usage

The general syntax for setcap is:

setcap [options] <capabilities> <file>
  • <capabilities>: The capabilities to set on the file.

  • <file>: The path to the file on which to set the capabilities.

Example Usages

  1. Granting a Single Capability

    To grant a single capability, specify the capability and the file:

    setcap cap_net_raw+ep /path/to/file

    Example:

    sudo setcap cap_net_raw+ep /usr/bin/ping

    This command grants the ping command the capability to perform raw network operations.

  2. Granting Multiple Capabilities

    To grant multiple capabilities, separate them with commas:

    setcap cap_net_raw,cap_net_admin+ep /path/to/file

    Example:

    sudo setcap cap_net_raw,cap_net_admin+ep /usr/sbin/tcpdump

    This command grants tcpdump the capabilities to perform raw network operations and network administration tasks.

  3. Removing Capabilities

    To remove all capabilities from a file, use:

    setcap -r /path/to/file

    Example:

    sudo setcap -r /usr/bin/ping

Capabilities Format

Capabilities are specified in the following format:

  • capability_name: The name of the capability.

  • +: Indicates that the capability should be added.

  • -: Indicates that the capability should be removed.

  • =: Indicates that the capability should be set.

  • e: Effective flag, the capability is in the set of effective capabilities.

  • p: Permitted flag, the capability is in the set of permitted capabilities.

  • i: Inheritable flag, the capability is in the set of inheritable capabilities.

Common Capabilities

Here are some common capabilities and their purposes:

  • cap_net_raw: Perform raw network operations.

  • cap_net_admin: Perform various network-related administrative tasks.

  • cap_sys_admin: Perform various system administrative tasks.

  • cap_sys_ptrace: Trace arbitrary processes using ptrace.

Example Capabilities and Their Meanings

  • cap_net_raw+ep: Grants the file the ability to perform raw network operations with effective (e) and permitted (p) flags.

  • cap_net_admin+eip: Grants the file network administration capabilities with effective (e), inheritable (i), and permitted (p) flags.

Conclusion

setcap is a powerful tool for enhancing security by assigning specific capabilities to executables. By carefully granting only the necessary capabilities, you can minimize the potential impact of vulnerabilities in privileged executables.

Last updated