Skip to content

Commit 5211445

Browse files
committed
capability: let's protect against the kernel eventually doing more than 64 caps
Everyone will be in trouble then (as quite widely caps are store in 64bit fields). But let's protect ourselves at least to the point that we ignore all higher caps for now.
1 parent 248dd94 commit 5211445

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/basic/capability-util.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ unsigned long cap_last_cap(void) {
4747
if (r >= 0) {
4848
r = safe_atolu(content, &p);
4949
if (r >= 0) {
50+
51+
if (p > 63) /* Safety for the future: if one day the kernel learns more than 64 caps,
52+
* then we are in trouble (since we, as much userspace and kernel space
53+
* store capability masks in uint64_t types. Let's hence protect
54+
* ourselves against that and always cap at 63 for now. */
55+
p = 63;
56+
5057
saved = p;
5158
valid = true;
5259
return p;
@@ -58,17 +65,15 @@ unsigned long cap_last_cap(void) {
5865

5966
if (prctl(PR_CAPBSET_READ, p) < 0) {
6067

61-
/* Hmm, look downwards, until we find one that
62-
* works */
68+
/* Hmm, look downwards, until we find one that works */
6369
for (p--; p > 0; p --)
6470
if (prctl(PR_CAPBSET_READ, p) >= 0)
6571
break;
6672

6773
} else {
6874

69-
/* Hmm, look upwards, until we find one that doesn't
70-
* work */
71-
for (;; p++)
75+
/* Hmm, look upwards, until we find one that doesn't work */
76+
for (; p < 63; p++)
7277
if (prctl(PR_CAPBSET_READ, p+1) < 0)
7378
break;
7479
}

0 commit comments

Comments
 (0)