[libc-commits] [libc] [libc] Add capability functions (PR #244053)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Wed Nov 18 16:02:37 PST 2026


https://github.com/michaelrj-google created https://github.com/llvm/llvm-project/pull/244053

The capget and capset functions are weird, the current linux API expects
that they are provided by the system libc, but not exposed via headers.
Based on that, the lack of a yaml change is intentional.

Assisted-by: Automated tooling, human reviewed.


>From 50e51bc6a6b4895c67a7ac1535ed42291be8e095 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Thu, 19 Nov 2026 00:00:59 +0000
Subject: [PATCH] [libc] Add capability functions

The capget and capset functions are weird, the current linux API expects
that they are provided by the system libc, but not exposed via headers.
Based on that, the lack of a yaml change is intentional.

Assisted-by: Automated tooling, human reviewed.
---
 libc/config/linux/aarch64/entrypoints.txt     |  4 ++
 libc/config/linux/arm/entrypoints.txt         |  4 ++
 libc/config/linux/riscv/entrypoints.txt       |  4 ++
 libc/config/linux/x86_64/entrypoints.txt      |  4 ++
 libc/hdr/types/CMakeLists.txt                 | 12 ++++
 libc/hdr/types/cap_user_data_t.h              | 21 +++++++
 libc/hdr/types/cap_user_header_t.h            | 21 +++++++
 .../linux/syscall_wrappers/CMakeLists.txt     | 28 +++++++++
 .../OSUtil/linux/syscall_wrappers/capget.h    | 36 ++++++++++++
 .../OSUtil/linux/syscall_wrappers/capset.h    | 37 ++++++++++++
 libc/src/sys/CMakeLists.txt                   |  1 +
 libc/src/sys/capability/CMakeLists.txt        | 17 ++++++
 libc/src/sys/capability/capget.h              | 27 +++++++++
 libc/src/sys/capability/capset.h              | 27 +++++++++
 libc/src/sys/capability/linux/CMakeLists.txt  | 33 +++++++++++
 libc/src/sys/capability/linux/capget.cpp      | 36 ++++++++++++
 libc/src/sys/capability/linux/capset.cpp      | 36 ++++++++++++
 libc/test/src/sys/CMakeLists.txt              |  1 +
 libc/test/src/sys/capability/CMakeLists.txt   | 32 +++++++++++
 libc/test/src/sys/capability/capget_test.cpp  | 41 +++++++++++++
 libc/test/src/sys/capability/capset_test.cpp  | 57 +++++++++++++++++++
 21 files changed, 479 insertions(+)
 create mode 100644 libc/hdr/types/cap_user_data_t.h
 create mode 100644 libc/hdr/types/cap_user_header_t.h
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/capget.h
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/capset.h
 create mode 100644 libc/src/sys/capability/CMakeLists.txt
 create mode 100644 libc/src/sys/capability/capget.h
 create mode 100644 libc/src/sys/capability/capset.h
 create mode 100644 libc/src/sys/capability/linux/CMakeLists.txt
 create mode 100644 libc/src/sys/capability/linux/capget.cpp
 create mode 100644 libc/src/sys/capability/linux/capset.cpp
 create mode 100644 libc/test/src/sys/capability/CMakeLists.txt
 create mode 100644 libc/test/src/sys/capability/capget_test.cpp
 create mode 100644 libc/test/src/sys/capability/capset_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index b43c7c670db822..0d4df042381746 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -289,6 +289,10 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.vsprintf
     libc.src.stdio.vasprintf
 
+    # sys/capability.h entrypoints
+    libc.src.sys.capability.capget
+    libc.src.sys.capability.capset
+
     # sys/epoll.h entrypoints
     libc.src.sys.epoll.epoll_create
     libc.src.sys.epoll.epoll_create1
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 956a154cf0d5ec..55785f1ab7177a 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -211,6 +211,10 @@ set(TARGET_LIBC_ENTRYPOINTS
     # stdio.h entrypoints
     libc.src.stdio.tmpfile
 
+    # sys/capability.h entrypoints
+    libc.src.sys.capability.capget
+    libc.src.sys.capability.capset
+
     # sys/eventfd.h entrypoints
     libc.src.sys.eventfd.eventfd
     libc.src.sys.eventfd.eventfd_read
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 6354889fa0edd5..e7038e6b2967e2 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -319,6 +319,10 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.vsprintf
     libc.src.stdio.vasprintf
 
+    # sys/capability.h entrypoints
+    libc.src.sys.capability.capget
+    libc.src.sys.capability.capset
+
     # sys/epoll.h entrypoints
     libc.src.sys.epoll.epoll_create
     libc.src.sys.epoll.epoll_create1
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 0e7ae76c11457c..64ee14e130a964 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -319,6 +319,10 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.vsprintf
     libc.src.stdio.vasprintf
 
+    # sys/capability.h entrypoints
+    libc.src.sys.capability.capget
+    libc.src.sys.capability.capset
+
     # sys/epoll.h entrypoints
     libc.src.sys.epoll.epoll_create
     libc.src.sys.epoll.epoll_create1
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 61fc9c948a638c..be4a5b0e3f7937 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -1445,3 +1445,15 @@ add_proxy_header_library(
   FULL_BUILD_DEPENDS
     libc.include.llvm-libc-types.eventfd_t
 )
+
+add_proxy_header_library(
+  cap_user_header_t
+  HDRS
+    cap_user_header_t.h
+)
+
+add_proxy_header_library(
+  cap_user_data_t
+  HDRS
+    cap_user_data_t.h
+)
diff --git a/libc/hdr/types/cap_user_data_t.h b/libc/hdr/types/cap_user_data_t.h
new file mode 100644
index 00000000000000..edf32021f0f7a2
--- /dev/null
+++ b/libc/hdr/types/cap_user_data_t.h
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Proxy header for cap_user_data_t.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_CAP_USER_DATA_T_H
+#define LLVM_LIBC_HDR_TYPES_CAP_USER_DATA_T_H
+
+#ifdef __linux__
+#include <linux/capability.h>
+#endif // __linux__
+
+#endif // LLVM_LIBC_HDR_TYPES_CAP_USER_DATA_T_H
diff --git a/libc/hdr/types/cap_user_header_t.h b/libc/hdr/types/cap_user_header_t.h
new file mode 100644
index 00000000000000..0de35b6e315c15
--- /dev/null
+++ b/libc/hdr/types/cap_user_header_t.h
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Proxy header for cap_user_header_t.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_CAP_USER_HEADER_T_H
+#define LLVM_LIBC_HDR_TYPES_CAP_USER_HEADER_T_H
+
+#ifdef __linux__
+#include <linux/capability.h>
+#endif // __linux__
+
+#endif // LLVM_LIBC_HDR_TYPES_CAP_USER_HEADER_T_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index bb9d3a1846bef6..d7a75d9172e06c 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -2701,3 +2701,31 @@ add_header_library(
     libc.src.__support.error_or
     libc.src.__support.macros.config
 )
+
+add_header_library(
+  capget
+  HDRS
+    capget.h
+  DEPENDS
+    libc.hdr.types.cap_user_data_t
+    libc.hdr.types.cap_user_header_t
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  capset
+  HDRS
+    capset.h
+  DEPENDS
+    libc.hdr.types.cap_user_data_t
+    libc.hdr.types.cap_user_header_t
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+)
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/capget.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/capget.h
new file mode 100644
index 00000000000000..1e4b2d98bd4975
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/capget.h
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for capget.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_CAPGET_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_CAPGET_H
+
+#include "hdr/types/cap_user_data_t.h"
+#include "hdr/types/cap_user_header_t.h"
+#include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> capget(cap_user_header_t hdrp, cap_user_data_t datap) {
+  return syscall_checked<int>(SYS_capget, hdrp, datap);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_CAPGET_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/capset.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/capset.h
new file mode 100644
index 00000000000000..e9b8e9e87aabd2
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/capset.h
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for capset.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_CAPSET_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_CAPSET_H
+
+#include "hdr/types/cap_user_data_t.h"
+#include "hdr/types/cap_user_header_t.h"
+#include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> capset(cap_user_header_t hdrp,
+                                const cap_user_data_t datap) {
+  return syscall_checked<int>(SYS_capset, hdrp, datap);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_CAPSET_H
diff --git a/libc/src/sys/CMakeLists.txt b/libc/src/sys/CMakeLists.txt
index c891c471ed4f13..d43daa8411cfe8 100644
--- a/libc/src/sys/CMakeLists.txt
+++ b/libc/src/sys/CMakeLists.txt
@@ -1,4 +1,5 @@
 add_subdirectory(auxv)
+add_subdirectory(capability)
 add_subdirectory(epoll)
 add_subdirectory(eventfd)
 add_subdirectory(file)
diff --git a/libc/src/sys/capability/CMakeLists.txt b/libc/src/sys/capability/CMakeLists.txt
new file mode 100644
index 00000000000000..0db9a3bd7ed8a9
--- /dev/null
+++ b/libc/src/sys/capability/CMakeLists.txt
@@ -0,0 +1,17 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+endif()
+
+add_entrypoint_object(
+  capget
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.capget
+)
+
+add_entrypoint_object(
+  capset
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.capset
+)
diff --git a/libc/src/sys/capability/capget.h b/libc/src/sys/capability/capget.h
new file mode 100644
index 00000000000000..66d50a1109a1d3
--- /dev/null
+++ b/libc/src/sys/capability/capget.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for capget.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_CAPABILITY_CAPGET_H
+#define LLVM_LIBC_SRC_SYS_CAPABILITY_CAPGET_H
+
+#include "hdr/types/cap_user_data_t.h"
+#include "hdr/types/cap_user_header_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int capget(cap_user_header_t hdrp, cap_user_data_t datap);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_CAPABILITY_CAPGET_H
diff --git a/libc/src/sys/capability/capset.h b/libc/src/sys/capability/capset.h
new file mode 100644
index 00000000000000..96dcd2c56c092f
--- /dev/null
+++ b/libc/src/sys/capability/capset.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for capset.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_CAPABILITY_CAPSET_H
+#define LLVM_LIBC_SRC_SYS_CAPABILITY_CAPSET_H
+
+#include "hdr/types/cap_user_data_t.h"
+#include "hdr/types/cap_user_header_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int capset(cap_user_header_t hdrp, const cap_user_data_t datap);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_CAPABILITY_CAPSET_H
diff --git a/libc/src/sys/capability/linux/CMakeLists.txt b/libc/src/sys/capability/linux/CMakeLists.txt
new file mode 100644
index 00000000000000..8f778ba9b6d20e
--- /dev/null
+++ b/libc/src/sys/capability/linux/CMakeLists.txt
@@ -0,0 +1,33 @@
+add_entrypoint_object(
+  capget
+  SRCS
+    capget.cpp
+  HDRS
+    ../capget.h
+  DEPENDS
+    libc.hdr.types.cap_user_data_t
+    libc.hdr.types.cap_user_header_t
+    libc.src.__support.OSUtil.linux.syscall_wrappers.capget
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  capset
+  SRCS
+    capset.cpp
+  HDRS
+    ../capset.h
+  DEPENDS
+    libc.hdr.types.cap_user_data_t
+    libc.hdr.types.cap_user_header_t
+    libc.src.__support.OSUtil.linux.syscall_wrappers.capset
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+    libc.src.errno.errno
+)
diff --git a/libc/src/sys/capability/linux/capget.cpp b/libc/src/sys/capability/linux/capget.cpp
new file mode 100644
index 00000000000000..d9f7b2fae718bf
--- /dev/null
+++ b/libc/src/sys/capability/linux/capget.cpp
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of capget.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/capability/capget.h"
+
+#include "hdr/types/cap_user_data_t.h"
+#include "hdr/types/cap_user_header_t.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/capget.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, capget,
+                   (cap_user_header_t hdrp, cap_user_data_t datap)) {
+  auto result = linux_syscalls::capget(hdrp, datap);
+  if (!result.has_value()) {
+    libc_errno = result.error();
+    return -1;
+  }
+  return result.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/capability/linux/capset.cpp b/libc/src/sys/capability/linux/capset.cpp
new file mode 100644
index 00000000000000..facbd53ecd25de
--- /dev/null
+++ b/libc/src/sys/capability/linux/capset.cpp
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of capset.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/capability/capset.h"
+
+#include "hdr/types/cap_user_data_t.h"
+#include "hdr/types/cap_user_header_t.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/capset.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, capset,
+                   (cap_user_header_t hdrp, const cap_user_data_t datap)) {
+  auto result = linux_syscalls::capset(hdrp, datap);
+  if (!result.has_value()) {
+    libc_errno = result.error();
+    return -1;
+  }
+  return result.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/sys/CMakeLists.txt b/libc/test/src/sys/CMakeLists.txt
index 6650f0842f3ae9..5b8642a304e3a8 100644
--- a/libc/test/src/sys/CMakeLists.txt
+++ b/libc/test/src/sys/CMakeLists.txt
@@ -12,6 +12,7 @@ add_subdirectory(utsname)
 add_subdirectory(wait)
 add_subdirectory(prctl)
 add_subdirectory(auxv)
+add_subdirectory(capability)
 add_subdirectory(epoll)
 add_subdirectory(eventfd)
 add_subdirectory(file)
diff --git a/libc/test/src/sys/capability/CMakeLists.txt b/libc/test/src/sys/capability/CMakeLists.txt
new file mode 100644
index 00000000000000..1c2eedf6025600
--- /dev/null
+++ b/libc/test/src/sys/capability/CMakeLists.txt
@@ -0,0 +1,32 @@
+add_libc_testsuite(libc-sys-capability-tests)
+
+add_libc_test(
+  capget_test
+  SUITE
+    libc-sys-capability-tests
+  SRCS
+    capget_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.types.cap_user_data_t
+    libc.hdr.types.cap_user_header_t
+    libc.src.sys.capability.capget
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
+
+add_libc_test(
+  capset_test
+  SUITE
+    libc-sys-capability-tests
+  SRCS
+    capset_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.types.cap_user_data_t
+    libc.hdr.types.cap_user_header_t
+    libc.src.sys.capability.capget
+    libc.src.sys.capability.capset
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
diff --git a/libc/test/src/sys/capability/capget_test.cpp b/libc/test/src/sys/capability/capget_test.cpp
new file mode 100644
index 00000000000000..fc43f634d084a4
--- /dev/null
+++ b/libc/test/src/sys/capability/capget_test.cpp
@@ -0,0 +1,41 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for capget.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/types/cap_user_data_t.h"
+#include "hdr/types/cap_user_header_t.h"
+#include "src/sys/capability/capget.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include <linux/capability.h>
+
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+using LlvmLibcCapgetTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcCapgetTest, VersionProbeAndCurrent) {
+  __user_cap_header_struct hdr = {0, 0};
+  ASSERT_THAT(LIBC_NAMESPACE::capget(&hdr, nullptr), Succeeds(0));
+  ASSERT_EQ(hdr.version, static_cast<__u32>(_LINUX_CAPABILITY_VERSION_3));
+
+  __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_3] = {};
+  ASSERT_THAT(LIBC_NAMESPACE::capget(&hdr, data), Succeeds(0));
+}
+
+TEST_F(LlvmLibcCapgetTest, InvalidPid) {
+  __user_cap_header_struct hdr = {_LINUX_CAPABILITY_VERSION_3, -1};
+  __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_3] = {};
+  ASSERT_THAT(LIBC_NAMESPACE::capget(&hdr, data), Fails(EINVAL));
+}
diff --git a/libc/test/src/sys/capability/capset_test.cpp b/libc/test/src/sys/capability/capset_test.cpp
new file mode 100644
index 00000000000000..f597a3110b285a
--- /dev/null
+++ b/libc/test/src/sys/capability/capset_test.cpp
@@ -0,0 +1,57 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for capset.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/types/cap_user_data_t.h"
+#include "hdr/types/cap_user_header_t.h"
+#include "src/sys/capability/capget.h"
+#include "src/sys/capability/capset.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include <linux/capability.h>
+
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::any_of;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+using LlvmLibcCapsetTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcCapsetTest, Current) {
+  __user_cap_header_struct hdr = {_LINUX_CAPABILITY_VERSION_3, 0};
+  __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_3] = {};
+  ASSERT_THAT(LIBC_NAMESPACE::capget(&hdr, data), Succeeds(0));
+
+  int ret = LIBC_NAMESPACE::capset(&hdr, data);
+  if (ret == 0) {
+    ASSERT_ERRNO_SUCCESS();
+  } else {
+    ASSERT_THAT(ret, Fails(any_of(EPERM, ENOSYS)));
+  }
+}
+
+TEST_F(LlvmLibcCapsetTest, Failure) {
+  __user_cap_header_struct bad_hdr = {0, 0};
+  __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_3] = {};
+  ASSERT_THAT(LIBC_NAMESPACE::capset(&bad_hdr, data),
+              Fails(any_of(EINVAL, EPERM, ENOSYS)));
+
+  __user_cap_header_struct hdr = {_LINUX_CAPABILITY_VERSION_3, 0};
+  ASSERT_THAT(LIBC_NAMESPACE::capget(&hdr, data), Succeeds(0));
+  if ((data[0].permitted & (1U << CAP_SETUID)) == 0) {
+    data[0].effective |= (1U << CAP_SETUID);
+    data[0].permitted |= (1U << CAP_SETUID);
+    ASSERT_THAT(LIBC_NAMESPACE::capset(&hdr, data),
+                Fails(any_of(EPERM, ENOSYS)));
+  }
+}



More information about the libc-commits mailing list