1 /* vi: set sw=4 ts=4: */
3 * setpriv implementation for busybox based on linux-utils-ng 2.29
5 * Copyright (C) 2017 by <assafgordon@gmail.com>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 //config:config SETPRIV
11 //config: bool "setpriv"
13 //config: select PLATFORM_LINUX
14 //config: select LONG_OPTS
16 //config: Run a program with different Linux privilege settings.
17 //config: Requires kernel >= 3.5
19 //config:config FEATURE_SETPRIV_DUMP
20 //config: bool "Support dumping current privilege state"
22 //config: depends on SETPRIV
24 //config: Enables the "--dump" switch to print out the current privilege
25 //config: state. This is helpful for diagnosing problems.
27 //applet:IF_SETPRIV(APPLET(setpriv, BB_DIR_BIN, BB_SUID_DROP))
29 //kbuild:lib-$(CONFIG_SETPRIV) += setpriv.o
31 //usage:#define setpriv_trivial_usage
32 //usage: "[OPTIONS] PROG [ARGS]"
33 //usage:#define setpriv_full_usage "\n\n"
34 //usage: "Run PROG with different privilege settings\n"
35 //usage: IF_FEATURE_SETPRIV_DUMP(
36 //usage: "\n-d,--dump Show current capabilities"
38 //usage: "\n--nnp,--no-new-privs Ignore setuid/setgid bits and file capabilities"
40 //setpriv from util-linux 2.28:
41 // -d, --dump show current state (and do not exec anything)
42 // --nnp, --no-new-privs disallow granting new privileges
43 // --inh-caps <caps,...> set inheritable capabilities
44 // --bounding-set <caps> set capability bounding set
45 // --ruid <uid> set real uid
46 // --euid <uid> set effective uid
47 // --rgid <gid> set real gid
48 // --egid <gid> set effective gid
49 // --reuid <uid> set real and effective uid
50 // --regid <gid> set real and effective gid
51 // --clear-groups clear supplementary groups
52 // --keep-groups keep supplementary groups
53 // --groups <group,...> set supplementary groups
54 // --securebits <bits> set securebits
55 // --selinux-label <label> set SELinux label
56 // --apparmor-profile <pr> set AppArmor profile
58 #include <sys/prctl.h>
61 #ifndef PR_SET_NO_NEW_PRIVS
62 #define PR_SET_NO_NEW_PRIVS 38
65 #ifndef PR_GET_NO_NEW_PRIVS
66 #define PR_GET_NO_NEW_PRIVS 39
70 IF_FEATURE_SETPRIV_DUMP(OPTBIT_DUMP,)
73 IF_FEATURE_SETPRIV_DUMP(OPT_DUMP = (1 << OPTBIT_DUMP),)
74 OPT_NNP = (1 << OPTBIT_NNP),
77 #if ENABLE_FEATURE_SETPRIV_DUMP
80 uid_t ruid, euid, suid;
81 gid_t rgid, egid, sgid;
85 getresuid(&ruid, &euid, &suid); /* never fails in Linux */
86 getresgid(&rgid, &egid, &sgid); /* never fails in Linux */
88 gids = bb_getgroups(&ngids, NULL); /* never fails in Linux */
90 nnp = prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0);
92 bb_simple_perror_msg_and_die("prctl: GET_NO_NEW_PRIVS");
94 printf("uid: %u\n", (unsigned)ruid);
95 printf("euid: %u\n", (unsigned)euid);
96 printf("gid: %u\n", (unsigned)rgid);
97 printf("egid: %u\n", (unsigned)egid);
99 printf("Supplementary groups: ");
103 const char *fmt = ",%u" + 1;
105 for (i = 0; i < ngids; i++) {
106 printf(fmt, (unsigned)gids[i]);
110 printf("\nno_new_privs: %d\n", nnp);
112 if (ENABLE_FEATURE_CLEAN_UP)
116 #endif /* FEATURE_SETPRIV_DUMP */
118 int setpriv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
119 int setpriv_main(int argc UNUSED_PARAM, char **argv)
121 static const char setpriv_longopts[] ALIGN1 =
122 IF_FEATURE_SETPRIV_DUMP(
123 "dump\0" No_argument "d"
125 "nnp\0" No_argument "\xff"
126 "no-new-privs\0" No_argument "\xff"
130 applet_long_options = setpriv_longopts;
131 opts = getopt32(argv, "+"IF_FEATURE_SETPRIV_DUMP("d"));
134 #if ENABLE_FEATURE_SETPRIV_DUMP
135 if (opts & OPT_DUMP) {
136 if (argv[0] || (opts - OPT_DUMP) != 0)
141 if (opts & OPT_NNP) {
142 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
143 bb_simple_perror_msg_and_die("prctl: NO_NEW_PRIVS");
148 BB_EXECVP_or_die(argv);