runcon.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * runcon [ context |
  3. * ( [ -c ] [ -r role ] [-t type] [ -u user ] [ -l levelrange ] )
  4. * command [arg1 [arg2 ...] ]
  5. *
  6. * attempt to run the specified command with the specified context.
  7. *
  8. * -r role : use the current context with the specified role
  9. * -t type : use the current context with the specified type
  10. * -u user : use the current context with the specified user
  11. * -l level : use the current context with the specified level range
  12. * -c : compute process transition context before modifying
  13. *
  14. * Contexts are interpreted as follows:
  15. *
  16. * Number of MLS
  17. * components system?
  18. *
  19. * 1 - type
  20. * 2 - role:type
  21. * 3 Y role:type:range
  22. * 3 N user:role:type
  23. * 4 Y user:role:type:range
  24. * 4 N error
  25. *
  26. * Port to busybox: KaiGai Kohei <kaigai@kaigai.gr.jp>
  27. * - based on coreutils-5.97 (in Fedora Core 6)
  28. *
  29. * Licensed under GPLv2, see file LICENSE in this source tree.
  30. */
  31. //config:config RUNCON
  32. //config: bool "runcon"
  33. //config: default n
  34. //config: depends on SELINUX
  35. //config: help
  36. //config: Enable support to run command in specified security context.
  37. //config:
  38. //config:config FEATURE_RUNCON_LONG_OPTIONS
  39. //config: bool "Enable long options"
  40. //config: default y
  41. //config: depends on RUNCON && LONG_OPTS
  42. //config: help
  43. //config: Support long options for the runcon applet.
  44. //applet:IF_RUNCON(APPLET(runcon, BB_DIR_USR_BIN, BB_SUID_DROP))
  45. //kbuild:lib-$(CONFIG_RUNCON) += runcon.o
  46. //usage:#define runcon_trivial_usage
  47. //usage: "[-c] [-u USER] [-r ROLE] [-t TYPE] [-l RANGE] PROG ARGS\n"
  48. //usage: "runcon CONTEXT PROG ARGS"
  49. //usage:#define runcon_full_usage "\n\n"
  50. //usage: "Run PROG in a different security context\n"
  51. //usage: "\n CONTEXT Complete security context\n"
  52. //usage: IF_FEATURE_RUNCON_LONG_OPTIONS(
  53. //usage: "\n -c,--compute Compute process transition context before modifying"
  54. //usage: "\n -t,--type=TYPE Type (for same role as parent)"
  55. //usage: "\n -u,--user=USER User identity"
  56. //usage: "\n -r,--role=ROLE Role"
  57. //usage: "\n -l,--range=RNG Levelrange"
  58. //usage: )
  59. //usage: IF_NOT_FEATURE_RUNCON_LONG_OPTIONS(
  60. //usage: "\n -c Compute process transition context before modifying"
  61. //usage: "\n -t TYPE Type (for same role as parent)"
  62. //usage: "\n -u USER User identity"
  63. //usage: "\n -r ROLE Role"
  64. //usage: "\n -l RNG Levelrange"
  65. //usage: )
  66. #include <selinux/context.h>
  67. /* from deprecated <selinux/flask.h>: */
  68. #undef SECCLASS_PROCESS
  69. #define SECCLASS_PROCESS 2
  70. #include "libbb.h"
  71. static context_t runcon_compute_new_context(char *user, char *role, char *type, char *range,
  72. char *command, int compute_trans)
  73. {
  74. context_t con;
  75. security_context_t cur_context;
  76. if (getcon(&cur_context))
  77. bb_error_msg_and_die("can't get current context");
  78. if (compute_trans) {
  79. security_context_t file_context, new_context;
  80. if (getfilecon(command, &file_context) < 0)
  81. bb_error_msg_and_die("can't retrieve attributes of '%s'",
  82. command);
  83. if (security_compute_create(cur_context, file_context,
  84. SECCLASS_PROCESS, &new_context))
  85. bb_error_msg_and_die("unable to compute a new context");
  86. cur_context = new_context;
  87. }
  88. con = context_new(cur_context);
  89. if (!con)
  90. bb_error_msg_and_die("'%s' is not a valid context", cur_context);
  91. if (user && context_user_set(con, user))
  92. bb_error_msg_and_die("can't set new user '%s'", user);
  93. if (type && context_type_set(con, type))
  94. bb_error_msg_and_die("can't set new type '%s'", type);
  95. if (range && context_range_set(con, range))
  96. bb_error_msg_and_die("can't set new range '%s'", range);
  97. if (role && context_role_set(con, role))
  98. bb_error_msg_and_die("can't set new role '%s'", role);
  99. return con;
  100. }
  101. #if ENABLE_FEATURE_RUNCON_LONG_OPTIONS
  102. static const char runcon_longopts[] ALIGN1 =
  103. "user\0" Required_argument "u"
  104. "role\0" Required_argument "r"
  105. "type\0" Required_argument "t"
  106. "range\0" Required_argument "l"
  107. "compute\0" No_argument "c"
  108. "help\0" No_argument "h"
  109. ;
  110. #endif
  111. #define OPTS_ROLE (1<<0) /* r */
  112. #define OPTS_TYPE (1<<1) /* t */
  113. #define OPTS_USER (1<<2) /* u */
  114. #define OPTS_RANGE (1<<3) /* l */
  115. #define OPTS_COMPUTE (1<<4) /* c */
  116. #define OPTS_HELP (1<<5) /* h */
  117. #define OPTS_CONTEXT_COMPONENT (OPTS_ROLE | OPTS_TYPE | OPTS_USER | OPTS_RANGE)
  118. int runcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  119. int runcon_main(int argc UNUSED_PARAM, char **argv)
  120. {
  121. char *role = NULL;
  122. char *range = NULL;
  123. char *user = NULL;
  124. char *type = NULL;
  125. char *context = NULL;
  126. unsigned opts;
  127. context_t con;
  128. selinux_or_die();
  129. #if ENABLE_FEATURE_RUNCON_LONG_OPTIONS
  130. applet_long_options = runcon_longopts;
  131. #endif
  132. opt_complementary = "-1";
  133. opts = getopt32(argv, "r:t:u:l:ch", &role, &type, &user, &range);
  134. argv += optind;
  135. if (!(opts & OPTS_CONTEXT_COMPONENT)) {
  136. context = *argv++;
  137. if (!argv[0])
  138. bb_error_msg_and_die("no command given");
  139. }
  140. if (context) {
  141. con = context_new(context);
  142. if (!con)
  143. bb_error_msg_and_die("'%s' is not a valid context", context);
  144. } else {
  145. con = runcon_compute_new_context(user, role, type, range,
  146. argv[0], opts & OPTS_COMPUTE);
  147. }
  148. if (security_check_context(context_str(con)))
  149. bb_error_msg_and_die("'%s' is not a valid context",
  150. context_str(con));
  151. if (setexeccon(context_str(con)))
  152. bb_error_msg_and_die("can't set up security context '%s'",
  153. context_str(con));
  154. BB_EXECVP_or_die(argv);
  155. }