[packages/libcap] Rel 2. Backport psx setuid/setgid and memory safety fixes
arekm
arekm at pld-linux.org
Thu Oct 29 08:41:02 CET 2026
commit e81f21fdaa5a0aec7a7ffb2fce1e435be06ad128
Author: Arkadiusz Miśkiewicz <arekm at maven.pl>
Date: Thu Oct 29 00:08:23 2026 +0100
Rel 2. Backport psx setuid/setgid and memory safety fixes
libcap-iab-negative-bit.patch | 64 +++++++++++++++++++++
libcap-psx-getdents64.patch | 38 +++++++++++++
libcap-setuid32.patch | 126 ++++++++++++++++++++++++++++++++++++++++++
libcap.spec | 12 +++-
4 files changed, 238 insertions(+), 2 deletions(-)
---
diff --git a/libcap.spec b/libcap.spec
index 9680f03..6c425c5 100644
--- a/libcap.spec
+++ b/libcap.spec
@@ -10,14 +10,19 @@ Summary(pl.UTF-8): Wsparcie dla standardu "capability" POSIX.1e
Summary(pt_BR.UTF-8): Biblioteca para leitura e configuração de capabilities.
Name: libcap
Version: 2.78
-Release: 1
+Release: 2
Epoch: 1
License: GPL v2 or BSD
Group: Applications/System
Source0: https://www.kernel.org/pub/linux/libs/security/linux-privs/libcap2/%{name}-%{version}.tar.xz
# Source0-md5: 0ff11419aa4813c0a0f84fe67bf3b39e
+# https://git.kernel.org/pub/scm/libs/libcap/libcap.git/commit/?id=27e899db7fd3f945f0c144cff179e4e558d0a053
+Patch0: %{name}-setuid32.patch
+# https://git.kernel.org/pub/scm/libs/libcap/libcap.git/commit/?id=4bef9a89d94cafa551f5513ec58e96eb9199426b
+Patch1: %{name}-psx-getdents64.patch
+# https://git.kernel.org/pub/scm/libs/libcap/libcap.git/commit/?id=35e8ea44adbaa1c17d5dae1df6b9d4a9770ad662
+Patch2: %{name}-iab-negative-bit.patch
URL: https://sites.google.com/site/fullycapable/
-BuildRequires: attr-devel
%{?with_golang:BuildRequires: golang}
BuildRequires: pam-devel
BuildRequires: tar >= 1:1.22
@@ -93,6 +98,9 @@ Moduł PAM capability wymuszający dziedziczone zbiory uprawnień.
%prep
%setup -q
+%patch -P0 -p1
+%patch -P1 -p1
+%patch -P2 -p1
%build
LDFLAGS="%{rpmldflags}" \
diff --git a/libcap-iab-negative-bit.patch b/libcap-iab-negative-bit.patch
new file mode 100644
index 0000000..112ca53
--- /dev/null
+++ b/libcap-iab-negative-bit.patch
@@ -0,0 +1,64 @@
+From 35e8ea44adbaa1c17d5dae1df6b9d4a9770ad662 Mon Sep 17 00:00:00 2001
+From: Sayed Kaif <skaif at digiscrypt.com>
+Date: Fri, 10 Jul 2026 01:31:05 +0530
+Subject: cap_flag: reject negative bit in the cap_iab_t vector accessors
+
+cap_iab_get_vector() and cap_iab_set_vector() bound the requested
+capability index with a single test, "bit >= cap_max_bits()".
+cap_value_t is a signed int, so this signed comparison does not reject
+a negative bit: for example -1 >= cap_max_bits() is false. The
+functions then compute
+
+ unsigned o = (bit >> 5);
+
+and use o to index the fixed-size iab->i[], iab->a[] and iab->nb[]
+arrays. For a negative bit the arithmetic right shift preserves the
+sign, so the value converted to the unsigned index is enormous
+(-1 >> 5 == -1, which becomes 0xFFFFFFFF). cap_iab_get_vector() then
+reads, and cap_iab_set_vector() reads and writes, far outside the
+heap-allocated iab object.
+
+The sibling accessors cap_get_flag() and cap_set_flag() already reject
+out-of-range indices with an explicit "value >= 0" test; the IAB
+vector accessors only checked the upper bound. Add the matching
+"bit < 0" check to both so a negative index is rejected the same way an
+oversized one is (get returns 0, set returns -1/EINVAL) instead of
+touching memory out of bounds.
+
+Verified by compiling the real cap_flag.c and calling
+cap_iab_set_vector(iab, CAP_IAB_INH, -32, CAP_SET): before the change
+AddressSanitizer reports an out-of-bounds access at cap_flag.c:261
+(index 0xFFFFFFFF); after the change the call returns -1/EINVAL, while
+valid bits (e.g. 5) still succeed and the upper bound is unaffected.
+
+Signed-off-by: Sayed Kaif <skaif at digiscrypt.com>
+Signed-off-by: Andrew G. Morgan <morgan at kernel.org>
+---
+ libcap/cap_flag.c | 4 ++--
+ 1 file changed, 2 insertions(+), 2 deletions(-)
+
+diff --git a/libcap/cap_flag.c b/libcap/cap_flag.c
+index 94afd1e..7ce5909 100644
+--- a/libcap/cap_flag.c
++++ b/libcap/cap_flag.c
+@@ -211,7 +211,7 @@ int cap_fill(cap_t cap_d, cap_flag_t to, cap_flag_t from)
+ cap_flag_value_t cap_iab_get_vector(cap_iab_t iab, cap_iab_vector_t vec,
+ cap_value_t bit)
+ {
+- if (!good_cap_iab_t(iab) || bit >= cap_max_bits()) {
++ if (!good_cap_iab_t(iab) || bit < 0 || bit >= cap_max_bits()) {
+ return 0;
+ }
+
+@@ -246,7 +246,7 @@ cap_flag_value_t cap_iab_get_vector(cap_iab_t iab, cap_iab_vector_t vec,
+ int cap_iab_set_vector(cap_iab_t iab, cap_iab_vector_t vec, cap_value_t bit,
+ cap_flag_value_t raised)
+ {
+- if (!good_cap_iab_t(iab) || (raised >> 1) || bit >= cap_max_bits()) {
++ if (!good_cap_iab_t(iab) || (raised >> 1) || bit < 0 || bit >= cap_max_bits()) {
+ errno = EINVAL;
+ return -1;
+ }
+--
+cgit 1.3.1-korg
+
diff --git a/libcap-psx-getdents64.patch b/libcap-psx-getdents64.patch
new file mode 100644
index 0000000..f85052d
--- /dev/null
+++ b/libcap-psx-getdents64.patch
@@ -0,0 +1,38 @@
+From 4bef9a89d94cafa551f5513ec58e96eb9199426b Mon Sep 17 00:00:00 2001
+From: "Andrew G. Morgan" <morgan at kernel.org>
+Date: Tue, 16 Jun 2026 05:54:28 -0700
+Subject: Cast getdents64() return as ssize_t and not the unsigned size_t.
+
+Thanks to Sayed Kaif <skaif at digiscrypt.com> for reporting and
+collaborating on the fix.
+
+Signed-off-by: Andrew G. Morgan <morgan at kernel.org>
+---
+ psx/psx.c | 4 ++--
+ 1 file changed, 2 insertions(+), 2 deletions(-)
+
+diff --git a/psx/psx.c b/psx/psx.c
+index bfd4d61..ed08b55 100644
+--- a/psx/psx.c
++++ b/psx/psx.c
+@@ -346,7 +346,7 @@ long int __psx_syscall(long int syscall_nr, ...) {
+
+ for (;;) {
+ char buf[BUF_SIZE];
+- size_t nread = syscall(SYS_getdents64, fd, buf, BUF_SIZE);
++ ssize_t nread = syscall(SYS_getdents64, fd, buf, BUF_SIZE);
+ if (nread == 0) {
+ break;
+ } else if (nread < 0) {
+@@ -354,7 +354,7 @@ long int __psx_syscall(long int syscall_nr, ...) {
+ kill(psx_tracker.pid, SIGKILL);
+ }
+
+- size_t offset;
++ ssize_t offset;
+ unsigned short reclen;
+ for (offset = 0; offset < nread; offset += reclen) {
+ /* deal with potential unaligned reads */
+--
+cgit 1.3.1-korg
+
diff --git a/libcap-setuid32.patch b/libcap-setuid32.patch
new file mode 100644
index 0000000..2f8cb20
--- /dev/null
+++ b/libcap-setuid32.patch
@@ -0,0 +1,126 @@
+From 27e899db7fd3f945f0c144cff179e4e558d0a053 Mon Sep 17 00:00:00 2001
+From: Sayed Kaif <skaif at digiscrypt.com>
+Date: Tue, 16 Jun 2026 23:34:34 +0530
+Subject: cap_proc: use 32-bit-clean setuid/setgid syscalls in the psx path
+
+_cap_setuid() and _cap_setgroups() issued the bare SYS_setuid and
+SYS_setgid system calls. On 32-bit architectures that predate the
+Linux 2.4 uid_t/gid_t widening (i386, arm, ...), these numbers refer
+to the legacy 16-bit system calls, which silently truncate any id
+that does not fit in 16 bits. The glibc wrappers avoid this by using
+the *32 variants, and libcap already did so for setgroups via
+sys_setgroups_variant, but setuid and setgid were missed.
+
+This is a security problem, not a portability nit. The raw syscall is
+taken when libpsx is linked (_libcap_overrode_syscalls == 1), the
+recommended setup for multithreaded programs needing all-threads
+credential changes. On such a build a process calling cap_setuid(0x10000)
+to drop privilege has the id truncated to 0 and stays root; high ids
+from subuid/container ranges land on a different, unintended user.
+
+Select the *32 variants for setuid and setgid alongside the existing
+setgroups handling, and add build-time static assertions so that any
+future regression to a truncating variant on an architecture that
+exposes the legacy calls becomes a compile error.
+
+Verified on x86-64: the tree builds cleanly and cap_test passes; the
+static_assert fires when the syscall is forced back to the legacy
+number. The truncation itself was reproduced on a live kernel via the
+IA32 legacy syscall table (nr 23 truncates 66536 & 0xffff to 1000 and
+succeeds; nr 213 correctly rejects with EPERM).
+
+Signed-off-by: Sayed Kaif <skaif at digiscrypt.com>
+Signed-off-by: Andrew G. Morgan <morgan at kernel.org>
+---
+ libcap/cap_proc.c | 54 +++++++++++++++++++++++++++++++++++++++++++++---------
+ 1 file changed, 45 insertions(+), 9 deletions(-)
+
+diff --git a/libcap/cap_proc.c b/libcap/cap_proc.c
+index 09b74ca..a84d70e 100644
+--- a/libcap/cap_proc.c
++++ b/libcap/cap_proc.c
+@@ -549,6 +549,49 @@ cap_mode_t cap_get_mode(void)
+ return CAP_MODE_NOPRIV;
+ }
+
++/*
++ * Changing the uid/gid of a process must use the 32-bit-clean system
++ * calls. On 32-bit architectures that predate the Linux 2.4 uid_t/gid_t
++ * widening (i386, arm, ...), the bare SYS_setuid/SYS_setgid/SYS_setgroups
++ * numbers refer to the legacy system calls that operate on a 16-bit
++ * identifier and *silently truncate* any value that does not fit. The
++ * glibc setuid()/setgid()/setgroups() wrappers avoid this by issuing the
++ * *32 variants, but the psx syscall path that libcap uses to perform a
++ * (POSIX semantics, all-threads) credential change bypasses glibc, so we
++ * must select the wide variants ourselves. Getting this wrong is a
++ * security problem rather than a mere portability nit: e.g. an attempt to
++ * drop privilege to uid 0x10000 would be truncated to uid 0, leaving the
++ * process running as root.
++ */
++#if defined(__arm__) || defined(__i386__) || \
++ defined(__i486__) || defined(__i586__) || defined(__i686__)
++#define sys_setuid_variant SYS_setuid32
++#define sys_setgid_variant SYS_setgid32
++#define sys_setgroups_variant SYS_setgroups32
++#else
++#define sys_setuid_variant SYS_setuid
++#define sys_setgid_variant SYS_setgid
++#define sys_setgroups_variant SYS_setgroups
++#endif
++
++/*
++ * Whenever the kernel exposes the legacy 16-bit credential system calls
++ * it also exposes the 32-bit-clean *32 variants. Assert at build time
++ * that we never regress to a truncating variant on such architectures.
++ */
++#ifdef SYS_setuid32
++libcap_static_assert(sys_setuid_variant == SYS_setuid32,
++ setuid_syscall_must_be_32bit_clean);
++#endif
++#ifdef SYS_setgid32
++libcap_static_assert(sys_setgid_variant == SYS_setgid32,
++ setgid_syscall_must_be_32bit_clean);
++#endif
++#ifdef SYS_setgroups32
++libcap_static_assert(sys_setgroups_variant == SYS_setgroups32,
++ setgroups_syscall_must_be_32bit_clean);
++#endif
++
+ static int _cap_setuid(struct syscaller_s *sc, uid_t uid)
+ {
+ const cap_value_t raise_cap_setuid[] = {CAP_SETUID};
+@@ -570,7 +613,7 @@ static int _cap_setuid(struct syscaller_s *sc, uid_t uid)
+ int ret = _cap_set_proc(sc, working);
+ if (ret == 0) {
+ if (_libcap_overrode_syscalls) {
+- ret = sc->three(SYS_setuid, (long int) uid, 0, 0);
++ ret = sc->three(sys_setuid_variant, (long int) uid, 0, 0);
+ if (ret < 0) {
+ errno = -ret;
+ ret = -1;
+@@ -600,13 +643,6 @@ int cap_setuid(uid_t uid)
+ return _cap_setuid(&multithread, uid);
+ }
+
+-#if defined(__arm__) || defined(__i386__) || \
+- defined(__i486__) || defined(__i586__) || defined(__i686__)
+-#define sys_setgroups_variant SYS_setgroups32
+-#else
+-#define sys_setgroups_variant SYS_setgroups
+-#endif
+-
+ static int _cap_setgroups(struct syscaller_s *sc,
+ gid_t gid, size_t ngroups, const gid_t groups[])
+ {
+@@ -628,7 +664,7 @@ static int _cap_setgroups(struct syscaller_s *sc,
+ int ret = _cap_set_proc(sc, working);
+ if (_libcap_overrode_syscalls) {
+ if (ret == 0) {
+- ret = sc->three(SYS_setgid, (long int) gid, 0, 0);
++ ret = sc->three(sys_setgid_variant, (long int) gid, 0, 0);
+ }
+ if (ret == 0) {
+ ret = sc->three(sys_setgroups_variant, (long int) ngroups,
+--
+cgit 1.3.1-korg
+
================================================================
---- gitweb:
http://git.pld-linux.org/gitweb.cgi/packages/libcap.git/commitdiff/e81f21fdaa5a0aec7a7ffb2fce1e435be06ad128
More information about the pld-cvs-commit
mailing list