summaryrefslogtreecommitdiff
path: root/security/landlock/cap.h
blob: 11208cbf15d2e82a2c985ae47999fad947aae872 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* SPDX-License-Identifier: GPL-2.0-only */
/*
 * Landlock - Capability hooks
 *
 * Copyright © 2026 Cloudflare, Inc.
 */

#ifndef _SECURITY_LANDLOCK_CAP_H
#define _SECURITY_LANDLOCK_CAP_H

#include <linux/bitops.h>
#include <linux/bug.h>
#include <linux/capability.h>
#include <linux/types.h>

/**
 * landlock_cap_to_bit - Convert a capability number to a compact bitmask
 *
 * @cap: Capability number (CAP_*).
 *
 * Return: BIT_ULL(@cap), or 0 if @cap is invalid (with a WARN).
 */
static inline u64 landlock_cap_to_bit(const int cap)
{
	if (WARN_ON_ONCE(!cap_valid(cap)))
		return 0;

	return BIT_ULL(cap);
}

/**
 * landlock_caps_to_bits - Validate and mask a capability bitmask
 *
 * @capabilities: Bitmask of capabilities (e.g. from user space).
 *
 * Return: @capabilities masked to known capabilities.  Warns if unknown bits
 * are present (callers must pre-mask for user input).
 */
static inline u64 landlock_caps_to_bits(const u64 capabilities)
{
	/* Callers pre-mask (CAP_VALID_MASK); the WARN guards future callers. */
	WARN_ON_ONCE(capabilities & ~CAP_VALID_MASK);
	return capabilities & CAP_VALID_MASK;
}

__init void landlock_add_cap_hooks(void);

#endif /* _SECURITY_LANDLOCK_CAP_H */