| 1 |
/* libcap-ng.c -- |
| 2 |
* Copyright 2009-10, 2013, 2017, 2020-26 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 <string.h> |
| 27 |
#include <stdarg.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <stdio_ext.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <limits.h> |
| 32 |
#include <sys/prctl.h> |
| 33 |
#include <pwd.h> |
| 34 |
#include <grp.h> |
| 35 |
#include <sys/stat.h> |
| 36 |
#include <errno.h> |
| 37 |
#include <fcntl.h> |
| 38 |
#include <endian.h> |
| 39 |
#include <byteswap.h> |
| 40 |
#ifdef HAVE_PTHREAD_H |
| 41 |
#include <pthread.h> // For pthread_atfork |
| 42 |
#endif |
| 43 |
#ifdef HAVE_SYSCALL_H |
| 44 |
#include <sys/syscall.h> |
| 45 |
#endif |
| 46 |
#ifdef HAVE_LINUX_SECUREBITS_H |
| 47 |
#include <linux/securebits.h> |
| 48 |
#endif |
| 49 |
#ifdef HAVE_LINUX_MAGIC_H |
| 50 |
#include <sys/vfs.h> |
| 51 |
#include <linux/magic.h> |
| 52 |
#endif |
| 53 |
|
| 54 |
# define hidden __attribute__ ((visibility ("hidden"))) |
| 55 |
unsigned int last_cap hidden = 0; |
| 56 |
/* |
| 57 |
* Some milestones of when things became available: |
| 58 |
* 2.6.24 kernel XATTR_NAME_CAPS |
| 59 |
* 2.6.25 kernel PR_CAPBSET_DROP, CAPABILITY_VERSION_2 |
| 60 |
* 2.6.26 kernel PR_SET_SECUREBITS, SECURE_*_LOCKED, VERSION_3 |
| 61 |
* 3.5 kernel PR_SET_NO_NEW_PRIVS |
| 62 |
* 4.3 kernel PR_CAP_AMBIENT |
| 63 |
* 4.14 kernel VFS_CAP_REVISION_3 |
| 64 |
*/ |
| 65 |
#ifdef PR_CAPBSET_DROP |
| 66 |
static int HAVE_PR_CAPBSET_DROP = 0; |
| 67 |
#endif |
| 68 |
#ifdef PR_CAP_AMBIENT |
| 69 |
static int HAVE_PR_CAP_AMBIENT = 0; |
| 70 |
#endif |
| 71 |
|
| 72 |
/* External syscall prototypes */ |
| 73 |
extern int capset(cap_user_header_t header, cap_user_data_t data); |
| 74 |
extern int capget(cap_user_header_t header, const cap_user_data_t data); |
| 75 |
|
| 76 |
// Local functions |
| 77 |
static void update_bounding_set(capng_act_t action, unsigned int capability, |
| 78 |
unsigned int idx); |
| 79 |
static void update_ambient_set(capng_act_t action, unsigned int capability, |
| 80 |
unsigned int idx); |
| 81 |
|
| 82 |
// Local defines |
| 83 |
#define MASK(x) (1U << (x)) |
| 84 |
#ifdef PR_CAPBSET_DROP |
| 85 |
#define UPPER_MASK ~((~0U)<<(last_cap-31)) |
| 86 |
#else |
| 87 |
// For v1 systems UPPER_MASK will never be used |
| 88 |
#define UPPER_MASK (unsigned)(~0U) |
| 89 |
#endif |
| 90 |
|
| 91 |
// Re-define cap_valid so its uniform between V1 and V3 |
| 92 |
#undef cap_valid |
| 93 |
#define cap_valid(x) ((x) <= last_cap) |
| 94 |
|
| 95 |
// If we don't have the xattr library, then we can't |
| 96 |
// compile-in file system capabilities |
| 97 |
#if !defined(HAVE_ATTR_XATTR_H) && !defined (HAVE_SYS_XATTR_H) |
| 98 |
#undef VFS_CAP_U32 |
| 99 |
#endif |
| 100 |
|
| 101 |
#ifdef VFS_CAP_U32 |
| 102 |
#ifdef HAVE_SYS_XATTR_H |
| 103 |
#include <sys/xattr.h> |
| 104 |
#else |
| 105 |
#ifdef HAVE_ATTR_XATTR_H |
| 106 |
#include <attr/xattr.h> |
| 107 |
#endif |
| 108 |
#endif |
| 109 |
#if __BYTE_ORDER == __BIG_ENDIAN |
| 110 |
#define FIXUP(x) bswap_32(x) |
| 111 |
#else |
| 112 |
#define FIXUP(x) (x) |
| 113 |
#endif |
| 114 |
#endif |
| 115 |
|
| 116 |
#ifndef _LINUX_CAPABILITY_VERSION_1 |
| 117 |
#define _LINUX_CAPABILITY_VERSION_1 0x19980330 |
| 118 |
#endif |
| 119 |
#ifndef _LINUX_CAPABILITY_VERSION_2 |
| 120 |
#define _LINUX_CAPABILITY_VERSION_2 0x20071026 |
| 121 |
#endif |
| 122 |
#ifndef _LINUX_CAPABILITY_VERSION_3 |
| 123 |
#define _LINUX_CAPABILITY_VERSION_3 0x20080522 |
| 124 |
#endif |
| 125 |
|
| 126 |
// This public API went private in the 2.6.36 kernel - hope it never changes |
| 127 |
#ifndef XATTR_CAPS_SUFFIX |
| 128 |
#define XATTR_CAPS_SUFFIX "capability" |
| 129 |
#endif |
| 130 |
#ifndef XATTR_SECURITY_PREFIX |
| 131 |
#define XATTR_SECURITY_PREFIX "security." |
| 132 |
#endif |
| 133 |
#ifndef XATTR_NAME_CAPS |
| 134 |
#define XATTR_NAME_CAPS XATTR_SECURITY_PREFIX XATTR_CAPS_SUFFIX |
| 135 |
#endif |
| 136 |
|
| 137 |
|
| 138 |
/* Child processes can't get caps back */ |
| 139 |
#ifndef SECURE_NOROOT |
| 140 |
#define SECURE_NOROOT 0 |
| 141 |
#endif |
| 142 |
#ifndef SECURE_NOROOT_LOCKED |
| 143 |
#define SECURE_NOROOT_LOCKED 1 /* make bit-0 immutable */ |
| 144 |
#endif |
| 145 |
/* Setuid apps run by uid 0 don't get caps back */ |
| 146 |
#ifndef SECURE_NO_SETUID_FIXUP |
| 147 |
#define SECURE_NO_SETUID_FIXUP 2 |
| 148 |
#endif |
| 149 |
#ifndef SECURE_NO_SETUID_FIXUP_LOCKED |
| 150 |
#define SECURE_NO_SETUID_FIXUP_LOCKED 3 /* make bit-2 immutable */ |
| 151 |
#endif |
| 152 |
|
| 153 |
#ifndef VFS_CAP_U32 |
| 154 |
#define VFS_CAP_U32 2 |
| 155 |
#endif |
| 156 |
|
| 157 |
#if (VFS_CAP_U32 != 2) |
| 158 |
#error VFS_CAP_U32 does not match the library, you need a new version |
| 159 |
#endif |
| 160 |
|
| 161 |
|
| 162 |
// States: new, allocated, initted, updated, applied |
| 163 |
typedef enum { CAPNG_NEW, CAPNG_ERROR, CAPNG_ALLOCATED, CAPNG_INIT, |
| 164 |
CAPNG_UPDATED, CAPNG_APPLIED } capng_states_t; |
| 165 |
|
| 166 |
// Create an easy data struct out of the kernel definitions |
| 167 |
typedef union { |
| 168 |
struct __user_cap_data_struct v1; |
| 169 |
struct __user_cap_data_struct v3[VFS_CAP_U32]; |
| 170 |
} cap_data_t; |
| 171 |
|
| 172 |
// This struct keeps all state info |
| 173 |
struct cap_ng |
| 174 |
{ |
| 175 |
int cap_ver; |
| 176 |
int vfs_cap_ver; |
| 177 |
struct __user_cap_header_struct hdr; |
| 178 |
cap_data_t data; |
| 179 |
capng_states_t state; |
| 180 |
__le32 rootid; |
| 181 |
__u32 bounds[VFS_CAP_U32]; |
| 182 |
__u32 ambient[VFS_CAP_U32]; |
| 183 |
unsigned char bounds_state_changed; |
| 184 |
gid_t *add_groups; |
| 185 |
size_t add_group_cnt; |
| 186 |
}; |
| 187 |
|
| 188 |
// Global variables with per thread uniqueness |
| 189 |
static __thread struct cap_ng m = { 1, 1, |
| 190 |
{0, 0}, |
| 191 |
{ {0, 0, 0} }, |
| 192 |
CAPNG_NEW, CAPNG_UNSET_ROOTID, |
| 193 |
{0, 0}, |
| 194 |
{0, 0}, |
| 195 |
0, |
| 196 |
NULL, 0 }; |
| 197 |
|
| 198 |
static void clear_staged_additional_groups(struct cap_ng *c) |
| 199 |
{ |
| 200 |
free(c->add_groups); |
| 201 |
c->add_groups = NULL; |
| 202 |
c->add_group_cnt = 0; |
| 203 |
} |
| 204 |
|
| 205 |
static inline void mark_bounding_set_changed(struct cap_ng *c) |
| 206 |
{ |
| 207 |
c->bounds_state_changed = 1; |
| 208 |
} |
| 209 |
|
| 210 |
static inline void clear_bounding_set_changed(struct cap_ng *c) |
| 211 |
{ |
| 212 |
c->bounds_state_changed = 0; |
| 213 |
} |
| 214 |
|
| 215 |
static int copy_staged_additional_groups(struct cap_ng *dst, |
| 216 |
const struct cap_ng *src) |
| 217 |
{ |
| 218 |
size_t len; |
| 219 |
|
| 220 |
dst->add_groups = NULL; |
| 221 |
dst->add_group_cnt = 0; |
| 222 |
if (src->add_group_cnt == 0) |
| 223 |
return 0; |
| 224 |
|
| 225 |
len = src->add_group_cnt * sizeof(gid_t); |
| 226 |
dst->add_groups = malloc(len); |
| 227 |
if (dst->add_groups == NULL) |
| 228 |
return -1; |
| 229 |
|
| 230 |
memcpy(dst->add_groups, src->add_groups, len); |
| 231 |
dst->add_group_cnt = src->add_group_cnt; |
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
static int gid_in_list(const gid_t *gids, size_t count, gid_t gid) |
| 236 |
{ |
| 237 |
size_t i; |
| 238 |
|
| 239 |
for (i = 0; i < count; i++) { |
| 240 |
if (gids[i] == gid) |
| 241 |
return 1; |
| 242 |
} |
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
static int get_additional_groups(const struct passwd *pw, gid_t gid, |
| 247 |
gid_t **gids, size_t *count) |
| 248 |
{ |
| 249 |
gid_t *list; |
| 250 |
int ngroups = 1; |
| 251 |
int rc; |
| 252 |
|
| 253 |
*gids = NULL; |
| 254 |
*count = 0; |
| 255 |
|
| 256 |
list = malloc(sizeof(gid_t)); |
| 257 |
if (list == NULL) |
| 258 |
return -1; |
| 259 |
|
| 260 |
rc = getgrouplist(pw->pw_name, gid, list, &ngroups); |
| 261 |
if (rc == -1) { |
| 262 |
gid_t *tmp; |
| 263 |
|
| 264 |
tmp = realloc(list, sizeof(gid_t) * ngroups); |
| 265 |
if (tmp == NULL) { |
| 266 |
free(list); |
| 267 |
return -1; |
| 268 |
} |
| 269 |
list = tmp; |
| 270 |
rc = getgrouplist(pw->pw_name, gid, list, &ngroups); |
| 271 |
} |
| 272 |
if (rc == -1) { |
| 273 |
free(list); |
| 274 |
return -1; |
| 275 |
} |
| 276 |
|
| 277 |
*gids = list; |
| 278 |
*count = ngroups; |
| 279 |
return 0; |
| 280 |
} |
| 281 |
|
| 282 |
static int merge_additional_groups(const gid_t *base, size_t base_cnt, |
| 283 |
const gid_t *extra, size_t extra_cnt, gid_t **merged, |
| 284 |
size_t *merged_cnt) |
| 285 |
{ |
| 286 |
gid_t *list; |
| 287 |
size_t i, count = 0, total = base_cnt + extra_cnt; |
| 288 |
|
| 289 |
*merged = NULL; |
| 290 |
*merged_cnt = 0; |
| 291 |
if (total == 0) |
| 292 |
return 0; |
| 293 |
|
| 294 |
list = malloc(sizeof(gid_t) * total); |
| 295 |
if (list == NULL) |
| 296 |
return -1; |
| 297 |
|
| 298 |
for (i = 0; i < base_cnt; i++) { |
| 299 |
if (gid_in_list(list, count, base[i]) == 0) |
| 300 |
list[count++] = base[i]; |
| 301 |
} |
| 302 |
for (i = 0; i < extra_cnt; i++) { |
| 303 |
if (gid_in_list(list, count, extra[i]) == 0) |
| 304 |
list[count++] = extra[i]; |
| 305 |
} |
| 306 |
|
| 307 |
*merged = list; |
| 308 |
*merged_cnt = count; |
| 309 |
return 0; |
| 310 |
} |
| 311 |
|
| 312 |
/* |
| 313 |
* Reset the state so that init gets called to erase everything |
| 314 |
*/ |
| 315 |
static void deinit(void) |
| 316 |
{ |
| 317 |
clear_staged_additional_groups(&m); |
| 318 |
m.state = CAPNG_NEW; |
| 319 |
} |
| 320 |
|
| 321 |
static inline int test_cap(unsigned int cap) |
| 322 |
{ |
| 323 |
// prctl returns 0 or 1 for valid caps, -1 otherwise |
| 324 |
return prctl(PR_CAPBSET_READ, cap) >= 0; |
| 325 |
} |
| 326 |
|
| 327 |
// The capability storage is fixed by VFS_CAP_U32 |
| 328 |
#define MAX_CAP_BITS (VFS_CAP_U32 * sizeof(__le32) * 8) |
| 329 |
#define MAX_CAP_VALUE (MAX_CAP_BITS - 1) |
| 330 |
|
| 331 |
static void init_lib(void) __attribute__ ((constructor)); |
| 332 |
static void init_lib(void) |
| 333 |
{ |
| 334 |
/* This is so that dynamic or static libraries don't re-init */ |
| 335 |
static unsigned int run_once; |
| 336 |
|
| 337 |
if (__atomic_load_n(&run_once, __ATOMIC_ACQUIRE) == 2) |
| 338 |
return; |
| 339 |
if (!__sync_bool_compare_and_swap(&run_once, 0, 1)) { |
| 340 |
while (__atomic_load_n(&run_once, __ATOMIC_ACQUIRE) != 2) |
| 341 |
; |
| 342 |
return; |
| 343 |
} |
| 344 |
|
| 345 |
#ifdef HAVE_PTHREAD_H |
| 346 |
pthread_atfork(NULL, NULL, deinit); |
| 347 |
#endif |
| 348 |
// Detect last cap |
| 349 |
if (last_cap == 0) { |
| 350 |
int fd; |
| 351 |
|
| 352 |
// Try to read last cap from procfs |
| 353 |
fd = open("/proc/sys/kernel/cap_last_cap", O_RDONLY); |
| 354 |
if (fd >= 0) { |
| 355 |
#ifdef HAVE_LINUX_MAGIC_H |
| 356 |
struct statfs st; |
| 357 |
// Bail out if procfs is invalid or fstatfs fails |
| 358 |
if (fstatfs(fd, &st) || st.f_type != PROC_SUPER_MAGIC) |
| 359 |
goto fail; |
| 360 |
#endif |
| 361 |
char buf[8]; |
| 362 |
int num = read(fd, buf, sizeof(buf) - 1); |
| 363 |
if (num > 0) { |
| 364 |
buf[num] = 0; |
| 365 |
errno = 0; |
| 366 |
unsigned int val = strtoul(buf, NULL, 10); |
| 367 |
if (errno == 0) |
| 368 |
last_cap = val; |
| 369 |
} |
| 370 |
fail: |
| 371 |
close(fd); |
| 372 |
} |
| 373 |
if (last_cap >= MAX_CAP_BITS) |
| 374 |
last_cap = MAX_CAP_VALUE; |
| 375 |
// Run a binary search over capabilities |
| 376 |
if (last_cap == 0) { |
| 377 |
// starting with last_cap=MAX_CAP_BITS means we always know |
| 378 |
// that cap1 is invalid after the first iteration |
| 379 |
last_cap = MAX_CAP_BITS; |
| 380 |
unsigned int cap0 = 0, cap1 = MAX_CAP_BITS; |
| 381 |
|
| 382 |
while (cap0 < last_cap) { |
| 383 |
if (test_cap(last_cap)) |
| 384 |
cap0 = last_cap; |
| 385 |
else |
| 386 |
cap1 = last_cap; |
| 387 |
|
| 388 |
last_cap = (cap0 + cap1) / 2U; |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
// Detect prctl options at runtime |
| 393 |
#ifdef PR_CAPBSET_DROP |
| 394 |
errno = 0; |
| 395 |
prctl(PR_CAPBSET_READ, 0, 0, 0, 0); |
| 396 |
if (errno != EINVAL) |
| 397 |
HAVE_PR_CAPBSET_DROP = 1; |
| 398 |
#endif |
| 399 |
#ifdef PR_CAP_AMBIENT |
| 400 |
errno = 0; |
| 401 |
prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_CHOWN, 0, 0); |
| 402 |
if (errno != EINVAL) |
| 403 |
HAVE_PR_CAP_AMBIENT = 1; |
| 404 |
#endif |
| 405 |
__atomic_store_n(&run_once, 2, __ATOMIC_RELEASE); |
| 406 |
} |
| 407 |
|
| 408 |
static void init(void) |
| 409 |
{ |
| 410 |
// This is so static libs get initialized |
| 411 |
init_lib(); |
| 412 |
|
| 413 |
if (m.state != CAPNG_NEW) |
| 414 |
return; |
| 415 |
|
| 416 |
memset(&m.hdr, 0, sizeof(m.hdr)); |
| 417 |
(void)capget(&m.hdr, NULL); // Returns -EINVAL |
| 418 |
if (m.hdr.version == _LINUX_CAPABILITY_VERSION_3 || |
| 419 |
m.hdr.version == _LINUX_CAPABILITY_VERSION_2) { |
| 420 |
m.cap_ver = 3; |
| 421 |
} else if (m.hdr.version == _LINUX_CAPABILITY_VERSION_1) { |
| 422 |
m.cap_ver = 1; |
| 423 |
} else { |
| 424 |
m.state = CAPNG_ERROR; |
| 425 |
return; |
| 426 |
} |
| 427 |
|
| 428 |
#if VFS_CAP_REVISION == VFS_CAP_REVISION_1 |
| 429 |
m.vfs_cap_ver = 1; |
| 430 |
#else |
| 431 |
m.vfs_cap_ver = 2; // Intentionally set to 2 for both 2 & 3 |
| 432 |
#endif |
| 433 |
|
| 434 |
memset(&m.data, 0, sizeof(cap_data_t)); |
| 435 |
#ifdef HAVE_SYSCALL_H |
| 436 |
m.hdr.pid = (unsigned)syscall(__NR_gettid); |
| 437 |
#else |
| 438 |
m.hdr.pid = (unsigned)getpid(); |
| 439 |
#endif |
| 440 |
m.rootid = CAPNG_UNSET_ROOTID; |
| 441 |
m.state = CAPNG_ALLOCATED; |
| 442 |
} |
| 443 |
|
| 444 |
void capng_clear(capng_select_t set) |
| 445 |
{ |
| 446 |
if (m.state == CAPNG_NEW) |
| 447 |
init(); |
| 448 |
if (m.state == CAPNG_ERROR) |
| 449 |
return; |
| 450 |
|
| 451 |
if (set & CAPNG_SELECT_CAPS) |
| 452 |
memset(&m.data, 0, sizeof(cap_data_t)); |
| 453 |
#ifdef PR_CAPBSET_DROP |
| 454 |
if (HAVE_PR_CAPBSET_DROP) { |
| 455 |
if (set & CAPNG_SELECT_BOUNDS) { |
| 456 |
memset(m.bounds, 0, sizeof(m.bounds)); |
| 457 |
mark_bounding_set_changed(&m); |
| 458 |
} |
| 459 |
} |
| 460 |
#endif |
| 461 |
#ifdef PR_CAP_AMBIENT |
| 462 |
if (HAVE_PR_CAP_AMBIENT) { |
| 463 |
if (set & CAPNG_SELECT_AMBIENT) |
| 464 |
memset(m.ambient, 0, sizeof(m.ambient)); |
| 465 |
} |
| 466 |
#endif |
| 467 |
m.state = CAPNG_INIT; |
| 468 |
} |
| 469 |
|
| 470 |
void capng_fill(capng_select_t set) |
| 471 |
{ |
| 472 |
if (m.state == CAPNG_NEW) |
| 473 |
init(); |
| 474 |
if (m.state == CAPNG_ERROR) |
| 475 |
return; |
| 476 |
|
| 477 |
if (set & CAPNG_SELECT_CAPS) { |
| 478 |
if (m.cap_ver == 1) { |
| 479 |
m.data.v1.effective = 0x7FFFFFFFU; |
| 480 |
m.data.v1.permitted = 0x7FFFFFFFU; |
| 481 |
m.data.v1.inheritable = 0; |
| 482 |
} else { |
| 483 |
m.data.v3[0].effective = 0xFFFFFFFFU; |
| 484 |
m.data.v3[0].permitted = 0xFFFFFFFFU; |
| 485 |
m.data.v3[0].inheritable = 0; |
| 486 |
m.data.v3[1].effective = 0xFFFFFFFFU; |
| 487 |
m.data.v3[1].permitted = 0xFFFFFFFFU; |
| 488 |
m.data.v3[1].inheritable = 0; |
| 489 |
} |
| 490 |
} |
| 491 |
#ifdef PR_CAPBSET_DROP |
| 492 |
if (HAVE_PR_CAPBSET_DROP) { |
| 493 |
if (set & CAPNG_SELECT_BOUNDS) { |
| 494 |
unsigned i; |
| 495 |
for (i=0; i<sizeof(m.bounds)/sizeof(__u32); i++) |
| 496 |
m.bounds[i] = 0xFFFFFFFFU; |
| 497 |
mark_bounding_set_changed(&m); |
| 498 |
} |
| 499 |
} |
| 500 |
#endif |
| 501 |
#ifdef PR_CAP_AMBIENT |
| 502 |
if (HAVE_PR_CAP_AMBIENT) { |
| 503 |
if (set & CAPNG_SELECT_AMBIENT) { |
| 504 |
unsigned i; |
| 505 |
for (i=0; i<sizeof(m.ambient)/sizeof(__u32); i++) |
| 506 |
m.ambient[i] = 0xFFFFFFFFU; |
| 507 |
} |
| 508 |
} |
| 509 |
#endif |
| 510 |
m.state = CAPNG_INIT; |
| 511 |
} |
| 512 |
|
| 513 |
void capng_setpid(int pid) |
| 514 |
{ |
| 515 |
if (m.state == CAPNG_NEW) |
| 516 |
init(); |
| 517 |
if (m.state == CAPNG_ERROR) |
| 518 |
return; |
| 519 |
|
| 520 |
m.hdr.pid = pid; |
| 521 |
} |
| 522 |
|
| 523 |
int capng_get_rootid(void) |
| 524 |
{ |
| 525 |
#ifdef VFS_CAP_REVISION_3 |
| 526 |
return m.rootid; |
| 527 |
#else |
| 528 |
return CAPNG_UNSET_ROOTID; |
| 529 |
#endif |
| 530 |
} |
| 531 |
|
| 532 |
int capng_set_rootid(int rootid) |
| 533 |
{ |
| 534 |
#ifdef VFS_CAP_REVISION_3 |
| 535 |
if (m.state == CAPNG_NEW) |
| 536 |
init(); |
| 537 |
if (m.state == CAPNG_ERROR) |
| 538 |
return -1; |
| 539 |
|
| 540 |
if (rootid < 0 && rootid != CAPNG_UNSET_ROOTID) |
| 541 |
return -1; |
| 542 |
|
| 543 |
m.rootid = rootid; |
| 544 |
if (rootid == CAPNG_UNSET_ROOTID) |
| 545 |
m.vfs_cap_ver = 2; |
| 546 |
else |
| 547 |
m.vfs_cap_ver = 3; |
| 548 |
|
| 549 |
return 0; |
| 550 |
#else |
| 551 |
return -1; |
| 552 |
#endif |
| 553 |
} |
| 554 |
|
| 555 |
int capng_stage_additional_groups(const gid_t *gids, size_t count) |
| 556 |
{ |
| 557 |
gid_t *list = NULL; |
| 558 |
|
| 559 |
if (count && gids == NULL) { |
| 560 |
errno = EINVAL; |
| 561 |
return -1; |
| 562 |
} |
| 563 |
if (count > NGROUPS_MAX) { |
| 564 |
errno = EINVAL; |
| 565 |
return -1; |
| 566 |
} |
| 567 |
|
| 568 |
if (count) { |
| 569 |
size_t len = count * sizeof(gid_t); |
| 570 |
|
| 571 |
list = malloc(len); |
| 572 |
if (list == NULL) |
| 573 |
return -1; |
| 574 |
memcpy(list, gids, len); |
| 575 |
} |
| 576 |
|
| 577 |
clear_staged_additional_groups(&m); |
| 578 |
m.add_groups = list; |
| 579 |
m.add_group_cnt = count; |
| 580 |
return 0; |
| 581 |
} |
| 582 |
|
| 583 |
#ifdef PR_CAPBSET_DROP |
| 584 |
static int get_bounding_set(void) |
| 585 |
{ |
| 586 |
char buf[64]; |
| 587 |
FILE *f; |
| 588 |
int rc; |
| 589 |
|
| 590 |
snprintf(buf, sizeof(buf), "/proc/%d/status", m.hdr.pid ? m.hdr.pid : |
| 591 |
#ifdef HAVE_SYSCALL_H |
| 592 |
(int)syscall(__NR_gettid)); |
| 593 |
#else |
| 594 |
(int)getpid()); |
| 595 |
#endif |
| 596 |
f = fopen(buf, "re"); |
| 597 |
if (f) { |
| 598 |
__fsetlocking(f, FSETLOCKING_BYCALLER); |
| 599 |
while (fgets(buf, sizeof(buf), f)) { |
| 600 |
if (strncmp(buf, "CapB", 4)) |
| 601 |
continue; |
| 602 |
int num = sscanf(buf, "CapBnd: %08x%08x", |
| 603 |
&m.bounds[1], &m.bounds[0]); |
| 604 |
fclose(f); |
| 605 |
if (num != 2) |
| 606 |
return -1; |
| 607 |
clear_bounding_set_changed(&m); |
| 608 |
return 0; |
| 609 |
} |
| 610 |
// Didn't find bounding set, fall through and try prctl way |
| 611 |
fclose(f); |
| 612 |
} |
| 613 |
// Might be in a container with no procfs - do it the hard way |
| 614 |
memset(m.bounds, 0, sizeof(m.bounds)); |
| 615 |
unsigned int i = 0; |
| 616 |
do { |
| 617 |
rc = prctl(PR_CAPBSET_READ, i, 0, 0, 0); |
| 618 |
if (rc < 0) |
| 619 |
return -1; |
| 620 |
|
| 621 |
// Just add set bits |
| 622 |
if (rc) |
| 623 |
update_bounding_set(CAPNG_ADD, i%32, i>>5); |
| 624 |
i++; |
| 625 |
} while (cap_valid(i)); |
| 626 |
|
| 627 |
clear_bounding_set_changed(&m); |
| 628 |
return 0; |
| 629 |
} |
| 630 |
#endif |
| 631 |
|
| 632 |
#ifdef PR_CAP_AMBIENT |
| 633 |
static int get_ambient_set(void) |
| 634 |
{ |
| 635 |
char buf[64]; |
| 636 |
FILE *f; |
| 637 |
int rc; |
| 638 |
|
| 639 |
snprintf(buf, sizeof(buf), "/proc/%d/status", m.hdr.pid ? m.hdr.pid : |
| 640 |
#ifdef HAVE_SYSCALL_H |
| 641 |
(int)syscall(__NR_gettid)); |
| 642 |
#else |
| 643 |
(int)getpid()); |
| 644 |
#endif |
| 645 |
f = fopen(buf, "re"); |
| 646 |
if (f) { |
| 647 |
__fsetlocking(f, FSETLOCKING_BYCALLER); |
| 648 |
while (fgets(buf, sizeof(buf), f)) { |
| 649 |
if (strncmp(buf, "CapA", 4)) |
| 650 |
continue; |
| 651 |
int num = sscanf(buf, "CapAmb: %08x%08x", |
| 652 |
&m.ambient[1], &m.ambient[0]); |
| 653 |
fclose(f); |
| 654 |
if (num != 2) |
| 655 |
return -1; |
| 656 |
return 0; |
| 657 |
} |
| 658 |
fclose(f); |
| 659 |
// Didn't find ambient set, fall through and try prctl way |
| 660 |
} |
| 661 |
// Might be in a container with no procfs - do it the hard way |
| 662 |
memset(m.ambient, 0, sizeof(m.ambient)); |
| 663 |
unsigned int i = 0; |
| 664 |
do { |
| 665 |
rc = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, i, 0, 0); |
| 666 |
if (rc < 0) |
| 667 |
return -1; |
| 668 |
|
| 669 |
// Just add set bits |
| 670 |
if (rc) |
| 671 |
update_ambient_set(CAPNG_ADD, i%32, i>>5); |
| 672 |
i++; |
| 673 |
} while (cap_valid(i)); |
| 674 |
|
| 675 |
return 0; |
| 676 |
} |
| 677 |
#endif |
| 678 |
|
| 679 |
/* |
| 680 |
* Returns 0 on success and -1 on failure |
| 681 |
*/ |
| 682 |
int capng_get_caps_process(void) |
| 683 |
{ |
| 684 |
int rc; |
| 685 |
|
| 686 |
if (m.state == CAPNG_NEW) |
| 687 |
init(); |
| 688 |
if (m.state == CAPNG_ERROR) |
| 689 |
return -1; |
| 690 |
|
| 691 |
rc = capget((cap_user_header_t)&m.hdr, (cap_user_data_t)&m.data); |
| 692 |
if (rc == 0) { |
| 693 |
m.state = CAPNG_INIT; |
| 694 |
#ifdef PR_CAPBSET_DROP |
| 695 |
if (HAVE_PR_CAPBSET_DROP) { |
| 696 |
rc = get_bounding_set(); |
| 697 |
if (rc < 0) |
| 698 |
m.state = CAPNG_ERROR; |
| 699 |
} |
| 700 |
#endif |
| 701 |
#ifdef PR_CAP_AMBIENT |
| 702 |
if (HAVE_PR_CAP_AMBIENT) { |
| 703 |
rc = get_ambient_set(); |
| 704 |
if (rc < 0) |
| 705 |
m.state = CAPNG_ERROR; |
| 706 |
} |
| 707 |
#endif |
| 708 |
} |
| 709 |
|
| 710 |
return rc; |
| 711 |
} |
| 712 |
|
| 713 |
#ifdef VFS_CAP_U32 |
| 714 |
#ifdef VFS_CAP_REVISION_3 |
| 715 |
static int load_data(const struct vfs_ns_cap_data *filedata, int size) |
| 716 |
#else |
| 717 |
static int load_data(const struct vfs_cap_data *filedata, int size) |
| 718 |
#endif |
| 719 |
{ |
| 720 |
unsigned int magic; |
| 721 |
|
| 722 |
if (m.cap_ver == 1) |
| 723 |
return -1; // Should never get here but just in case |
| 724 |
|
| 725 |
magic = FIXUP(filedata->magic_etc); |
| 726 |
switch (magic & VFS_CAP_REVISION_MASK) |
| 727 |
{ |
| 728 |
case VFS_CAP_REVISION_1: |
| 729 |
m.vfs_cap_ver = 1; |
| 730 |
if (size != XATTR_CAPS_SZ_1) |
| 731 |
return -1; |
| 732 |
break; |
| 733 |
case VFS_CAP_REVISION_2: |
| 734 |
m.vfs_cap_ver = 2; |
| 735 |
if (size != XATTR_CAPS_SZ_2) |
| 736 |
return -1; |
| 737 |
break; |
| 738 |
#ifdef VFS_CAP_REVISION_3 |
| 739 |
case VFS_CAP_REVISION_3: |
| 740 |
m.vfs_cap_ver = 3; |
| 741 |
if (size != XATTR_CAPS_SZ_3) |
| 742 |
return -1; |
| 743 |
break; |
| 744 |
#endif |
| 745 |
default: |
| 746 |
return -1; |
| 747 |
} |
| 748 |
|
| 749 |
// Now stuff the data structures |
| 750 |
m.data.v3[0].permitted = FIXUP(filedata->data[0].permitted); |
| 751 |
m.data.v3[1].permitted = FIXUP(filedata->data[1].permitted); |
| 752 |
m.data.v3[0].inheritable = FIXUP(filedata->data[0].inheritable); |
| 753 |
m.data.v3[1].inheritable = FIXUP(filedata->data[1].inheritable); |
| 754 |
if (magic & VFS_CAP_FLAGS_EFFECTIVE) { |
| 755 |
m.data.v3[0].effective = |
| 756 |
m.data.v3[0].permitted | m.data.v3[0].inheritable; |
| 757 |
m.data.v3[1].effective = |
| 758 |
m.data.v3[1].permitted | m.data.v3[1].inheritable; |
| 759 |
} else { |
| 760 |
m.data.v3[0].effective = 0; |
| 761 |
m.data.v3[1].effective = 0; |
| 762 |
} |
| 763 |
#ifdef VFS_CAP_REVISION_3 |
| 764 |
if (size == XATTR_CAPS_SZ_3) { |
| 765 |
struct vfs_ns_cap_data *d = (struct vfs_ns_cap_data *)filedata; |
| 766 |
m.rootid = FIXUP(d->rootid); |
| 767 |
} |
| 768 |
#endif |
| 769 |
return 0; |
| 770 |
} |
| 771 |
#endif |
| 772 |
|
| 773 |
int capng_get_caps_fd(int fd) |
| 774 |
{ |
| 775 |
#ifndef VFS_CAP_U32 |
| 776 |
return -1; |
| 777 |
#else |
| 778 |
int rc; |
| 779 |
#ifdef VFS_CAP_REVISION_3 |
| 780 |
struct vfs_ns_cap_data filedata; |
| 781 |
#else |
| 782 |
struct vfs_cap_data filedata; |
| 783 |
#endif |
| 784 |
if (m.state == CAPNG_NEW) |
| 785 |
init(); |
| 786 |
if (m.state == CAPNG_ERROR) |
| 787 |
return -1; |
| 788 |
|
| 789 |
rc = fgetxattr(fd, XATTR_NAME_CAPS, &filedata, sizeof(filedata)); |
| 790 |
if (rc <= 0) |
| 791 |
return -1; |
| 792 |
|
| 793 |
rc = load_data(&filedata, rc); |
| 794 |
if (rc == 0) |
| 795 |
m.state = CAPNG_INIT; |
| 796 |
else |
| 797 |
m.state = CAPNG_ERROR; // If load data failed, malformed data |
| 798 |
|
| 799 |
return rc; |
| 800 |
#endif |
| 801 |
} |
| 802 |
|
| 803 |
static void v1_update(capng_act_t action, unsigned int capability, __u32 *data) |
| 804 |
{ |
| 805 |
if (action == CAPNG_ADD) |
| 806 |
*data |= MASK(capability); |
| 807 |
else |
| 808 |
*data &= ~(MASK(capability)); |
| 809 |
} |
| 810 |
|
| 811 |
static void update_effective(capng_act_t action, unsigned int capability, |
| 812 |
unsigned int idx) |
| 813 |
{ |
| 814 |
if (action == CAPNG_ADD) |
| 815 |
m.data.v3[idx].effective |= MASK(capability); |
| 816 |
else |
| 817 |
m.data.v3[idx].effective &= ~(MASK(capability)); |
| 818 |
} |
| 819 |
|
| 820 |
static void update_permitted(capng_act_t action, unsigned int capability, |
| 821 |
unsigned int idx) |
| 822 |
{ |
| 823 |
if (action == CAPNG_ADD) |
| 824 |
m.data.v3[idx].permitted |= MASK(capability); |
| 825 |
else |
| 826 |
m.data.v3[idx].permitted &= ~(MASK(capability)); |
| 827 |
} |
| 828 |
|
| 829 |
static void update_inheritable(capng_act_t action, unsigned int capability, |
| 830 |
unsigned int idx) |
| 831 |
{ |
| 832 |
if (action == CAPNG_ADD) |
| 833 |
m.data.v3[idx].inheritable |= MASK(capability); |
| 834 |
else |
| 835 |
m.data.v3[idx].inheritable &= ~(MASK(capability)); |
| 836 |
} |
| 837 |
|
| 838 |
static void update_bounding_set(capng_act_t action, unsigned int capability, |
| 839 |
unsigned int idx) |
| 840 |
{ |
| 841 |
#ifdef PR_CAPBSET_DROP |
| 842 |
if (HAVE_PR_CAPBSET_DROP) { |
| 843 |
if (action == CAPNG_ADD) |
| 844 |
m.bounds[idx] |= MASK(capability); |
| 845 |
else |
| 846 |
m.bounds[idx] &= ~(MASK(capability)); |
| 847 |
} |
| 848 |
#endif |
| 849 |
} |
| 850 |
|
| 851 |
static void update_ambient_set(capng_act_t action, unsigned int capability, |
| 852 |
unsigned int idx) |
| 853 |
{ |
| 854 |
#ifdef PR_CAP_AMBIENT |
| 855 |
if (HAVE_PR_CAP_AMBIENT) { |
| 856 |
if (action == CAPNG_ADD) |
| 857 |
m.ambient[idx] |= MASK(capability); |
| 858 |
else |
| 859 |
m.ambient[idx] &= ~(MASK(capability)); |
| 860 |
} |
| 861 |
#endif |
| 862 |
} |
| 863 |
|
| 864 |
int capng_update(capng_act_t action, capng_type_t type, unsigned int capability) |
| 865 |
{ |
| 866 |
// Before updating, we expect that the data is initialized to something |
| 867 |
if (m.state < CAPNG_INIT) |
| 868 |
return -1; |
| 869 |
if (!cap_valid(capability)) { |
| 870 |
errno = EINVAL; |
| 871 |
return -1; |
| 872 |
} |
| 873 |
|
| 874 |
if (m.cap_ver == 1) { |
| 875 |
if (CAPNG_EFFECTIVE & type) |
| 876 |
v1_update(action, capability, &m.data.v1.effective); |
| 877 |
if (CAPNG_PERMITTED & type) |
| 878 |
v1_update(action, capability, &m.data.v1.permitted); |
| 879 |
if (CAPNG_INHERITABLE & type) |
| 880 |
v1_update(action, capability, &m.data.v1.inheritable); |
| 881 |
} else { |
| 882 |
unsigned int idx; |
| 883 |
|
| 884 |
if (capability > 31) { |
| 885 |
idx = capability>>5; |
| 886 |
capability %= 32; |
| 887 |
} else |
| 888 |
idx = 0; |
| 889 |
|
| 890 |
if (CAPNG_EFFECTIVE & type) |
| 891 |
update_effective(action, capability, idx); |
| 892 |
if (CAPNG_PERMITTED & type) |
| 893 |
update_permitted(action, capability, idx); |
| 894 |
if (CAPNG_INHERITABLE & type) |
| 895 |
update_inheritable(action, capability, idx); |
| 896 |
if (CAPNG_BOUNDING_SET & type) |
| 897 |
update_bounding_set(action, capability, idx); |
| 898 |
if (CAPNG_AMBIENT & type) |
| 899 |
update_ambient_set(action, capability, idx); |
| 900 |
} |
| 901 |
if (CAPNG_BOUNDING_SET & type) |
| 902 |
mark_bounding_set_changed(&m); |
| 903 |
|
| 904 |
m.state = CAPNG_UPDATED; |
| 905 |
return 0; |
| 906 |
} |
| 907 |
|
| 908 |
int capng_updatev(capng_act_t action, capng_type_t type, |
| 909 |
unsigned int capability, ...) |
| 910 |
{ |
| 911 |
int rc; |
| 912 |
unsigned int cap; |
| 913 |
va_list ap; |
| 914 |
|
| 915 |
rc = capng_update(action, type, capability); |
| 916 |
if (rc) |
| 917 |
return rc; |
| 918 |
va_start(ap, capability); |
| 919 |
cap = va_arg(ap, unsigned int); |
| 920 |
while (cap_valid(cap)) { |
| 921 |
rc = capng_update(action, type, cap); |
| 922 |
if (rc) |
| 923 |
break; |
| 924 |
cap = va_arg(ap, unsigned int); |
| 925 |
} |
| 926 |
va_end(ap); |
| 927 |
|
| 928 |
// See if planned exit or invalid |
| 929 |
if (cap == (unsigned)-1) |
| 930 |
rc = 0; |
| 931 |
else { |
| 932 |
rc = -1; |
| 933 |
errno = EINVAL; |
| 934 |
} |
| 935 |
|
| 936 |
return rc; |
| 937 |
} |
| 938 |
|
| 939 |
int capng_apply(capng_select_t set) |
| 940 |
{ |
| 941 |
int rc = 0; |
| 942 |
|
| 943 |
// Before updating, we expect that the data is initialized to something |
| 944 |
if (m.state < CAPNG_INIT) |
| 945 |
return -1; |
| 946 |
if (set == 0) { |
| 947 |
errno = EINVAL; |
| 948 |
return -1; |
| 949 |
} |
| 950 |
|
| 951 |
if (set & CAPNG_SELECT_BOUNDS) { |
| 952 |
#ifdef PR_CAPBSET_DROP |
| 953 |
if (HAVE_PR_CAPBSET_DROP) { |
| 954 |
struct cap_ng state; |
| 955 |
memcpy(&state, &m, sizeof(state)); /* save state */ |
| 956 |
if (capng_get_caps_process()) |
| 957 |
return -9; |
| 958 |
if (capng_have_capability(CAPNG_EFFECTIVE, CAP_SETPCAP)) { |
| 959 |
unsigned int i; |
| 960 |
memcpy(&m, &state, sizeof(m)); /* restore state */ |
| 961 |
for (i=0; i <= last_cap; i++) { |
| 962 |
if (capng_have_capability(CAPNG_BOUNDING_SET, |
| 963 |
i) == 0) { |
| 964 |
if (prctl(PR_CAPBSET_DROP, i, 0, 0, 0) <0) { |
| 965 |
rc = -2; |
| 966 |
goto try_caps; |
| 967 |
} |
| 968 |
} |
| 969 |
} |
| 970 |
if (get_bounding_set() < 0) { |
| 971 |
rc = -3; |
| 972 |
goto try_caps; |
| 973 |
} |
| 974 |
clear_bounding_set_changed(&m); |
| 975 |
m.state = CAPNG_APPLIED; |
| 976 |
} else { |
| 977 |
memcpy(&m, &state, sizeof(m)); /* restore state */ |
| 978 |
rc = -4; |
| 979 |
goto try_caps; |
| 980 |
} |
| 981 |
} |
| 982 |
#endif |
| 983 |
} |
| 984 |
|
| 985 |
// Try caps is here so that if someone had SELECT_BOTH and we blew up |
| 986 |
// doing the bounding set, we at least try to set any capabilities |
| 987 |
// before returning in case the caller also doesn't bother checking |
| 988 |
// the return code. |
| 989 |
try_caps: |
| 990 |
if (set & CAPNG_SELECT_CAPS) { |
| 991 |
if (capset((cap_user_header_t)&m.hdr, |
| 992 |
(cap_user_data_t)&m.data) == 0) |
| 993 |
m.state = CAPNG_APPLIED; |
| 994 |
else |
| 995 |
rc = -5; |
| 996 |
} |
| 997 |
|
| 998 |
// Most programs do not and should not mess with ambient capabilities. |
| 999 |
// Instead of returning here if rc is set, we'll let it try to |
| 1000 |
// do something with ambient capabilities in hopes that it's lowering |
| 1001 |
// capabilities. Again, this is for people that don't check their |
| 1002 |
// return codes. |
| 1003 |
// |
| 1004 |
// Do ambient last so that inheritable and permitted are set by the |
| 1005 |
// time we get here. |
| 1006 |
if (set & CAPNG_SELECT_AMBIENT) { |
| 1007 |
#ifdef PR_CAP_AMBIENT |
| 1008 |
if (HAVE_PR_CAP_AMBIENT) { |
| 1009 |
if (capng_have_capabilities(CAPNG_SELECT_AMBIENT) == |
| 1010 |
CAPNG_NONE) { |
| 1011 |
if (prctl(PR_CAP_AMBIENT, |
| 1012 |
PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) < 0) { |
| 1013 |
rc = -6; |
| 1014 |
goto out; |
| 1015 |
} |
| 1016 |
} else { |
| 1017 |
unsigned int i; |
| 1018 |
|
| 1019 |
// Clear them all |
| 1020 |
if (prctl(PR_CAP_AMBIENT, |
| 1021 |
PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) < 0) { |
| 1022 |
rc = -7; |
| 1023 |
goto out; |
| 1024 |
} |
| 1025 |
for (i=0; i <= last_cap; i++) { |
| 1026 |
if (capng_have_capability(CAPNG_AMBIENT, i)) |
| 1027 |
if (prctl(PR_CAP_AMBIENT, |
| 1028 |
PR_CAP_AMBIENT_RAISE, i, 0, 0) < 0){ |
| 1029 |
rc = -8; |
| 1030 |
goto out; |
| 1031 |
} |
| 1032 |
} |
| 1033 |
} |
| 1034 |
m.state = CAPNG_APPLIED; |
| 1035 |
} |
| 1036 |
#endif |
| 1037 |
} |
| 1038 |
out: |
| 1039 |
return rc; |
| 1040 |
} |
| 1041 |
|
| 1042 |
#ifdef VFS_CAP_U32 |
| 1043 |
#ifdef VFS_CAP_REVISION_3 |
| 1044 |
static int save_data(struct vfs_ns_cap_data *filedata, int *size) |
| 1045 |
#else |
| 1046 |
static int save_data(struct vfs_cap_data *filedata, int *size) |
| 1047 |
#endif |
| 1048 |
{ |
| 1049 |
// Now stuff the data structures |
| 1050 |
if (m.vfs_cap_ver == 1) { |
| 1051 |
filedata->data[0].permitted = FIXUP(m.data.v1.permitted); |
| 1052 |
filedata->data[0].inheritable = FIXUP(m.data.v1.inheritable); |
| 1053 |
filedata->magic_etc = FIXUP(VFS_CAP_REVISION_1); |
| 1054 |
*size = XATTR_CAPS_SZ_1; |
| 1055 |
} else if (m.vfs_cap_ver == 2 || m.vfs_cap_ver == 3) { |
| 1056 |
int eff; |
| 1057 |
|
| 1058 |
if (m.data.v3[0].effective || m.data.v3[1].effective) |
| 1059 |
eff = VFS_CAP_FLAGS_EFFECTIVE; |
| 1060 |
else |
| 1061 |
eff = 0; |
| 1062 |
filedata->data[0].permitted = FIXUP(m.data.v3[0].permitted); |
| 1063 |
filedata->data[0].inheritable = FIXUP(m.data.v3[0].inheritable); |
| 1064 |
filedata->data[1].permitted = FIXUP(m.data.v3[1].permitted); |
| 1065 |
filedata->data[1].inheritable = FIXUP(m.data.v3[1].inheritable); |
| 1066 |
filedata->magic_etc = FIXUP(VFS_CAP_REVISION_2 | eff); |
| 1067 |
*size = XATTR_CAPS_SZ_2; |
| 1068 |
} |
| 1069 |
#ifdef VFS_CAP_REVISION_3 |
| 1070 |
if (m.vfs_cap_ver == 3) { |
| 1071 |
// Kernel doesn't support namespaces with non-0 rootid |
| 1072 |
if (m.rootid != 0) |
| 1073 |
return -1; |
| 1074 |
filedata->rootid = FIXUP(m.rootid); |
| 1075 |
*size = XATTR_CAPS_SZ_3; |
| 1076 |
} |
| 1077 |
#endif |
| 1078 |
|
| 1079 |
return 0; |
| 1080 |
} |
| 1081 |
#endif |
| 1082 |
|
| 1083 |
int capng_apply_caps_fd(int fd) |
| 1084 |
{ |
| 1085 |
#ifndef VFS_CAP_U32 |
| 1086 |
return -1; |
| 1087 |
#else |
| 1088 |
int rc, size = 0; |
| 1089 |
#ifdef VFS_CAP_REVISION_3 |
| 1090 |
struct vfs_ns_cap_data filedata; |
| 1091 |
#else |
| 1092 |
struct vfs_cap_data filedata; |
| 1093 |
#endif |
| 1094 |
struct stat buf; |
| 1095 |
|
| 1096 |
// Before updating, we expect that the data is initialized to something |
| 1097 |
if (m.state < CAPNG_INIT) |
| 1098 |
return -1; |
| 1099 |
|
| 1100 |
if (fstat(fd, &buf) != 0) |
| 1101 |
return -1; |
| 1102 |
if (S_ISLNK(buf.st_mode) || !S_ISREG(buf.st_mode)) { |
| 1103 |
errno = EINVAL; |
| 1104 |
return -1; |
| 1105 |
} |
| 1106 |
if (capng_have_capabilities(CAPNG_SELECT_CAPS) == CAPNG_NONE) |
| 1107 |
rc = fremovexattr(fd, XATTR_NAME_CAPS); |
| 1108 |
else { |
| 1109 |
if (save_data(&filedata, &size)) { |
| 1110 |
m.state = CAPNG_ERROR; |
| 1111 |
errno = EINVAL; |
| 1112 |
return -2; |
| 1113 |
} |
| 1114 |
rc = fsetxattr(fd, XATTR_NAME_CAPS, &filedata, size, 0); |
| 1115 |
} |
| 1116 |
|
| 1117 |
if (rc == 0) |
| 1118 |
m.state = CAPNG_APPLIED; |
| 1119 |
|
| 1120 |
return rc; |
| 1121 |
#endif |
| 1122 |
} |
| 1123 |
|
| 1124 |
// Change uids keeping/removing only certain capabilities |
| 1125 |
int capng_change_id(int uid, int gid, capng_flags_t flag) |
| 1126 |
{ |
| 1127 |
enum { |
| 1128 |
TMP_SETPCAP_E = 1 << 0, |
| 1129 |
TMP_SETPCAP_P = 1 << 1, |
| 1130 |
TMP_SETUID_E = 1 << 2, |
| 1131 |
TMP_SETUID_P = 1 << 3, |
| 1132 |
TMP_SETGID_E = 1 << 4, |
| 1133 |
TMP_SETGID_P = 1 << 5, |
| 1134 |
}; |
| 1135 |
unsigned int tmp_caps = 0; |
| 1136 |
int rc, ret; |
| 1137 |
struct passwd *pw = NULL; |
| 1138 |
gid_t *gids = NULL, *merged = NULL; |
| 1139 |
size_t gid_cnt = 0, merged_cnt = 0; |
| 1140 |
|
| 1141 |
// Before updating, we expect that the data is initialized to something |
| 1142 |
if (m.state < CAPNG_INIT) { |
| 1143 |
ret = -1; |
| 1144 |
goto out; |
| 1145 |
} |
| 1146 |
// Validate mutually exclusive staged-group combinations up front. |
| 1147 |
if ((flag & CAPNG_APPLY_STAGED_GROUPS) && |
| 1148 |
(flag & CAPNG_DROP_SUPP_GRP)) { |
| 1149 |
ret = -12; |
| 1150 |
goto out; |
| 1151 |
} |
| 1152 |
// Validate mutually exclusive bounding-set operations up front. |
| 1153 |
if ((flag & CAPNG_APPLY_BOUNDING) && |
| 1154 |
(flag & CAPNG_CLEAR_BOUNDING)) { |
| 1155 |
ret = -17; |
| 1156 |
goto out; |
| 1157 |
} |
| 1158 |
// Staged application only makes sense when a list was staged earlier. |
| 1159 |
if ((flag & CAPNG_APPLY_STAGED_GROUPS) && m.add_group_cnt == 0) { |
| 1160 |
ret = -13; |
| 1161 |
goto out; |
| 1162 |
} |
| 1163 |
|
| 1164 |
// Make sure the temporary transition caps we may need are present. |
| 1165 |
#ifdef PR_CAPBSET_DROP |
| 1166 |
if (HAVE_PR_CAPBSET_DROP) { |
| 1167 |
// If newer kernel, we need setpcap to change the bounding set |
| 1168 |
if ((flag & CAPNG_CLEAR_BOUNDING) || |
| 1169 |
((flag & CAPNG_APPLY_BOUNDING) && |
| 1170 |
m.bounds_state_changed)) { |
| 1171 |
if (capng_have_capability(CAPNG_EFFECTIVE, |
| 1172 |
CAP_SETPCAP) == 0) { |
| 1173 |
tmp_caps |= TMP_SETPCAP_E; |
| 1174 |
capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, |
| 1175 |
CAP_SETPCAP); |
| 1176 |
} |
| 1177 |
if (capng_have_capability(CAPNG_PERMITTED, |
| 1178 |
CAP_SETPCAP) == 0) { |
| 1179 |
tmp_caps |= TMP_SETPCAP_P; |
| 1180 |
capng_update(CAPNG_ADD, CAPNG_PERMITTED, |
| 1181 |
CAP_SETPCAP); |
| 1182 |
} |
| 1183 |
} |
| 1184 |
} |
| 1185 |
#endif |
| 1186 |
if (gid != -1) { |
| 1187 |
if (capng_have_capability(CAPNG_EFFECTIVE, CAP_SETGID) == 0) { |
| 1188 |
tmp_caps |= TMP_SETGID_E; |
| 1189 |
capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_SETGID); |
| 1190 |
} |
| 1191 |
if (capng_have_capability(CAPNG_PERMITTED, CAP_SETGID) == 0) { |
| 1192 |
tmp_caps |= TMP_SETGID_P; |
| 1193 |
capng_update(CAPNG_ADD, CAPNG_PERMITTED, CAP_SETGID); |
| 1194 |
} |
| 1195 |
} |
| 1196 |
if (uid != -1) { |
| 1197 |
if (capng_have_capability(CAPNG_EFFECTIVE, CAP_SETUID) == 0) { |
| 1198 |
tmp_caps |= TMP_SETUID_E; |
| 1199 |
capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_SETUID); |
| 1200 |
} |
| 1201 |
if (capng_have_capability(CAPNG_PERMITTED, CAP_SETUID) == 0) { |
| 1202 |
tmp_caps |= TMP_SETUID_P; |
| 1203 |
capng_update(CAPNG_ADD, CAPNG_PERMITTED, CAP_SETUID); |
| 1204 |
} |
| 1205 |
} |
| 1206 |
|
| 1207 |
// Enable keepcaps before changing credentials so final caps survive. |
| 1208 |
if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) < 0) { |
| 1209 |
ret = -2; |
| 1210 |
goto out; |
| 1211 |
} |
| 1212 |
|
| 1213 |
// Apply the temporary working set before touching ids or groups. |
| 1214 |
rc = capng_apply(CAPNG_SELECT_CAPS); |
| 1215 |
if (rc < 0) { |
| 1216 |
ret = -3; |
| 1217 |
goto err_out; |
| 1218 |
} |
| 1219 |
|
| 1220 |
// If we are clearing ambient, only clear since its applied at the end |
| 1221 |
if (flag & CAPNG_CLEAR_AMBIENT) |
| 1222 |
capng_clear(CAPNG_SELECT_AMBIENT); |
| 1223 |
|
| 1224 |
// Clear bounding set if needed while we have CAP_SETPCAP |
| 1225 |
if (flag & CAPNG_CLEAR_BOUNDING) { |
| 1226 |
capng_clear(CAPNG_SELECT_BOUNDS); |
| 1227 |
rc = capng_apply(CAPNG_SELECT_BOUNDS); |
| 1228 |
if (rc) { |
| 1229 |
ret = -8; |
| 1230 |
goto err_out; |
| 1231 |
} |
| 1232 |
} |
| 1233 |
// Apply a caller-prepared bounding set only when it is explicitly dirty. |
| 1234 |
if ((flag & CAPNG_APPLY_BOUNDING) && m.bounds_state_changed) { |
| 1235 |
rc = capng_apply(CAPNG_SELECT_BOUNDS); |
| 1236 |
if (rc) { |
| 1237 |
ret = -8; |
| 1238 |
goto err_out; |
| 1239 |
} |
| 1240 |
} |
| 1241 |
|
| 1242 |
// Change gid before supplemental groups so kernel permission checks |
| 1243 |
// see the target primary group. |
| 1244 |
if (gid != -1) { |
| 1245 |
rc = setresgid(gid, gid, gid); |
| 1246 |
if (rc) { |
| 1247 |
ret = -4; |
| 1248 |
goto err_out; |
| 1249 |
} |
| 1250 |
} |
| 1251 |
|
| 1252 |
// Resolve the passwd entry once when natural group setup is requested. |
| 1253 |
if ((flag & CAPNG_INIT_SUPP_GRP) && uid != -1) { |
| 1254 |
pw = getpwuid(uid); |
| 1255 |
if (pw == NULL) { |
| 1256 |
ret = -10; |
| 1257 |
goto err_out; |
| 1258 |
} |
| 1259 |
} |
| 1260 |
|
| 1261 |
// There are four supplemental/additional group modes: |
| 1262 |
// init+staged merge, init only, drop only, or staged only. |
| 1263 |
if ((flag & CAPNG_INIT_SUPP_GRP) && |
| 1264 |
(flag & CAPNG_APPLY_STAGED_GROUPS)) { |
| 1265 |
if (uid != -1) { |
| 1266 |
gid_t base_gid = gid != -1 ? (gid_t)gid : pw->pw_gid; |
| 1267 |
|
| 1268 |
// Build the natural target account group list first. |
| 1269 |
rc = get_additional_groups(pw, base_gid, &gids, |
| 1270 |
&gid_cnt); |
| 1271 |
if (rc) { |
| 1272 |
ret = -15; |
| 1273 |
goto err_out; |
| 1274 |
} |
| 1275 |
} |
| 1276 |
// Then append staged gids without duplicates and apply the result. |
| 1277 |
rc = merge_additional_groups(gids, gid_cnt, m.add_groups, |
| 1278 |
m.add_group_cnt, &merged, |
| 1279 |
&merged_cnt); |
| 1280 |
if (rc) { |
| 1281 |
ret = -16; |
| 1282 |
goto err_out; |
| 1283 |
} |
| 1284 |
if (setgroups(merged_cnt, merged)) { |
| 1285 |
ret = -14; |
| 1286 |
goto err_out; |
| 1287 |
} |
| 1288 |
} else if ((flag & CAPNG_INIT_SUPP_GRP) && uid != -1) { |
| 1289 |
gid_t base_gid = gid != -1 ? (gid_t)gid : pw->pw_gid; |
| 1290 |
|
| 1291 |
// Preserve the long-standing initgroups-only behavior. |
| 1292 |
if (initgroups(pw->pw_name, base_gid)) { |
| 1293 |
ret = -5; |
| 1294 |
goto err_out; |
| 1295 |
} |
| 1296 |
} else if ((flag & CAPNG_DROP_SUPP_GRP) && gid != -1) { |
| 1297 |
// Drop all supplemental groups for the target identity. |
| 1298 |
if (setgroups(0, NULL)) { |
| 1299 |
ret = -11; |
| 1300 |
goto err_out; |
| 1301 |
} |
| 1302 |
} else if (flag & CAPNG_APPLY_STAGED_GROUPS) { |
| 1303 |
// Apply exactly the caller-staged gid list. |
| 1304 |
if (setgroups(m.add_group_cnt, m.add_groups)) { |
| 1305 |
ret = -14; |
| 1306 |
goto err_out; |
| 1307 |
} |
| 1308 |
} |
| 1309 |
|
| 1310 |
// Change uid |
| 1311 |
if (uid != -1) { |
| 1312 |
rc = setresuid(uid, uid, uid); |
| 1313 |
if (rc) { |
| 1314 |
ret = -6; |
| 1315 |
goto err_out; |
| 1316 |
} |
| 1317 |
} |
| 1318 |
|
| 1319 |
// Credential changes are complete, so disable keepcaps again. |
| 1320 |
rc = prctl(PR_SET_KEEPCAPS, 0, 0, 0, 0); |
| 1321 |
if (rc < 0) { |
| 1322 |
ret = -7; |
| 1323 |
goto out; |
| 1324 |
} |
| 1325 |
|
| 1326 |
// Now throw away CAP_SETPCAP so no more changes |
| 1327 |
if (tmp_caps & TMP_SETGID_E) |
| 1328 |
capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_SETGID); |
| 1329 |
if (tmp_caps & TMP_SETGID_P) |
| 1330 |
capng_update(CAPNG_DROP, CAPNG_PERMITTED, CAP_SETGID); |
| 1331 |
if (tmp_caps & TMP_SETUID_E) |
| 1332 |
capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_SETUID); |
| 1333 |
if (tmp_caps & TMP_SETUID_P) |
| 1334 |
capng_update(CAPNG_DROP, CAPNG_PERMITTED, CAP_SETUID); |
| 1335 |
|
| 1336 |
// Drop any temporary transition caps and install the final sets. |
| 1337 |
if (tmp_caps & TMP_SETPCAP_E) |
| 1338 |
capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_SETPCAP); |
| 1339 |
if (tmp_caps & TMP_SETPCAP_P) |
| 1340 |
capng_update(CAPNG_DROP, CAPNG_PERMITTED, CAP_SETPCAP); |
| 1341 |
rc = capng_apply(CAPNG_SELECT_CAPS|CAPNG_SELECT_AMBIENT); |
| 1342 |
if (rc < 0) { |
| 1343 |
ret = -9; |
| 1344 |
goto out; |
| 1345 |
} |
| 1346 |
|
| 1347 |
ret = 0; |
| 1348 |
goto out; |
| 1349 |
|
| 1350 |
err_out: |
| 1351 |
prctl(PR_SET_KEEPCAPS, 0, 0, 0, 0); |
| 1352 |
out: |
| 1353 |
// Staged gids are one-shot state, so always clear them before return. |
| 1354 |
free(gids); |
| 1355 |
free(merged); |
| 1356 |
clear_staged_additional_groups(&m); |
| 1357 |
return ret; |
| 1358 |
} |
| 1359 |
|
| 1360 |
int capng_lock(void) |
| 1361 |
{ |
| 1362 |
int rc = 0; |
| 1363 |
|
| 1364 |
// If either fail, return -1 since something is not right |
| 1365 |
#ifdef PR_SET_SECUREBITS |
| 1366 |
if (prctl(PR_SET_SECUREBITS, |
| 1367 |
1 << SECURE_NOROOT | |
| 1368 |
1 << SECURE_NOROOT_LOCKED | |
| 1369 |
1 << SECURE_NO_SETUID_FIXUP | |
| 1370 |
1 << SECURE_NO_SETUID_FIXUP_LOCKED, 0, 0, 0) < 0) |
| 1371 |
rc = -1; |
| 1372 |
#endif |
| 1373 |
#ifdef PR_SET_NO_NEW_PRIVS |
| 1374 |
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) |
| 1375 |
rc += -2; |
| 1376 |
#endif |
| 1377 |
return rc; |
| 1378 |
} |
| 1379 |
|
| 1380 |
// -1 - error, 0 - no caps, 1 partial caps, 2 full caps |
| 1381 |
capng_results_t capng_have_capabilities(capng_select_t set) |
| 1382 |
{ |
| 1383 |
int empty = 0, full = 0; |
| 1384 |
|
| 1385 |
// First, try to init with current set |
| 1386 |
if (m.state < CAPNG_INIT) { |
| 1387 |
if (capng_get_caps_process()) |
| 1388 |
return CAPNG_FAIL; |
| 1389 |
} |
| 1390 |
|
| 1391 |
// If we still don't have anything, error out |
| 1392 |
if (m.state < CAPNG_INIT) |
| 1393 |
return CAPNG_FAIL; |
| 1394 |
|
| 1395 |
if (set & CAPNG_SELECT_CAPS) { |
| 1396 |
if (m.cap_ver == 1) { |
| 1397 |
if (m.data.v1.effective == 0) |
| 1398 |
empty = 1; |
| 1399 |
// after fill, 30 bits starts from upper to lower |
| 1400 |
else if (m.data.v1.effective == 0x7FFFFFFFU) |
| 1401 |
full = 1; |
| 1402 |
// actual capabilities read from system |
| 1403 |
else if (m.data.v1.effective == 0xFFFFFEFFU) |
| 1404 |
full = 1; |
| 1405 |
else |
| 1406 |
return CAPNG_PARTIAL; |
| 1407 |
} else { |
| 1408 |
if (m.data.v3[0].effective == 0) |
| 1409 |
empty = 1; |
| 1410 |
else if (m.data.v3[0].effective == 0xFFFFFFFFU) |
| 1411 |
full = 1; |
| 1412 |
else |
| 1413 |
return CAPNG_PARTIAL; |
| 1414 |
if ((m.data.v3[1].effective & UPPER_MASK) == 0 && !full) |
| 1415 |
empty = 1; |
| 1416 |
else if ((m.data.v3[1].effective & UPPER_MASK) == |
| 1417 |
UPPER_MASK && !empty) |
| 1418 |
full = 1; |
| 1419 |
else |
| 1420 |
return CAPNG_PARTIAL; |
| 1421 |
} |
| 1422 |
} |
| 1423 |
#ifdef PR_CAPBSET_DROP |
| 1424 |
if (HAVE_PR_CAPBSET_DROP) { |
| 1425 |
if (set & CAPNG_SELECT_BOUNDS) { |
| 1426 |
if (m.bounds[0] == 0) |
| 1427 |
empty = 1; |
| 1428 |
else if (m.bounds[0] == 0xFFFFFFFFU) |
| 1429 |
full = 1; |
| 1430 |
else |
| 1431 |
return CAPNG_PARTIAL; |
| 1432 |
if ((m.bounds[1] & UPPER_MASK) == 0) |
| 1433 |
empty = 1; |
| 1434 |
else if ((m.bounds[1] & UPPER_MASK) == UPPER_MASK) |
| 1435 |
full = 1; |
| 1436 |
else |
| 1437 |
return CAPNG_PARTIAL; |
| 1438 |
} |
| 1439 |
} else if (set & CAPNG_SELECT_BOUNDS) |
| 1440 |
empty = 1; // Only report empty if they asked about it |
| 1441 |
#endif |
| 1442 |
#ifdef PR_CAP_AMBIENT |
| 1443 |
if (HAVE_PR_CAP_AMBIENT) { |
| 1444 |
if (set & CAPNG_SELECT_AMBIENT) { |
| 1445 |
if (m.ambient[0] == 0) |
| 1446 |
empty = 1; |
| 1447 |
else if (m.ambient[0] == 0xFFFFFFFFU) |
| 1448 |
full = 1; |
| 1449 |
else |
| 1450 |
return CAPNG_PARTIAL; |
| 1451 |
if ((m.ambient[1] & UPPER_MASK) == 0) |
| 1452 |
empty = 1; |
| 1453 |
else if ((m.ambient[1] & UPPER_MASK) == UPPER_MASK) |
| 1454 |
full = 1; |
| 1455 |
else |
| 1456 |
return CAPNG_PARTIAL; |
| 1457 |
} |
| 1458 |
} else if (set & CAPNG_SELECT_AMBIENT) |
| 1459 |
empty = 1; // Only report empty if they asked about it |
| 1460 |
#endif |
| 1461 |
if (empty == 1 && full == 0) |
| 1462 |
return CAPNG_NONE; |
| 1463 |
else if (empty == 0 && full == 1) |
| 1464 |
return CAPNG_FULL; |
| 1465 |
|
| 1466 |
return CAPNG_PARTIAL; |
| 1467 |
} |
| 1468 |
|
| 1469 |
// -1 - error, 0 - no caps, 1 partial caps, 2 full caps |
| 1470 |
capng_results_t capng_have_permitted_capabilities(void) |
| 1471 |
{ |
| 1472 |
int empty = 0; |
| 1473 |
int lower_full = 0; |
| 1474 |
|
| 1475 |
// First, try to init with current set |
| 1476 |
if (m.state < CAPNG_INIT) { |
| 1477 |
if (capng_get_caps_process()) |
| 1478 |
return CAPNG_FAIL; |
| 1479 |
} |
| 1480 |
|
| 1481 |
// If we still don't have anything, error out |
| 1482 |
if (m.state < CAPNG_INIT) |
| 1483 |
return CAPNG_FAIL; |
| 1484 |
|
| 1485 |
if (m.data.v3[0].permitted == 0) |
| 1486 |
empty = 1; |
| 1487 |
else if (m.data.v3[0].permitted == 0xFFFFFFFFU) |
| 1488 |
lower_full = 1; |
| 1489 |
else |
| 1490 |
return CAPNG_PARTIAL; |
| 1491 |
|
| 1492 |
// At this point, lower 32 bits are either full or empty |
| 1493 |
if ((m.data.v3[1].permitted & UPPER_MASK) == 0 && !lower_full) |
| 1494 |
empty = 1; |
| 1495 |
else if ((m.data.v3[1].permitted & UPPER_MASK) != UPPER_MASK || empty) |
| 1496 |
return CAPNG_PARTIAL; |
| 1497 |
|
| 1498 |
// Partial is already handled, it's either empty or full now |
| 1499 |
if (empty) |
| 1500 |
return CAPNG_NONE; |
| 1501 |
|
| 1502 |
return CAPNG_FULL; |
| 1503 |
} |
| 1504 |
|
| 1505 |
static int check_effective(unsigned int capability, unsigned int idx) |
| 1506 |
{ |
| 1507 |
return MASK(capability) & m.data.v3[idx].effective ? 1 : 0; |
| 1508 |
} |
| 1509 |
|
| 1510 |
static int check_permitted(unsigned int capability, unsigned int idx) |
| 1511 |
{ |
| 1512 |
return MASK(capability) & m.data.v3[idx].permitted ? 1 : 0; |
| 1513 |
} |
| 1514 |
|
| 1515 |
static int check_inheritable(unsigned int capability, unsigned int idx) |
| 1516 |
{ |
| 1517 |
return MASK(capability) & m.data.v3[idx].inheritable ? 1 : 0; |
| 1518 |
} |
| 1519 |
|
| 1520 |
static int bounds_bit_check(unsigned int capability, unsigned int idx) |
| 1521 |
{ |
| 1522 |
#ifdef PR_CAPBSET_DROP |
| 1523 |
if (HAVE_PR_CAPBSET_DROP) { |
| 1524 |
return MASK(capability) & m.bounds[idx] ? 1 : 0; |
| 1525 |
} |
| 1526 |
#endif |
| 1527 |
return 0; |
| 1528 |
} |
| 1529 |
|
| 1530 |
static int ambient_bit_check(unsigned int capability, unsigned int idx) |
| 1531 |
{ |
| 1532 |
#ifdef PR_CAP_AMBIENT |
| 1533 |
if (HAVE_PR_CAP_AMBIENT) { |
| 1534 |
return MASK(capability) & m.ambient[idx] ? 1 : 0; |
| 1535 |
} |
| 1536 |
#endif |
| 1537 |
return 0; |
| 1538 |
} |
| 1539 |
|
| 1540 |
static int v1_check(unsigned int capability, __u32 data) |
| 1541 |
{ |
| 1542 |
return MASK(capability) & data ? 1 : 0; |
| 1543 |
} |
| 1544 |
|
| 1545 |
int capng_have_capability(capng_type_t which, unsigned int capability) |
| 1546 |
{ |
| 1547 |
// First, try to init with current set |
| 1548 |
if (m.state < CAPNG_INIT) { |
| 1549 |
if (capng_get_caps_process()) |
| 1550 |
return 0; |
| 1551 |
} |
| 1552 |
|
| 1553 |
// If we still don't have anything, error out |
| 1554 |
if (m.state < CAPNG_INIT) |
| 1555 |
return 0; |
| 1556 |
if (m.cap_ver == 1 && capability > 31) |
| 1557 |
return 0; |
| 1558 |
if (!cap_valid(capability)) |
| 1559 |
return 0; |
| 1560 |
|
| 1561 |
if (m.cap_ver == 1) { |
| 1562 |
if (which == CAPNG_EFFECTIVE) |
| 1563 |
return v1_check(capability, m.data.v1.effective); |
| 1564 |
else if (which == CAPNG_PERMITTED) |
| 1565 |
return v1_check(capability, m.data.v1.permitted); |
| 1566 |
else if (which == CAPNG_INHERITABLE) |
| 1567 |
return v1_check(capability, m.data.v1.inheritable); |
| 1568 |
} else { |
| 1569 |
unsigned int idx; |
| 1570 |
|
| 1571 |
if (capability > 31) { |
| 1572 |
idx = capability>>5; |
| 1573 |
capability %= 32; |
| 1574 |
} else |
| 1575 |
idx = 0; |
| 1576 |
|
| 1577 |
if (which == CAPNG_EFFECTIVE) |
| 1578 |
return check_effective(capability, idx); |
| 1579 |
else if (which == CAPNG_PERMITTED) |
| 1580 |
return check_permitted(capability, idx); |
| 1581 |
else if (which == CAPNG_INHERITABLE) |
| 1582 |
return check_inheritable(capability, idx); |
| 1583 |
else if (which == CAPNG_BOUNDING_SET) |
| 1584 |
return bounds_bit_check(capability, idx); |
| 1585 |
else if (which == CAPNG_AMBIENT) |
| 1586 |
return ambient_bit_check(capability, idx); |
| 1587 |
} |
| 1588 |
return 0; |
| 1589 |
} |
| 1590 |
|
| 1591 |
char *capng_print_caps_numeric(capng_print_t where, capng_select_t set) |
| 1592 |
{ |
| 1593 |
char *ptr = NULL; |
| 1594 |
|
| 1595 |
if (m.state < CAPNG_INIT) |
| 1596 |
return ptr; |
| 1597 |
|
| 1598 |
if (where == CAPNG_PRINT_STDOUT) { |
| 1599 |
if (set & CAPNG_SELECT_CAPS) { |
| 1600 |
if (m.cap_ver == 1) { |
| 1601 |
printf( "Effective: %08X\n" |
| 1602 |
"Permitted: %08X\n" |
| 1603 |
"Inheritable: %08X\n", |
| 1604 |
m.data.v1.effective, |
| 1605 |
m.data.v1.permitted, |
| 1606 |
m.data.v1.inheritable); |
| 1607 |
} else { |
| 1608 |
printf( "Effective: %08X, %08X\n" |
| 1609 |
"Permitted: %08X, %08X\n" |
| 1610 |
"Inheritable: %08X, %08X\n", |
| 1611 |
m.data.v3[1].effective & UPPER_MASK, |
| 1612 |
m.data.v3[0].effective, |
| 1613 |
m.data.v3[1].permitted & UPPER_MASK, |
| 1614 |
m.data.v3[0].permitted, |
| 1615 |
m.data.v3[1].inheritable & UPPER_MASK, |
| 1616 |
m.data.v3[0].inheritable); |
| 1617 |
} |
| 1618 |
} |
| 1619 |
#ifdef PR_CAPBSET_DROP |
| 1620 |
if (HAVE_PR_CAPBSET_DROP) { |
| 1621 |
if (set & CAPNG_SELECT_BOUNDS) |
| 1622 |
printf("Bounding Set: %08X, %08X\n", |
| 1623 |
m.bounds[1] & UPPER_MASK, m.bounds[0]); |
| 1624 |
} |
| 1625 |
#endif |
| 1626 |
#ifdef PR_CAP_AMBIENT |
| 1627 |
if (HAVE_PR_CAP_AMBIENT) { |
| 1628 |
if (set & CAPNG_SELECT_AMBIENT) |
| 1629 |
printf("Ambient: %08X, %08X\n", |
| 1630 |
m.ambient[1] & UPPER_MASK, m.ambient[0]); |
| 1631 |
} |
| 1632 |
#endif |
| 1633 |
} else if (where == CAPNG_PRINT_BUFFER) { |
| 1634 |
if (set & CAPNG_SELECT_CAPS) { |
| 1635 |
// Make it big enough for bounding & ambient set, too |
| 1636 |
size_t buf_size = 180; |
| 1637 |
ptr = malloc(buf_size); |
| 1638 |
if (m.cap_ver == 1) { |
| 1639 |
// 22 * 3 + 1 |
| 1640 |
snprintf(ptr, buf_size, |
| 1641 |
"Effective: %08X\n" |
| 1642 |
"Permitted: %08X\n" |
| 1643 |
"Inheritable: %08X\n", |
| 1644 |
m.data.v1.effective, |
| 1645 |
m.data.v1.permitted, |
| 1646 |
m.data.v1.inheritable); |
| 1647 |
} else { |
| 1648 |
// 35 * 5 + 1 (bounding is 35) |
| 1649 |
snprintf(ptr, buf_size, |
| 1650 |
"Effective: %08X, %08X\n" |
| 1651 |
"Permitted: %08X, %08X\n" |
| 1652 |
"Inheritable: %08X, %08X\n", |
| 1653 |
m.data.v3[1].effective & UPPER_MASK, |
| 1654 |
m.data.v3[0].effective, |
| 1655 |
m.data.v3[1].permitted & UPPER_MASK, |
| 1656 |
m.data.v3[0].permitted, |
| 1657 |
m.data.v3[1].inheritable & UPPER_MASK, |
| 1658 |
m.data.v3[0].inheritable); |
| 1659 |
} |
| 1660 |
} |
| 1661 |
if (set & CAPNG_SELECT_BOUNDS) { |
| 1662 |
#ifdef PR_CAPBSET_DROP |
| 1663 |
if (HAVE_PR_CAPBSET_DROP) { |
| 1664 |
char *s; |
| 1665 |
// If ptr is NULL, we only room for bounding and ambient |
| 1666 |
if (ptr == NULL ) { |
| 1667 |
ptr = malloc(80); |
| 1668 |
if (ptr == NULL) |
| 1669 |
return ptr; |
| 1670 |
*ptr = 0; |
| 1671 |
s = ptr; |
| 1672 |
} else |
| 1673 |
s = ptr + strlen(ptr); |
| 1674 |
// prints 34 + 1 chars |
| 1675 |
snprintf(s, 40, "Bounding Set: %08X, %08X\n", |
| 1676 |
m.bounds[1] & UPPER_MASK, m.bounds[0]); |
| 1677 |
} |
| 1678 |
#endif |
| 1679 |
} |
| 1680 |
if (set & CAPNG_SELECT_AMBIENT) { |
| 1681 |
#ifdef PR_CAP_AMBIENT |
| 1682 |
if (HAVE_PR_CAP_AMBIENT) { |
| 1683 |
char *s; |
| 1684 |
// If ptr is NULL, we only have room for ambient |
| 1685 |
if (ptr == NULL ) { |
| 1686 |
ptr = malloc(40); |
| 1687 |
if (ptr == NULL) |
| 1688 |
return ptr; |
| 1689 |
*ptr = 0; |
| 1690 |
s = ptr; |
| 1691 |
} else |
| 1692 |
s = ptr + strlen(ptr); |
| 1693 |
// prints 33 + 1 chars |
| 1694 |
snprintf(s, 40, "Ambient Set: %08X, %08X\n", |
| 1695 |
m.ambient[1] & UPPER_MASK, |
| 1696 |
m.ambient[0]); |
| 1697 |
} |
| 1698 |
#endif |
| 1699 |
} |
| 1700 |
} |
| 1701 |
|
| 1702 |
return ptr; |
| 1703 |
} |
| 1704 |
|
| 1705 |
char *capng_print_caps_text(capng_print_t where, capng_type_t which) |
| 1706 |
{ |
| 1707 |
unsigned int i, cnt = 0; |
| 1708 |
size_t total = 1; |
| 1709 |
int found = 0; |
| 1710 |
char *ptr = NULL; |
| 1711 |
|
| 1712 |
if (m.state < CAPNG_INIT) |
| 1713 |
return ptr; |
| 1714 |
|
| 1715 |
if (where == CAPNG_PRINT_BUFFER) { |
| 1716 |
for (i=0; i<=last_cap; i++) { |
| 1717 |
if (capng_have_capability(which, i)) { |
| 1718 |
const char *n = capng_capability_to_name(i); |
| 1719 |
size_t len; |
| 1720 |
|
| 1721 |
if (n == NULL) |
| 1722 |
n = "unknown"; |
| 1723 |
len = strlen(n); |
| 1724 |
total += len; |
| 1725 |
if (found) |
| 1726 |
total += 2; |
| 1727 |
found = 1; |
| 1728 |
} |
| 1729 |
} |
| 1730 |
ptr = malloc(found ? total : sizeof("none")); |
| 1731 |
if (ptr == NULL) |
| 1732 |
return ptr; |
| 1733 |
*ptr = 0; |
| 1734 |
found = 0; |
| 1735 |
} |
| 1736 |
|
| 1737 |
for (i=0; i<=last_cap; i++) { |
| 1738 |
if (capng_have_capability(which, i)) { |
| 1739 |
const char *n = capng_capability_to_name(i); |
| 1740 |
size_t len; |
| 1741 |
|
| 1742 |
if (n == NULL) |
| 1743 |
n = "unknown"; |
| 1744 |
len = strlen(n); |
| 1745 |
if (where == CAPNG_PRINT_STDOUT) { |
| 1746 |
if (found == 0) |
| 1747 |
printf("%s", n); |
| 1748 |
else |
| 1749 |
printf(", %s", n); |
| 1750 |
} else if (where == CAPNG_PRINT_BUFFER) { |
| 1751 |
if (ptr == NULL) |
| 1752 |
return NULL; |
| 1753 |
if (found) { |
| 1754 |
ptr[cnt++] = ','; |
| 1755 |
ptr[cnt++] = ' '; |
| 1756 |
} |
| 1757 |
memcpy(ptr + cnt, n, len + 1); |
| 1758 |
cnt += len; |
| 1759 |
} |
| 1760 |
found = 1; |
| 1761 |
} |
| 1762 |
} |
| 1763 |
if (found == 0) { |
| 1764 |
if (where == CAPNG_PRINT_STDOUT) |
| 1765 |
printf("none"); |
| 1766 |
else if (where == CAPNG_PRINT_BUFFER && ptr) |
| 1767 |
strcpy(ptr, "none"); |
| 1768 |
else |
| 1769 |
return NULL; |
| 1770 |
} |
| 1771 |
return ptr; |
| 1772 |
} |
| 1773 |
|
| 1774 |
void *capng_save_state(void) |
| 1775 |
{ |
| 1776 |
void *ptr = malloc(sizeof(m)); |
| 1777 |
if (ptr) { |
| 1778 |
memcpy(ptr, &m, sizeof(m)); |
| 1779 |
if (copy_staged_additional_groups(ptr, &m)) { |
| 1780 |
free(ptr); |
| 1781 |
ptr = NULL; |
| 1782 |
} |
| 1783 |
} |
| 1784 |
return ptr; |
| 1785 |
} |
| 1786 |
|
| 1787 |
void capng_restore_state(void **state) |
| 1788 |
{ |
| 1789 |
if (state) { |
| 1790 |
struct cap_ng *ptr = *state; |
| 1791 |
if (ptr) { |
| 1792 |
clear_staged_additional_groups(&m); |
| 1793 |
memcpy(&m, ptr, sizeof(m)); |
| 1794 |
if (copy_staged_additional_groups(&m, ptr)) |
| 1795 |
m.state = CAPNG_ERROR; |
| 1796 |
clear_staged_additional_groups(ptr); |
| 1797 |
} |
| 1798 |
free(ptr); |
| 1799 |
*state = NULL; |
| 1800 |
} |
| 1801 |
} |