060-v5.18-01-bpf-selftests-Add-helpers-to-directly-use-the-capget.patch 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. From 5287acc6f097c0c18e54401b611a877a3083b68c Mon Sep 17 00:00:00 2001
  2. From: Martin KaFai Lau <[email protected]>
  3. Date: Wed, 16 Mar 2022 10:38:23 -0700
  4. Subject: [PATCH 1/3] bpf: selftests: Add helpers to directly use the capget
  5. and capset syscall
  6. After upgrading to the newer libcap (>= 2.60),
  7. the libcap commit aca076443591 ("Make cap_t operations thread safe.")
  8. added a "__u8 mutex;" to the "struct _cap_struct". It caused a few byte
  9. shift that breaks the assumption made in the "struct libcap" definition
  10. in test_verifier.c.
  11. The bpf selftest usage only needs to enable and disable the effective
  12. caps of the running task. It is easier to directly syscall the
  13. capget and capset instead. It can also remove the libcap
  14. library dependency.
  15. The cap_helpers.{c,h} is added. One __u64 is used for all CAP_*
  16. bits instead of two __u32.
  17. Signed-off-by: Martin KaFai Lau <[email protected]>
  18. Signed-off-by: Alexei Starovoitov <[email protected]>
  19. Acked-by: John Fastabend <[email protected]>
  20. Link: https://lore.kernel.org/bpf/[email protected]
  21. ---
  22. tools/testing/selftests/bpf/cap_helpers.c | 67 +++++++++++++++++++++++
  23. tools/testing/selftests/bpf/cap_helpers.h | 19 +++++++
  24. 2 files changed, 86 insertions(+)
  25. create mode 100644 tools/testing/selftests/bpf/cap_helpers.c
  26. create mode 100644 tools/testing/selftests/bpf/cap_helpers.h
  27. --- /dev/null
  28. +++ b/tools/testing/selftests/bpf/cap_helpers.c
  29. @@ -0,0 +1,67 @@
  30. +// SPDX-License-Identifier: GPL-2.0
  31. +#include "cap_helpers.h"
  32. +
  33. +/* Avoid including <sys/capability.h> from the libcap-devel package,
  34. + * so directly declare them here and use them from glibc.
  35. + */
  36. +int capget(cap_user_header_t header, cap_user_data_t data);
  37. +int capset(cap_user_header_t header, const cap_user_data_t data);
  38. +
  39. +int cap_enable_effective(__u64 caps, __u64 *old_caps)
  40. +{
  41. + struct __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_3];
  42. + struct __user_cap_header_struct hdr = {
  43. + .version = _LINUX_CAPABILITY_VERSION_3,
  44. + };
  45. + __u32 cap0 = caps;
  46. + __u32 cap1 = caps >> 32;
  47. + int err;
  48. +
  49. + err = capget(&hdr, data);
  50. + if (err)
  51. + return err;
  52. +
  53. + if (old_caps)
  54. + *old_caps = (__u64)(data[1].effective) << 32 | data[0].effective;
  55. +
  56. + if ((data[0].effective & cap0) == cap0 &&
  57. + (data[1].effective & cap1) == cap1)
  58. + return 0;
  59. +
  60. + data[0].effective |= cap0;
  61. + data[1].effective |= cap1;
  62. + err = capset(&hdr, data);
  63. + if (err)
  64. + return err;
  65. +
  66. + return 0;
  67. +}
  68. +
  69. +int cap_disable_effective(__u64 caps, __u64 *old_caps)
  70. +{
  71. + struct __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_3];
  72. + struct __user_cap_header_struct hdr = {
  73. + .version = _LINUX_CAPABILITY_VERSION_3,
  74. + };
  75. + __u32 cap0 = caps;
  76. + __u32 cap1 = caps >> 32;
  77. + int err;
  78. +
  79. + err = capget(&hdr, data);
  80. + if (err)
  81. + return err;
  82. +
  83. + if (old_caps)
  84. + *old_caps = (__u64)(data[1].effective) << 32 | data[0].effective;
  85. +
  86. + if (!(data[0].effective & cap0) && !(data[1].effective & cap1))
  87. + return 0;
  88. +
  89. + data[0].effective &= ~cap0;
  90. + data[1].effective &= ~cap1;
  91. + err = capset(&hdr, data);
  92. + if (err)
  93. + return err;
  94. +
  95. + return 0;
  96. +}
  97. --- /dev/null
  98. +++ b/tools/testing/selftests/bpf/cap_helpers.h
  99. @@ -0,0 +1,19 @@
  100. +/* SPDX-License-Identifier: GPL-2.0 */
  101. +#ifndef __CAP_HELPERS_H
  102. +#define __CAP_HELPERS_H
  103. +
  104. +#include <linux/types.h>
  105. +#include <linux/capability.h>
  106. +
  107. +#ifndef CAP_PERFMON
  108. +#define CAP_PERFMON 38
  109. +#endif
  110. +
  111. +#ifndef CAP_BPF
  112. +#define CAP_BPF 39
  113. +#endif
  114. +
  115. +int cap_enable_effective(__u64 caps, __u64 *old_caps);
  116. +int cap_disable_effective(__u64 caps, __u64 *old_caps);
  117. +
  118. +#endif