};
#if ENABLE_FEATURE_SETPRIV_CAPABILITIES
-struct caps {
- struct __user_cap_header_struct header;
- cap_user_data_t data;
- int u32s;
-};
-
-static void getcaps(struct caps *caps)
-{
- static const uint8_t versions[] = {
- _LINUX_CAPABILITY_U32S_3, /* = 2 (fits into byte) */
- _LINUX_CAPABILITY_U32S_2, /* = 2 */
- _LINUX_CAPABILITY_U32S_1, /* = 1 */
- };
- int i;
-
- caps->header.pid = 0;
- for (i = 0; i < ARRAY_SIZE(versions); i++) {
- caps->header.version = versions[i];
- if (capget(&caps->header, NULL) == 0)
- goto got_it;
- }
- bb_simple_perror_msg_and_die("capget");
- got_it:
-
- switch (caps->header.version) {
- case _LINUX_CAPABILITY_VERSION_1:
- caps->u32s = _LINUX_CAPABILITY_U32S_1;
- break;
- case _LINUX_CAPABILITY_VERSION_2:
- caps->u32s = _LINUX_CAPABILITY_U32S_2;
- break;
- case _LINUX_CAPABILITY_VERSION_3:
- caps->u32s = _LINUX_CAPABILITY_U32S_3;
- break;
- default:
- bb_error_msg_and_die("unsupported capability version");
- }
-
- caps->data = xmalloc(sizeof(caps->data[0]) * caps->u32s);
- if (capget(&caps->header, caps->data) < 0)
- bb_simple_perror_msg_and_die("capget");
-}
+DEFINE_STRUCT_CAPS;
static unsigned parse_cap(const char *cap)
{
cap = parse_cap(capstring);
if (CAP_TO_INDEX(cap) >= caps.u32s)
- bb_error_msg_and_die("invalid capability cap");
+ bb_error_msg_and_die("invalid capability '%s'", capstring);
if (capstring[0] == '+')
caps.data[CAP_TO_INDEX(cap)].inheritable |= CAP_TO_MASK(cap);
capstring = strtok(NULL, ",");
}
- if ((capset(&caps.header, caps.data)) < 0)
+ if (capset(&caps.header, caps.data) != 0)
bb_perror_msg_and_die("capset");
-
- if (ENABLE_FEATURE_CLEAN_UP)
- free(caps.data);
}
static void set_ambient_caps(char *string)
cap = strtok(string, ",");
while (cap) {
- unsigned index;
+ unsigned idx;
- index = parse_cap(cap);
+ idx = parse_cap(cap);
if (cap[0] == '+') {
- if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, index, 0, 0) < 0)
+ if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, idx, 0, 0) < 0)
bb_perror_msg("cap_ambient_raise");
} else {
- if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_LOWER, index, 0, 0) < 0)
+ if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_LOWER, idx, 0, 0) < 0)
bb_perror_msg("cap_ambient_lower");
}
cap = strtok(NULL, ",");
bb_putchar('\n');
# endif
- if (ENABLE_FEATURE_CLEAN_UP) {
- IF_FEATURE_SETPRIV_CAPABILITIES(free(caps.data);)
+ if (ENABLE_FEATURE_CLEAN_UP)
free(gids);
- }
+
return EXIT_SUCCESS;
}
#endif /* FEATURE_SETPRIV_DUMP */