1 From 5287acc6f097c0c18e54401b611a877a3083b68c Mon Sep 17 00:00:00 2001
2 From: Martin KaFai Lau <kafai@fb.com>
3 Date: Wed, 16 Mar 2022 10:38:23 -0700
4 Subject: [PATCH 1/3] bpf: selftests: Add helpers to directly use the capget
7 After upgrading to the newer libcap (>= 2.60),
8 the libcap commit aca076443591 ("Make cap_t operations thread safe.")
9 added a "__u8 mutex;" to the "struct _cap_struct". It caused a few byte
10 shift that breaks the assumption made in the "struct libcap" definition
13 The bpf selftest usage only needs to enable and disable the effective
14 caps of the running task. It is easier to directly syscall the
15 capget and capset instead. It can also remove the libcap
18 The cap_helpers.{c,h} is added. One __u64 is used for all CAP_*
19 bits instead of two __u32.
21 Signed-off-by: Martin KaFai Lau <kafai@fb.com>
22 Signed-off-by: Alexei Starovoitov <ast@kernel.org>
23 Acked-by: John Fastabend <john.fastabend@gmail.com>
24 Link: https://lore.kernel.org/bpf/20220316173823.2036955-1-kafai@fb.com
26 tools/testing/selftests/bpf/cap_helpers.c | 67 +++++++++++++++++++++++
27 tools/testing/selftests/bpf/cap_helpers.h | 19 +++++++
28 2 files changed, 86 insertions(+)
29 create mode 100644 tools/testing/selftests/bpf/cap_helpers.c
30 create mode 100644 tools/testing/selftests/bpf/cap_helpers.h
33 +++ b/tools/testing/selftests/bpf/cap_helpers.c
35 +// SPDX-License-Identifier: GPL-2.0
36 +#include "cap_helpers.h"
38 +/* Avoid including <sys/capability.h> from the libcap-devel package,
39 + * so directly declare them here and use them from glibc.
41 +int capget(cap_user_header_t header, cap_user_data_t data);
42 +int capset(cap_user_header_t header, const cap_user_data_t data);
44 +int cap_enable_effective(__u64 caps, __u64 *old_caps)
46 + struct __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_3];
47 + struct __user_cap_header_struct hdr = {
48 + .version = _LINUX_CAPABILITY_VERSION_3,
51 + __u32 cap1 = caps >> 32;
54 + err = capget(&hdr, data);
59 + *old_caps = (__u64)(data[1].effective) << 32 | data[0].effective;
61 + if ((data[0].effective & cap0) == cap0 &&
62 + (data[1].effective & cap1) == cap1)
65 + data[0].effective |= cap0;
66 + data[1].effective |= cap1;
67 + err = capset(&hdr, data);
74 +int cap_disable_effective(__u64 caps, __u64 *old_caps)
76 + struct __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_3];
77 + struct __user_cap_header_struct hdr = {
78 + .version = _LINUX_CAPABILITY_VERSION_3,
81 + __u32 cap1 = caps >> 32;
84 + err = capget(&hdr, data);
89 + *old_caps = (__u64)(data[1].effective) << 32 | data[0].effective;
91 + if (!(data[0].effective & cap0) && !(data[1].effective & cap1))
94 + data[0].effective &= ~cap0;
95 + data[1].effective &= ~cap1;
96 + err = capset(&hdr, data);
103 +++ b/tools/testing/selftests/bpf/cap_helpers.h
105 +/* SPDX-License-Identifier: GPL-2.0 */
106 +#ifndef __CAP_HELPERS_H
107 +#define __CAP_HELPERS_H
109 +#include <linux/types.h>
110 +#include <linux/capability.h>
113 +#define CAP_PERFMON 38
120 +int cap_enable_effective(__u64 caps, __u64 *old_caps);
121 +int cap_disable_effective(__u64 caps, __u64 *old_caps);