busybox/libbb/capability.c

128 lines
2.9 KiB
C
Raw Normal View History

/*
* Copyright (C) 2017 by <assafgordon@gmail.com>
*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
//kbuild:lib-$(CONFIG_FEATURE_SETPRIV_CAPABILITIES) += capability.o
//kbuild:lib-$(CONFIG_RUN_INIT) += capability.o
#include <linux/capability.h>
// #include <sys/capability.h>
// This header is in libcap, but the functions are in libc.
// Comment in the header says this above capset/capget:
/* system calls - look to libc for function to system call mapping */
extern int capset(cap_user_header_t header, cap_user_data_t data);
extern int capget(cap_user_header_t header, const cap_user_data_t data);
// so for bbox, let's just repeat the declarations.
// This way, libcap needs not be installed in build environment.
#include "libbb.h"
static const char *const capabilities[] ALIGN_PTR = {
"chown",
"dac_override",
"dac_read_search",
"fowner",
"fsetid",
"kill",
"setgid",
"setuid",
"setpcap",
"linux_immutable",
"net_bind_service",
"net_broadcast",
"net_admin",
"net_raw",
"ipc_lock",
"ipc_owner",
"sys_module",
"sys_rawio",
"sys_chroot",
"sys_ptrace",
"sys_pacct",
"sys_admin",
"sys_boot",
"sys_nice",
"sys_resource",
"sys_time",
"sys_tty_config",
"mknod",
"lease",
"audit_write",
"audit_control",
"setfcap",
"mac_override",
"mac_admin",
"syslog",
"wake_alarm",
"block_suspend",
"audit_read",
};
unsigned FAST_FUNC cap_name_to_number(const char *cap)
{
unsigned i, n;
if ((sscanf(cap, "cap_%u", &n)) == 1) {
i = n;
goto found;
}
for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
if (strcasecmp(capabilities[i], cap) == 0)
goto found;
}
bb_error_msg_and_die("unknown capability '%s'", cap);
found:
if (!cap_valid(i))
bb_error_msg_and_die("unknown capability '%s'", cap);
return i;
}
void FAST_FUNC printf_cap(const char *pfx, unsigned cap_no)
{
if (cap_no < ARRAY_SIZE(capabilities)) {
printf("%s%s", pfx, capabilities[cap_no]);
return;
}
printf("%scap_%u", pfx, cap_no);
}
DEFINE_STRUCT_CAPS;
void FAST_FUNC getcaps(void *arg)
{
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;
struct caps *caps = arg;
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:
libbb: reduce the overhead of single parameter bb_error_msg() calls Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower overhead call to bb_perror_msg() when only a string was being printed with no parameters. This saves space for some CPU architectures because it avoids the overhead of a call to a variadic function. However there has never been a simple version of bb_error_msg(), and since 2007 many new calls to bb_perror_msg() have been added that only take a single parameter and so could have been using bb_simple_perror_message(). This changeset introduces 'simple' versions of bb_info_msg(), bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and bb_herror_msg_and_die(), and replaces all calls that only take a single parameter, or use something like ("%s", arg), with calls to the corresponding 'simple' version. Since it is likely that single parameter calls to the variadic functions may be accidentally reintroduced in the future a new debugging config option WARN_SIMPLE_MSG has been introduced. This uses some macro magic which will cause any such calls to generate a warning, but this is turned off by default to avoid use of the unpleasant macros in normal circumstances. This is a large changeset due to the number of calls that have been replaced. The only files that contain changes other than simple substitution of function calls are libbb.h, libbb/herror_msg.c, libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c, networking/udhcp/common.h and util-linux/mdev.c additonal macros have been added for logging so that single parameter and multiple parameter logging variants exist. The amount of space saved varies considerably by architecture, and was found to be as follows (for 'defconfig' using GCC 7.4): Arm: -92 bytes MIPS: -52 bytes PPC: -1836 bytes x86_64: -938 bytes Note that for the MIPS architecture only an exception had to be made disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h) because it made these files larger on MIPS. Signed-off-by: James Byrne <james.byrne@origamienergy.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
bb_simple_error_msg_and_die("unsupported capability version");
}
if (capget(&caps->header, caps->data) != 0)
bb_simple_perror_msg_and_die("capget");
}