#capabilities #linux #action #prctl

no-std capctl

A pure-Rust interface to prctl() and Linux capabilities

6 releases

0.2.4 Mar 17, 2024
0.2.3 Jul 30, 2023
0.2.2 Jun 26, 2022
0.2.1 Mar 19, 2022
0.1.0 Nov 4, 2020

#111 in Unix APIs

Download history 4263/week @ 2024-07-23 4177/week @ 2024-07-30 4552/week @ 2024-08-06 4894/week @ 2024-08-13 4310/week @ 2024-08-20 4778/week @ 2024-08-27 4282/week @ 2024-09-03 4818/week @ 2024-09-10 4064/week @ 2024-09-17 4248/week @ 2024-09-24 3780/week @ 2024-10-01 4308/week @ 2024-10-08 3793/week @ 2024-10-15 4141/week @ 2024-10-22 3282/week @ 2024-10-29 2491/week @ 2024-11-05

14,331 downloads per month
Used in 6 crates (4 directly)

MIT license

160KB
3.5K SLoC

capctl

crates.io Docs GitHub Actions codecov

A pure-Rust interface to prctl() and Linux capabilities.

Features

This crate has the following features (by default, only std is enabled):

  • std: Link against the standard library.

    Interfaces that depend on this feature are marked in the documentation on docs.rs.

  • sc: Allow making inline syscalls with the sc crate instead of calling into the system's libc for some operations.

    Note: Currently, support for inline syscalls is limited to the following syscalls: prctl(), capget(), capset(), setresuid(), setresgid(), setgroups(). capctl will still call into the system's libc for most other syscalls.

  • serde: Enables implementations of Serialize and Deserialize for most (non-error) types.

Why not caps?

TL;DR: In the opinion of capctl's author, caps adds too much abstraction and overhead.

  1. The kernel APIs to access the 5 capability sets (permitted, effective, inheritable, bounding, and ambient) are very different. However, caps presents a unified interface that allows for manipulating all of them the same way.

    This is certainly more convenient to use. However, a) it minimizes the differences between the capabilities sets (something that is fundamental and must be understood to use capabilities properly), b) it allows users to write code that attempts to perform operations that are actually impossible (i.e. adding capabilities to the bounding capability set), and c) it can result in excessive syscalls (because operations that the kernel APIs allow to be performed together instead must done separately).

    Note: The author of capctl is not completely opposed to adding these kinds of interfaces, provided that lower-level APIs are also provided to allow users finer control. caps, however, does not do this.

  2. capctl uses more efficient representations internally.

    For example, caps uses HashSets to store sets of capabilities, which is wasteful. capctl, meanwhile, has a custom CapSet struct that stores a set of capabilities much more efficiently. (CapSet also has methods specially designed to work with capabilities, instead of just being a generalized set implementation.)

Why not prctl?

TL;DR: prctl is a very low-level wrapper crate, and some of its "safe" code should be unsafe.

  1. prctl concentrates on the prctl() system call, not Linux capabilities in general. As a result, its interface to Linux capabilities is an afterthought and incomplete.

  2. prctl returns raw errno values when an error occurs. This crate returns a friendlier custom error type that can be converted into an io::Error.

  3. Most importantly, prctl fails to recognize that, as the man page explains, prctl() is a very low-level syscall, and it should be used cautiously.

    As a result, some of the "safe" functions in prctl are actually highly unsafe! prctl::set_mm() is the worst example: it can be used to set raw addresses, such as the end of the heap (as with brk()), and it's a "safe" function! It even accepts these addresses as libc::c_ulongs instead of raw pointers, making it easy to abuse.

Dependencies

~81–380KB