3
0

runcon.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include <getopt.h>
  30. #include <selinux/context.h>
  31. #include <selinux/flask.h>
  32. #include "libbb.h"
  33. static context_t runcon_compute_new_context(char *user, char *role, char *type, char *range,
  34. char *command, int compute_trans)
  35. {
  36. context_t con;
  37. security_context_t cur_context;
  38. if (getcon(&cur_context))
  39. bb_error_msg_and_die("cannot get current context");
  40. if (compute_trans) {
  41. security_context_t file_context, new_context;
  42. if (getfilecon(command, &file_context) < 0)
  43. bb_error_msg_and_die("cannot retrieve attributes of '%s'",
  44. command);
  45. if (security_compute_create(cur_context, file_context,
  46. SECCLASS_PROCESS, &new_context))
  47. bb_error_msg_and_die("unable to compute a new context");
  48. cur_context = new_context;
  49. }
  50. con = context_new(cur_context);
  51. if (!con)
  52. bb_error_msg_and_die("'%s' is not a valid context", cur_context);
  53. if (user && context_user_set(con, user))
  54. bb_error_msg_and_die("failed to set new user '%s'", user);
  55. if (type && context_type_set(con, type))
  56. bb_error_msg_and_die("failed to set new type '%s'", type);
  57. if (range && context_range_set(con, range))
  58. bb_error_msg_and_die("failed to set new range '%s'", range);
  59. if (role && context_role_set(con, role))
  60. bb_error_msg_and_die("failed to set new role '%s'", role);
  61. return con;
  62. }
  63. #if ENABLE_FEATURE_RUNCON_LONG_OPTIONS
  64. static const char runcon_longopts[] ALIGN1 =
  65. "user\0" Required_argument "u"
  66. "role\0" Required_argument "r"
  67. "type\0" Required_argument "t"
  68. "range\0" Required_argument "l"
  69. "compute\0" No_argument "c"
  70. "help\0" No_argument "h"
  71. ;
  72. #endif
  73. #define OPTS_ROLE (1<<0) /* r */
  74. #define OPTS_TYPE (1<<1) /* t */
  75. #define OPTS_USER (1<<2) /* u */
  76. #define OPTS_RANGE (1<<3) /* l */
  77. #define OPTS_COMPUTE (1<<4) /* c */
  78. #define OPTS_HELP (1<<5) /* h */
  79. #define OPTS_CONTEXT_COMPONENT (OPTS_ROLE | OPTS_TYPE | OPTS_USER | OPTS_RANGE)
  80. int runcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  81. int runcon_main(int argc ATTRIBUTE_UNUSED, char **argv)
  82. {
  83. char *role = NULL;
  84. char *range = NULL;
  85. char *user = NULL;
  86. char *type = NULL;
  87. char *context = NULL;
  88. unsigned opts;
  89. context_t con;
  90. selinux_or_die();
  91. #if ENABLE_FEATURE_RUNCON_LONG_OPTIONS
  92. applet_long_options = runcon_longopts;
  93. #endif
  94. opt_complementary = "-1";
  95. opts = getopt32(argv, "r:t:u:l:ch", &role, &type, &user, &range);
  96. argv += optind;
  97. if (!(opts & OPTS_CONTEXT_COMPONENT)) {
  98. context = *argv++;
  99. if (!argv[0])
  100. bb_error_msg_and_die("no command given");
  101. }
  102. if (context) {
  103. con = context_new(context);
  104. if (!con)
  105. bb_error_msg_and_die("'%s' is not a valid context", context);
  106. } else {
  107. con = runcon_compute_new_context(user, role, type, range,
  108. argv[0], opts & OPTS_COMPUTE);
  109. }
  110. if (security_check_context(context_str(con)))
  111. bb_error_msg_and_die("'%s' is not a valid context",
  112. context_str(con));
  113. if (setexeccon(context_str(con)))
  114. bb_error_msg_and_die("cannot set up security context '%s'",
  115. context_str(con));
  116. execvp(argv[0], argv);
  117. bb_perror_msg_and_die("cannot execute '%s'", argv[0]);
  118. }