Skip to content

Commit 6a6ea42

Browse files
[AutoPR- Security] Patch libcap for CVE-2026-4878 [MEDIUM] (#16600)
1 parent 8000499 commit 6a6ea42

6 files changed

Lines changed: 176 additions & 11 deletions

File tree

SPECS/libcap/CVE-2026-4878.patch

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
From 0995849402740389d45006b91648d03b220cd769 Mon Sep 17 00:00:00 2001
2+
From: "Andrew G. Morgan" <morgan@kernel.org>
3+
Date: Thu, 12 Mar 2026 07:38:05 -0700
4+
Subject: [PATCH] Address a potential TOCTOU race condition in cap_set_file().
5+
6+
This issue was researched and reported by Ali Raza (@locus-x64). It
7+
has been assigned CVE-2026-4878.
8+
9+
The finding is that while cap_set_file() checks if a file is a regular
10+
file before applying or removing a capability attribute, a small
11+
window existed after that check when the filepath could be overwritten
12+
either with new content or a symlink to some other file. To do this
13+
would imply that the caller of cap_set_file() was directing it to a
14+
directory over which a local attacker has write access, and performed
15+
the operation frequently enough that an attacker had a non-negligible
16+
chance of exploiting the race condition. The code now locks onto the
17+
intended file, eliminating the race condition.
18+
19+
Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
20+
Signed-off-by: Azure Linux Security Servicing Account <azurelinux-security@microsoft.com>
21+
Upstream-reference: https://git.kernel.org/pub/scm/libs/libcap/libcap.git/patch/?id=286ace1259992bd0c5d9016715833f2e148ac596
22+
---
23+
libcap/cap_file.c | 69 +++++++++++++++++++++++++++++++++++++++-------
24+
progs/quicktest.sh | 14 +++++++++-
25+
2 files changed, 72 insertions(+), 11 deletions(-)
26+
27+
diff --git a/libcap/cap_file.c b/libcap/cap_file.c
28+
index 4178705..9418da0 100644
29+
--- a/libcap/cap_file.c
30+
+++ b/libcap/cap_file.c
31+
@@ -8,8 +8,13 @@
32+
#define _DEFAULT_SOURCE
33+
#endif
34+
35+
+#ifndef _GNU_SOURCE
36+
+#define _GNU_SOURCE
37+
+#endif
38+
+
39+
#include <sys/types.h>
40+
#include <byteswap.h>
41+
+#include <fcntl.h>
42+
#include <sys/stat.h>
43+
#include <unistd.h>
44+
#include <linux/xattr.h>
45+
@@ -323,26 +328,70 @@ int cap_set_file(const char *filename, cap_t cap_d)
46+
struct vfs_ns_cap_data rawvfscap;
47+
int sizeofcaps;
48+
struct stat buf;
49+
+ char fdpath[64];
50+
+ int fd, ret;
51+
+
52+
+ _cap_debug("setting filename capabilities");
53+
+ fd = open(filename, O_RDONLY|O_NOFOLLOW);
54+
+ if (fd >= 0) {
55+
+ ret = cap_set_fd(fd, cap_d);
56+
+ close(fd);
57+
+ return ret;
58+
+ }
59+
60+
- if (lstat(filename, &buf) != 0) {
61+
- _cap_debug("unable to stat file [%s]", filename);
62+
+ /*
63+
+ * Attempting to set a file capability on a file the process can't
64+
+ * read the content of. This is considered a non-standard use case
65+
+ * and the following (slower) code is complicated because it is
66+
+ * trying to avoid a TOCTOU race condition.
67+
+ */
68+
+
69+
+ fd = open(filename, O_PATH|O_NOFOLLOW);
70+
+ if (fd < 0) {
71+
+ _cap_debug("cannot find file at path [%s]", filename);
72+
+ return -1;
73+
+ }
74+
+ if (fstat(fd, &buf) != 0) {
75+
+ _cap_debug("unable to stat file [%s] descriptor %d",
76+
+ filename, fd);
77+
+ close(fd);
78+
return -1;
79+
}
80+
if (S_ISLNK(buf.st_mode) || !S_ISREG(buf.st_mode)) {
81+
- _cap_debug("file [%s] is not a regular file", filename);
82+
+ _cap_debug("file [%s] descriptor %d for non-regular file",
83+
+ filename, fd);
84+
+ close(fd);
85+
errno = EINVAL;
86+
return -1;
87+
}
88+
89+
- if (cap_d == NULL) {
90+
- _cap_debug("removing filename capabilities");
91+
- return removexattr(filename, XATTR_NAME_CAPS);
92+
+ /*
93+
+ * While the fd remains open, this named file is locked to the
94+
+ * origin regular file. The size of the fdpath variable is
95+
+ * sufficient to support a 160+ bit number.
96+
+ */
97+
+ if (snprintf(fdpath, sizeof(fdpath), "/proc/self/fd/%d", fd)
98+
+ >= sizeof(fdpath)) {
99+
+ _cap_debug("file descriptor too large %d", fd);
100+
+ errno = EINVAL;
101+
+ ret = -1;
102+
+
103+
+ } else if (cap_d == NULL) {
104+
+ _cap_debug("dropping file caps on [%s] via [%s]",
105+
+ filename, fdpath);
106+
+ ret = removexattr(fdpath, XATTR_NAME_CAPS);
107+
+
108+
} else if (_fcaps_save(&rawvfscap, cap_d, &sizeofcaps) != 0) {
109+
- return -1;
110+
- }
111+
+ _cap_debug("problem converting cap_d to vfscap format");
112+
+ ret = -1;
113+
114+
- _cap_debug("setting filename capabilities");
115+
- return setxattr(filename, XATTR_NAME_CAPS, &rawvfscap, sizeofcaps, 0);
116+
+ } else {
117+
+ _cap_debug("setting filename capabilities");
118+
+ ret = setxattr(fdpath, XATTR_NAME_CAPS, &rawvfscap,
119+
+ sizeofcaps, 0);
120+
+ }
121+
+ close(fd);
122+
+ return ret;
123+
}
124+
125+
/*
126+
diff --git a/progs/quicktest.sh b/progs/quicktest.sh
127+
index 776b175..0b1d706 100755
128+
--- a/progs/quicktest.sh
129+
+++ b/progs/quicktest.sh
130+
@@ -148,7 +148,19 @@ pass_capsh --caps="cap_setpcap=p" --inh=cap_chown --current
131+
pass_capsh --strict --caps="cap_chown=p" --inh=cap_chown --current
132+
133+
# change the way the capability is obtained (make it inheritable)
134+
+chmod 0000 ./privileged
135+
./setcap cap_setuid,cap_setgid=ei ./privileged
136+
+if [ $? -ne 0 ]; then
137+
+ echo "FAILED to set file capability"
138+
+ exit 1
139+
+fi
140+
+chmod 0755 ./privileged
141+
+ln -s privileged unprivileged
142+
+./setcap -r ./unprivileged
143+
+if [ $? -eq 0 ]; then
144+
+ echo "FAILED by removing a capability from a symlinked file"
145+
+ exit 1
146+
+fi
147+
148+
# Note, the bounding set (edited with --drop) only limits p
149+
# capabilities, not i's.
150+
@@ -246,7 +258,7 @@ EOF
151+
pass_capsh --iab='!%cap_chown,^cap_setpcap,cap_setuid'
152+
fail_capsh --mode=PURE1E --iab='!%cap_chown,^cap_setuid'
153+
fi
154+
-/bin/rm -f ./privileged
155+
+/bin/rm -f ./privileged ./unprivileged
156+
157+
echo "testing namespaced file caps"
158+
159+
--
160+
2.45.4
161+

SPECS/libcap/libcap.spec

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Summary: Libcap
22
Name: libcap
33
Version: 2.60
4-
Release: 7%{?dist}
4+
Release: 8%{?dist}
55
License: GPLv2+
66
Group: System Environment/Security
77
URL: https://www.gnu.org/software/hurd/community/gsoc/project_ideas/libcap.html
@@ -11,6 +11,7 @@ Distribution: Mariner
1111
Patch0: CVE-2023-2602.patch
1212
Patch1: CVE-2023-2603.patch
1313
Patch2: CVE-2025-1390.patch
14+
Patch3: CVE-2026-4878.patch
1415
BuildRequires: glibc-static >= 2.35-10%{?dist}
1516

1617
%description
@@ -64,6 +65,9 @@ sed -i '/echo "attempt to exploit kernel bug"/,/^fi$/d' quicktest.sh
6465
%{_mandir}/man3/*
6566

6667
%changelog
68+
* Sat Apr 11 2026 Azure Linux Security Servicing Account <azurelinux-security@microsoft.com> - 2.60-8
69+
- Patch for CVE-2026-4878
70+
6771
* Tue Feb 03 2026 Aditya Singh <v-aditysing@microsoft.com> - 2.60-7
6872
- Bump to rebuild with updated glibc
6973

toolkit/resources/manifests/package/pkggen_core_aarch64.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ openssl-devel-1.1.1k-39.cm2.aarch64.rpm
170170
openssl-libs-1.1.1k-39.cm2.aarch64.rpm
171171
openssl-perl-1.1.1k-39.cm2.aarch64.rpm
172172
openssl-static-1.1.1k-39.cm2.aarch64.rpm
173-
libcap-2.60-7.cm2.aarch64.rpm
174-
libcap-devel-2.60-7.cm2.aarch64.rpm
173+
libcap-2.60-8.cm2.aarch64.rpm
174+
libcap-devel-2.60-8.cm2.aarch64.rpm
175175
debugedit-5.0-2.cm2.aarch64.rpm
176176
libarchive-3.6.1-9.cm2.aarch64.rpm
177177
libarchive-devel-3.6.1-9.cm2.aarch64.rpm

toolkit/resources/manifests/package/pkggen_core_x86_64.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ openssl-devel-1.1.1k-39.cm2.x86_64.rpm
170170
openssl-libs-1.1.1k-39.cm2.x86_64.rpm
171171
openssl-perl-1.1.1k-39.cm2.x86_64.rpm
172172
openssl-static-1.1.1k-39.cm2.x86_64.rpm
173-
libcap-2.60-7.cm2.x86_64.rpm
174-
libcap-devel-2.60-7.cm2.x86_64.rpm
173+
libcap-2.60-8.cm2.x86_64.rpm
174+
libcap-devel-2.60-8.cm2.x86_64.rpm
175175
debugedit-5.0-2.cm2.x86_64.rpm
176176
libarchive-3.6.1-9.cm2.x86_64.rpm
177177
libarchive-devel-3.6.1-9.cm2.x86_64.rpm

toolkit/resources/manifests/package/toolchain_aarch64.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ libassuan-2.5.5-2.cm2.aarch64.rpm
151151
libassuan-debuginfo-2.5.5-2.cm2.aarch64.rpm
152152
libassuan-devel-2.5.5-2.cm2.aarch64.rpm
153153
libbacktrace-static-11.2.0-9.cm2.aarch64.rpm
154-
libcap-2.60-7.cm2.aarch64.rpm
155-
libcap-debuginfo-2.60-7.cm2.aarch64.rpm
156-
libcap-devel-2.60-7.cm2.aarch64.rpm
154+
libcap-2.60-8.cm2.aarch64.rpm
155+
libcap-debuginfo-2.60-8.cm2.aarch64.rpm
156+
libcap-devel-2.60-8.cm2.aarch64.rpm
157157
libcap-ng-0.8.2-2.cm2.aarch64.rpm
158158
libcap-ng-debuginfo-0.8.2-2.cm2.aarch64.rpm
159159
libcap-ng-devel-0.8.2-2.cm2.aarch64.rpm

toolkit/resources/manifests/package/toolchain_x86_64.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ libassuan-2.5.5-2.cm2.x86_64.rpm
157157
libassuan-debuginfo-2.5.5-2.cm2.x86_64.rpm
158158
libassuan-devel-2.5.5-2.cm2.x86_64.rpm
159159
libbacktrace-static-11.2.0-9.cm2.x86_64.rpm
160-
libcap-2.60-7.cm2.x86_64.rpm
161-
libcap-debuginfo-2.60-7.cm2.x86_64.rpm
162-
libcap-devel-2.60-7.cm2.x86_64.rpm
160+
libcap-2.60-8.cm2.x86_64.rpm
161+
libcap-debuginfo-2.60-8.cm2.x86_64.rpm
162+
libcap-devel-2.60-8.cm2.x86_64.rpm
163163
libcap-ng-0.8.2-2.cm2.x86_64.rpm
164164
libcap-ng-debuginfo-0.8.2-2.cm2.x86_64.rpm
165165
libcap-ng-devel-0.8.2-2.cm2.x86_64.rpm

0 commit comments

Comments
 (0)