Skip to content

Commit 261680b

Browse files
committed
basic/capability-util: let cap_last_cap() return unsigned integer
We never return anything higher than 63, so using "long unsigned" as the type only confused the reader. (We can still use "long unsigned" and safe_atolu() to parse the kernel file.) (cherry picked from commit 864a25d)
1 parent 325edff commit 261680b

File tree

5 files changed

+18
-27
lines changed

5 files changed

+18
-27
lines changed

src/basic/cap-list.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,19 @@ int capability_list_length(void) {
5757

5858
int capability_set_to_string_alloc(uint64_t set, char **s) {
5959
_cleanup_free_ char *str = NULL;
60-
unsigned long i;
6160
size_t allocated = 0, n = 0;
6261

6362
assert(s);
6463

65-
for (i = 0; i <= cap_last_cap(); i++)
64+
for (unsigned i = 0; i <= cap_last_cap(); i++)
6665
if (set & (UINT64_C(1) << i)) {
6766
const char *p;
6867
char buf[2 + 16 + 1];
6968
size_t add;
7069

7170
p = capability_to_name(i);
7271
if (!p) {
73-
xsprintf(buf, "0x%lx", i);
72+
xsprintf(buf, "0x%x", i);
7473
p = buf;
7574
}
7675

src/basic/capability-util.c

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ int have_effective_cap(int value) {
3131
return fv == CAP_SET;
3232
}
3333

34-
unsigned long cap_last_cap(void) {
35-
static thread_local unsigned long saved;
34+
unsigned cap_last_cap(void) {
35+
static thread_local unsigned saved;
3636
static thread_local bool valid = false;
3737
_cleanup_free_ char *content = NULL;
3838
unsigned long p = 0;
@@ -65,7 +65,7 @@ unsigned long cap_last_cap(void) {
6565
if (prctl(PR_CAPBSET_READ, p) < 0) {
6666

6767
/* Hmm, look downwards, until we find one that works */
68-
for (p--; p > 0; p --)
68+
for (p--; p > 0; p--)
6969
if (prctl(PR_CAPBSET_READ, p) >= 0)
7070
break;
7171

@@ -84,12 +84,10 @@ unsigned long cap_last_cap(void) {
8484
}
8585

8686
int capability_update_inherited_set(cap_t caps, uint64_t set) {
87-
unsigned long i;
88-
8987
/* Add capabilities in the set to the inherited caps, drops capabilities not in the set.
9088
* Do not apply them yet. */
9189

92-
for (i = 0; i <= cap_last_cap(); i++) {
90+
for (unsigned i = 0; i <= cap_last_cap(); i++) {
9391
cap_flag_value_t flag = set & (UINT64_C(1) << i) ? CAP_SET : CAP_CLEAR;
9492
cap_value_t v;
9593

@@ -104,11 +102,10 @@ int capability_update_inherited_set(cap_t caps, uint64_t set) {
104102

105103
int capability_ambient_set_apply(uint64_t set, bool also_inherit) {
106104
_cleanup_cap_free_ cap_t caps = NULL;
107-
unsigned long i;
108105
int r;
109106

110107
/* Remove capabilities requested in ambient set, but not in the bounding set */
111-
for (i = 0; i <= cap_last_cap(); i++) {
108+
for (unsigned i = 0; i <= cap_last_cap(); i++) {
112109
if (set == 0)
113110
break;
114111

@@ -140,7 +137,7 @@ int capability_ambient_set_apply(uint64_t set, bool also_inherit) {
140137
return -errno;
141138
}
142139

143-
for (i = 0; i <= cap_last_cap(); i++) {
140+
for (unsigned i = 0; i <= cap_last_cap(); i++) {
144141

145142
if (set & (UINT64_C(1) << i)) {
146143

@@ -167,7 +164,6 @@ int capability_ambient_set_apply(uint64_t set, bool also_inherit) {
167164
int capability_bounding_set_drop(uint64_t keep, bool right_now) {
168165
_cleanup_cap_free_ cap_t before_cap = NULL, after_cap = NULL;
169166
cap_flag_value_t fv;
170-
unsigned long i;
171167
int r;
172168

173169
/* If we are run as PID 1 we will lack CAP_SETPCAP by default
@@ -204,7 +200,7 @@ int capability_bounding_set_drop(uint64_t keep, bool right_now) {
204200
if (!after_cap)
205201
return -errno;
206202

207-
for (i = 0; i <= cap_last_cap(); i++) {
203+
for (unsigned i = 0; i <= cap_last_cap(); i++) {
208204
cap_value_t v;
209205

210206
if ((keep & (UINT64_C(1) << i)))
@@ -390,7 +386,6 @@ bool ambient_capabilities_supported(void) {
390386
}
391387

392388
bool capability_quintet_mangle(CapabilityQuintet *q) {
393-
unsigned long i;
394389
uint64_t combined, drop = 0;
395390
bool ambient_supported;
396391

@@ -402,7 +397,7 @@ bool capability_quintet_mangle(CapabilityQuintet *q) {
402397
if (ambient_supported)
403398
combined |= q->ambient;
404399

405-
for (i = 0; i <= cap_last_cap(); i++) {
400+
for (unsigned i = 0; i <= cap_last_cap(); i++) {
406401
unsigned long bit = UINT64_C(1) << i;
407402
if (!FLAGS_SET(combined, bit))
408403
continue;
@@ -431,16 +426,15 @@ int capability_quintet_enforce(const CapabilityQuintet *q) {
431426
int r;
432427

433428
if (q->ambient != (uint64_t) -1) {
434-
unsigned long i;
435429
bool changed = false;
436430

437431
c = cap_get_proc();
438432
if (!c)
439433
return -errno;
440434

441-
/* In order to raise the ambient caps set we first need to raise the matching inheritable + permitted
442-
* cap */
443-
for (i = 0; i <= cap_last_cap(); i++) {
435+
/* In order to raise the ambient caps set we first need to raise the matching
436+
* inheritable + permitted cap */
437+
for (unsigned i = 0; i <= cap_last_cap(); i++) {
444438
uint64_t m = UINT64_C(1) << i;
445439
cap_value_t cv = (cap_value_t) i;
446440
cap_flag_value_t old_value_inheritable, old_value_permitted;
@@ -475,15 +469,14 @@ int capability_quintet_enforce(const CapabilityQuintet *q) {
475469

476470
if (q->inheritable != (uint64_t) -1 || q->permitted != (uint64_t) -1 || q->effective != (uint64_t) -1) {
477471
bool changed = false;
478-
unsigned long i;
479472

480473
if (!c) {
481474
c = cap_get_proc();
482475
if (!c)
483476
return -errno;
484477
}
485478

486-
for (i = 0; i <= cap_last_cap(); i++) {
479+
for (unsigned i = 0; i <= cap_last_cap(); i++) {
487480
uint64_t m = UINT64_C(1) << i;
488481
cap_value_t cv = (cap_value_t) i;
489482

src/basic/capability-util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#define CAP_ALL (uint64_t) -1
1414

15-
unsigned long cap_last_cap(void);
15+
unsigned cap_last_cap(void);
1616
int have_effective_cap(int value);
1717
int capability_bounding_set_drop(uint64_t keep, bool right_now);
1818
int capability_bounding_set_drop_usermode(uint64_t keep);

src/libsystemd/sd-bus/bus-creds.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,16 +650,15 @@ _public_ int sd_bus_creds_get_description(sd_bus_creds *c, const char **ret) {
650650
}
651651

652652
static int has_cap(sd_bus_creds *c, size_t offset, int capability) {
653-
unsigned long lc;
654653
size_t sz;
655654

656655
assert(c);
657656
assert(capability >= 0);
658657
assert(c->capability);
659658

660-
lc = cap_last_cap();
659+
unsigned lc = cap_last_cap();
661660

662-
if ((unsigned long) capability > lc)
661+
if ((unsigned) capability > lc)
663662
return 0;
664663

665664
/* If the last cap is 63, then there are 64 caps defined, and we need 2 entries á 32bit hence. *

src/test/test-capability.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ static void test_ensure_cap_64bit(void) {
240240
assert_se(p <= 63);
241241

242242
/* Also check for the header definition */
243-
assert_se(CAP_LAST_CAP <= 63);
243+
assert_cc(CAP_LAST_CAP <= 63);
244244
}
245245

246246
int main(int argc, char *argv[]) {

0 commit comments

Comments
 (0)