forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bpf: selftests: Add helpers to directly use the capget and capset sys…
…call After upgrading to the newer libcap (>= 2.60), the libcap commit aca076443591 ("Make cap_t operations thread safe.") added a "__u8 mutex;" to the "struct _cap_struct". It caused a few byte shift that breaks the assumption made in the "struct libcap" definition in test_verifier.c. The bpf selftest usage only needs to enable and disable the effective caps of the running task. It is easier to directly syscall the capget and capset instead. It can also remove the libcap library dependency. The cap_helpers.{c,h} is added. One __u64 is used for all CAP_* bits instead of two __u32. Signed-off-by: Martin KaFai Lau <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: John Fastabend <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
- Loading branch information
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
#include "cap_helpers.h" | ||
|
||
/* Avoid including <sys/capability.h> from the libcap-devel package, | ||
* so directly declare them here and use them from glibc. | ||
*/ | ||
int capget(cap_user_header_t header, cap_user_data_t data); | ||
int capset(cap_user_header_t header, const cap_user_data_t data); | ||
|
||
int cap_enable_effective(__u64 caps, __u64 *old_caps) | ||
{ | ||
struct __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_3]; | ||
struct __user_cap_header_struct hdr = { | ||
.version = _LINUX_CAPABILITY_VERSION_3, | ||
}; | ||
__u32 cap0 = caps; | ||
__u32 cap1 = caps >> 32; | ||
int err; | ||
|
||
err = capget(&hdr, data); | ||
if (err) | ||
return err; | ||
|
||
if (old_caps) | ||
*old_caps = (__u64)(data[1].effective) << 32 | data[0].effective; | ||
|
||
if ((data[0].effective & cap0) == cap0 && | ||
(data[1].effective & cap1) == cap1) | ||
return 0; | ||
|
||
data[0].effective |= cap0; | ||
data[1].effective |= cap1; | ||
err = capset(&hdr, data); | ||
if (err) | ||
return err; | ||
|
||
return 0; | ||
} | ||
|
||
int cap_disable_effective(__u64 caps, __u64 *old_caps) | ||
{ | ||
struct __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_3]; | ||
struct __user_cap_header_struct hdr = { | ||
.version = _LINUX_CAPABILITY_VERSION_3, | ||
}; | ||
__u32 cap0 = caps; | ||
__u32 cap1 = caps >> 32; | ||
int err; | ||
|
||
err = capget(&hdr, data); | ||
if (err) | ||
return err; | ||
|
||
if (old_caps) | ||
*old_caps = (__u64)(data[1].effective) << 32 | data[0].effective; | ||
|
||
if (!(data[0].effective & cap0) && !(data[1].effective & cap1)) | ||
return 0; | ||
|
||
data[0].effective &= ~cap0; | ||
data[1].effective &= ~cap1; | ||
err = capset(&hdr, data); | ||
if (err) | ||
return err; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* SPDX-License-Identifier: GPL-2.0 */ | ||
#ifndef __CAP_HELPERS_H | ||
#define __CAP_HELPERS_H | ||
|
||
#include <linux/types.h> | ||
#include <linux/capability.h> | ||
|
||
#ifndef CAP_PERFMON | ||
#define CAP_PERFMON 38 | ||
#endif | ||
|
||
#ifndef CAP_BPF | ||
#define CAP_BPF 39 | ||
#endif | ||
|
||
int cap_enable_effective(__u64 caps, __u64 *old_caps); | ||
int cap_disable_effective(__u64 caps, __u64 *old_caps); | ||
|
||
#endif |