• Mickaël Salaün's avatar
    selftests/landlock: Fix capability for net_test · bb6f4dbe
    Mickaël Salaün authored
    CAP_NET_ADMIN allows to configure network interfaces, not CAP_SYS_ADMIN
    which only allows to call unshare(2).  Without this change, running
    network tests as a non-root user but with all capabilities would fail at
    the setup_loopback() step with "RTNETLINK answers: Operation not
    permitted".
    
    The issue is only visible when running tests with non-root users (i.e.
    only relying on ambient capabilities).  Indeed, when configuring the
    network interface, the "ip" command is called, which may lead to the
    special handling of capabilities for the root user by execve(2).  If
    root is the caller, then the inherited, permitted and effective
    capabilities are all reset, which then includes CAP_NET_ADMIN.  However,
    if a non-root user is the caller, then ambient capabilities are masked
    by the inherited ones, which were explicitly dropped.
    
    To make execution deterministic whatever users are running the tests,
    set the noroot secure bit for each test, and set the inheritable and
    ambient capabilities to CAP_NET_ADMIN, the only capability that may be
    required after an execve(2).
    
    Factor out _effective_cap() into _change_cap(), and use it to manage
    ambient capabilities with the new set_ambient_cap() and
    clear_ambient_cap() helpers.
    
    This makes it possible to run all Landlock tests with check-linux.sh
    from https://github.com/landlock-lsm/landlock-test-tools
    
    Cc: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
    Fixes: a549d055 ("selftests/landlock: Add network tests")
    Link: https://lore.kernel.org/r/20240125153230.3817165-2-mic@digikod.net
    [mic: Make sure SECBIT_NOROOT_LOCKED is set]
    Signed-off-by: default avatarMickaël Salaün <mic@digikod.net>
    bb6f4dbe
common.h 7.7 KB