aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew G. Morgan <morgan@kernel.org>2023-05-03 19:44:22 -0700
committerAndrew G. Morgan <morgan@kernel.org>2023-05-09 18:49:46 -0700
commit422bec25ae4a1ab03fd4d6f728695ed279173b18 (patch)
treead410b049eb57ce24fd45c664e8d253feaa31393
parentbc6b36682f188020ee4770fae1d41bde5b2c97bb (diff)
downloadlibcap-422bec25ae4a1ab03fd4d6f728695ed279173b18.tar.gz
Large strings can confuse libcap's internal strdup code.
Avoid something subtle with really long strings: 1073741823 should be enough for anybody. This is an improved fix over something attempted in libcap-2.55 to address some static analysis findings. Reviewing the library, cap_proc_root() and cap_launcher_set_chroot() are the only two calls where the library is potentially exposed to a user controlled string input. Credit for finding this bug in libcap goes to Richard Weinberger of X41 D-Sec GmbH (https://x41-dsec.de/) who performed a security audit of the libcap source code in April of 2023. The audit was sponsored by the Open Source Technology Improvement Fund (https://ostif.org/). Audit ref: LCAP-CR-23-02 (CVE-2023-2603) Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
-rw-r--r--libcap/cap_alloc.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/libcap/cap_alloc.c b/libcap/cap_alloc.c
index 59fe503..504abd2 100644
--- a/libcap/cap_alloc.c
+++ b/libcap/cap_alloc.c
@@ -106,15 +106,17 @@ __attribute__((visibility ("hidden"))) char *_libcap_strdup(const char *old)
errno = EINVAL;
return NULL;
}
- len = strlen(old) + 1 + 2*sizeof(__u32);
- if (len < sizeof(struct _cap_alloc_s)) {
- len = sizeof(struct _cap_alloc_s);
- }
- if ((len & 0xffffffff) != len) {
+
+ len = strlen(old);
+ if ((len & 0x3fffffff) != len) {
_cap_debug("len is too long for libcap to manage");
errno = EINVAL;
return NULL;
}
+ len += 1 + 2*sizeof(__u32);
+ if (len < sizeof(struct _cap_alloc_s)) {
+ len = sizeof(struct _cap_alloc_s);
+ }
raw_data = calloc(1, len);
if (raw_data == NULL) {