| 1 |
/* lib_test.c -- simple libcap-ng test suite |
| 2 |
* Copyright 2009,2012-13 Red Hat Inc. |
| 3 |
* All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public License |
| 16 |
* along with this program; see the file COPYING.LIB. If not, write to the |
| 17 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor |
| 18 |
* Boston, MA 02110-1335, USA. |
| 19 |
* |
| 20 |
* Authors: |
| 21 |
* Steve Grubb <[email protected]> |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "config.h" |
| 25 |
#include "../cap-ng.h" |
| 26 |
#include <stdlib.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <string.h> |
| 29 |
#include <errno.h> |
| 30 |
#include <fcntl.h> |
| 31 |
#include <sys/stat.h> |
| 32 |
#include <stdint.h> |
| 33 |
#include <unistd.h> |
| 34 |
|
| 35 |
struct proc_caps { |
| 36 |
uint64_t effective; |
| 37 |
uint64_t permitted; |
| 38 |
uint64_t inheritable; |
| 39 |
}; |
| 40 |
|
| 41 |
static unsigned int get_last_cap(void) |
| 42 |
{ |
| 43 |
int fd; |
| 44 |
|
| 45 |
fd = open("/proc/sys/kernel/cap_last_cap", O_RDONLY); |
| 46 |
if (fd == -1) { |
| 47 |
return CAP_LAST_CAP; |
| 48 |
} else { |
| 49 |
char buf[8]; |
| 50 |
int num = read(fd, buf, sizeof(buf)); |
| 51 |
if (num > 0) { |
| 52 |
errno = 0; |
| 53 |
unsigned int val = strtoul(buf, NULL, 10); |
| 54 |
if (errno == 0) |
| 55 |
return val; |
| 56 |
} |
| 57 |
close(fd); |
| 58 |
} |
| 59 |
return CAP_LAST_CAP; |
| 60 |
} |
| 61 |
|
| 62 |
static int read_process_caps(struct proc_caps *caps) |
| 63 |
{ |
| 64 |
FILE *f; |
| 65 |
char buf[128]; |
| 66 |
unsigned long long val; |
| 67 |
int found = 0; |
| 68 |
|
| 69 |
memset(caps, 0, sizeof(*caps)); |
| 70 |
f = fopen("/proc/self/status", "re"); |
| 71 |
if (f == NULL) |
| 72 |
return -1; |
| 73 |
|
| 74 |
while (fgets(buf, sizeof(buf), f)) { |
| 75 |
if (sscanf(buf, "CapEff:\t%llx", &val) == 1) { |
| 76 |
caps->effective = val; |
| 77 |
found |= 1; |
| 78 |
} else if (sscanf(buf, "CapPrm:\t%llx", &val) == 1) { |
| 79 |
caps->permitted = val; |
| 80 |
found |= 2; |
| 81 |
} else if (sscanf(buf, "CapInh:\t%llx", &val) == 1) { |
| 82 |
caps->inheritable = val; |
| 83 |
found |= 4; |
| 84 |
} |
| 85 |
} |
| 86 |
fclose(f); |
| 87 |
|
| 88 |
return found == 7 ? 0 : -1; |
| 89 |
} |
| 90 |
|
| 91 |
static int expected_cap(uint64_t set, unsigned int capability) |
| 92 |
{ |
| 93 |
return (set & (1ULL << capability)) ? 1 : 0; |
| 94 |
} |
| 95 |
|
| 96 |
static capng_results_t expected_caps_result(uint64_t set, unsigned int last) |
| 97 |
{ |
| 98 |
unsigned int i; |
| 99 |
int found = 0, missing = 0; |
| 100 |
|
| 101 |
for (i = 0; i <= last; i++) { |
| 102 |
if (expected_cap(set, i)) |
| 103 |
found = 1; |
| 104 |
else |
| 105 |
missing = 1; |
| 106 |
} |
| 107 |
if (!found) |
| 108 |
return CAPNG_NONE; |
| 109 |
if (!missing) |
| 110 |
return CAPNG_FULL; |
| 111 |
return CAPNG_PARTIAL; |
| 112 |
} |
| 113 |
|
| 114 |
int main(void) |
| 115 |
{ |
| 116 |
int rc; |
| 117 |
unsigned int i, len, last = get_last_cap(); |
| 118 |
struct proc_caps caps; |
| 119 |
char expected[128]; |
| 120 |
char *text; |
| 121 |
const char *name; |
| 122 |
void *saved; |
| 123 |
|
| 124 |
puts("Doing process capability tests..."); |
| 125 |
if (read_process_caps(&caps)) { |
| 126 |
puts("Failed reading process capabilities from procfs"); |
| 127 |
abort(); |
| 128 |
} |
| 129 |
if (capng_get_caps_process()) { |
| 130 |
puts("Failed getting process capabilities"); |
| 131 |
abort(); |
| 132 |
} |
| 133 |
|
| 134 |
for (i = 0; i <= last; i++) { |
| 135 |
if (capng_have_capability(CAPNG_PERMITTED, i) != |
| 136 |
expected_cap(caps.permitted, i)) { |
| 137 |
puts("Failed process permitted capabilities test"); |
| 138 |
abort(); |
| 139 |
} |
| 140 |
if (capng_have_capability(CAPNG_EFFECTIVE, i) != |
| 141 |
expected_cap(caps.effective, i)) { |
| 142 |
puts("Failed process effective capabilities test"); |
| 143 |
abort(); |
| 144 |
} |
| 145 |
if (capng_have_capability(CAPNG_INHERITABLE, i) != |
| 146 |
expected_cap(caps.inheritable, i)) { |
| 147 |
puts("Failed process inheritable capabilities test"); |
| 148 |
abort(); |
| 149 |
} |
| 150 |
} |
| 151 |
if (capng_have_permitted_capabilities() != |
| 152 |
expected_caps_result(caps.permitted, last)) { |
| 153 |
puts("Failed process permitted capabilities aggregate test"); |
| 154 |
abort(); |
| 155 |
} |
| 156 |
if (capng_have_capabilities(CAPNG_SELECT_CAPS) != |
| 157 |
expected_caps_result(caps.effective, last)) { |
| 158 |
puts("Failed process effective capabilities aggregate test"); |
| 159 |
abort(); |
| 160 |
} |
| 161 |
text = capng_print_caps_numeric(CAPNG_PRINT_BUFFER, CAPNG_SELECT_CAPS); |
| 162 |
if (text == NULL) { |
| 163 |
puts("Failed getting process numeric capabilities"); |
| 164 |
abort(); |
| 165 |
} |
| 166 |
snprintf(expected, sizeof(expected), |
| 167 |
"Effective: %08X, %08X\n" |
| 168 |
"Permitted: %08X, %08X\n" |
| 169 |
"Inheritable: %08X, %08X\n", |
| 170 |
(unsigned int)(caps.effective >> 32), |
| 171 |
(unsigned int)(caps.effective & 0xFFFFFFFFU), |
| 172 |
(unsigned int)(caps.permitted >> 32), |
| 173 |
(unsigned int)(caps.permitted & 0xFFFFFFFFU), |
| 174 |
(unsigned int)(caps.inheritable >> 32), |
| 175 |
(unsigned int)(caps.inheritable & 0xFFFFFFFFU)); |
| 176 |
if (strcmp(text, expected)) { |
| 177 |
snprintf(expected, sizeof(expected), |
| 178 |
"Effective: %08X\n" |
| 179 |
"Permitted: %08X\n" |
| 180 |
"Inheritable: %08X\n", |
| 181 |
(unsigned int)caps.effective, |
| 182 |
(unsigned int)caps.permitted, |
| 183 |
(unsigned int)caps.inheritable); |
| 184 |
if (strcmp(text, expected)) { |
| 185 |
puts("Failed process numeric capabilities test"); |
| 186 |
free(text); |
| 187 |
abort(); |
| 188 |
} |
| 189 |
} |
| 190 |
free(text); |
| 191 |
|
| 192 |
puts("Doing basic bit tests..."); |
| 193 |
capng_clear(CAPNG_SELECT_BOTH); |
| 194 |
errno = 0; |
| 195 |
rc = capng_apply(0); |
| 196 |
if (rc != -1 || errno != EINVAL) { |
| 197 |
puts("Failed apply empty selection test"); |
| 198 |
abort(); |
| 199 |
} |
| 200 |
if (capng_have_permitted_capabilities() != CAPNG_NONE) { |
| 201 |
puts("Failed permitted capabilities none test"); |
| 202 |
abort(); |
| 203 |
} |
| 204 |
saved = capng_save_state(); |
| 205 |
capng_fill(CAPNG_SELECT_BOTH); |
| 206 |
if (capng_have_permitted_capabilities() != CAPNG_FULL) { |
| 207 |
puts("Failed permitted capabilities full test"); |
| 208 |
abort(); |
| 209 |
} |
| 210 |
capng_restore_state(&saved); |
| 211 |
capng_clear(CAPNG_SELECT_BOTH); |
| 212 |
rc = capng_update(CAPNG_ADD, CAPNG_PERMITTED, CAP_CHOWN); |
| 213 |
if (rc) { |
| 214 |
puts("Failed update permitted test"); |
| 215 |
abort(); |
| 216 |
} |
| 217 |
if (capng_have_permitted_capabilities() != CAPNG_PARTIAL) { |
| 218 |
puts("Failed permitted capabilities partial test"); |
| 219 |
abort(); |
| 220 |
} |
| 221 |
if (capng_have_capabilities(CAPNG_SELECT_BOTH) != CAPNG_NONE) { |
| 222 |
puts("Failed clearing capabilities"); |
| 223 |
abort(); |
| 224 |
} |
| 225 |
saved = capng_save_state(); |
| 226 |
capng_fill(CAPNG_SELECT_BOTH); |
| 227 |
if (capng_have_capabilities(CAPNG_SELECT_BOTH) != CAPNG_FULL) { |
| 228 |
puts("Failed filling capabilities"); |
| 229 |
abort(); |
| 230 |
} |
| 231 |
// Need to detect if version 1 or 2 capabilities |
| 232 |
text = capng_print_caps_numeric(CAPNG_PRINT_BUFFER, CAPNG_SELECT_CAPS); |
| 233 |
len = strlen(text); |
| 234 |
free(text); |
| 235 |
if (len < 80 && last > 30) // The kernel & headers are mismatched |
| 236 |
last = 30; |
| 237 |
// Now test that restore still works |
| 238 |
capng_restore_state(&saved); |
| 239 |
if (capng_have_capabilities(CAPNG_SELECT_BOTH) != CAPNG_NONE) { |
| 240 |
puts("Failed restoring capabilities"); |
| 241 |
abort(); |
| 242 |
} |
| 243 |
printf("Doing advanced bit tests for %d capabilities...\n", last); |
| 244 |
for (i=0; i<=last; i++) { |
| 245 |
capng_clear(CAPNG_SELECT_BOTH); |
| 246 |
rc = capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, i); |
| 247 |
if (rc) { |
| 248 |
puts("Failed update test 1"); |
| 249 |
abort(); |
| 250 |
} |
| 251 |
rc = capng_have_capability(CAPNG_EFFECTIVE, i); |
| 252 |
if (rc == 0) { |
| 253 |
puts("Failed have capability test 1"); |
| 254 |
capng_print_caps_numeric(CAPNG_PRINT_STDOUT, |
| 255 |
CAPNG_SELECT_CAPS); |
| 256 |
abort(); |
| 257 |
} |
| 258 |
if(capng_have_capabilities(CAPNG_SELECT_CAPS)!=CAPNG_PARTIAL){ |
| 259 |
puts("Failed have capabilities test 1"); |
| 260 |
capng_print_caps_numeric(CAPNG_PRINT_STDOUT, |
| 261 |
CAPNG_SELECT_CAPS); |
| 262 |
abort(); |
| 263 |
} |
| 264 |
#if CAP_LAST_CAP > 31 |
| 265 |
rc = capng_update(CAPNG_ADD, CAPNG_BOUNDING_SET, i); |
| 266 |
if (rc) { |
| 267 |
puts("Failed bset update test 2"); |
| 268 |
abort(); |
| 269 |
} |
| 270 |
rc = capng_have_capability(CAPNG_BOUNDING_SET, i); |
| 271 |
if (rc == 0) { |
| 272 |
puts("Failed bset have capability test 2"); |
| 273 |
capng_print_caps_numeric(CAPNG_PRINT_STDOUT, |
| 274 |
CAPNG_SELECT_BOTH); |
| 275 |
abort(); |
| 276 |
} |
| 277 |
if(capng_have_capabilities(CAPNG_SELECT_BOUNDS)!=CAPNG_PARTIAL){ |
| 278 |
puts("Failed bset have capabilities test 2"); |
| 279 |
capng_print_caps_numeric(CAPNG_PRINT_STDOUT, |
| 280 |
CAPNG_SELECT_BOTH); |
| 281 |
abort(); |
| 282 |
} |
| 283 |
#endif |
| 284 |
text=capng_print_caps_text(CAPNG_PRINT_BUFFER, CAPNG_EFFECTIVE); |
| 285 |
if (text == NULL) { |
| 286 |
puts("Failed getting print text to buffer"); |
| 287 |
abort(); |
| 288 |
} |
| 289 |
name = capng_capability_to_name(i); |
| 290 |
if (name == NULL) { |
| 291 |
printf("Failed converting capability %d to name\n", i); |
| 292 |
abort(); |
| 293 |
} |
| 294 |
if (strcmp(text, name)) { |
| 295 |
puts("Failed print text comparison"); |
| 296 |
printf("%s != %s\n", text, name); |
| 297 |
abort(); |
| 298 |
} |
| 299 |
free(text); |
| 300 |
// Now make sure the mask part is working |
| 301 |
capng_fill(CAPNG_SELECT_BOTH); |
| 302 |
rc = capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, i); |
| 303 |
if (rc) { |
| 304 |
puts("Failed update test 3"); |
| 305 |
abort(); |
| 306 |
} |
| 307 |
// Should be partial |
| 308 |
if(capng_have_capabilities(CAPNG_SELECT_CAPS)!=CAPNG_PARTIAL){ |
| 309 |
puts("Failed have capabilities test 3"); |
| 310 |
capng_print_caps_numeric(CAPNG_PRINT_STDOUT, |
| 311 |
CAPNG_SELECT_CAPS); |
| 312 |
abort(); |
| 313 |
} |
| 314 |
// Add back the bit and should be full capabilities |
| 315 |
rc = capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, i); |
| 316 |
if (rc) { |
| 317 |
puts("Failed update test 4"); |
| 318 |
abort(); |
| 319 |
} |
| 320 |
if (capng_have_capabilities(CAPNG_SELECT_CAPS) != CAPNG_FULL){ |
| 321 |
puts("Failed have capabilities test 4"); |
| 322 |
capng_print_caps_numeric(CAPNG_PRINT_STDOUT, |
| 323 |
CAPNG_SELECT_CAPS); |
| 324 |
abort(); |
| 325 |
} |
| 326 |
} |
| 327 |
// Verify text formatting when many capabilities are present |
| 328 |
capng_clear(CAPNG_SELECT_BOTH); |
| 329 |
for (i=0; i<=last; i++) { |
| 330 |
rc = capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, i); |
| 331 |
if (rc) { |
| 332 |
puts("Failed setup test for print buffer length"); |
| 333 |
abort(); |
| 334 |
} |
| 335 |
} |
| 336 |
text = capng_print_caps_text(CAPNG_PRINT_BUFFER, CAPNG_EFFECTIVE); |
| 337 |
if (text == NULL) { |
| 338 |
puts("Failed getting full print text to buffer"); |
| 339 |
abort(); |
| 340 |
} |
| 341 |
len = 0; |
| 342 |
for (i=0; i<=last; i++) { |
| 343 |
if (capng_have_capability(CAPNG_EFFECTIVE, i)) { |
| 344 |
name = capng_capability_to_name(i); |
| 345 |
if (name == NULL) |
| 346 |
name = "unknown"; |
| 347 |
if (len) |
| 348 |
len += 2; |
| 349 |
len += strlen(name); |
| 350 |
} |
| 351 |
} |
| 352 |
if (strlen(text) != len) { |
| 353 |
puts("Failed print text length comparison"); |
| 354 |
printf("%zu != %u\n", strlen(text), len); |
| 355 |
abort(); |
| 356 |
} |
| 357 |
free(text); |
| 358 |
|
| 359 |
// Now test the updatev function |
| 360 |
capng_clear(CAPNG_SELECT_BOTH); |
| 361 |
rc = capng_updatev(CAPNG_ADD, CAPNG_EFFECTIVE, |
| 362 |
CAP_CHOWN, CAP_FOWNER, CAP_KILL, -1); |
| 363 |
if (rc) { |
| 364 |
puts("Failed updatev test"); |
| 365 |
abort(); |
| 366 |
} |
| 367 |
rc = capng_have_capability(CAPNG_EFFECTIVE, CAP_CHOWN) && |
| 368 |
capng_have_capability(CAPNG_EFFECTIVE, CAP_FOWNER) && |
| 369 |
capng_have_capability(CAPNG_EFFECTIVE, CAP_KILL); |
| 370 |
if (rc == 0) { |
| 371 |
puts("Failed have updatev capability test"); |
| 372 |
capng_print_caps_numeric(CAPNG_PRINT_STDOUT, |
| 373 |
CAPNG_SELECT_CAPS); |
| 374 |
abort(); |
| 375 |
} |
| 376 |
|
| 377 |
return EXIT_SUCCESS; |
| 378 |
} |