|
Packit |
3a1417 |
/*
|
|
Packit |
3a1417 |
* This was written by Andrew G. Morgan <morgan@kernel.org>
|
|
Packit |
3a1417 |
*
|
|
Packit |
3a1417 |
* This is a program that is intended to exec a subsequent program.
|
|
Packit |
3a1417 |
* The purpose of this 'execcap' wrapper is to limit the inheritable
|
|
Packit |
3a1417 |
* capabilities of the exec()'d program. All environment variables
|
|
Packit |
3a1417 |
* are inherited.
|
|
Packit |
3a1417 |
*/
|
|
Packit |
3a1417 |
|
|
Packit |
3a1417 |
#include <sys/types.h>
|
|
Packit |
3a1417 |
#include <errno.h>
|
|
Packit |
3a1417 |
#include <stdio.h>
|
|
Packit |
3a1417 |
#include <sys/capability.h>
|
|
Packit |
3a1417 |
#include <unistd.h>
|
|
Packit |
3a1417 |
#include <string.h>
|
|
Packit |
3a1417 |
#include <stdlib.h>
|
|
Packit |
3a1417 |
|
|
Packit |
3a1417 |
static void usage(void)
|
|
Packit |
3a1417 |
{
|
|
Packit |
3a1417 |
fprintf(stderr,
|
|
Packit |
3a1417 |
"usage: execcap <caps> <command-path> [command-args...]\n\n"
|
|
Packit |
3a1417 |
" This program is a wrapper that can be used to limit the Inheritable\n"
|
|
Packit |
3a1417 |
" capabilities of a program to be executed. Note, this wrapper is\n"
|
|
Packit |
3a1417 |
" intended to assist in overcoming a lack of support for filesystem\n"
|
|
Packit |
3a1417 |
" capability attributes and should be used to launch other files.\n"
|
|
Packit |
3a1417 |
" This program should _NOT_ be made setuid-0.\n\n"
|
|
Packit |
3a1417 |
"[Copyright (c) 1998 Andrew G. Morgan <morgan@kernel.org>]\n");
|
|
Packit |
3a1417 |
|
|
Packit |
3a1417 |
exit(1);
|
|
Packit |
3a1417 |
}
|
|
Packit |
3a1417 |
|
|
Packit |
3a1417 |
int main(int argc, char **argv)
|
|
Packit |
3a1417 |
{
|
|
Packit |
3a1417 |
cap_t new_caps;
|
|
Packit |
3a1417 |
|
|
Packit |
3a1417 |
/* this program should not be made setuid-0 */
|
|
Packit |
3a1417 |
if (getuid() && !geteuid()) {
|
|
Packit |
3a1417 |
usage();
|
|
Packit |
3a1417 |
}
|
|
Packit |
3a1417 |
|
|
Packit |
3a1417 |
/* check that we have at least 2 arguments */
|
|
Packit |
3a1417 |
if (argc < 3) {
|
|
Packit |
3a1417 |
usage();
|
|
Packit |
3a1417 |
}
|
|
Packit |
3a1417 |
|
|
Packit |
3a1417 |
/* parse the first argument to obtain a set of capabilities */
|
|
Packit |
3a1417 |
new_caps = cap_from_text(argv[1]);
|
|
Packit |
3a1417 |
if (new_caps == NULL) {
|
|
Packit |
3a1417 |
fprintf(stderr, "requested capabilities were not recognized\n");
|
|
Packit |
3a1417 |
usage();
|
|
Packit |
3a1417 |
}
|
|
Packit |
3a1417 |
|
|
Packit |
3a1417 |
/* set these capabilities for the current process */
|
|
Packit |
3a1417 |
if (cap_set_proc(new_caps) != 0) {
|
|
Packit |
3a1417 |
fprintf(stderr, "unable to set capabilities: %s\n", strerror(errno));
|
|
Packit |
3a1417 |
usage();
|
|
Packit |
3a1417 |
}
|
|
Packit |
3a1417 |
|
|
Packit |
3a1417 |
/* exec the program indicated by args 2 ... */
|
|
Packit |
3a1417 |
execvp(argv[2], argv+2);
|
|
Packit |
3a1417 |
|
|
Packit |
3a1417 |
/* if we fall through to here, our exec failed -- announce the fact */
|
|
Packit |
3a1417 |
fprintf(stderr, "Unable to execute command: %s\n", strerror(errno));
|
|
Packit |
3a1417 |
|
|
Packit |
3a1417 |
usage();
|
|
Packit |
3a1417 |
|
|
Packit |
3a1417 |
return 0;
|
|
Packit |
3a1417 |
}
|